Reputation: 69
This is my first post to stackoverflow. At present I am developing an voip app for ios. I want to do something like this.
//in a thread
while(callIsOnGoing){
data = getDataFromNetwork()
playData()
sleep(10ms)
}
But problem is that audio in ios works in a "Pull" model(uses callback to get data). But i need to push data to play it. I have tried AudioQueue, but in audioQueue the data i push in buffer outside of callback doesn't get played though callback is called.
Again, i have seen AVCaptureToAudioUnit example by apple(http://developer.apple.com/library/ios/#samplecode/AVCaptureToAudioUnit/Introduction/Intro.html) where they called AudioUnitRender synchronously in case of of a delay audio unit. I tried similar for RemoteI/O Audio unit. But every time it returns OSStatus -50. The code is given below
//in a separate thread
do { // 5
int data_length = [NativeLibraryHelper GetData:(playBuff)];
if(data_length == 0){
}else{
double numberOfFrameCount = data_length / player->audioStreamDesc->mBytesPerFrame;
currentSampleTime += numberOfFrameCount;
//AudioUnitRenderActionFlags flags = 0;
AudioTimeStamp timeStamp;
memset(&timeStamp, 0, sizeof(AudioTimeStamp));
timeStamp.mSampleTime = currentSampleTime;
timeStamp.mFlags |= kAudioTimeStampSampleTimeValid;
AudioUnitRenderActionFlags flags = 0;
AudioBuffer buffer;
buffer.mNumberChannels = player->audioStreamDesc->mChannelsPerFrame;
buffer.mDataByteSize = data_length;
buffer.mData = malloc(data_length);
memcpy(buffer.mData, playBuff, data_length);
AudioBufferList audBuffList;
audBuffList.mBuffers[0] = buffer;
audBuffList.mNumberBuffers = 1;
printf("Audio REnder call back funciotn called with data size %d\n", data_length);
status = AudioUnitRender(audioUnitInstance, &flags, &timeStamp, 0, numberOfFrameCount, &audBuffList);
printf("osstatus %d\n", status);
}//end if else
CFRunLoopRunInMode ( // 6
kCFRunLoopDefaultMode, // 7
0.25, // 8
false // 9
);
//} while (aqData.mIsRunning);
[NSThread sleepForTimeInterval:.05];
}while (player->isRunning == YES);
I am struggling with audio play part for more than one month. Please help. Thanks in advance.
Upvotes: 0
Views: 672
Reputation: 70663
One general solution is to have an async network getdata/read function push data to an intermediate buffer or queue, then have the audio callback read from that intermediate buffer (or read silence if the intermediate buffer/queue is empty).
Upvotes: 1