28 #import <AVFoundation/AVFoundation.h>
78 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
136 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
137 AVCaptureDeviceTransportControlsPlaybackMode observed_mode;
161 - (void) captureOutput:(AVCaptureOutput *)captureOutput
162 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
163 fromConnection:(AVCaptureConnection *)connection;
171 if (
self = [super
init]) {
175 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
177 NSString *keyPath = NSStringFromSelector(
@selector(transportControlsPlaybackMode));
178 NSKeyValueObservingOptions
options = NSKeyValueObservingOptionNew;
180 [
_context->observed_device addObserver: self
192 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
194 NSString *keyPath = NSStringFromSelector(
@selector(transportControlsPlaybackMode));
195 [_context->observed_device removeObserver: self forKeyPath: keyPath];
201 - (void)observeValueForKeyPath:(NSString *)keyPath
203 change:(NSDictionary *)change
204 context:(
void *)context {
206 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
207 AVCaptureDeviceTransportControlsPlaybackMode
mode =
208 [change[NSKeyValueChangeNewKey] integerValue];
211 if (
mode == AVCaptureDeviceTransportControlsNotPlayingMode) {
218 [
super observeValueForKeyPath: keyPath
225 - (void) captureOutput:(AVCaptureOutput *)captureOutput
226 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
227 fromConnection:(AVCaptureConnection *)connection
253 - (void) captureOutput:(AVCaptureOutput *)captureOutput
254 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
255 fromConnection:(AVCaptureConnection *)connection;
263 if (
self = [super
init]) {
269 - (void) captureOutput:(AVCaptureOutput *)captureOutput
270 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
271 fromConnection:(AVCaptureConnection *)connection
290 [ctx->capture_session stopRunning];
292 [ctx->capture_session release];
293 [ctx->video_output release];
294 [ctx->audio_output release];
295 [ctx->avf_delegate release];
296 [ctx->avf_audio_delegate release];
302 ctx->avf_audio_delegate =
NULL;
309 if (
ctx->current_frame) {
310 CFRelease(
ctx->current_frame);
347 NSObject *
range = nil;
349 NSObject *selected_range = nil;
350 NSObject *selected_format = nil;
356 for (
format in [video_device valueForKey:
@"formats"]) {
357 CMFormatDescriptionRef formatDescription;
358 CMVideoDimensions dimensions;
360 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
361 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
363 if ((
ctx->width == 0 &&
ctx->height == 0) ||
364 (dimensions.width ==
ctx->width && dimensions.height ==
ctx->height)) {
368 for (
range in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
369 double max_framerate;
371 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
373 selected_range =
range;
380 if (!selected_format) {
383 goto unsupported_format;
386 if (!selected_range) {
389 if (
ctx->video_is_muxed) {
392 goto unsupported_format;
396 if ([video_device lockForConfiguration:
NULL] == YES) {
397 if (selected_format) {
398 [video_device setValue:selected_format forKey:@"activeFormat"];
400 if (selected_range) {
401 NSValue *min_frame_duration = [selected_range valueForKey:@"minFrameDuration"];
402 [video_device setValue:min_frame_duration forKey:@"activeVideoMinFrameDuration"];
403 [video_device setValue:min_frame_duration forKey:@"activeVideoMaxFrameDuration"];
409 }
@catch(NSException *e) {
418 for (
format in [video_device valueForKey:
@"formats"]) {
419 CMFormatDescriptionRef formatDescription;
420 CMVideoDimensions dimensions;
422 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
423 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
425 for (
range in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
426 double min_framerate;
427 double max_framerate;
429 [[range valueForKey:@"minFrameRate"] getValue:&min_framerate];
430 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
432 dimensions.width, dimensions.height,
433 min_framerate, max_framerate);
443 NSError *
error = nil;
444 AVCaptureInput* capture_input = nil;
446 NSNumber *pixel_format;
447 NSDictionary *capture_dict;
448 dispatch_queue_t queue;
450 if (
ctx->video_device_index <
ctx->num_video_devices) {
451 capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device
error:&
error] autorelease];
453 capture_input = (AVCaptureInput*) video_device;
456 if (!capture_input) {
458 [[
error localizedDescription] UTF8String]);
462 if ([
ctx->capture_session canAddInput:capture_input]) {
463 [ctx->capture_session addInput:capture_input];
470 ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
472 if (!
ctx->video_output) {
482 }
@catch (NSException *exception) {
483 if (![[exception
name] isEqualToString:NSUndefinedKeyException]) {
507 if ([[
ctx->video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.avf_id]] == NSNotFound) {
508 av_log(
s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by the input device.\n",
514 for (NSNumber *pxl_fmt in [
ctx->video_output availableVideoCVPixelFormatTypes]) {
529 pxl_fmt_spec = pxl_fmt_dummy;
544 if (
ctx->capture_raw_data) {
545 ctx->pixel_format = pxl_fmt_spec.ff_id;
546 ctx->video_output.videoSettings = @{ };
548 ctx->pixel_format = pxl_fmt_spec.ff_id;
549 pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
550 capture_dict = [NSDictionary dictionaryWithObject:pixel_format
551 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
553 [ctx->video_output setVideoSettings:capture_dict];
555 [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames];
557 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
559 if (!
ctx->video_is_screen) {
560 int trans_ctrl = [video_device transportControlsSupported];
561 AVCaptureDeviceTransportControlsPlaybackMode trans_mode = [video_device transportControlsPlaybackMode];
564 ctx->observed_mode = trans_mode;
565 ctx->observed_device = video_device;
572 queue = dispatch_queue_create(
"avf_queue",
NULL);
573 [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
574 dispatch_release(queue);
576 if ([
ctx->capture_session canAddOutput:
ctx->video_output]) {
577 [ctx->capture_session addOutput:ctx->video_output];
589 NSError *
error = nil;
590 AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device
error:&
error] autorelease];
591 dispatch_queue_t queue;
593 if (!audio_dev_input) {
595 [[
error localizedDescription] UTF8String]);
599 if ([
ctx->capture_session canAddInput:audio_dev_input]) {
600 [ctx->capture_session addInput:audio_dev_input];
607 ctx->audio_output = [[AVCaptureAudioDataOutput alloc] init];
609 if (!
ctx->audio_output) {
616 queue = dispatch_queue_create(
"avf_audio_queue",
NULL);
617 [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
618 dispatch_release(queue);
620 if ([
ctx->capture_session canAddOutput:
ctx->audio_output]) {
621 [ctx->capture_session addOutput:ctx->audio_output];
633 CVImageBufferRef image_buffer;
634 CMBlockBufferRef block_buffer;
635 CGSize image_buffer_size;
643 while (
ctx->frames_captured < 1) {
644 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
649 ctx->video_stream_index = stream->index;
653 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
654 block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_frame);
657 image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
661 stream->codecpar->width = (
int)image_buffer_size.width;
662 stream->codecpar->height = (
int)image_buffer_size.height;
663 stream->codecpar->format =
ctx->pixel_format;
667 stream->codecpar->format =
ctx->pixel_format;
670 CFRelease(
ctx->current_frame);
671 ctx->current_frame = nil;
681 CMFormatDescriptionRef format_desc;
689 while (
ctx->audio_frames_captured < 1) {
690 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
695 ctx->audio_stream_index = stream->index;
699 format_desc = CMSampleBufferGetFormatDescription(
ctx->current_audio_frame);
700 const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
709 stream->codecpar->sample_rate = basic_desc->mSampleRate;
712 ctx->audio_channels = basic_desc->mChannelsPerFrame;
713 ctx->audio_bits_per_sample = basic_desc->mBitsPerChannel;
714 ctx->audio_float = basic_desc->mFormatFlags & kAudioFormatFlagIsFloat;
715 ctx->audio_be = basic_desc->mFormatFlags & kAudioFormatFlagIsBigEndian;
716 ctx->audio_signed_integer = basic_desc->mFormatFlags & kAudioFormatFlagIsSignedInteger;
717 ctx->audio_packed = basic_desc->mFormatFlags & kAudioFormatFlagIsPacked;
718 ctx->audio_non_interleaved = basic_desc->mFormatFlags & kAudioFormatFlagIsNonInterleaved;
720 if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
722 ctx->audio_bits_per_sample == 32 &&
725 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
726 ctx->audio_signed_integer &&
727 ctx->audio_bits_per_sample == 16 &&
730 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
731 ctx->audio_signed_integer &&
732 ctx->audio_bits_per_sample == 24 &&
735 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
736 ctx->audio_signed_integer &&
737 ctx->audio_bits_per_sample == 32 &&
746 if (
ctx->audio_non_interleaved) {
747 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
748 ctx->audio_buffer_size = CMBlockBufferGetDataLength(block_buffer);
750 if (!
ctx->audio_buffer) {
757 CFRelease(
ctx->current_audio_frame);
758 ctx->current_audio_frame = nil;
766 #if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500))
767 NSMutableArray *deviceTypes = nil;
768 if (mediaType == AVMediaTypeVideo) {
769 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]];
770 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000)
771 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualCamera];
772 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTelephotoCamera];
774 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110100)
775 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTrueDepthCamera];
777 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 130000)
778 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTripleCamera];
779 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualWideCamera];
780 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInUltraWideCamera];
782 #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000)
783 [deviceTypes addObject: AVCaptureDeviceTypeDeskViewCamera];
785 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 150400)
786 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInLiDARDepthCamera];
788 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
789 [deviceTypes addObject: AVCaptureDeviceTypeContinuityCamera];
790 [deviceTypes addObject: AVCaptureDeviceTypeExternal];
791 #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
792 [deviceTypes addObject: AVCaptureDeviceTypeExternalUnknown];
794 }
else if (mediaType == AVMediaTypeAudio) {
795 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
796 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeMicrophone]];
798 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInMicrophone]];
800 }
else if (mediaType == AVMediaTypeMuxed) {
801 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
802 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternal]];
803 #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
804 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternalUnknown]];
812 AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession =
813 [AVCaptureDeviceDiscoverySession
814 discoverySessionWithDeviceTypes:deviceTypes
816 position:AVCaptureDevicePositionUnspecified];
817 return [captureDeviceDiscoverySession devices];
819 return [AVCaptureDevice devicesWithMediaType:mediaType];
826 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
827 uint32_t num_screens = 0;
829 AVCaptureDevice *video_device = nil;
830 AVCaptureDevice *audio_device = nil;
835 ctx->num_video_devices = [devices count] + [devices_muxed count];
839 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
840 CGGetActiveDisplayList(0,
NULL, &num_screens);
844 if (
ctx->list_devices) {
847 for (AVCaptureDevice *device in devices) {
848 const char *
name = [[device localizedName] UTF8String];
849 index = [devices indexOfObject:device];
852 for (AVCaptureDevice *device in devices_muxed) {
853 const char *
name = [[device localizedName] UTF8String];
854 index = [devices count] + [devices_muxed indexOfObject:device];
857 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
858 if (num_screens > 0) {
859 CGDirectDisplayID screens[num_screens];
860 CGGetActiveDisplayList(num_screens, screens, &num_screens);
861 for (
int i = 0;
i < num_screens;
i++) {
869 for (AVCaptureDevice *device in devices) {
870 const char *
name = [[device localizedName] UTF8String];
871 int index = [devices indexOfObject:device];
883 if (
ctx->video_device_index == -1 &&
ctx->video_filename) {
884 sscanf(
ctx->video_filename,
"%d", &
ctx->video_device_index);
886 if (
ctx->audio_device_index == -1 &&
ctx->audio_filename) {
887 sscanf(
ctx->audio_filename,
"%d", &
ctx->audio_device_index);
890 if (
ctx->video_device_index >= 0) {
891 if (
ctx->video_device_index <
ctx->num_video_devices) {
892 if (
ctx->video_device_index < [devices count]) {
893 video_device = [devices objectAtIndex:ctx->video_device_index];
895 video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])];
896 ctx->video_is_muxed = 1;
898 }
else if (
ctx->video_device_index <
ctx->num_video_devices + num_screens) {
899 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
900 CGDirectDisplayID screens[num_screens];
901 CGGetActiveDisplayList(num_screens, screens, &num_screens);
902 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->
num_video_devices]] autorelease];
904 if (
ctx->framerate.num > 0) {
905 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
908 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
909 if (
ctx->capture_cursor) {
910 capture_screen_input.capturesCursor = YES;
912 capture_screen_input.capturesCursor = NO;
916 if (
ctx->capture_mouse_clicks) {
917 capture_screen_input.capturesMouseClicks = YES;
919 capture_screen_input.capturesMouseClicks = NO;
922 video_device = (AVCaptureDevice*) capture_screen_input;
923 ctx->video_is_screen = 1;
929 }
else if (
ctx->video_filename &&
930 strncmp(
ctx->video_filename,
"none", 4)) {
931 if (!strncmp(
ctx->video_filename,
"default", 7)) {
932 video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
935 for (AVCaptureDevice *device in devices) {
936 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
937 video_device = device;
942 for (AVCaptureDevice *device in devices_muxed) {
943 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
944 video_device = device;
945 ctx->video_is_muxed = 1;
950 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
954 if(sscanf(
ctx->video_filename,
"Capture screen %d", &idx) && idx < num_screens) {
955 CGDirectDisplayID screens[num_screens];
956 CGGetActiveDisplayList(num_screens, screens, &num_screens);
957 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
958 video_device = (AVCaptureDevice*) capture_screen_input;
959 ctx->video_device_index =
ctx->num_video_devices + idx;
960 ctx->video_is_screen = 1;
962 if (
ctx->framerate.num > 0) {
963 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
966 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
967 if (
ctx->capture_cursor) {
968 capture_screen_input.capturesCursor = YES;
970 capture_screen_input.capturesCursor = NO;
974 if (
ctx->capture_mouse_clicks) {
975 capture_screen_input.capturesMouseClicks = YES;
977 capture_screen_input.capturesMouseClicks = NO;
991 if (
ctx->audio_device_index >= 0) {
994 if (
ctx->audio_device_index >= [devices count]) {
999 audio_device = [devices objectAtIndex:ctx->audio_device_index];
1000 }
else if (
ctx->audio_filename &&
1001 strncmp(
ctx->audio_filename,
"none", 4)) {
1002 if (!strncmp(
ctx->audio_filename,
"default", 7)) {
1003 audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
1007 for (AVCaptureDevice *device in devices) {
1008 if (!strncmp(
ctx->audio_filename, [[device localizedName] UTF8String], strlen(
ctx->audio_filename))) {
1009 audio_device = device;
1015 if (!audio_device) {
1022 if (!video_device && !audio_device) {
1028 if (
ctx->video_device_index <
ctx->num_video_devices) {
1035 av_log(
s,
AV_LOG_DEBUG,
"audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
1039 ctx->capture_session = [[AVCaptureSession alloc] init];
1047 [ctx->capture_session startRunning];
1051 if (!
ctx->video_is_screen) {
1052 [video_device unlockForConfiguration];
1076 CVPixelBufferRef image_buffer,
1080 int src_linesize[4];
1081 const uint8_t *src_data[4];
1082 int width = CVPixelBufferGetWidth(image_buffer);
1083 int height = CVPixelBufferGetHeight(image_buffer);
1086 memset(src_linesize, 0,
sizeof(src_linesize));
1087 memset(src_data, 0,
sizeof(src_data));
1089 status = CVPixelBufferLockBaseAddress(image_buffer, 0);
1090 if (
status != kCVReturnSuccess) {
1095 if (CVPixelBufferIsPlanar(image_buffer)) {
1096 size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
1098 for(
i = 0;
i < plane_count;
i++){
1099 src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer,
i);
1100 src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer,
i);
1103 src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
1104 src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
1108 src_data, src_linesize,
1113 CVPixelBufferUnlockBaseAddress(image_buffer, 0);
1123 CVImageBufferRef image_buffer;
1124 CMBlockBufferRef block_buffer;
1127 if (
ctx->current_frame != nil) {
1131 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
1132 block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_frame);
1134 if (image_buffer != nil) {
1135 length = (
int)CVPixelBufferGetDataSize(image_buffer);
1136 }
else if (block_buffer != nil) {
1137 length = (
int)CMBlockBufferGetDataLength(block_buffer);
1151 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_frame, 1, &
timing_info, &count) == noErr) {
1163 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1164 if (
ret != kCMBlockBufferNoErr) {
1168 CFRelease(
ctx->current_frame);
1169 ctx->current_frame = nil;
1175 }
else if (
ctx->current_audio_frame != nil) {
1176 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
1177 int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
1179 if (!block_buffer || !block_buffer_size) {
1184 if (
ctx->audio_non_interleaved && block_buffer_size >
ctx->audio_buffer_size) {
1197 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_audio_frame, 1, &
timing_info, &count) == noErr) {
1205 if (
ctx->audio_non_interleaved) {
1208 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
ctx->audio_buffer);
1209 if (
ret != kCMBlockBufferNoErr) {
1214 num_samples =
pkt->
size / (
ctx->audio_channels * (
ctx->audio_bits_per_sample >> 3));
1217 #define INTERLEAVE_OUTPUT(bps) \
1219 int##bps##_t **src; \
1220 int##bps##_t *dest; \
1221 src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
1223 unlock_frames(ctx); \
1224 return AVERROR(EIO); \
1227 for (c = 0; c < ctx->audio_channels; c++) { \
1228 src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
1230 dest = (int##bps##_t*)pkt->data; \
1231 shift = bps - ctx->audio_bits_per_sample; \
1232 for (sample = 0; sample < num_samples; sample++) \
1233 for (c = 0; c < ctx->audio_channels; c++) \
1234 *dest++ = src[c][sample] << shift; \
1238 if (
ctx->audio_bits_per_sample <= 16) {
1244 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1245 if (
ret != kCMBlockBufferNoErr) {
1251 CFRelease(
ctx->current_audio_frame);
1252 ctx->current_audio_frame = nil;
1256 if (
ctx->observed_quit) {
1300 .
p.
name =
"avfoundation",