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}
53
54impl NackControl {
55 /// The packet that carries this to the NACK interceptors when written into the chain at
56 /// `now`.
57 pub fn into_packet(self, now: Instant) -> TaggedPacket {
58 TaggedPacket {
59 now,
60 transport: TransportContext::default(),
61 message: AttributedPacket::new(Packet::Rtcp(vec![]))
62 .with(Attribute::Custom(Arc::new(self))),
63 }
64 }
65}
66
67/// Check if a stream supports NACK feedback.
68///
69/// Returns `true` if the stream has an RTCPFeedback entry with `type: "nack"`
70/// and empty `parameter`.
71pub(crate) fn stream_supports_nack(info: &StreamInfo) -> bool {
72 info.rtcp_feedback
73 .iter()
74 .any(|fb| fb.typ == "nack" && fb.parameter.is_empty())
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80 use crate::stream_info::RTCPFeedback;
81
82 #[test]
83 fn test_stream_supports_nack() {
84 // Stream with nack support
85 let info_with_nack = StreamInfo {
86 ssrc: 12345,
87 rtcp_feedback: vec![RTCPFeedback {
88 typ: "nack".to_string(),
89 parameter: "".to_string(),
90 }],
91 ..Default::default()
92 };
93 assert!(stream_supports_nack(&info_with_nack));
94
95 // Stream with nack-pli (not generic nack)
96 let info_with_nack_pli = StreamInfo {
97 ssrc: 12345,
98 rtcp_feedback: vec![RTCPFeedback {
99 typ: "nack".to_string(),
100 parameter: "pli".to_string(),
101 }],
102 ..Default::default()
103 };
104 assert!(!stream_supports_nack(&info_with_nack_pli));
105
106 // Stream without nack
107 let info_without_nack = StreamInfo {
108 ssrc: 12345,
109 rtcp_feedback: vec![RTCPFeedback {
110 typ: "goog-remb".to_string(),
111 parameter: "".to_string(),
112 }],
113 ..Default::default()
114 };
115 assert!(!stream_supports_nack(&info_without_nack));
116
117 // Stream with no feedback
118 let info_no_feedback = StreamInfo {
119 ssrc: 12345,
120 rtcp_feedback: vec![],
121 ..Default::default()
122 };
123 assert!(!stream_supports_nack(&info_no_feedback));
124 }
125}