From 1834be435f4929390ce47a1273ab7b81629d22bf Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 11 Feb 2017 22:44:20 -1000 Subject: [PATCH] Fix AudioQueueServices stall-out --- lib/audiodev/AQS.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/audiodev/AQS.cpp b/lib/audiodev/AQS.cpp index 9a6f374..eb70f40 100644 --- a/lib/audiodev/AQS.cpp +++ b/lib/audiodev/AQS.cpp @@ -57,7 +57,17 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine std::unique_lock lk(engine->m_engineMutex); engine->m_cbWaiting = true; - engine->m_engineEnterCv.wait(lk); + if (engine->m_engineEnterCv.wait_for(lk, + std::chrono::nanoseconds(engine->m_mixInfo.m_periodFrames * 750000000 / + size_t(engine->m_mixInfo.m_sampleRate))) == std::cv_status::timeout) + { + inBuffer->mAudioDataByteSize = engine->m_frameBytes; + memset(inBuffer->mAudioData, 0, engine->m_frameBytes); + AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, nullptr); + engine->m_cbWaiting = false; + engine->m_engineLeaveCv.notify_one(); + return; + } engine->m_cbWaiting = false; engine->_pumpAndMixVoices(engine->m_mixInfo.m_periodFrames, reinterpret_cast(inBuffer->mAudioData)); @@ -240,7 +250,6 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine for (int i=0 ; inumPackets ; ++i) { std::vector bytes(std::cbegin(packet->data), std::cbegin(packet->data) + packet->length); - ; readProcRefCon->m_receiver(std::move(bytes), AudioConvertHostTimeToNanos(packet->timeStamp) / 1.0e9); packet = MIDIPacketNext(packet); } @@ -689,7 +698,7 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine while (chMapOut.m_channelCount < chCount) chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::Unknown; - m_mixInfo.m_periodFrames = 2400; + m_mixInfo.m_periodFrames = 2400 * size_t(actualSampleRate) / 48000; for (int i=0 ; i<3 ; ++i) if (AudioQueueAllocateBuffer(m_queue, m_mixInfo.m_periodFrames * chCount * 4, &m_buffers[i])) {