android: defer stopping audio until after current frame is rendered

In android, stop requests are not run in the UI thread so in
single-threaded mode, they must be deferred until after the current
frame is rendered to avoid deadlocking on audio.
This commit is contained in:
Flyinghead 2023-01-07 11:51:32 +01:00
parent bf3f927a99
commit d0bb0aea97
2 changed files with 14 additions and 4 deletions

View File

@ -671,14 +671,15 @@ void Emulator::stop() {
} catch (const FlycastException& e) {
WARN_LOG(COMMON, "%s", e.what());
}
SaveRomFiles();
EventManager::event(Event::Pause);
}
else
{
// FIXME Android: need to terminate render thread before
TermAudio();
// defer stopping audio until after the current frame is finished
// normally only useful on android due to multithreading
stopRequested = true;
}
SaveRomFiles();
EventManager::event(Event::Pause);
}
// Called on the emulator thread for soft reset
@ -869,6 +870,7 @@ void Emulator::start()
}
else
{
stopRequested = false;
InitAudio();
}
@ -901,6 +903,13 @@ bool Emulator::render()
if (state != Running)
return false;
run();
if (stopRequested)
{
stopRequested = false;
TermAudio();
SaveRomFiles();
EventManager::event(Event::Pause);
}
// TODO if stopping due to a user request, no frame has been rendered
return !renderTimeout;
}

View File

@ -177,6 +177,7 @@ private:
bool renderTimeout = false;
u32 stepRangeFrom = 0;
u32 stepRangeTo = 0;
bool stopRequested = false;
};
extern Emulator emu;