28 #import <AVFoundation/AVFoundation.h>
77 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
134 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
135 AVCaptureDeviceTransportControlsPlaybackMode observed_mode;
159 - (void) captureOutput:(AVCaptureOutput *)captureOutput
160 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
161 fromConnection:(AVCaptureConnection *)connection;
169 if (
self = [super
init]) {
173 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
175 NSString *keyPath = NSStringFromSelector(
@selector(transportControlsPlaybackMode));
176 NSKeyValueObservingOptions
options = NSKeyValueObservingOptionNew;
178 [
_context->observed_device addObserver: self
190 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
192 NSString *keyPath = NSStringFromSelector(
@selector(transportControlsPlaybackMode));
193 [_context->observed_device removeObserver: self forKeyPath: keyPath];
199 - (void)observeValueForKeyPath:(NSString *)keyPath
201 change:(NSDictionary *)change
202 context:(
void *)context {
204 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
205 AVCaptureDeviceTransportControlsPlaybackMode
mode =
206 [change[NSKeyValueChangeNewKey] integerValue];
209 if (
mode == AVCaptureDeviceTransportControlsNotPlayingMode) {
216 [
super observeValueForKeyPath: keyPath
223 - (void) captureOutput:(AVCaptureOutput *)captureOutput
224 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
225 fromConnection:(AVCaptureConnection *)connection
251 - (void) captureOutput:(AVCaptureOutput *)captureOutput
252 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
253 fromConnection:(AVCaptureConnection *)connection;
261 if (
self = [super
init]) {
267 - (void) captureOutput:(AVCaptureOutput *)captureOutput
268 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
269 fromConnection:(AVCaptureConnection *)connection
288 [ctx->capture_session stopRunning];
290 [ctx->capture_session release];
291 [ctx->video_output release];
292 [ctx->audio_output release];
293 [ctx->avf_delegate release];
294 [ctx->avf_audio_delegate release];
300 ctx->avf_audio_delegate =
NULL;
306 if (
ctx->current_frame) {
307 CFRelease(
ctx->current_frame);
340 NSObject *range = nil;
342 NSObject *selected_range = nil;
343 NSObject *selected_format = nil;
349 for (
format in [video_device valueForKey:
@"formats"]) {
350 CMFormatDescriptionRef formatDescription;
351 CMVideoDimensions dimensions;
353 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
354 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
356 if ((
ctx->width == 0 &&
ctx->height == 0) ||
357 (dimensions.width ==
ctx->width && dimensions.height ==
ctx->height)) {
361 for (range in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
362 double max_framerate;
364 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
366 selected_range = range;
373 if (!selected_format) {
376 goto unsupported_format;
379 if (!selected_range) {
382 if (
ctx->video_is_muxed) {
385 goto unsupported_format;
389 if ([video_device lockForConfiguration:
NULL] == YES) {
390 if (selected_format) {
391 [video_device setValue:selected_format forKey:@"activeFormat"];
393 if (selected_range) {
394 NSValue *min_frame_duration = [selected_range valueForKey:@"minFrameDuration"];
395 [video_device setValue:min_frame_duration forKey:@"activeVideoMinFrameDuration"];
396 [video_device setValue:min_frame_duration forKey:@"activeVideoMaxFrameDuration"];
402 }
@catch(NSException *e) {
411 for (
format in [video_device valueForKey:
@"formats"]) {
412 CMFormatDescriptionRef formatDescription;
413 CMVideoDimensions dimensions;
415 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
416 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
418 for (range in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
419 double min_framerate;
420 double max_framerate;
422 [[range valueForKey:@"minFrameRate"] getValue:&min_framerate];
423 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
425 dimensions.width, dimensions.height,
426 min_framerate, max_framerate);
436 NSError *
error = nil;
437 AVCaptureInput* capture_input = nil;
439 NSNumber *pixel_format;
440 NSDictionary *capture_dict;
441 dispatch_queue_t queue;
443 if (
ctx->video_device_index <
ctx->num_video_devices) {
444 capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device
error:&
error] autorelease];
446 capture_input = (AVCaptureInput*) video_device;
449 if (!capture_input) {
451 [[
error localizedDescription] UTF8String]);
455 if ([
ctx->capture_session canAddInput:capture_input]) {
456 [ctx->capture_session addInput:capture_input];
463 ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
465 if (!
ctx->video_output) {
475 }
@catch (NSException *exception) {
476 if (![[exception
name] isEqualToString:NSUndefinedKeyException]) {
500 if ([[
ctx->video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.avf_id]] == NSNotFound) {
501 av_log(
s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by the input device.\n",
507 for (NSNumber *pxl_fmt in [
ctx->video_output availableVideoCVPixelFormatTypes]) {
522 pxl_fmt_spec = pxl_fmt_dummy;
537 if (
ctx->capture_raw_data) {
538 ctx->pixel_format = pxl_fmt_spec.ff_id;
539 ctx->video_output.videoSettings = @{ };
541 ctx->pixel_format = pxl_fmt_spec.ff_id;
542 pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
543 capture_dict = [NSDictionary dictionaryWithObject:pixel_format
544 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
546 [ctx->video_output setVideoSettings:capture_dict];
548 [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames];
550 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
552 if (!
ctx->video_is_screen) {
553 int trans_ctrl = [video_device transportControlsSupported];
554 AVCaptureDeviceTransportControlsPlaybackMode trans_mode = [video_device transportControlsPlaybackMode];
557 ctx->observed_mode = trans_mode;
558 ctx->observed_device = video_device;
565 queue = dispatch_queue_create(
"avf_queue",
NULL);
566 [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
567 dispatch_release(queue);
569 if ([
ctx->capture_session canAddOutput:
ctx->video_output]) {
570 [ctx->capture_session addOutput:ctx->video_output];
582 NSError *
error = nil;
583 AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device
error:&
error] autorelease];
584 dispatch_queue_t queue;
586 if (!audio_dev_input) {
588 [[
error localizedDescription] UTF8String]);
592 if ([
ctx->capture_session canAddInput:audio_dev_input]) {
593 [ctx->capture_session addInput:audio_dev_input];
600 ctx->audio_output = [[AVCaptureAudioDataOutput alloc] init];
602 if (!
ctx->audio_output) {
609 queue = dispatch_queue_create(
"avf_audio_queue",
NULL);
610 [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
611 dispatch_release(queue);
613 if ([
ctx->capture_session canAddOutput:
ctx->audio_output]) {
614 [ctx->capture_session addOutput:ctx->audio_output];
626 CVImageBufferRef image_buffer;
627 CMBlockBufferRef block_buffer;
628 CGSize image_buffer_size;
636 while (
ctx->frames_captured < 1) {
637 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
642 ctx->video_stream_index = stream->index;
646 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
647 block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_frame);
650 image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
654 stream->codecpar->width = (
int)image_buffer_size.width;
655 stream->codecpar->height = (
int)image_buffer_size.height;
656 stream->codecpar->format =
ctx->pixel_format;
660 stream->codecpar->format =
ctx->pixel_format;
663 CFRelease(
ctx->current_frame);
664 ctx->current_frame = nil;
674 CMFormatDescriptionRef format_desc;
682 while (
ctx->audio_frames_captured < 1) {
683 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
688 ctx->audio_stream_index = stream->index;
692 format_desc = CMSampleBufferGetFormatDescription(
ctx->current_audio_frame);
693 const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
702 stream->codecpar->sample_rate = basic_desc->mSampleRate;
703 stream->codecpar->channels = basic_desc->mChannelsPerFrame;
706 ctx->audio_channels = basic_desc->mChannelsPerFrame;
707 ctx->audio_bits_per_sample = basic_desc->mBitsPerChannel;
708 ctx->audio_float = basic_desc->mFormatFlags & kAudioFormatFlagIsFloat;
709 ctx->audio_be = basic_desc->mFormatFlags & kAudioFormatFlagIsBigEndian;
710 ctx->audio_signed_integer = basic_desc->mFormatFlags & kAudioFormatFlagIsSignedInteger;
711 ctx->audio_packed = basic_desc->mFormatFlags & kAudioFormatFlagIsPacked;
712 ctx->audio_non_interleaved = basic_desc->mFormatFlags & kAudioFormatFlagIsNonInterleaved;
714 if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
716 ctx->audio_bits_per_sample == 32 &&
719 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
720 ctx->audio_signed_integer &&
721 ctx->audio_bits_per_sample == 16 &&
724 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
725 ctx->audio_signed_integer &&
726 ctx->audio_bits_per_sample == 24 &&
729 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
730 ctx->audio_signed_integer &&
731 ctx->audio_bits_per_sample == 32 &&
740 if (
ctx->audio_non_interleaved) {
741 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
742 ctx->audio_buffer_size = CMBlockBufferGetDataLength(block_buffer);
744 if (!
ctx->audio_buffer) {
751 CFRelease(
ctx->current_audio_frame);
752 ctx->current_audio_frame = nil;
761 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
762 uint32_t num_screens = 0;
764 AVCaptureDevice *video_device = nil;
765 AVCaptureDevice *audio_device = nil;
767 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
768 NSArray *devices_muxed = [AVCaptureDevice devicesWithMediaType:AVMediaTypeMuxed];
770 ctx->num_video_devices = [devices count] + [devices_muxed count];
774 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
775 CGGetActiveDisplayList(0,
NULL, &num_screens);
779 if (
ctx->list_devices) {
782 for (AVCaptureDevice *device in devices) {
783 const char *
name = [[device localizedName] UTF8String];
784 index = [devices indexOfObject:device];
787 for (AVCaptureDevice *device in devices_muxed) {
788 const char *
name = [[device localizedName] UTF8String];
789 index = [devices count] + [devices_muxed indexOfObject:device];
792 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
793 if (num_screens > 0) {
794 CGDirectDisplayID screens[num_screens];
795 CGGetActiveDisplayList(num_screens, screens, &num_screens);
796 for (
int i = 0;
i < num_screens;
i++) {
803 devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
804 for (AVCaptureDevice *device in devices) {
805 const char *
name = [[device localizedName] UTF8String];
806 int index = [devices indexOfObject:device];
816 if (
ctx->video_device_index == -1 &&
ctx->video_filename) {
817 sscanf(
ctx->video_filename,
"%d", &
ctx->video_device_index);
819 if (
ctx->audio_device_index == -1 &&
ctx->audio_filename) {
820 sscanf(
ctx->audio_filename,
"%d", &
ctx->audio_device_index);
823 if (
ctx->video_device_index >= 0) {
824 if (
ctx->video_device_index <
ctx->num_video_devices) {
825 if (
ctx->video_device_index < [devices count]) {
826 video_device = [devices objectAtIndex:ctx->video_device_index];
828 video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])];
829 ctx->video_is_muxed = 1;
831 }
else if (
ctx->video_device_index <
ctx->num_video_devices + num_screens) {
832 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
833 CGDirectDisplayID screens[num_screens];
834 CGGetActiveDisplayList(num_screens, screens, &num_screens);
835 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->
num_video_devices]] autorelease];
837 if (
ctx->framerate.num > 0) {
838 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
841 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
842 if (
ctx->capture_cursor) {
843 capture_screen_input.capturesCursor = YES;
845 capture_screen_input.capturesCursor = NO;
849 if (
ctx->capture_mouse_clicks) {
850 capture_screen_input.capturesMouseClicks = YES;
852 capture_screen_input.capturesMouseClicks = NO;
855 video_device = (AVCaptureDevice*) capture_screen_input;
856 ctx->video_is_screen = 1;
862 }
else if (
ctx->video_filename &&
863 strncmp(
ctx->video_filename,
"none", 4)) {
864 if (!strncmp(
ctx->video_filename,
"default", 7)) {
865 video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
868 for (AVCaptureDevice *device in devices) {
869 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
870 video_device = device;
875 for (AVCaptureDevice *device in devices_muxed) {
876 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
877 video_device = device;
878 ctx->video_is_muxed = 1;
883 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
887 if(sscanf(
ctx->video_filename,
"Capture screen %d", &idx) && idx < num_screens) {
888 CGDirectDisplayID screens[num_screens];
889 CGGetActiveDisplayList(num_screens, screens, &num_screens);
890 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
891 video_device = (AVCaptureDevice*) capture_screen_input;
892 ctx->video_device_index =
ctx->num_video_devices + idx;
893 ctx->video_is_screen = 1;
895 if (
ctx->framerate.num > 0) {
896 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
899 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
900 if (
ctx->capture_cursor) {
901 capture_screen_input.capturesCursor = YES;
903 capture_screen_input.capturesCursor = NO;
907 if (
ctx->capture_mouse_clicks) {
908 capture_screen_input.capturesMouseClicks = YES;
910 capture_screen_input.capturesMouseClicks = NO;
924 if (
ctx->audio_device_index >= 0) {
925 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
927 if (
ctx->audio_device_index >= [devices count]) {
932 audio_device = [devices objectAtIndex:ctx->audio_device_index];
933 }
else if (
ctx->audio_filename &&
934 strncmp(
ctx->audio_filename,
"none", 4)) {
935 if (!strncmp(
ctx->audio_filename,
"default", 7)) {
936 audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
938 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
940 for (AVCaptureDevice *device in devices) {
941 if (!strncmp(
ctx->audio_filename, [[device localizedName] UTF8String], strlen(
ctx->audio_filename))) {
942 audio_device = device;
955 if (!video_device && !audio_device) {
961 if (
ctx->video_device_index <
ctx->num_video_devices) {
968 av_log(
s,
AV_LOG_DEBUG,
"audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
972 ctx->capture_session = [[AVCaptureSession alloc] init];
980 [ctx->capture_session startRunning];
984 if (!
ctx->video_is_screen) {
985 [video_device unlockForConfiguration];
1007 CVPixelBufferRef image_buffer,
1011 int src_linesize[4];
1012 const uint8_t *src_data[4];
1013 int width = CVPixelBufferGetWidth(image_buffer);
1014 int height = CVPixelBufferGetHeight(image_buffer);
1017 memset(src_linesize, 0,
sizeof(src_linesize));
1018 memset(src_data, 0,
sizeof(src_data));
1020 status = CVPixelBufferLockBaseAddress(image_buffer, 0);
1021 if (
status != kCVReturnSuccess) {
1026 if (CVPixelBufferIsPlanar(image_buffer)) {
1027 size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
1029 for(
i = 0;
i < plane_count;
i++){
1030 src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer,
i);
1031 src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer,
i);
1034 src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
1035 src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
1039 src_data, src_linesize,
1044 CVPixelBufferUnlockBaseAddress(image_buffer, 0);
1054 CVImageBufferRef image_buffer;
1055 CMBlockBufferRef block_buffer;
1058 if (
ctx->current_frame != nil) {
1062 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
1063 block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_frame);
1065 if (image_buffer != nil) {
1066 length = (
int)CVPixelBufferGetDataSize(image_buffer);
1067 }
else if (block_buffer != nil) {
1068 length = (
int)CMBlockBufferGetDataLength(block_buffer);
1082 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_frame, 1, &
timing_info, &count) == noErr) {
1094 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1095 if (
ret != kCMBlockBufferNoErr) {
1099 CFRelease(
ctx->current_frame);
1100 ctx->current_frame = nil;
1106 }
else if (
ctx->current_audio_frame != nil) {
1107 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
1108 int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
1110 if (!block_buffer || !block_buffer_size) {
1115 if (
ctx->audio_non_interleaved && block_buffer_size >
ctx->audio_buffer_size) {
1128 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_audio_frame, 1, &
timing_info, &count) == noErr) {
1136 if (
ctx->audio_non_interleaved) {
1139 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
ctx->audio_buffer);
1140 if (
ret != kCMBlockBufferNoErr) {
1145 num_samples =
pkt->
size / (
ctx->audio_channels * (
ctx->audio_bits_per_sample >> 3));
1148 #define INTERLEAVE_OUTPUT(bps) \
1150 int##bps##_t **src; \
1151 int##bps##_t *dest; \
1152 src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
1154 unlock_frames(ctx); \
1155 return AVERROR(EIO); \
1158 for (c = 0; c < ctx->audio_channels; c++) { \
1159 src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
1161 dest = (int##bps##_t*)pkt->data; \
1162 shift = bps - ctx->audio_bits_per_sample; \
1163 for (sample = 0; sample < num_samples; sample++) \
1164 for (c = 0; c < ctx->audio_channels; c++) \
1165 *dest++ = src[c][sample] << shift; \
1169 if (
ctx->audio_bits_per_sample <= 16) {
1175 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1176 if (
ret != kCMBlockBufferNoErr) {
1182 CFRelease(
ctx->current_audio_frame);
1183 ctx->current_audio_frame = nil;
1187 if (
ctx->observed_quit) {
1231 .
name =
"avfoundation",