av-capture.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. #import <AVFoundation/AVFoundation.h>
  2. #import <CoreFoundation/CoreFoundation.h>
  3. #import <CoreMedia/CoreMedia.h>
  4. #import <CoreVideo/CoreVideo.h>
  5. #include <arpa/inet.h>
  6. #include <obs-module.h>
  7. #include <media-io/video-io.h>
  8. #import "AVCaptureInputPort+PreMavericksCompat.h"
  9. #define TEXT_AVCAPTURE obs_module_text("AVCapture")
  10. #define TEXT_DEVICE obs_module_text("Device")
  11. #define TEXT_USE_PRESET obs_module_text("UsePreset")
  12. #define TEXT_PRESET obs_module_text("Preset")
  13. #define MILLI_TIMESCALE 1000
  14. #define MICRO_TIMESCALE (MILLI_TIMESCALE * 1000)
  15. #define NANO_TIMESCALE (MICRO_TIMESCALE * 1000)
  16. #define AV_FOURCC_STR(code) \
  17. (char[5]) { \
  18. (code >> 24) & 0xFF, \
  19. (code >> 16) & 0xFF, \
  20. (code >> 8) & 0xFF, \
  21. code & 0xFF, \
  22. 0 \
  23. }
  24. struct av_capture;
  25. #define AVLOG(level, format, ...) \
  26. blog(level, "%s: " format, \
  27. obs_source_get_name(capture->source), ##__VA_ARGS__)
  28. #define AVFREE(x) {[x release]; x = nil;}
  29. @interface OBSAVCaptureDelegate :
  30. NSObject<AVCaptureVideoDataOutputSampleBufferDelegate>
  31. {
  32. @public
  33. struct av_capture *capture;
  34. }
  35. - (void)captureOutput:(AVCaptureOutput *)out
  36. didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
  37. fromConnection:(AVCaptureConnection *)connection;
  38. - (void)captureOutput:(AVCaptureOutput *)captureOutput
  39. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  40. fromConnection:(AVCaptureConnection *)connection;
  41. @end
  42. struct av_capture {
  43. AVCaptureSession *session;
  44. AVCaptureDevice *device;
  45. AVCaptureDeviceInput *device_input;
  46. AVCaptureVideoDataOutput *out;
  47. OBSAVCaptureDelegate *delegate;
  48. dispatch_queue_t queue;
  49. bool has_clock;
  50. NSString *uid;
  51. id connect_observer;
  52. id disconnect_observer;
  53. FourCharCode fourcc;
  54. enum video_format video_format;
  55. enum video_colorspace colorspace;
  56. enum video_range_type video_range;
  57. obs_source_t *source;
  58. struct obs_source_frame frame;
  59. };
  60. static inline enum video_format format_from_subtype(FourCharCode subtype)
  61. {
  62. //TODO: uncomment VIDEO_FORMAT_NV12 and VIDEO_FORMAT_ARGB once libobs
  63. // gains matching GPU conversions or a CPU fallback is implemented
  64. switch (subtype) {
  65. case kCVPixelFormatType_422YpCbCr8:
  66. return VIDEO_FORMAT_UYVY;
  67. case kCVPixelFormatType_422YpCbCr8_yuvs:
  68. return VIDEO_FORMAT_YUY2;
  69. /*case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
  70. case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
  71. return VIDEO_FORMAT_NV12;*/
  72. /*case kCVPixelFormatType_32ARGB:
  73. return VIDEO_FORMAT_ARGB;*/
  74. case kCVPixelFormatType_32BGRA:
  75. return VIDEO_FORMAT_BGRA;
  76. default:
  77. return VIDEO_FORMAT_NONE;
  78. }
  79. }
  80. static inline bool is_fullrange_yuv(FourCharCode pixel_format)
  81. {
  82. switch (pixel_format) {
  83. case kCVPixelFormatType_420YpCbCr8PlanarFullRange:
  84. case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
  85. case kCVPixelFormatType_422YpCbCr8FullRange:
  86. return true;
  87. default:
  88. return false;
  89. }
  90. }
  91. static inline enum video_colorspace get_colorspace(CMFormatDescriptionRef desc)
  92. {
  93. CFPropertyListRef matrix = CMFormatDescriptionGetExtension(desc,
  94. kCMFormatDescriptionExtension_YCbCrMatrix);
  95. if (!matrix)
  96. return VIDEO_CS_DEFAULT;
  97. if (CFStringCompare(matrix, kCVImageBufferYCbCrMatrix_ITU_R_709_2, 0)
  98. == kCFCompareEqualTo)
  99. return VIDEO_CS_709;
  100. return VIDEO_CS_601;
  101. }
  102. static inline bool update_colorspace(struct av_capture *capture,
  103. struct obs_source_frame *frame, CMFormatDescriptionRef desc,
  104. bool full_range)
  105. {
  106. enum video_colorspace colorspace = get_colorspace(desc);
  107. enum video_range_type range = full_range ?
  108. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  109. if (colorspace == capture->colorspace && range == capture->video_range)
  110. return true;
  111. frame->full_range = full_range;
  112. if (!video_format_get_parameters(colorspace, range,
  113. frame->color_matrix,
  114. frame->color_range_min,
  115. frame->color_range_max)) {
  116. AVLOG(LOG_ERROR, "Failed to get colorspace parameters for "
  117. "colorspace %u range %u", colorspace, range);
  118. return false;
  119. }
  120. capture->colorspace = colorspace;
  121. capture->video_range = range;
  122. return true;
  123. }
  124. static inline bool update_frame(struct av_capture *capture,
  125. struct obs_source_frame *frame, CMSampleBufferRef sample_buffer)
  126. {
  127. CMFormatDescriptionRef desc =
  128. CMSampleBufferGetFormatDescription(sample_buffer);
  129. FourCharCode fourcc = CMFormatDescriptionGetMediaSubType(desc);
  130. enum video_format format = format_from_subtype(fourcc);
  131. CMVideoDimensions dims = CMVideoFormatDescriptionGetDimensions(desc);
  132. CVImageBufferRef img = CMSampleBufferGetImageBuffer(sample_buffer);
  133. if (format == VIDEO_FORMAT_NONE) {
  134. if (capture->fourcc == fourcc)
  135. return false;
  136. capture->fourcc = fourcc;
  137. AVLOG(LOG_ERROR, "Unhandled fourcc: %s (0x%x) (%zu planes)",
  138. AV_FOURCC_STR(fourcc), fourcc,
  139. CVPixelBufferGetPlaneCount(img));
  140. return false;
  141. }
  142. if (frame->format != format)
  143. AVLOG(LOG_DEBUG, "Switching fourcc: "
  144. "'%s' (0x%x) -> '%s' (0x%x)",
  145. AV_FOURCC_STR(capture->fourcc), capture->fourcc,
  146. AV_FOURCC_STR(fourcc), fourcc);
  147. capture->fourcc = fourcc;
  148. frame->format = format;
  149. frame->width = dims.width;
  150. frame->height = dims.height;
  151. if (format_is_yuv(format) && !update_colorspace(capture, frame, desc,
  152. is_fullrange_yuv(fourcc)))
  153. return false;
  154. CVPixelBufferLockBaseAddress(img, kCVPixelBufferLock_ReadOnly);
  155. if (!CVPixelBufferIsPlanar(img)) {
  156. frame->linesize[0] = CVPixelBufferGetBytesPerRow(img);
  157. frame->data[0] = CVPixelBufferGetBaseAddress(img);
  158. return true;
  159. }
  160. size_t count = CVPixelBufferGetPlaneCount(img);
  161. for (size_t i = 0; i < count; i++) {
  162. frame->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(img, i);
  163. frame->data[i] = CVPixelBufferGetBaseAddressOfPlane(img, i);
  164. }
  165. return true;
  166. }
  167. @implementation OBSAVCaptureDelegate
  168. - (void)captureOutput:(AVCaptureOutput *)out
  169. didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
  170. fromConnection:(AVCaptureConnection *)connection
  171. {
  172. UNUSED_PARAMETER(out);
  173. UNUSED_PARAMETER(sampleBuffer);
  174. UNUSED_PARAMETER(connection);
  175. }
  176. - (void)captureOutput:(AVCaptureOutput *)captureOutput
  177. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  178. fromConnection:(AVCaptureConnection *)connection
  179. {
  180. UNUSED_PARAMETER(captureOutput);
  181. UNUSED_PARAMETER(connection);
  182. CMItemCount count = CMSampleBufferGetNumSamples(sampleBuffer);
  183. if (count < 1 || !capture)
  184. return;
  185. struct obs_source_frame *frame = &capture->frame;
  186. CMTime target_pts =
  187. CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
  188. CMTime target_pts_nano = CMTimeConvertScale(target_pts, NANO_TIMESCALE,
  189. kCMTimeRoundingMethod_Default);
  190. frame->timestamp = target_pts_nano.value;
  191. frame->flags = 0;
  192. if (!update_frame(capture, frame, sampleBuffer))
  193. return;
  194. obs_source_output_video(capture->source, frame);
  195. CVImageBufferRef img = CMSampleBufferGetImageBuffer(sampleBuffer);
  196. CVPixelBufferUnlockBaseAddress(img, kCVPixelBufferLock_ReadOnly);
  197. }
  198. @end
  199. static const char *av_capture_getname(void)
  200. {
  201. return TEXT_AVCAPTURE;
  202. }
  203. static void remove_device(struct av_capture *capture)
  204. {
  205. [capture->session stopRunning];
  206. [capture->session removeInput:capture->device_input];
  207. AVFREE(capture->device_input);
  208. AVFREE(capture->device);
  209. }
  210. static void av_capture_destroy(void *data)
  211. {
  212. struct av_capture *capture = data;
  213. if (!capture)
  214. return;
  215. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  216. [nc removeObserver:capture->connect_observer];
  217. [nc removeObserver:capture->disconnect_observer];
  218. remove_device(capture);
  219. AVFREE(capture->out);
  220. if (capture->queue)
  221. dispatch_release(capture->queue);
  222. AVFREE(capture->delegate);
  223. AVFREE(capture->session);
  224. AVFREE(capture->uid);
  225. bfree(capture);
  226. }
  227. static NSString *get_string(obs_data_t *data, char const *name)
  228. {
  229. return @(obs_data_get_string(data, name));
  230. }
  231. static bool init_session(struct av_capture *capture)
  232. {
  233. capture->session = [[AVCaptureSession alloc] init];
  234. if (!capture->session) {
  235. AVLOG(LOG_ERROR, "Could not create AVCaptureSession");
  236. goto error;
  237. }
  238. capture->delegate = [[OBSAVCaptureDelegate alloc] init];
  239. if (!capture->delegate) {
  240. AVLOG(LOG_ERROR, "Could not create OBSAVCaptureDelegate");
  241. goto error;
  242. }
  243. capture->delegate->capture = capture;
  244. capture->out = [[AVCaptureVideoDataOutput alloc] init];
  245. if (!capture->out) {
  246. AVLOG(LOG_ERROR, "Could not create AVCaptureVideoDataOutput");
  247. goto error;
  248. }
  249. capture->queue = dispatch_queue_create(NULL, NULL);
  250. if (!capture->queue) {
  251. AVLOG(LOG_ERROR, "Could not create dispatch queue");
  252. goto error;
  253. }
  254. [capture->session addOutput:capture->out];
  255. [capture->out
  256. setSampleBufferDelegate:capture->delegate
  257. queue:capture->queue];
  258. return true;
  259. error:
  260. AVFREE(capture->session);
  261. AVFREE(capture->delegate);
  262. AVFREE(capture->out);
  263. return false;
  264. }
  265. static bool init_device_input(struct av_capture *capture)
  266. {
  267. NSError *err = nil;
  268. capture->device_input = [[AVCaptureDeviceInput
  269. deviceInputWithDevice:capture->device error:&err] retain];
  270. if (!capture->device_input) {
  271. AVLOG(LOG_ERROR, "Error while initializing device input: %s",
  272. err.localizedFailureReason.UTF8String);
  273. return false;
  274. }
  275. [capture->session addInput:capture->device_input];
  276. return true;
  277. }
  278. static uint32_t uint_from_dict(NSDictionary *dict, CFStringRef key)
  279. {
  280. return ((NSNumber*)dict[(__bridge NSString*)key]).unsignedIntValue;
  281. }
  282. static bool init_format(struct av_capture *capture)
  283. {
  284. AVCaptureDeviceFormat *format = capture->device.activeFormat;
  285. CMMediaType mtype = CMFormatDescriptionGetMediaType(
  286. format.formatDescription);
  287. // TODO: support other media types
  288. if (mtype != kCMMediaType_Video) {
  289. AVLOG(LOG_ERROR, "CMMediaType '%s' is unsupported",
  290. AV_FOURCC_STR(mtype));
  291. return false;
  292. }
  293. capture->out.videoSettings = nil;
  294. FourCharCode subtype = uint_from_dict(capture->out.videoSettings,
  295. kCVPixelBufferPixelFormatTypeKey);
  296. if (format_from_subtype(subtype) != VIDEO_FORMAT_NONE) {
  297. AVLOG(LOG_DEBUG, "Using native fourcc '%s'",
  298. AV_FOURCC_STR(subtype));
  299. return true;
  300. }
  301. AVLOG(LOG_DEBUG, "Using fallback fourcc '%s' ('%s' 0x%08x unsupported)",
  302. AV_FOURCC_STR(kCVPixelFormatType_32BGRA),
  303. AV_FOURCC_STR(subtype), subtype);
  304. capture->out.videoSettings = @{
  305. (__bridge NSString*)kCVPixelBufferPixelFormatTypeKey:
  306. @(kCVPixelFormatType_32BGRA)
  307. };
  308. return true;
  309. }
  310. static NSArray *presets(void);
  311. static NSString *preset_names(NSString *preset);
  312. static NSString *select_preset(AVCaptureDevice *dev, NSString *cur_preset)
  313. {
  314. NSString *new_preset = nil;
  315. bool found_previous_preset = false;
  316. for (NSString *preset in presets().reverseObjectEnumerator) {
  317. if (!found_previous_preset)
  318. found_previous_preset =
  319. [cur_preset isEqualToString:preset];
  320. if (![dev supportsAVCaptureSessionPreset:preset])
  321. continue;
  322. if (!new_preset || !found_previous_preset)
  323. new_preset = preset;
  324. }
  325. return new_preset;
  326. }
  327. static void capture_device(struct av_capture *capture, AVCaptureDevice *dev,
  328. obs_data_t *settings)
  329. {
  330. capture->device = dev;
  331. const char *name = capture->device.localizedName.UTF8String;
  332. obs_data_set_string(settings, "device_name", name);
  333. obs_data_set_string(settings, "device",
  334. capture->device.uniqueID.UTF8String);
  335. AVLOG(LOG_INFO, "Selected device '%s'", name);
  336. if (obs_data_get_bool(settings, "use_preset")) {
  337. NSString *preset = get_string(settings, "preset");
  338. if (![dev supportsAVCaptureSessionPreset:preset]) {
  339. AVLOG(LOG_ERROR, "Preset %s not available",
  340. preset_names(preset).UTF8String);
  341. preset = select_preset(dev, preset);
  342. }
  343. if (!preset) {
  344. AVLOG(LOG_ERROR, "Could not select a preset, "
  345. "initialization failed");
  346. return;
  347. }
  348. capture->session.sessionPreset = preset;
  349. AVLOG(LOG_INFO, "Using preset %s",
  350. preset_names(preset).UTF8String);
  351. }
  352. if (!init_device_input(capture))
  353. goto error_input;
  354. AVCaptureInputPort *port = capture->device_input.ports[0];
  355. capture->has_clock = [port respondsToSelector:@selector(clock)];
  356. if (!init_format(capture))
  357. goto error;
  358. [capture->session startRunning];
  359. return;
  360. error:
  361. [capture->session removeInput:capture->device_input];
  362. AVFREE(capture->device_input);
  363. error_input:
  364. AVFREE(capture->device);
  365. }
  366. static inline void handle_disconnect_capture(struct av_capture *capture,
  367. AVCaptureDevice *dev)
  368. {
  369. if (![dev.uniqueID isEqualTo:capture->uid])
  370. return;
  371. if (!capture->device) {
  372. AVLOG(LOG_ERROR, "Received disconnect for unused device '%s'",
  373. capture->uid.UTF8String);
  374. return;
  375. }
  376. AVLOG(LOG_ERROR, "Device with unique ID '%s' disconnected",
  377. dev.uniqueID.UTF8String);
  378. remove_device(capture);
  379. }
  380. static inline void handle_disconnect(struct av_capture *capture,
  381. AVCaptureDevice *dev)
  382. {
  383. if (!dev)
  384. return;
  385. handle_disconnect_capture(capture, dev);
  386. obs_source_update_properties(capture->source);
  387. }
  388. static inline void handle_connect_capture(struct av_capture *capture,
  389. AVCaptureDevice *dev, obs_data_t *settings)
  390. {
  391. if (![dev.uniqueID isEqualTo:capture->uid])
  392. return;
  393. if (capture->device) {
  394. AVLOG(LOG_ERROR, "Received connect for in-use device '%s'",
  395. capture->uid.UTF8String);
  396. return;
  397. }
  398. AVLOG(LOG_INFO, "Device with unique ID '%s' connected, "
  399. "resuming capture", dev.uniqueID.UTF8String);
  400. capture_device(capture, [dev retain], settings);
  401. }
  402. static inline void handle_connect(struct av_capture *capture,
  403. AVCaptureDevice *dev, obs_data_t *settings)
  404. {
  405. if (!dev)
  406. return;
  407. handle_connect_capture(capture, dev, settings);
  408. obs_source_update_properties(capture->source);
  409. }
  410. static void av_capture_init(struct av_capture *capture, obs_data_t *settings)
  411. {
  412. if (!init_session(capture))
  413. return;
  414. capture->uid = [get_string(settings, "device") retain];
  415. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  416. capture->disconnect_observer = [nc
  417. addObserverForName:AVCaptureDeviceWasDisconnectedNotification
  418. object:nil
  419. queue:[NSOperationQueue mainQueue]
  420. usingBlock:^(NSNotification *note)
  421. {
  422. handle_disconnect(capture, note.object);
  423. }
  424. ];
  425. capture->connect_observer = [nc
  426. addObserverForName:AVCaptureDeviceWasConnectedNotification
  427. object:nil
  428. queue:[NSOperationQueue mainQueue]
  429. usingBlock:^(NSNotification *note)
  430. {
  431. handle_connect(capture, note.object, settings);
  432. }
  433. ];
  434. AVCaptureDevice *dev =
  435. [[AVCaptureDevice deviceWithUniqueID:capture->uid] retain];
  436. if (!dev) {
  437. if (capture->uid.length < 1)
  438. AVLOG(LOG_ERROR, "No device selected");
  439. else
  440. AVLOG(LOG_ERROR, "Could not initialize device " \
  441. "with unique ID '%s'",
  442. capture->uid.UTF8String);
  443. return;
  444. }
  445. capture_device(capture, dev, settings);
  446. }
  447. static void *av_capture_create(obs_data_t *settings, obs_source_t *source)
  448. {
  449. UNUSED_PARAMETER(source);
  450. struct av_capture *capture = bzalloc(sizeof(struct av_capture));
  451. capture->source = source;
  452. av_capture_init(capture, settings);
  453. if (!capture->session) {
  454. AVLOG(LOG_ERROR, "No valid session, returning NULL context");
  455. av_capture_destroy(capture);
  456. bfree(capture);
  457. capture = NULL;
  458. }
  459. return capture;
  460. }
  461. static NSArray *presets(void)
  462. {
  463. return @[
  464. //AVCaptureSessionPresetiFrame1280x720,
  465. //AVCaptureSessionPresetiFrame960x540,
  466. AVCaptureSessionPreset1280x720,
  467. AVCaptureSessionPreset960x540,
  468. AVCaptureSessionPreset640x480,
  469. AVCaptureSessionPreset352x288,
  470. AVCaptureSessionPreset320x240,
  471. //AVCaptureSessionPresetHigh,
  472. //AVCaptureSessionPresetMedium,
  473. //AVCaptureSessionPresetLow,
  474. //AVCaptureSessionPresetPhoto,
  475. ];
  476. }
  477. static NSString *preset_names(NSString *preset)
  478. {
  479. NSDictionary *preset_names = @{
  480. AVCaptureSessionPresetLow:@"Low",
  481. AVCaptureSessionPresetMedium:@"Medium",
  482. AVCaptureSessionPresetHigh:@"High",
  483. AVCaptureSessionPreset320x240:@"320x240",
  484. AVCaptureSessionPreset352x288:@"352x288",
  485. AVCaptureSessionPreset640x480:@"640x480",
  486. AVCaptureSessionPreset960x540:@"960x540",
  487. AVCaptureSessionPreset1280x720:@"1280x720",
  488. };
  489. NSString *name = preset_names[preset];
  490. if (name)
  491. return name;
  492. return [NSString stringWithFormat:@"Unknown (%@)", preset];
  493. }
  494. static void av_capture_defaults(obs_data_t *settings)
  495. {
  496. obs_data_set_default_bool(settings, "use_preset", true);
  497. obs_data_set_default_string(settings, "preset",
  498. AVCaptureSessionPreset1280x720.UTF8String);
  499. }
  500. static bool update_device_list(obs_property_t *list,
  501. NSString *uid, NSString *name, bool disconnected)
  502. {
  503. bool dev_found = false;
  504. bool list_modified = false;
  505. size_t size = obs_property_list_item_count(list);
  506. for (size_t i = 0; i < size;) {
  507. const char *uid_ = obs_property_list_item_string(list, i);
  508. bool found = [uid isEqualToString:@(uid_)];
  509. bool disabled = obs_property_list_item_disabled(list, i);
  510. if (!found && !disabled) {
  511. i += 1;
  512. continue;
  513. }
  514. if (disabled && !found) {
  515. list_modified = true;
  516. obs_property_list_item_remove(list, i);
  517. continue;
  518. }
  519. if (disabled != disconnected)
  520. list_modified = true;
  521. dev_found = true;
  522. obs_property_list_item_disable(list, i, disconnected);
  523. i += 1;
  524. }
  525. if (dev_found)
  526. return list_modified;
  527. size_t idx = obs_property_list_add_string(list, name.UTF8String,
  528. uid.UTF8String);
  529. obs_property_list_item_disable(list, idx, disconnected);
  530. return true;
  531. }
  532. static void fill_presets(AVCaptureDevice *dev, obs_property_t *list,
  533. NSString *current_preset)
  534. {
  535. obs_property_list_clear(list);
  536. bool preset_found = false;
  537. for (NSString *preset in presets()) {
  538. bool is_current = [preset isEqualToString:current_preset];
  539. bool supported = dev &&
  540. [dev supportsAVCaptureSessionPreset:preset];
  541. if (is_current)
  542. preset_found = true;
  543. if (!supported && !is_current)
  544. continue;
  545. size_t idx = obs_property_list_add_string(list,
  546. preset_names(preset).UTF8String,
  547. preset.UTF8String);
  548. obs_property_list_item_disable(list, idx, !supported);
  549. }
  550. if (preset_found)
  551. return;
  552. size_t idx = obs_property_list_add_string(list,
  553. preset_names(current_preset).UTF8String,
  554. current_preset.UTF8String);
  555. obs_property_list_item_disable(list, idx, true);
  556. }
  557. static bool check_preset(AVCaptureDevice *dev,
  558. obs_property_t *list, obs_data_t *settings)
  559. {
  560. NSString *current_preset = get_string(settings, "preset");
  561. size_t size = obs_property_list_item_count(list);
  562. NSMutableSet *listed = [NSMutableSet setWithCapacity:size];
  563. for (size_t i = 0; i < size; i++)
  564. [listed addObject:@(obs_property_list_item_string(list, i))];
  565. bool presets_changed = false;
  566. for (NSString *preset in presets()) {
  567. bool is_listed = [listed member:preset] != nil;
  568. bool supported = dev &&
  569. [dev supportsAVCaptureSessionPreset:preset];
  570. if (supported == is_listed)
  571. continue;
  572. presets_changed = true;
  573. }
  574. if (!presets_changed && [listed member:current_preset] != nil)
  575. return false;
  576. fill_presets(dev, list, current_preset);
  577. return true;
  578. }
  579. static bool autoselect_preset(AVCaptureDevice *dev, obs_data_t *settings)
  580. {
  581. NSString *preset = get_string(settings, "preset");
  582. if (!dev || [dev supportsAVCaptureSessionPreset:preset]) {
  583. if (obs_data_has_autoselect_value(settings, "preset")) {
  584. obs_data_unset_autoselect_value(settings, "preset");
  585. return true;
  586. }
  587. } else {
  588. preset = select_preset(dev, preset);
  589. const char *autoselect =
  590. obs_data_get_autoselect_string(settings, "preset");
  591. if (![preset isEqualToString:@(autoselect)]) {
  592. obs_data_set_autoselect_string(settings, "preset",
  593. preset.UTF8String);
  594. return true;
  595. }
  596. }
  597. return false;
  598. }
  599. static bool properties_device_changed(obs_properties_t *props, obs_property_t *p,
  600. obs_data_t *settings)
  601. {
  602. NSString *uid = get_string(settings, "device");
  603. AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
  604. NSString *name = get_string(settings, "device_name");
  605. bool dev_list_updated = update_device_list(p, uid, name, !dev);
  606. p = obs_properties_get(props, "preset");
  607. bool preset_list_changed = check_preset(dev, p, settings);
  608. bool autoselect_changed = autoselect_preset(dev, settings);
  609. return preset_list_changed || autoselect_changed || dev_list_updated;
  610. }
  611. static bool properties_preset_changed(obs_properties_t *props, obs_property_t *p,
  612. obs_data_t *settings)
  613. {
  614. UNUSED_PARAMETER(props);
  615. NSString *uid = get_string(settings, "device");
  616. AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
  617. bool preset_list_changed = check_preset(dev, p, settings);
  618. bool autoselect_changed = autoselect_preset(dev, settings);
  619. return preset_list_changed || autoselect_changed;
  620. }
  621. static obs_properties_t *av_capture_properties(void *unused)
  622. {
  623. UNUSED_PARAMETER(unused);
  624. obs_properties_t *props = obs_properties_create();
  625. obs_property_t *dev_list = obs_properties_add_list(props, "device",
  626. TEXT_DEVICE, OBS_COMBO_TYPE_LIST,
  627. OBS_COMBO_FORMAT_STRING);
  628. for (AVCaptureDevice *dev in [AVCaptureDevice
  629. devicesWithMediaType:AVMediaTypeVideo]) {
  630. obs_property_list_add_string(dev_list,
  631. dev.localizedName.UTF8String,
  632. dev.uniqueID.UTF8String);
  633. }
  634. obs_property_set_modified_callback(dev_list,
  635. properties_device_changed);
  636. obs_property_t *use_preset = obs_properties_add_bool(props,
  637. "use_preset", TEXT_USE_PRESET);
  638. // TODO: implement manual configuration
  639. obs_property_set_enabled(use_preset, false);
  640. obs_property_t *preset_list = obs_properties_add_list(props, "preset",
  641. TEXT_PRESET, OBS_COMBO_TYPE_LIST,
  642. OBS_COMBO_FORMAT_STRING);
  643. for (NSString *preset in presets())
  644. obs_property_list_add_string(preset_list,
  645. preset_names(preset).UTF8String,
  646. preset.UTF8String);
  647. obs_property_set_modified_callback(preset_list,
  648. properties_preset_changed);
  649. return props;
  650. }
  651. static void switch_device(struct av_capture *capture, NSString *uid,
  652. obs_data_t *settings)
  653. {
  654. if (!uid)
  655. return;
  656. if (capture->device)
  657. remove_device(capture);
  658. AVFREE(capture->uid);
  659. capture->uid = [uid retain];
  660. AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
  661. if (!dev) {
  662. AVLOG(LOG_ERROR, "Device with unique id '%s' not found",
  663. uid.UTF8String);
  664. return;
  665. }
  666. capture_device(capture, [dev retain], settings);
  667. }
  668. static void av_capture_update(void *data, obs_data_t *settings)
  669. {
  670. struct av_capture *capture = data;
  671. NSString *uid = get_string(settings, "device");
  672. if (!capture->device || ![capture->device.uniqueID isEqualToString:uid])
  673. return switch_device(capture, uid, settings);
  674. NSString *preset = get_string(settings, "preset");
  675. if (![capture->device supportsAVCaptureSessionPreset:preset]) {
  676. AVLOG(LOG_ERROR, "Preset %s not available", preset.UTF8String);
  677. preset = select_preset(capture->device, preset);
  678. }
  679. capture->session.sessionPreset = preset;
  680. AVLOG(LOG_INFO, "Selected preset %s", preset.UTF8String);
  681. }
  682. struct obs_source_info av_capture_info = {
  683. .id = "av_capture_input",
  684. .type = OBS_SOURCE_TYPE_INPUT,
  685. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  686. .get_name = av_capture_getname,
  687. .create = av_capture_create,
  688. .destroy = av_capture_destroy,
  689. .get_defaults = av_capture_defaults,
  690. .get_properties = av_capture_properties,
  691. .update = av_capture_update,
  692. };