Skip to main content

msf_rtsp/header/
rtpinfo.rs

1//! RTP-Info header and related types.
2
3use std::{
4    borrow::{Borrow, Cow},
5    fmt::{self, Display, Formatter},
6};
7
8use crate::header::HeaderFieldValue;
9
10/// RTP info header field.
11#[derive(Clone)]
12pub struct RTPInfo {
13    inner: RTPInfoRef<'static>,
14}
15
16impl RTPInfo {
17    /// Create a new RTP-Info header.
18    pub fn new<T>(url: T) -> Self
19    where
20        T: Into<String>,
21    {
22        let inner = RTPInfoRef {
23            url: Cow::Owned(url.into()),
24            seq: None,
25            rtp_time: None,
26        };
27
28        Self { inner }
29    }
30
31    /// Set the RTP packet sequence number.
32    #[inline]
33    pub const fn with_seq(mut self, seq: u16) -> Self {
34        self.inner.seq = Some(seq);
35        self
36    }
37
38    /// Set the RTP timestamp.
39    #[inline]
40    pub const fn with_rtp_time(mut self, rtp_time: u32) -> Self {
41        self.inner.rtp_time = Some(rtp_time);
42        self
43    }
44}
45
46impl Borrow<RTPInfoRef<'static>> for RTPInfo {
47    #[inline]
48    fn borrow(&self) -> &RTPInfoRef<'static> {
49        &self.inner
50    }
51}
52
53impl Display for RTPInfo {
54    #[inline]
55    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
56        Display::fmt(Borrow::<RTPInfoRef>::borrow(self), f)
57    }
58}
59
60impl From<RTPInfo> for HeaderFieldValue {
61    #[inline]
62    fn from(value: RTPInfo) -> Self {
63        HeaderFieldValue::from(value.to_string())
64    }
65}
66
67/// RTP info header field.
68#[derive(Clone)]
69pub struct RTPInfoRef<'a> {
70    url: Cow<'a, str>,
71    seq: Option<u16>,
72    rtp_time: Option<u32>,
73}
74
75impl<'a> RTPInfoRef<'a> {
76    /// Create a new RTP-Info header.
77    #[inline]
78    pub const fn new(url: &'a str) -> Self {
79        Self {
80            url: Cow::Borrowed(url),
81            seq: None,
82            rtp_time: None,
83        }
84    }
85}
86
87impl RTPInfoRef<'_> {
88    /// Set the RTP packet sequence number.
89    #[inline]
90    pub const fn with_seq(mut self, seq: u16) -> Self {
91        self.seq = Some(seq);
92        self
93    }
94
95    /// Set the RTP timestamp.
96    #[inline]
97    pub const fn with_rtp_time(mut self, rtp_time: u32) -> Self {
98        self.rtp_time = Some(rtp_time);
99        self
100    }
101}
102
103impl Display for RTPInfoRef<'_> {
104    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
105        write!(f, "url={}", self.url)?;
106
107        if let Some(seq) = self.seq {
108            write!(f, ";seq={seq}")?;
109        }
110
111        if let Some(rtp_time) = self.rtp_time {
112            write!(f, ";rtptime={rtp_time}")?;
113        }
114
115        Ok(())
116    }
117}
118
119impl From<RTPInfoRef<'_>> for HeaderFieldValue {
120    #[inline]
121    fn from(value: RTPInfoRef) -> Self {
122        HeaderFieldValue::from(value.to_string())
123    }
124}