avfoundation.m 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. * AVFoundation input device
  3. * Copyright (c) 2014 Thilo Borgmann <thilo.borgmann@mail.de>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * AVFoundation input device
  24. * @author Thilo Borgmann <thilo.borgmann@mail.de>
  25. */
  26. #import <AVFoundation/AVFoundation.h>
  27. #include <pthread.h>
  28. #include "libavutil/channel_layout.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavformat/demux.h"
  33. #include "libavformat/internal.h"
  34. #include "libavutil/internal.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/time.h"
  37. #include "libavutil/imgutils.h"
  38. #include "avdevice.h"
  39. static const int avf_time_base = 1000000;
  40. static const AVRational avf_time_base_q = {
  41. .num = 1,
  42. .den = avf_time_base
  43. };
  44. struct AVFPixelFormatSpec {
  45. enum AVPixelFormat ff_id;
  46. OSType avf_id;
  47. };
  48. static const struct AVFPixelFormatSpec avf_pixel_formats[] = {
  49. { AV_PIX_FMT_MONOBLACK, kCVPixelFormatType_1Monochrome },
  50. { AV_PIX_FMT_RGB555BE, kCVPixelFormatType_16BE555 },
  51. { AV_PIX_FMT_RGB555LE, kCVPixelFormatType_16LE555 },
  52. { AV_PIX_FMT_RGB565BE, kCVPixelFormatType_16BE565 },
  53. { AV_PIX_FMT_RGB565LE, kCVPixelFormatType_16LE565 },
  54. { AV_PIX_FMT_RGB24, kCVPixelFormatType_24RGB },
  55. { AV_PIX_FMT_BGR24, kCVPixelFormatType_24BGR },
  56. { AV_PIX_FMT_0RGB, kCVPixelFormatType_32ARGB },
  57. { AV_PIX_FMT_BGR0, kCVPixelFormatType_32BGRA },
  58. { AV_PIX_FMT_0BGR, kCVPixelFormatType_32ABGR },
  59. { AV_PIX_FMT_RGB0, kCVPixelFormatType_32RGBA },
  60. { AV_PIX_FMT_BGR48BE, kCVPixelFormatType_48RGB },
  61. { AV_PIX_FMT_UYVY422, kCVPixelFormatType_422YpCbCr8 },
  62. { AV_PIX_FMT_YUVA444P, kCVPixelFormatType_4444YpCbCrA8R },
  63. { AV_PIX_FMT_YUVA444P16LE, kCVPixelFormatType_4444AYpCbCr16 },
  64. { AV_PIX_FMT_YUV444P, kCVPixelFormatType_444YpCbCr8 },
  65. { AV_PIX_FMT_YUV422P16, kCVPixelFormatType_422YpCbCr16 },
  66. { AV_PIX_FMT_YUV422P10, kCVPixelFormatType_422YpCbCr10 },
  67. { AV_PIX_FMT_YUV444P10, kCVPixelFormatType_444YpCbCr10 },
  68. { AV_PIX_FMT_YUV420P, kCVPixelFormatType_420YpCbCr8Planar },
  69. { AV_PIX_FMT_NV12, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange },
  70. { AV_PIX_FMT_YUYV422, kCVPixelFormatType_422YpCbCr8_yuvs },
  71. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
  72. { AV_PIX_FMT_GRAY8, kCVPixelFormatType_OneComponent8 },
  73. #endif
  74. { AV_PIX_FMT_NONE, 0 }
  75. };
  76. typedef struct
  77. {
  78. AVClass* class;
  79. int frames_captured;
  80. int audio_frames_captured;
  81. pthread_mutex_t frame_lock;
  82. id avf_delegate;
  83. id avf_audio_delegate;
  84. AVRational framerate;
  85. int width, height;
  86. int capture_cursor;
  87. int capture_mouse_clicks;
  88. int capture_raw_data;
  89. int drop_late_frames;
  90. int video_is_muxed;
  91. int video_is_screen;
  92. int list_devices;
  93. int video_device_index;
  94. int video_stream_index;
  95. int audio_device_index;
  96. int audio_stream_index;
  97. char *url;
  98. char *video_filename;
  99. char *audio_filename;
  100. int num_video_devices;
  101. int audio_channels;
  102. int audio_bits_per_sample;
  103. int audio_float;
  104. int audio_be;
  105. int audio_signed_integer;
  106. int audio_packed;
  107. int audio_non_interleaved;
  108. int32_t *audio_buffer;
  109. int audio_buffer_size;
  110. enum AVPixelFormat pixel_format;
  111. AVCaptureSession *capture_session;
  112. AVCaptureVideoDataOutput *video_output;
  113. AVCaptureAudioDataOutput *audio_output;
  114. CMSampleBufferRef current_frame;
  115. CMSampleBufferRef current_audio_frame;
  116. AVCaptureDevice *observed_device;
  117. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  118. AVCaptureDeviceTransportControlsPlaybackMode observed_mode;
  119. #endif
  120. int observed_quit;
  121. } AVFContext;
  122. static void lock_frames(AVFContext* ctx)
  123. {
  124. pthread_mutex_lock(&ctx->frame_lock);
  125. }
  126. static void unlock_frames(AVFContext* ctx)
  127. {
  128. pthread_mutex_unlock(&ctx->frame_lock);
  129. }
  130. /** FrameReciever class - delegate for AVCaptureSession
  131. */
  132. @interface AVFFrameReceiver : NSObject
  133. {
  134. AVFContext* _context;
  135. }
  136. - (id)initWithContext:(AVFContext*)context;
  137. - (void) captureOutput:(AVCaptureOutput *)captureOutput
  138. didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
  139. fromConnection:(AVCaptureConnection *)connection;
  140. @end
  141. @implementation AVFFrameReceiver
  142. - (id)initWithContext:(AVFContext*)context
  143. {
  144. if (self = [super init]) {
  145. _context = context;
  146. // start observing if a device is set for it
  147. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  148. if (_context->observed_device) {
  149. NSString *keyPath = NSStringFromSelector(@selector(transportControlsPlaybackMode));
  150. NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew;
  151. [_context->observed_device addObserver: self
  152. forKeyPath: keyPath
  153. options: options
  154. context: _context];
  155. }
  156. #endif
  157. }
  158. return self;
  159. }
  160. - (void)dealloc {
  161. // stop observing if a device is set for it
  162. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  163. if (_context->observed_device) {
  164. NSString *keyPath = NSStringFromSelector(@selector(transportControlsPlaybackMode));
  165. [_context->observed_device removeObserver: self forKeyPath: keyPath];
  166. }
  167. #endif
  168. [super dealloc];
  169. }
  170. - (void)observeValueForKeyPath:(NSString *)keyPath
  171. ofObject:(id)object
  172. change:(NSDictionary *)change
  173. context:(void *)context {
  174. if (context == _context) {
  175. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  176. AVCaptureDeviceTransportControlsPlaybackMode mode =
  177. [change[NSKeyValueChangeNewKey] integerValue];
  178. if (mode != _context->observed_mode) {
  179. if (mode == AVCaptureDeviceTransportControlsNotPlayingMode) {
  180. _context->observed_quit = 1;
  181. }
  182. _context->observed_mode = mode;
  183. }
  184. #endif
  185. } else {
  186. [super observeValueForKeyPath: keyPath
  187. ofObject: object
  188. change: change
  189. context: context];
  190. }
  191. }
  192. - (void) captureOutput:(AVCaptureOutput *)captureOutput
  193. didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
  194. fromConnection:(AVCaptureConnection *)connection
  195. {
  196. lock_frames(_context);
  197. if (_context->current_frame != nil) {
  198. CFRelease(_context->current_frame);
  199. }
  200. _context->current_frame = (CMSampleBufferRef)CFRetain(videoFrame);
  201. unlock_frames(_context);
  202. ++_context->frames_captured;
  203. }
  204. @end
  205. /** AudioReciever class - delegate for AVCaptureSession
  206. */
  207. @interface AVFAudioReceiver : NSObject
  208. {
  209. AVFContext* _context;
  210. }
  211. - (id)initWithContext:(AVFContext*)context;
  212. - (void) captureOutput:(AVCaptureOutput *)captureOutput
  213. didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
  214. fromConnection:(AVCaptureConnection *)connection;
  215. @end
  216. @implementation AVFAudioReceiver
  217. - (id)initWithContext:(AVFContext*)context
  218. {
  219. if (self = [super init]) {
  220. _context = context;
  221. }
  222. return self;
  223. }
  224. - (void) captureOutput:(AVCaptureOutput *)captureOutput
  225. didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
  226. fromConnection:(AVCaptureConnection *)connection
  227. {
  228. lock_frames(_context);
  229. if (_context->current_audio_frame != nil) {
  230. CFRelease(_context->current_audio_frame);
  231. }
  232. _context->current_audio_frame = (CMSampleBufferRef)CFRetain(audioFrame);
  233. unlock_frames(_context);
  234. ++_context->audio_frames_captured;
  235. }
  236. @end
  237. static void destroy_context(AVFContext* ctx)
  238. {
  239. [ctx->capture_session stopRunning];
  240. [ctx->capture_session release];
  241. [ctx->video_output release];
  242. [ctx->audio_output release];
  243. [ctx->avf_delegate release];
  244. [ctx->avf_audio_delegate release];
  245. ctx->capture_session = NULL;
  246. ctx->video_output = NULL;
  247. ctx->audio_output = NULL;
  248. ctx->avf_delegate = NULL;
  249. ctx->avf_audio_delegate = NULL;
  250. av_freep(&ctx->url);
  251. av_freep(&ctx->audio_buffer);
  252. pthread_mutex_destroy(&ctx->frame_lock);
  253. if (ctx->current_frame) {
  254. CFRelease(ctx->current_frame);
  255. }
  256. }
  257. static int parse_device_name(AVFormatContext *s)
  258. {
  259. AVFContext *ctx = (AVFContext*)s->priv_data;
  260. char *save;
  261. ctx->url = av_strdup(s->url);
  262. if (!ctx->url)
  263. return AVERROR(ENOMEM);
  264. if (ctx->url[0] != ':') {
  265. ctx->video_filename = av_strtok(ctx->url, ":", &save);
  266. ctx->audio_filename = av_strtok(NULL, ":", &save);
  267. } else {
  268. ctx->audio_filename = av_strtok(ctx->url, ":", &save);
  269. }
  270. return 0;
  271. }
  272. /**
  273. * Configure the video device.
  274. *
  275. * Configure the video device using a run-time approach to access properties
  276. * since formats, activeFormat are available since iOS >= 7.0 or OSX >= 10.7
  277. * and activeVideoMaxFrameDuration is available since i0S >= 7.0 and OSX >= 10.9.
  278. *
  279. * The NSUndefinedKeyException must be handled by the caller of this function.
  280. *
  281. */
  282. static int configure_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
  283. {
  284. AVFContext *ctx = (AVFContext*)s->priv_data;
  285. double framerate = av_q2d(ctx->framerate);
  286. NSObject *range = nil;
  287. NSObject *format = nil;
  288. NSObject *selected_range = nil;
  289. NSObject *selected_format = nil;
  290. // try to configure format by formats list
  291. // might raise an exception if no format list is given
  292. // (then fallback to default, no configuration)
  293. @try {
  294. for (format in [video_device valueForKey:@"formats"]) {
  295. CMFormatDescriptionRef formatDescription;
  296. CMVideoDimensions dimensions;
  297. formatDescription = (CMFormatDescriptionRef) [format performSelector:@selector(formatDescription)];
  298. dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
  299. if ((ctx->width == 0 && ctx->height == 0) ||
  300. (dimensions.width == ctx->width && dimensions.height == ctx->height)) {
  301. selected_format = format;
  302. for (range in [format valueForKey:@"videoSupportedFrameRateRanges"]) {
  303. double max_framerate;
  304. [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
  305. if (fabs (framerate - max_framerate) < 0.01) {
  306. selected_range = range;
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. if (!selected_format) {
  313. av_log(s, AV_LOG_ERROR, "Selected video size (%dx%d) is not supported by the device.\n",
  314. ctx->width, ctx->height);
  315. goto unsupported_format;
  316. }
  317. if (!selected_range) {
  318. av_log(s, AV_LOG_ERROR, "Selected framerate (%f) is not supported by the device.\n",
  319. framerate);
  320. if (ctx->video_is_muxed) {
  321. av_log(s, AV_LOG_ERROR, "Falling back to default.\n");
  322. } else {
  323. goto unsupported_format;
  324. }
  325. }
  326. if ([video_device lockForConfiguration:NULL] == YES) {
  327. if (selected_format) {
  328. [video_device setValue:selected_format forKey:@"activeFormat"];
  329. }
  330. if (selected_range) {
  331. NSValue *min_frame_duration = [selected_range valueForKey:@"minFrameDuration"];
  332. [video_device setValue:min_frame_duration forKey:@"activeVideoMinFrameDuration"];
  333. [video_device setValue:min_frame_duration forKey:@"activeVideoMaxFrameDuration"];
  334. }
  335. } else {
  336. av_log(s, AV_LOG_ERROR, "Could not lock device for configuration.\n");
  337. return AVERROR(EINVAL);
  338. }
  339. } @catch(NSException *e) {
  340. av_log(ctx, AV_LOG_WARNING, "Configuration of video device failed, falling back to default.\n");
  341. }
  342. return 0;
  343. unsupported_format:
  344. av_log(s, AV_LOG_ERROR, "Supported modes:\n");
  345. for (format in [video_device valueForKey:@"formats"]) {
  346. CMFormatDescriptionRef formatDescription;
  347. CMVideoDimensions dimensions;
  348. formatDescription = (CMFormatDescriptionRef) [format performSelector:@selector(formatDescription)];
  349. dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
  350. for (range in [format valueForKey:@"videoSupportedFrameRateRanges"]) {
  351. double min_framerate;
  352. double max_framerate;
  353. [[range valueForKey:@"minFrameRate"] getValue:&min_framerate];
  354. [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
  355. av_log(s, AV_LOG_ERROR, " %dx%d@[%f %f]fps\n",
  356. dimensions.width, dimensions.height,
  357. min_framerate, max_framerate);
  358. }
  359. }
  360. return AVERROR(EINVAL);
  361. }
  362. static int add_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
  363. {
  364. AVFContext *ctx = (AVFContext*)s->priv_data;
  365. int ret;
  366. NSError *error = nil;
  367. AVCaptureInput* capture_input = nil;
  368. struct AVFPixelFormatSpec pxl_fmt_spec;
  369. NSNumber *pixel_format;
  370. NSDictionary *capture_dict;
  371. dispatch_queue_t queue;
  372. if (ctx->video_device_index < ctx->num_video_devices) {
  373. capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device error:&error] autorelease];
  374. } else {
  375. capture_input = (AVCaptureInput*) video_device;
  376. }
  377. if (!capture_input) {
  378. av_log(s, AV_LOG_ERROR, "Failed to create AV capture input device: %s\n",
  379. [[error localizedDescription] UTF8String]);
  380. return 1;
  381. }
  382. if ([ctx->capture_session canAddInput:capture_input]) {
  383. [ctx->capture_session addInput:capture_input];
  384. } else {
  385. av_log(s, AV_LOG_ERROR, "can't add video input to capture session\n");
  386. return 1;
  387. }
  388. // Attaching output
  389. ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
  390. if (!ctx->video_output) {
  391. av_log(s, AV_LOG_ERROR, "Failed to init AV video output\n");
  392. return 1;
  393. }
  394. // Configure device framerate and video size
  395. @try {
  396. if ((ret = configure_video_device(s, video_device)) < 0) {
  397. return ret;
  398. }
  399. } @catch (NSException *exception) {
  400. if (![[exception name] isEqualToString:NSUndefinedKeyException]) {
  401. av_log (s, AV_LOG_ERROR, "An error occurred: %s", [exception.reason UTF8String]);
  402. return AVERROR_EXTERNAL;
  403. }
  404. }
  405. // select pixel format
  406. pxl_fmt_spec.ff_id = AV_PIX_FMT_NONE;
  407. for (int i = 0; avf_pixel_formats[i].ff_id != AV_PIX_FMT_NONE; i++) {
  408. if (ctx->pixel_format == avf_pixel_formats[i].ff_id) {
  409. pxl_fmt_spec = avf_pixel_formats[i];
  410. break;
  411. }
  412. }
  413. // check if selected pixel format is supported by AVFoundation
  414. if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
  415. av_log(s, AV_LOG_ERROR, "Selected pixel format (%s) is not supported by AVFoundation.\n",
  416. av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
  417. return 1;
  418. }
  419. // check if the pixel format is available for this device
  420. if ([[ctx->video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.avf_id]] == NSNotFound) {
  421. av_log(s, AV_LOG_ERROR, "Selected pixel format (%s) is not supported by the input device.\n",
  422. av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
  423. pxl_fmt_spec.ff_id = AV_PIX_FMT_NONE;
  424. av_log(s, AV_LOG_ERROR, "Supported pixel formats:\n");
  425. for (NSNumber *pxl_fmt in [ctx->video_output availableVideoCVPixelFormatTypes]) {
  426. struct AVFPixelFormatSpec pxl_fmt_dummy;
  427. pxl_fmt_dummy.ff_id = AV_PIX_FMT_NONE;
  428. for (int i = 0; avf_pixel_formats[i].ff_id != AV_PIX_FMT_NONE; i++) {
  429. if ([pxl_fmt intValue] == avf_pixel_formats[i].avf_id) {
  430. pxl_fmt_dummy = avf_pixel_formats[i];
  431. break;
  432. }
  433. }
  434. if (pxl_fmt_dummy.ff_id != AV_PIX_FMT_NONE) {
  435. av_log(s, AV_LOG_ERROR, " %s\n", av_get_pix_fmt_name(pxl_fmt_dummy.ff_id));
  436. // select first supported pixel format instead of user selected (or default) pixel format
  437. if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
  438. pxl_fmt_spec = pxl_fmt_dummy;
  439. }
  440. }
  441. }
  442. // fail if there is no appropriate pixel format or print a warning about overriding the pixel format
  443. if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
  444. return 1;
  445. } else {
  446. av_log(s, AV_LOG_WARNING, "Overriding selected pixel format to use %s instead.\n",
  447. av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
  448. }
  449. }
  450. // set videoSettings to an empty dict for receiving raw data of muxed devices
  451. if (ctx->capture_raw_data) {
  452. ctx->pixel_format = pxl_fmt_spec.ff_id;
  453. ctx->video_output.videoSettings = @{ };
  454. } else {
  455. ctx->pixel_format = pxl_fmt_spec.ff_id;
  456. pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
  457. capture_dict = [NSDictionary dictionaryWithObject:pixel_format
  458. forKey:(id)kCVPixelBufferPixelFormatTypeKey];
  459. [ctx->video_output setVideoSettings:capture_dict];
  460. }
  461. [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames];
  462. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  463. // check for transport control support and set observer device if supported
  464. if (!ctx->video_is_screen) {
  465. int trans_ctrl = [video_device transportControlsSupported];
  466. AVCaptureDeviceTransportControlsPlaybackMode trans_mode = [video_device transportControlsPlaybackMode];
  467. if (trans_ctrl) {
  468. ctx->observed_mode = trans_mode;
  469. ctx->observed_device = video_device;
  470. }
  471. }
  472. #endif
  473. ctx->avf_delegate = [[AVFFrameReceiver alloc] initWithContext:ctx];
  474. queue = dispatch_queue_create("avf_queue", NULL);
  475. [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
  476. dispatch_release(queue);
  477. if ([ctx->capture_session canAddOutput:ctx->video_output]) {
  478. [ctx->capture_session addOutput:ctx->video_output];
  479. } else {
  480. av_log(s, AV_LOG_ERROR, "can't add video output to capture session\n");
  481. return 1;
  482. }
  483. return 0;
  484. }
  485. static int add_audio_device(AVFormatContext *s, AVCaptureDevice *audio_device)
  486. {
  487. AVFContext *ctx = (AVFContext*)s->priv_data;
  488. NSError *error = nil;
  489. AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device error:&error] autorelease];
  490. dispatch_queue_t queue;
  491. if (!audio_dev_input) {
  492. av_log(s, AV_LOG_ERROR, "Failed to create AV capture input device: %s\n",
  493. [[error localizedDescription] UTF8String]);
  494. return 1;
  495. }
  496. if ([ctx->capture_session canAddInput:audio_dev_input]) {
  497. [ctx->capture_session addInput:audio_dev_input];
  498. } else {
  499. av_log(s, AV_LOG_ERROR, "can't add audio input to capture session\n");
  500. return 1;
  501. }
  502. // Attaching output
  503. ctx->audio_output = [[AVCaptureAudioDataOutput alloc] init];
  504. if (!ctx->audio_output) {
  505. av_log(s, AV_LOG_ERROR, "Failed to init AV audio output\n");
  506. return 1;
  507. }
  508. ctx->avf_audio_delegate = [[AVFAudioReceiver alloc] initWithContext:ctx];
  509. queue = dispatch_queue_create("avf_audio_queue", NULL);
  510. [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
  511. dispatch_release(queue);
  512. if ([ctx->capture_session canAddOutput:ctx->audio_output]) {
  513. [ctx->capture_session addOutput:ctx->audio_output];
  514. } else {
  515. av_log(s, AV_LOG_ERROR, "adding audio output to capture session failed\n");
  516. return 1;
  517. }
  518. return 0;
  519. }
  520. static int get_video_config(AVFormatContext *s)
  521. {
  522. AVFContext *ctx = (AVFContext*)s->priv_data;
  523. CVImageBufferRef image_buffer;
  524. CMBlockBufferRef block_buffer;
  525. CGSize image_buffer_size;
  526. AVStream* stream = avformat_new_stream(s, NULL);
  527. if (!stream) {
  528. return 1;
  529. }
  530. // Take stream info from the first frame.
  531. while (ctx->frames_captured < 1) {
  532. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
  533. }
  534. lock_frames(ctx);
  535. ctx->video_stream_index = stream->index;
  536. avpriv_set_pts_info(stream, 64, 1, avf_time_base);
  537. image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
  538. block_buffer = CMSampleBufferGetDataBuffer(ctx->current_frame);
  539. if (image_buffer) {
  540. image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
  541. stream->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  542. stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  543. stream->codecpar->width = (int)image_buffer_size.width;
  544. stream->codecpar->height = (int)image_buffer_size.height;
  545. stream->codecpar->format = ctx->pixel_format;
  546. } else {
  547. stream->codecpar->codec_id = AV_CODEC_ID_DVVIDEO;
  548. stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  549. stream->codecpar->format = ctx->pixel_format;
  550. }
  551. CFRelease(ctx->current_frame);
  552. ctx->current_frame = nil;
  553. unlock_frames(ctx);
  554. return 0;
  555. }
  556. static int get_audio_config(AVFormatContext *s)
  557. {
  558. AVFContext *ctx = (AVFContext*)s->priv_data;
  559. CMFormatDescriptionRef format_desc;
  560. AVStream* stream = avformat_new_stream(s, NULL);
  561. if (!stream) {
  562. return 1;
  563. }
  564. // Take stream info from the first frame.
  565. while (ctx->audio_frames_captured < 1) {
  566. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
  567. }
  568. lock_frames(ctx);
  569. ctx->audio_stream_index = stream->index;
  570. avpriv_set_pts_info(stream, 64, 1, avf_time_base);
  571. format_desc = CMSampleBufferGetFormatDescription(ctx->current_audio_frame);
  572. const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
  573. if (!basic_desc) {
  574. unlock_frames(ctx);
  575. av_log(s, AV_LOG_ERROR, "audio format not available\n");
  576. return 1;
  577. }
  578. stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  579. stream->codecpar->sample_rate = basic_desc->mSampleRate;
  580. av_channel_layout_default(&stream->codecpar->ch_layout, basic_desc->mChannelsPerFrame);
  581. ctx->audio_channels = basic_desc->mChannelsPerFrame;
  582. ctx->audio_bits_per_sample = basic_desc->mBitsPerChannel;
  583. ctx->audio_float = basic_desc->mFormatFlags & kAudioFormatFlagIsFloat;
  584. ctx->audio_be = basic_desc->mFormatFlags & kAudioFormatFlagIsBigEndian;
  585. ctx->audio_signed_integer = basic_desc->mFormatFlags & kAudioFormatFlagIsSignedInteger;
  586. ctx->audio_packed = basic_desc->mFormatFlags & kAudioFormatFlagIsPacked;
  587. ctx->audio_non_interleaved = basic_desc->mFormatFlags & kAudioFormatFlagIsNonInterleaved;
  588. if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
  589. ctx->audio_float &&
  590. ctx->audio_bits_per_sample == 32 &&
  591. ctx->audio_packed) {
  592. stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
  593. } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
  594. ctx->audio_signed_integer &&
  595. ctx->audio_bits_per_sample == 16 &&
  596. ctx->audio_packed) {
  597. stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
  598. } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
  599. ctx->audio_signed_integer &&
  600. ctx->audio_bits_per_sample == 24 &&
  601. ctx->audio_packed) {
  602. stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
  603. } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
  604. ctx->audio_signed_integer &&
  605. ctx->audio_bits_per_sample == 32 &&
  606. ctx->audio_packed) {
  607. stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
  608. } else {
  609. unlock_frames(ctx);
  610. av_log(s, AV_LOG_ERROR, "audio format is not supported\n");
  611. return 1;
  612. }
  613. if (ctx->audio_non_interleaved) {
  614. CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame);
  615. ctx->audio_buffer_size = CMBlockBufferGetDataLength(block_buffer);
  616. ctx->audio_buffer = av_malloc(ctx->audio_buffer_size);
  617. if (!ctx->audio_buffer) {
  618. unlock_frames(ctx);
  619. av_log(s, AV_LOG_ERROR, "error allocating audio buffer\n");
  620. return 1;
  621. }
  622. }
  623. CFRelease(ctx->current_audio_frame);
  624. ctx->current_audio_frame = nil;
  625. unlock_frames(ctx);
  626. return 0;
  627. }
  628. static NSArray* getDevicesWithMediaType(AVMediaType mediaType) {
  629. #if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500))
  630. NSMutableArray *deviceTypes = nil;
  631. if (mediaType == AVMediaTypeVideo) {
  632. deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]];
  633. #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000)
  634. [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualCamera];
  635. [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTelephotoCamera];
  636. #endif
  637. #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110100)
  638. [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTrueDepthCamera];
  639. #endif
  640. #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 130000)
  641. [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTripleCamera];
  642. [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualWideCamera];
  643. [deviceTypes addObject: AVCaptureDeviceTypeBuiltInUltraWideCamera];
  644. #endif
  645. #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000)
  646. [deviceTypes addObject: AVCaptureDeviceTypeDeskViewCamera];
  647. #endif
  648. #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 150400)
  649. [deviceTypes addObject: AVCaptureDeviceTypeBuiltInLiDARDepthCamera];
  650. #endif
  651. #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
  652. [deviceTypes addObject: AVCaptureDeviceTypeContinuityCamera];
  653. [deviceTypes addObject: AVCaptureDeviceTypeExternal];
  654. #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
  655. [deviceTypes addObject: AVCaptureDeviceTypeExternalUnknown];
  656. #endif
  657. } else if (mediaType == AVMediaTypeAudio) {
  658. #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
  659. deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeMicrophone]];
  660. #else
  661. deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInMicrophone]];
  662. #endif
  663. } else if (mediaType == AVMediaTypeMuxed) {
  664. #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
  665. deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternal]];
  666. #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
  667. deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternalUnknown]];
  668. #else
  669. return nil;
  670. #endif
  671. } else {
  672. return nil;
  673. }
  674. AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession =
  675. [AVCaptureDeviceDiscoverySession
  676. discoverySessionWithDeviceTypes:deviceTypes
  677. mediaType:mediaType
  678. position:AVCaptureDevicePositionUnspecified];
  679. return [captureDeviceDiscoverySession devices];
  680. #else
  681. return [AVCaptureDevice devicesWithMediaType:mediaType];
  682. #endif
  683. }
  684. static int avf_read_header(AVFormatContext *s)
  685. {
  686. int ret = 0;
  687. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  688. uint32_t num_screens = 0;
  689. AVFContext *ctx = (AVFContext*)s->priv_data;
  690. AVCaptureDevice *video_device = nil;
  691. AVCaptureDevice *audio_device = nil;
  692. // Find capture device
  693. NSArray *devices = getDevicesWithMediaType(AVMediaTypeVideo);
  694. NSArray *devices_muxed = getDevicesWithMediaType(AVMediaTypeMuxed);
  695. ctx->num_video_devices = [devices count] + [devices_muxed count];
  696. pthread_mutex_init(&ctx->frame_lock, NULL);
  697. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  698. CGGetActiveDisplayList(0, NULL, &num_screens);
  699. #endif
  700. // List devices if requested
  701. if (ctx->list_devices) {
  702. int index = 0;
  703. av_log(ctx, AV_LOG_INFO, "AVFoundation video devices:\n");
  704. for (AVCaptureDevice *device in devices) {
  705. const char *name = [[device localizedName] UTF8String];
  706. index = [devices indexOfObject:device];
  707. av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
  708. }
  709. for (AVCaptureDevice *device in devices_muxed) {
  710. const char *name = [[device localizedName] UTF8String];
  711. index = [devices count] + [devices_muxed indexOfObject:device];
  712. av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
  713. }
  714. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  715. if (num_screens > 0) {
  716. CGDirectDisplayID screens[num_screens];
  717. CGGetActiveDisplayList(num_screens, screens, &num_screens);
  718. for (int i = 0; i < num_screens; i++) {
  719. av_log(ctx, AV_LOG_INFO, "[%d] Capture screen %d\n", ctx->num_video_devices + i, i);
  720. }
  721. }
  722. #endif
  723. av_log(ctx, AV_LOG_INFO, "AVFoundation audio devices:\n");
  724. devices = getDevicesWithMediaType(AVMediaTypeAudio);
  725. for (AVCaptureDevice *device in devices) {
  726. const char *name = [[device localizedName] UTF8String];
  727. int index = [devices indexOfObject:device];
  728. av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
  729. }
  730. goto fail;
  731. }
  732. // parse input filename for video and audio device
  733. ret = parse_device_name(s);
  734. if (ret)
  735. goto fail;
  736. // check for device index given in filename
  737. if (ctx->video_device_index == -1 && ctx->video_filename) {
  738. sscanf(ctx->video_filename, "%d", &ctx->video_device_index);
  739. }
  740. if (ctx->audio_device_index == -1 && ctx->audio_filename) {
  741. sscanf(ctx->audio_filename, "%d", &ctx->audio_device_index);
  742. }
  743. if (ctx->video_device_index >= 0) {
  744. if (ctx->video_device_index < ctx->num_video_devices) {
  745. if (ctx->video_device_index < [devices count]) {
  746. video_device = [devices objectAtIndex:ctx->video_device_index];
  747. } else {
  748. video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])];
  749. ctx->video_is_muxed = 1;
  750. }
  751. } else if (ctx->video_device_index < ctx->num_video_devices + num_screens) {
  752. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  753. CGDirectDisplayID screens[num_screens];
  754. CGGetActiveDisplayList(num_screens, screens, &num_screens);
  755. AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->num_video_devices]] autorelease];
  756. if (ctx->framerate.num > 0) {
  757. capture_screen_input.minFrameDuration = CMTimeMake(ctx->framerate.den, ctx->framerate.num);
  758. }
  759. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
  760. if (ctx->capture_cursor) {
  761. capture_screen_input.capturesCursor = YES;
  762. } else {
  763. capture_screen_input.capturesCursor = NO;
  764. }
  765. #endif
  766. if (ctx->capture_mouse_clicks) {
  767. capture_screen_input.capturesMouseClicks = YES;
  768. } else {
  769. capture_screen_input.capturesMouseClicks = NO;
  770. }
  771. video_device = (AVCaptureDevice*) capture_screen_input;
  772. ctx->video_is_screen = 1;
  773. #endif
  774. } else {
  775. av_log(ctx, AV_LOG_ERROR, "Invalid device index\n");
  776. goto fail;
  777. }
  778. } else if (ctx->video_filename &&
  779. strncmp(ctx->video_filename, "none", 4)) {
  780. if (!strncmp(ctx->video_filename, "default", 7)) {
  781. video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  782. } else {
  783. // looking for video inputs
  784. for (AVCaptureDevice *device in devices) {
  785. if (!strncmp(ctx->video_filename, [[device localizedName] UTF8String], strlen(ctx->video_filename))) {
  786. video_device = device;
  787. break;
  788. }
  789. }
  790. // looking for muxed inputs
  791. for (AVCaptureDevice *device in devices_muxed) {
  792. if (!strncmp(ctx->video_filename, [[device localizedName] UTF8String], strlen(ctx->video_filename))) {
  793. video_device = device;
  794. ctx->video_is_muxed = 1;
  795. break;
  796. }
  797. }
  798. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  799. // looking for screen inputs
  800. if (!video_device) {
  801. int idx;
  802. if(sscanf(ctx->video_filename, "Capture screen %d", &idx) && idx < num_screens) {
  803. CGDirectDisplayID screens[num_screens];
  804. CGGetActiveDisplayList(num_screens, screens, &num_screens);
  805. AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
  806. video_device = (AVCaptureDevice*) capture_screen_input;
  807. ctx->video_device_index = ctx->num_video_devices + idx;
  808. ctx->video_is_screen = 1;
  809. if (ctx->framerate.num > 0) {
  810. capture_screen_input.minFrameDuration = CMTimeMake(ctx->framerate.den, ctx->framerate.num);
  811. }
  812. #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
  813. if (ctx->capture_cursor) {
  814. capture_screen_input.capturesCursor = YES;
  815. } else {
  816. capture_screen_input.capturesCursor = NO;
  817. }
  818. #endif
  819. if (ctx->capture_mouse_clicks) {
  820. capture_screen_input.capturesMouseClicks = YES;
  821. } else {
  822. capture_screen_input.capturesMouseClicks = NO;
  823. }
  824. }
  825. }
  826. #endif
  827. }
  828. if (!video_device) {
  829. av_log(ctx, AV_LOG_ERROR, "Video device not found\n");
  830. goto fail;
  831. }
  832. }
  833. // get audio device
  834. if (ctx->audio_device_index >= 0) {
  835. NSArray *devices = getDevicesWithMediaType(AVMediaTypeAudio);
  836. if (ctx->audio_device_index >= [devices count]) {
  837. av_log(ctx, AV_LOG_ERROR, "Invalid audio device index\n");
  838. goto fail;
  839. }
  840. audio_device = [devices objectAtIndex:ctx->audio_device_index];
  841. } else if (ctx->audio_filename &&
  842. strncmp(ctx->audio_filename, "none", 4)) {
  843. if (!strncmp(ctx->audio_filename, "default", 7)) {
  844. audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
  845. } else {
  846. NSArray *devices = getDevicesWithMediaType(AVMediaTypeAudio);
  847. for (AVCaptureDevice *device in devices) {
  848. if (!strncmp(ctx->audio_filename, [[device localizedName] UTF8String], strlen(ctx->audio_filename))) {
  849. audio_device = device;
  850. break;
  851. }
  852. }
  853. }
  854. if (!audio_device) {
  855. av_log(ctx, AV_LOG_ERROR, "Audio device not found\n");
  856. goto fail;
  857. }
  858. }
  859. // Video nor Audio capture device not found, looking for AVMediaTypeVideo/Audio
  860. if (!video_device && !audio_device) {
  861. av_log(s, AV_LOG_ERROR, "No AV capture device found\n");
  862. goto fail;
  863. }
  864. if (video_device) {
  865. if (ctx->video_device_index < ctx->num_video_devices) {
  866. av_log(s, AV_LOG_DEBUG, "'%s' opened\n", [[video_device localizedName] UTF8String]);
  867. } else {
  868. av_log(s, AV_LOG_DEBUG, "'%s' opened\n", [[video_device description] UTF8String]);
  869. }
  870. }
  871. if (audio_device) {
  872. av_log(s, AV_LOG_DEBUG, "audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
  873. }
  874. // Initialize capture session
  875. ctx->capture_session = [[AVCaptureSession alloc] init];
  876. if (video_device && add_video_device(s, video_device)) {
  877. goto fail;
  878. }
  879. if (audio_device && add_audio_device(s, audio_device)) {
  880. }
  881. [ctx->capture_session startRunning];
  882. /* Unlock device configuration only after the session is started so it
  883. * does not reset the capture formats */
  884. if (!ctx->video_is_screen) {
  885. [video_device unlockForConfiguration];
  886. }
  887. if (video_device && get_video_config(s)) {
  888. goto fail;
  889. }
  890. // set audio stream
  891. if (audio_device && get_audio_config(s)) {
  892. goto fail;
  893. }
  894. [pool release];
  895. return 0;
  896. fail:
  897. [pool release];
  898. destroy_context(ctx);
  899. if (ret)
  900. return ret;
  901. return AVERROR(EIO);
  902. }
  903. static int copy_cvpixelbuffer(AVFormatContext *s,
  904. CVPixelBufferRef image_buffer,
  905. AVPacket *pkt)
  906. {
  907. AVFContext *ctx = s->priv_data;
  908. int src_linesize[4];
  909. const uint8_t *src_data[4];
  910. int width = CVPixelBufferGetWidth(image_buffer);
  911. int height = CVPixelBufferGetHeight(image_buffer);
  912. int status;
  913. memset(src_linesize, 0, sizeof(src_linesize));
  914. memset(src_data, 0, sizeof(src_data));
  915. status = CVPixelBufferLockBaseAddress(image_buffer, 0);
  916. if (status != kCVReturnSuccess) {
  917. av_log(s, AV_LOG_ERROR, "Could not lock base address: %d (%dx%d)\n", status, width, height);
  918. return AVERROR_EXTERNAL;
  919. }
  920. if (CVPixelBufferIsPlanar(image_buffer)) {
  921. size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
  922. int i;
  923. for(i = 0; i < plane_count; i++){
  924. src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer, i);
  925. src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer, i);
  926. }
  927. } else {
  928. src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
  929. src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
  930. }
  931. status = av_image_copy_to_buffer(pkt->data, pkt->size,
  932. src_data, src_linesize,
  933. ctx->pixel_format, width, height, 1);
  934. CVPixelBufferUnlockBaseAddress(image_buffer, 0);
  935. return status;
  936. }
  937. static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
  938. {
  939. AVFContext* ctx = (AVFContext*)s->priv_data;
  940. do {
  941. CVImageBufferRef image_buffer;
  942. CMBlockBufferRef block_buffer;
  943. lock_frames(ctx);
  944. if (ctx->current_frame != nil) {
  945. int status;
  946. int length = 0;
  947. image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
  948. block_buffer = CMSampleBufferGetDataBuffer(ctx->current_frame);
  949. if (image_buffer != nil) {
  950. length = (int)CVPixelBufferGetDataSize(image_buffer);
  951. } else if (block_buffer != nil) {
  952. length = (int)CMBlockBufferGetDataLength(block_buffer);
  953. } else {
  954. unlock_frames(ctx);
  955. return AVERROR(EINVAL);
  956. }
  957. if (av_new_packet(pkt, length) < 0) {
  958. unlock_frames(ctx);
  959. return AVERROR(EIO);
  960. }
  961. CMItemCount count;
  962. CMSampleTimingInfo timing_info;
  963. if (CMSampleBufferGetOutputSampleTimingInfoArray(ctx->current_frame, 1, &timing_info, &count) == noErr) {
  964. AVRational timebase_q = av_make_q(1, timing_info.presentationTimeStamp.timescale);
  965. pkt->pts = pkt->dts = av_rescale_q(timing_info.presentationTimeStamp.value, timebase_q, avf_time_base_q);
  966. }
  967. pkt->stream_index = ctx->video_stream_index;
  968. pkt->flags |= AV_PKT_FLAG_KEY;
  969. if (image_buffer) {
  970. status = copy_cvpixelbuffer(s, image_buffer, pkt);
  971. } else {
  972. status = 0;
  973. OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, pkt->data);
  974. if (ret != kCMBlockBufferNoErr) {
  975. status = AVERROR(EIO);
  976. }
  977. }
  978. CFRelease(ctx->current_frame);
  979. ctx->current_frame = nil;
  980. if (status < 0) {
  981. unlock_frames(ctx);
  982. return status;
  983. }
  984. } else if (ctx->current_audio_frame != nil) {
  985. CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame);
  986. int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
  987. if (!block_buffer || !block_buffer_size) {
  988. unlock_frames(ctx);
  989. return AVERROR(EIO);
  990. }
  991. if (ctx->audio_non_interleaved && block_buffer_size > ctx->audio_buffer_size) {
  992. unlock_frames(ctx);
  993. return AVERROR_BUFFER_TOO_SMALL;
  994. }
  995. if (av_new_packet(pkt, block_buffer_size) < 0) {
  996. unlock_frames(ctx);
  997. return AVERROR(EIO);
  998. }
  999. CMItemCount count;
  1000. CMSampleTimingInfo timing_info;
  1001. if (CMSampleBufferGetOutputSampleTimingInfoArray(ctx->current_audio_frame, 1, &timing_info, &count) == noErr) {
  1002. AVRational timebase_q = av_make_q(1, timing_info.presentationTimeStamp.timescale);
  1003. pkt->pts = pkt->dts = av_rescale_q(timing_info.presentationTimeStamp.value, timebase_q, avf_time_base_q);
  1004. }
  1005. pkt->stream_index = ctx->audio_stream_index;
  1006. pkt->flags |= AV_PKT_FLAG_KEY;
  1007. if (ctx->audio_non_interleaved) {
  1008. int sample, c, shift, num_samples;
  1009. OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, ctx->audio_buffer);
  1010. if (ret != kCMBlockBufferNoErr) {
  1011. unlock_frames(ctx);
  1012. return AVERROR(EIO);
  1013. }
  1014. num_samples = pkt->size / (ctx->audio_channels * (ctx->audio_bits_per_sample >> 3));
  1015. // transform decoded frame into output format
  1016. #define INTERLEAVE_OUTPUT(bps) \
  1017. { \
  1018. int##bps##_t **src; \
  1019. int##bps##_t *dest; \
  1020. src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
  1021. if (!src) { \
  1022. unlock_frames(ctx); \
  1023. return AVERROR(EIO); \
  1024. } \
  1025. \
  1026. for (c = 0; c < ctx->audio_channels; c++) { \
  1027. src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
  1028. } \
  1029. dest = (int##bps##_t*)pkt->data; \
  1030. shift = bps - ctx->audio_bits_per_sample; \
  1031. for (sample = 0; sample < num_samples; sample++) \
  1032. for (c = 0; c < ctx->audio_channels; c++) \
  1033. *dest++ = src[c][sample] << shift; \
  1034. av_freep(&src); \
  1035. }
  1036. if (ctx->audio_bits_per_sample <= 16) {
  1037. INTERLEAVE_OUTPUT(16)
  1038. } else {
  1039. INTERLEAVE_OUTPUT(32)
  1040. }
  1041. } else {
  1042. OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, pkt->data);
  1043. if (ret != kCMBlockBufferNoErr) {
  1044. unlock_frames(ctx);
  1045. return AVERROR(EIO);
  1046. }
  1047. }
  1048. CFRelease(ctx->current_audio_frame);
  1049. ctx->current_audio_frame = nil;
  1050. } else {
  1051. pkt->data = NULL;
  1052. unlock_frames(ctx);
  1053. if (ctx->observed_quit) {
  1054. return AVERROR_EOF;
  1055. } else {
  1056. return AVERROR(EAGAIN);
  1057. }
  1058. }
  1059. unlock_frames(ctx);
  1060. } while (!pkt->data);
  1061. return 0;
  1062. }
  1063. static int avf_close(AVFormatContext *s)
  1064. {
  1065. AVFContext* ctx = (AVFContext*)s->priv_data;
  1066. destroy_context(ctx);
  1067. return 0;
  1068. }
  1069. static const AVOption options[] = {
  1070. { "list_devices", "list available devices", offsetof(AVFContext, list_devices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  1071. { "video_device_index", "select video device by index for devices with same name (starts at 0)", offsetof(AVFContext, video_device_index), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  1072. { "audio_device_index", "select audio device by index for devices with same name (starts at 0)", offsetof(AVFContext, audio_device_index), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  1073. { "pixel_format", "set pixel format", offsetof(AVFContext, pixel_format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_YUV420P}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
  1074. { "framerate", "set frame rate", offsetof(AVFContext, framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  1075. { "video_size", "set video size", offsetof(AVFContext, width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  1076. { "capture_cursor", "capture the screen cursor", offsetof(AVFContext, capture_cursor), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  1077. { "capture_mouse_clicks", "capture the screen mouse clicks", offsetof(AVFContext, capture_mouse_clicks), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  1078. { "capture_raw_data", "capture the raw data from device connection", offsetof(AVFContext, capture_raw_data), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  1079. { "drop_late_frames", "drop frames that are available later than expected", offsetof(AVFContext, drop_late_frames), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  1080. { NULL },
  1081. };
  1082. static const AVClass avf_class = {
  1083. .class_name = "AVFoundation indev",
  1084. .item_name = av_default_item_name,
  1085. .option = options,
  1086. .version = LIBAVUTIL_VERSION_INT,
  1087. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  1088. };
  1089. const FFInputFormat ff_avfoundation_demuxer = {
  1090. .p.name = "avfoundation",
  1091. .p.long_name = NULL_IF_CONFIG_SMALL("AVFoundation input device"),
  1092. .p.flags = AVFMT_NOFILE,
  1093. .p.priv_class = &avf_class,
  1094. .priv_data_size = sizeof(AVFContext),
  1095. .read_header = avf_read_header,
  1096. .read_packet = avf_read_packet,
  1097. .read_close = avf_close,
  1098. };