avfoundation.m 45 KB

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