av-capture.m 23 KB

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