Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 460da8d

Browse files
author
Rain Liu
committed
add prt
1 parent b557269 commit 460da8d

File tree

4 files changed

+98
-21
lines changed

4 files changed

+98
-21
lines changed

src/extended_report/dlrr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl MarshalSize for DLRRReportBlock {
9696
impl Marshal for DLRRReportBlock {
9797
/// marshal_to encodes the DLRRReportBlock in binary
9898
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
99-
if buf.remaining_mut() < self.raw_size() {
99+
if buf.remaining_mut() < self.marshal_size() {
100100
return Err(error::Error::BufferTooShort.into());
101101
}
102102

src/extended_report/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
mod extended_report_test;
33

44
pub mod dlrr;
5-
/*pub mod prt;
6-
pub mod rle;
7-
pub mod rrt;
8-
pub mod ssr;
9-
pub mod vm;*/
5+
pub mod prt; /*
6+
pub mod rle;
7+
pub mod rrt;
8+
pub mod ssr;
9+
pub mod vm;*/
1010
pub mod unknown;
1111

1212
pub use unknown::UnknownReportBlock;

src/extended_report/prt.rs

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::*;
22

3+
const PACKET_RECEIPT_TIMES_REPORT_BLOCK_MIN_LENGTH: u16 = 9;
4+
35
/// PacketReceiptTimesReportBlock represents a Packet Receipt Times
46
/// report block, as described in RFC 3611 section 4.3.
57
///
@@ -22,7 +24,6 @@ use super::*;
2224
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2325
#[derive(Debug, Default, PartialEq, Clone)]
2426
pub struct PacketReceiptTimesReportBlock {
25-
pub xr_header: XRHeader,
2627
pub t: u8,
2728
pub ssrc: u32,
2829
pub begin_seq: u16,
@@ -36,36 +37,112 @@ impl fmt::Display for PacketReceiptTimesReportBlock {
3637
}
3738
}
3839

39-
impl ReportBlock for PacketReceiptTimesReportBlock {
40-
/// destination_ssrc returns an array of ssrc values that this report block refers to.
41-
fn destination_ssrc(&self) -> Vec<u32> {
42-
vec![self.ssrc]
40+
impl PacketReceiptTimesReportBlock {
41+
pub fn xr_header(&self) -> XRHeader {
42+
XRHeader {
43+
block_type: BlockType::PacketReceiptTimes,
44+
type_specific: self.t & 0x0F,
45+
block_length: (self.raw_size() / 4 - 1) as u16,
46+
}
4347
}
48+
}
4449

45-
fn setup_block_header(&mut self) {
46-
self.xr_header.block_type = ReportBlockType::PacketReceiptTimes;
47-
self.xr_header.type_specific = self.t & 0x0F;
48-
self.xr_header.block_length = (self.raw_size() / 4 - 1) as u16;
50+
impl Packet for PacketReceiptTimesReportBlock {
51+
fn header(&self) -> Header {
52+
Header::default()
4953
}
5054

51-
fn unpack_block_header(&mut self) {
52-
self.t = (self.xr_header.type_specific) & 0x0F;
55+
/// destination_ssrc returns an array of ssrc values that this report block refers to.
56+
fn destination_ssrc(&self) -> Vec<u32> {
57+
vec![self.ssrc]
5358
}
5459

5560
fn raw_size(&self) -> usize {
56-
4 + 1 + 4 + 2 + 2 + self.receipt_time.len() * 4
61+
XR_HEADER_LENGTH
62+
+ PACKET_RECEIPT_TIMES_REPORT_BLOCK_MIN_LENGTH as usize
63+
+ self.receipt_time.len() * 4
5764
}
5865

5966
fn as_any(&self) -> &(dyn Any + Send + Sync) {
6067
self
6168
}
62-
fn equal(&self, other: &(dyn ReportBlock + Send + Sync)) -> bool {
69+
fn equal(&self, other: &(dyn Packet + Send + Sync)) -> bool {
6370
other
6471
.as_any()
6572
.downcast_ref::<PacketReceiptTimesReportBlock>()
6673
.map_or(false, |a| self == a)
6774
}
68-
fn cloned(&self) -> Box<dyn ReportBlock + Send + Sync> {
75+
fn cloned(&self) -> Box<dyn Packet + Send + Sync> {
6976
Box::new(self.clone())
7077
}
7178
}
79+
80+
impl MarshalSize for PacketReceiptTimesReportBlock {
81+
fn marshal_size(&self) -> usize {
82+
self.raw_size()
83+
}
84+
}
85+
86+
impl Marshal for PacketReceiptTimesReportBlock {
87+
/// marshal_to encodes the PacketReceiptTimesReportBlock in binary
88+
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
89+
if buf.remaining_mut() < self.marshal_size() {
90+
return Err(error::Error::BufferTooShort.into());
91+
}
92+
93+
let h = self.xr_header();
94+
let n = h.marshal_to(buf)?;
95+
buf = &mut buf[n..];
96+
97+
buf.put_u8(self.t);
98+
buf.put_u32(self.ssrc);
99+
buf.put_u16(self.begin_seq);
100+
buf.put_u16(self.end_seq);
101+
for rt in &self.receipt_time {
102+
buf.put_u32(*rt);
103+
}
104+
105+
Ok(self.marshal_size())
106+
}
107+
}
108+
109+
impl Unmarshal for PacketReceiptTimesReportBlock {
110+
/// Unmarshal decodes the PacketReceiptTimesReportBlock from binary
111+
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
112+
where
113+
Self: Sized,
114+
B: Buf,
115+
{
116+
if raw_packet.remaining() < XR_HEADER_LENGTH {
117+
return Err(error::Error::PacketTooShort.into());
118+
}
119+
120+
let xr_header = XRHeader::unmarshal(raw_packet)?;
121+
122+
if xr_header.block_length < PACKET_RECEIPT_TIMES_REPORT_BLOCK_MIN_LENGTH
123+
|| (xr_header.block_length - PACKET_RECEIPT_TIMES_REPORT_BLOCK_MIN_LENGTH) % 4 != 0
124+
|| raw_packet.remaining() < xr_header.block_length as usize
125+
{
126+
return Err(error::Error::PacketTooShort.into());
127+
}
128+
129+
let t = raw_packet.get_u8();
130+
let ssrc = raw_packet.get_u32();
131+
let begin_seq = raw_packet.get_u16();
132+
let end_seq = raw_packet.get_u16();
133+
134+
let remaining = xr_header.block_length - PACKET_RECEIPT_TIMES_REPORT_BLOCK_MIN_LENGTH;
135+
let mut receipt_time = vec![];
136+
for _ in 0..remaining / 4 {
137+
receipt_time.push(raw_packet.get_u32());
138+
}
139+
140+
Ok(PacketReceiptTimesReportBlock {
141+
t,
142+
ssrc,
143+
begin_seq,
144+
end_seq,
145+
receipt_time,
146+
})
147+
}
148+
}

src/extended_report/unknown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl MarshalSize for UnknownReportBlock {
6060
impl Marshal for UnknownReportBlock {
6161
/// marshal_to encodes the UnknownReportBlock in binary
6262
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
63-
if buf.remaining_mut() < self.raw_size() {
63+
if buf.remaining_mut() < self.marshal_size() {
6464
return Err(error::Error::BufferTooShort.into());
6565
}
6666

0 commit comments

Comments
 (0)