943596da6d
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.
64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace sc {
|
|
|
|
// Minimal RTP header (RFC 3550) without extensions.
|
|
struct RtpHeader {
|
|
uint8_t version = 2;
|
|
bool padding = false;
|
|
bool extension = false;
|
|
uint8_t csrc_count = 0;
|
|
bool marker = false;
|
|
uint8_t payload_type = 96; // dynamic
|
|
uint16_t sequence_number = 0;
|
|
uint32_t timestamp = 0;
|
|
uint32_t ssrc = 0;
|
|
|
|
bool serialize(std::span<std::byte, 12> out) const noexcept;
|
|
static std::optional<RtpHeader> parse(std::span<const std::byte, 12> in) noexcept;
|
|
};
|
|
|
|
struct RtpPacket {
|
|
RtpHeader header;
|
|
std::vector<std::byte> payload;
|
|
|
|
std::vector<std::byte> serialize() const;
|
|
static std::optional<RtpPacket> parse(std::span<const std::byte> in) noexcept;
|
|
};
|
|
|
|
// Reorders RTP packets by sequence number before depacketization so that a
|
|
// reordering link (Wi-Fi) does not read as loss. Delivery stays in order;
|
|
// only aged-out or overflowing buffers release out of order, which the
|
|
// downstream gap detection still handles for genuine loss.
|
|
class RtpJitterBuffer {
|
|
public:
|
|
explicit RtpJitterBuffer(std::size_t max_depth = 16,
|
|
std::chrono::milliseconds max_delay = std::chrono::milliseconds{60});
|
|
|
|
// Insert one packet and return the packets now ready for in-order
|
|
// delivery. In-order streams release immediately (zero added latency);
|
|
// a straggler older than the next expected sequence is discarded.
|
|
std::vector<RtpPacket> push(RtpPacket packet);
|
|
|
|
// Discard everything still buffered.
|
|
void clear();
|
|
|
|
private:
|
|
std::size_t max_depth_;
|
|
std::chrono::milliseconds max_delay_;
|
|
std::mutex mutex_;
|
|
std::map<std::uint16_t, std::pair<std::chrono::steady_clock::time_point, RtpPacket>> buffer_;
|
|
std::optional<std::uint16_t> next_expected_;
|
|
};
|
|
|
|
} // namespace sc
|