Problem with audio queues
Problem with audio queues
- Subject: Problem with audio queues
- From: Brad Soe <email@hidden>
- Date: Sun, 10 Aug 2008 20:38:22 -0700 (PDT)
I'm having a problem with core audio queues. I have set up several audio queues to service my applications sound requirements and I continually play different sounds in my app. I have already converted the data to PCM and I am writting PCM data directly to the buffers. What I do when I play a sound is that when I want to play a sound, I grab an unused queue. If the queue is not set up, I call, AudioQueueNewOutput, then I call AudioQueueAllocateBuffer for the buffers. I fill the buffers with audio data and then call AudioQueueStart. In my call back, I fill a buffer and enqueue them, if there's no more data I AudioQueueStop. Then the queue sits until the next time when I call my initialize and play functions again. My callback and some code snippets are included.I don't tear down the queues until the app quits. My callback and some code snippets are included. I can play audio correctly , but eventually the application will hang on
AudioQueueEnqueueBuffer in my callback and spinlock (show the beach ball). The stack trace is below and it appears that a thread is locked. I don't set up a thread before hand, I assume that core audio is taking care of that in AudioQueueNewOutput? Can someone suggest the right way to do this? Sometimes the application works and sometimes it works and eventually quits. It also may happen on the first sound played, so it's pretty random. I'm using 10.5.2 and xcode 3.0. If you need more details, please let me know.
Thanks
Brad
#0 0xffff027f in __spin_lock ()
#1 0x909dd41f in pthread_cond_signal_thread_np ()
#2 0x909dd286 in pthread_cond_signal ()
#3 0x91f09250 in CAGuard::Notify ()
#4 0x91f2b876 in AudioQueueObject::EnqueueBuffer ()
#5 0x91f3c72e in AQServer_EnqueueBuffer ()
#6 0x91f40293 in AudioQueueEnqueueBufferWithParameters ()
#7 0x91f404b6 in AudioQueueEnqueueBuffer ()
#8 0x00b138d1 in OSXAudioCallback (inUserData=0x279e5c10, inAQ=0x351da0c0, inCompleteAQBuffer=0x351df080) at ../../../src/Test/../Lib/Sound/MacOSXAudioStream.cpp:48
#9 0x00b14135 in MacOSXAudioStream::prime (this=0x279e5c10) at ../../../src/Test/../Lib/Sound/MacOSXAudioStream.cpp:239
#10 0x00b13f7f in MacOSXAudioStream::initialize (this=0x279e5c10, attributes=@0x279e5424) at ../../../src/Test/../Lib/Sound/MacOSXAudioStream.cpp:225
#11 0x00b1569a in MacOSXSamplePlayer::update (this=0x279e5410) at ../../../src/Test/../Lib/Sound/MacOSXSoundDriver.cpp:588
#12 0x00b158aa in MacOSXSamplePlayer::setPlayState (this=0x279e5410, s=SoundPlayStateStarting) at ../../../src/Test/../Lib/Sound/MacOSXSoundDriver.cpp:573
#13 0x00b1d0c8 in SoundManager3::updateStartingParameters (this=0x14d1ecc0, ss=0x30fb8ad0) at ../../../src/Test/../Lib/Sound/SoundManager3.cpp:1764
#14 0x00b1d552 in SoundManager3::updateLooping (this=0x14d1ecc0) at ../../../src/Test/../Lib/Sound/SoundManager3.cpp:1933
#15 0x00b1debe in SoundManager3::step (this=0x14d1ecc0) at ../../../src/Test/../Lib/Sound/SoundManager3.cpp:1545
#16 0x00276bf5 in ClientApp::onEvent (this=0x2799e210, id=11665486) at ../../../src/Test/../Lib/Client/ClientApp.cpp:5494
#17 0x0604d1cc in EventScheduler::fireFirstEvent (this=0x9cc5a10) at ../../../../src/Dll/ThereKernel/../../Lib/Event/EventScheduler.cpp:1136
#18 0x0604d41d in EventScheduler::pollOnce (this=0x9cc5a10, timeToNextEvent=0xbfffe1c0) at ../../../../src/Dll/ThereKernel/../../Lib/Event/EventScheduler.cpp:379
#19 0x00ca1cb2 in RunThereEvents (outDidEvent=0xbfffe23f, outTimeUntilNext=0xbfffe230) at ../../../src/Test/../Lib/UiMacOSX/MacOSXUiAppHost.cpp:185
#20 0x00ca1f1f in PlatformEventLoop (scheduler=0x9cc5a10, causalityScheduler=0x962aef4) at ../../../src/Test/../Lib/UiMacOSX/MacOSXUiAppHost.cpp:294
#21 0x0011921f in AppConfig::eventLoop (this=0x962a2e0) at ../../../src/Test/../Lib/App/AppConfig.cpp:581
#22 0x00024155 in ThereMain (argc=4, argv=0xbfffef78) at ../../../src/Test/ToonTest1/ToonTestMain.cpp:990
#23 0x000241ca in main (argc=4, argv=0xbfffef78) at ../../../src/Test/ToonTest1/ToonTestMain.cpp:155
/*
play: start playing the audio
*/
void
MacOSXAudioStream::play()
{
FID;
// Prime the queue
if (!isplaying_)
{
initialize
Log3(L"MacOSXAudioStream - play() : (%lx) ENTER isplaying_:%d !!!!", this, isplaying_);
OSStatus err = AudioQueueStart(aqRef_, (const AudioTimeStamp *)NULL);
if (err != noErr)
{
Log3(L"MacOSXAudioStream - AudioQueueStart failed!: err = %d", err);
return;
}
isplaying_ = true;
} else
Log3(L"MacOSXAudioStream - play() : (%lx) ALREADY playing !!!!", this);
}
/*
stop: stop playing audio
*/
void
MacOSXAudioStream::stop()
{
if (aqRef_ && isplaying_)
{
isplaying_ = false;
Log3(L"MacOSXAudioStream - AudioQueueStop called START %lx", this);
OSStatus err = AudioQueueStop(aqRef_, false); // true immediate, false async
if (err != noErr)
{
Log3(L"MacOSXAudioStream - AudioQueueStop failed!: err = %d", err);
}
Log3(L"MacOSXAudioStream - AudioQueueStop called STOPPED %lx doneplaying_", this);
doneplaying_ = true;
}
// sleepTimer_ = 10;
}
/*
initialize: initialize the queue with audio data
*/
void
MacOSXAudioStream::initialize( SoundSampleAttrDesc & attributes )
{
FID;
OSStatus err = noErr;
const int kPacketSize = 2048;
Log3(L"MacOSXAudioStream::initialize (%lx)", this );
// teardown();
// initialized_ = false;
isplaying_ = false;
doneplaying_ = false;
sampleDataPtr_ = currentDataPtr_ = NULL;
sampleDataLen_ = 0;
if (initialized_ == false)
{
sampleDataPtr_ = currentDataPtr_ = (void *)attributes.soundSampleDataInfo.pcmData.sampleData;
sampleDataLen_ = remainLen_ = attributes.soundSampleDataInfo.pcmData.sampleDataLength;
osxattributes_ = attributes;
numPackets_ = 1;
bufferLen_ = kPacketSize * numPackets_;
if (attributes.soundSampleDataInfo.dataFormat == SoundSampleDataFormatNamedSampleFile)
{
// When we get file based requests, they start here
}
else if (attributes.soundSampleDataInfo.dataFormat == SoundSampleDataFormatPcmData)
{
AudioStreamBasicDescription * oggasbd = (AudioStreamBasicDescription *)attributes.soundSampleDataInfo.macSpecific;
asbDesc_ = *oggasbd;
}
err = AudioQueueNewOutput(&asbDesc_, OSXAudioCallback, this, NULL, kCFRunLoopCommonModes, 0, &aqRef_);
if (err != noErr)
{
Log3(L"MacOSXAudioStream::initialize - AudioQueueNewOutput failed!: err = %d", err);
return;
}
err = AudioQueueAllocateBuffer(aqRef_, bufferLen_, &aqBuf1Ref_);
if (err != noErr)
{
Log3(L"MacOSXAudioStream::initialize - AudioQueueAllocateBuffer failed!: err = %d", err);
return;
}
err = AudioQueueAllocateBuffer(aqRef_, bufferLen_, &aqBuf2Ref_);
if (err != noErr)
{
Log3(L"MacOSXAudioStream::initialize - AudioQueueAllocateBuffer failed!: err = %d", err);
return;
}
err = AudioQueueAllocateBuffer(aqRef_, bufferLen_, &aqBuf3Ref_);
if (err != noErr)
{
Log3(L"MacOSXAudioStream::initialize - AudioQueueAllocateBuffer failed!: err = %d", err);
return;
}
initialized_ = true;
Log3(L"MacOSXAudioStream::initialize (%lx) - buffers created", this);
} else {
Log3(L"MacOSXAudioStream::initialize AudioQueueFlush - (%lx) - buffers created", aqRef_);
OSStatus err = AudioQueueFlush(aqRef_);
if (err != noErr)
{
Log3(L"MacOSXAudioStream::initialize - AudioQueueAllocateBuffer failed!: err = %d", err);
return;
}
}
// Set the sound data pointers and len here
sampleDataPtr_ = currentDataPtr_ = (void *)attributes.soundSampleDataInfo.pcmData.sampleData;
sampleDataLen_ = remainLen_ = attributes.soundSampleDataInfo.pcmData.sampleDataLength;
prime();
Log3(L"MacOSXAudioStream::initialize (%lx) - got data sampleDataPtr_ = %lx, sampleDataLen_ = %d", this, sampleDataPtr_, sampleDataLen_);
}
/*
prime: initialize the queue with audio data
*/
void
MacOSXAudioStream::prime()
{
FID;
OSXAudioCallback( this, aqRef_, aqBuf1Ref_);
OSXAudioCallback( this, aqRef_, aqBuf2Ref_);
OSXAudioCallback( this, aqRef_, aqBuf3Ref_);
}
/*
OSXAudioCallback: Audio Queue call back that fills the buffer with audio data once it's emptied
*/
static void OSXAudioCallback( void * inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inCompleteAQBuffer)
{
MacOSXAudioStream * myStream = (MacOSXAudioStream *)inUserData;
Assert(myStream != NULL);
if (myStream->doneplaying_)
{
Log3(L"MacOSXAudioStream (%lx) : OSXAudioCallback done playing leave!! %lx", myStream);
return;
}
if (myStream->remainLen_ > 0)
{
int bufferSize = myStream->bufferLen_;
void * buffer = myStream->currentDataPtr_;
int audioLen = (myStream->remainLen_ > bufferSize)?bufferSize:myStream->remainLen_;
Assert(buffer != NULL);
Log3(L"MacOSXAudioStream (%lx) : OSXAudioCallback copying %ld bytes", myStream, audioLen);
memcpy( inCompleteAQBuffer->mAudioData, buffer, audioLen );
inCompleteAQBuffer->mAudioDataByteSize = audioLen;
Log3(L"MacOSXAudioStream (%lx) : OSXAudioCallback AudioQueueEnqueueBuffer %ld bytes from buffer = %lx", myStream, audioLen, buffer);
OSStatus err = AudioQueueEnqueueBuffer(inAQ, inCompleteAQBuffer, 0, 0);
if (err != noErr)
{
Log3(L"MacOSXAudioStream (%lx) : OSXAudioCallback AudioQueueEnqueueBuffer failed! %lx", myStream);
}
// Adjust these pointers
myStream->currentDataPtr_ = ((unsigned char *)myStream->currentDataPtr_ + audioLen);
myStream->remainLen_ -= audioLen;
Log3(L"MacOSXAudioStream (%lx) : OSXAudioCallback queued %ld bytes, remain %ld", myStream, audioLen, myStream->remainLen_);
}
else
{
Log3(L"MacOSXAudioStream (%lx) - OSXAudioCallback stopped", myStream);
myStream->stop();
}
}
_______________________________________________
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