AudioOutputUnitStart is slow on iphone simulator&device
I work on custom voip application. It relies in audio input/output on CoreAudio. I discovered a problem - when application calls AudioOutputUnitStart - it freezes for a few seconds.
OSStatus status;
InitializeAudioSession();
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
mVoiceUnitComponent = AudioComponentFindNext(NULL, &desc);
status = AudioComponentInstanceNew(mVoiceUnitComponent, &mVoiceUnit);
UInt32 enable = 1;
AudioUnitElement inputBus = 1, outputBus = 0;
status = AudioUnitSetProperty(mVoiceUnit, kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input, inputBus,
&enable, sizeof(enable));
status = AudioUnitSetProperty(mVoiceUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output,
outputBus, &enable, sizeof(enable));
memset(&mStreamDesc, 0, sizeof(mStreamDesc));
mStreamDesc.mSampleRate = SPK_SAMPLE_RATE;
mStreamDesc.mFormatID = kAudioFormatLinearPCM;
mStreamDesc.mFormatFlags = kAudioFormatFlagsCanonical;
mStreamDesc.mBitsPerChannel = 16;
mStreamDesc.mChannelsPerFrame = 1;
mStreamDesc.mBytesPerFrame = 2;
mStreamDesc.mFramesPerPacket = 1;
mStreamDesc.mBytesPerPacket = 2;
status = AudioUnitSetProperty(mVoiceUnit, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
inputBus, &mStreamDesc, sizeof(mStreamDesc));
StartAudioSession();
status = AudioUnitInitialize(mVoiceUnit);
status = AudioUnitSetProperty(mVoiceUnit, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
outputBus, &mStreamDesc, sizeof(mStreamDesc));
AURenderCallbackStruct outputCallback;
outputCallback.inputProc = IOSAudio::OutputRenderCallback开发者_如何学运维;
outputCallback.inputProcRefCon = this;
status = AudioUnitSetProperty(mVoiceUnit, kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, outputBus,
&outputCallback, sizeof(outputCallback));
AURenderCallbackStruct inputCallback;
inputCallback.inputProc = IOSAudio::InputRenderCallback;
inputCallback.inputProcRefCon = this;
status = AudioUnitSetProperty(mVoiceUnit, kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global, inputBus,
&inputCallback, sizeof(inputCallback));
status = AudioOutputUnitStart(mVoiceUnit);
mActive = true;
The SPK_SAMPLE_RATE is 8000 or 32000 - I tested both of them.
May anyone give me advice?
I made a little investigation. Looks I was wrong - there is only simulator's issue. I did not find any cause of this. So I add workaround - open audio in worker thread.
精彩评论