28 #import <AVFoundation/AVFoundation.h>
74 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
143 - (
void) captureOutput:(AVCaptureOutput *)captureOutput
144 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
145 fromConnection:(AVCaptureConnection *)connection;
153 if (
self = [super
init]) {
159 - (
void) captureOutput:(AVCaptureOutput *)captureOutput
160 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
161 fromConnection:(AVCaptureConnection *)connection
189 - (
void) captureOutput:(AVCaptureOutput *)captureOutput
190 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
191 fromConnection:(AVCaptureConnection *)connection;
199 if (
self = [super
init]) {
205 - (
void) captureOutput:(AVCaptureOutput *)captureOutput
206 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
207 fromConnection:(AVCaptureConnection *)connection
228 [ctx->capture_session stopRunning];
230 [ctx->capture_session release];
231 [ctx->video_output release];
232 [ctx->audio_output release];
233 [ctx->avf_delegate release];
234 [ctx->avf_audio_delegate release];
269 NSError *error = nil;
270 AVCaptureInput* capture_input = nil;
273 capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device error:&error] autorelease];
275 capture_input = (AVCaptureInput*) video_device;
278 if (!capture_input) {
280 [[error localizedDescription] UTF8String]);
285 [ctx->capture_session addInput:capture_input];
292 ctx->
video_output = [[AVCaptureVideoDataOutput alloc] init];
305 pxl_fmt_spec = avf_pixel_formats[i];
312 av_log(s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by AVFoundation.\n",
318 if ([[ctx->
video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.
avf_id]] == NSNotFound) {
319 av_log(s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by the input device.\n",
325 for (NSNumber *pxl_fmt
in [ctx->
video_output availableVideoCVPixelFormatTypes]) {
329 if ([pxl_fmt intValue] == avf_pixel_formats[i].
avf_id) {
330 pxl_fmt_dummy = avf_pixel_formats[i];
340 pxl_fmt_spec = pxl_fmt_dummy;
355 NSNumber *pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
356 NSDictionary *capture_dict = [NSDictionary dictionaryWithObject:pixel_format
357 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
359 [ctx->video_output setVideoSettings:capture_dict];
360 [ctx->video_output setAlwaysDiscardsLateVideoFrames:YES];
364 dispatch_queue_t queue = dispatch_queue_create(
"avf_queue",
NULL);
365 [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
366 dispatch_release(queue);
369 [ctx->capture_session addOutput:ctx->video_output];
381 NSError *error = nil;
382 AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device error:&error] autorelease];
384 if (!audio_dev_input) {
386 [[error localizedDescription] UTF8String]);
391 [ctx->capture_session addInput:audio_dev_input];
398 ctx->
audio_output = [[AVCaptureAudioDataOutput alloc] init];
407 dispatch_queue_t queue = dispatch_queue_create(
"avf_audio_queue",
NULL);
408 [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
409 dispatch_release(queue);
412 [ctx->capture_session addOutput:ctx->audio_output];
427 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
442 CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(ctx->
current_frame);
443 CGSize image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
447 stream->
codec->
width = (int)image_buffer_size.width;
448 stream->
codec->
height = (
int)image_buffer_size.height;
465 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
480 CMFormatDescriptionRef format_desc = CMSampleBufferGetFormatDescription(ctx->
current_audio_frame);
481 const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
495 ctx->
audio_float = basic_desc->mFormatFlags & kAudioFormatFlagIsFloat;
496 ctx->
audio_be = basic_desc->mFormatFlags & kAudioFormatFlagIsBigEndian;
498 ctx->
audio_packed = basic_desc->mFormatFlags & kAudioFormatFlagIsPacked;
501 if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
506 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
511 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
516 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
546 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
550 uint32_t num_screens = 0;
555 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
556 CGGetActiveDisplayList(0,
NULL, &num_screens);
562 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
564 for (AVCaptureDevice *device
in devices) {
565 const char *
name = [[device localizedName] UTF8String];
566 index = [devices indexOfObject:device];
570 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
571 if (num_screens > 0) {
572 CGDirectDisplayID screens[num_screens];
573 CGGetActiveDisplayList(num_screens, screens, &num_screens);
574 for (
int i = 0; i < num_screens; i++) {
581 devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
582 for (AVCaptureDevice *device
in devices) {
583 const char *
name = [[device localizedName] UTF8String];
584 int index = [devices indexOfObject:device];
591 AVCaptureDevice *video_device = nil;
592 AVCaptureDevice *audio_device = nil;
594 NSArray *video_devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
610 video_device = [video_devices objectAtIndex:ctx->video_device_index];
612 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
613 CGDirectDisplayID screens[num_screens];
614 CGGetActiveDisplayList(num_screens, screens, &num_screens);
615 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->
num_video_devices]] autorelease];
616 video_device = (AVCaptureDevice*) capture_screen_input;
625 video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
628 for (AVCaptureDevice *device
in video_devices) {
630 video_device = device;
635 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
639 if(sscanf(ctx->
video_filename,
"Capture screen %d", &idx) && idx < num_screens) {
640 CGDirectDisplayID screens[num_screens];
641 CGGetActiveDisplayList(num_screens, screens, &num_screens);
642 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
643 video_device = (AVCaptureDevice*) capture_screen_input;
658 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
665 audio_device = [devices objectAtIndex:ctx->audio_device_index];
669 audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
671 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
673 for (AVCaptureDevice *device
in devices) {
675 audio_device = device;
688 if (!video_device && !audio_device) {
701 av_log(s,
AV_LOG_DEBUG,
"audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
713 [ctx->capture_session startRunning];
740 CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(ctx->
current_frame);
743 if (
av_new_packet(pkt, (
int)CVPixelBufferGetDataSize(image_buffer)) < 0) {
753 CVPixelBufferLockBaseAddress(image_buffer, 0);
755 void*
data = CVPixelBufferGetBaseAddress(image_buffer);
758 CVPixelBufferUnlockBaseAddress(image_buffer, 0);
763 int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
765 if (!block_buffer || !block_buffer_size) {
788 if (ret != kCMBlockBufferNoErr) {
795 #define INTERLEAVE_OUTPUT(bps) \
797 int##bps##_t **src; \
798 int##bps##_t *dest; \
799 src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
800 if (!src) return AVERROR(EIO); \
801 for (c = 0; c < ctx->audio_channels; c++) { \
802 src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
804 dest = (int##bps##_t*)pkt->data; \
805 shift = bps - ctx->audio_bits_per_sample; \
806 for (sample = 0; sample < num_samples; sample++) \
807 for (c = 0; c < ctx->audio_channels; c++) \
808 *dest++ = src[c][sample] << shift; \
818 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->
size, pkt->
data);
819 if (ret != kCMBlockBufferNoErr) {
845 {
"list_devices",
"list available devices", offsetof(
AVFContext, list_devices),
AV_OPT_TYPE_INT, {.i64=0}, 0, 1,
AV_OPT_FLAG_DECODING_PARAM,
"list_devices" },
863 .
name =
"avfoundation",
870 .priv_class = &avf_class,