rtc_sdp/description/
common.rs

1use std::fmt;
2
3/// Information describes the "i=" field which provides textual information
4/// about the session.
5pub type Information = String;
6
7/// ConnectionInformation defines the representation for the "c=" field
8/// containing connection data.
9#[derive(Debug, Default, Clone)]
10pub struct ConnectionInformation {
11    pub network_type: String,
12    pub address_type: String,
13    pub address: Option<Address>,
14}
15
16impl fmt::Display for ConnectionInformation {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        if let Some(address) = &self.address {
19            write!(f, "{} {} {}", self.network_type, self.address_type, address,)
20        } else {
21            write!(f, "{} {}", self.network_type, self.address_type,)
22        }
23    }
24}
25
26/// Address describes a structured address token from within the "c=" field.
27#[derive(Debug, Default, Clone)]
28pub struct Address {
29    pub address: String,
30    pub ttl: Option<isize>,
31    pub range: Option<isize>,
32}
33
34impl fmt::Display for Address {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "{}", self.address)?;
37        if let Some(t) = &self.ttl {
38            write!(f, "/{t}")?;
39        }
40        if let Some(r) = &self.range {
41            write!(f, "/{r}")?;
42        }
43        Ok(())
44    }
45}
46
47/// Bandwidth describes an optional field which denotes the proposed bandwidth
48/// to be used by the session or media.
49#[derive(Debug, Default, Clone)]
50pub struct Bandwidth {
51    pub experimental: bool,
52    pub bandwidth_type: String,
53    pub bandwidth: u64,
54}
55
56impl fmt::Display for Bandwidth {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        let output = if self.experimental { "X-" } else { "" };
59        write!(f, "{}{}:{}", output, self.bandwidth_type, self.bandwidth)
60    }
61}
62
63/// EncryptionKey describes the "k=" which conveys encryption key information.
64pub type EncryptionKey = String;
65
66/// Attribute describes the "a=" field which represents the primary means for
67/// extending SDP.
68#[derive(Debug, Default, Clone)]
69pub struct Attribute {
70    pub key: String,
71    pub value: Option<String>,
72}
73
74impl fmt::Display for Attribute {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        if let Some(value) = &self.value {
77            write!(f, "{}:{}", self.key, value)
78        } else {
79            write!(f, "{}", self.key)
80        }
81    }
82}
83
84impl Attribute {
85    /// new constructs a new attribute
86    pub fn new(key: String, value: Option<String>) -> Self {
87        Attribute { key, value }
88    }
89
90    /// is_ice_candidate returns true if the attribute key equals "candidate".
91    pub fn is_ice_candidate(&self) -> bool {
92        self.key.as_str() == "candidate"
93    }
94}