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

Commit 90b2c64

Browse files
author
Rain Liu
committed
add xr test
1 parent 8e60711 commit 90b2c64

File tree

11 files changed

+233
-43
lines changed

11 files changed

+233
-43
lines changed

src/extended_report/dlrr.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,15 @@ impl Unmarshal for DLRRReportBlock {
126126
}
127127

128128
let xr_header = XRHeader::unmarshal(raw_packet)?;
129-
130-
if xr_header.block_length % DLRR_REPORT_LENGTH != 0
131-
|| raw_packet.remaining() < xr_header.block_length as usize
129+
let block_length = xr_header.block_length * 4;
130+
if block_length % DLRR_REPORT_LENGTH != 0 || raw_packet.remaining() < block_length as usize
132131
{
133132
return Err(error::Error::PacketTooShort.into());
134133
}
135134

136135
let mut offset = 0;
137136
let mut reports = vec![];
138-
while offset < xr_header.block_length {
137+
while offset < block_length {
139138
let ssrc = raw_packet.get_u32();
140139
let last_rr = raw_packet.get_u32();
141140
let dlrr = raw_packet.get_u32();
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,184 @@
1+
use super::*;
12

3+
fn decoded_packet() -> ExtendedReport {
4+
ExtendedReport {
5+
sender_ssrc: 0x01020304,
6+
reports: vec![
7+
Box::new(LossRLEReportBlock {
8+
is_loss_rle: true,
9+
t: 12,
10+
11+
ssrc: 0x12345689,
12+
begin_seq: 5,
13+
end_seq: 12,
14+
chunks: vec![Chunk(0x4006), Chunk(0x0006), Chunk(0x8765), Chunk(0x0000)],
15+
}),
16+
Box::new(DuplicateRLEReportBlock {
17+
is_loss_rle: false,
18+
t: 6,
19+
20+
ssrc: 0x12345689,
21+
begin_seq: 5,
22+
end_seq: 12,
23+
chunks: vec![Chunk(0x4123), Chunk(0x3FFF), Chunk(0xFFFF), Chunk(0x0000)],
24+
}),
25+
Box::new(PacketReceiptTimesReportBlock {
26+
t: 3,
27+
28+
ssrc: 0x98765432,
29+
begin_seq: 15432,
30+
end_seq: 15577,
31+
receipt_time: vec![0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555],
32+
}),
33+
Box::new(ReceiverReferenceTimeReportBlock {
34+
ntp_timestamp: 0x0102030405060708,
35+
}),
36+
Box::new(DLRRReportBlock {
37+
reports: vec![
38+
DLRRReport {
39+
ssrc: 0x88888888,
40+
last_rr: 0x12345678,
41+
dlrr: 0x99999999,
42+
},
43+
DLRRReport {
44+
ssrc: 0x09090909,
45+
last_rr: 0x12345678,
46+
dlrr: 0x99999999,
47+
},
48+
DLRRReport {
49+
ssrc: 0x11223344,
50+
last_rr: 0x12345678,
51+
dlrr: 0x99999999,
52+
},
53+
],
54+
}),
55+
Box::new(StatisticsSummaryReportBlock {
56+
loss_reports: true,
57+
duplicate_reports: true,
58+
jitter_reports: true,
59+
ttl_or_hop_limit: TTLorHopLimitType::IPv4,
60+
61+
ssrc: 0xFEDCBA98,
62+
begin_seq: 0x1234,
63+
end_seq: 0x5678,
64+
lost_packets: 0x11111111,
65+
dup_packets: 0x22222222,
66+
min_jitter: 0x33333333,
67+
max_jitter: 0x44444444,
68+
mean_jitter: 0x55555555,
69+
dev_jitter: 0x66666666,
70+
min_ttl_or_hl: 0x01,
71+
max_ttl_or_hl: 0x02,
72+
mean_ttl_or_hl: 0x03,
73+
dev_ttl_or_hl: 0x04,
74+
}),
75+
Box::new(VoIPMetricsReportBlock {
76+
ssrc: 0x89ABCDEF,
77+
loss_rate: 0x05,
78+
discard_rate: 0x06,
79+
burst_density: 0x07,
80+
gap_density: 0x08,
81+
burst_duration: 0x1111,
82+
gap_duration: 0x2222,
83+
round_trip_delay: 0x3333,
84+
end_system_delay: 0x4444,
85+
signal_level: 0x11,
86+
noise_level: 0x22,
87+
rerl: 0x33,
88+
gmin: 0x44,
89+
rfactor: 0x55,
90+
ext_rfactor: 0x66,
91+
mos_lq: 0x77,
92+
mos_cq: 0x88,
93+
rx_config: 0x99,
94+
reserved: 0x00,
95+
jb_nominal: 0x1122,
96+
jb_maximum: 0x3344,
97+
jb_abs_max: 0x5566,
98+
}),
99+
],
100+
}
101+
}
102+
103+
fn encoded_packet() -> Bytes {
104+
Bytes::from_static(&[
105+
// RTP Header
106+
0x80, 0xCF, 0x00, 0x33, // byte 0 - 3
107+
// Sender SSRC
108+
0x01, 0x02, 0x03, 0x04, // Loss RLE Report Block
109+
0x01, 0x0C, 0x00, 0x04, // byte 8 - 11
110+
// Source SSRC
111+
0x12, 0x34, 0x56, 0x89, // Begin & End Seq
112+
0x00, 0x05, 0x00, 0x0C, // byte 16 - 19
113+
// Chunks
114+
0x40, 0x06, 0x00, 0x06, 0x87, 0x65, 0x00, 0x00, // byte 24 - 27
115+
// Duplicate RLE Report Block
116+
0x02, 0x06, 0x00, 0x04, // Source SSRC
117+
0x12, 0x34, 0x56, 0x89, // byte 32 - 35
118+
// Begin & End Seq
119+
0x00, 0x05, 0x00, 0x0C, // Chunks
120+
0x41, 0x23, 0x3F, 0xFF, // byte 40 - 43
121+
0xFF, 0xFF, 0x00, 0x00, // Packet Receipt Times Report Block
122+
0x03, 0x03, 0x00, 0x07, // byte 48 - 51
123+
// Source SSRC
124+
0x98, 0x76, 0x54, 0x32, // Begin & End Seq
125+
0x3C, 0x48, 0x3C, 0xD9, // byte 56 - 59
126+
// Receipt times
127+
0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, // byte 64 - 67
128+
0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, // byte 72 - 75
129+
0x55, 0x55, 0x55, 0x55, // Receiver Reference Time Report
130+
0x04, 0x00, 0x00, 0x02, // byte 80 - 83
131+
// Timestamp
132+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // byte 88 - 91
133+
// DLRR Report
134+
0x05, 0x00, 0x00, 0x09, // SSRC 1
135+
0x88, 0x88, 0x88, 0x88, // byte 96 - 99
136+
// LastRR 1
137+
0x12, 0x34, 0x56, 0x78, // DLRR 1
138+
0x99, 0x99, 0x99, 0x99, // byte 104 - 107
139+
// SSRC 2
140+
0x09, 0x09, 0x09, 0x09, // LastRR 2
141+
0x12, 0x34, 0x56, 0x78, // byte 112 - 115
142+
// DLRR 2
143+
0x99, 0x99, 0x99, 0x99, // SSRC 3
144+
0x11, 0x22, 0x33, 0x44, // byte 120 - 123
145+
// LastRR 3
146+
0x12, 0x34, 0x56, 0x78, // DLRR 3
147+
0x99, 0x99, 0x99, 0x99, // byte 128 - 131
148+
// Statistics Summary Report
149+
0x06, 0xE8, 0x00, 0x09, // SSRC
150+
0xFE, 0xDC, 0xBA, 0x98, // byte 136 - 139
151+
// Various statistics
152+
0x12, 0x34, 0x56, 0x78, 0x11, 0x11, 0x11, 0x11, // byte 144 - 147
153+
0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33, // byte 152 - 155
154+
0x44, 0x44, 0x44, 0x44, 0x55, 0x55, 0x55, 0x55, // byte 160 - 163
155+
0x66, 0x66, 0x66, 0x66, 0x01, 0x02, 0x03, 0x04, // byte 168 - 171
156+
// VoIP Metrics Report
157+
0x07, 0x00, 0x00, 0x08, // SSRC
158+
0x89, 0xAB, 0xCD, 0xEF, // byte 176 - 179
159+
// Various statistics
160+
0x05, 0x06, 0x07, 0x08, 0x11, 0x11, 0x22, 0x22, // byte 184 - 187
161+
0x33, 0x33, 0x44, 0x44, 0x11, 0x22, 0x33, 0x44, // byte 192 - 195
162+
0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, // byte 200 - 203
163+
0x33, 0x44, 0x55, 0x66, // byte 204 - 207
164+
])
165+
}
166+
167+
#[test]
168+
fn test_encode() -> Result<()> {
169+
let expected = encoded_packet();
170+
let packet = decoded_packet();
171+
let actual = packet.marshal()?;
172+
assert_eq!(actual, expected);
173+
Ok(())
174+
}
175+
176+
#[test]
177+
fn test_decode() -> Result<()> {
178+
let mut encoded = encoded_packet();
179+
let expected = decoded_packet();
180+
let actual = ExtendedReport::unmarshal(&mut encoded)?;
181+
assert_eq!(actual, expected);
182+
assert_eq!(actual.to_string(), expected.to_string());
183+
Ok(())
184+
}

src/extended_report/mod.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ pub mod ssr;
99
pub mod unknown;
1010
pub mod vm;
1111

12+
pub use dlrr::{DLRRReport, DLRRReportBlock};
13+
pub use prt::PacketReceiptTimesReportBlock;
14+
pub use rle::{Chunk, ChunkType, DuplicateRLEReportBlock, LossRLEReportBlock, RLEReportBlock};
15+
pub use rrt::ReceiverReferenceTimeReportBlock;
16+
pub use ssr::{StatisticsSummaryReportBlock, TTLorHopLimitType};
1217
pub use unknown::UnknownReportBlock;
18+
pub use vm::VoIPMetricsReportBlock;
1319

1420
use crate::error;
1521
use crate::header::{Header, PacketType, HEADER_LENGTH, SSRC_LENGTH};
@@ -270,19 +276,21 @@ impl Unmarshal for ExtendedReport {
270276

271277
let block_type: BlockType = raw_packet.chunk()[0].into();
272278
let report: Box<dyn Packet + Send + Sync> = match block_type {
273-
/*ReportBlockType::LossRLE=>LossRLEReportBlock::
274-
case DuplicateRLEReportBlockType:
275-
block = new(DuplicateRLEReportBlock)
276-
case PacketReceiptTimesReportBlockType:
277-
block = new(PacketReceiptTimesReportBlock)
278-
case ReceiverReferenceTimeReportBlockType:
279-
block = new(ReceiverReferenceTimeReportBlock)
280-
case DLRRReportBlockType:
281-
block = new(DLRRReportBlock)
282-
case StatisticsSummaryReportBlockType:
283-
block = new(StatisticsSummaryReportBlock)
284-
case VoIPMetricsReportBlockType:
285-
block = new(VoIPMetricsReportBlock)*/
279+
BlockType::LossRLE => Box::new(LossRLEReportBlock::unmarshal(raw_packet)?),
280+
BlockType::DuplicateRLE => {
281+
Box::new(DuplicateRLEReportBlock::unmarshal(raw_packet)?)
282+
}
283+
BlockType::PacketReceiptTimes => {
284+
Box::new(PacketReceiptTimesReportBlock::unmarshal(raw_packet)?)
285+
}
286+
BlockType::ReceiverReferenceTime => {
287+
Box::new(ReceiverReferenceTimeReportBlock::unmarshal(raw_packet)?)
288+
}
289+
BlockType::DLRR => Box::new(DLRRReportBlock::unmarshal(raw_packet)?),
290+
BlockType::StatisticsSummary => {
291+
Box::new(StatisticsSummaryReportBlock::unmarshal(raw_packet)?)
292+
}
293+
BlockType::VoIPMetrics => Box::new(VoIPMetricsReportBlock::unmarshal(raw_packet)?),
286294
_ => Box::new(UnknownReportBlock::unmarshal(raw_packet)?),
287295
};
288296

src/extended_report/prt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ impl Unmarshal for PacketReceiptTimesReportBlock {
118118
}
119119

120120
let xr_header = XRHeader::unmarshal(raw_packet)?;
121-
122-
if xr_header.block_length < PRT_REPORT_BLOCK_MIN_LENGTH
123-
|| (xr_header.block_length - PRT_REPORT_BLOCK_MIN_LENGTH) % 4 != 0
124-
|| raw_packet.remaining() < xr_header.block_length as usize
121+
let block_length = xr_header.block_length * 4;
122+
if block_length < PRT_REPORT_BLOCK_MIN_LENGTH
123+
|| (block_length - PRT_REPORT_BLOCK_MIN_LENGTH) % 4 != 0
124+
|| raw_packet.remaining() < block_length as usize
125125
{
126126
return Err(error::Error::PacketTooShort.into());
127127
}
@@ -132,7 +132,7 @@ impl Unmarshal for PacketReceiptTimesReportBlock {
132132
let begin_seq = raw_packet.get_u16();
133133
let end_seq = raw_packet.get_u16();
134134

135-
let remaining = xr_header.block_length - PRT_REPORT_BLOCK_MIN_LENGTH;
135+
let remaining = block_length - PRT_REPORT_BLOCK_MIN_LENGTH;
136136
let mut receipt_time = vec![];
137137
for _ in 0..remaining / 4 {
138138
receipt_time.push(raw_packet.get_u32());

src/extended_report/rle.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl MarshalSize for RLEReportBlock {
180180
}
181181

182182
impl Marshal for RLEReportBlock {
183-
/// marshal_to encodes the RleReportBlock in binary
183+
/// marshal_to encodes the RLEReportBlock in binary
184184
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
185185
if buf.remaining_mut() < self.marshal_size() {
186186
return Err(error::Error::BufferTooShort.into());
@@ -202,7 +202,7 @@ impl Marshal for RLEReportBlock {
202202
}
203203

204204
impl Unmarshal for RLEReportBlock {
205-
/// Unmarshal decodes the RleReportBlock from binary
205+
/// Unmarshal decodes the RLEReportBlock from binary
206206
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
207207
where
208208
Self: Sized,
@@ -213,10 +213,10 @@ impl Unmarshal for RLEReportBlock {
213213
}
214214

215215
let xr_header = XRHeader::unmarshal(raw_packet)?;
216-
217-
if xr_header.block_length < RLE_REPORT_BLOCK_MIN_LENGTH
218-
|| (xr_header.block_length - RLE_REPORT_BLOCK_MIN_LENGTH) % 2 != 0
219-
|| raw_packet.remaining() < xr_header.block_length as usize
216+
let block_length = xr_header.block_length * 4;
217+
if block_length < RLE_REPORT_BLOCK_MIN_LENGTH
218+
|| (block_length - RLE_REPORT_BLOCK_MIN_LENGTH) % 2 != 0
219+
|| raw_packet.remaining() < block_length as usize
220220
{
221221
return Err(error::Error::PacketTooShort.into());
222222
}
@@ -228,7 +228,7 @@ impl Unmarshal for RLEReportBlock {
228228
let begin_seq = raw_packet.get_u16();
229229
let end_seq = raw_packet.get_u16();
230230

231-
let remaining = xr_header.block_length - RLE_REPORT_BLOCK_MIN_LENGTH;
231+
let remaining = block_length - RLE_REPORT_BLOCK_MIN_LENGTH;
232232
let mut chunks = vec![];
233233
for _ in 0..remaining / 2 {
234234
chunks.push(Chunk(raw_packet.get_u16()));

src/extended_report/rrt.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl MarshalSize for ReceiverReferenceTimeReportBlock {
7070
}
7171

7272
impl Marshal for ReceiverReferenceTimeReportBlock {
73-
/// marshal_to encodes the PacketReceiptTimesReportBlock in binary
73+
/// marshal_to encodes the ReceiverReferenceTimeReportBlock in binary
7474
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
7575
if buf.remaining_mut() < self.marshal_size() {
7676
return Err(error::Error::BufferTooShort.into());
@@ -87,7 +87,7 @@ impl Marshal for ReceiverReferenceTimeReportBlock {
8787
}
8888

8989
impl Unmarshal for ReceiverReferenceTimeReportBlock {
90-
/// Unmarshal decodes the PacketReceiptTimesReportBlock from binary
90+
/// Unmarshal decodes the ReceiverReferenceTimeReportBlock from binary
9191
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
9292
where
9393
Self: Sized,
@@ -98,9 +98,8 @@ impl Unmarshal for ReceiverReferenceTimeReportBlock {
9898
}
9999

100100
let xr_header = XRHeader::unmarshal(raw_packet)?;
101-
102-
if xr_header.block_length != RRT_REPORT_BLOCK_LENGTH
103-
|| raw_packet.remaining() < xr_header.block_length as usize
101+
let block_length = xr_header.block_length * 4;
102+
if block_length != RRT_REPORT_BLOCK_LENGTH || raw_packet.remaining() < block_length as usize
104103
{
105104
return Err(error::Error::PacketTooShort.into());
106105
}

src/extended_report/ssr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ impl Unmarshal for StatisticsSummaryReportBlock {
191191
}
192192

193193
let xr_header = XRHeader::unmarshal(raw_packet)?;
194-
195-
if xr_header.block_length != SSR_REPORT_BLOCK_LENGTH
196-
|| raw_packet.remaining() < xr_header.block_length as usize
194+
let block_length = xr_header.block_length * 4;
195+
if block_length != SSR_REPORT_BLOCK_LENGTH || raw_packet.remaining() < block_length as usize
197196
{
198197
return Err(error::Error::PacketTooShort.into());
199198
}

src/extended_report/unknown.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ impl Unmarshal for UnknownReportBlock {
8686
}
8787

8888
let xr_header = XRHeader::unmarshal(raw_packet)?;
89-
90-
if raw_packet.remaining() < xr_header.block_length as usize {
89+
let block_length = xr_header.block_length * 4;
90+
if raw_packet.remaining() < block_length as usize {
9191
return Err(error::Error::PacketTooShort.into());
9292
}
9393

94-
let bytes = raw_packet.copy_to_bytes(xr_header.block_length as usize);
94+
let bytes = raw_packet.copy_to_bytes(block_length as usize);
9595

9696
Ok(UnknownReportBlock { bytes })
9797
}

src/extended_report/vm.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,8 @@ impl Unmarshal for VoIPMetricsReportBlock {
152152
}
153153

154154
let xr_header = XRHeader::unmarshal(raw_packet)?;
155-
156-
if xr_header.block_length != VM_REPORT_BLOCK_LENGTH
157-
|| raw_packet.remaining() < xr_header.block_length as usize
155+
let block_length = xr_header.block_length * 4;
156+
if block_length != VM_REPORT_BLOCK_LENGTH || raw_packet.remaining() < block_length as usize
158157
{
159158
return Err(error::Error::PacketTooShort.into());
160159
}

src/header.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl From<u8> for PacketType {
6868
204 => PacketType::ApplicationDefined, // RFC 3550, 6.7 (unimplemented)
6969
205 => PacketType::TransportSpecificFeedback, // RFC 4585, 6051
7070
206 => PacketType::PayloadSpecificFeedback, // RFC 4585, 6.3
71+
207 => PacketType::ExtendedReport, // RFC 3611
7172
_ => PacketType::Unsupported,
7273
}
7374
}

0 commit comments

Comments
 (0)