Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Show Steam download speed and time remaining

Leonardo Lobatof4fbd0cfd4b3
diff --git a/cmake/SteamBackend.cmake b/cmake/SteamBackend.cmake
index 4d0ebee..06a28ec 100644
--- a/cmake/SteamBackend.cmake
+++ b/cmake/SteamBackend.cmake
@@ -235,6 +235,9 @@ function(playden_register_steam_backend_tests)
target_link_libraries(${test_name}_test PRIVATE playden_steam_cm)
add_test(NAME ${test_name} COMMAND ${test_name}_test "${CMAKE_SOURCE_DIR}/fixtures/steam/cm")
endforeach()
+ add_executable(download_rate_test "${CMAKE_SOURCE_DIR}/tests/download_rate_test.cpp")
+ target_link_libraries(download_rate_test PRIVATE playden_steam_core)
+ add_test(NAME download_rate COMMAND download_rate_test)
add_executable(install_service_test "${CMAKE_SOURCE_DIR}/tests/install_service_test.cpp")
target_link_libraries(install_service_test PRIVATE playden_steam_core)
add_test(NAME install_service COMMAND install_service_test)
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cpp
index a5627de..aa702f8 100644
--- a/src/homebrew/controller.cpp
+++ b/src/homebrew/controller.cpp
@@ -1124,7 +1124,8 @@ void Controller::TickInstalls() {
case steam::InstallJobState::kPaused: state = JobState::Paused; break;
case steam::InstallJobState::kFailed: state = JobState::Failed; break;
}
- jobs.push_back({job.id, state, job.done_bytes, job.total_bytes, job.failure, job.volume_id});
+ jobs.push_back({job.id, state, job.done_bytes, job.total_bytes, job.failure, job.volume_id,
+ job.bytes_per_second, job.eta_seconds});
}
std::vector<LiveInstalledGame> installed;
for (const auto& game : snapshot.installed) installed.push_back({game.id, game.install_bytes, game.volume_id, game.available});
diff --git a/src/homebrew/simulation.cpp b/src/homebrew/simulation.cpp
index 3d7fbc3..8124725 100644
--- a/src/homebrew/simulation.cpp
+++ b/src/homebrew/simulation.cpp
@@ -29,6 +29,18 @@
#include <type_traits>
namespace playden::homebrew {
+std::string FormatDownloadRate(std::optional<std::uint64_t> rate, std::optional<int> eta) {
+ if (!rate) return "—";
+ char text[64];
+ std::snprintf(text, sizeof(text), "%.1f MB/s", static_cast<double>(*rate) / (1024.0 * 1024.0));
+ std::string result(text);
+ result += " · ";
+ if (!eta || *eta < 0) return result + "—";
+ if (*eta < 60) return result + "~" + std::to_string(*eta) + " sec left";
+ if (*eta < 3600) return result + "~" + std::to_string((*eta + 59) / 60) + " min left";
+ return result + "~" + std::to_string((static_cast<std::int64_t>(*eta) + 3599) / 3600) + " hr left";
+}
+
namespace {
constexpr std::uint64_t kTransferPerTick = 67'108'864;
@@ -978,6 +990,10 @@ void Simulation::UpdateInstalls(const std::vector<LiveInstallJob>& jobs, const s
job.total_bytes = live_job.total_bytes;
job.failure = live_job.failure;
job.volume_id = live_job.volume_id;
+ if (live_job.state == JobState::Downloading) {
+ job.bytes_per_second = live_job.bytes_per_second;
+ job.eta_seconds = live_job.eta_seconds;
+ }
projected.push_back(std::move(job));
}
std::vector<Installation> installations;
diff --git a/src/homebrew/simulation.hpp b/src/homebrew/simulation.hpp
index 789de2a..c25a39b 100644
--- a/src/homebrew/simulation.hpp
+++ b/src/homebrew/simulation.hpp
@@ -42,6 +42,7 @@ enum class MaintenanceKind { ApplySettings, ClearShaderCache, Repair, BackupSave
// Whether a game's PS5 title runs now, as the console last answered.
enum class TitleActivity { Unknown, Stopped, Running };
// Live install state projected from the real install service.
+std::string FormatDownloadRate(std::optional<std::uint64_t> bytes_per_second, std::optional<int> eta_seconds);
struct LiveInstallJob {
GameId game_id = 0;
JobState state = JobState::Queued;
@@ -49,6 +50,8 @@ struct LiveInstallJob {
std::uint64_t total_bytes = 0;
std::string failure;
std::string volume_id;
+ std::optional<std::uint64_t> bytes_per_second;
+ std::optional<int> eta_seconds;
};
struct LiveInstalledGame {
GameId game_id = 0;
diff --git a/src/homebrew/view.cpp b/src/homebrew/view.cpp
index d9d1ce3..728eaec 100644
--- a/src/homebrew/view.cpp
+++ b/src/homebrew/view.cpp
@@ -207,16 +207,6 @@ std::string short_size(std::uint64_t bytes)
return written > 0 ? std::string(value.data(), static_cast<std::size_t>(written)) : std::string("Unknown");
}
-std::string transfer_rate(std::optional<std::uint64_t> bytes_per_second)
-{
- if (!bytes_per_second)
- return "—";
- std::array<char, 32> value{};
- const double mib = static_cast<double>(*bytes_per_second) / (1024.0 * 1024.0);
- const int written = std::snprintf(value.data(), value.size(), "%.1f MB/s", mib);
- return written > 0 ? std::string(value.data(), static_cast<std::size_t>(written)) : "—";
-}
-
std::string join_words(const std::vector<std::string> &words)
{
if (words.empty())
@@ -1807,7 +1797,8 @@ class Render::Impl
const std::string bytes = byte_count(primary->completed_bytes) + " / " +
byte_count(primary->total_bytes);
ui::text(scene_, fonts_.regular, bytes, 282.0F, 352.0F, 22.0F, kText);
- ui::text(scene_, fonts_.regular, transfer_rate(primary->bytes_per_second), 1038.0F,
+ ui::text(scene_, fonts_.regular, FormatDownloadRate(primary->state == JobState::Downloading ? primary->bytes_per_second : std::nullopt,
+ primary->state == JobState::Downloading ? primary->eta_seconds : std::nullopt), 1038.0F,
352.0F, 22.0F, kSecondary, gfx::Align::right);
const bool card_focused = route_focus_ && state.navigation.focus_key == "job-" + primary->id;
focus_ring(scene_, active_card, control_focus("job-" + primary->id, card_focused));
diff --git a/src/steam/download_rate.h b/src/steam/download_rate.h
new file mode 100644
index 0000000..dce4eaf
--- /dev/null
+++ b/src/steam/download_rate.h
@@ -0,0 +1,53 @@
+#pragma once
+#include <algorithm>
+#include <array>
+#include <cstdint>
+#include <limits>
+#include <optional>
+namespace playden::steam {
+struct DownloadEstimate {
+ std::optional<std::uint64_t> bytes_per_second;
+ std::optional<int> eta_seconds;
+};
+// Six fixed buckets cover a recent five-second window; all times are steady-clock milliseconds.
+class DownloadRate {
+ public:
+ void Reset() { *this = DownloadRate{}; }
+ void Record(std::uint64_t done, std::int64_t now) {
+ if (now < 0) { Reset(); return; }
+ if (!started_ || now < last_ms_ || done < last_done_) {
+ Reset(); started_ = true; start_ms_ = now; last_ms_ = now; last_done_ = done;
+ return; // A journal's resumed bytes are the baseline, never new traffic.
+ }
+ const auto second = now / 1000;
+ auto& bucket = buckets_[second % buckets_.size()];
+ if (bucket.second != second) bucket = {second, 0};
+ bucket.bytes += done - last_done_;
+ last_ms_ = now; last_done_ = done;
+ }
+ DownloadEstimate Estimate(std::int64_t now, std::uint64_t done, std::uint64_t total) const {
+ if (!started_ || now < last_ms_ || now - start_ms_ < 1000) return {};
+ std::uint64_t bytes = 0;
+ for (const auto& bucket : buckets_)
+ if (bucket.second >= 0 && now >= bucket.second * 1000 && now - bucket.second * 1000 < 5000)
+ bytes += bucket.bytes;
+ const auto elapsed = std::min<std::int64_t>(5000, now - start_ms_);
+ const long double per_second = static_cast<long double>(bytes) * 1000 / elapsed;
+ DownloadEstimate result;
+ result.bytes_per_second = per_second >= std::numeric_limits<std::uint64_t>::max()
+ ? std::numeric_limits<std::uint64_t>::max() : static_cast<std::uint64_t>(per_second);
+ if (total && *result.bytes_per_second) {
+ const auto remaining = total > done ? total - done : 0;
+ const auto seconds = remaining ? (remaining - 1) / *result.bytes_per_second + 1 : 0;
+ result.eta_seconds = static_cast<int>(std::min<std::uint64_t>(seconds, std::numeric_limits<int>::max()));
+ }
+ return result;
+ }
+ private:
+ struct Bucket { std::int64_t second = -1; std::uint64_t bytes = 0; };
+ std::array<Bucket, 6> buckets_{};
+ bool started_ = false;
+ std::int64_t start_ms_ = 0, last_ms_ = 0;
+ std::uint64_t last_done_ = 0;
+};
+}
diff --git a/src/steam/install_service.cpp b/src/steam/install_service.cpp
index ab4f078..47a0835 100644
--- a/src/steam/install_service.cpp
+++ b/src/steam/install_service.cpp
@@ -41,6 +41,10 @@ std::vector<InstallRequest> ParseInstallRequests(const std::string& text) {
return requests;
}
namespace {
+std::int64_t RateNowMs() {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
+ std::chrono::steady_clock::now().time_since_epoch()).count();
+}
constexpr const char* kRecord = ".playden-install.json";
constexpr const char* kJournal = ".playden-download";
@@ -264,6 +268,7 @@ void InstallService::Pause(GameId id) {
if (!job) return;
if (active_ == id) active_cancel_.store(true);
job->view.state = InstallJobState::kPaused;
+ job->rate.Reset(); job->view.bytes_per_second.reset(); job->view.eta_seconds.reset();
job->view.failure.clear();
Publish();
}
@@ -362,6 +367,19 @@ void InstallService::SetDefaultVolume(const std::string& volume_id) {
InstallSnapshot InstallService::Snapshot() {
std::lock_guard<std::mutex> lock(mutex_);
InstallSnapshot snapshot;
+ const auto now = RateNowMs();
+ for (auto& job : jobs_) {
+ DownloadEstimate estimate;
+ if (job.view.state == InstallJobState::kRunning) {
+ if (job.rate_tick == now / 1000) continue;
+ job.rate_tick = now / 1000;
+ estimate = job.rate.Estimate(now, job.view.done_bytes, job.view.total_bytes);
+ }
+ if (job.view.bytes_per_second != estimate.bytes_per_second || job.view.eta_seconds != estimate.eta_seconds) {
+ job.view.bytes_per_second = estimate.bytes_per_second; job.view.eta_seconds = estimate.eta_seconds;
+ ++revision_; // Time-only telemetry changes do not write the install registry.
+ }
+ }
snapshot.revision = revision_;
for (const auto& job : jobs_) snapshot.jobs.push_back(job.view);
snapshot.installed = installed_;
@@ -407,6 +425,8 @@ void InstallService::Run() {
continue;
}
job.view.state = InstallJobState::kRunning;
+ job.rate.Reset(); job.rate_tick = -1;
+ job.view.bytes_per_second.reset(); job.view.eta_seconds.reset();
next = job.view.id;
next_root = RootOn(job.view.volume_id, job.view.id);
active_ = next;
@@ -444,6 +464,10 @@ void InstallService::Run() {
[this, id = *next](std::uint64_t done, std::uint64_t total) {
std::lock_guard<std::mutex> lock(mutex_);
if (auto* job = Find(id)) {
+ if (job->view.state == InstallJobState::kRunning) {
+ job->rate.Record(done, RateNowMs());
+ job->rate_tick = -1;
+ }
job->view.done_bytes = done;
job->view.total_bytes = total;
++revision_; // progress only; the registry is unchanged
diff --git a/src/steam/install_service.h b/src/steam/install_service.h
index fa8c564..59c1184 100644
--- a/src/steam/install_service.h
+++ b/src/steam/install_service.h
@@ -2,6 +2,7 @@
#include "library/model.h"
#include "steam/session_store.h"
+#include "steam/download_rate.h"
#include <atomic>
#include <condition_variable>
@@ -43,6 +44,8 @@ struct InstallJobView {
std::uint64_t total_bytes = 0;
std::string failure; // short, user-facing
std::string volume_id;
+ std::optional<std::uint64_t> bytes_per_second;
+ std::optional<int> eta_seconds;
};
struct InstalledGame {
@@ -117,6 +120,8 @@ class InstallService {
private:
struct Job {
InstallJobView view;
+ DownloadRate rate;
+ std::int64_t rate_tick = -1;
bool cancel_requested = false; // delete files when the run stops
};
void Run();
diff --git a/tests/download_rate_test.cpp b/tests/download_rate_test.cpp
new file mode 100644
index 0000000..4b26727
--- /dev/null
+++ b/tests/download_rate_test.cpp
@@ -0,0 +1,34 @@
+#include "steam/download_rate.h"
+#include <cstdio>
+#include <cstdlib>
+#include <limits>
+using namespace playden::steam;
+#define CHECK(value) do { if (!(value)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #value); std::exit(1); } } while(false)
+int main() {
+ DownloadRate rate;
+ CHECK(!rate.Estimate(0, 0, 100000000).bytes_per_second);
+ rate.Record(9000000000ull, 0); // resumed bytes seed the baseline; they were not transferred now
+ CHECK(!rate.Estimate(0, 9000000000ull, 9100000000ull).bytes_per_second);
+ rate.Record(9002000000ull, 1000);
+ auto result = rate.Estimate(1000, 9002000000ull, 9100000000ull);
+ CHECK(result.bytes_per_second == 2000000 && result.eta_seconds == 49);
+ rate.Record(9004000000ull, 2000);
+ CHECK(rate.Estimate(2000, 9004000000ull, 9100000000ull).bytes_per_second == 2000000);
+ // Time alone changes the estimate, even when no callback arrives during a stall.
+ CHECK(rate.Estimate(4000, 9004000000ull, 9100000000ull).bytes_per_second == 1000000);
+ result = rate.Estimate(7000, 9004000000ull, 9100000000ull);
+ CHECK(result.bytes_per_second == 0 && !result.eta_seconds);
+ rate.Reset();
+ rate.Record(9004000000ull, 8000);
+ CHECK(!rate.Estimate(8500, 9004000000ull, 9100000000ull).bytes_per_second);
+ rate.Record(9005000000ull, 9000);
+ CHECK(rate.Estimate(9000, 9005000000ull, 0).bytes_per_second == 1000000);
+ CHECK(!rate.Estimate(9000, 9005000000ull, 0).eta_seconds);
+ CHECK(rate.Estimate(9000, 9005000000ull, 9005000000ull).eta_seconds == 0);
+ rate.Record(5, 10000); // regressing progress starts a new baseline
+ CHECK(!rate.Estimate(10000, 5, 10).bytes_per_second);
+ rate.Record(6, 11000);
+ CHECK(rate.Estimate(11000, 6, std::numeric_limits<std::uint64_t>::max()).eta_seconds == std::numeric_limits<int>::max());
+ rate.Record(10, 500); // a changed clock resets too
+ CHECK(!rate.Estimate(500, 10, 20).bytes_per_second);
+}
diff --git a/tests/homebrew_live_model_test.cpp b/tests/homebrew_live_model_test.cpp
index a711d9f..6ab9e9e 100644
--- a/tests/homebrew_live_model_test.cpp
+++ b/tests/homebrew_live_model_test.cpp
@@ -383,9 +383,32 @@ void live_program_change_applies_like_other_settings(const std::filesystem::path
"program: the reset applies the same way");
}
+void live_download_telemetry() {
+ using namespace playden::homebrew;
+ Simulation model({}, {}, ModelMode::Live);
+ LiveInstallJob job; job.game_id = 275390; job.state = JobState::Downloading;
+ job.done_bytes = 100000000; job.total_bytes = 1000000000;
+ job.bytes_per_second = 40055603; job.eta_seconds = 300;
+ model.UpdateInstalls({job}, {});
+ check(model.state().jobs.size() == 1 && model.state().jobs[0].bytes_per_second == 40055603 &&
+ model.state().jobs[0].eta_seconds == 300, "live transfer telemetry reaches the Downloads model");
+ check(FormatDownloadRate(job.bytes_per_second, job.eta_seconds) == "38.2 MB/s · ~5 min left", "rate and ETA formatting");
+ check(FormatDownloadRate(0, {}) == "0.0 MB/s · —", "stalled rate has no invented ETA");
+ check(FormatDownloadRate(1048576, {}) == "1.0 MB/s · —", "unknown total keeps rate but hides ETA");
+ check(FormatDownloadRate({}, 300) == "—", "unknown rate hides stale ETA");
+ check(FormatDownloadRate(1048576, 59) == "1.0 MB/s · ~59 sec left", "short ETA");
+ check(FormatDownloadRate(1048576, 61) == "1.0 MB/s · ~2 min left", "ETA rounds up");
+ for (const auto state : {JobState::Paused, JobState::Queued, JobState::Failed}) {
+ job.state = state; // stale incoming fields must never appear outside an active transfer
+ model.UpdateInstalls({job}, {});
+ check(!model.state().jobs[0].bytes_per_second && !model.state().jobs[0].eta_seconds, "inactive telemetry clears");
+ }
+}
+
} // namespace
int main() {
+ live_download_telemetry();
const auto base = std::filesystem::canonical(std::filesystem::temp_directory_path())
/ ("playden-homebrew-live-model-test-" + std::to_string(getpid()));
std::error_code error;
diff --git a/tests/install_service_test.cpp b/tests/install_service_test.cpp
index 950d619..a503aee 100644
--- a/tests/install_service_test.cpp
+++ b/tests/install_service_test.cpp
@@ -41,6 +41,7 @@ class Sessions final : public SessionRepository {
class Runner final : public InstallRunner {
public:
std::atomic<bool> block{false};
+ bool telemetry = false;
std::atomic<int> installs{0};
std::atomic<int> canceled{0};
std::mutex mutex;
@@ -53,9 +54,13 @@ class Runner final : public InstallRunner {
std::lock_guard<std::mutex> lock(mutex);
install_roots.push_back(root);
}
- progress(10, 100);
+ if (telemetry) { progress(9000000000ull, 10000000000ull); progress(9100000000ull, 10000000000ull); }
+ else progress(10, 100);
while (block) {
- if (cancel) { ++canceled; return {false, true, {}, 0}; }
+ if (cancel) {
+ if (telemetry) progress(9200000000ull, 10000000000ull); // a late callback after Pause
+ ++canceled; return {false, true, {}, 0};
+ }
std::this_thread::sleep_for(1ms);
}
std::filesystem::create_directories(root);
@@ -92,9 +97,52 @@ bool Installed(const InstallSnapshot& snapshot, GameId id) {
return false;
}
+void LiveTelemetryDecaysWithoutCallbacks(const std::filesystem::path& root) {
+ Runner runner; runner.block = true; runner.telemetry = true;
+ Sessions sessions;
+ InstallService service(runner, sessions, root.string());
+ service.Install(900);
+ AwaitAt(__LINE__, service, [](const InstallSnapshot& snapshot) {
+ const auto* job = JobOf(snapshot, 900);
+ return job && job->bytes_per_second && *job->bytes_per_second > 0;
+ });
+ const auto moving = service.Snapshot();
+ const auto* job = JobOf(moving, 900);
+ CHECK(job && *job->bytes_per_second <= 100000000 && job->eta_seconds);
+ CHECK(job->done_bytes == 9100000000ull); // only the fresh 100 MB belongs in the rate
+ const auto deadline = std::chrono::steady_clock::now() + 6s;
+ bool stalled = false;
+ while (std::chrono::steady_clock::now() < deadline) {
+ const auto snapshot = service.Snapshot();
+ const auto* current = JobOf(snapshot, 900);
+ CHECK(current && current->done_bytes == job->done_bytes);
+ if (current->bytes_per_second == 0) {
+ CHECK(!current->eta_seconds && snapshot.revision > moving.revision);
+ stalled = true; break;
+ }
+ std::this_thread::sleep_for(10ms);
+ }
+ CHECK(stalled);
+ service.Pause(900);
+ const auto paused = service.Snapshot();
+ CHECK(!JobOf(paused, 900)->bytes_per_second && !JobOf(paused, 900)->eta_seconds);
+ AwaitAt(__LINE__, service, [&](const InstallSnapshot&) { return runner.canceled == 1; });
+ CHECK(!JobOf(service.Snapshot(), 900)->bytes_per_second); // late callback cannot restore it
+ service.Resume(900);
+ AwaitAt(__LINE__, service, [&](const InstallSnapshot& snapshot) {
+ const auto* current = JobOf(snapshot, 900);
+ return runner.installs == 2 && current && current->state == InstallJobState::kRunning;
+ });
+ const auto resumed = service.Snapshot();
+ CHECK(!JobOf(resumed, 900)->bytes_per_second && !JobOf(resumed, 900)->eta_seconds);
+ service.Shutdown();
+ std::filesystem::remove_all(root);
+}
+
} // namespace
int main() {
+ LiveTelemetryDecaysWithoutCallbacks(std::filesystem::temp_directory_path() / ("playden-rate-service-" + std::to_string(::getpid())));
const auto requests = ParseInstallRequests("uninstall 439490\n439490\ncancel 108600\n108600\n");
CHECK(requests.size() == 4);
CHECK(requests[0].action == InstallRequest::Action::kUninstall && requests[0].app == 439490);