mac-screen-capture.m 34 KB

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