Skip to main content

rtc/rtp_transceiver/
direction.rs

1use crate::peer_connection::configuration::UNSPECIFIED_STR;
2use std::fmt;
3
4/// Direction of media flow for an RTP transceiver.
5///
6/// Indicates whether the transceiver will send and/or receive RTP media.
7///
8/// # Specification
9///
10/// See [RTCRtpTransceiverDirection](https://www.w3.org/TR/webrtc/#dom-rtcrtptransceiverdirection)
11/// in the W3C WebRTC specification.
12///
13/// # MDN
14///
15/// See [RTCRtpTransceiver.direction](https://developer.mozilla.org/en-US/docs/Web/API/RTCRtpTransceiver/direction).
16#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum RTCRtpTransceiverDirection {
19    /// Direction is not specified (internal use only).
20    #[default]
21    Unspecified,
22
23    /// Transceiver will both send and receive RTP media.
24    ///
25    /// The RTP sender will offer to send RTP and the RTP receiver will offer to receive RTP.
26    Sendrecv,
27
28    /// Transceiver will only send RTP media.
29    ///
30    /// The RTP sender will offer to send RTP.
31    Sendonly,
32
33    /// Transceiver will only receive RTP media.
34    ///
35    /// The RTP receiver will offer to receive RTP.
36    Recvonly,
37
38    /// Transceiver will neither send nor receive RTP media.
39    ///
40    /// Neither the RTP sender nor receiver will be active.
41    Inactive,
42}
43
44const RTP_TRANSCEIVER_DIRECTION_SENDRECV_STR: &str = "sendrecv";
45const RTP_TRANSCEIVER_DIRECTION_SENDONLY_STR: &str = "sendonly";
46const RTP_TRANSCEIVER_DIRECTION_RECVONLY_STR: &str = "recvonly";
47const RTP_TRANSCEIVER_DIRECTION_INACTIVE_STR: &str = "inactive";
48
49/// defines a procedure for creating a new
50/// RTPTransceiverDirection from a raw string naming the transceiver direction.
51impl From<&str> for RTCRtpTransceiverDirection {
52    fn from(raw: &str) -> Self {
53        match raw {
54            RTP_TRANSCEIVER_DIRECTION_SENDRECV_STR => RTCRtpTransceiverDirection::Sendrecv,
55            RTP_TRANSCEIVER_DIRECTION_SENDONLY_STR => RTCRtpTransceiverDirection::Sendonly,
56            RTP_TRANSCEIVER_DIRECTION_RECVONLY_STR => RTCRtpTransceiverDirection::Recvonly,
57            RTP_TRANSCEIVER_DIRECTION_INACTIVE_STR => RTCRtpTransceiverDirection::Inactive,
58            _ => RTCRtpTransceiverDirection::Unspecified,
59        }
60    }
61}
62
63impl From<u8> for RTCRtpTransceiverDirection {
64    fn from(v: u8) -> Self {
65        match v {
66            1 => RTCRtpTransceiverDirection::Sendrecv,
67            2 => RTCRtpTransceiverDirection::Sendonly,
68            3 => RTCRtpTransceiverDirection::Recvonly,
69            4 => RTCRtpTransceiverDirection::Inactive,
70            _ => RTCRtpTransceiverDirection::Unspecified,
71        }
72    }
73}
74
75impl fmt::Display for RTCRtpTransceiverDirection {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        match *self {
78            RTCRtpTransceiverDirection::Sendrecv => {
79                write!(f, "{RTP_TRANSCEIVER_DIRECTION_SENDRECV_STR}")
80            }
81            RTCRtpTransceiverDirection::Sendonly => {
82                write!(f, "{RTP_TRANSCEIVER_DIRECTION_SENDONLY_STR}")
83            }
84            RTCRtpTransceiverDirection::Recvonly => {
85                write!(f, "{RTP_TRANSCEIVER_DIRECTION_RECVONLY_STR}")
86            }
87            RTCRtpTransceiverDirection::Inactive => {
88                write!(f, "{RTP_TRANSCEIVER_DIRECTION_INACTIVE_STR}")
89            }
90            _ => write!(f, "{}", UNSPECIFIED_STR),
91        }
92    }
93}
94
95impl RTCRtpTransceiverDirection {
96    /// Returns the opposite direction.
97    ///
98    /// Swaps send-only with receive-only. Sendrecv and Inactive remain unchanged.
99    pub fn reverse(&self) -> RTCRtpTransceiverDirection {
100        match *self {
101            RTCRtpTransceiverDirection::Sendonly => RTCRtpTransceiverDirection::Recvonly,
102            RTCRtpTransceiverDirection::Recvonly => RTCRtpTransceiverDirection::Sendonly,
103            _ => *self,
104        }
105    }
106
107    /// Returns the intersection of two directions.
108    ///
109    /// The resulting direction will only send if both directions can send,
110    /// and will only receive if both directions can receive.
111    pub fn intersect(&self, other: RTCRtpTransceiverDirection) -> RTCRtpTransceiverDirection {
112        Self::from_send_recv(
113            self.has_send() && other.has_send(),
114            self.has_recv() && other.has_recv(),
115        )
116    }
117
118    /// Creates a direction from separate send and receive capabilities.
119    pub fn from_send_recv(send: bool, recv: bool) -> RTCRtpTransceiverDirection {
120        match (send, recv) {
121            (true, true) => Self::Sendrecv,
122            (true, false) => Self::Sendonly,
123            (false, true) => Self::Recvonly,
124            (false, false) => Self::Inactive,
125        }
126    }
127
128    /// Returns true if this direction includes sending media.
129    pub fn has_send(&self) -> bool {
130        matches!(self, Self::Sendrecv | Self::Sendonly)
131    }
132
133    /// Returns true if this direction includes receiving media.
134    pub fn has_recv(&self) -> bool {
135        matches!(self, Self::Sendrecv | Self::Recvonly)
136    }
137}
138
139#[cfg(test)]
140mod test {
141    use super::*;
142
143    #[test]
144    fn test_new_rtp_transceiver_direction() {
145        let tests = vec![
146            ("Unspecified", RTCRtpTransceiverDirection::Unspecified),
147            ("sendrecv", RTCRtpTransceiverDirection::Sendrecv),
148            ("sendonly", RTCRtpTransceiverDirection::Sendonly),
149            ("recvonly", RTCRtpTransceiverDirection::Recvonly),
150            ("inactive", RTCRtpTransceiverDirection::Inactive),
151        ];
152
153        for (ct_str, expected_type) in tests {
154            assert_eq!(RTCRtpTransceiverDirection::from(ct_str), expected_type);
155        }
156    }
157
158    #[test]
159    fn test_rtp_transceiver_direction_string() {
160        let tests = vec![
161            (RTCRtpTransceiverDirection::Unspecified, "Unspecified"),
162            (RTCRtpTransceiverDirection::Sendrecv, "sendrecv"),
163            (RTCRtpTransceiverDirection::Sendonly, "sendonly"),
164            (RTCRtpTransceiverDirection::Recvonly, "recvonly"),
165            (RTCRtpTransceiverDirection::Inactive, "inactive"),
166        ];
167
168        for (d, expected_string) in tests {
169            assert_eq!(d.to_string(), expected_string);
170        }
171    }
172
173    #[test]
174    fn test_rtp_transceiver_has_send() {
175        let tests = vec![
176            (RTCRtpTransceiverDirection::Unspecified, false),
177            (RTCRtpTransceiverDirection::Sendrecv, true),
178            (RTCRtpTransceiverDirection::Sendonly, true),
179            (RTCRtpTransceiverDirection::Recvonly, false),
180            (RTCRtpTransceiverDirection::Inactive, false),
181        ];
182
183        for (d, expected_value) in tests {
184            assert_eq!(d.has_send(), expected_value);
185        }
186    }
187
188    #[test]
189    fn test_rtp_transceiver_has_recv() {
190        let tests = vec![
191            (RTCRtpTransceiverDirection::Unspecified, false),
192            (RTCRtpTransceiverDirection::Sendrecv, true),
193            (RTCRtpTransceiverDirection::Sendonly, false),
194            (RTCRtpTransceiverDirection::Recvonly, true),
195            (RTCRtpTransceiverDirection::Inactive, false),
196        ];
197
198        for (d, expected_value) in tests {
199            assert_eq!(d.has_recv(), expected_value);
200        }
201    }
202
203    #[test]
204    fn test_rtp_transceiver_from_send_recv() {
205        let tests = vec![
206            (RTCRtpTransceiverDirection::Sendrecv, (true, true)),
207            (RTCRtpTransceiverDirection::Sendonly, (true, false)),
208            (RTCRtpTransceiverDirection::Recvonly, (false, true)),
209            (RTCRtpTransceiverDirection::Inactive, (false, false)),
210        ];
211
212        for (expected_value, (send, recv)) in tests {
213            assert_eq!(
214                RTCRtpTransceiverDirection::from_send_recv(send, recv),
215                expected_value
216            );
217        }
218    }
219
220    #[test]
221    fn test_rtp_transceiver_intersect() {
222        use RTCRtpTransceiverDirection::*;
223
224        let tests = vec![
225            ((Sendrecv, Recvonly), Recvonly),
226            ((Sendrecv, Sendonly), Sendonly),
227            ((Sendrecv, Inactive), Inactive),
228            ((Sendonly, Inactive), Inactive),
229            ((Recvonly, Inactive), Inactive),
230            ((Recvonly, Sendrecv), Recvonly),
231            ((Sendonly, Sendrecv), Sendonly),
232            ((Sendonly, Recvonly), Inactive),
233            ((Recvonly, Recvonly), Recvonly),
234        ];
235
236        for ((a, b), expected_direction) in tests {
237            assert_eq!(a.intersect(b), expected_direction);
238        }
239    }
240}