Skip to content
File

Blob: firmware/vendor/str0m/src/rtp/dir.rs

rust86 lines
1use std::fmt;
2 
3/// Media direction.
4///
5/// And also extmap direction.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Direction {
8 /// Send only direction.
9 SendOnly,
10 /// Receive only direction.
11 RecvOnly,
12 /// Bi-directional.
13 SendRecv,
14 /// Disabled direction.
15 Inactive,
16}
17 
18#[cfg(feature = "drv")]
19crate::drv_identity_copy!(Direction);
20 
21impl Direction {
22 /// Change the direction to the opposite.
23 ///
24 /// `SendRecv` and `Inactive` are left unchanged.
25 pub fn invert(&self) -> Self {
26 match self {
27 Direction::SendOnly => Direction::RecvOnly,
28 Direction::RecvOnly => Direction::SendOnly,
29 _ => *self,
30 }
31 }
32 
33 /// Whether this direction is a sending direction.
34 pub fn is_sending(&self) -> bool {
35 matches!(self, Direction::SendOnly | Direction::SendRecv)
36 }
37 
38 /// Whether this direction is a receiving direction.
39 pub fn is_receiving(&self) -> bool {
40 matches!(self, Direction::RecvOnly | Direction::SendRecv)
41 }
42 
43 /// When negotiating SDP, in certain cases, we need to treat `Inactive`
44 /// as if it is receiving.
45 pub(crate) fn sdp_is_receiving(&self) -> bool {
46 // The spec says:
47 //
48 // > For streams marked as inactive in the answer, the list of media
49 // > formats is constructed based on the offer. If the offer was sendonly,
50 // > the list is constructed as if the answer were recvonly.
51 //
52 // https://datatracker.ietf.org/doc/html/rfc3264#section-6.1
53 matches!(
54 self,
55 Direction::RecvOnly | Direction::SendRecv | Direction::Inactive
56 )
57 }
58}
59 
60impl From<&str> for Direction {
61 fn from(v: &str) -> Self {
62 use Direction::*;
63 match v {
64 "sendonly" => SendOnly,
65 "recvonly" => RecvOnly,
66 "sendrecv" => SendRecv,
67 _ => Inactive,
68 }
69 }
70}
71 
72impl fmt::Display for Direction {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 write!(
75 f,
76 "{}",
77 match self {
78 Direction::SendOnly => "sendonly",
79 Direction::RecvOnly => "recvonly",
80 Direction::SendRecv => "sendrecv",
81 Direction::Inactive => "inactive",
82 }
83 )
84 }
85}