avfoundation.m 48 KB

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