Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions corelink/core_link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,11 @@ bool CoreLink::states_supported() const {
bool CoreLink::replays_supported() const {
return states_supported() && link_has_replays(identity_.protocol_minor);
}
bool CoreLink::request_recording(bool record, const fs::path& path) {
bool CoreLink::request_recording(bool record, const fs::path& path, bool diagnostics) {
if (!replays_supported() || !can_grant()) return false;
ReplayControlMsg m{}; m.h.type=Msg::ReplayControl; m.record=record ? 1 : 0;
ReplayControlMsg m{};
m.h.type = Msg::ReplayControl;
m.record = record ? (diagnostics ? 2 : 1) : 0;
const auto p=path_utf8(path);
if (p.size()>=sizeof m.path) return false;
std::memcpy(m.path,p.c_str(),p.size()+1);
Expand Down
2 changes: 1 addition & 1 deletion corelink/core_link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class CoreLink {
// answers with a failure naming that.
std::optional<StateResult> take_state_result();
bool replays_supported() const;
bool request_recording(bool record, const fs::path& path = {});
bool request_recording(bool record, const fs::path& path = {}, bool diagnostics = false);
bool replay_pending() const { return replay_pending_; }
const ReplayStatusMsg& replay_status() const { return replay_status_; }

Expand Down
4 changes: 3 additions & 1 deletion corelink/link_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ struct MsgHeader {
enum class ReplayState : std::uint32_t { Idle, Recording, Saved, Playing, TakenOver, Failed, Complete, Stopped };
struct ReplayControlMsg {
MsgHeader h;
std::uint32_t record; // 1: start at a restored anchor, 0: stop recording
// 0: stop, 1: start full recording, 2: start a diagnostics recording.
// Use 2 only when the runner advertises safe_interactive_recording.
std::uint32_t record;
char path[4096]; // exclusive destination, never overwrite
};
struct ReplayStatusMsg {
Expand Down
9 changes: 6 additions & 3 deletions corelink/link_test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ int main(int argc, char** argv) {
std::uint64_t at;
bool save;
std::string path;
bool diagnostics = false;
};
std::vector<StateOp> state_ops;
std::vector<StateOp> record_ops;
Expand Down Expand Up @@ -203,10 +204,11 @@ int main(int argc, char** argv) {
if (comma == std::string::npos) break;
start = comma + 1;
}
} else if (a == "--record-start-at") {
} else if (a == "--record-start-at" || a == "--diagnostic-record-start-at") {
const auto v=val(); const auto colon=v.find(':');
if (colon==std::string::npos) die("--record-start-at K:PATH");
record_ops.push_back({std::strtoull(v.substr(0,colon).c_str(),nullptr,10),true,v.substr(colon+1)});
record_ops.push_back({std::strtoull(v.substr(0,colon).c_str(),nullptr,10),true,
v.substr(colon+1),a=="--diagnostic-record-start-at"});
} else if (a == "--record-stop-at") {
record_ops.push_back({std::strtoull(val().c_str(),nullptr,10),false,{}});
} else if (a == "--state-save-at") {
Expand Down Expand Up @@ -340,7 +342,8 @@ int main(int argc, char** argv) {
if (clean_route_end) break;
for (const StateOp& op : record_ops) {
if (op.at!=k || !ok) continue;
if (!link.request_recording(op.save,utf8_path(op.path))) die("replay request refused");
if (!link.request_recording(op.save,utf8_path(op.path),op.diagnostics))
die("replay request refused");
while (link.replay_pending() && link.state()==LinkState::Ready) link.pump(100);
const auto& status=link.replay_status();
std::printf("replay: %s at %llu state %u fields %llu %s\n",op.save ? "start" : "stop",
Expand Down
1 change: 1 addition & 0 deletions corelink/runner_probe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ bool probe_runner(const fs::path& runner, RunnerVersion& out, std::string* error
v.version = fields["version"];
v.commit = fields["commit"];
v.gl = fields["gl"] == "1";
v.safe_interactive_recording = fields["safe_interactive_recording"] == "1";
if (fields.count("game_package") && !num("game_package", v.game_package)) {
return fail("--version printed an unreadable game_package line");
}
Expand Down
5 changes: 5 additions & 0 deletions corelink/runner_probe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <filesystem>
#include <string>

#define RETRO_CORELINK_HAS_SAFE_INTERACTIVE_RECORDING 1

namespace retro::corelink {

struct RunnerVersion {
Expand All @@ -19,6 +21,9 @@ struct RunnerVersion {
std::uint32_t link_major = 0, link_minor = 0;
std::uint32_t abi_major = 0, draft_revision = 0;
bool gl = false;
// Interactive replays cannot interrupt play on a write failure and seal
// before a player state operation. Old runners omit this capability.
bool safe_interactive_recording = false;
// 1 when the runner takes --package (GAME_PACKAGE cores); 0 for a runner
// from before it did, which prints no game_package line.
std::uint32_t game_package = 0;
Expand Down
11 changes: 10 additions & 1 deletion docs/BOOT_ROUTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ reports divergence instead of weakening checkpoints. Anchors are limited to
must wait for the response before granting another frame and display recording
status only after confirmation. Controls are unavailable with older peers,
netplay, accessories, or an active playback. Existing-file destinations refuse.

A runner advertising `safe_interactive_recording 1` also accepts automatic
gameplay diagnostics. These recordings keep every input and clock callback
with a checkpoint at the first field and every 120 fields. They carry a
`D 1` marker and omit picture and audio digests so capture works alongside
normal presentation. Saving or loading a state seals the active interactive
recording first. Capture failures are reported to the launcher and play
continues.
The frontend can list sealed `.route` files and start playback in a fresh runner
using `RETRO_PLAY_ROUTE`. `RETRO_ROUTE_VERDICT` selects the result path.
The hub supplies presentation and pacing. Normal completion ends the runner;
Expand Down Expand Up @@ -96,7 +104,8 @@ configuration, not every game or future core version.

## Interrupted recording

The active file is `<route>.partial`. The writer flushes each complete field.
The active file is `<route>.partial`. Full recordings flush each complete
field. Automatic diagnostics flush at checkpoints.
A normal link Quit or headless Ctrl+C seals completed fields in the requested
route file. A fault leaves the partial file. An unsealed partial file refuses
normal playback.
Expand Down
19 changes: 11 additions & 8 deletions runner/host_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ std::uint32_t HostSession::h_video_acquire_cpu(void* ctx, std::uint32_t width,
(format == RCORE_PIXEL_RGBA8_X2 && width > UINT32_MAX / 2) || width > UINT32_MAX / 4 ||
std::size_t(height) > SIZE_MAX / (std::size_t(width) * 4)) return 0;
auto* s = self(ctx);
const bool observes_av = s->route_ && s->route_->observes_av();
// Replay observers retain the canonical full-width byte oracle.
if (format == RCORE_PIXEL_RGBA8_X2 && s->route_) return 0;
if (format == RCORE_PIXEL_RGBA8_X2 && observes_av) return 0;
// Route observers request pictures even when the host does not display.
if (!s->video_requested_ && !s->route_) return 0;
if (!s->video_requested_ && !observes_av) return 0;
// The producer always gets our full local object; an older core lends
// only the rev13 prefix. Never write our appended flags into its stack.
const auto supplied = std::min<std::size_t>(out->struct_size, sizeof *out);
Expand Down Expand Up @@ -275,25 +276,27 @@ void HostSession::h_report(void* ctx, const rcore_event* e) {
}

void HostSession::h_video(void* ctx, const rcore_frame* f) {
if (f && self(ctx)->route_) self(ctx)->route_->video(*f);
auto* s = self(ctx);
const bool observes_av = s->route_ && s->route_->observes_av();
if (f && observes_av) s->route_->video(*f);
if (f) {
auto* s = self(ctx);
s->sink_.video_progress(f->frame_number,
f->struct_size >= offsetof(rcore_frame, game_frame) + sizeof f->game_frame ? f->game_frame : 0);
if (s->video_requested_ || s->route_) s->sink_.frame(*f);
if (s->video_requested_ || observes_av) s->sink_.frame(*f);
}
}

std::uint32_t HostSession::h_video_requested(void* ctx, std::uint64_t frame, std::uint64_t picture) {
auto* s = self(ctx);
s->sink_.video_progress(frame, picture);
// Routes/recordings observe per-field pictures; never suppress those.
return s->video_requested_ || s->route_ != nullptr;
return s->video_requested_ || (s->route_ && s->route_->observes_av());
}

void HostSession::h_audio(void* ctx, const std::int16_t* s, std::uint32_t n) {
if (self(ctx)->route_) self(ctx)->route_->audio(s, n);
self(ctx)->sink_.audio(s, n);
auto* host = self(ctx);
if (host->route_ && host->route_->observes_av()) host->route_->audio(s, n);
host->sink_.audio(s, n);
}

void HostSession::h_rate(void* ctx, std::uint32_t hz) { self(ctx)->sink_.audio_rate(hz); }
Expand Down
54 changes: 40 additions & 14 deletions runner/route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern char** environ;

namespace retro::runner {
namespace {
constexpr std::uint64_t kDiagnosticCheckpointFields = 120;
constexpr std::size_t limit = 256 * 1024 * 1024;
std::string hex(const void* data, std::size_t n) {
if (!n) return "-";
Expand Down Expand Up @@ -132,11 +133,18 @@ bool Route::open(const std::string& rom, const std::string& package_sha,
session_.set_presentation_size(boot_.width,boot_.height);
} else {
partial_ = args_.record; partial_ += ".partial";
if (fs::exists(args_.record) || fs::exists(partial_)) return fail("route or partial file already exists");
if (!args_.record.parent_path().empty()) fs::create_directories(args_.record.parent_path());
std::error_code ec;
const bool target_exists = fs::exists(args_.record, ec);
if (ec) return fail("cannot inspect route destination: " + ec.message());
const bool partial_exists = fs::exists(partial_, ec);
if (ec) return fail("cannot inspect partial route: " + ec.message());
if (target_exists || partial_exists) return fail("route or partial file already exists");
if (!args_.record.parent_path().empty()) fs::create_directories(args_.record.parent_path(), ec);
if (ec) return fail("cannot create route directory: " + ec.message());
file_.open(partial_, std::ios::binary | std::ios::out);
if (!file_) return fail("cannot create route partial file");
file_ << "RCORE_ROUTE 1\n";
if (args_.diagnostics) file_ << "D 1\n";
for (const auto& [k,v] : identity_) file_ << "I " << hex(k) << ' ' << hex(v) << '\n';
}
return true;
Expand All @@ -155,13 +163,17 @@ bool Route::capture_anchor() {
if (api.serialize(anchor_.data(),size) != RCORE_OK)
return fail("the core refused to save the replay anchor");
const auto before=state_digest();
// Record and playback both follow the core's restore path.
if (api.unserialize(anchor_.data(),size) != RCORE_OK)
return fail("the core refused to restore the replay anchor");
anchor_state_=state_digest();
if (before!=anchor_state_) {
expected_state_=before; actual_state_=anchor_state_;
return fail("the core changed state while restoring the replay anchor",true);
anchor_state_ = before;
// A developer recording checks the restore path immediately. Automatic
// diagnostics must leave the live game alone and verify on playback.
if (!args_.diagnostics) {
if (api.unserialize(anchor_.data(),size) != RCORE_OK)
return fail("the core refused to restore the replay anchor");
anchor_state_ = state_digest();
if (before != anchor_state_) {
expected_state_ = before; actual_state_ = anchor_state_;
return fail("the core changed state while restoring the replay anchor",true);
}
}
file_ << "A " << hex(anchor_state_) << ' ' << hex(anchor_.data(),anchor_.size()) << '\n';
return bool(file_);
Expand Down Expand Up @@ -267,9 +279,18 @@ std::string Route::state_digest() const {
}
bool Route::end() {
if (!calls_complete()) return false;
current_.state=state_digest();
current_.image = picture_.empty() ? "-" : std::to_string(picture_width_) + ":" + std::to_string(picture_height_) + ":" + sha256_hex(picture_.data(),picture_.size());
current_.audio = sha256_hex(pcm_.data(),pcm_.size());
const bool checkpoint = !args_.diagnostics || next_ == 0 ||
(next_ + 1) % kDiagnosticCheckpointFields == 0;
current_.state = checkpoint ? state_digest() : "*";
if (observes_av()) {
current_.image = picture_.empty() ? "-" :
std::to_string(picture_width_) + ":" + std::to_string(picture_height_) + ":" +
sha256_hex(picture_.data(), picture_.size());
current_.audio = sha256_hex(pcm_.data(), pcm_.size());
} else {
current_.image = "*";
current_.audio = "*";
}
const auto mark = args_.marks.find(next_+1);
current_.mark = playback() ? frames_[next_].mark : mark == args_.marks.end() ? "" : mark->second;
if (playback()) {
Expand Down Expand Up @@ -300,7 +321,9 @@ bool Route::write_field(const Field& f, bool boot) {
file_ << '\n';
}
for (auto t : f.clocks) file_ << "T " << t << '\n';
file_.flush();
// Strict routes retain every field on disk. Background recording batches
// unobserved fields with the adjacent checkpoint instead of flushing each.
if (!args_.diagnostics || boot || f.state != "*") file_.flush();
if(file_.tellp()>std::streamoff(limit-1024))return fail("route exceeds 256 MiB limit");
return file_ ? true : fail("cannot write route");
}
Expand All @@ -324,7 +347,10 @@ bool Route::read() {
bool boot_seen = false;
while (std::getline(lines,line)) {
std::istringstream row(line); const auto type = word(row);
if (type == "I" && !boot_seen && saves_.empty()) {
if (type == "D" && !args_.diagnostics && !boot_seen && recorded_identity_.empty()) {
if (word(row) != "1") throw std::runtime_error("unsupported diagnostics recording");
args_.diagnostics = true;
} else if (type == "I" && !boot_seen && saves_.empty()) {
const auto key=unhex(word(row)), value=unhex(word(row));
if (!recorded_identity_.emplace(key,value).second) throw std::runtime_error("duplicate identity");
} else if (type == "A" && !boot_seen && saves_.empty() && anchor_.empty()) {
Expand Down
2 changes: 2 additions & 0 deletions runner/route.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct RouteArgs {
fs::path record, play, verdict, runner, runner_reference;
bool allow_core_change = false;
bool takeover = false;
bool diagnostics = false;
std::map<std::uint64_t, std::string> marks;
bool active() const { return !record.empty() || !play.empty(); }
};
Expand All @@ -30,6 +31,7 @@ class Route {
bool finish(bool success, bool stopped = false, bool taken_over = false);
static bool live_input(const rcore_pad pads[RCORE_MAX_SEATS]);
bool playback() const { return !args_.play.empty(); }
bool observes_av() const { return !args_.diagnostics; }
bool done() const { return playback() && next_ >= frames_.size(); }
std::uint64_t length() const { return frames_.size(); }
std::uint64_t field() const { return next_; }
Expand Down
Loading
Loading