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
#![allow(dead_code)]

use std::borrow::Cow;

use derive_into_owned::IntoOwned;
use nom::{combinator::map, sequence::tuple, IResult};

use crate::parsers::*;
#[cfg(test)]
use crate::{assert_line, assert_line_print};

#[derive(Clone, Debug, IntoOwned, PartialEq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct Media<'a> {
    pub r#type: Cow<'a, str>,
    pub port: u32,
    pub protocol: Vec<Cow<'a, str>>,
    pub payloads: Vec<Cow<'a, str>>,
}

pub fn media_line(input: &str) -> IResult<&str, Media> {
    line(
        "m=",
        wsf(map(
            tuple((
                wsf(cowify(read_string)),         // type
                wsf(read_number),                 // port
                wsf(slash_separated_cow_strings), // protocol
                wsf(read_as_cow_strings),         //payloads
            )),
            |(r#type, port, protocol, payloads)| Media {
                r#type,
                port,
                protocol,
                payloads,
            },
        )),
    )(input)
}

#[test]
fn test_mline() {
    assert_line!(
        media_line,
        "m=video 51744 RTP/AVP 126 97 98 34 31",
        Media {
            r#type: "video".into(),
            port: 51744,
            protocol: create_test_vec(&["RTP", "AVP"]),
            payloads: create_test_vec(&["126", "97", "98", "34", "31"]),
        },
        print
    );
    assert_line!(
        media_line,
        "m=audio 9 UDP/TLS/RTP/SAVPF 111 103 104 9 0 8 106 105 13 110 112 113 126",
        Media {
            r#type: "audio".into(),
            port: 9,
            protocol: create_test_vec(&["UDP", "TLS", "RTP", "SAVPF"]),
            payloads: create_test_vec(&[
                "111", "103", "104", "9", "0", "8", "106", "105", "13", "110", "112", "113", "126"
            ]),
        },
        print
    );
    assert_line_print!(
        media_line,
        "m=video 9 UDP/TLS/RTP/SAVPF 96 98 100 102 127 125 97 99 101 124"
    );
    assert_line_print!(media_line, "m=application 3238 UDP/BFCP *")
}