iOS音频推流格式转换

音频采集会得到格式为CMSampleBufferRef的音频包,音频推流一般把音频流转换成flv格式

(1)将音频流CMSampleBufferRef转换成PCM data

- (NSData *)convertAudioSmapleBufferToPcmData:(CMSampleBufferRef) audioSample{

//获取数据大小

NSInteger audioDataSize =CMSampleBufferGetTotalSampleSize(audioSample);

//分配数据空间

int8_t * audio_data =aw_alloc((int32_t)audioDataSize);

//创建CMBlockBufferRef,获取PCM数据

CMBlockBufferRef dataBuffer =CMSampleBufferGetDataBuffer(audioSample);

//将数据copy至分配的内存中

CMBlockBufferCopyDataBytes(dataBuffer,0, audioDataSize, audio_data);

//返回PCM data

return [NSData dataWithBytesNoCopy:audio_data length:audioDataSize];

}

(2)将音频PCM data编码成acc格式并合成为flv

- (aw_flv_audio_tag*)encodePCMDataToFlvTag:(NSData*)pcmData{

self.curFramePcmData= pcmData;

AudioBufferList outAudioBufferList = {0};

outAudioBufferList.mNumberBuffers=1;

outAudioBufferList.mBuffers[0].mNumberChannels = (uint32_t)self.audioConfig.channelCount;

outAudioBufferList.mBuffers[0].mDataByteSize = self.aMaxOutputFrameSize;

outAudioBufferList.mBuffers[0].mData = malloc(self.aMaxOutputFrameSize);

uint32_t outputDataPacketSize =1;

OSStatus status =AudioConverterFillComplexBuffer(_aConverter,aacEncodeInputDataProc, (__bridgevoid*_Nullable)(self), &outputDataPacketSize, &outAudioBufferList,NULL);

if(status == noErr) {

NSData * rawAAC = [NSDatadataWithBytesNoCopy: outAudioBufferList.mBuffers[0].mDatalength:outAudioBufferList.mBuffers[0].mDataByteSize];

self.manager.timestamp += 1024*1000/self.audioConfig.sampleRate;

return aw_encoder_create_audio_tag((int8_t*)rawAAC.bytes, rawAAC.length, (uint32_t)self.manager.timestamp, &_faacConfig);

}else {

NSLog(@"aac编码错误");

}

return NULL;

}

你可能感兴趣的:(iOS音频推流格式转换)