rtci_interceptor/nack/mod.rs
1//! NACK interceptors (internal module).
2//!
3//! `NackGeneratorInterceptor` and `NackResponderInterceptor` are re-exported from the crate
4//! root, where the user-facing documentation and examples live so that rustdoc renders them.
5//!
6//! # References
7//!
8//! - [RFC 4585](https://datatracker.ietf.org/doc/html/rfc4585) - RTP/AVPF (NACK feedback)
9//! - [RFC 4588](https://datatracker.ietf.org/doc/html/rfc4588) - RTP Retransmission (RTX)
10pub(crate) mod generator;
11pub(crate) mod receive_log;
12pub(crate) mod responder;
13pub(crate) mod send_buffer;
14
15use crate::stream_info::StreamInfo;
16use crate::{Attribute, AttributedPacket, Packet, TaggedPacket};
17use shared::TransportContext;
18use std::sync::Arc;
19use std::time::{Duration, Instant};
20
21/// What a session description negotiated for retransmission (RFC 4588), told to the NACK
22/// interceptors while they run.
23///
24/// It travels as an [`Attribute::Custom`] on an empty RTCP packet written into the chain
25/// ([`NackControl::into_packet`]). Each NACK interceptor reads what is its own and hands the
26/// packet on.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28#[non_exhaustive]
29pub enum NackControl {
30 /// The `rtx-time` (RFC 4588 8.1) this endpoint announced for its retransmission format
31 /// `payload_type_rtx`: how long the [`NackResponderInterceptor`](crate::NackResponderInterceptor)
32 /// keeps a sent packet of a stream repaired under that payload type available. It replaces
33 /// the builder's `with_max_packet_age` for those streams; `None` returns them to it.
34 LocalRtxTime {
35 /// The payload type of the retransmission format.
36 payload_type_rtx: u8,
37 /// The announced `rtx-time`.
38 rtx_time: Option<Duration>,
39 },
40 /// The `rtx-time` the remote endpoint announced for its retransmission format
41 /// `payload_type_rtx`: past it the remote sender no longer has the packet, so the
42 /// [`NackGeneratorInterceptor`](crate::NackGeneratorInterceptor) stops asking for a packet
43 /// of a stream repaired under that payload type that has been missing for longer, or for
44 /// longer than the builder's `with_max_packet_age`, whichever is shorter. `None` leaves
45 /// the builder's value alone.
46 RemoteRtxTime {
47 /// The payload type of the retransmission format.
48 payload_type_rtx: u8,
49 /// The announced `rtx-time`.
50 rtx_time: Option<Duration>,
51 },
52 /// The oldest sequence number of the remote stream `media_ssrc` that the receiving
53 /// application can still use (RFC 4588 6.3: "the receiver should generally assess whether
54 /// the retransmitted packet would still be useful at the time it is received"): its video
55 /// pipeline released or gave up every frame before it. The
56 /// [`NackGeneratorInterceptor`](crate::NackGeneratorInterceptor) asks for no missing packet
57 /// older than `oldest_useful` from then on, and forgets the requests it had for them. A
58 /// request for them that the [`RtcpSessionInterceptor`](crate::RtcpSessionInterceptor)
59 /// still holds for its next packet (RFC 4585 3.5.2: early feedback is rate-limited, and
60 /// what is stored is sent only if it is still useful) is dropped there as well, so no
61 /// NACK that leaves after the horizon was given names a packet behind it.
62 /// `None` removes the horizon. Sequence numbers compare with wrap-around (RFC 3550 A.1):
63 /// a packet up to 32767 behind the horizon is older; the application moves the horizon
64 /// on as it plays out, so it never lags that far.
65 Horizon {
66 /// The SSRC of the remote stream.
67 media_ssrc: u32,
68 /// The oldest sequence number still useful.
69 oldest_useful: Option<u16>,
70 },
71}
72
73impl NackControl {
74 /// The packet that carries this to the NACK interceptors when written into the chain at
75 /// `now`.
76 pub fn into_packet(self, now: Instant) -> TaggedPacket {
77 TaggedPacket {
78 now,
79 transport: TransportContext::default(),
80 message: AttributedPacket::new(Packet::Rtcp(vec![]))
81 .with(Attribute::Custom(Arc::new(self))),
82 }
83 }
84}
85
86/// Whether `sequence_number` comes before `horizon` (RFC 3550 A.1 wrap-around: up to 32767
87/// behind it).
88pub(crate) fn older_than(sequence_number: u16, horizon: u16) -> bool {
89 (1..0x8000).contains(&horizon.wrapping_sub(sequence_number))
90}
91
92/// Check if a stream supports NACK feedback.
93///
94/// Returns `true` if the stream has an RTCPFeedback entry with `type: "nack"`
95/// and empty `parameter`.
96pub(crate) fn stream_supports_nack(info: &StreamInfo) -> bool {
97 info.rtcp_feedback
98 .iter()
99 .any(|fb| fb.typ == "nack" && fb.parameter.is_empty())
100}
101
102/// Whether a lost packet of the stream can be repaired by RTX (RFC 4588): `nack` was
103/// negotiated and the stream has an RTX stream. RFC 8854 section 8 prefers "RTX [RFC4588] or
104/// Flexible FEC's retransmission mode" to FEC; a retransmission on the media SSRC is neither.
105pub(crate) fn stream_supports_rtx_repair(info: &StreamInfo) -> bool {
106 stream_supports_nack(info) && info.ssrc_rtx.is_some()
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112 use crate::stream_info::RTCPFeedback;
113
114 #[test]
115 fn test_stream_supports_nack() {
116 // Stream with nack support
117 let info_with_nack = StreamInfo {
118 ssrc: 12345,
119 rtcp_feedback: vec![RTCPFeedback {
120 typ: "nack".to_string(),
121 parameter: "".to_string(),
122 }],
123 ..Default::default()
124 };
125 assert!(stream_supports_nack(&info_with_nack));
126
127 // Stream with nack-pli (not generic nack)
128 let info_with_nack_pli = StreamInfo {
129 ssrc: 12345,
130 rtcp_feedback: vec![RTCPFeedback {
131 typ: "nack".to_string(),
132 parameter: "pli".to_string(),
133 }],
134 ..Default::default()
135 };
136 assert!(!stream_supports_nack(&info_with_nack_pli));
137
138 // Stream without nack
139 let info_without_nack = StreamInfo {
140 ssrc: 12345,
141 rtcp_feedback: vec![RTCPFeedback {
142 typ: "goog-remb".to_string(),
143 parameter: "".to_string(),
144 }],
145 ..Default::default()
146 };
147 assert!(!stream_supports_nack(&info_without_nack));
148
149 // Stream with no feedback
150 let info_no_feedback = StreamInfo {
151 ssrc: 12345,
152 rtcp_feedback: vec![],
153 ..Default::default()
154 };
155 assert!(!stream_supports_nack(&info_no_feedback));
156 }
157}