mac-screen-capture.m 31 KB

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