Skip to content
File

Blob: firmware/vendor/str0m/src/rtp/rtcp/sr.rs

rust151 lines
1use std::time::SystemTime;
2 
3use crate::rtp_::MediaTime;
4use crate::util::SystemTimeExt;
5 
6use super::{FeedbackMessageType, RtcpType, Ssrc};
7use super::{ReceptionReport, ReportList, RtcpHeader, RtcpPacket};
8 
9/// A report of packets sent.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct SenderReport {
12 /// Information about the sender of this report.
13 pub sender_info: SenderInfo,
14 /// A sender report is implicitly also a receiver report. This
15 /// might hold data that would otherwise come in a separate RR.
16 pub reports: ReportList<ReceptionReport>,
17}
18 
19/// Information about a stream being sent.
20///
21/// A subset of the information contained in Sender Reports(SR).
22///
23/// See [RFC 3550 6.4.1](https://www.rfc-editor.org/rfc/rfc3550#section-6.4.1)
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct SenderInfo {
26 /// The SSRC of the SR originator.
27 pub ssrc: Ssrc,
28 /// The 64 bit NTP timestamp converted to a [`SystemTime`].
29 pub ntp_time: SystemTime,
30 /// The RTP timestamp that corresponds to the same point in time as the NTP timestamp above.
31 pub rtp_time: MediaTime,
32 /// The total number of packets the sender had sent when this information was generated.
33 pub sender_packet_count: u32,
34 /// The total number of octets the sender had sent when this information was generated.
35 pub sender_octet_count: u32,
36}
37 
38impl RtcpPacket for SenderReport {
39 fn header(&self) -> RtcpHeader {
40 RtcpHeader {
41 rtcp_type: RtcpType::SenderReport,
42 feedback_message_type: FeedbackMessageType::ReceptionReport(self.reports.len() as u8),
43 words_less_one: (self.length_words() - 1) as u16,
44 }
45 }
46 
47 fn length_words(&self) -> usize {
48 // * header: 1
49 // * sender info: 6
50 // * reports: x 6
51 1 + 6 + 6 * self.reports.len()
52 }
53 
54 fn write_to(&self, buf: &mut [u8]) -> usize {
55 self.header().write_to(buf);
56 
57 self.sender_info.write_to(&mut buf[4..]);
58 
59 for (i, r) in self.reports.iter().enumerate() {
60 r.write_to(&mut buf[28 + i * 24..]);
61 }
62 
63 self.length_words() * 4
64 }
65}
66impl SenderInfo {
67 fn write_to(&self, buf: &mut [u8]) {
68 // pub ssrc: Ssrc,
69 // pub ntp_time: MediaTime,
70 // pub rtp_time: u32,
71 // pub sender_packet_count: u32,
72 // pub sender_octet_count: u32,
73 buf[..4].copy_from_slice(&self.ssrc.to_be_bytes());
74 
75 let mt = self.ntp_time.as_ntp_64();
76 buf[4..12].copy_from_slice(&mt.to_be_bytes());
77 
78 buf[12..16].copy_from_slice(&(self.rtp_time.numer() as u32).to_be_bytes());
79 buf[16..20].copy_from_slice(&self.sender_packet_count.to_be_bytes());
80 buf[20..24].copy_from_slice(&self.sender_octet_count.to_be_bytes());
81 }
82}
83 
84impl<'a> TryFrom<&'a [u8]> for SenderReport {
85 type Error = &'static str;
86 
87 fn try_from(buf: &'a [u8]) -> Result<Self, Self::Error> {
88 let sender_info = buf.try_into()?;
89 
90 let mut reports = ReportList::new();
91 let mut buf = &buf[24..];
92 
93 let count = buf.len() / 24;
94 
95 let max = count.min(31);
96 
97 for _ in 0..max {
98 let report = buf.try_into()?;
99 reports.push(report);
100 buf = &buf[24..];
101 }
102 
103 Ok(SenderReport {
104 sender_info,
105 reports,
106 })
107 }
108}
109 
110impl<'a> TryFrom<&'a [u8]> for SenderInfo {
111 type Error = &'static str;
112 
113 fn try_from(buf: &'a [u8]) -> Result<Self, Self::Error> {
114 if buf.len() < 24 {
115 return Err("Less than 24 bytes for SenderInfo");
116 }
117 
118 // Sender report shape is here
119 // https://www.rfc-editor.org/rfc/rfc3550#section-6.4.1
120 
121 let ssrc = u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]).into();
122 
123 let ntp_time = u64::from_be_bytes([
124 buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11],
125 ]);
126 let ntp_time = SystemTime::from_ntp_64(ntp_time);
127 
128 // https://www.cs.columbia.edu/~hgs/rtp/faq.html#timestamp-computed
129 // For video, time clock rate is fixed at 90 kHz. The timestamps generated
130 // depend on whether the application can determine the frame number or not.
131 // If it can or it can be sure that it is transmitting every frame with a
132 // fixed frame rate, the timestamp is governed by the nominal frame rate.
133 // Thus, for a 30 f/s video, timestamps would increase by 3,000 for each
134 // frame, for a 25 f/s video by 3,600 for each frame.
135 let rtp_time = u32::from_be_bytes([buf[12], buf[13], buf[14], buf[15]]);
136 // The base (90kHz etc) is set higher up the stack (in StreamRx to be precise).
137 let rtp_time = MediaTime::from_secs(rtp_time as u64);
138 
139 let sender_packet_count = u32::from_be_bytes([buf[16], buf[17], buf[18], buf[19]]);
140 let sender_octet_count = u32::from_be_bytes([buf[20], buf[21], buf[22], buf[23]]);
141 
142 Ok(SenderInfo {
143 ssrc,
144 ntp_time,
145 rtp_time,
146 sender_packet_count,
147 sender_octet_count,
148 })
149 }
150}