mac-screen-capture.m 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. #include <AvailabilityMacros.h>
  2. #include <Cocoa/Cocoa.h>
  3. bool is_screen_capture_available(void)
  4. {
  5. if (@available(macOS 12.5, *)) {
  6. return true;
  7. } else {
  8. return false;
  9. }
  10. }
  11. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 120300 // __MAC_12_3
  12. #pragma clang diagnostic push
  13. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  14. #include <stdlib.h>
  15. #include <obs-module.h>
  16. #include <util/threading.h>
  17. #include <pthread.h>
  18. #include <IOSurface/IOSurface.h>
  19. #include <ScreenCaptureKit/ScreenCaptureKit.h>
  20. #include <CoreMedia/CMSampleBuffer.h>
  21. #include <CoreVideo/CVPixelBuffer.h>
  22. #define MACCAP_LOG(level, msg, ...) \
  23. blog(level, "[ mac-screencapture ]: " msg, ##__VA_ARGS__)
  24. #define MACCAP_ERR(msg, ...) MACCAP_LOG(LOG_ERROR, msg, ##__VA_ARGS__)
  25. typedef enum {
  26. ScreenCaptureDisplayStream = 0,
  27. ScreenCaptureWindowStream = 1,
  28. ScreenCaptureApplicationStream = 2,
  29. } ScreenCaptureStreamType;
  30. @interface ScreenCaptureDelegate : NSObject <SCStreamOutput>
  31. @property struct screen_capture *sc;
  32. @end
  33. struct screen_capture {
  34. obs_source_t *source;
  35. gs_samplerstate_t *sampler;
  36. gs_effect_t *effect;
  37. gs_texture_t *tex;
  38. gs_vertbuffer_t *vertbuf;
  39. NSRect frame;
  40. bool hide_cursor;
  41. bool show_hidden_windows;
  42. bool show_empty_names;
  43. SCStream *disp;
  44. SCStreamConfiguration *stream_properties;
  45. SCShareableContent *shareable_content;
  46. ScreenCaptureDelegate *capture_delegate;
  47. os_event_t *disp_finished;
  48. os_event_t *stream_start_completed;
  49. os_sem_t *shareable_content_available;
  50. IOSurfaceRef current, prev;
  51. pthread_mutex_t mutex;
  52. unsigned capture_type;
  53. CGDirectDisplayID display;
  54. CGWindowID window;
  55. NSString *application_id;
  56. };
  57. static void destroy_screen_stream(struct screen_capture *sc)
  58. {
  59. if (sc->disp) {
  60. [sc->disp stopCaptureWithCompletionHandler:^(
  61. NSError *_Nullable error) {
  62. if (error && error.code != 3808) {
  63. MACCAP_ERR(
  64. "destroy_screen_stream: Failed to stop stream with error %s\n",
  65. [[error localizedFailureReason]
  66. cStringUsingEncoding:
  67. NSUTF8StringEncoding]);
  68. }
  69. os_event_signal(sc->disp_finished);
  70. }];
  71. os_event_wait(sc->disp_finished);
  72. }
  73. if (sc->stream_properties) {
  74. [sc->stream_properties release];
  75. sc->stream_properties = NULL;
  76. }
  77. if (sc->tex) {
  78. gs_texture_destroy(sc->tex);
  79. sc->tex = NULL;
  80. }
  81. if (sc->current) {
  82. IOSurfaceDecrementUseCount(sc->current);
  83. CFRelease(sc->current);
  84. sc->current = NULL;
  85. }
  86. if (sc->prev) {
  87. IOSurfaceDecrementUseCount(sc->prev);
  88. CFRelease(sc->prev);
  89. sc->prev = NULL;
  90. }
  91. if (sc->disp) {
  92. [sc->disp release];
  93. sc->disp = NULL;
  94. }
  95. os_event_destroy(sc->disp_finished);
  96. os_event_destroy(sc->stream_start_completed);
  97. }
  98. static void screen_capture_destroy(void *data)
  99. {
  100. struct screen_capture *sc = data;
  101. if (!sc)
  102. return;
  103. obs_enter_graphics();
  104. destroy_screen_stream(sc);
  105. if (sc->sampler)
  106. gs_samplerstate_destroy(sc->sampler);
  107. if (sc->vertbuf)
  108. gs_vertexbuffer_destroy(sc->vertbuf);
  109. obs_leave_graphics();
  110. if (sc->shareable_content) {
  111. os_sem_wait(sc->shareable_content_available);
  112. [sc->shareable_content release];
  113. os_sem_destroy(sc->shareable_content_available);
  114. sc->shareable_content_available = NULL;
  115. }
  116. if (sc->capture_delegate) {
  117. [sc->capture_delegate release];
  118. }
  119. pthread_mutex_destroy(&sc->mutex);
  120. bfree(sc);
  121. }
  122. static inline void screen_stream_video_update(struct screen_capture *sc,
  123. CMSampleBufferRef sample_buffer)
  124. {
  125. bool frame_detail_errored = false;
  126. float scale_factor = 1.0f;
  127. CGRect window_rect = {};
  128. CFArrayRef attachments_array =
  129. CMSampleBufferGetSampleAttachmentsArray(sample_buffer, false);
  130. if (sc->capture_type == ScreenCaptureWindowStream &&
  131. attachments_array != NULL &&
  132. CFArrayGetCount(attachments_array) > 0) {
  133. CFDictionaryRef attachments_dict =
  134. CFArrayGetValueAtIndex(attachments_array, 0);
  135. if (attachments_dict != NULL) {
  136. CFTypeRef frame_scale_factor = CFDictionaryGetValue(
  137. attachments_dict, SCStreamFrameInfoScaleFactor);
  138. if (frame_scale_factor != NULL) {
  139. Boolean result = CFNumberGetValue(
  140. (CFNumberRef)frame_scale_factor,
  141. kCFNumberFloatType, &scale_factor);
  142. if (result == false) {
  143. scale_factor = 1.0f;
  144. frame_detail_errored = true;
  145. }
  146. }
  147. CFTypeRef content_rect_dict = CFDictionaryGetValue(
  148. attachments_dict, SCStreamFrameInfoContentRect);
  149. CFTypeRef content_scale_factor = CFDictionaryGetValue(
  150. attachments_dict,
  151. SCStreamFrameInfoContentScale);
  152. if ((content_rect_dict != NULL) &&
  153. (content_scale_factor != NULL)) {
  154. CGRect content_rect = {};
  155. float points_to_pixels = 0.0f;
  156. Boolean result =
  157. CGRectMakeWithDictionaryRepresentation(
  158. (__bridge CFDictionaryRef)
  159. content_rect_dict,
  160. &content_rect);
  161. if (result == false) {
  162. content_rect = CGRectZero;
  163. frame_detail_errored = true;
  164. }
  165. result = CFNumberGetValue(
  166. (CFNumberRef)content_scale_factor,
  167. kCFNumberFloatType, &points_to_pixels);
  168. if (result == false) {
  169. points_to_pixels = 1.0f;
  170. frame_detail_errored = true;
  171. }
  172. window_rect.origin = content_rect.origin;
  173. window_rect.size.width =
  174. content_rect.size.width /
  175. points_to_pixels * scale_factor;
  176. window_rect.size.height =
  177. content_rect.size.height /
  178. points_to_pixels * scale_factor;
  179. }
  180. }
  181. }
  182. CVImageBufferRef image_buffer =
  183. CMSampleBufferGetImageBuffer(sample_buffer);
  184. CVPixelBufferLockBaseAddress(image_buffer, 0);
  185. IOSurfaceRef frame_surface = CVPixelBufferGetIOSurface(image_buffer);
  186. CVPixelBufferUnlockBaseAddress(image_buffer, 0);
  187. IOSurfaceRef prev_current = NULL;
  188. if (frame_surface && !pthread_mutex_lock(&sc->mutex)) {
  189. bool needs_to_update_properties = false;
  190. if (!frame_detail_errored) {
  191. if (sc->capture_type == ScreenCaptureWindowStream) {
  192. if ((sc->frame.size.width !=
  193. window_rect.size.width) ||
  194. (sc->frame.size.height !=
  195. window_rect.size.height)) {
  196. sc->frame.size.width =
  197. window_rect.size.width;
  198. sc->frame.size.height =
  199. window_rect.size.height;
  200. needs_to_update_properties = true;
  201. }
  202. } else {
  203. size_t width =
  204. CVPixelBufferGetWidth(image_buffer);
  205. size_t height =
  206. CVPixelBufferGetHeight(image_buffer);
  207. if ((sc->frame.size.width != width) ||
  208. (sc->frame.size.height != height)) {
  209. sc->frame.size.width = width;
  210. sc->frame.size.height = height;
  211. needs_to_update_properties = true;
  212. }
  213. }
  214. }
  215. if (needs_to_update_properties) {
  216. [sc->stream_properties setWidth:sc->frame.size.width];
  217. [sc->stream_properties setHeight:sc->frame.size.height];
  218. [sc->disp
  219. updateConfiguration:sc->stream_properties
  220. completionHandler:^(
  221. NSError *_Nullable error) {
  222. if (error) {
  223. MACCAP_ERR(
  224. "screen_stream_video_update: Failed to update stream properties with error %s\n",
  225. [[error localizedFailureReason]
  226. cStringUsingEncoding:
  227. NSUTF8StringEncoding]);
  228. }
  229. }];
  230. }
  231. prev_current = sc->current;
  232. sc->current = frame_surface;
  233. CFRetain(sc->current);
  234. IOSurfaceIncrementUseCount(sc->current);
  235. pthread_mutex_unlock(&sc->mutex);
  236. }
  237. if (prev_current) {
  238. IOSurfaceDecrementUseCount(prev_current);
  239. CFRelease(prev_current);
  240. }
  241. }
  242. static inline void screen_stream_audio_update(struct screen_capture *sc,
  243. CMSampleBufferRef sample_buffer)
  244. {
  245. CMFormatDescriptionRef format_description =
  246. CMSampleBufferGetFormatDescription(sample_buffer);
  247. const AudioStreamBasicDescription *audio_description =
  248. CMAudioFormatDescriptionGetStreamBasicDescription(
  249. format_description);
  250. char *_Nullable bytes = NULL;
  251. CMBlockBufferRef data_buffer =
  252. CMSampleBufferGetDataBuffer(sample_buffer);
  253. size_t data_buffer_length = CMBlockBufferGetDataLength(data_buffer);
  254. CMBlockBufferGetDataPointer(data_buffer, 0, &data_buffer_length, NULL,
  255. &bytes);
  256. CMTime presentation_time =
  257. CMSampleBufferGetOutputPresentationTimeStamp(sample_buffer);
  258. struct obs_source_audio audio_data = {};
  259. for (uint32_t channel_idx = 0;
  260. channel_idx < audio_description->mChannelsPerFrame;
  261. ++channel_idx) {
  262. uint32_t offset =
  263. (uint32_t)(data_buffer_length /
  264. audio_description->mChannelsPerFrame) *
  265. channel_idx;
  266. audio_data.data[channel_idx] = (uint8_t *)bytes + offset;
  267. }
  268. audio_data.frames = (uint32_t)(data_buffer_length /
  269. audio_description->mBytesPerFrame /
  270. audio_description->mChannelsPerFrame);
  271. audio_data.speakers = audio_description->mChannelsPerFrame;
  272. audio_data.samples_per_sec = audio_description->mSampleRate;
  273. audio_data.timestamp =
  274. CMTimeGetSeconds(presentation_time) * NSEC_PER_SEC;
  275. audio_data.format = AUDIO_FORMAT_FLOAT_PLANAR;
  276. obs_source_output_audio(sc->source, &audio_data);
  277. }
  278. static bool init_screen_stream(struct screen_capture *sc)
  279. {
  280. SCContentFilter *content_filter;
  281. sc->frame = CGRectZero;
  282. sc->stream_properties = [[SCStreamConfiguration alloc] init];
  283. os_sem_wait(sc->shareable_content_available);
  284. SCDisplay * (^get_target_display)() = ^SCDisplay *()
  285. {
  286. __block SCDisplay *target_display = nil;
  287. [sc->shareable_content.displays
  288. indexOfObjectPassingTest:^BOOL(
  289. SCDisplay *_Nonnull display, NSUInteger idx,
  290. BOOL *_Nonnull stop) {
  291. if (display.displayID == sc->display) {
  292. target_display = sc->shareable_content
  293. .displays[idx];
  294. *stop = TRUE;
  295. }
  296. return *stop;
  297. }];
  298. return target_display;
  299. };
  300. void (^set_display_mode)(struct screen_capture *, SCDisplay *) = ^void(
  301. struct screen_capture *sc, SCDisplay *target_display) {
  302. CGDisplayModeRef display_mode =
  303. CGDisplayCopyDisplayMode(target_display.displayID);
  304. [sc->stream_properties
  305. setWidth:CGDisplayModeGetPixelWidth(display_mode)];
  306. [sc->stream_properties
  307. setHeight:CGDisplayModeGetPixelHeight(display_mode)];
  308. CGDisplayModeRelease(display_mode);
  309. };
  310. switch (sc->capture_type) {
  311. case ScreenCaptureDisplayStream: {
  312. SCDisplay *target_display = get_target_display();
  313. if (@available(macOS 13.0, *)) {
  314. content_filter = [[SCContentFilter alloc]
  315. initWithDisplay:target_display
  316. excludingWindows:[[NSArray alloc] init]];
  317. } else {
  318. NSArray *excluded = [sc->shareable_content.applications
  319. filteredArrayUsingPredicate:
  320. [NSPredicate predicateWithBlock:^BOOL(
  321. SCRunningApplication
  322. *application,
  323. NSDictionary<
  324. NSString *,
  325. id>
  326. *_Nullable bindings
  327. __attribute__((
  328. unused))) {
  329. return [application
  330. .bundleIdentifier
  331. isEqualToString:
  332. @"com.apple.controlcenter"];
  333. }]];
  334. content_filter = [[SCContentFilter alloc]
  335. initWithDisplay:target_display
  336. excludingApplications:excluded
  337. exceptingWindows:[[NSArray alloc] init]];
  338. }
  339. set_display_mode(sc, target_display);
  340. } break;
  341. case ScreenCaptureWindowStream: {
  342. __block SCWindow *target_window = nil;
  343. if (sc->window != 0) {
  344. [sc->shareable_content.windows
  345. indexOfObjectPassingTest:^BOOL(
  346. SCWindow *_Nonnull window,
  347. NSUInteger idx, BOOL *_Nonnull stop) {
  348. if (window.windowID == sc->window) {
  349. target_window =
  350. sc->shareable_content
  351. .windows[idx];
  352. *stop = TRUE;
  353. }
  354. return *stop;
  355. }];
  356. } else {
  357. target_window =
  358. [sc->shareable_content.windows objectAtIndex:0];
  359. sc->window = target_window.windowID;
  360. }
  361. content_filter = [[SCContentFilter alloc]
  362. initWithDesktopIndependentWindow:target_window];
  363. if (target_window) {
  364. [sc->stream_properties
  365. setWidth:target_window.frame.size.width];
  366. [sc->stream_properties
  367. setHeight:target_window.frame.size.height];
  368. }
  369. } break;
  370. case ScreenCaptureApplicationStream: {
  371. SCDisplay *target_display = get_target_display();
  372. __block SCRunningApplication *target_application = nil;
  373. {
  374. [sc->shareable_content.applications
  375. indexOfObjectPassingTest:^BOOL(
  376. SCRunningApplication
  377. *_Nonnull application,
  378. NSUInteger idx, BOOL *_Nonnull stop) {
  379. if ([application.bundleIdentifier
  380. isEqualToString:
  381. sc->
  382. application_id]) {
  383. target_application =
  384. sc->shareable_content
  385. .applications
  386. [idx];
  387. *stop = TRUE;
  388. }
  389. return *stop;
  390. }];
  391. }
  392. NSArray *target_application_array = [[NSArray alloc]
  393. initWithObjects:target_application, nil];
  394. content_filter = [[SCContentFilter alloc]
  395. initWithDisplay:target_display
  396. includingApplications:target_application_array
  397. exceptingWindows:[[NSArray alloc] init]];
  398. if (@available(macOS 13.0, *))
  399. [sc->stream_properties
  400. setBackgroundColor:CGColorGetConstantColor(
  401. kCGColorClear)];
  402. set_display_mode(sc, target_display);
  403. } break;
  404. }
  405. os_sem_post(sc->shareable_content_available);
  406. [sc->stream_properties setQueueDepth:8];
  407. [sc->stream_properties setShowsCursor:!sc->hide_cursor];
  408. [sc->stream_properties setColorSpaceName:kCGColorSpaceSRGB];
  409. [sc->stream_properties setPixelFormat:'BGRA'];
  410. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000
  411. if (@available(macOS 13.0, *)) {
  412. [sc->stream_properties setCapturesAudio:TRUE];
  413. [sc->stream_properties setExcludesCurrentProcessAudio:TRUE];
  414. [sc->stream_properties setChannelCount:2];
  415. }
  416. #endif
  417. sc->disp = [[SCStream alloc] initWithFilter:content_filter
  418. configuration:sc->stream_properties
  419. delegate:nil];
  420. NSError *error = nil;
  421. BOOL did_add_output = [sc->disp addStreamOutput:sc->capture_delegate
  422. type:SCStreamOutputTypeScreen
  423. sampleHandlerQueue:nil
  424. error:&error];
  425. if (!did_add_output) {
  426. MACCAP_ERR(
  427. "init_screen_stream: Failed to add stream output with error %s\n",
  428. [[error localizedFailureReason]
  429. cStringUsingEncoding:NSUTF8StringEncoding]);
  430. [error release];
  431. return !did_add_output;
  432. }
  433. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000
  434. if (__builtin_available(macOS 13.0, *)) {
  435. did_add_output = [sc->disp
  436. addStreamOutput:sc->capture_delegate
  437. type:SCStreamOutputTypeAudio
  438. sampleHandlerQueue:nil
  439. error:&error];
  440. if (!did_add_output) {
  441. MACCAP_ERR(
  442. "init_screen_stream: Failed to add audio stream output with error %s\n",
  443. [[error localizedFailureReason]
  444. cStringUsingEncoding:
  445. NSUTF8StringEncoding]);
  446. [error release];
  447. return !did_add_output;
  448. }
  449. }
  450. #endif
  451. os_event_init(&sc->disp_finished, OS_EVENT_TYPE_MANUAL);
  452. os_event_init(&sc->stream_start_completed, OS_EVENT_TYPE_MANUAL);
  453. __block BOOL did_stream_start = false;
  454. [sc->disp startCaptureWithCompletionHandler:^(
  455. NSError *_Nullable error) {
  456. did_stream_start = (BOOL)(error == nil);
  457. if (!did_stream_start) {
  458. MACCAP_ERR(
  459. "init_screen_stream: Failed to start capture with error %s\n",
  460. [[error localizedFailureReason]
  461. cStringUsingEncoding:
  462. NSUTF8StringEncoding]);
  463. // Clean up disp so it isn't stopped
  464. [sc->disp release];
  465. sc->disp = NULL;
  466. }
  467. os_event_signal(sc->stream_start_completed);
  468. }];
  469. os_event_wait(sc->stream_start_completed);
  470. return did_stream_start;
  471. }
  472. bool init_vertbuf_screen_capture(struct screen_capture *sc)
  473. {
  474. struct gs_vb_data *vb_data = gs_vbdata_create();
  475. vb_data->num = 4;
  476. vb_data->points = bzalloc(sizeof(struct vec3) * 4);
  477. if (!vb_data->points)
  478. return false;
  479. vb_data->num_tex = 1;
  480. vb_data->tvarray = bzalloc(sizeof(struct gs_tvertarray));
  481. if (!vb_data->tvarray)
  482. return false;
  483. vb_data->tvarray[0].width = 2;
  484. vb_data->tvarray[0].array = bzalloc(sizeof(struct vec2) * 4);
  485. if (!vb_data->tvarray[0].array)
  486. return false;
  487. sc->vertbuf = gs_vertexbuffer_create(vb_data, GS_DYNAMIC);
  488. return sc->vertbuf != NULL;
  489. }
  490. static void screen_capture_build_content_list(struct screen_capture *sc,
  491. bool display_capture)
  492. {
  493. typedef void (^shareable_content_callback)(SCShareableContent *,
  494. NSError *);
  495. shareable_content_callback new_content_received = ^void(
  496. SCShareableContent *shareable_content, NSError *error) {
  497. if (error == nil && sc->shareable_content_available != NULL) {
  498. sc->shareable_content = [shareable_content retain];
  499. } else {
  500. #ifdef DEBUG
  501. MACCAP_ERR(
  502. "screen_capture_properties: Failed to get shareable content with error %s\n",
  503. [[error localizedFailureReason]
  504. cStringUsingEncoding:
  505. NSUTF8StringEncoding]);
  506. #endif
  507. MACCAP_LOG(
  508. LOG_WARNING,
  509. "Unable to get list of available applications or windows. "
  510. "Please check if OBS has necessary screen capture permissions.");
  511. }
  512. os_sem_post(sc->shareable_content_available);
  513. };
  514. os_sem_wait(sc->shareable_content_available);
  515. [sc->shareable_content release];
  516. [SCShareableContent
  517. getShareableContentExcludingDesktopWindows:TRUE
  518. onScreenWindowsOnly:
  519. (display_capture
  520. ? FALSE
  521. : !sc->show_hidden_windows)
  522. completionHandler:new_content_received];
  523. }
  524. static void *screen_capture_create(obs_data_t *settings, obs_source_t *source)
  525. {
  526. struct screen_capture *sc = bzalloc(sizeof(struct screen_capture));
  527. sc->source = source;
  528. sc->hide_cursor = !obs_data_get_bool(settings, "show_cursor");
  529. sc->show_empty_names = obs_data_get_bool(settings, "show_empty_names");
  530. sc->show_hidden_windows =
  531. obs_data_get_bool(settings, "show_hidden_windows");
  532. sc->window = obs_data_get_int(settings, "window");
  533. sc->capture_type = obs_data_get_int(settings, "type");
  534. os_sem_init(&sc->shareable_content_available, 1);
  535. screen_capture_build_content_list(
  536. sc, sc->capture_type == ScreenCaptureDisplayStream);
  537. sc->capture_delegate = [[ScreenCaptureDelegate alloc] init];
  538. sc->capture_delegate.sc = sc;
  539. sc->effect = obs_get_base_effect(OBS_EFFECT_DEFAULT_RECT);
  540. if (!sc->effect)
  541. goto fail;
  542. obs_enter_graphics();
  543. struct gs_sampler_info info = {
  544. .filter = GS_FILTER_LINEAR,
  545. .address_u = GS_ADDRESS_CLAMP,
  546. .address_v = GS_ADDRESS_CLAMP,
  547. .address_w = GS_ADDRESS_CLAMP,
  548. .max_anisotropy = 1,
  549. };
  550. sc->sampler = gs_samplerstate_create(&info);
  551. if (!sc->sampler)
  552. goto fail;
  553. if (!init_vertbuf_screen_capture(sc))
  554. goto fail;
  555. obs_leave_graphics();
  556. sc->display = obs_data_get_int(settings, "display");
  557. sc->application_id = [[NSString alloc]
  558. initWithUTF8String:obs_data_get_string(settings,
  559. "application")];
  560. pthread_mutex_init(&sc->mutex, NULL);
  561. if (!init_screen_stream(sc))
  562. goto fail;
  563. return sc;
  564. fail:
  565. obs_leave_graphics();
  566. screen_capture_destroy(sc);
  567. return NULL;
  568. }
  569. static void build_sprite(struct gs_vb_data *data, float fcx, float fcy,
  570. float start_u, float end_u, float start_v, float end_v)
  571. {
  572. struct vec2 *tvarray = data->tvarray[0].array;
  573. vec3_set(data->points + 1, fcx, 0.0f, 0.0f);
  574. vec3_set(data->points + 2, 0.0f, fcy, 0.0f);
  575. vec3_set(data->points + 3, fcx, fcy, 0.0f);
  576. vec2_set(tvarray, start_u, start_v);
  577. vec2_set(tvarray + 1, end_u, start_v);
  578. vec2_set(tvarray + 2, start_u, end_v);
  579. vec2_set(tvarray + 3, end_u, end_v);
  580. }
  581. static inline void build_sprite_rect(struct gs_vb_data *data, float origin_x,
  582. float origin_y, float end_x, float end_y)
  583. {
  584. build_sprite(data, fabs(end_x - origin_x), fabs(end_y - origin_y),
  585. origin_x, end_x, origin_y, end_y);
  586. }
  587. static void screen_capture_video_tick(void *data,
  588. float seconds __attribute__((unused)))
  589. {
  590. struct screen_capture *sc = data;
  591. if (!sc->current)
  592. return;
  593. if (!obs_source_showing(sc->source))
  594. return;
  595. IOSurfaceRef prev_prev = sc->prev;
  596. if (pthread_mutex_lock(&sc->mutex))
  597. return;
  598. sc->prev = sc->current;
  599. sc->current = NULL;
  600. pthread_mutex_unlock(&sc->mutex);
  601. if (prev_prev == sc->prev)
  602. return;
  603. CGPoint origin = {0.f, 0.f};
  604. CGPoint end = {sc->frame.size.width, sc->frame.size.height};
  605. obs_enter_graphics();
  606. build_sprite_rect(gs_vertexbuffer_get_data(sc->vertbuf), origin.x,
  607. origin.y, end.x, end.y);
  608. if (sc->tex)
  609. gs_texture_rebind_iosurface(sc->tex, sc->prev);
  610. else
  611. sc->tex = gs_texture_create_from_iosurface(sc->prev);
  612. obs_leave_graphics();
  613. if (prev_prev) {
  614. IOSurfaceDecrementUseCount(prev_prev);
  615. CFRelease(prev_prev);
  616. }
  617. }
  618. static void screen_capture_video_render(void *data, gs_effect_t *effect
  619. __attribute__((unused)))
  620. {
  621. struct screen_capture *sc = data;
  622. if (!sc->tex)
  623. return;
  624. const bool linear_srgb = gs_get_linear_srgb();
  625. const bool previous = gs_framebuffer_srgb_enabled();
  626. gs_enable_framebuffer_srgb(linear_srgb);
  627. gs_vertexbuffer_flush(sc->vertbuf);
  628. gs_load_vertexbuffer(sc->vertbuf);
  629. gs_load_indexbuffer(NULL);
  630. gs_load_samplerstate(sc->sampler, 0);
  631. gs_technique_t *tech = gs_effect_get_technique(sc->effect, "Draw");
  632. gs_eparam_t *param = gs_effect_get_param_by_name(sc->effect, "image");
  633. if (linear_srgb)
  634. gs_effect_set_texture_srgb(param, sc->tex);
  635. else
  636. gs_effect_set_texture(param, sc->tex);
  637. gs_technique_begin(tech);
  638. gs_technique_begin_pass(tech, 0);
  639. gs_draw(GS_TRISTRIP, 0, 4);
  640. gs_technique_end_pass(tech);
  641. gs_technique_end(tech);
  642. gs_enable_framebuffer_srgb(previous);
  643. }
  644. static const char *screen_capture_getname(void *unused __attribute__((unused)))
  645. {
  646. return obs_module_text("SCK.Name");
  647. }
  648. static uint32_t screen_capture_getwidth(void *data)
  649. {
  650. struct screen_capture *sc = data;
  651. return sc->frame.size.width;
  652. }
  653. static uint32_t screen_capture_getheight(void *data)
  654. {
  655. struct screen_capture *sc = data;
  656. return sc->frame.size.height;
  657. }
  658. static void screen_capture_defaults(obs_data_t *settings)
  659. {
  660. CGDirectDisplayID initial_display = 0;
  661. {
  662. NSScreen *mainScreen = [NSScreen mainScreen];
  663. if (mainScreen) {
  664. NSNumber *screen_num =
  665. mainScreen.deviceDescription[@"NSScreenNumber"];
  666. if (screen_num) {
  667. initial_display =
  668. (CGDirectDisplayID)(uintptr_t)
  669. screen_num.pointerValue;
  670. }
  671. }
  672. }
  673. obs_data_set_default_int(settings, "type", 0);
  674. obs_data_set_default_int(settings, "display", initial_display);
  675. obs_data_set_default_int(settings, "window", kCGNullWindowID);
  676. obs_data_set_default_obj(settings, "application", NULL);
  677. obs_data_set_default_bool(settings, "show_cursor", true);
  678. obs_data_set_default_bool(settings, "show_empty_names", false);
  679. obs_data_set_default_bool(settings, "show_hidden_windows", false);
  680. }
  681. static void screen_capture_update(void *data, obs_data_t *settings)
  682. {
  683. struct screen_capture *sc = data;
  684. CGWindowID old_window_id = sc->window;
  685. CGWindowID new_window_id = obs_data_get_int(settings, "window");
  686. if (new_window_id > 0 && new_window_id != old_window_id)
  687. sc->window = new_window_id;
  688. ScreenCaptureStreamType capture_type =
  689. (ScreenCaptureStreamType)obs_data_get_int(settings, "type");
  690. CGDirectDisplayID display =
  691. (CGDirectDisplayID)obs_data_get_int(settings, "display");
  692. NSString *application_id = [[NSString alloc]
  693. initWithUTF8String:obs_data_get_string(settings,
  694. "application")];
  695. bool show_cursor = obs_data_get_bool(settings, "show_cursor");
  696. bool show_empty_names = obs_data_get_bool(settings, "show_empty_names");
  697. bool show_hidden_windows =
  698. obs_data_get_bool(settings, "show_hidden_windows");
  699. if (capture_type == sc->capture_type) {
  700. switch (sc->capture_type) {
  701. case ScreenCaptureDisplayStream: {
  702. if (sc->display == display &&
  703. sc->hide_cursor != show_cursor)
  704. return;
  705. } break;
  706. case ScreenCaptureWindowStream: {
  707. if (old_window_id == sc->window &&
  708. sc->hide_cursor != show_cursor)
  709. return;
  710. } break;
  711. case ScreenCaptureApplicationStream: {
  712. if (sc->display == display &&
  713. [application_id
  714. isEqualToString:sc->application_id] &&
  715. sc->hide_cursor != show_cursor)
  716. return;
  717. } break;
  718. }
  719. }
  720. obs_enter_graphics();
  721. destroy_screen_stream(sc);
  722. sc->capture_type = capture_type;
  723. sc->display = display;
  724. sc->application_id = application_id;
  725. sc->hide_cursor = !show_cursor;
  726. sc->show_empty_names = show_empty_names;
  727. sc->show_hidden_windows = show_hidden_windows;
  728. init_screen_stream(sc);
  729. obs_leave_graphics();
  730. }
  731. static bool build_display_list(struct screen_capture *sc,
  732. obs_properties_t *props)
  733. {
  734. os_sem_wait(sc->shareable_content_available);
  735. obs_property_t *display_list = obs_properties_get(props, "display");
  736. obs_property_list_clear(display_list);
  737. [sc->shareable_content.displays enumerateObjectsUsingBlock:^(
  738. SCDisplay *_Nonnull display,
  739. NSUInteger idx
  740. __attribute__((unused)),
  741. BOOL *_Nonnull stop
  742. __attribute__((unused))) {
  743. NSUInteger screen_index = [NSScreen.screens
  744. indexOfObjectPassingTest:^BOOL(
  745. NSScreen *_Nonnull screen,
  746. NSUInteger index __attribute__((unused)),
  747. BOOL *_Nonnull stop) {
  748. NSNumber *screen_num =
  749. screen.deviceDescription
  750. [@"NSScreenNumber"];
  751. CGDirectDisplayID screen_display_id =
  752. (CGDirectDisplayID)screen_num.intValue;
  753. *stop = (screen_display_id ==
  754. display.displayID);
  755. return *stop;
  756. }];
  757. NSScreen *screen =
  758. [NSScreen.screens objectAtIndex:screen_index];
  759. char dimension_buffer[4][12] = {};
  760. char name_buffer[256] = {};
  761. sprintf(dimension_buffer[0], "%u",
  762. (uint32_t)screen.frame.size.width);
  763. sprintf(dimension_buffer[1], "%u",
  764. (uint32_t)screen.frame.size.height);
  765. sprintf(dimension_buffer[2], "%d",
  766. (int32_t)screen.frame.origin.x);
  767. sprintf(dimension_buffer[3], "%d",
  768. (int32_t)screen.frame.origin.y);
  769. sprintf(name_buffer, "%.200s: %.12sx%.12s @ %.12s,%.12s",
  770. screen.localizedName.UTF8String, dimension_buffer[0],
  771. dimension_buffer[1], dimension_buffer[2],
  772. dimension_buffer[3]);
  773. obs_property_list_add_int(display_list, name_buffer,
  774. display.displayID);
  775. }];
  776. os_sem_post(sc->shareable_content_available);
  777. return true;
  778. }
  779. static bool build_window_list(struct screen_capture *sc,
  780. obs_properties_t *props)
  781. {
  782. os_sem_wait(sc->shareable_content_available);
  783. obs_property_t *window_list = obs_properties_get(props, "window");
  784. obs_property_list_clear(window_list);
  785. [sc->shareable_content.windows enumerateObjectsUsingBlock:^(
  786. SCWindow *_Nonnull window,
  787. NSUInteger idx
  788. __attribute__((unused)),
  789. BOOL *_Nonnull stop
  790. __attribute__((unused))) {
  791. NSString *app_name = window.owningApplication.applicationName;
  792. NSString *title = window.title;
  793. if (!sc->show_empty_names) {
  794. if (app_name == NULL || title == NULL) {
  795. return;
  796. } else if ([app_name isEqualToString:@""] ||
  797. [title isEqualToString:@""]) {
  798. return;
  799. }
  800. }
  801. const char *list_text =
  802. [[NSString stringWithFormat:@"[%@] %@", app_name, title]
  803. UTF8String];
  804. obs_property_list_add_int(window_list, list_text,
  805. window.windowID);
  806. }];
  807. os_sem_post(sc->shareable_content_available);
  808. return true;
  809. }
  810. static bool build_application_list(struct screen_capture *sc,
  811. obs_properties_t *props)
  812. {
  813. os_sem_wait(sc->shareable_content_available);
  814. obs_property_t *application_list =
  815. obs_properties_get(props, "application");
  816. obs_property_list_clear(application_list);
  817. [sc->shareable_content.applications
  818. enumerateObjectsUsingBlock:^(
  819. SCRunningApplication *_Nonnull application,
  820. NSUInteger idx __attribute__((unused)),
  821. BOOL *_Nonnull stop __attribute__((unused))) {
  822. const char *name =
  823. [application.applicationName UTF8String];
  824. const char *bundle_id =
  825. [application.bundleIdentifier UTF8String];
  826. if (strcmp(name, "") != 0)
  827. obs_property_list_add_string(application_list,
  828. name, bundle_id);
  829. }];
  830. os_sem_post(sc->shareable_content_available);
  831. return true;
  832. }
  833. static bool content_settings_changed(void *data, obs_properties_t *props,
  834. obs_property_t *list
  835. __attribute__((unused)),
  836. obs_data_t *settings)
  837. {
  838. struct screen_capture *sc = data;
  839. unsigned int capture_type_id = obs_data_get_int(settings, "type");
  840. obs_property_t *display_list = obs_properties_get(props, "display");
  841. obs_property_t *window_list = obs_properties_get(props, "window");
  842. obs_property_t *app_list = obs_properties_get(props, "application");
  843. obs_property_t *empty = obs_properties_get(props, "show_empty_names");
  844. obs_property_t *hidden =
  845. obs_properties_get(props, "show_hidden_windows");
  846. if (sc->capture_type != capture_type_id) {
  847. switch (capture_type_id) {
  848. case 0: {
  849. obs_property_set_visible(display_list, true);
  850. obs_property_set_visible(window_list, false);
  851. obs_property_set_visible(app_list, false);
  852. obs_property_set_visible(empty, false);
  853. obs_property_set_visible(hidden, false);
  854. break;
  855. }
  856. case 1: {
  857. obs_property_set_visible(display_list, false);
  858. obs_property_set_visible(window_list, true);
  859. obs_property_set_visible(app_list, false);
  860. obs_property_set_visible(empty, true);
  861. obs_property_set_visible(hidden, true);
  862. break;
  863. }
  864. case 2: {
  865. obs_property_set_visible(display_list, true);
  866. obs_property_set_visible(app_list, true);
  867. obs_property_set_visible(window_list, false);
  868. obs_property_set_visible(empty, false);
  869. obs_property_set_visible(hidden, true);
  870. break;
  871. }
  872. }
  873. }
  874. sc->show_empty_names = obs_data_get_bool(settings, "show_empty_names");
  875. sc->show_hidden_windows =
  876. obs_data_get_bool(settings, "show_hidden_windows");
  877. screen_capture_build_content_list(
  878. sc, capture_type_id == ScreenCaptureDisplayStream);
  879. build_display_list(sc, props);
  880. build_window_list(sc, props);
  881. build_application_list(sc, props);
  882. return true;
  883. }
  884. static obs_properties_t *screen_capture_properties(void *data)
  885. {
  886. struct screen_capture *sc = data;
  887. obs_properties_t *props = obs_properties_create();
  888. obs_property_t *capture_type = obs_properties_add_list(
  889. props, "type", obs_module_text("SCK.Method"),
  890. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  891. obs_property_list_add_int(capture_type,
  892. obs_module_text("DisplayCapture"), 0);
  893. obs_property_list_add_int(capture_type,
  894. obs_module_text("WindowCapture"), 1);
  895. obs_property_list_add_int(capture_type,
  896. obs_module_text("ApplicationCapture"), 2);
  897. obs_property_set_modified_callback2(capture_type,
  898. content_settings_changed, data);
  899. obs_property_t *display_list = obs_properties_add_list(
  900. props, "display", obs_module_text("DisplayCapture.Display"),
  901. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  902. obs_property_t *app_list = obs_properties_add_list(
  903. props, "application", obs_module_text("Application"),
  904. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  905. obs_property_t *window_list = obs_properties_add_list(
  906. props, "window", obs_module_text("WindowUtils.Window"),
  907. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  908. obs_property_t *empty = obs_properties_add_bool(
  909. props, "show_empty_names",
  910. obs_module_text("WindowUtils.ShowEmptyNames"));
  911. obs_property_t *hidden = obs_properties_add_bool(
  912. props, "show_hidden_windows",
  913. obs_module_text("WindowUtils.ShowHidden"));
  914. obs_property_set_modified_callback2(hidden, content_settings_changed,
  915. sc);
  916. obs_properties_add_bool(props, "show_cursor",
  917. obs_module_text("DisplayCapture.ShowCursor"));
  918. switch (sc->capture_type) {
  919. case 0: {
  920. obs_property_set_visible(display_list, true);
  921. obs_property_set_visible(window_list, false);
  922. obs_property_set_visible(app_list, false);
  923. obs_property_set_visible(empty, false);
  924. obs_property_set_visible(hidden, false);
  925. break;
  926. }
  927. case 1: {
  928. obs_property_set_visible(display_list, false);
  929. obs_property_set_visible(window_list, true);
  930. obs_property_set_visible(app_list, false);
  931. obs_property_set_visible(empty, true);
  932. obs_property_set_visible(hidden, true);
  933. break;
  934. }
  935. case 2: {
  936. obs_property_set_visible(display_list, true);
  937. obs_property_set_visible(app_list, true);
  938. obs_property_set_visible(window_list, false);
  939. obs_property_set_visible(empty, false);
  940. obs_property_set_visible(hidden, true);
  941. break;
  942. }
  943. }
  944. obs_property_set_modified_callback2(empty, content_settings_changed,
  945. sc);
  946. if (@available(macOS 13.0, *))
  947. ;
  948. else
  949. obs_properties_add_text(props, "audio_info",
  950. obs_module_text("SCK.AudioUnavailable"),
  951. OBS_TEXT_INFO);
  952. return props;
  953. }
  954. struct obs_source_info screen_capture_info = {
  955. .id = "screen_capture",
  956. .type = OBS_SOURCE_TYPE_INPUT,
  957. .get_name = screen_capture_getname,
  958. .create = screen_capture_create,
  959. .destroy = screen_capture_destroy,
  960. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  961. OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB |
  962. OBS_SOURCE_AUDIO,
  963. .video_tick = screen_capture_video_tick,
  964. .video_render = screen_capture_video_render,
  965. .get_width = screen_capture_getwidth,
  966. .get_height = screen_capture_getheight,
  967. .get_defaults = screen_capture_defaults,
  968. .get_properties = screen_capture_properties,
  969. .update = screen_capture_update,
  970. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  971. };
  972. @implementation ScreenCaptureDelegate
  973. - (void)stream:(SCStream *)stream
  974. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  975. ofType:(SCStreamOutputType)type
  976. {
  977. if (self.sc != NULL) {
  978. if (type == SCStreamOutputTypeScreen) {
  979. screen_stream_video_update(self.sc, sampleBuffer);
  980. }
  981. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000
  982. else if (@available(macOS 13.0, *)) {
  983. if (type == SCStreamOutputTypeAudio) {
  984. screen_stream_audio_update(self.sc,
  985. sampleBuffer);
  986. }
  987. }
  988. #endif
  989. }
  990. }
  991. @end
  992. // "-Wunguarded-availability-new"
  993. #pragma clang diagnostic pop
  994. #endif