28 #import <AVFoundation/AVFoundation.h>
79 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
139 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
140 AVCaptureDeviceTransportControlsPlaybackMode observed_mode;
165 - (void) captureOutput:(AVCaptureOutput *)captureOutput
166 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
167 fromConnection:(AVCaptureConnection *)connection;
175 if (
self = [super
init]) {
179 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
181 NSString *keyPath = NSStringFromSelector(
@selector(transportControlsPlaybackMode));
182 NSKeyValueObservingOptions
options = NSKeyValueObservingOptionNew;
184 [
_context->observed_device addObserver: self
196 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
198 NSString *keyPath = NSStringFromSelector(
@selector(transportControlsPlaybackMode));
199 [_context->observed_device removeObserver: self forKeyPath: keyPath];
205 - (void)observeValueForKeyPath:(NSString *)keyPath
207 change:(NSDictionary *)change
208 context:(
void *)context {
210 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
211 AVCaptureDeviceTransportControlsPlaybackMode
mode =
212 [change[NSKeyValueChangeNewKey] integerValue];
215 if (
mode == AVCaptureDeviceTransportControlsNotPlayingMode) {
227 [
super observeValueForKeyPath: keyPath
234 - (void) captureOutput:(AVCaptureOutput *)captureOutput
235 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
236 fromConnection:(AVCaptureConnection *)connection
267 - (void) captureOutput:(AVCaptureOutput *)captureOutput
268 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
269 fromConnection:(AVCaptureConnection *)connection;
277 if (
self = [super
init]) {
283 - (void) captureOutput:(AVCaptureOutput *)captureOutput
284 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
285 fromConnection:(AVCaptureConnection *)connection
312 ctx->is_stopping = 1;
315 [ctx->capture_session stopRunning];
317 [ctx->capture_session release];
318 [ctx->video_output release];
319 [ctx->audio_output release];
320 [ctx->avf_delegate release];
321 [ctx->avf_audio_delegate release];
327 ctx->avf_audio_delegate =
NULL;
335 if (
ctx->current_frame) {
336 CFRelease(
ctx->current_frame);
337 ctx->current_frame = nil;
340 if (
ctx->current_audio_frame) {
341 CFRelease(
ctx->current_audio_frame);
342 ctx->current_audio_frame = nil;
379 NSObject *
range = nil;
381 NSObject *selected_range = nil;
382 NSObject *selected_format = nil;
388 for (
format in [video_device valueForKey:
@"formats"]) {
389 CMFormatDescriptionRef formatDescription;
390 CMVideoDimensions dimensions;
392 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
393 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
395 if ((
ctx->width == 0 &&
ctx->height == 0) ||
396 (dimensions.width ==
ctx->width && dimensions.height ==
ctx->height)) {
400 for (
range in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
401 double max_framerate;
403 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
405 selected_range =
range;
412 if (!selected_format) {
415 goto unsupported_format;
418 if (!selected_range) {
421 if (
ctx->video_is_muxed) {
424 goto unsupported_format;
428 if ([video_device lockForConfiguration:
NULL] == YES) {
429 if (selected_format) {
430 [video_device setValue:selected_format forKey:@"activeFormat"];
432 if (selected_range) {
433 NSValue *min_frame_duration = [selected_range valueForKey:@"minFrameDuration"];
434 [video_device setValue:min_frame_duration forKey:@"activeVideoMinFrameDuration"];
435 [video_device setValue:min_frame_duration forKey:@"activeVideoMaxFrameDuration"];
441 }
@catch(NSException *e) {
450 for (
format in [video_device valueForKey:
@"formats"]) {
451 CMFormatDescriptionRef formatDescription;
452 CMVideoDimensions dimensions;
454 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
455 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
457 for (
range in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
458 double min_framerate;
459 double max_framerate;
461 [[range valueForKey:@"minFrameRate"] getValue:&min_framerate];
462 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
464 dimensions.width, dimensions.height,
465 min_framerate, max_framerate);
475 NSError *
error = nil;
476 AVCaptureInput* capture_input = nil;
478 NSNumber *pixel_format;
479 NSDictionary *capture_dict;
480 dispatch_queue_t queue;
482 if (
ctx->video_device_index <
ctx->num_video_devices) {
483 capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device
error:&
error] autorelease];
485 capture_input = (AVCaptureInput*) video_device;
488 if (!capture_input) {
490 [[
error localizedDescription] UTF8String]);
494 if ([
ctx->capture_session canAddInput:capture_input]) {
495 [ctx->capture_session addInput:capture_input];
502 ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
504 if (!
ctx->video_output) {
514 }
@catch (NSException *exception) {
515 if (![[exception
name] isEqualToString:NSUndefinedKeyException]) {
539 if ([[
ctx->video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.avf_id]] == NSNotFound) {
540 av_log(
s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by the input device.\n",
546 for (NSNumber *pxl_fmt in [
ctx->video_output availableVideoCVPixelFormatTypes]) {
561 pxl_fmt_spec = pxl_fmt_dummy;
576 if (
ctx->capture_raw_data) {
577 ctx->pixel_format = pxl_fmt_spec.ff_id;
578 ctx->video_output.videoSettings = @{ };
580 ctx->pixel_format = pxl_fmt_spec.ff_id;
581 pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
582 capture_dict = [NSDictionary dictionaryWithObject:pixel_format
583 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
585 [ctx->video_output setVideoSettings:capture_dict];
587 [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames];
589 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
591 if (!
ctx->video_is_screen) {
592 int trans_ctrl = [video_device transportControlsSupported];
593 AVCaptureDeviceTransportControlsPlaybackMode trans_mode = [video_device transportControlsPlaybackMode];
596 ctx->observed_mode = trans_mode;
597 ctx->observed_device = video_device;
604 queue = dispatch_queue_create(
"avf_queue",
NULL);
605 [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
606 dispatch_release(queue);
608 if ([
ctx->capture_session canAddOutput:
ctx->video_output]) {
609 [ctx->capture_session addOutput:ctx->video_output];
621 NSError *
error = nil;
622 AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device
error:&
error] autorelease];
623 dispatch_queue_t queue;
625 if (!audio_dev_input) {
627 [[
error localizedDescription] UTF8String]);
631 if ([
ctx->capture_session canAddInput:audio_dev_input]) {
632 [ctx->capture_session addInput:audio_dev_input];
639 ctx->audio_output = [[AVCaptureAudioDataOutput alloc] init];
641 if (!
ctx->audio_output) {
648 queue = dispatch_queue_create(
"avf_audio_queue",
NULL);
649 [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
650 dispatch_release(queue);
652 if ([
ctx->capture_session canAddOutput:
ctx->audio_output]) {
653 [ctx->capture_session addOutput:ctx->audio_output];
665 CVImageBufferRef image_buffer;
666 CGSize image_buffer_size;
674 while (
ctx->frames_captured < 1) {
675 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
680 ctx->video_stream_index = stream->index;
684 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
687 image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
691 stream->codecpar->width = (int)image_buffer_size.width;
692 stream->codecpar->height = (
int)image_buffer_size.height;
693 stream->codecpar->format =
ctx->pixel_format;
697 stream->codecpar->format =
ctx->pixel_format;
700 CFRelease(
ctx->current_frame);
701 ctx->current_frame = nil;
711 CMFormatDescriptionRef format_desc;
719 while (
ctx->audio_frames_captured < 1) {
720 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
725 ctx->audio_stream_index = stream->index;
729 format_desc = CMSampleBufferGetFormatDescription(
ctx->current_audio_frame);
730 const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
739 stream->codecpar->sample_rate = basic_desc->mSampleRate;
742 ctx->audio_channels = basic_desc->mChannelsPerFrame;
743 ctx->audio_bits_per_sample = basic_desc->mBitsPerChannel;
744 ctx->audio_float = basic_desc->mFormatFlags & kAudioFormatFlagIsFloat;
745 ctx->audio_be = basic_desc->mFormatFlags & kAudioFormatFlagIsBigEndian;
746 ctx->audio_signed_integer = basic_desc->mFormatFlags & kAudioFormatFlagIsSignedInteger;
747 ctx->audio_packed = basic_desc->mFormatFlags & kAudioFormatFlagIsPacked;
748 ctx->audio_non_interleaved = basic_desc->mFormatFlags & kAudioFormatFlagIsNonInterleaved;
750 if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
752 ctx->audio_bits_per_sample == 32 &&
755 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
756 ctx->audio_signed_integer &&
757 ctx->audio_bits_per_sample == 16 &&
760 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
761 ctx->audio_signed_integer &&
762 ctx->audio_bits_per_sample == 24 &&
765 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
766 ctx->audio_signed_integer &&
767 ctx->audio_bits_per_sample == 32 &&
776 if (
ctx->audio_non_interleaved) {
777 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
778 ctx->audio_buffer_size = CMBlockBufferGetDataLength(block_buffer);
780 if (!
ctx->audio_buffer) {
787 CFRelease(
ctx->current_audio_frame);
788 ctx->current_audio_frame = nil;
796 #if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500))
797 NSMutableArray *deviceTypes = nil;
798 if (mediaType == AVMediaTypeVideo) {
799 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]];
800 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000)
801 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualCamera];
802 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTelephotoCamera];
804 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110100)
805 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTrueDepthCamera];
807 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 130000)
808 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTripleCamera];
809 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualWideCamera];
810 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInUltraWideCamera];
812 #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000)
813 [deviceTypes addObject: AVCaptureDeviceTypeDeskViewCamera];
815 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 150400)
816 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInLiDARDepthCamera];
818 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
819 [deviceTypes addObject: AVCaptureDeviceTypeContinuityCamera];
820 [deviceTypes addObject: AVCaptureDeviceTypeExternal];
821 #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
822 [deviceTypes addObject: AVCaptureDeviceTypeExternalUnknown];
824 }
else if (mediaType == AVMediaTypeAudio) {
825 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
826 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeMicrophone]];
828 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInMicrophone]];
830 }
else if (mediaType == AVMediaTypeMuxed) {
831 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
832 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternal]];
833 #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
834 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternalUnknown]];
842 AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession =
843 [AVCaptureDeviceDiscoverySession
844 discoverySessionWithDeviceTypes:deviceTypes
846 position:AVCaptureDevicePositionUnspecified];
847 return [captureDeviceDiscoverySession devices];
849 return [AVCaptureDevice devicesWithMediaType:mediaType];
858 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
859 uint32_t num_screens = 0;
861 AVCaptureDevice *video_device = nil;
862 AVCaptureDevice *audio_device = nil;
867 ctx->num_video_devices = [devices count] + [devices_muxed count];
871 ctx->is_stopping = 0;
873 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
874 CGGetActiveDisplayList(0,
NULL, &num_screens);
878 if (
ctx->list_devices) {
881 for (AVCaptureDevice *device in devices) {
882 const char *
name = [[device localizedName] UTF8String];
883 index = [devices indexOfObject:device];
886 for (AVCaptureDevice *device in devices_muxed) {
887 const char *
name = [[device localizedName] UTF8String];
888 index = [devices count] + [devices_muxed indexOfObject:device];
891 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
892 if (num_screens > 0) {
893 CGDirectDisplayID screens[num_screens];
894 CGGetActiveDisplayList(num_screens, screens, &num_screens);
895 for (
int i = 0;
i < num_screens;
i++) {
903 for (AVCaptureDevice *device in devices) {
904 const char *
name = [[device localizedName] UTF8String];
905 int index = [devices indexOfObject:device];
917 if (
ctx->video_device_index == -1 &&
ctx->video_filename) {
918 sscanf(
ctx->video_filename,
"%d", &
ctx->video_device_index);
920 if (
ctx->audio_device_index == -1 &&
ctx->audio_filename) {
921 sscanf(
ctx->audio_filename,
"%d", &
ctx->audio_device_index);
924 if (
ctx->video_device_index >= 0) {
925 if (
ctx->video_device_index <
ctx->num_video_devices) {
926 if (
ctx->video_device_index < [devices count]) {
927 video_device = [devices objectAtIndex:ctx->video_device_index];
929 video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])];
930 ctx->video_is_muxed = 1;
932 }
else if (
ctx->video_device_index <
ctx->num_video_devices + num_screens) {
933 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
934 CGDirectDisplayID screens[num_screens];
935 CGGetActiveDisplayList(num_screens, screens, &num_screens);
936 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->
num_video_devices]] autorelease];
938 if (
ctx->framerate.num > 0) {
939 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
942 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
943 if (
ctx->capture_cursor) {
944 capture_screen_input.capturesCursor = YES;
946 capture_screen_input.capturesCursor = NO;
950 if (
ctx->capture_mouse_clicks) {
951 capture_screen_input.capturesMouseClicks = YES;
953 capture_screen_input.capturesMouseClicks = NO;
956 video_device = (AVCaptureDevice*) capture_screen_input;
957 ctx->video_is_screen = 1;
963 }
else if (
ctx->video_filename &&
964 strncmp(
ctx->video_filename,
"none", 4)) {
965 if (!strncmp(
ctx->video_filename,
"default", 7)) {
966 video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
969 for (AVCaptureDevice *device in devices) {
970 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
971 video_device = device;
976 for (AVCaptureDevice *device in devices_muxed) {
977 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
978 video_device = device;
979 ctx->video_is_muxed = 1;
984 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
988 if(sscanf(
ctx->video_filename,
"Capture screen %d", &idx) && idx < num_screens) {
989 CGDirectDisplayID screens[num_screens];
990 CGGetActiveDisplayList(num_screens, screens, &num_screens);
991 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
992 video_device = (AVCaptureDevice*) capture_screen_input;
993 ctx->video_device_index =
ctx->num_video_devices + idx;
994 ctx->video_is_screen = 1;
996 if (
ctx->framerate.num > 0) {
997 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
1000 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
1001 if (
ctx->capture_cursor) {
1002 capture_screen_input.capturesCursor = YES;
1004 capture_screen_input.capturesCursor = NO;
1008 if (
ctx->capture_mouse_clicks) {
1009 capture_screen_input.capturesMouseClicks = YES;
1011 capture_screen_input.capturesMouseClicks = NO;
1018 if (!video_device) {
1025 if (
ctx->audio_device_index >= 0) {
1028 if (
ctx->audio_device_index >= [devices count]) {
1033 audio_device = [devices objectAtIndex:ctx->audio_device_index];
1034 }
else if (
ctx->audio_filename &&
1035 strncmp(
ctx->audio_filename,
"none", 4)) {
1036 if (!strncmp(
ctx->audio_filename,
"default", 7)) {
1037 audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
1041 for (AVCaptureDevice *device in devices) {
1042 if (!strncmp(
ctx->audio_filename, [[device localizedName] UTF8String], strlen(
ctx->audio_filename))) {
1043 audio_device = device;
1049 if (!audio_device) {
1056 if (!video_device && !audio_device) {
1062 if (
ctx->video_device_index <
ctx->num_video_devices) {
1069 av_log(
s,
AV_LOG_DEBUG,
"audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
1073 ctx->capture_session = [[AVCaptureSession alloc] init];
1081 [ctx->capture_session startRunning];
1085 if (!
ctx->video_is_screen) {
1086 [video_device unlockForConfiguration];
1110 CVPixelBufferRef image_buffer,
1114 int src_linesize[4];
1115 const uint8_t *src_data[4];
1116 int width = CVPixelBufferGetWidth(image_buffer);
1117 int height = CVPixelBufferGetHeight(image_buffer);
1120 memset(src_linesize, 0,
sizeof(src_linesize));
1121 memset(src_data, 0,
sizeof(src_data));
1123 status = CVPixelBufferLockBaseAddress(image_buffer, 0);
1124 if (
status != kCVReturnSuccess) {
1129 if (CVPixelBufferIsPlanar(image_buffer)) {
1130 size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
1132 for(
i = 0;
i < plane_count;
i++){
1133 src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer,
i);
1134 src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer,
i);
1137 src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
1138 src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
1142 src_data, src_linesize,
1147 CVPixelBufferUnlockBaseAddress(image_buffer, 0);
1158 CVImageBufferRef image_buffer;
1159 CMBlockBufferRef block_buffer;
1161 if (
ctx->current_frame != nil) {
1165 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
1166 block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_frame);
1168 if (image_buffer != nil) {
1169 length = (int)CVPixelBufferGetDataSize(image_buffer);
1170 }
else if (block_buffer != nil) {
1171 length = (int)CMBlockBufferGetDataLength(block_buffer);
1185 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_frame, 1, &
timing_info, &count) == noErr) {
1197 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1198 if (
ret != kCMBlockBufferNoErr) {
1202 CFRelease(
ctx->current_frame);
1203 ctx->current_frame = nil;
1209 }
else if (
ctx->current_audio_frame != nil) {
1210 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
1211 int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
1213 if (!block_buffer || !block_buffer_size) {
1218 if (
ctx->audio_non_interleaved && block_buffer_size >
ctx->audio_buffer_size) {
1231 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_audio_frame, 1, &
timing_info, &count) == noErr) {
1239 if (
ctx->audio_non_interleaved) {
1242 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
ctx->audio_buffer);
1243 if (
ret != kCMBlockBufferNoErr) {
1248 num_samples =
pkt->
size / (
ctx->audio_channels * (
ctx->audio_bits_per_sample >> 3));
1251 #define INTERLEAVE_OUTPUT(bps) \
1253 int##bps##_t **src; \
1254 int##bps##_t *dest; \
1255 src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
1257 unlock_frames(ctx); \
1258 return AVERROR(EIO); \
1261 for (c = 0; c < ctx->audio_channels; c++) { \
1262 src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
1264 dest = (int##bps##_t*)pkt->data; \
1265 shift = bps - ctx->audio_bits_per_sample; \
1266 for (sample = 0; sample < num_samples; sample++) \
1267 for (c = 0; c < ctx->audio_channels; c++) \
1268 *dest++ = src[c][sample] << shift; \
1272 if (
ctx->audio_bits_per_sample <= 16) {
1278 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1279 if (
ret != kCMBlockBufferNoErr) {
1285 CFRelease(
ctx->current_audio_frame);
1286 ctx->current_audio_frame = nil;
1289 if (
ctx->observed_quit) {
1299 if (
ctx->is_stopping) {
1339 .
p.
name =
"avfoundation",