[英]Using AVCaptureSession and Audio Units Together Causes Problems for AVAssetWriterInput
我正在开发一个同时执行两项操作的iOS应用程序:
不幸的是,当我们使用音频单元配置应用程序以启用回声消除时,录制功能会中断:我们用来编码音频的AVAssetWriterInput
实例会拒绝传入的样本。 当我们没有设置音频单元时,录音工作,但我们有可怕的回声。
为了启用回声消除,我们配置了这样的音频单元(为了简洁起见):
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
OSStatus status = AudioComponentInstanceNew(comp, &_audioUnit);
status = AudioUnitInitialize(_audioUnit);
这适用于视频聊天,但它打破了录制功能,这样设置(再次,释义 - 实际实现分散在几种方法)。
_captureSession = [[AVCaptureSession alloc] init];
// Need to use the existing audio session & configuration to ensure we get echo cancellation
_captureSession.usesApplicationAudioSession = YES;
_captureSession.automaticallyConfiguresApplicationAudioSession = NO;
[_captureSession beginConfiguration];
AVCaptureDeviceInput *audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self audioCaptureDevice] error:NULL];
[_captureSession addInput:audioInput];
_audioDataOutput = [[AVCaptureAudioDataOutput alloc] init];
[_audioDataOutput setSampleBufferDelegate:self queue:_cameraProcessingQueue];
[_captureSession addOutput:_audioDataOutput];
[_captureSession commitConfiguration];
captureOutput
的相关部分看起来像这样:
NSLog(@"Audio format, channels: %d, sample rate: %f, format id: %d, bits per channel: %d", basicFormat->mChannelsPerFrame, basicFormat->mSampleRate, basicFormat->mFormatID, basicFormat->mBitsPerChannel);
if (_assetWriter.status == AVAssetWriterStatusWriting) {
if (_audioEncoder.readyForMoreMediaData) {
if (![_audioEncoder appendSampleBuffer:sampleBuffer]) {
NSLog(@"Audio encoder couldn't append sample buffer");
}
}
}
发生的事情是对appendSampleBuffer
的调用失败,但是 - 这是奇怪的部分 - 如果我没有耳机插入我的手机。 检查发生这种情况时产生的日志,我发现没有连接耳机,日志消息中报告的频道数为3 ,而连接耳机时,频道数为1 。 这解释了编码操作失败的原因,因为编码器配置为只需要一个通道。
我不明白为什么我在这里有三个频道。 如果我注释掉初始化音频单元的代码,我只能获得单个通道并且录制工作正常,但回声消除不起作用。 此外,如果我删除这些行
// Need to use the existing audio session & configuration to ensure we get echo cancellation
_captureSession.usesApplicationAudioSession = YES;
_captureSession.automaticallyConfiguresApplicationAudioSession = NO;
录音工作(我只有一个有或没有耳机的通道),但同样,我们失去了回声消除。
因此,我的问题的关键是:当我配置音频单元以提供回声消除时,为什么我会获得三个音频通道? 此外,有没有办法防止这种情况发生或使用AVCaptureSession
解决此问题?
我已经考虑过将麦克风音频直接从低级音频单元回调传输到编码器以及聊天管道,但似乎想要制作必要的Core Media缓冲区就可以做一些工作。我想尽可能避免。
请注意,聊天和录制功能是由不同的人编写的 - 他们都不是我 - 这就是这个代码没有更多集成的原因。 如果可能的话,我想避免重构整个混乱局面。
最后,我能够通过I / O音频单元从麦克风收集音频样本,将这些样本重新打包到CMSampleBuffer
,并将新构建的CMSampleBuffer
传递到编码器中来解决此问题。
执行转换的代码如下所示(为简洁起见,缩写)。
// Create a CMSampleBufferRef from the list of samples, which we'll own
AudioStreamBasicDescription monoStreamFormat;
memset(&monoStreamFormat, 0, sizeof(monoStreamFormat));
monoStreamFormat.mSampleRate = 48000;
monoStreamFormat.mFormatID = kAudioFormatLinearPCM;
monoStreamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved;
monoStreamFormat.mBytesPerPacket = 2;
monoStreamFormat.mFramesPerPacket = 1;
monoStreamFormat.mBytesPerFrame = 2;
monoStreamFormat.mChannelsPerFrame = 1;
monoStreamFormat.mBitsPerChannel = 16;
CMFormatDescriptionRef format = NULL;
OSStatus status = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &monoStreamFormat, 0, NULL, 0, NULL, NULL, &format);
// Convert the AudioTimestamp to a CMTime and create a CMTimingInfo for this set of samples
uint64_t timeNS = (uint64_t)(hostTime * _hostTimeToNSFactor);
CMTime presentationTime = CMTimeMake(timeNS, 1000000000);
CMSampleTimingInfo timing = { CMTimeMake(1, 48000), presentationTime, kCMTimeInvalid };
CMSampleBufferRef sampleBuffer = NULL;
status = CMSampleBufferCreate(kCFAllocatorDefault, NULL, false, NULL, NULL, format, numSamples, 1, &timing, 0, NULL, &sampleBuffer);
// add the samples to the buffer
status = CMSampleBufferSetDataBufferFromAudioBufferList(sampleBuffer,
kCFAllocatorDefault,
kCFAllocatorDefault,
0,
samples);
// Pass the buffer into the encoder...
请注意,我已删除错误处理和清理已分配的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.