Files
screen_cast/include/screencast/network/h264_packetizer.h
T
fegger 943596da6d feat(app): add PLI feedback, jitter reordering, and hardware decode
Loss recovery for the streaming path:

- PLI over signaling: the depacketizer now reports damaged frames
  (DepacketizeResult) and the receiver asks the sender for a keyframe
  (SessionPli, rate-limited to one per 500 ms). The sender keeps the
  signaling channel open during the session and honors PLIs through
  the new thread-safe SenderPipeline::request_keyframe(). Recovery
  takes one frame time instead of waiting out the GOP.
- RtpJitterBuffer: reorders RTP packets by sequence number (16 packets
  / 60 ms) before the in-order depacketizer, so Wi-Fi reordering is
  not misread as loss; in-order streams release immediately, and a
  straggler older than the delivered sequence is discarded.
- Hardware H.264 decode probe: DecoderFactory tries h264_v4l2m2m (the
  VideoCore path on the Pi) with an automatic software fallback and a
  clear journal line for the chosen path; --swdecode opts out.

Validated: PLI end-to-end with a probe that drops a mid-keyframe
packet over real UDP (receiver logged the damaged frame and the PLI
arrived with the session id); hardware probe fails cleanly and falls
back on this desktop; jitter reordering covered by unit tests.
meson test 5/5 in both build configurations, valgrind clean.
2026-09-08 16:54:09 +02:00

76 lines
2.5 KiB
C++

#pragma once
#include "screencast/network/rtp_packet.h"
#include <cstddef>
#include <cstdint>
#include <optional>
#include <vector>
namespace sc {
struct EncodedFrame;
// Smallest usable MTU: 12-byte RTP header + 2-byte FU-A prefix + 1 data byte.
inline constexpr std::size_t kMinimumRtpMtu = 15;
struct RtpPacketizerConfig {
// Zero values ask the packetizer to choose random values, as recommended
// by RFC 3550 for SSRC and the initial sequence number.
std::uint32_t ssrc = 0;
std::uint16_t initial_sequence_number = 0;
std::uint8_t payload_type = 96; // dynamic payload type range
std::size_t mtu = 1200; // maximum size of a serialized RTP packet
};
// Splits Annex-B encoded frames into RFC 6184 RTP packets using single NAL
// unit packets and FU-A fragmentation. STAP-A is never emitted.
class H264Packetizer {
public:
explicit H264Packetizer(const RtpPacketizerConfig& config = RtpPacketizerConfig{});
// One encoded frame becomes one or more packets sharing the frame's RTP
// timestamp; the final packet carries the marker bit. Returns an empty
// vector for frames without NAL units or for an unusable MTU.
std::vector<RtpPacket> packetize(const EncodedFrame& frame);
private:
RtpPacket next_packet(uint32_t timestamp);
void append_fu_a_packets(std::span<const std::byte> nal, uint32_t timestamp, std::vector<RtpPacket>& out);
RtpPacketizerConfig config_;
std::uint16_t next_sequence_number_ = 0;
};
// Result of feeding one packet to the depacketizer.
struct DepacketizeResult {
// The completed access unit (Annex-B with 3-byte start codes) when the
// packet closed an undamaged frame.
std::optional<std::vector<std::byte>> access_unit;
// True when this call discarded a frame as damaged (packet loss or an
// unsupported packetization). Pipelines use it to request a keyframe.
bool frame_dropped = false;
};
// Reassembles RFC 6184 packet streams (single NAL unit packets and FU-A)
// into Annex-B access units. Packets must arrive in order; frames damaged by
// sequence gaps or missing fragments are reported via DepacketizeResult.
class H264Depacketizer {
public:
// Feed one packet.
DepacketizeResult depacketize(const RtpPacket& packet);
private:
void drop_frame();
std::optional<std::uint16_t> last_sequence_number_;
bool frame_started_ = false;
bool frame_damaged_ = false;
std::uint32_t frame_timestamp_ = 0;
std::vector<std::byte> access_unit_;
bool fu_active_ = false;
std::vector<std::byte> fu_nal_;
};
} // namespace sc