Xbox Cloud Gaming app for PS5 Homebrew
C
fix(stream): eliminate microstutter, burst resets, and snapshot I/O stalls
Rafael Abud Ferreirab9155185880e
diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cppindex bdbd0d7..5980bc3 100644--- a/src/app/stream_player.cpp+++ b/src/app/stream_player.cpp@@ -31,8 +31,7 @@ namespace {// Beyond this, start over from a key frame. Generous: the jitter buffer// hands over several frames at once after waiting for a retransmission, and// decoding without drawing catches up with those in a few milliseconds.-constexpr size_t kMaxQueuedFrames = 30;-constexpr int kMaxFullStreak = 30; // frames shown one refresh late before dropping one+constexpr size_t kMaxQueuedFrames = 90;}struct StreamPlayer::Impl {@@ -210,7 +209,6 @@ struct StreamPlayer::Impl {bool hwOk = hwProbe && hw.init(1920, 1088);if (hwProbe) XC_LOGI("hwdec: probe %s", hwOk ? "running" : "failed to start");uint64_t hwUs = 0, hwMaxUs = 0, hwPictures = 0, hwCalls = 0, hwLogAt = platform::nowMs() + 1000;- int fullStreak = 0;int skippedInRow = 0;constexpr uint64_t kShownRing = 8;std::pair<uint64_t, uint64_t> inScreenQueue[kShownRing] = {}; // present id, arrival us@@ -294,33 +292,26 @@ struct StreamPlayer::Impl {++qpCount;}pictureSize = (static_cast<uint32_t>(pic.width) << 16) | static_cast<uint32_t>(pic.height);- bool screenshot = saveSnapshotIfAsked(pic);- // A newer frame already waits: decode this one (the next needs it)- // but show the newer one, a frame sooner. At most two in a row, so- // the picture keeps moving when decoding runs behind.- if (backlog > 0 && skippedInRow < 2) {+ // When a burst of packets arrives after network jitter/retransmission,+ // fast-forward decode without drawing to drain the stale backlog in a few+ // milliseconds and present the newest live frame immediately.+ // Under normal playback (backlog <= 1), every frame is drawn smoothly.+ if (backlog >= 2 && skippedInRow < 15) {++skippedInRow;++droppedFrames;continue;}skippedInRow = 0;+ bool screenshot = saveSnapshotIfAsked(pic);std::lock_guard<std::mutex> lock(display::frameMutex());if (decodedFrames == 1) XC_LOGI("first draw %dx%d", pic.width, pic.height);uint64_t t2 = platform::nowUs();- // Both flips queued means the picture shows a frame later than- // it could. Arrival jitter does that now and then; it is waited- // out (the queue then stays full: stream and display both run at- // 60 Hz). After half a second of that, a frame is dropped to get- // the latency back.+ // Try acquire with zero wait first; if not ready, wait for swapchain image.bool drawn = display::drawYuv420(pic.y, pic.u, pic.v, pic.strideY, pic.strideU, pic.strideV,pic.width, pic.height, false);- if (drawn) {- fullStreak = 0;- } else if (++fullStreak < kMaxFullStreak) {+ if (!drawn) {drawn = display::drawYuv420(pic.y, pic.u, pic.v, pic.strideY, pic.strideU, pic.strideV,pic.width, pic.height, true);- } else {- fullStreak = 0;}if (!drawn) {++lateFrames;@@ -565,10 +556,12 @@ struct StreamPlayer::Impl {o[1] = clamp((c - 218 * d - 546 * e + 512) >> 10);o[2] = clamp((c + 2163 * d + 512) >> 10);}- std::string data = "P6\n" + std::to_string(w) + " " + std::to_string(h) + "\n255\n";- data.append(reinterpret_cast<const char*>(rgb.data()), rgb.size());- bool ok = platform::writeFileAtomic(path, data);- XC_LOGI("snapshot %dx%d -> %s: %s", w, h, path.c_str(), ok ? "ok" : "failed");+ std::thread([path, rgb = std::move(rgb), w, h]() mutable {+ std::string data = "P6\n" + std::to_string(w) + " " + std::to_string(h) + "\n255\n";+ data.append(reinterpret_cast<const char*>(rgb.data()), rgb.size());+ bool ok = platform::writeFileAtomic(path, data);+ XC_LOGI("snapshot %dx%d -> %s: %s", w, h, path.c_str(), ok ? "ok" : "failed");+ }).detach();return true;}@@ -577,10 +570,13 @@ struct StreamPlayer::Impl {std::vector<uint8_t> rgb;int w = 0, h = 0;if (!display::readBackRgb(rgb, w, h)) return;- std::string data = "P6\n" + std::to_string(w) + " " + std::to_string(h) + "\n255\n";- data.append(reinterpret_cast<const char*>(rgb.data()), rgb.size());- bool ok = platform::writeFileAtomic(screenPath, data);- XC_LOGI("screen -> %s: %s", screenPath.c_str(), ok ? "ok" : "failed");+ std::string path = screenPath;+ std::thread([path, rgb = std::move(rgb), w, h]() mutable {+ std::string data = "P6\n" + std::to_string(w) + " " + std::to_string(h) + "\n255\n";+ data.append(reinterpret_cast<const char*>(rgb.data()), rgb.size());+ bool ok = platform::writeFileAtomic(path, data);+ XC_LOGI("screen -> %s: %s", path.c_str(), ok ? "ok" : "failed");+ }).detach();}// Video thread only (data channel sends are thread-safe).diff --git a/src/media/audio_queue.h b/src/media/audio_queue.hindex e913cef..70de2c8 100644--- a/src/media/audio_queue.h+++ b/src/media/audio_queue.h@@ -21,9 +21,9 @@ class AudioQueue {public:static constexpr int kRate = 48000;// Bounded to keep the sound close to the picture while absorbing normal- // network jitter (20-40 ms bursts). Stereo frames.- static constexpr size_t kMaxQueued = kRate * 100 / 1000; // 100 ms- static constexpr size_t kTrimTo = kRate * 60 / 1000; // 60 ms+ // network jitter (20-40 ms bursts, up to 150 ms NACK recovery). Stereo frames.+ static constexpr size_t kMaxQueued = kRate * 200 / 1000; // 200 ms+ static constexpr size_t kTrimTo = kRate * 100 / 1000; // 100 ms// Since the last log (reset by it).struct Stats {diff --git a/tests/test_main.cpp b/tests/test_main.cppindex d5cac44..ab2301a 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -1540,7 +1540,7 @@ static void testNullBackends() {// --- M1 input / audio backends (pure parts, null backends) -----------------// The audio queue: bounded at ~100 ms (cut back to 60 ms, oldest first),+// The audio queue: bounded at ~200 ms (cut back to 100 ms, oldest first),// silence past its end, the volume, and what the periodic log counts.static void testAudioQueue() {media::AudioQueue q;@@ -1548,22 +1548,22 @@ static void testAudioQueue() {q.push(in.data(), 480);CHECK(q.queuedFrames() == 0); // closed: ignoredq.open();- for (int i = 0; i < 9; ++i) q.push(in.data(), 480); // 90 ms: under the bound- CHECK(q.queuedFrames() == 4320 && q.stats().trimmed == 0);- q.push(in.data(), 480); // 100 ms- q.push(in.data(), 480); // 110 ms: past 100, back to 60- CHECK(q.queuedFrames() == media::AudioQueue::kTrimTo && q.stats().trimmed == 5280 - 2880);+ for (int i = 0; i < 19; ++i) q.push(in.data(), 480); // 190 ms: under the bound+ CHECK(q.queuedFrames() == 9120 && q.stats().trimmed == 0);+ q.push(in.data(), 480); // 200 ms+ q.push(in.data(), 480); // 210 ms: past 200, back to 100+ CHECK(q.queuedFrames() == media::AudioQueue::kTrimTo && q.stats().trimmed == 10080 - 4800);// The oldest go first: mark the newest frame and find it last.float mark[2] = {0.5f, -0.5f};q.push(mark, 1);- std::vector<float> out(2881 * 2);- q.pull(out.data(), 2881);- CHECK(out[2880 * 2] == 0.5f && out[2880 * 2 + 1] == -0.5f && out[100 * 2] == 0.25f);+ std::vector<float> out(4801 * 2);+ q.pull(out.data(), 4801);+ CHECK(out[4800 * 2] == 0.5f && out[4800 * 2 + 1] == -0.5f && out[100 * 2] == 0.25f);CHECK(q.queuedFrames() == 0);q.pull(out.data(), 256); // empty: silence, countedCHECK(out[0] == 0.0f && out[511] == 0.0f);media::AudioQueue::Stats st = q.stats();- CHECK(st.pulls == 2 && st.empty == 1 && st.frames == 2881 + 256 && st.depthMax == 2881);+ CHECK(st.pulls == 2 && st.empty == 1 && st.frames == 4801 + 256 && st.depthMax == 4801);// Volume: clamped to 0..2, applied and clipped at full scale.q.setVolume(5.0f);CHECK(q.volume() == 2.0f);@@ -1575,9 +1575,9 @@ static void testAudioQueue() {CHECK(q.volume() == 0.0f);q.setVolume(1.0f);// ~10 s of sound pulled (480000 frames): logged, and the counts start- // again. 3139 frames so far, so the 1863rd grain of 256 crosses it.+ // again. 5059 frames so far, so the 1856th grain of 256 crosses it.for (int i = 0; i < 1875; ++i) q.pull(out.data(), 256);- CHECK(q.stats().pulls == 12 && q.stats().frames == 12 * 256 && q.stats().trimmed == 0);+ CHECK(q.stats().pulls == 19 && q.stats().frames == 19 * 256 && q.stats().trimmed == 0);q.close();q.push(loud, 2);CHECK(q.queuedFrames() == 0);@@ -1732,8 +1732,8 @@ static void testNullBackendsM1() {CHECK(!kbm.keyboard && !kbm.mouse && !kbm.moved && kbm.dx == 0);input::kbmSetCaptured(false);CHECK(media::audioStart() && media::audioStart());- std::vector<float> sound(48 * 2 * 110, 0.1f); // 110 ms, never played: kept to the bound- media::audioPush(sound.data(), 48 * 110);+ std::vector<float> sound(48 * 2 * 210, 0.1f); // 210 ms, never played: kept to the bound+ media::audioPush(sound.data(), 48 * 210);CHECK(media::audioQueue().queuedFrames() == media::AudioQueue::kTrimTo);media::audioStop();CHECK(media::audioQueue().queuedFrames() == 0);