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