mac-screen-capture.m 32 KB

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