Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

fix(stream): increase NACK wait window to 300ms and tune catch-up for Wi-Fi jitter resilience

Rafael Abud Ferreira96ce241a84c8
diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cpp
index 5980bc3..1edf90d 100644
--- a/src/app/stream_player.cpp
+++ b/src/app/stream_player.cpp
@@ -295,8 +295,8 @@ struct StreamPlayer::Impl {
// 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) {
+ // Under normal playback (backlog <= 3), every frame is drawn smoothly.
+ if (backlog >= 4 && skippedInRow < 15) {
++skippedInRow;
++droppedFrames;
continue;
diff --git a/src/stream/h264_receiver.cpp b/src/stream/h264_receiver.cpp
index 3f2a232..71de5a1 100644
--- a/src/stream/h264_receiver.cpp
+++ b/src/stream/h264_receiver.cpp
@@ -15,9 +15,8 @@ namespace {
// How long a hole may wait for its retransmission (a couple of RTTs), how
// often a NACK is repeated, and how many times.
-constexpr uint64_t kWaitMs = 150;
-constexpr uint64_t kRenackMs = 40;
-constexpr int kMaxNacks = 3;
+constexpr uint64_t kRenackMs = 50;
+constexpr int kMaxNacks = 5;
constexpr size_t kMaxBuffered = 4000; // packets (about 4 MB)
constexpr uint8_t kStartCode[] = {0, 0, 0, 1};
@@ -37,8 +36,8 @@ const std::byte* asBytes(const std::vector<uint8_t>& v) { return reinterpret_cas
} // namespace
-H264Receiver::H264Receiver(std::function<void()> onKeyframeNeeded, VideoReceiveStats& stats)
- : onKeyframeNeeded_(std::move(onKeyframeNeeded)), stats_(stats) {}
+H264Receiver::H264Receiver(std::function<void()> onKeyframeNeeded, VideoReceiveStats& stats, uint64_t waitMs)
+ : onKeyframeNeeded_(std::move(onKeyframeNeeded)), stats_(stats), waitMs_(waitMs) {}
int64_t H264Receiver::unwrap(uint16_t seq) {
if (highest_ < 0) return seq;
@@ -231,7 +230,7 @@ void H264Receiver::drain(uint64_t now, rtc::message_vector& out, const rtc::mess
auto m = missing_.find(hole);
uint64_t since = m != missing_.end() ? m->second.sinceMs : now;
if (m == missing_.end()) missing_[hole] = Missing{now, now, 0};
- if (now - since < kWaitMs) return; // give the retransmission time
+ if (now - since < waitMs_) return; // give the retransmission time
// Lost for good: drop what we have of this frame and resync on
// the next key frame.
++stats_.framesDropped;
diff --git a/src/stream/h264_receiver.h b/src/stream/h264_receiver.h
index 59b2939..e00b2b1 100644
--- a/src/stream/h264_receiver.h
+++ b/src/stream/h264_receiver.h
@@ -27,7 +27,7 @@ namespace xc::stream {
class H264Receiver final : public rtc::MediaHandler {
public:
// `onKeyframeNeeded` runs on libdatachannel's thread.
- H264Receiver(std::function<void()> onKeyframeNeeded, VideoReceiveStats& stats);
+ H264Receiver(std::function<void()> onKeyframeNeeded, VideoReceiveStats& stats, uint64_t waitMs = 300);
void incoming(rtc::message_vector& messages, const rtc::message_callback& send) override;
@@ -62,6 +62,7 @@ private:
std::function<void()> onKeyframeNeeded_;
VideoReceiveStats& stats_;
+ uint64_t waitMs_ = 300;
std::map<int64_t, Packet> packets_;
std::map<int64_t, Missing> missing_;
int64_t highest_ = -1; // highest extended sequence number seen
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index ab2301a..b855247 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -962,7 +962,7 @@ static void testAudioEncoder() {
static void testH264Resync() {
xc::stream::VideoReceiveStats stats;
int keyframeAsks = 0;
- xc::stream::H264Receiver rx([&] { ++keyframeAsks; }, stats);
+ xc::stream::H264Receiver rx([&] { ++keyframeAsks; }, stats, 150);
auto packet = [](uint16_t seq, uint32_t ts, bool marker, uint8_t nal) {
std::vector<uint8_t> b = {0x80, static_cast<uint8_t>((marker ? 0x80 : 0) | 102),
static_cast<uint8_t>(seq >> 8), static_cast<uint8_t>(seq),