Skip to main content

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#[cfg(test)]
103mod tests {
104    use super::*;
105    use crate::stream_info::RTCPFeedback;
106
107    #[test]
108    fn test_stream_supports_nack() {
109        // Stream with nack support
110        let info_with_nack = StreamInfo {
111            ssrc: 12345,
112            rtcp_feedback: vec![RTCPFeedback {
113                typ: "nack".to_string(),
114                parameter: "".to_string(),
115            }],
116            ..Default::default()
117        };
118        assert!(stream_supports_nack(&info_with_nack));
119
120        // Stream with nack-pli (not generic nack)
121        let info_with_nack_pli = StreamInfo {
122            ssrc: 12345,
123            rtcp_feedback: vec![RTCPFeedback {
124                typ: "nack".to_string(),
125                parameter: "pli".to_string(),
126            }],
127            ..Default::default()
128        };
129        assert!(!stream_supports_nack(&info_with_nack_pli));
130
131        // Stream without nack
132        let info_without_nack = StreamInfo {
133            ssrc: 12345,
134            rtcp_feedback: vec![RTCPFeedback {
135                typ: "goog-remb".to_string(),
136                parameter: "".to_string(),
137            }],
138            ..Default::default()
139        };
140        assert!(!stream_supports_nack(&info_without_nack));
141
142        // Stream with no feedback
143        let info_no_feedback = StreamInfo {
144            ssrc: 12345,
145            rtcp_feedback: vec![],
146            ..Default::default()
147        };
148        assert!(!stream_supports_nack(&info_no_feedback));
149    }
150}