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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
// 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 "base/big_endian.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "chrome/browser/media/webrtc_rtp_dump_writer.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/zlib/zlib.h"
static const size_t kMinimumRtpHeaderLength = 12;
static void CreateFakeRtpPacketHeader(size_t csrc_count,
size_t extension_header_count,
std::vector<uint8>* packet_header) {
packet_header->resize(kMinimumRtpHeaderLength + csrc_count * sizeof(uint32) +
(extension_header_count + 1) * sizeof(uint32));
memset(&(*packet_header)[0], 0, packet_header->size());
// First byte format: vvpxcccc, where 'vv' is the version, 'p' is padding, 'x'
// is the extension bit, 'cccc' is the CSRC count.
(*packet_header)[0] = 0;
(*packet_header)[0] |= (0x2 << 6); // version.
// The extension bit.
(*packet_header)[0] |= (extension_header_count > 0 ? (0x1 << 4) : 0);
(*packet_header)[0] |= (csrc_count & 0xf);
// Set extension length.
size_t offset = kMinimumRtpHeaderLength +
(csrc_count & 0xf) * sizeof(uint32) + sizeof(uint16);
base::WriteBigEndian(reinterpret_cast<char*>(&(*packet_header)[offset]),
static_cast<uint16>(extension_header_count));
}
class WebRtcRtpDumpWriterTest : public testing::Test {
public:
WebRtcRtpDumpWriterTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP |
content::TestBrowserThreadBundle::REAL_FILE_THREAD),
temp_dir_(new base::ScopedTempDir()) {}
virtual void SetUp() {
ASSERT_TRUE(temp_dir_->CreateUniqueTempDir());
incoming_dump_path_ = temp_dir_->path().AppendASCII("rtpdump_recv");
outgoing_dump_path_ = temp_dir_->path().AppendASCII("rtpdump_send");
writer_.reset(new WebRtcRtpDumpWriter(
incoming_dump_path_,
outgoing_dump_path_,
4 * 1024 * 1024,
base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached,
base::Unretained(this))));
}
// Verifies that the dump contains records of |rtp_packet| repeated
// |packet_count| times.
void VerifyDumps(size_t incoming_packet_count, size_t outgoing_packet_count) {
std::string incoming_dump;
std::string outgoing_dump;
if (incoming_packet_count) {
EXPECT_TRUE(base::ReadFileToString(incoming_dump_path_, &incoming_dump));
EXPECT_TRUE(VerifyCompressedDump(&incoming_dump, incoming_packet_count));
} else {
EXPECT_FALSE(base::PathExists(incoming_dump_path_));
}
if (outgoing_packet_count) {
EXPECT_TRUE(base::ReadFileToString(outgoing_dump_path_, &outgoing_dump));
EXPECT_TRUE(VerifyCompressedDump(&outgoing_dump, outgoing_packet_count));
} else {
EXPECT_FALSE(base::PathExists(outgoing_dump_path_));
}
}
MOCK_METHOD2(OnEndDumpDone, void(bool, bool));
MOCK_METHOD0(OnMaxSizeReached, void(void));
protected:
// Verifies the compressed dump file contains the expected number of packets.
bool VerifyCompressedDump(std::string* dump, size_t expected_packet_count) {
EXPECT_GT(dump->size(), 0U);
std::vector<uint8> decompressed_dump;
EXPECT_TRUE(Decompress(dump, &decompressed_dump));
size_t actual_packet_count = 0;
EXPECT_TRUE(ReadDecompressedDump(decompressed_dump, &actual_packet_count));
EXPECT_EQ(expected_packet_count, actual_packet_count);
return true;
}
// Decompresses the |input| into |output|.
bool Decompress(std::string* input, std::vector<uint8>* output) {
z_stream stream = {0};
int result = inflateInit2(&stream, 15 + 16);
EXPECT_EQ(Z_OK, result);
output->resize(input->size() * 100);
stream.next_in =
reinterpret_cast<unsigned char*>(const_cast<char*>(&(*input)[0]));
stream.avail_in = input->size();
stream.next_out = &(*output)[0];
stream.avail_out = output->size();
result = inflate(&stream, Z_FINISH);
DCHECK_EQ(Z_STREAM_END, result);
result = inflateEnd(&stream);
DCHECK_EQ(Z_OK, result);
output->resize(output->size() - stream.avail_out);
return true;
}
// Tries to read |dump| as a rtpplay dump file and returns the number of
// packets found in the dump.
bool ReadDecompressedDump(const std::vector<uint8>& dump,
size_t* packet_count) {
static const char kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n";
static const size_t kDumpFileHeaderSize = 4 * sizeof(uint32);
*packet_count = 0;
size_t dump_pos = 0;
// Verifies the first line.
EXPECT_EQ(memcmp(&dump[0], kFirstLine, arraysize(kFirstLine) - 1), 0);
dump_pos += arraysize(kFirstLine) - 1;
EXPECT_GT(dump.size(), dump_pos);
// Skips the file header.
dump_pos += kDumpFileHeaderSize;
EXPECT_GT(dump.size(), dump_pos);
// Reads each packet dump.
while (dump_pos < dump.size()) {
size_t packet_dump_length = 0;
if (!VerifyPacketDump(&dump[dump_pos],
dump.size() - dump_pos,
&packet_dump_length)) {
DVLOG(0) << "Failed to read the packet dump for packet "
<< *packet_count << ", dump_pos = " << dump_pos
<< ", dump_length = " << dump.size();
return false;
}
EXPECT_GE(dump.size(), dump_pos + packet_dump_length);
dump_pos += packet_dump_length;
(*packet_count)++;
}
return true;
}
// Tries to read one packet dump starting at |dump| and returns the size of
// the packet dump.
bool VerifyPacketDump(const uint8* dump,
size_t dump_length,
size_t* packet_dump_length) {
static const size_t kDumpHeaderLength = 8;
size_t dump_pos = 0;
base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos),
reinterpret_cast<uint16*>(packet_dump_length));
if (*packet_dump_length < kDumpHeaderLength + kMinimumRtpHeaderLength)
return false;
EXPECT_GE(dump_length, *packet_dump_length);
dump_pos += sizeof(uint16);
uint16 rtp_packet_length = 0;
base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos),
&rtp_packet_length);
if (rtp_packet_length < kMinimumRtpHeaderLength)
return false;
dump_pos += sizeof(uint16);
// Skips the elapsed time field.
dump_pos += sizeof(uint32);
return IsValidRtpHeader(dump + dump_pos,
*packet_dump_length - kDumpHeaderLength);
}
// Returns true if |header| is a valid RTP header.
bool IsValidRtpHeader(const uint8* header, size_t length) {
if ((header[0] & 0xC0) != 0x80)
return false;
size_t cc_count = header[0] & 0x0F;
size_t header_length_without_extn = kMinimumRtpHeaderLength + 4 * cc_count;
if (length < header_length_without_extn)
return false;
uint16 extension_count = 0;
base::ReadBigEndian(
reinterpret_cast<const char*>(header + header_length_without_extn + 2),
&extension_count);
if (length < (extension_count + 1) * 4 + header_length_without_extn)
return false;
return true;
}
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<base::ScopedTempDir> temp_dir_;
base::FilePath incoming_dump_path_;
base::FilePath outgoing_dump_path_;
scoped_ptr<WebRtcRtpDumpWriter> writer_;
};
TEST_F(WebRtcRtpDumpWriterTest, NoDumpFileIfNoPacketDumped) {
// The scope is used to make sure the EXPECT_CALL is checked before exiting
// the scope.
{
EXPECT_CALL(*this, OnEndDumpDone(false, false));
writer_->EndDump(RTP_DUMP_BOTH,
base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
base::Unretained(this)));
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
}
EXPECT_FALSE(base::PathExists(incoming_dump_path_));
EXPECT_FALSE(base::PathExists(outgoing_dump_path_));
}
TEST_F(WebRtcRtpDumpWriterTest, WriteAndFlushSmallSizeDump) {
std::vector<uint8> packet_header;
CreateFakeRtpPacketHeader(1, 2, &packet_header);
writer_->WriteRtpPacket(
&packet_header[0], packet_header.size(), 100, true);
writer_->WriteRtpPacket(
&packet_header[0], packet_header.size(), 100, false);
// The scope is used to make sure the EXPECT_CALL is checked before exiting
// the scope.
{
EXPECT_CALL(*this, OnEndDumpDone(true, true));
writer_->EndDump(RTP_DUMP_BOTH,
base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
base::Unretained(this)));
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
}
VerifyDumps(1, 1);
}
TEST_F(WebRtcRtpDumpWriterTest, WriteOverMaxLimit) {
// Reset the writer with a small max size limit.
writer_.reset(new WebRtcRtpDumpWriter(
incoming_dump_path_,
outgoing_dump_path_,
100,
base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached,
base::Unretained(this))));
std::vector<uint8> packet_header;
CreateFakeRtpPacketHeader(3, 4, &packet_header);
const size_t kPacketCount = 200;
// The scope is used to make sure the EXPECT_CALL is checked before exiting
// the scope.
{
EXPECT_CALL(*this, OnMaxSizeReached()).Times(testing::AtLeast(1));
// Write enough packets to overflow the in-memory buffer and max limit.
for (size_t i = 0; i < kPacketCount; ++i) {
writer_->WriteRtpPacket(
&packet_header[0], packet_header.size(), 100, true);
writer_->WriteRtpPacket(
&packet_header[0], packet_header.size(), 100, false);
}
EXPECT_CALL(*this, OnEndDumpDone(true, true));
writer_->EndDump(RTP_DUMP_BOTH,
base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
base::Unretained(this)));
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
}
VerifyDumps(kPacketCount, kPacketCount);
}
TEST_F(WebRtcRtpDumpWriterTest, DestroyWriterBeforeEndDumpCallback) {
EXPECT_CALL(*this, OnEndDumpDone(testing::_, testing::_)).Times(0);
writer_->EndDump(RTP_DUMP_BOTH,
base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
base::Unretained(this)));
writer_.reset();
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
}
TEST_F(WebRtcRtpDumpWriterTest, EndDumpsSeparately) {
std::vector<uint8> packet_header;
CreateFakeRtpPacketHeader(1, 2, &packet_header);
writer_->WriteRtpPacket(
&packet_header[0], packet_header.size(), 100, true);
writer_->WriteRtpPacket(
&packet_header[0], packet_header.size(), 100, true);
writer_->WriteRtpPacket(
&packet_header[0], packet_header.size(), 100, false);
// The scope is used to make sure the EXPECT_CALL is checked before exiting
// the scope.
{
EXPECT_CALL(*this, OnEndDumpDone(true, false));
EXPECT_CALL(*this, OnEndDumpDone(false, true));
writer_->EndDump(RTP_DUMP_INCOMING,
base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
base::Unretained(this)));
writer_->EndDump(RTP_DUMP_OUTGOING,
base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
base::Unretained(this)));
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
base::RunLoop().RunUntilIdle();
}
VerifyDumps(2, 1);
}
|