all repos — mgba @ 2a80ddf8d0e1eaee3437c46547e6255f039987d0

mGBA Game Boy Advance Emulator

Qt: Fix hang if audio sync is enabled and audio fails to initialize
Jeffrey Pfau jeffrey@endrift.com
Tue, 09 Feb 2016 02:01:21 -0800
commit

2a80ddf8d0e1eaee3437c46547e6255f039987d0

parent

1fdf413ee9f31e797618d0968f709d8db0674b6b

M CHANGESCHANGES

@@ -2,6 +2,7 @@ 0.5.0: (Future)

Bugfixes: - VFS: Fix reading 7z archives without rewinding first - Qt: Fix sending gameStopped twice + - Qt: Fix hang if audio sync is enabled and audio fails to initialize Misc: - GBA: Slightly optimize GBAProcessEvents
M src/platform/qt/AudioProcessor.hsrc/platform/qt/AudioProcessor.h

@@ -34,7 +34,7 @@ int getBufferSamples() const { return m_samples; }

virtual unsigned sampleRate() const = 0; public slots: - virtual void start() = 0; + virtual bool start() = 0; virtual void pause() = 0; virtual void setBufferSamples(int samples) = 0;
M src/platform/qt/AudioProcessorQt.cppsrc/platform/qt/AudioProcessorQt.cpp

@@ -34,10 +34,10 @@ }

} } -void AudioProcessorQt::start() { +bool AudioProcessorQt::start() { if (!input()) { LOG(WARN) << tr("Can't start an audio processor without input"); - return; + return false; } if (!m_device) {

@@ -62,6 +62,7 @@ m_device->setFormat(m_audioOutput->format());

m_audioOutput->setBufferSize(input()->audioBuffers * 4); m_audioOutput->start(m_device); + return m_audioOutput->state() == QAudio::ActiveState; } void AudioProcessorQt::pause() {
M src/platform/qt/AudioProcessorQt.hsrc/platform/qt/AudioProcessorQt.h

@@ -23,7 +23,7 @@ virtual void setInput(GBAThread* input) override;

virtual unsigned sampleRate() const override; public slots: - virtual void start() override; + virtual bool start() override; virtual void pause() override; virtual void setBufferSamples(int samples) override;
M src/platform/qt/AudioProcessorSDL.cppsrc/platform/qt/AudioProcessorSDL.cpp

@@ -23,19 +23,20 @@ AudioProcessorSDL::~AudioProcessorSDL() {

GBASDLDeinitAudio(&m_audio); } -void AudioProcessorSDL::start() { +bool AudioProcessorSDL::start() { if (!input()) { LOG(WARN) << tr("Can't start an audio processor without input"); - return; + return false; } if (m_audio.thread) { GBASDLResumeAudio(&m_audio); + return true; } else { if (!m_audio.samples) { m_audio.samples = input()->audioBuffers; } - GBASDLInitAudio(&m_audio, input()); + return GBASDLInitAudio(&m_audio, input()); } }
M src/platform/qt/AudioProcessorSDL.hsrc/platform/qt/AudioProcessorSDL.h

@@ -25,7 +25,7 @@

virtual unsigned sampleRate() const override; public slots: - virtual void start() override; + virtual bool start() override; virtual void pause() override; virtual void setBufferSamples(int samples) override;
M src/platform/qt/GameController.cppsrc/platform/qt/GameController.cpp

@@ -204,9 +204,7 @@

m_audioThread->setObjectName("Audio Thread"); m_audioThread->start(QThread::TimeCriticalPriority); m_audioProcessor->moveToThread(m_audioThread); - connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start())); connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause())); - connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start())); connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(pollEvents())); connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(updateAutofire())); }

@@ -341,6 +339,17 @@

if (!GBAThreadStart(&m_threadContext)) { m_gameOpen = false; emit gameFailed(); + } else if (m_audioProcessor) { + bool started = false; + QMetaObject::invokeMethod(m_audioProcessor, "start", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, started)); + if (!started) { + LOG(ERROR) << tr("Failed to start audio processor"); + // Don't freeze! + m_audioSync = false; + m_videoSync = true; + m_threadContext.sync.audioWait = false; + m_threadContext.sync.videoFrameWait = true; + } } }

@@ -900,12 +909,19 @@ if (sampleRate) {

m_audioProcessor->requestSampleRate(sampleRate); } m_audioProcessor->moveToThread(m_audioThread); - connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start())); connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause())); - connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start())); if (isLoaded()) { m_audioProcessor->setInput(&m_threadContext); - QMetaObject::invokeMethod(m_audioProcessor, "start"); + bool started = false; + QMetaObject::invokeMethod(m_audioProcessor, "start", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, started)); + if (!started) { + LOG(ERROR) << tr("Failed to start audio processor"); + // Don't freeze! + m_audioSync = false; + m_videoSync = true; + m_threadContext.sync.audioWait = false; + m_threadContext.sync.videoFrameWait = true; + } } }