citra_qt: Fix potential indeterminstism caused by starting record/playback

Previously the movie was started *after* core starts running, causing potential indeterminism.

Some desyncs are still not fixed; they may be caused by core timing. More investigation is required.
This commit is contained in:
zhupengfei 2020-08-27 22:27:29 +08:00
parent e60e20666e
commit f8eb9a541d
No known key found for this signature in database
GPG key ID: DD129E108BD09378
4 changed files with 57 additions and 39 deletions

View file

@ -1018,6 +1018,9 @@ void GMainWindow::BootGame(const QString& filename) {
if (movie_record_on_start) { if (movie_record_on_start) {
Core::Movie::GetInstance().PrepareForRecording(); Core::Movie::GetInstance().PrepareForRecording();
} }
if (movie_playback_on_start) {
Core::Movie::GetInstance().PrepareForPlayback(movie_playback_path.toStdString());
}
// Save configurations // Save configurations
UpdateUISettings(); UpdateUISettings();
@ -1027,6 +1030,42 @@ void GMainWindow::BootGame(const QString& filename) {
if (!LoadROM(filename)) if (!LoadROM(filename))
return; return;
// Set everything up
if (movie_record_on_start) {
Core::Movie::GetInstance().StartRecording(movie_record_path.toStdString(),
movie_record_author.toStdString());
movie_record_on_start = false;
movie_record_path.clear();
movie_record_author.clear();
}
if (movie_playback_on_start) {
Core::Movie::GetInstance().StartPlayback(movie_playback_path.toStdString());
movie_playback_on_start = false;
movie_playback_path.clear();
}
if (ui->action_Enable_Frame_Advancing->isChecked()) {
ui->action_Advance_Frame->setEnabled(true);
Core::System::GetInstance().frame_limiter.SetFrameAdvancing(true);
} else {
ui->action_Advance_Frame->setEnabled(false);
}
if (video_dumping_on_start) {
Layout::FramebufferLayout layout{
Layout::FrameLayoutFromResolutionScale(VideoCore::GetResolutionScaleFactor())};
if (!Core::System::GetInstance().VideoDumper().StartDumping(
video_dumping_path.toStdString(), layout)) {
QMessageBox::critical(
this, tr("Citra"),
tr("Could not start video dumping.<br>Refer to the log for details."));
ui->action_Dump_Video->setChecked(false);
}
video_dumping_on_start = false;
video_dumping_path.clear();
}
// Create and start the emulation thread // Create and start the emulation thread
emu_thread = std::make_unique<EmuThread>(*render_window); emu_thread = std::make_unique<EmuThread>(*render_window);
emit EmulationStarting(emu_thread.get()); emit EmulationStarting(emu_thread.get());
@ -1076,35 +1115,6 @@ void GMainWindow::BootGame(const QString& filename) {
ShowFullscreen(); ShowFullscreen();
} }
if (movie_record_on_start) {
Core::Movie::GetInstance().StartRecording(movie_record_path.toStdString(),
movie_record_author.toStdString());
movie_record_on_start = false;
movie_record_path.clear();
movie_record_author.clear();
}
if (ui->action_Enable_Frame_Advancing->isChecked()) {
ui->action_Advance_Frame->setEnabled(true);
Core::System::GetInstance().frame_limiter.SetFrameAdvancing(true);
} else {
ui->action_Advance_Frame->setEnabled(false);
}
if (video_dumping_on_start) {
Layout::FramebufferLayout layout{
Layout::FrameLayoutFromResolutionScale(VideoCore::GetResolutionScaleFactor())};
if (!Core::System::GetInstance().VideoDumper().StartDumping(
video_dumping_path.toStdString(), layout)) {
QMessageBox::critical(
this, tr("Citra"),
tr("Could not start video dumping.<br>Refer to the log for details."));
ui->action_Dump_Video->setChecked(false);
}
video_dumping_on_start = false;
video_dumping_path.clear();
}
OnStartGame(); OnStartGame();
} }
@ -1128,7 +1138,6 @@ void GMainWindow::ShutdownGame() {
AllowOSSleep(); AllowOSSleep();
discord_rpc->Pause(); discord_rpc->Pause();
OnCloseMovie(true);
emu_thread->RequestStop(); emu_thread->RequestStop();
// Release emu threads from any breakpoints // Release emu threads from any breakpoints
@ -1147,6 +1156,8 @@ void GMainWindow::ShutdownGame() {
emu_thread->wait(); emu_thread->wait();
emu_thread = nullptr; emu_thread = nullptr;
OnCloseMovie();
discord_rpc->Update(); discord_rpc->Update();
Camera::QtMultimediaCameraHandler::ReleaseHandlers(); Camera::QtMultimediaCameraHandler::ReleaseHandlers();
@ -1875,22 +1886,21 @@ void GMainWindow::OnPlayMovie() {
return; return;
} }
const auto movie_path = dialog.GetMoviePath().toStdString(); movie_playback_on_start = true;
Core::Movie::GetInstance().PrepareForPlayback(movie_path); movie_playback_path = dialog.GetMoviePath();
BootGame(dialog.GetGamePath()); BootGame(dialog.GetGamePath());
Core::Movie::GetInstance().StartPlayback(movie_path);
ui->action_Close_Movie->setEnabled(true); ui->action_Close_Movie->setEnabled(true);
} }
void GMainWindow::OnCloseMovie(bool shutting_down) { void GMainWindow::OnCloseMovie() {
if (movie_record_on_start) { if (movie_record_on_start) {
QMessageBox::information(this, tr("Record Movie"), tr("Movie recording cancelled.")); QMessageBox::information(this, tr("Record Movie"), tr("Movie recording cancelled."));
movie_record_on_start = false; movie_record_on_start = false;
movie_record_path.clear(); movie_record_path.clear();
movie_record_author.clear(); movie_record_author.clear();
} else { } else {
const bool was_running = !shutting_down && emu_thread && emu_thread->IsRunning(); const bool was_running = emu_thread && emu_thread->IsRunning();
if (was_running) { if (was_running) {
OnPauseGame(); OnPauseGame();
} }

View file

@ -208,7 +208,7 @@ private slots:
void OnCreateGraphicsSurfaceViewer(); void OnCreateGraphicsSurfaceViewer();
void OnRecordMovie(); void OnRecordMovie();
void OnPlayMovie(); void OnPlayMovie();
void OnCloseMovie(bool shutting_down = false); void OnCloseMovie();
void OnCaptureScreenshot(); void OnCaptureScreenshot();
#ifdef ENABLE_FFMPEG_VIDEO_DUMPER #ifdef ENABLE_FFMPEG_VIDEO_DUMPER
void OnStartVideoDumping(); void OnStartVideoDumping();
@ -269,6 +269,9 @@ private:
QString movie_record_path; QString movie_record_path;
QString movie_record_author; QString movie_record_author;
bool movie_playback_on_start = false;
QString movie_playback_path;
// Video dumping // Video dumping
bool video_dumping_on_start = false; bool video_dumping_on_start = false;
QString video_dumping_path; QString video_dumping_path;

View file

@ -491,6 +491,7 @@ void Movie::SaveMovie() {
CTMHeader header = {}; CTMHeader header = {};
header.filetype = header_magic_bytes; header.filetype = header_magic_bytes;
header.program_id = program_id;
header.clock_init_time = init_time; header.clock_init_time = init_time;
header.id = id; header.id = id;
@ -500,8 +501,6 @@ void Movie::SaveMovie() {
header.rerecord_count = rerecord_count; header.rerecord_count = rerecord_count;
header.input_count = GetInputCount(recorded_input); header.input_count = GetInputCount(recorded_input);
Core::System::GetInstance().GetAppLoader().ReadProgramId(header.program_id);
std::string rev_bytes; std::string rev_bytes;
CryptoPP::StringSource(Common::g_scm_rev, true, CryptoPP::StringSource(Common::g_scm_rev, true,
new CryptoPP::HexDecoder(new CryptoPP::StringSink(rev_bytes))); new CryptoPP::HexDecoder(new CryptoPP::StringSink(rev_bytes)));
@ -562,6 +561,10 @@ void Movie::StartRecording(const std::string& movie_file, const std::string& aut
CryptoPP::AutoSeededRandomPool rng; CryptoPP::AutoSeededRandomPool rng;
rng.GenerateBlock(reinterpret_cast<CryptoPP::byte*>(&id), sizeof(id)); rng.GenerateBlock(reinterpret_cast<CryptoPP::byte*>(&id), sizeof(id));
// Get program ID
program_id = 0;
Core::System::GetInstance().GetAppLoader().ReadProgramId(program_id);
LOG_INFO(Movie, "Enabling Movie recording, ID: {:016X}", id); LOG_INFO(Movie, "Enabling Movie recording, ID: {:016X}", id);
} }

View file

@ -159,6 +159,8 @@ private:
std::string record_movie_file; std::string record_movie_file;
std::string record_movie_author; std::string record_movie_author;
u64 init_time; // Clock init time override for RNG consistency
std::vector<u8> recorded_input; std::vector<u8> recorded_input;
std::size_t current_byte = 0; std::size_t current_byte = 0;
u64 current_input = 0; u64 current_input = 0;
@ -166,7 +168,7 @@ private:
u64 total_input = 0; u64 total_input = 0;
u64 id = 0; // ID of the current movie loaded u64 id = 0; // ID of the current movie loaded
u64 init_time; u64 program_id = 0;
u32 rerecord_count = 1; u32 rerecord_count = 1;
bool read_only = true; bool read_only = true;