1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/cast/net/rtcp/test_rtcp_packet_builder.h"
#include "base/logging.h"
#include "media/cast/net/rtcp/rtcp_utility.h"
namespace media {
namespace cast {
TestRtcpPacketBuilder::TestRtcpPacketBuilder()
: ptr_of_length_(NULL),
big_endian_writer_(reinterpret_cast<char*>(buffer_), kMaxIpPacketSize),
big_endian_reader_(NULL, 0) {}
void TestRtcpPacketBuilder::AddSr(uint32_t sender_ssrc,
int number_of_report_blocks) {
AddRtcpHeader(200, number_of_report_blocks);
big_endian_writer_.WriteU32(sender_ssrc);
big_endian_writer_.WriteU32(kNtpHigh); // NTP timestamp.
big_endian_writer_.WriteU32(kNtpLow);
big_endian_writer_.WriteU32(kRtpTimestamp);
big_endian_writer_.WriteU32(kSendPacketCount);
big_endian_writer_.WriteU32(kSendOctetCount);
}
void TestRtcpPacketBuilder::AddSrWithNtp(uint32_t sender_ssrc,
uint32_t ntp_high,
uint32_t ntp_low,
uint32_t rtp_timestamp) {
AddRtcpHeader(200, 0);
big_endian_writer_.WriteU32(sender_ssrc);
big_endian_writer_.WriteU32(ntp_high);
big_endian_writer_.WriteU32(ntp_low);
big_endian_writer_.WriteU32(rtp_timestamp);
big_endian_writer_.WriteU32(kSendPacketCount);
big_endian_writer_.WriteU32(kSendOctetCount);
}
void TestRtcpPacketBuilder::AddRr(uint32_t sender_ssrc,
int number_of_report_blocks) {
AddRtcpHeader(201, number_of_report_blocks);
big_endian_writer_.WriteU32(sender_ssrc);
}
void TestRtcpPacketBuilder::AddRb(uint32_t rtp_ssrc) {
big_endian_writer_.WriteU32(rtp_ssrc);
big_endian_writer_.WriteU32(kLoss);
big_endian_writer_.WriteU32(kExtendedMax);
big_endian_writer_.WriteU32(kTestJitter);
big_endian_writer_.WriteU32(kLastSr);
big_endian_writer_.WriteU32(kDelayLastSr);
}
void TestRtcpPacketBuilder::AddXrHeader(uint32_t sender_ssrc) {
AddRtcpHeader(207, 0);
big_endian_writer_.WriteU32(sender_ssrc);
}
void TestRtcpPacketBuilder::AddXrUnknownBlock() {
big_endian_writer_.WriteU8(9); // Block type.
big_endian_writer_.WriteU8(0); // Reserved.
big_endian_writer_.WriteU16(4); // Block length.
// First receiver same as sender of this report.
big_endian_writer_.WriteU32(0);
big_endian_writer_.WriteU32(0);
big_endian_writer_.WriteU32(0);
big_endian_writer_.WriteU32(0);
}
void TestRtcpPacketBuilder::AddUnknownBlock() {
AddRtcpHeader(99, 0);
big_endian_writer_.WriteU32(42);
big_endian_writer_.WriteU32(42);
big_endian_writer_.WriteU32(42);
}
void TestRtcpPacketBuilder::AddXrDlrrBlock(uint32_t sender_ssrc) {
big_endian_writer_.WriteU8(5); // Block type.
big_endian_writer_.WriteU8(0); // Reserved.
big_endian_writer_.WriteU16(3); // Block length.
// First receiver same as sender of this report.
big_endian_writer_.WriteU32(sender_ssrc);
big_endian_writer_.WriteU32(kLastRr);
big_endian_writer_.WriteU32(kDelayLastRr);
}
void TestRtcpPacketBuilder::AddXrExtendedDlrrBlock(uint32_t sender_ssrc) {
big_endian_writer_.WriteU8(5); // Block type.
big_endian_writer_.WriteU8(0); // Reserved.
big_endian_writer_.WriteU16(9); // Block length.
big_endian_writer_.WriteU32(0xaaaaaaaa);
big_endian_writer_.WriteU32(0xaaaaaaaa);
big_endian_writer_.WriteU32(0xaaaaaaaa);
// First receiver same as sender of this report.
big_endian_writer_.WriteU32(sender_ssrc);
big_endian_writer_.WriteU32(kLastRr);
big_endian_writer_.WriteU32(kDelayLastRr);
big_endian_writer_.WriteU32(0xbbbbbbbb);
big_endian_writer_.WriteU32(0xbbbbbbbb);
big_endian_writer_.WriteU32(0xbbbbbbbb);
}
void TestRtcpPacketBuilder::AddXrRrtrBlock() {
big_endian_writer_.WriteU8(4); // Block type.
big_endian_writer_.WriteU8(0); // Reserved.
big_endian_writer_.WriteU16(2); // Block length.
big_endian_writer_.WriteU32(kNtpHigh);
big_endian_writer_.WriteU32(kNtpLow);
}
void TestRtcpPacketBuilder::AddNack(uint32_t sender_ssrc, uint32_t media_ssrc) {
AddRtcpHeader(205, 1);
big_endian_writer_.WriteU32(sender_ssrc);
big_endian_writer_.WriteU32(media_ssrc);
big_endian_writer_.WriteU16(kMissingPacket);
big_endian_writer_.WriteU16(0);
}
void TestRtcpPacketBuilder::AddSendReportRequest(uint32_t sender_ssrc,
uint32_t media_ssrc) {
AddRtcpHeader(205, 5);
big_endian_writer_.WriteU32(sender_ssrc);
big_endian_writer_.WriteU32(media_ssrc);
}
void TestRtcpPacketBuilder::AddCast(uint32_t sender_ssrc,
uint32_t media_ssrc,
base::TimeDelta target_delay) {
AddRtcpHeader(206, 15);
big_endian_writer_.WriteU32(sender_ssrc);
big_endian_writer_.WriteU32(media_ssrc);
big_endian_writer_.WriteU8('C');
big_endian_writer_.WriteU8('A');
big_endian_writer_.WriteU8('S');
big_endian_writer_.WriteU8('T');
big_endian_writer_.WriteU8(kAckFrameId);
big_endian_writer_.WriteU8(3); // Loss fields.
big_endian_writer_.WriteU16(target_delay.InMilliseconds());
big_endian_writer_.WriteU8(kLostFrameId);
big_endian_writer_.WriteU16(kRtcpCastAllPacketsLost);
big_endian_writer_.WriteU8(0); // Lost packet id mask.
big_endian_writer_.WriteU8(kFrameIdWithLostPackets);
big_endian_writer_.WriteU16(kLostPacketId1);
big_endian_writer_.WriteU8(0x2); // Lost packet id mask.
big_endian_writer_.WriteU8(kFrameIdWithLostPackets);
big_endian_writer_.WriteU16(kLostPacketId3);
big_endian_writer_.WriteU8(0); // Lost packet id mask.
}
void TestRtcpPacketBuilder::AddCst2(
const std::vector<uint32_t>& later_received_frames) {
big_endian_writer_.WriteU8('C');
big_endian_writer_.WriteU8('S');
big_endian_writer_.WriteU8('T');
big_endian_writer_.WriteU8('2');
big_endian_writer_.WriteU8(kFeedbackSeq);
std::vector<uint8_t> ack_bitmasks;
for (uint32_t ack_frame : later_received_frames) {
CHECK_LE(kAckFrameId, ack_frame);
const size_t bit_index = ack_frame - kAckFrameId - 1;
const size_t index = bit_index / 8;
const size_t bit_index_within_byte = bit_index % 8;
if (index >= ack_bitmasks.size())
ack_bitmasks.resize(index + 1);
ack_bitmasks[index] |= (1 << (8 - bit_index_within_byte - 1));
}
CHECK_LT(ack_bitmasks.size(), 256u);
big_endian_writer_.WriteU8(ack_bitmasks.size());
for (uint8_t ack_bits : ack_bitmasks)
big_endian_writer_.WriteU8(ack_bits);
// Pad to ensure the extra CST2 data chunk is 32-bit aligned.
for (size_t num_bytes_written = 6 + ack_bitmasks.size();
num_bytes_written % 4; ++num_bytes_written) {
big_endian_writer_.WriteU8(0);
}
}
void TestRtcpPacketBuilder::AddReceiverLog(uint32_t sender_ssrc) {
AddRtcpHeader(204, 2);
big_endian_writer_.WriteU32(sender_ssrc);
big_endian_writer_.WriteU8('C');
big_endian_writer_.WriteU8('A');
big_endian_writer_.WriteU8('S');
big_endian_writer_.WriteU8('T');
}
void TestRtcpPacketBuilder::AddReceiverFrameLog(uint32_t rtp_timestamp,
int num_events,
uint32_t event_timesamp_base) {
big_endian_writer_.WriteU32(rtp_timestamp);
big_endian_writer_.WriteU8(static_cast<uint8_t>(num_events - 1));
big_endian_writer_.WriteU8(static_cast<uint8_t>(event_timesamp_base >> 16));
big_endian_writer_.WriteU8(static_cast<uint8_t>(event_timesamp_base >> 8));
big_endian_writer_.WriteU8(static_cast<uint8_t>(event_timesamp_base));
}
void TestRtcpPacketBuilder::AddReceiverEventLog(uint16_t event_data,
CastLoggingEvent event,
uint16_t event_timesamp_delta) {
big_endian_writer_.WriteU16(event_data);
uint8_t event_id = ConvertEventTypeToWireFormat(event);
uint16_t type_and_delta = static_cast<uint16_t>(event_id) << 12;
type_and_delta += event_timesamp_delta & 0x0fff;
big_endian_writer_.WriteU16(type_and_delta);
}
scoped_ptr<media::cast::Packet> TestRtcpPacketBuilder::GetPacket() {
PatchLengthField();
return scoped_ptr<media::cast::Packet>(
new media::cast::Packet(buffer_, buffer_ + Length()));
}
const uint8_t* TestRtcpPacketBuilder::Data() {
PatchLengthField();
return buffer_;
}
base::BigEndianReader* TestRtcpPacketBuilder::Reader() {
big_endian_reader_ = base::BigEndianReader(
reinterpret_cast<const char *>(Data()), Length());
return &big_endian_reader_;
}
void TestRtcpPacketBuilder::PatchLengthField() {
if (ptr_of_length_) {
// Back-patch the packet length. The client must have taken
// care of proper padding to 32-bit words.
int this_packet_length = (big_endian_writer_.ptr() - ptr_of_length_ - 2);
DCHECK_EQ(0, this_packet_length % 4)
<< "Packets must be a multiple of 32 bits long";
*ptr_of_length_ = this_packet_length >> 10;
*(ptr_of_length_ + 1) = (this_packet_length >> 2) & 0xFF;
ptr_of_length_ = NULL;
}
}
// Set the 5-bit value in the 1st byte of the header
// and the payload type. Set aside room for the length field,
// and make provision for back-patching it.
void TestRtcpPacketBuilder::AddRtcpHeader(int payload, int format_or_count) {
PatchLengthField();
big_endian_writer_.WriteU8(0x80 | (format_or_count & 0x1F));
big_endian_writer_.WriteU8(payload);
ptr_of_length_ = big_endian_writer_.ptr();
// Initialize length to "clearly illegal".
big_endian_writer_.WriteU16(0xDEAD);
}
} // namespace cast
} // namespace media
|