mac-screen-capture.m 31 KB

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