Xbox Cloud Gaming app for PS5 Homebrew
C
fix(stream): skip padding-only RTP packets; treat oversized payloads as lost
Rafael Abud Ferreirab26871e47cc3
diff --git a/src/stream/h264_receiver.cpp b/src/stream/h264_receiver.cppindex 0337e22..2f13569 100644--- a/src/stream/h264_receiver.cpp+++ b/src/stream/h264_receiver.cpp@@ -53,7 +53,7 @@ void H264Receiver::putPacket(int64_t s, uint32_t ts, bool marker, const uint8_t*ring_[idx].seq = s;ring_[idx].timestamp = ts;ring_[idx].marker = marker;- ring_[idx].length = static_cast<uint16_t>(std::min<size_t>(len, 1536));+ ring_[idx].length = static_cast<uint16_t>(std::min(len, kMaxPayload)); // accept() refuses larger onesif (ring_[idx].length > 0 && data) {std::memcpy(ring_[idx].payload, data, ring_[idx].length);}@@ -173,7 +173,8 @@ bool H264Receiver::assemble(int64_t first, int64_t last, std::vector<uint8_t>& okeyframe = false;for (int64_t s = first; s <= last; ++s) {const auto* slot = getPacket(s);- if (!slot || slot->length == 0) return false;+ if (!slot) return false;+ if (slot->length == 0) continue; // padding only: nothing to add, the frame goes onconst uint8_t* p = slot->payload;size_t pLen = slot->length;uint8_t type = p[0] & 0x1F;@@ -329,6 +330,16 @@ void H264Receiver::accept(const uint8_t* d, size_t n, uint64_t now, const rtc::mint64_t seq = unwrap(rawSeq);if (nextSeq_ >= 0 && seq < nextSeq_) return; // too late or duplicateif (hasPacket(seq)) return;+ if (end - off > kMaxPayload) {+ // Larger than a slot: cut short it would corrupt the NAL unit. Left+ // as a hole instead, NACKed when the next packet shows it (and+ // still missing if it was), then the frame dropped and a key frame+ // asked for, like any loss.+ if (oversized_++ % 100 == 0)+ XC_LOGW("video: RTP payload of %zu bytes is larger than %zu, treated as lost (%llu so far)", end - off,+ kMaxPayload, static_cast<unsigned long long>(oversized_));+ return;+ }if (missing_.erase(seq)) ++stats_.recovered;uint32_t ts = (uint32_t(d[4]) << 24) | (uint32_t(d[5]) << 16) | (uint32_t(d[6]) << 8) | d[7];diff --git a/src/stream/h264_receiver.h b/src/stream/h264_receiver.hindex f09daf4..c044690 100644--- a/src/stream/h264_receiver.h+++ b/src/stream/h264_receiver.h@@ -38,12 +38,13 @@ public:void setSimulatedLoss(int percent) { simulatedLoss_ = percent; }private:+ static constexpr size_t kMaxPayload = 1536; // a larger payload is treated as lost (accept())struct PacketSlot {int64_t seq = -1;uint32_t timestamp = 0;bool marker = false;- uint16_t length = 0;- uint8_t payload[1536];+ uint16_t length = 0; // 0: padding only (kept: it fills its sequence number)+ uint8_t payload[kMaxPayload];};static constexpr size_t kRingSize = 2048;static constexpr size_t kRingMask = kRingSize - 1;@@ -87,6 +88,7 @@ private:bool waitingKeyframe_ = true;uint32_t mediaSsrc_ = 0;uint64_t lastKeyframeRequestMs_ = 0;+ uint64_t oversized_ = 0; // payloads too large for a slot, droppedint simulatedLoss_ = 0;uint32_t lossState_ = 12345;std::map<uint16_t, std::vector<uint8_t>> held_; // simulated lossdiff --git a/tests/test_main.cpp b/tests/test_main.cppindex 6d226b4..bebd8db 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -1160,6 +1160,51 @@ static void testH264Resync() {CHECK(keyframeAsks >= 1);}+// A padding-only RTP packet (payload all padding) inside a frame is skipped,+// not a reason to fail that frame; a payload larger than a buffer slot is a+// lost packet (NACKed, then the frame dropped), never a cut NAL unit.+static void testH264PaddingPacket() {+ xc::stream::VideoReceiveStats stats;+ int keyframeAsks = 0;+ xc::stream::H264Receiver rx([&] { ++keyframeAsks; }, stats, 60);+ auto packet = [](uint16_t seq, uint32_t ts, bool marker, std::vector<uint8_t> payload, uint8_t padding = 0) {+ std::vector<uint8_t> b = {static_cast<uint8_t>(0x80 | (padding ? 0x20 : 0)),+ static_cast<uint8_t>((marker ? 0x80 : 0) | 102),+ static_cast<uint8_t>(seq >> 8), static_cast<uint8_t>(seq),+ static_cast<uint8_t>(ts >> 24), static_cast<uint8_t>(ts >> 16),+ static_cast<uint8_t>(ts >> 8), static_cast<uint8_t>(ts), 0, 0, 0, 7};+ b.insert(b.end(), payload.begin(), payload.end());+ for (uint8_t i = 1; i <= padding; ++i) b.push_back(i == padding ? padding : 0);+ return rtc::make_message(reinterpret_cast<const std::byte*>(b.data()),+ reinterpret_cast<const std::byte*>(b.data()) + b.size());+ };+ auto nal = [](uint8_t type) { return std::vector<uint8_t>{type, 0xAA, 0xBB}; };+ size_t framesOut = 0;+ auto feed = [&](rtc::message_vector in) {+ rx.incoming(in, [](rtc::message_ptr) { return true; });+ framesOut += in.size();+ };+ // A boundary, a key frame, a P frame, then a padding-only packet opening+ // the next P frame (same timestamp, no marker), that P frame and another.+ feed({packet(0, 0, true, nal(0x41)), packet(1, 1500, true, nal(0x65)), packet(2, 3000, true, nal(0x41)),+ packet(3, 4500, false, {}, 4), packet(4, 4500, true, nal(0x41)), packet(5, 6000, true, nal(0x41))});+ CHECK(framesOut == 4);+ CHECK(stats.framesDropped == 0 && keyframeAsks == 0);+ // A payload too large for a slot: a hole (NACKed), then the frame dropped.+ std::vector<uint8_t> big(2000, 0xCC);+ big[0] = 0x41;+ uint64_t nacksBefore = stats.nacks;+ feed({packet(6, 7500, true, big), packet(7, 9000, true, nal(0x41))});+ CHECK(framesOut == 4); // waiting for the retransmission+ CHECK(stats.nacks > nacksBefore && stats.lost >= 1);+ xc::platform::sleepMs(100);+ feed({});+ CHECK(framesOut == 4 && stats.framesDropped >= 1 && keyframeAsks >= 1);+ // The key frame asked for resumes the picture.+ feed({packet(8, 10500, true, nal(0x65)), packet(9, 12000, true, nal(0x41))});+ CHECK(framesOut == 6);+}+// An empty audio packet (seen after loss) must not end the sound.static void testAudioDecoderEmptyPacket() {xc::media::AudioEncoder enc;@@ -2024,6 +2069,7 @@ int main() {testMicrosoftHost();testAudioEncoder();testH264Resync();+ testH264PaddingPacket();testAudioDecoderEmptyPacket();testAudioDecoderPlc();testAudioGapLossConcealment();