mac-screen-capture.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. content_filter = [[SCContentFilter alloc]
  319. initWithDisplay:target_display
  320. includingWindows:sc->shareable_content.windows];
  321. }
  322. set_display_mode(sc, target_display);
  323. } break;
  324. case ScreenCaptureWindowStream: {
  325. __block SCWindow *target_window = nil;
  326. if (sc->window != 0) {
  327. [sc->shareable_content.windows
  328. indexOfObjectPassingTest:^BOOL(
  329. SCWindow *_Nonnull window,
  330. NSUInteger idx, BOOL *_Nonnull stop) {
  331. if (window.windowID == sc->window) {
  332. target_window =
  333. sc->shareable_content
  334. .windows[idx];
  335. *stop = TRUE;
  336. }
  337. return *stop;
  338. }];
  339. } else {
  340. target_window =
  341. [sc->shareable_content.windows objectAtIndex:0];
  342. sc->window = target_window.windowID;
  343. }
  344. content_filter = [[SCContentFilter alloc]
  345. initWithDesktopIndependentWindow:target_window];
  346. if (target_window) {
  347. [sc->stream_properties
  348. setWidth:target_window.frame.size.width];
  349. [sc->stream_properties
  350. setHeight:target_window.frame.size.height];
  351. }
  352. } break;
  353. case ScreenCaptureApplicationStream: {
  354. SCDisplay *target_display = get_target_display();
  355. __block SCRunningApplication *target_application = nil;
  356. {
  357. [sc->shareable_content.applications
  358. indexOfObjectPassingTest:^BOOL(
  359. SCRunningApplication
  360. *_Nonnull application,
  361. NSUInteger idx, BOOL *_Nonnull stop) {
  362. if ([application.bundleIdentifier
  363. isEqualToString:
  364. sc->
  365. application_id]) {
  366. target_application =
  367. sc->shareable_content
  368. .applications
  369. [idx];
  370. *stop = TRUE;
  371. }
  372. return *stop;
  373. }];
  374. }
  375. NSArray *target_application_array = [[NSArray alloc]
  376. initWithObjects:target_application, nil];
  377. content_filter = [[SCContentFilter alloc]
  378. initWithDisplay:target_display
  379. includingApplications:target_application_array
  380. exceptingWindows:[[NSArray alloc] init]];
  381. if (@available(macOS 13.0, *))
  382. [sc->stream_properties
  383. setBackgroundColor:CGColorGetConstantColor(
  384. kCGColorClear)];
  385. set_display_mode(sc, target_display);
  386. } break;
  387. }
  388. os_sem_post(sc->shareable_content_available);
  389. [sc->stream_properties setQueueDepth:8];
  390. [sc->stream_properties setShowsCursor:!sc->hide_cursor];
  391. [sc->stream_properties setPixelFormat:'BGRA'];
  392. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000
  393. if (@available(macOS 13.0, *)) {
  394. [sc->stream_properties setCapturesAudio:TRUE];
  395. [sc->stream_properties setExcludesCurrentProcessAudio:TRUE];
  396. [sc->stream_properties setChannelCount:2];
  397. }
  398. #endif
  399. sc->disp = [[SCStream alloc] initWithFilter:content_filter
  400. configuration:sc->stream_properties
  401. delegate:nil];
  402. NSError *error = nil;
  403. BOOL did_add_output = [sc->disp addStreamOutput:sc->capture_delegate
  404. type:SCStreamOutputTypeScreen
  405. sampleHandlerQueue:nil
  406. error:&error];
  407. if (!did_add_output) {
  408. MACCAP_ERR(
  409. "init_screen_stream: Failed to add stream output with error %s\n",
  410. [[error localizedFailureReason]
  411. cStringUsingEncoding:NSUTF8StringEncoding]);
  412. [error release];
  413. return !did_add_output;
  414. }
  415. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000
  416. if (__builtin_available(macOS 13.0, *)) {
  417. did_add_output = [sc->disp
  418. addStreamOutput:sc->capture_delegate
  419. type:SCStreamOutputTypeAudio
  420. sampleHandlerQueue:nil
  421. error:&error];
  422. if (!did_add_output) {
  423. MACCAP_ERR(
  424. "init_screen_stream: Failed to add audio stream output with error %s\n",
  425. [[error localizedFailureReason]
  426. cStringUsingEncoding:
  427. NSUTF8StringEncoding]);
  428. [error 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. bool init_vertbuf_screen_capture(struct screen_capture *sc)
  455. {
  456. struct gs_vb_data *vb_data = gs_vbdata_create();
  457. vb_data->num = 4;
  458. vb_data->points = bzalloc(sizeof(struct vec3) * 4);
  459. if (!vb_data->points)
  460. return false;
  461. vb_data->num_tex = 1;
  462. vb_data->tvarray = bzalloc(sizeof(struct gs_tvertarray));
  463. if (!vb_data->tvarray)
  464. return false;
  465. vb_data->tvarray[0].width = 2;
  466. vb_data->tvarray[0].array = bzalloc(sizeof(struct vec2) * 4);
  467. if (!vb_data->tvarray[0].array)
  468. return false;
  469. sc->vertbuf = gs_vertexbuffer_create(vb_data, GS_DYNAMIC);
  470. return sc->vertbuf != NULL;
  471. }
  472. static void screen_capture_build_content_list(struct screen_capture *sc,
  473. bool excludingDesktopWindows)
  474. {
  475. typedef void (^shareable_content_callback)(SCShareableContent *,
  476. NSError *);
  477. shareable_content_callback new_content_received = ^void(
  478. SCShareableContent *shareable_content, NSError *error) {
  479. if (error == nil && sc->shareable_content_available != NULL) {
  480. sc->shareable_content = [shareable_content retain];
  481. } else {
  482. #ifdef DEBUG
  483. MACCAP_ERR(
  484. "screen_capture_properties: Failed to get shareable content with error %s\n",
  485. [[error localizedFailureReason]
  486. cStringUsingEncoding:
  487. NSUTF8StringEncoding]);
  488. #endif
  489. MACCAP_LOG(
  490. LOG_WARNING,
  491. "Unable to get list of available applications or windows. "
  492. "Please check if OBS has necessary screen capture permissions.");
  493. }
  494. os_sem_post(sc->shareable_content_available);
  495. };
  496. os_sem_wait(sc->shareable_content_available);
  497. [sc->shareable_content release];
  498. [SCShareableContent
  499. getShareableContentExcludingDesktopWindows:excludingDesktopWindows
  500. onScreenWindowsOnly:!sc->show_hidden_windows
  501. completionHandler:new_content_received];
  502. }
  503. static void *screen_capture_create(obs_data_t *settings, obs_source_t *source)
  504. {
  505. struct screen_capture *sc = bzalloc(sizeof(struct screen_capture));
  506. sc->source = source;
  507. sc->hide_cursor = !obs_data_get_bool(settings, "show_cursor");
  508. sc->show_empty_names = obs_data_get_bool(settings, "show_empty_names");
  509. sc->show_hidden_windows =
  510. obs_data_get_bool(settings, "show_hidden_windows");
  511. sc->window = obs_data_get_int(settings, "window");
  512. sc->capture_type = obs_data_get_int(settings, "type");
  513. os_sem_init(&sc->shareable_content_available, 1);
  514. screen_capture_build_content_list(
  515. sc, sc->capture_type == ScreenCaptureWindowStream);
  516. sc->capture_delegate = [[ScreenCaptureDelegate alloc] init];
  517. sc->capture_delegate.sc = sc;
  518. sc->effect = obs_get_base_effect(OBS_EFFECT_DEFAULT_RECT);
  519. if (!sc->effect)
  520. goto fail;
  521. obs_enter_graphics();
  522. struct gs_sampler_info info = {
  523. .filter = GS_FILTER_LINEAR,
  524. .address_u = GS_ADDRESS_CLAMP,
  525. .address_v = GS_ADDRESS_CLAMP,
  526. .address_w = GS_ADDRESS_CLAMP,
  527. .max_anisotropy = 1,
  528. };
  529. sc->sampler = gs_samplerstate_create(&info);
  530. if (!sc->sampler)
  531. goto fail;
  532. if (!init_vertbuf_screen_capture(sc))
  533. goto fail;
  534. obs_leave_graphics();
  535. sc->display = obs_data_get_int(settings, "display");
  536. sc->application_id = [[NSString alloc]
  537. initWithUTF8String:obs_data_get_string(settings,
  538. "application")];
  539. pthread_mutex_init(&sc->mutex, NULL);
  540. if (!init_screen_stream(sc))
  541. goto fail;
  542. return sc;
  543. fail:
  544. obs_leave_graphics();
  545. screen_capture_destroy(sc);
  546. return NULL;
  547. }
  548. static void build_sprite(struct gs_vb_data *data, float fcx, float fcy,
  549. float start_u, float end_u, float start_v, float end_v)
  550. {
  551. struct vec2 *tvarray = data->tvarray[0].array;
  552. vec3_set(data->points + 1, fcx, 0.0f, 0.0f);
  553. vec3_set(data->points + 2, 0.0f, fcy, 0.0f);
  554. vec3_set(data->points + 3, fcx, fcy, 0.0f);
  555. vec2_set(tvarray, start_u, start_v);
  556. vec2_set(tvarray + 1, end_u, start_v);
  557. vec2_set(tvarray + 2, start_u, end_v);
  558. vec2_set(tvarray + 3, end_u, end_v);
  559. }
  560. static inline void build_sprite_rect(struct gs_vb_data *data, float origin_x,
  561. float origin_y, float end_x, float end_y)
  562. {
  563. build_sprite(data, fabs(end_x - origin_x), fabs(end_y - origin_y),
  564. origin_x, end_x, origin_y, end_y);
  565. }
  566. static void screen_capture_video_tick(void *data,
  567. float seconds __attribute__((unused)))
  568. {
  569. struct screen_capture *sc = data;
  570. if (!sc->current)
  571. return;
  572. if (!obs_source_showing(sc->source))
  573. return;
  574. IOSurfaceRef prev_prev = sc->prev;
  575. if (pthread_mutex_lock(&sc->mutex))
  576. return;
  577. sc->prev = sc->current;
  578. sc->current = NULL;
  579. pthread_mutex_unlock(&sc->mutex);
  580. if (prev_prev == sc->prev)
  581. return;
  582. CGPoint origin = {0.f, 0.f};
  583. CGPoint end = {sc->frame.size.width, sc->frame.size.height};
  584. obs_enter_graphics();
  585. build_sprite_rect(gs_vertexbuffer_get_data(sc->vertbuf), origin.x,
  586. origin.y, end.x, end.y);
  587. if (sc->tex)
  588. gs_texture_rebind_iosurface(sc->tex, sc->prev);
  589. else
  590. sc->tex = gs_texture_create_from_iosurface(sc->prev);
  591. obs_leave_graphics();
  592. if (prev_prev) {
  593. IOSurfaceDecrementUseCount(prev_prev);
  594. CFRelease(prev_prev);
  595. }
  596. }
  597. static void screen_capture_video_render(void *data, gs_effect_t *effect
  598. __attribute__((unused)))
  599. {
  600. struct screen_capture *sc = data;
  601. if (!sc->tex)
  602. return;
  603. const bool linear_srgb = gs_get_linear_srgb();
  604. const bool previous = gs_framebuffer_srgb_enabled();
  605. gs_enable_framebuffer_srgb(linear_srgb);
  606. gs_vertexbuffer_flush(sc->vertbuf);
  607. gs_load_vertexbuffer(sc->vertbuf);
  608. gs_load_indexbuffer(NULL);
  609. gs_load_samplerstate(sc->sampler, 0);
  610. gs_technique_t *tech = gs_effect_get_technique(sc->effect, "Draw");
  611. gs_eparam_t *param = gs_effect_get_param_by_name(sc->effect, "image");
  612. if (linear_srgb)
  613. gs_effect_set_texture_srgb(param, sc->tex);
  614. else
  615. gs_effect_set_texture(param, sc->tex);
  616. gs_technique_begin(tech);
  617. gs_technique_begin_pass(tech, 0);
  618. gs_draw(GS_TRISTRIP, 0, 4);
  619. gs_technique_end_pass(tech);
  620. gs_technique_end(tech);
  621. gs_enable_framebuffer_srgb(previous);
  622. }
  623. static const char *screen_capture_getname(void *unused __attribute__((unused)))
  624. {
  625. return obs_module_text("SCK.Name");
  626. }
  627. static uint32_t screen_capture_getwidth(void *data)
  628. {
  629. struct screen_capture *sc = data;
  630. return sc->frame.size.width;
  631. }
  632. static uint32_t screen_capture_getheight(void *data)
  633. {
  634. struct screen_capture *sc = data;
  635. return sc->frame.size.height;
  636. }
  637. static void screen_capture_defaults(obs_data_t *settings)
  638. {
  639. CGDirectDisplayID initial_display = 0;
  640. {
  641. NSScreen *mainScreen = [NSScreen mainScreen];
  642. if (mainScreen) {
  643. NSNumber *screen_num =
  644. mainScreen.deviceDescription[@"NSScreenNumber"];
  645. if (screen_num) {
  646. initial_display =
  647. (CGDirectDisplayID)(uintptr_t)
  648. screen_num.pointerValue;
  649. }
  650. }
  651. }
  652. obs_data_set_default_int(settings, "type", 0);
  653. obs_data_set_default_int(settings, "display", initial_display);
  654. obs_data_set_default_int(settings, "window", kCGNullWindowID);
  655. obs_data_set_default_obj(settings, "application", NULL);
  656. obs_data_set_default_bool(settings, "show_cursor", true);
  657. obs_data_set_default_bool(settings, "show_empty_names", false);
  658. obs_data_set_default_bool(settings, "show_hidden_windows", false);
  659. }
  660. static void screen_capture_update(void *data, obs_data_t *settings)
  661. {
  662. struct screen_capture *sc = data;
  663. CGWindowID old_window_id = sc->window;
  664. CGWindowID new_window_id = obs_data_get_int(settings, "window");
  665. if (new_window_id > 0 && new_window_id != old_window_id)
  666. sc->window = new_window_id;
  667. ScreenCaptureStreamType capture_type =
  668. (ScreenCaptureStreamType)obs_data_get_int(settings, "type");
  669. CGDirectDisplayID display =
  670. (CGDirectDisplayID)obs_data_get_int(settings, "display");
  671. NSString *application_id = [[NSString alloc]
  672. initWithUTF8String:obs_data_get_string(settings,
  673. "application")];
  674. bool show_cursor = obs_data_get_bool(settings, "show_cursor");
  675. bool show_empty_names = obs_data_get_bool(settings, "show_empty_names");
  676. bool show_hidden_windows =
  677. obs_data_get_bool(settings, "show_hidden_windows");
  678. if (capture_type == sc->capture_type) {
  679. switch (sc->capture_type) {
  680. case ScreenCaptureDisplayStream: {
  681. if (sc->display == display &&
  682. sc->hide_cursor != show_cursor)
  683. return;
  684. } break;
  685. case ScreenCaptureWindowStream: {
  686. if (old_window_id == sc->window &&
  687. sc->hide_cursor != show_cursor)
  688. return;
  689. } break;
  690. case ScreenCaptureApplicationStream: {
  691. if (sc->display == display &&
  692. [application_id
  693. isEqualToString:sc->application_id] &&
  694. sc->hide_cursor != show_cursor)
  695. return;
  696. } break;
  697. }
  698. }
  699. obs_enter_graphics();
  700. destroy_screen_stream(sc);
  701. sc->capture_type = capture_type;
  702. sc->display = display;
  703. sc->application_id = application_id;
  704. sc->hide_cursor = !show_cursor;
  705. sc->show_empty_names = show_empty_names;
  706. sc->show_hidden_windows = show_hidden_windows;
  707. init_screen_stream(sc);
  708. obs_leave_graphics();
  709. }
  710. static bool build_display_list(struct screen_capture *sc,
  711. obs_properties_t *props)
  712. {
  713. os_sem_wait(sc->shareable_content_available);
  714. obs_property_t *display_list = obs_properties_get(props, "display");
  715. obs_property_list_clear(display_list);
  716. [sc->shareable_content.displays enumerateObjectsUsingBlock:^(
  717. SCDisplay *_Nonnull display,
  718. NSUInteger idx
  719. __attribute__((unused)),
  720. BOOL *_Nonnull stop
  721. __attribute__((unused))) {
  722. NSUInteger screen_index = [NSScreen.screens
  723. indexOfObjectPassingTest:^BOOL(
  724. NSScreen *_Nonnull screen,
  725. NSUInteger index __attribute__((unused)),
  726. BOOL *_Nonnull stop) {
  727. NSNumber *screen_num =
  728. screen.deviceDescription
  729. [@"NSScreenNumber"];
  730. CGDirectDisplayID screen_display_id =
  731. (CGDirectDisplayID)screen_num.intValue;
  732. *stop = (screen_display_id ==
  733. display.displayID);
  734. return *stop;
  735. }];
  736. NSScreen *screen =
  737. [NSScreen.screens objectAtIndex:screen_index];
  738. char dimension_buffer[4][12] = {};
  739. char name_buffer[256] = {};
  740. sprintf(dimension_buffer[0], "%u",
  741. (uint32_t)screen.frame.size.width);
  742. sprintf(dimension_buffer[1], "%u",
  743. (uint32_t)screen.frame.size.height);
  744. sprintf(dimension_buffer[2], "%d",
  745. (int32_t)screen.frame.origin.x);
  746. sprintf(dimension_buffer[3], "%d",
  747. (int32_t)screen.frame.origin.y);
  748. sprintf(name_buffer, "%.200s: %.12sx%.12s @ %.12s,%.12s",
  749. screen.localizedName.UTF8String, dimension_buffer[0],
  750. dimension_buffer[1], dimension_buffer[2],
  751. dimension_buffer[3]);
  752. obs_property_list_add_int(display_list, name_buffer,
  753. display.displayID);
  754. }];
  755. os_sem_post(sc->shareable_content_available);
  756. return true;
  757. }
  758. static bool build_window_list(struct screen_capture *sc,
  759. obs_properties_t *props)
  760. {
  761. os_sem_wait(sc->shareable_content_available);
  762. obs_property_t *window_list = obs_properties_get(props, "window");
  763. obs_property_list_clear(window_list);
  764. [sc->shareable_content.windows enumerateObjectsUsingBlock:^(
  765. SCWindow *_Nonnull window,
  766. NSUInteger idx
  767. __attribute__((unused)),
  768. BOOL *_Nonnull stop
  769. __attribute__((unused))) {
  770. NSString *app_name = window.owningApplication.applicationName;
  771. NSString *title = window.title;
  772. if (!sc->show_empty_names) {
  773. if (app_name == NULL || title == NULL) {
  774. return;
  775. } else if ([app_name isEqualToString:@""] ||
  776. [title isEqualToString:@""]) {
  777. return;
  778. }
  779. }
  780. const char *list_text =
  781. [[NSString stringWithFormat:@"[%@] %@", app_name, title]
  782. UTF8String];
  783. obs_property_list_add_int(window_list, list_text,
  784. window.windowID);
  785. }];
  786. os_sem_post(sc->shareable_content_available);
  787. return true;
  788. }
  789. static bool build_application_list(struct screen_capture *sc,
  790. obs_properties_t *props)
  791. {
  792. os_sem_wait(sc->shareable_content_available);
  793. obs_property_t *application_list =
  794. obs_properties_get(props, "application");
  795. obs_property_list_clear(application_list);
  796. [sc->shareable_content.applications
  797. enumerateObjectsUsingBlock:^(
  798. SCRunningApplication *_Nonnull application,
  799. NSUInteger idx __attribute__((unused)),
  800. BOOL *_Nonnull stop __attribute__((unused))) {
  801. const char *name =
  802. [application.applicationName UTF8String];
  803. const char *bundle_id =
  804. [application.bundleIdentifier UTF8String];
  805. if (strcmp(name, "") != 0)
  806. obs_property_list_add_string(application_list,
  807. name, bundle_id);
  808. }];
  809. os_sem_post(sc->shareable_content_available);
  810. return true;
  811. }
  812. static bool content_settings_changed(void *data, obs_properties_t *props,
  813. obs_property_t *list
  814. __attribute__((unused)),
  815. obs_data_t *settings)
  816. {
  817. struct screen_capture *sc = data;
  818. unsigned int capture_type_id = obs_data_get_int(settings, "type");
  819. obs_property_t *display_list = obs_properties_get(props, "display");
  820. obs_property_t *window_list = obs_properties_get(props, "window");
  821. obs_property_t *app_list = obs_properties_get(props, "application");
  822. obs_property_t *empty = obs_properties_get(props, "show_empty_names");
  823. obs_property_t *hidden =
  824. obs_properties_get(props, "show_hidden_windows");
  825. if (sc->capture_type != capture_type_id) {
  826. switch (capture_type_id) {
  827. case 0: {
  828. obs_property_set_visible(display_list, true);
  829. obs_property_set_visible(window_list, false);
  830. obs_property_set_visible(app_list, false);
  831. obs_property_set_visible(empty, false);
  832. obs_property_set_visible(hidden, false);
  833. break;
  834. }
  835. case 1: {
  836. obs_property_set_visible(display_list, false);
  837. obs_property_set_visible(window_list, true);
  838. obs_property_set_visible(app_list, false);
  839. obs_property_set_visible(empty, true);
  840. obs_property_set_visible(hidden, true);
  841. break;
  842. }
  843. case 2: {
  844. obs_property_set_visible(display_list, true);
  845. obs_property_set_visible(app_list, true);
  846. obs_property_set_visible(window_list, false);
  847. obs_property_set_visible(empty, false);
  848. obs_property_set_visible(hidden, true);
  849. break;
  850. }
  851. }
  852. }
  853. sc->show_empty_names = obs_data_get_bool(settings, "show_empty_names");
  854. sc->show_hidden_windows =
  855. obs_data_get_bool(settings, "show_hidden_windows");
  856. screen_capture_build_content_list(
  857. sc, capture_type_id == ScreenCaptureWindowStream);
  858. build_display_list(sc, props);
  859. build_window_list(sc, props);
  860. build_application_list(sc, props);
  861. return true;
  862. }
  863. static obs_properties_t *screen_capture_properties(void *data)
  864. {
  865. struct screen_capture *sc = data;
  866. obs_properties_t *props = obs_properties_create();
  867. obs_property_t *capture_type = obs_properties_add_list(
  868. props, "type", obs_module_text("SCK.Method"),
  869. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  870. obs_property_list_add_int(capture_type,
  871. obs_module_text("DisplayCapture"), 0);
  872. obs_property_list_add_int(capture_type,
  873. obs_module_text("WindowCapture"), 1);
  874. obs_property_list_add_int(capture_type,
  875. obs_module_text("ApplicationCapture"), 2);
  876. obs_property_set_modified_callback2(capture_type,
  877. content_settings_changed, data);
  878. obs_property_t *display_list = obs_properties_add_list(
  879. props, "display", obs_module_text("DisplayCapture.Display"),
  880. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  881. obs_property_t *app_list = obs_properties_add_list(
  882. props, "application", obs_module_text("Application"),
  883. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  884. obs_property_t *window_list = obs_properties_add_list(
  885. props, "window", obs_module_text("WindowUtils.Window"),
  886. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  887. obs_property_t *empty = obs_properties_add_bool(
  888. props, "show_empty_names",
  889. obs_module_text("WindowUtils.ShowEmptyNames"));
  890. obs_property_t *hidden = obs_properties_add_bool(
  891. props, "show_hidden_windows",
  892. obs_module_text("WindowUtils.ShowHidden"));
  893. obs_property_set_modified_callback2(hidden, content_settings_changed,
  894. sc);
  895. obs_properties_add_bool(props, "show_cursor",
  896. obs_module_text("DisplayCapture.ShowCursor"));
  897. switch (sc->capture_type) {
  898. case 0: {
  899. obs_property_set_visible(display_list, true);
  900. obs_property_set_visible(window_list, false);
  901. obs_property_set_visible(app_list, false);
  902. obs_property_set_visible(empty, false);
  903. obs_property_set_visible(hidden, false);
  904. break;
  905. }
  906. case 1: {
  907. obs_property_set_visible(display_list, false);
  908. obs_property_set_visible(window_list, true);
  909. obs_property_set_visible(app_list, false);
  910. obs_property_set_visible(empty, true);
  911. obs_property_set_visible(hidden, true);
  912. break;
  913. }
  914. case 2: {
  915. obs_property_set_visible(display_list, true);
  916. obs_property_set_visible(app_list, true);
  917. obs_property_set_visible(window_list, false);
  918. obs_property_set_visible(empty, false);
  919. obs_property_set_visible(hidden, true);
  920. break;
  921. }
  922. }
  923. obs_property_set_modified_callback2(empty, content_settings_changed,
  924. sc);
  925. if (@available(macOS 13.0, *))
  926. ;
  927. else
  928. obs_properties_add_text(props, "audio_info",
  929. obs_module_text("SCK.AudioUnavailable"),
  930. OBS_TEXT_INFO);
  931. return props;
  932. }
  933. struct obs_source_info screen_capture_info = {
  934. .id = "screen_capture",
  935. .type = OBS_SOURCE_TYPE_INPUT,
  936. .get_name = screen_capture_getname,
  937. .create = screen_capture_create,
  938. .destroy = screen_capture_destroy,
  939. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  940. OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB |
  941. OBS_SOURCE_AUDIO,
  942. .video_tick = screen_capture_video_tick,
  943. .video_render = screen_capture_video_render,
  944. .get_width = screen_capture_getwidth,
  945. .get_height = screen_capture_getheight,
  946. .get_defaults = screen_capture_defaults,
  947. .get_properties = screen_capture_properties,
  948. .update = screen_capture_update,
  949. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  950. };
  951. @implementation ScreenCaptureDelegate
  952. - (void)stream:(SCStream *)stream
  953. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  954. ofType:(SCStreamOutputType)type
  955. {
  956. if (self.sc != NULL) {
  957. if (type == SCStreamOutputTypeScreen) {
  958. screen_stream_video_update(self.sc, sampleBuffer);
  959. }
  960. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000
  961. else if (@available(macOS 13.0, *)) {
  962. if (type == SCStreamOutputTypeAudio) {
  963. screen_stream_audio_update(self.sc,
  964. sampleBuffer);
  965. }
  966. }
  967. #endif
  968. }
  969. }
  970. @end
  971. // "-Wunguarded-availability-new"
  972. #pragma clang diagnostic pop
  973. #endif