1use crate::peer_connection::configuration::UNSPECIFIED_STR;
2use std::fmt;
3
4#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
17pub enum RTCRtpTransceiverDirection {
18 #[default]
20 Unspecified,
21
22 Sendrecv,
26
27 Sendonly,
31
32 Recvonly,
36
37 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
48impl 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 pub fn reverse(&self) -> RTCRtpTransceiverDirection {
99 match *self {
100 RTCRtpTransceiverDirection::Sendonly => RTCRtpTransceiverDirection::Recvonly,
101 RTCRtpTransceiverDirection::Recvonly => RTCRtpTransceiverDirection::Sendonly,
102 _ => *self,
103 }
104 }
105
106 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 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 pub fn has_send(&self) -> bool {
129 matches!(self, Self::Sendrecv | Self::Sendonly)
130 }
131
132 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}