Skip to content

Commit

Permalink
refactor(sdp-types): fix some inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
kbalt committed Dec 4, 2024
1 parent 828d30a commit 87777e6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 30 deletions.
12 changes: 0 additions & 12 deletions crates/sdp-types/src/attributes/ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ impl IceUsernameFragment {
}
}

impl fmt::Display for IceUsernameFragment {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "a=ice-ufrag:{}", self.ufrag)
}
}

/// Ice password attribute (`a=ice-pwd`)
///
/// Session and Media Level attribute
Expand All @@ -104,9 +98,3 @@ impl IcePassword {
)(i)
}
}

impl fmt::Display for IcePassword {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "a=ice-pwd:{}", self.pwd)
}
}
2 changes: 1 addition & 1 deletion crates/sdp-types/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Connection {

impl fmt::Display for Connection {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "c={}", self.address)?;
write!(f, "{}", self.address)?;

match self.address {
TaggedAddress::IP4(_) | TaggedAddress::IP4FQDN(_) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/sdp-types/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Media {

impl fmt::Display for Media {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "m={}", self.media_type)?;
write!(f, "{}", self.media_type)?;

if let Some(ports_num) = &self.ports_num {
write!(f, " {}/{} ", self.port, ports_num)?;
Expand Down
43 changes: 37 additions & 6 deletions crates/sdp-types/src/media_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::connection::Connection;
use crate::media::Media;
use crate::{bandwidth::Bandwidth, Rtcp};
use crate::{
Direction, ExtMap, Fmtp, IceCandidate, IcePassword, IceUsernameFragment, RtpMap, SrtpCrypto,
UnknownAttribute,
Direction, ExtMap, Fmtp, IceCandidate, IcePassword, IceUsernameFragment, MediaType, RtpMap,
SrtpCrypto, TransportProtocol, UnknownAttribute,
};
use bytesstr::BytesStr;
use std::fmt::{self, Debug};
Expand Down Expand Up @@ -67,10 +67,10 @@ pub struct MediaDescription {

impl fmt::Display for MediaDescription {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}\r\n", self.media)?;
write!(f, "m={}\r\n", self.media)?;

if let Some(conn) = &self.connection {
write!(f, "{}\r\n", conn)?;
write!(f, "c={}\r\n", conn)?;
}

for bw in &self.bandwidth {
Expand Down Expand Up @@ -100,11 +100,11 @@ impl fmt::Display for MediaDescription {
}

if let Some(ufrag) = &self.ice_ufrag {
write!(f, "{}\r\n", ufrag)?;
write!(f, "a=ice-ufrag:{}\r\n", ufrag.ufrag)?;
}

if let Some(pwd) = &self.ice_pwd {
write!(f, "{}\r\n", pwd)?;
write!(f, "a=ice-pwd:{}\r\n", pwd.pwd)?;
}

for candidate in &self.ice_candidates {
Expand Down Expand Up @@ -134,3 +134,34 @@ impl fmt::Display for MediaDescription {
Ok(())
}
}

impl MediaDescription {
/// Create media description which signals rejected media
pub fn rejected(media_type: MediaType) -> Self {
MediaDescription {
media: Media {
media_type,
port: 0,
ports_num: None,
proto: TransportProtocol::RtpAvp,
fmts: vec![],
},
connection: None,
bandwidth: vec![],
direction: Direction::Inactive,
rtcp: None,
rtcp_mux: false,
mid: None,
rtpmap: vec![],
fmtp: vec![],
ice_ufrag: None,
ice_pwd: None,
ice_candidates: vec![],
ice_end_of_candidates: false,
crypto: vec![],
extmap: vec![],
extmap_allow_mixed: false,
attributes: vec![],
}
}
}
9 changes: 4 additions & 5 deletions crates/sdp-types/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Parser {
ice_end_of_candidates: false,
crypto: vec![],
extmap: vec![],
// inherit extmap allow mixed attribute
// inherit extmap allow mixed atr
extmap_allow_mixed: self.extmap_allow_mixed,
attributes: vec![],
});
Expand All @@ -127,7 +127,7 @@ impl Parser {
line: &str,
) -> Result<(), ParseSessionDescriptionError> {
if let Some((name, value)) = line.split_once(':') {
self.parse_attribute_with_value(src, line, name, value)?;
self.parse_attribute_with_value(src, name, value)?;
} else {
self.parse_attribute_without_value(src, line);
}
Expand All @@ -138,7 +138,6 @@ impl Parser {
fn parse_attribute_with_value(
&mut self,
src: &BytesStr,
line: &str,
name: &str,
value: &str,
) -> Result<(), ParseSessionDescriptionError> {
Expand Down Expand Up @@ -207,7 +206,7 @@ impl Parser {
}
}
"candidate" => {
let (_, candidate) = IceCandidate::parse(src.as_ref(), line).finish()?;
let (_, candidate) = IceCandidate::parse(src.as_ref(), value).finish()?;

if let Some(media_description) = self.media_descriptions.last_mut() {
media_description.ice_candidates.push(candidate);
Expand Down Expand Up @@ -266,7 +265,7 @@ impl Parser {
self.extmap_allow_mixed = true;

if let Some(media_description) = self.media_descriptions.last_mut() {
media_description.extmap_allow_mixed = true;
media_description.rtcp_mux = true;
}
}
"rtcp-mux" => {
Expand Down
8 changes: 4 additions & 4 deletions crates/sdp-types/src/session_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ impl fmt::Display for SessionDescription {
write!(f, "s={}\r\n", self.name)?;

if let Some(conn) = &self.connection {
write!(f, "{conn}\r\n")?;
write!(f, "c={conn}\r\n")?;
}

for bw in &self.bandwidth {
write!(f, "b={bw}\r\n")?;
}

write!(f, "{}\r\n", self.time)?;
write!(f, "t={}\r\n", self.time)?;

// omit direction here, since it is always written in media descriptions

Expand All @@ -116,11 +116,11 @@ impl fmt::Display for SessionDescription {
}

if let Some(ufrag) = &self.ice_ufrag {
write!(f, "{ufrag}\r\n")?;
write!(f, "a=ice-ufrag:{}\r\n", ufrag.ufrag)?;
}

if let Some(pwd) = &self.ice_pwd {
write!(f, "{pwd}\r\n")?;
write!(f, "a=ice-pwd:{}\r\n", pwd.pwd)?;
}

for attr in &self.attributes {
Expand Down
2 changes: 1 addition & 1 deletion crates/sdp-types/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Time {

impl fmt::Display for Time {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "t={} {}", self.start, self.stop)
write!(f, "{} {}", self.start, self.stop)
}
}

Expand Down

0 comments on commit 87777e6

Please sign in to comment.