1
1
use super :: * ;
2
2
3
+ const PACKET_RECEIPT_TIMES_REPORT_BLOCK_MIN_LENGTH : u16 = 9 ;
4
+
3
5
/// PacketReceiptTimesReportBlock represents a Packet Receipt Times
4
6
/// report block, as described in RFC 3611 section 4.3.
5
7
///
@@ -22,7 +24,6 @@ use super::*;
22
24
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23
25
#[ derive( Debug , Default , PartialEq , Clone ) ]
24
26
pub struct PacketReceiptTimesReportBlock {
25
- pub xr_header : XRHeader ,
26
27
pub t : u8 ,
27
28
pub ssrc : u32 ,
28
29
pub begin_seq : u16 ,
@@ -36,36 +37,112 @@ impl fmt::Display for PacketReceiptTimesReportBlock {
36
37
}
37
38
}
38
39
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
+ }
43
47
}
48
+ }
44
49
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 ( )
49
53
}
50
54
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]
53
58
}
54
59
55
60
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
57
64
}
58
65
59
66
fn as_any ( & self ) -> & ( dyn Any + Send + Sync ) {
60
67
self
61
68
}
62
- fn equal ( & self , other : & ( dyn ReportBlock + Send + Sync ) ) -> bool {
69
+ fn equal ( & self , other : & ( dyn Packet + Send + Sync ) ) -> bool {
63
70
other
64
71
. as_any ( )
65
72
. downcast_ref :: < PacketReceiptTimesReportBlock > ( )
66
73
. map_or ( false , |a| self == a)
67
74
}
68
- fn cloned ( & self ) -> Box < dyn ReportBlock + Send + Sync > {
75
+ fn cloned ( & self ) -> Box < dyn Packet + Send + Sync > {
69
76
Box :: new ( self . clone ( ) )
70
77
}
71
78
}
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
+ }
0 commit comments