AVCaptureDevice::setActiveFormat breaks AVCaptureSession for Audio 16 bits formats

Operating system: MacOS Sonoma 14.1

Description:

I am using ffmpeg's AVFoundation input device to read data from audio devices.

The ffmpeg code for this can be found at: https://github.com/FFmpeg/FFmpeg/blob/master/libavdevice/avfoundation.m

I added some custom code that iterates over the formats available in audio_device and sets the active format based on the sample format or sample rate:

[audio_device setActiveFormat:selected_format];

This works for most of the cases, however, if the selected format is pcm_s16le instead of pcm_f32le the following error appears in the console: [AudioConverter] Input data proc returned inconsistent 512 packets for 2048 bytes; at 8 bytes per packet, that is actually 256 packets.

No frames are returned causing ffmpeg to get stuck at:

// Take stream info from the first frame.
while (ctx->audio_frames_captured < 1) {
   CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
}

My guess is that the AVCaptureSession has an internal AudioConverter which tries to convert from pcm_f32le even if the audio device's active format was changed.

If I remove the setActiveFormat line and restart the program it will keep failing unless I restart the device.

Is there any workaround for this or something I should set in the AVCaptureSession?

AVCaptureDevice::setActiveFormat breaks AVCaptureSession for Audio 16 bits formats
 
 
Q