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