xref: /webrtc/sdp/src/description/common.rs (revision ffe74184)
1 use std::fmt;
2 
3 /// Information describes the "i=" field which provides textual information
4 /// about the session.
5 pub type Information = String;
6 
7 /// ConnectionInformation defines the representation for the "c=" field
8 /// containing connection data.
9 #[derive(Debug, Default, Clone)]
10 pub struct ConnectionInformation {
11     pub network_type: String,
12     pub address_type: String,
13     pub address: Option<Address>,
14 }
15 
16 impl fmt::Display for ConnectionInformation {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result17     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)]
28 pub struct Address {
29     pub address: String,
30     pub ttl: Option<isize>,
31     pub range: Option<isize>,
32 }
33 
34 impl fmt::Display for Address {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result35     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36         let mut parts = vec![self.address.to_owned()];
37         if let Some(t) = &self.ttl {
38             parts.push(t.to_string());
39         }
40         if let Some(r) = &self.range {
41             parts.push(r.to_string());
42         }
43         write!(f, "{}", parts.join("/"))
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)]
50 pub struct Bandwidth {
51     pub experimental: bool,
52     pub bandwidth_type: String,
53     pub bandwidth: u64,
54 }
55 
56 impl fmt::Display for Bandwidth {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result57     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.
64 pub type EncryptionKey = String;
65 
66 /// Attribute describes the "a=" field which represents the primary means for
67 /// extending SDP.
68 #[derive(Debug, Default, Clone)]
69 pub struct Attribute {
70     pub key: String,
71     pub value: Option<String>,
72 }
73 
74 impl fmt::Display for Attribute {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result75     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 
84 impl Attribute {
85     /// new constructs a new attribute
new(key: String, value: Option<String>) -> Self86     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".
is_ice_candidate(&self) -> bool91     pub fn is_ice_candidate(&self) -> bool {
92         self.key.as_str() == "candidate"
93     }
94 }
95