Skip to main content

rtc_sdp/description/
common.rs

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