av-capture.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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_getname(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. if (!update_frame(capture, frame, sampleBuffer))
  192. return;
  193. obs_source_output_video(capture->source, frame);
  194. CVImageBufferRef img = CMSampleBufferGetImageBuffer(sampleBuffer);
  195. CVPixelBufferUnlockBaseAddress(img, kCVPixelBufferLock_ReadOnly);
  196. }
  197. @end
  198. static const char *av_capture_getname(void)
  199. {
  200. return TEXT_AVCAPTURE;
  201. }
  202. static void remove_device(struct av_capture *capture)
  203. {
  204. [capture->session stopRunning];
  205. [capture->session removeInput:capture->device_input];
  206. AVFREE(capture->device_input);
  207. AVFREE(capture->device);
  208. }
  209. static void av_capture_destroy(void *data)
  210. {
  211. struct av_capture *capture = data;
  212. if (!capture)
  213. return;
  214. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  215. [nc removeObserver:capture->connect_observer];
  216. [nc removeObserver:capture->disconnect_observer];
  217. remove_device(capture);
  218. AVFREE(capture->out);
  219. if (capture->queue)
  220. dispatch_release(capture->queue);
  221. AVFREE(capture->delegate);
  222. AVFREE(capture->session);
  223. AVFREE(capture->uid);
  224. bfree(capture);
  225. }
  226. static NSString *get_string(obs_data_t data, char const *name)
  227. {
  228. return @(obs_data_getstring(data, name));
  229. }
  230. static bool init_session(struct av_capture *capture)
  231. {
  232. capture->session = [[AVCaptureSession alloc] init];
  233. if (!capture->session) {
  234. AVLOG(LOG_ERROR, "Could not create AVCaptureSession");
  235. goto error;
  236. }
  237. capture->delegate = [[OBSAVCaptureDelegate alloc] init];
  238. if (!capture->delegate) {
  239. AVLOG(LOG_ERROR, "Could not create OBSAVCaptureDelegate");
  240. goto error;
  241. }
  242. capture->delegate->capture = capture;
  243. capture->out = [[AVCaptureVideoDataOutput alloc] init];
  244. if (!capture->out) {
  245. AVLOG(LOG_ERROR, "Could not create AVCaptureVideoDataOutput");
  246. goto error;
  247. }
  248. capture->queue = dispatch_queue_create(NULL, NULL);
  249. if (!capture->queue) {
  250. AVLOG(LOG_ERROR, "Could not create dispatch queue");
  251. goto error;
  252. }
  253. [capture->session addOutput:capture->out];
  254. [capture->out
  255. setSampleBufferDelegate:capture->delegate
  256. queue:capture->queue];
  257. return true;
  258. error:
  259. AVFREE(capture->session);
  260. AVFREE(capture->delegate);
  261. AVFREE(capture->out);
  262. return false;
  263. }
  264. static bool init_device_input(struct av_capture *capture)
  265. {
  266. NSError *err = nil;
  267. capture->device_input = [[AVCaptureDeviceInput
  268. deviceInputWithDevice:capture->device error:&err] retain];
  269. if (!capture->device_input) {
  270. AVLOG(LOG_ERROR, "Error while initializing device input: %s",
  271. err.localizedFailureReason.UTF8String);
  272. return false;
  273. }
  274. [capture->session addInput:capture->device_input];
  275. return true;
  276. }
  277. static uint32_t uint_from_dict(NSDictionary *dict, CFStringRef key)
  278. {
  279. return ((NSNumber*)dict[(__bridge NSString*)key]).unsignedIntValue;
  280. }
  281. static bool init_format(struct av_capture *capture)
  282. {
  283. AVCaptureDeviceFormat *format = capture->device.activeFormat;
  284. CMMediaType mtype = CMFormatDescriptionGetMediaType(
  285. format.formatDescription);
  286. // TODO: support other media types
  287. if (mtype != kCMMediaType_Video) {
  288. AVLOG(LOG_ERROR, "CMMediaType '%s' is unsupported",
  289. AV_FOURCC_STR(mtype));
  290. return false;
  291. }
  292. capture->out.videoSettings = nil;
  293. FourCharCode subtype = uint_from_dict(capture->out.videoSettings,
  294. kCVPixelBufferPixelFormatTypeKey);
  295. if (format_from_subtype(subtype) != VIDEO_FORMAT_NONE) {
  296. AVLOG(LOG_DEBUG, "Using native fourcc '%s'",
  297. AV_FOURCC_STR(subtype));
  298. return true;
  299. }
  300. AVLOG(LOG_DEBUG, "Using fallback fourcc '%s' ('%s' 0x%08x unsupported)",
  301. AV_FOURCC_STR(kCVPixelFormatType_32BGRA),
  302. AV_FOURCC_STR(subtype), subtype);
  303. capture->out.videoSettings = @{
  304. (__bridge NSString*)kCVPixelBufferPixelFormatTypeKey:
  305. @(kCVPixelFormatType_32BGRA)
  306. };
  307. return true;
  308. }
  309. static NSArray *presets(void);
  310. static NSString *preset_names(NSString *preset);
  311. static NSString *select_preset(AVCaptureDevice *dev, NSString *cur_preset)
  312. {
  313. NSString *new_preset = nil;
  314. bool found_previous_preset = false;
  315. for (NSString *preset in presets().reverseObjectEnumerator) {
  316. if (!found_previous_preset)
  317. found_previous_preset =
  318. [cur_preset isEqualToString:preset];
  319. if (![dev supportsAVCaptureSessionPreset:preset])
  320. continue;
  321. if (!new_preset || !found_previous_preset)
  322. new_preset = preset;
  323. }
  324. return new_preset;
  325. }
  326. static void capture_device(struct av_capture *capture, AVCaptureDevice *dev,
  327. obs_data_t settings)
  328. {
  329. capture->device = dev;
  330. const char *name = capture->device.localizedName.UTF8String;
  331. obs_data_setstring(settings, "device_name", name);
  332. obs_data_setstring(settings, "device",
  333. capture->device.uniqueID.UTF8String);
  334. AVLOG(LOG_INFO, "Selected device '%s'", name);
  335. if (obs_data_getbool(settings, "use_preset")) {
  336. NSString *preset = get_string(settings, "preset");
  337. if (![dev supportsAVCaptureSessionPreset:preset]) {
  338. AVLOG(LOG_ERROR, "Preset %s not available",
  339. preset_names(preset).UTF8String);
  340. preset = select_preset(dev, preset);
  341. }
  342. if (!preset) {
  343. AVLOG(LOG_ERROR, "Could not select a preset, "
  344. "initialization failed");
  345. return;
  346. }
  347. capture->session.sessionPreset = preset;
  348. AVLOG(LOG_INFO, "Using preset %s",
  349. preset_names(preset).UTF8String);
  350. }
  351. if (!init_device_input(capture))
  352. goto error_input;
  353. AVCaptureInputPort *port = capture->device_input.ports[0];
  354. capture->has_clock = [port respondsToSelector:@selector(clock)];
  355. if (!init_format(capture))
  356. goto error;
  357. [capture->session startRunning];
  358. return;
  359. error:
  360. [capture->session removeInput:capture->device_input];
  361. AVFREE(capture->device_input);
  362. error_input:
  363. AVFREE(capture->device);
  364. }
  365. static inline void handle_disconnect(struct av_capture* capture,
  366. AVCaptureDevice *dev)
  367. {
  368. if (!dev)
  369. return;
  370. if (![dev.uniqueID isEqualTo:capture->uid])
  371. return;
  372. if (!capture->device) {
  373. AVLOG(LOG_ERROR, "Received disconnect for unused device '%s'",
  374. capture->uid.UTF8String);
  375. return;
  376. }
  377. AVLOG(LOG_ERROR, "Device with unique ID '%s' disconnected",
  378. dev.uniqueID.UTF8String);
  379. remove_device(capture);
  380. }
  381. static inline void handle_connect(struct av_capture *capture,
  382. AVCaptureDevice *dev, obs_data_t settings)
  383. {
  384. if (!dev)
  385. return;
  386. if (![dev.uniqueID isEqualTo:capture->uid])
  387. return;
  388. if (capture->device) {
  389. AVLOG(LOG_ERROR, "Received connect for in-use device '%s'",
  390. capture->uid.UTF8String);
  391. return;
  392. }
  393. AVLOG(LOG_INFO, "Device with unique ID '%s' connected, "
  394. "resuming capture", dev.uniqueID.UTF8String);
  395. capture_device(capture, [dev retain], settings);
  396. }
  397. static void av_capture_init(struct av_capture *capture, obs_data_t settings)
  398. {
  399. if (!init_session(capture))
  400. return;
  401. capture->uid = [get_string(settings, "device") retain];
  402. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  403. capture->disconnect_observer = [nc
  404. addObserverForName:AVCaptureDeviceWasDisconnectedNotification
  405. object:nil
  406. queue:[NSOperationQueue mainQueue]
  407. usingBlock:^(NSNotification *note)
  408. {
  409. handle_disconnect(capture, note.object);
  410. }
  411. ];
  412. capture->connect_observer = [nc
  413. addObserverForName:AVCaptureDeviceWasConnectedNotification
  414. object:nil
  415. queue:[NSOperationQueue mainQueue]
  416. usingBlock:^(NSNotification *note)
  417. {
  418. handle_connect(capture, note.object, settings);
  419. }
  420. ];
  421. AVCaptureDevice *dev =
  422. [[AVCaptureDevice deviceWithUniqueID:capture->uid] retain];
  423. if (!dev) {
  424. if (capture->uid.length < 1)
  425. AVLOG(LOG_ERROR, "No device selected");
  426. else
  427. AVLOG(LOG_ERROR, "Could not initialize device " \
  428. "with unique ID '%s'",
  429. capture->uid.UTF8String);
  430. return;
  431. }
  432. capture_device(capture, dev, settings);
  433. }
  434. static void *av_capture_create(obs_data_t settings, obs_source_t source)
  435. {
  436. UNUSED_PARAMETER(source);
  437. struct av_capture *capture = bzalloc(sizeof(struct av_capture));
  438. capture->source = source;
  439. av_capture_init(capture, settings);
  440. if (!capture->session) {
  441. AVLOG(LOG_ERROR, "No valid session, returning NULL context");
  442. av_capture_destroy(capture);
  443. bfree(capture);
  444. capture = NULL;
  445. }
  446. return capture;
  447. }
  448. static NSArray *presets(void)
  449. {
  450. return @[
  451. //AVCaptureSessionPresetiFrame1280x720,
  452. //AVCaptureSessionPresetiFrame960x540,
  453. AVCaptureSessionPreset1280x720,
  454. AVCaptureSessionPreset960x540,
  455. AVCaptureSessionPreset640x480,
  456. AVCaptureSessionPreset352x288,
  457. AVCaptureSessionPreset320x240,
  458. //AVCaptureSessionPresetHigh,
  459. //AVCaptureSessionPresetMedium,
  460. //AVCaptureSessionPresetLow,
  461. //AVCaptureSessionPresetPhoto,
  462. ];
  463. }
  464. static NSString *preset_names(NSString *preset)
  465. {
  466. NSDictionary *preset_names = @{
  467. AVCaptureSessionPresetLow:@"Low",
  468. AVCaptureSessionPresetMedium:@"Medium",
  469. AVCaptureSessionPresetHigh:@"High",
  470. AVCaptureSessionPreset320x240:@"320x240",
  471. AVCaptureSessionPreset352x288:@"352x288",
  472. AVCaptureSessionPreset640x480:@"640x480",
  473. AVCaptureSessionPreset960x540:@"960x540",
  474. AVCaptureSessionPreset1280x720:@"1280x720",
  475. };
  476. NSString *name = preset_names[preset];
  477. if (name)
  478. return name;
  479. return [NSString stringWithFormat:@"Unknown (%@)", preset];
  480. }
  481. static void av_capture_defaults(obs_data_t settings)
  482. {
  483. obs_data_set_default_bool(settings, "use_preset", true);
  484. obs_data_set_default_string(settings, "preset",
  485. AVCaptureSessionPreset1280x720.UTF8String);
  486. }
  487. static bool update_device_list(obs_property_t list,
  488. NSString *uid, NSString *name, bool disconnected)
  489. {
  490. bool dev_found = false;
  491. bool list_modified = false;
  492. size_t size = obs_property_list_item_count(list);
  493. for (size_t i = 0; i < size;) {
  494. const char *uid_ = obs_property_list_item_string(list, i);
  495. bool found = [uid isEqualToString:@(uid_)];
  496. bool disabled = obs_property_list_item_disabled(list, i);
  497. if (!found && !disabled) {
  498. i += 1;
  499. continue;
  500. }
  501. if (disabled && !found) {
  502. list_modified = true;
  503. obs_property_list_item_remove(list, i);
  504. continue;
  505. }
  506. if (disabled != disconnected)
  507. list_modified = true;
  508. dev_found = true;
  509. obs_property_list_item_disable(list, i, disconnected);
  510. i += 1;
  511. }
  512. if (dev_found)
  513. return list_modified;
  514. size_t idx = obs_property_list_add_string(list, name.UTF8String,
  515. uid.UTF8String);
  516. obs_property_list_item_disable(list, idx, disconnected);
  517. return true;
  518. }
  519. static void fill_presets(AVCaptureDevice *dev, obs_property_t list,
  520. NSString *current_preset)
  521. {
  522. obs_property_list_clear(list);
  523. bool preset_found = false;
  524. for (NSString *preset in presets()) {
  525. bool is_current = [preset isEqualToString:current_preset];
  526. bool supported = dev &&
  527. [dev supportsAVCaptureSessionPreset:preset];
  528. if (is_current)
  529. preset_found = true;
  530. if (!supported && !is_current)
  531. continue;
  532. size_t idx = obs_property_list_add_string(list,
  533. preset_names(preset).UTF8String,
  534. preset.UTF8String);
  535. obs_property_list_item_disable(list, idx, !supported);
  536. }
  537. if (preset_found)
  538. return;
  539. size_t idx = obs_property_list_add_string(list,
  540. preset_names(current_preset).UTF8String,
  541. current_preset.UTF8String);
  542. obs_property_list_item_disable(list, idx, true);
  543. }
  544. static bool check_preset(AVCaptureDevice *dev,
  545. obs_property_t list, obs_data_t settings)
  546. {
  547. NSString *current_preset = get_string(settings, "preset");
  548. size_t size = obs_property_list_item_count(list);
  549. NSMutableSet *listed = [NSMutableSet setWithCapacity:size];
  550. for (size_t i = 0; i < size; i++)
  551. [listed addObject:@(obs_property_list_item_string(list, i))];
  552. bool presets_changed = false;
  553. for (NSString *preset in presets()) {
  554. bool is_listed = [listed member:preset] != nil;
  555. bool supported = dev &&
  556. [dev supportsAVCaptureSessionPreset:preset];
  557. if (supported == is_listed)
  558. continue;
  559. presets_changed = true;
  560. }
  561. if (!presets_changed && [listed member:current_preset] != nil)
  562. return false;
  563. fill_presets(dev, list, current_preset);
  564. return true;
  565. }
  566. static bool autoselect_preset(AVCaptureDevice *dev, obs_data_t settings)
  567. {
  568. NSString *preset = get_string(settings, "preset");
  569. if (!dev || [dev supportsAVCaptureSessionPreset:preset]) {
  570. if (obs_data_has_autoselect(settings, "preset")) {
  571. obs_data_unset_autoselect_value(settings, "preset");
  572. return true;
  573. }
  574. } else {
  575. preset = select_preset(dev, preset);
  576. const char *autoselect =
  577. obs_data_get_autoselect_string(settings, "preset");
  578. if (![preset isEqualToString:@(autoselect)]) {
  579. obs_data_set_autoselect_string(settings, "preset",
  580. preset.UTF8String);
  581. return true;
  582. }
  583. }
  584. return false;
  585. }
  586. static bool properties_device_changed(obs_properties_t props, obs_property_t p,
  587. obs_data_t settings)
  588. {
  589. NSString *uid = get_string(settings, "device");
  590. AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
  591. NSString *name = get_string(settings, "device_name");
  592. bool dev_list_updated = update_device_list(p, uid, name, !dev);
  593. p = obs_properties_get(props, "preset");
  594. bool preset_list_changed = check_preset(dev, p, settings);
  595. bool autoselect_changed = autoselect_preset(dev, settings);
  596. return preset_list_changed || autoselect_changed || dev_list_updated;
  597. }
  598. static bool properties_preset_changed(obs_properties_t props, obs_property_t p,
  599. obs_data_t settings)
  600. {
  601. UNUSED_PARAMETER(props);
  602. NSString *uid = get_string(settings, "device");
  603. AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
  604. bool preset_list_changed = check_preset(dev, p, settings);
  605. bool autoselect_changed = autoselect_preset(dev, settings);
  606. return preset_list_changed || autoselect_changed;
  607. }
  608. static obs_properties_t av_capture_properties(void)
  609. {
  610. obs_properties_t props = obs_properties_create();
  611. obs_property_t dev_list = obs_properties_add_list(props, "device",
  612. TEXT_DEVICE, OBS_COMBO_TYPE_LIST,
  613. OBS_COMBO_FORMAT_STRING);
  614. for (AVCaptureDevice *dev in [AVCaptureDevice
  615. devicesWithMediaType:AVMediaTypeVideo]) {
  616. obs_property_list_add_string(dev_list,
  617. dev.localizedName.UTF8String,
  618. dev.uniqueID.UTF8String);
  619. }
  620. obs_property_set_modified_callback(dev_list,
  621. properties_device_changed);
  622. obs_property_t use_preset = obs_properties_add_bool(props,
  623. "use_preset", TEXT_USE_PRESET);
  624. // TODO: implement manual configuration
  625. obs_property_set_enabled(use_preset, false);
  626. obs_property_t preset_list = obs_properties_add_list(props, "preset",
  627. TEXT_PRESET, OBS_COMBO_TYPE_LIST,
  628. OBS_COMBO_FORMAT_STRING);
  629. for (NSString *preset in presets())
  630. obs_property_list_add_string(preset_list,
  631. preset_names(preset).UTF8String,
  632. preset.UTF8String);
  633. obs_property_set_modified_callback(preset_list,
  634. properties_preset_changed);
  635. return props;
  636. }
  637. static void switch_device(struct av_capture *capture, NSString *uid,
  638. obs_data_t settings)
  639. {
  640. if (!uid)
  641. return;
  642. if (capture->device)
  643. remove_device(capture);
  644. AVFREE(capture->uid);
  645. capture->uid = [uid retain];
  646. AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
  647. if (!dev) {
  648. AVLOG(LOG_ERROR, "Device with unique id '%s' not found",
  649. uid.UTF8String);
  650. return;
  651. }
  652. capture_device(capture, [dev retain], settings);
  653. }
  654. static void av_capture_update(void *data, obs_data_t settings)
  655. {
  656. struct av_capture *capture = data;
  657. NSString *uid = get_string(settings, "device");
  658. if (!capture->device || ![capture->device.uniqueID isEqualToString:uid])
  659. return switch_device(capture, uid, settings);
  660. NSString *preset = get_string(settings, "preset");
  661. if (![capture->device supportsAVCaptureSessionPreset:preset]) {
  662. AVLOG(LOG_ERROR, "Preset %s not available", preset.UTF8String);
  663. preset = select_preset(capture->device, preset);
  664. }
  665. capture->session.sessionPreset = preset;
  666. AVLOG(LOG_INFO, "Selected preset %s", preset.UTF8String);
  667. }
  668. struct obs_source_info av_capture_info = {
  669. .id = "av_capture_input",
  670. .type = OBS_SOURCE_TYPE_INPUT,
  671. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  672. .getname = av_capture_getname,
  673. .create = av_capture_create,
  674. .destroy = av_capture_destroy,
  675. .defaults = av_capture_defaults,
  676. .properties = av_capture_properties,
  677. .update = av_capture_update,
  678. };