Ios 如何从AudioQueueBufferRef获取CMSampleBufferRef
我使用的是一个私人图书馆,它是为iPhone的直播制作的。 在每次录制每个帧时,它都会调用一个Ios 如何从AudioQueueBufferRef获取CMSampleBufferRef,ios,iphone,ffmpeg,avfoundation,Ios,Iphone,Ffmpeg,Avfoundation,我使用的是一个私人图书馆,它是为iPhone的直播制作的。 在每次录制每个帧时,它都会调用一个委托函数 void MyAQInputCallback(void *inUserData, AudioQueueRef inQueue, AudioQueueBufferRef inBuffer, const AudioTi
委托
函数
void MyAQInputCallback(void *inUserData,
AudioQueueRef inQueue,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription *inPacketDesc);
我想可能会以某种方式将AudioQueueBufferRef
转换为CMSampleBufferRef
谢谢。我想两年后你不会还在寻找解决方案,但为了防止有人遇到类似情况并发现这个问题(就像我一样),这里是我的解决方案 My Audio Queue callback函数调用下面的
appendAudioBuffer
函数,向其传递AudioQueueBufferRef及其长度(mAudioDataByteSize
)
请注意,当我调用appendAudioBuffer
时,声音不会被压缩;音频格式被指定为LPCM(这就是为什么我不使用数据包描述符,因为LPCM没有数据包描述符)。AVAssetWriterInput处理压缩。
我最初试图将AAC数据传递给AVAssetWriter,但这导致了太多的复杂性,我无法让它工作。您找到解决方案了吗?
[self.audioWriterInput appendSampleBuffer:sampleBuffer];
void appendAudioBuffer(void* pBuffer, long pLength)
{
// CMSampleBuffers require a CMBlockBuffer to hold the media data; we
// create a blockBuffer here from the AudioQueueBuffer's data.
CMBlockBufferRef blockBuffer;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
pBuffer,
pLength,
kCFAllocatorNull,
NULL,
0,
pLength,
kCMBlockBufferAssureMemoryNowFlag,
&blockBuffer);
// Timestamp of current sample
CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
CFTimeInterval elapsedTime = currentTime - mStartTime;
CMTime timeStamp = CMTimeMake(elapsedTime * mTimeScale, mTimeScale);
// Number of samples in the buffer
long nSamples = pLength / mWaveRecorder->audioFormat()->mBytesPerFrame;
CMSampleBufferRef sampleBuffer;
OSStatus err = CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault,
blockBuffer,
true,
NULL,
NULL,
mAudioFormatDescription,
nSamples,
timeStamp,
NULL,
&sampleBuffer);
// Add the audio sample to the asset writer input
if ([mAudioWriterInput isReadyForMoreMediaData]) {
if(![mAudioWriterInput appendSampleBuffer:sampleBuffer])
// print an error
}
else
// either do nothing and just print an error, or queue the CMSampleBuffer
// somewhere and add it later, when the AVAssetWriterInput is ready
CFRelease(sampleBuffer);
CFRelease(blockBuffer);
}