我正在尝试使用AVAssetReader读取视频文件并将音频传递给CoreAudio进行处理(添加效果和内容),然后使用AVAssetWriter将其保存回磁盘。我想指出,如果我将输出节点的AudioComponentDescription上的componentSubType设置为RemoteIO,那么通过扬声器可以正常播放。这使我确信我的AUGraph设置正确,因为我可以听到工作正常。我将subType设置为GenericOutput,因此我可以自己进行渲染并获取调整后的音频。
我正在读取音频,我将CMSampleBufferRef传递给copyBuffer。这会将音频放入循环缓冲区,稍后将会读取。
- (void)copyBuffer:(CMSampleBufferRef)buf {
if (_readyForMoreBytes == NO)
{
return;
}
AudioBufferList abl;
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(buf, NULL, &abl, sizeof(abl), NULL, NULL, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &blockBuffer);
UInt32 size = (unsigned int)CMSampleBufferGetTotalSampleSize(buf);
BOOL bytesCopied = TPCircularBufferProduceBytes(&circularBuffer, abl.mBuffers[0].mData, size);
if (!bytesCopied){
/
_readyForMoreBytes = NO;
if (size > kRescueBufferSize){
NSLog(@"Unable to allocate enought space for rescue buffer, dropping audio frame");
} else {
if (rescueBuffer == nil) {
rescueBuffer = malloc(kRescueBufferSize);
}
rescueBufferSize = size;
memcpy(rescueBuffer, abl.mBuffers[0].mData, size);
}
}
CFRelease(blockBuffer);
if (!self.hasBuffer && bytesCopied > 0)
{
self.hasBuffer = YES;
}
}
接下来我调用processOutput。这将在outputUnit上进行手动reder。当调用AudioUnitRender时,它会调用下面的playbackCallback,这是我第一个节点上作为输入回调连接的内容。 playbackCallback将数据从循环缓冲区中拉出并将其提供给传入的audioBufferList。如前所述,如果输出设置为RemoteIO,这将导致音频在扬声器上正确播放。当AudioUnitRender完成时,它返回noErr并且bufferList对象包含有效数据。 当我调用CMSampleBufferSetDataBufferFromAudioBufferList但我得到kCMSampleBufferError_RequiredParameterMissing(-12731)。
-(CMSampleBufferRef)processOutput
{
if(self.offline == NO)
{
return NULL;
}
AudioUnitRenderActionFlags flags = 0;
AudioTimeStamp inTimeStamp;
memset(&inTimeStamp, 0, sizeof(AudioTimeStamp));
inTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
UInt32 busNumber = 0;
UInt32 numberFrames = 512;
inTimeStamp.mSampleTime = 0;
UInt32 channelCount = 2;
AudioBufferList *bufferList = (AudioBufferList*)malloc(sizeof(AudioBufferList)+sizeof(AudioBuffer)*(channelCount-1));
bufferList->mNumberBuffers = channelCount;
for (int j=0; j<channelCount; j++)
{
AudioBuffer buffer = {0};
buffer.mNumberChannels = 1;
buffer.mDataByteSize = numberFrames*sizeof(SInt32);
buffer.mData = calloc(numberFrames,sizeof(SInt32));
bufferList->mBuffers[j] = buffer;
}
CheckError(AudioUnitRender(outputUnit, &flags, &inTimeStamp, busNumber, numberFrames, bufferList), @"AudioUnitRender outputUnit");
CMSampleBufferRef sampleBufferRef = NULL;
CMFormatDescriptionRef format = NULL;
CMSampleTimingInfo timing = { CMTimeMake(1, 44100), kCMTimeZero, kCMTimeInvalid };
AudioStreamBasicDescription audioFormat = self.audioFormat;
CheckError(CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &audioFormat, 0, NULL, 0, NULL, NULL, &format), @"CMAudioFormatDescriptionCreate");
CheckError(CMSampleBufferCreate(kCFAllocatorDefault, NULL, false, NULL, NULL, format, numberFrames, 1, &timing, 0, NULL, &sampleBufferRef), @"CMSampleBufferCreate");
CheckError(CMSampleBufferSetDataBufferFromAudioBufferList(sampleBufferRef, kCFAllocatorDefault, kCFAllocatorDefault, 0, bufferList), @"CMSampleBufferSetDataBufferFromAudioBufferList");
return sampleBufferRef;
}
static OSStatus playbackCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
int numberOfChannels = ioData->mBuffers[0].mNumberChannels;
SInt16 *outSample = (SInt16 *)ioData->mBuffers[0].mData;
/
memset(outSample, 0, ioData->mBuffers[0].mDataByteSize);
MyAudioPlayer *p = (__bridge MyAudioPlayer *)inRefCon;
if (p.hasBuffer){
int32_t availableBytes;
SInt16 *bufferTail = TPCircularBufferTail([p getBuffer], &availableBytes);
int32_t requestedBytesSize = inNumberFrames * kUnitSize * numberOfChannels;
int bytesToRead = MIN(availableBytes, requestedBytesSize);
memcpy(outSample, bufferTail, bytesToRead);
TPCircularBufferConsume([p getBuffer], bytesToRead);
if (availableBytes <= requestedBytesSize*2){
[p setReadyForMoreBytes];
}
if (availableBytes <= requestedBytesSize) {
p.hasBuffer = NO;
}
}
return noErr;
}
我传入的CMSampleBufferRef看起来有效(下面是调试器中对象的转储)
CMSampleBuffer 0x7f87d2a03120 retainCount: 1 allocator: 0x103333180
invalid = NO
dataReady = NO
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
formatDescription = <CMAudioFormatDescription 0x7f87d2a02b20 [0x103333180]> {
mediaType:'soun'
mediaSubType:'lpcm'
mediaSpecific: {
ASBD: {
mSampleRate: 44100.000000
mFormatID: 'lpcm'
mFormatFlags: 0xc2c
mBytesPerPacket: 2
mFramesPerPacket: 1
mBytesPerFrame: 2
mChannelsPerFrame: 1
mBitsPerChannel: 16 }
cookie: {(null)}
ACL: {(null)}
}
extensions: {(null)}
}
sbufToTrackReadiness = 0x0
numSamples = 512
sampleTimingArray[1] = {
{PTS = {0/1 = 0.000}, DTS = {INVALID}, duration = {1/44100 = 0.000}},
}
dataBuffer = 0x0
缓冲区列表如下所示
Printing description of bufferList:
(AudioBufferList *) bufferList = 0x00007f87d280b0a0
Printing description of bufferList->mNumberBuffers:
(UInt32) mNumberBuffers = 2
Printing description of bufferList->mBuffers:
(AudioBuffer [1]) mBuffers = {
[0] = (mNumberChannels = 1, mDataByteSize = 2048, mData = 0x00007f87d3008c00)
}
在这里真的不知所措,希望有人可以提供帮助。谢谢,
如果重要的话我在ios 8.3模拟器中进行调试,音频来自我在iphone 6上拍摄的mp4,然后保存到我的笔记本电脑中。
我已经阅读了以下问题,但仍无济于事,事情无效。
How to convert AudioBufferList to CMSampleBuffer?
Converting an AudioBufferList to a CMSampleBuffer Produces Unexpected Results
CMSampleBufferSetDataBufferFromAudioBufferList returning error 12731
core audio offline rendering GenericOutput
更新
我喋喋不休地注意到,当我在AudioUnitRender运行之前的AudioBufferList看起来像这样:
bufferList->mNumberBuffers = 2,
bufferList->mBuffers[0].mNumberChannels = 1,
bufferList->mBuffers[0].mDataByteSize = 2048
mDataByteSize是numberFrames * sizeof(SInt32),它是512 * 4.当我查看playbackCallback中传递的AudioBufferList时,列表如下所示:
bufferList->mNumberBuffers = 1,
bufferList->mBuffers[0].mNumberChannels = 1,
bufferList->mBuffers[0].mDataByteSize = 1024
不确定其他缓冲区的位置,或其他1024字节大小......
如果我完成了Redner的调用,如果我做了类似的事情
AudioBufferList newbuff;
newbuff.mNumberBuffers = 1;
newbuff.mBuffers[0] = bufferList->mBuffers[0];
newbuff.mBuffers[0].mDataByteSize = 1024;
并将newbuff传递给CMSampleBufferSetDataBufferFromAudioBufferList,错误消失。
如果我尝试将BufferList的大小设置为1 mNumberBuffers或其mDataByteSize为numberFrames * sizeof(SInt16),则在调用AudioUnitRender时会得到-50
更新2
我连接了渲染回调,因此当我在扬声器上播放声音时,我可以检查输出。我注意到发送到扬声器的输出还有一个带有2个缓冲区的AudioBufferList,输入回调期间的mDataByteSize是1024,而渲染回调中它是2048,这与我手动调用AudioUnitRender时看到的相同。当我检查渲染的AudioBufferList中的数据时,我注意到2个缓冲区中的字节是相同的,这意味着我可以忽略第二个缓冲区。但是我不知道如何处理这样一个事实,即数据在被渲染之后的大小是2048,而不是1024,因为它被吸收。任何关于为什么会发生这种情况的想法?在通过音频图表之后,它是更多的原始形式,这就是为什么尺寸加倍?
答案 0 :(得分:1)
听起来你正在处理的问题是由于频道数量的差异。您在2048而不是1024的块中查看数据的原因是因为它正在向您提供两个通道(立体声)。检查以确保所有音频单元都已正确配置为在整个音频图表中使用单声道,包括音高单元和任何音频格式说明。
要特别注意的一件事是对AudioUnitSetProperty
的调用可能会失败 - 所以一定要将它们包装在CheckError()
中。