28 #import <AVFoundation/AVFoundation.h>
76 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
153 - (void) captureOutput:(AVCaptureOutput *)captureOutput
154 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
155 fromConnection:(AVCaptureConnection *)connection;
163 if (
self = [super
init]) {
169 - (void) captureOutput:(AVCaptureOutput *)captureOutput
170 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
171 fromConnection:(AVCaptureConnection *)connection
199 - (void) captureOutput:(AVCaptureOutput *)captureOutput
200 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
201 fromConnection:(AVCaptureConnection *)connection;
209 if (
self = [super
init]) {
215 - (void) captureOutput:(AVCaptureOutput *)captureOutput
216 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
217 fromConnection:(AVCaptureConnection *)connection
238 [ctx->capture_session stopRunning];
240 [ctx->capture_session release];
241 [ctx->video_output release];
242 [ctx->audio_output release];
243 [ctx->avf_delegate release];
244 [ctx->avf_audio_delegate release];
250 ctx->avf_audio_delegate =
NULL;
257 if (
ctx->current_frame) {
258 CFRelease(
ctx->current_frame);
291 NSObject *range = nil;
293 NSObject *selected_range = nil;
294 NSObject *selected_format = nil;
300 for (
format in [video_device valueForKey:
@"formats"]) {
301 CMFormatDescriptionRef formatDescription;
302 CMVideoDimensions dimensions;
304 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
305 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
307 if ((
ctx->width == 0 &&
ctx->height == 0) ||
308 (dimensions.width ==
ctx->width && dimensions.height ==
ctx->height)) {
312 for (range
in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
313 double max_framerate;
315 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
316 if (fabs (
framerate - max_framerate) < 0.01) {
317 selected_range = range;
324 if (!selected_format) {
327 goto unsupported_format;
330 if (!selected_range) {
333 if (
ctx->video_is_muxed) {
336 goto unsupported_format;
340 if ([video_device lockForConfiguration:
NULL] == YES) {
341 if (selected_format) {
342 [video_device setValue:selected_format forKey:@"activeFormat"];
344 if (selected_range) {
345 NSValue *min_frame_duration = [selected_range valueForKey:@"minFrameDuration"];
346 [video_device setValue:min_frame_duration forKey:@"activeVideoMinFrameDuration"];
347 [video_device setValue:min_frame_duration forKey:@"activeVideoMaxFrameDuration"];
353 }
@catch(NSException *e) {
362 for (
format in [video_device valueForKey:
@"formats"]) {
363 CMFormatDescriptionRef formatDescription;
364 CMVideoDimensions dimensions;
366 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
367 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
369 for (range
in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
370 double min_framerate;
371 double max_framerate;
373 [[range valueForKey:@"minFrameRate"] getValue:&min_framerate];
374 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
376 dimensions.width, dimensions.height,
377 min_framerate, max_framerate);
387 NSError *
error = nil;
388 AVCaptureInput* capture_input = nil;
390 NSNumber *pixel_format;
391 NSDictionary *capture_dict;
392 dispatch_queue_t queue;
394 if (
ctx->video_device_index <
ctx->num_video_devices) {
395 capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device
error:&
error] autorelease];
397 capture_input = (AVCaptureInput*) video_device;
400 if (!capture_input) {
402 [[
error localizedDescription] UTF8String]);
406 if ([
ctx->capture_session canAddInput:capture_input]) {
407 [ctx->capture_session addInput:capture_input];
414 ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
416 if (!
ctx->video_output) {
426 }
@catch (NSException *exception) {
427 if (![[exception
name] isEqualToString:NSUndefinedKeyException]) {
451 if ([[
ctx->video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.avf_id]] == NSNotFound) {
452 av_log(
s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by the input device.\n",
458 for (NSNumber *pxl_fmt
in [
ctx->video_output availableVideoCVPixelFormatTypes]) {
473 pxl_fmt_spec = pxl_fmt_dummy;
488 if (
ctx->capture_raw_data) {
489 ctx->pixel_format = pxl_fmt_spec.ff_id;
490 ctx->video_output.videoSettings = @{ };
492 ctx->pixel_format = pxl_fmt_spec.ff_id;
493 pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
494 capture_dict = [NSDictionary dictionaryWithObject:pixel_format
495 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
497 [ctx->video_output setVideoSettings:capture_dict];
499 [ctx->video_output setAlwaysDiscardsLateVideoFrames:YES];
503 queue = dispatch_queue_create(
"avf_queue",
NULL);
504 [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
505 dispatch_release(queue);
507 if ([
ctx->capture_session canAddOutput:
ctx->video_output]) {
508 [ctx->capture_session addOutput:ctx->video_output];
520 NSError *
error = nil;
521 AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device
error:&
error] autorelease];
522 dispatch_queue_t queue;
524 if (!audio_dev_input) {
526 [[
error localizedDescription] UTF8String]);
530 if ([
ctx->capture_session canAddInput:audio_dev_input]) {
531 [ctx->capture_session addInput:audio_dev_input];
538 ctx->audio_output = [[AVCaptureAudioDataOutput alloc] init];
540 if (!
ctx->audio_output) {
547 queue = dispatch_queue_create(
"avf_audio_queue",
NULL);
548 [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
549 dispatch_release(queue);
551 if ([
ctx->capture_session canAddOutput:
ctx->audio_output]) {
552 [ctx->capture_session addOutput:ctx->audio_output];
564 CVImageBufferRef image_buffer;
565 CMBlockBufferRef block_buffer;
566 CGSize image_buffer_size;
574 while (
ctx->frames_captured < 1) {
575 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
580 ctx->video_stream_index = stream->index;
584 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
585 block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_frame);
588 image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
592 stream->codecpar->width = (
int)image_buffer_size.width;
593 stream->codecpar->height = (
int)image_buffer_size.height;
594 stream->codecpar->format =
ctx->pixel_format;
598 stream->codecpar->format =
ctx->pixel_format;
601 CFRelease(
ctx->current_frame);
602 ctx->current_frame = nil;
612 CMFormatDescriptionRef format_desc;
620 while (
ctx->audio_frames_captured < 1) {
621 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
626 ctx->audio_stream_index = stream->index;
630 format_desc = CMSampleBufferGetFormatDescription(
ctx->current_audio_frame);
631 const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
639 stream->codecpar->sample_rate = basic_desc->mSampleRate;
640 stream->codecpar->channels = basic_desc->mChannelsPerFrame;
643 ctx->audio_channels = basic_desc->mChannelsPerFrame;
644 ctx->audio_bits_per_sample = basic_desc->mBitsPerChannel;
645 ctx->audio_float = basic_desc->mFormatFlags & kAudioFormatFlagIsFloat;
646 ctx->audio_be = basic_desc->mFormatFlags & kAudioFormatFlagIsBigEndian;
647 ctx->audio_signed_integer = basic_desc->mFormatFlags & kAudioFormatFlagIsSignedInteger;
648 ctx->audio_packed = basic_desc->mFormatFlags & kAudioFormatFlagIsPacked;
649 ctx->audio_non_interleaved = basic_desc->mFormatFlags & kAudioFormatFlagIsNonInterleaved;
651 if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
653 ctx->audio_bits_per_sample == 32 &&
656 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
657 ctx->audio_signed_integer &&
658 ctx->audio_bits_per_sample == 16 &&
661 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
662 ctx->audio_signed_integer &&
663 ctx->audio_bits_per_sample == 24 &&
666 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
667 ctx->audio_signed_integer &&
668 ctx->audio_bits_per_sample == 32 &&
676 if (
ctx->audio_non_interleaved) {
677 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
678 ctx->audio_buffer_size = CMBlockBufferGetDataLength(block_buffer);
680 if (!
ctx->audio_buffer) {
686 CFRelease(
ctx->current_audio_frame);
687 ctx->current_audio_frame = nil;
696 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
698 uint32_t num_screens = 0;
700 AVCaptureDevice *video_device = nil;
701 AVCaptureDevice *audio_device = nil;
703 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
704 NSArray *devices_muxed = [AVCaptureDevice devicesWithMediaType:AVMediaTypeMuxed];
706 ctx->num_video_devices = [devices
count] + [devices_muxed
count];
713 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
714 CGGetActiveDisplayList(0,
NULL, &num_screens);
718 if (
ctx->list_devices) {
721 for (AVCaptureDevice *device
in devices) {
722 const char *
name = [[device localizedName] UTF8String];
723 index = [devices indexOfObject:device];
726 for (AVCaptureDevice *device
in devices_muxed) {
727 const char *
name = [[device localizedName] UTF8String];
728 index = [devices count] + [devices_muxed indexOfObject:device];
731 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
732 if (num_screens > 0) {
733 CGDirectDisplayID screens[num_screens];
734 CGGetActiveDisplayList(num_screens, screens, &num_screens);
735 for (
int i = 0;
i < num_screens;
i++) {
742 devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
743 for (AVCaptureDevice *device
in devices) {
744 const char *
name = [[device localizedName] UTF8String];
745 int index = [devices indexOfObject:device];
755 if (
ctx->video_device_index == -1 &&
ctx->video_filename) {
756 sscanf(
ctx->video_filename,
"%d", &
ctx->video_device_index);
758 if (
ctx->audio_device_index == -1 &&
ctx->audio_filename) {
759 sscanf(
ctx->audio_filename,
"%d", &
ctx->audio_device_index);
762 if (
ctx->video_device_index >= 0) {
763 if (
ctx->video_device_index <
ctx->num_video_devices) {
764 if (
ctx->video_device_index < [devices
count]) {
765 video_device = [devices objectAtIndex:ctx->video_device_index];
767 video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])];
768 ctx->video_is_muxed = 1;
770 }
else if (
ctx->video_device_index <
ctx->num_video_devices + num_screens) {
771 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
772 CGDirectDisplayID screens[num_screens];
773 CGGetActiveDisplayList(num_screens, screens, &num_screens);
774 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->
num_video_devices]] autorelease];
776 if (
ctx->framerate.num > 0) {
777 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
780 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
781 if (
ctx->capture_cursor) {
782 capture_screen_input.capturesCursor = YES;
784 capture_screen_input.capturesCursor = NO;
788 if (
ctx->capture_mouse_clicks) {
789 capture_screen_input.capturesMouseClicks = YES;
791 capture_screen_input.capturesMouseClicks = NO;
794 video_device = (AVCaptureDevice*) capture_screen_input;
801 }
else if (
ctx->video_filename &&
802 strncmp(
ctx->video_filename,
"none", 4)) {
803 if (!strncmp(
ctx->video_filename,
"default", 7)) {
804 video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
807 for (AVCaptureDevice *device
in devices) {
808 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
809 video_device = device;
814 for (AVCaptureDevice *device
in devices_muxed) {
815 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
816 video_device = device;
817 ctx->video_is_muxed = 1;
822 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
826 if(sscanf(
ctx->video_filename,
"Capture screen %d", &idx) && idx < num_screens) {
827 CGDirectDisplayID screens[num_screens];
828 CGGetActiveDisplayList(num_screens, screens, &num_screens);
829 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
830 video_device = (AVCaptureDevice*) capture_screen_input;
831 ctx->video_device_index =
ctx->num_video_devices + idx;
834 if (
ctx->framerate.num > 0) {
835 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
838 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
839 if (
ctx->capture_cursor) {
840 capture_screen_input.capturesCursor = YES;
842 capture_screen_input.capturesCursor = NO;
846 if (
ctx->capture_mouse_clicks) {
847 capture_screen_input.capturesMouseClicks = YES;
849 capture_screen_input.capturesMouseClicks = NO;
863 if (
ctx->audio_device_index >= 0) {
864 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
866 if (
ctx->audio_device_index >= [devices
count]) {
871 audio_device = [devices objectAtIndex:ctx->audio_device_index];
872 }
else if (
ctx->audio_filename &&
873 strncmp(
ctx->audio_filename,
"none", 4)) {
874 if (!strncmp(
ctx->audio_filename,
"default", 7)) {
875 audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
877 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
879 for (AVCaptureDevice *device
in devices) {
880 if (!strncmp(
ctx->audio_filename, [[device localizedName] UTF8String], strlen(
ctx->audio_filename))) {
881 audio_device = device;
894 if (!video_device && !audio_device) {
900 if (
ctx->video_device_index <
ctx->num_video_devices) {
907 av_log(
s,
AV_LOG_DEBUG,
"audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
911 ctx->capture_session = [[AVCaptureSession alloc] init];
919 [ctx->capture_session startRunning];
924 [video_device unlockForConfiguration];
946 CVPixelBufferRef image_buffer,
952 int width = CVPixelBufferGetWidth(image_buffer);
953 int height = CVPixelBufferGetHeight(image_buffer);
956 memset(src_linesize, 0,
sizeof(src_linesize));
957 memset(src_data, 0,
sizeof(src_data));
959 status = CVPixelBufferLockBaseAddress(image_buffer, 0);
960 if (
status != kCVReturnSuccess) {
965 if (CVPixelBufferIsPlanar(image_buffer)) {
966 size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
968 for(
i = 0;
i < plane_count;
i++){
969 src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer,
i);
970 src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer,
i);
973 src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
974 src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
978 src_data, src_linesize,
983 CVPixelBufferUnlockBaseAddress(image_buffer, 0);
993 CVImageBufferRef image_buffer;
994 CMBlockBufferRef block_buffer;
997 if (
ctx->current_frame != nil) {
1001 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
1002 block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_frame);
1004 if (image_buffer != nil) {
1005 length = (
int)CVPixelBufferGetDataSize(image_buffer);
1006 }
else if (block_buffer != nil) {
1007 length = (
int)CMBlockBufferGetDataLength(block_buffer);
1019 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_frame, 1, &
timing_info, &
count) == noErr) {
1031 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1032 if (
ret != kCMBlockBufferNoErr) {
1036 CFRelease(
ctx->current_frame);
1037 ctx->current_frame = nil;
1041 }
else if (
ctx->current_audio_frame != nil) {
1042 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
1043 int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
1045 if (!block_buffer || !block_buffer_size) {
1049 if (
ctx->audio_non_interleaved && block_buffer_size >
ctx->audio_buffer_size) {
1060 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_audio_frame, 1, &
timing_info, &
count) == noErr) {
1068 if (
ctx->audio_non_interleaved) {
1071 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
ctx->audio_buffer);
1072 if (
ret != kCMBlockBufferNoErr) {
1076 num_samples =
pkt->
size / (
ctx->audio_channels * (
ctx->audio_bits_per_sample >> 3));
1079 #define INTERLEAVE_OUTPUT(bps) \
1081 int##bps##_t **src; \
1082 int##bps##_t *dest; \
1083 src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
1084 if (!src) return AVERROR(EIO); \
1085 for (c = 0; c < ctx->audio_channels; c++) { \
1086 src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
1088 dest = (int##bps##_t*)pkt->data; \
1089 shift = bps - ctx->audio_bits_per_sample; \
1090 for (sample = 0; sample < num_samples; sample++) \
1091 for (c = 0; c < ctx->audio_channels; c++) \
1092 *dest++ = src[c][sample] << shift; \
1096 if (
ctx->audio_bits_per_sample <= 16) {
1102 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1103 if (
ret != kCMBlockBufferNoErr) {
1108 CFRelease(
ctx->current_audio_frame);
1109 ctx->current_audio_frame = nil;
1151 .
name =
"avfoundation",