1// Copyright (c) 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use xso::{AsXml, FromXml};
8
9use crate::ns;
10
11generate_attribute!(
12 /// Which party is allowed to send the negotiated RTP Header Extensions.
13 Senders, "senders", {
14 /// Both parties can send them.
15 Both => "both",
16
17 /// Only the initiator can send them.
18 Initiator => "initiator",
19
20 /// Only the responder can send them.
21 Responder => "responder",
22 }, Default = Both
23);
24
25/// Header extensions to be used in a RTP description.
26#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
27#[xml(namespace = ns::JINGLE_RTP_HDREXT, name = "rtp-hdrext")]
28pub struct RtpHdrext {
29 /// The ID of the extensions. The allowed values are only in the 1-256, 4096-4351 ranges,
30 /// this isn’t enforced by xmpp-parsers yet!
31 // TODO: make it so.
32 #[xml(attribute)]
33 pub id: u16,
34
35 /// The URI that defines the extension.
36 #[xml(attribute)]
37 pub uri: String,
38
39 /// Which party is allowed to send the negotiated RTP Header Extensions.
40 #[xml(attribute(default))]
41 pub senders: Senders,
42}
43
44impl RtpHdrext {
45 /// Create a new RTP header extension element.
46 pub fn new(id: u16, uri: String) -> RtpHdrext {
47 RtpHdrext {
48 id,
49 uri,
50 senders: Default::default(),
51 }
52 }
53
54 /// Set the senders.
55 pub fn with_senders(mut self, senders: Senders) -> RtpHdrext {
56 self.senders = senders;
57 self
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64 use minidom::Element;
65
66 #[cfg(target_pointer_width = "32")]
67 #[test]
68 fn test_size() {
69 assert_size!(Senders, 1);
70 assert_size!(RtpHdrext, 16);
71 }
72
73 #[cfg(target_pointer_width = "64")]
74 #[test]
75 fn test_size() {
76 assert_size!(Senders, 1);
77 assert_size!(RtpHdrext, 32);
78 }
79
80 #[test]
81 fn parse_exthdr() {
82 let elem: Element = "<rtp-hdrext xmlns='urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'
83 uri='urn:ietf:params:rtp-hdrext:toffset'
84 id='1'/>"
85 .parse()
86 .unwrap();
87 let rtp_hdrext = RtpHdrext::try_from(elem).unwrap();
88 assert_eq!(rtp_hdrext.id, 1);
89 assert_eq!(rtp_hdrext.uri, "urn:ietf:params:rtp-hdrext:toffset");
90 assert_eq!(rtp_hdrext.senders, Senders::Both);
91 }
92}