mac-screen-capture.m 32 KB

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