Please help with Audio Session and RemoteIO AU on iPhone and iPod touch
Please help with Audio Session and RemoteIO AU on iPhone and iPod touch
- Subject: Please help with Audio Session and RemoteIO AU on iPhone and iPod touch
- From: Inca Rose <email@hidden>
- Date: Fri, 21 Nov 2008 20:13:36 +0200
Hi;
First of all, sorry for the long post, but I'm stuck for a few days now.
I'm in charge of writing the VoIP client part of a game for iPod touch
and iPhone.
I have written most of the code already, just for a PoC, but I have
problems with the AudioSession + RemoteIO.
The signaling part of the project works OK, I'm using Sofia SIP
compiled for iPhone and it seems OK ( I have not tested too
much, but it works ).
The media part is really problematic. It is not consistence between
runs and I'm totally lost.
I Initialize the Audio Session with:
AudioSessionInitialize(NULL, NULL,
audioSessionInterruptionListener,self);
and set the Session with the following parameters:
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
err
=
AudioSessionSetProperty
(kAudioSessionProperty_AudioCategory,sizeof(audioCategory),
&audioCategory);
err
=
AudioSessionAddPropertyListener
(kAudioSessionProperty_AudioRouteChange, audioRouteChangeListener,self);
Float64 float64=8000.0;
UInt32 size = sizeof(float64);
err
=
AudioSessionSetProperty
(kAudioSessionProperty_PreferredHardwareSampleRate, size, &float64);
err
=
AudioSessionGetProperty
(kAudioSessionProperty_CurrentHardwareSampleRate, &size, &float64);
Float32 preferredBufferSize = 0.002;
size = sizeof(preferredBufferSize);
err
=
AudioSessionSetProperty
(kAudioSessionProperty_PreferredHardwareIOBufferDuration,
size,&preferredBufferSize);
err
=
AudioSessionGetProperty
(kAudioSessionProperty_CurrentHardwareIOBufferDuration,
&size,&preferredBufferSize);
First problem:
the CurrentHardwareSampleRate is not alway set to 8000, sometimes it
is, sometimes it remains on 44100
On iPhone without headset it is always 8000, but in iPod Touch it
sometimes is 8000 and sometimes is 44100
Second Problem:
the CurrentHardwareIOBuffer gets different values every run, some
times it gets very close to 0.002, sometimes it is
0.023... and sometimes it is 0.12.... or 0.018. This makes it
impossible to successfully gets samples from the mic
or send samples to the speaker in a proper manner.
Third problem is with the interrupt callback:
I get the callback for example when I press the mic button ( on the
headset ) on an iPod touch
and music starts to play. My app is still running, but when I press
the mic button again
the music stops playing but the callback is not called again, so I can
not continue
with the VoIP session because my AudioSession never becomes active
again.
The remoteIO initialization is:
AudioComponentDescription desc;
OSStatus err = noErr;
UInt32 param;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
AudioComponent component = AudioComponentFindNext(NULL, &desc);
AudioUnit rioUnit;
err = AudioComponentInstanceNew(component, &rioUnit);
UInt32 enableIO;
enableIO = 1;
err = AudioUnitSetProperty(rioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
1, // input element
&enableIO,
sizeof(enableIO));
err = AudioUnitSetProperty(rioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
0, //output element
&enableIO,
sizeof(enableIO));
UInt32 shouldAllocateBuffer = 0;
err = AudioUnitSetProperty(rioUnit,
kAudioUnitProperty_ShouldAllocateBuffer, kAudioUnitScope_Output,
1,&shouldAllocateBuffer, sizeof(shouldAllocateBuffer));
AudioStreamBasicDescription format;
param = sizeof(AudioStreamBasicDescription);
format.mBitsPerChannel =8;
format.mBytesPerFrame = 1;
format.mBytesPerPacket = 1;
format.mChannelsPerFrame = 1;
format.mFormatFlags = 0;
format.mFormatID = kAudioFormatULaw;
format.mFramesPerPacket = 1;
format.mReserved = 0;
format.mSampleRate = 8000.0;
err = AudioUnitSetProperty(rioUnit,
kAudioUnitProperty_StreamFormat,kAudioUnitScope_Output, 1,
&format,sizeof(AudioStreamBasicDescription));
err = AudioUnitSetProperty(rioUnit,
kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input, 0, &format,
sizeof(AudioStreamBasicDescription));
AURenderCallbackStruct renderCallback_struct;
renderCallback_struct.inputProc = callbackRender;
renderCallback_struct.inputProcRefCon = recorder;
err=AudioUnitSetProperty(rioUnit,kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, 0,&renderCallback_struct,
sizeof(renderCallback_struct));
AURenderCallbackStruct inputCallback_struct;
inputCallback_struct.inputProc = callbackInput;
inputCallback_struct.inputProcRefCon = recorder;
err
=
AudioUnitSetProperty
(rioUnit,kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Output, 0,&inputCallback_struct,
sizeof(inputCallback_struct));
err = AudioUnitInitialize(rioUnit);
On the InputCallback I get the samples from the mic to send to the
network. I was expecting to get 16 bytes of uLaw each time so I can
work with 10 millis ( 80 bytes of uLaw )
and 20 milis ( 160 bytes of uLaw ). But as I say before, I only get 16
bytes sometimes and only on the iPhone.
With the same code I also get 64 bytes, 183 bytes, and even 1024 bytes.
I use the RenderCallback to feed samples to the speakers ( or headset ).
I dont know if it is a problem with my code, a known problem or just
work like this by design, but I found very difficult to understand how
all this thing works.
It is not the first time we write a VoIP client, actually we have the
same app for different phone platforms.
If you have any idea on how to control sampleRate and the buffer size
please HELP ME.
Thanks
Inca Rose
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden