Fixing a re-sync issue on macOS by reworking CoreAudioPlayer to behave closer to AlsaPlayer: after 5 seconds of no chunks, the AudioQueue is destroyed. On a new chunk, the AudioQueue is re-created.

This commit is contained in:
Tommy Goode 2017-02-25 18:52:21 -06:00
parent d5e2446475
commit 1ae573a75f
2 changed files with 42 additions and 8 deletions

View file

@ -61,11 +61,18 @@ void CoreAudioPlayer::playerCallback(AudioQueueRef queue, AudioQueueBufferRef bu
char *buffer = (char*)bufferRef->mAudioData;
if (!pubStream_->getPlayerChunk(buffer, delay, frames_))
{
if (chronos::getTickCount() - lastChunkTick > 5000)
{
logO << "No chunk received for 5000ms. Closing Audio Queue.\n";
uninitAudioQueue(queue);
return;
}
// logO << "Failed to get chunk. Playing silence.\n";
memset(buffer, 0, buff_size_);
}
else
{
lastChunkTick = chronos::getTickCount();
adjustVolume(buffer, frames_);
}
@ -74,14 +81,32 @@ void CoreAudioPlayer::playerCallback(AudioQueueRef queue, AudioQueueBufferRef bu
if (!active_)
{
AudioQueueStop(queue, false);
AudioQueueDispose(queue, false);
CFRunLoopStop(CFRunLoopGetCurrent());
uninitAudioQueue(queue);
}
}
void CoreAudioPlayer::worker()
{
while (active_)
{
if (pubStream_->waitForChunk(100))
{
try
{
initAudioQueue();
}
catch (const std::exception& e)
{
logE << "Exception in worker: " << e.what() << "\n";
chronos::sleep(100);
}
}
chronos::sleep(100);
}
}
void CoreAudioPlayer::initAudioQueue()
{
const SampleFormat& sampleFormat = pubStream_->getFormat();
@ -126,4 +151,10 @@ void CoreAudioPlayer::worker()
CFRunLoopRun();
}
void CoreAudioPlayer::uninitAudioQueue(AudioQueueRef queue)
{
AudioQueueStop(queue, false);
AudioQueueDispose(queue, false);
pubStream_->clearChunks();
CFRunLoopStop(CFRunLoopGetCurrent());
}

View file

@ -44,12 +44,15 @@ public:
protected:
virtual void worker();
void initAudioQueue();
void uninitAudioQueue(AudioQueueRef queue);
AudioQueueTimelineRef timeLine_;
size_t ms_;
size_t frames_;
size_t buff_size_;
std::shared_ptr<Stream> pubStream_;
long lastChunkTick;
};