mac-screen-capture.m 30 KB

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