Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(639)

Side by Side Diff: chrome/browser/media/webrtc_rtp_dump_writer_unittest.cc

Issue 264793017: Implements RTP header dumping. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/big_endian.h"
6 #include "base/bind.h"
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/media/webrtc_rtp_dump_writer.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/public/test/test_utils.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/zlib/zlib.h"
19
20 using content::RtpDumpType;
21 using content::RTP_DUMP_BOTH;
22 using content::RTP_DUMP_INCOMING;
23 using content::RTP_DUMP_OUTGOING;
24
25 static const size_t kMinimumRtpHeaderLength = 12;
26
27 static void CreateFakeRtpPacketHeader(size_t csrc_count,
28 size_t extension_header_count,
29 std::vector<uint8>* packet_header) {
30 packet_header->resize(kMinimumRtpHeaderLength + csrc_count * sizeof(uint32) +
31 (extension_header_count + 1 * sizeof(uint32)));
32
33 // First byte format: vvpxcccc, where 'vv' is the version, 'p' is padding, 'x'
34 // is the extension bit, 'cccc' is the CSRC count.
35 (*packet_header)[0] = 0;
36 (*packet_header)[0] |= (0x2 << 6); // version.
37 // The extension bit.
38 (*packet_header)[0] |= (extension_header_count > 0 ? (0x1 << 4) : 0);
39 (*packet_header)[0] |= (csrc_count & 0xf);
40
41 // Set extension length.
42 size_t offset =
43 kMinimumRtpHeaderLength + (csrc_count & 0xf) * sizeof(uint32) + 2;
44 base::WriteBigEndian(reinterpret_cast<char*>(packet_header + offset),
45 static_cast<uint16>(extension_header_count));
46 }
47
48 class WebRtcRtpDumpWriterTest : public testing::Test {
49 public:
50 WebRtcRtpDumpWriterTest()
51 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP |
52 content::TestBrowserThreadBundle::REAL_FILE_THREAD),
53 temp_dir_(new base::ScopedTempDir()) {}
54
55 virtual void SetUp() {
56 ASSERT_TRUE(temp_dir_->CreateUniqueTempDir());
57
58 incoming_dump_path_ = temp_dir_->path().AppendASCII("rtpdump_recv");
59 outgoing_dump_path_ = temp_dir_->path().AppendASCII("rtpdump_send");
60 writer_.reset(new WebRtcRtpDumpWriter(
61 incoming_dump_path_,
62 outgoing_dump_path_,
63 4 * 1024 * 1024,
64 base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached,
65 base::Unretained(this))));
66 }
67
68 // Verifies that the dump contains records of |rtp_packet| repeated
69 // |packet_count| times.
70 bool VerifyDumps(size_t incoming_packet_count, size_t outgoing_packet_count) {
71 std::string incoming_dump;
72 std::string outgoing_dump;
73
74 if (incoming_packet_count) {
75 EXPECT_TRUE(base::ReadFileToString(incoming_dump_path_, &incoming_dump));
76 EXPECT_TRUE(VerifyCompressedDump(&incoming_dump, incoming_packet_count));
77 } else {
78 EXPECT_FALSE(base::PathExists(incoming_dump_path_));
79 }
80
81 if (outgoing_packet_count) {
82 EXPECT_TRUE(base::ReadFileToString(outgoing_dump_path_, &outgoing_dump));
83 EXPECT_TRUE(VerifyCompressedDump(&outgoing_dump, outgoing_packet_count));
84 } else {
85 EXPECT_FALSE(base::PathExists(outgoing_dump_path_));
86 }
87
88 return true;
89 }
90
91 MOCK_METHOD2(OnEndDumpDone, void(bool, bool));
92 MOCK_METHOD0(OnMaxSizeReached, void(void));
93
94 protected:
95 // Verifies the compressed dump file contains the expected number of packets.
96 bool VerifyCompressedDump(std::string* dump, size_t expected_packet_count) {
97 EXPECT_GT(dump->size(), 0U);
98
99 std::vector<uint8> decompressed_dump;
100 EXPECT_TRUE(Decompress(dump, &decompressed_dump));
101
102 size_t actual_packet_count = 0;
103 EXPECT_TRUE(ReadDecompressedDump(decompressed_dump, &actual_packet_count));
104 EXPECT_EQ(expected_packet_count, actual_packet_count);
105
106 return true;
107 }
108
109 // Decompresses the |input| into |output|.
110 bool Decompress(std::string* input, std::vector<uint8>* output) {
111 z_stream stream = {0};
112
113 int result = inflateInit2(&stream, 15 + 16);
114 EXPECT_EQ(Z_OK, result);
115
116 output->resize(input->size() * 100);
117
118 stream.next_in =
119 reinterpret_cast<unsigned char*>(const_cast<char*>(input->data()));
120 stream.avail_in = input->size();
121 stream.next_out = output->data();
122 stream.avail_out = output->size();
123
124 result = inflate(&stream, Z_FINISH);
125 DCHECK_EQ(Z_STREAM_END, result);
126 result = inflateEnd(&stream);
127 DCHECK_EQ(Z_OK, result);
128
129 output->resize(output->size() - stream.avail_out);
130 return true;
131 }
132
133 // Tries to read |dump| as a rtpplay dump file and returns the number of
134 // packets found in the dump.
135 bool ReadDecompressedDump(const std::vector<uint8>& dump,
136 size_t* packet_count) {
137 static const char kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n";
138 static const size_t kDumpFileHeaderSize = 4 * sizeof(uint32);
139
140 *packet_count = 0;
141 size_t dump_pos = 0;
142
143 // Verifies the first line.
144 EXPECT_EQ(memcmp(dump.data(), kFirstLine, arraysize(kFirstLine) - 1), 0);
145
146 dump_pos += arraysize(kFirstLine) - 1;
147 EXPECT_GT(dump.size(), dump_pos);
148
149 // Skips the file header.
150 dump_pos += kDumpFileHeaderSize;
151 EXPECT_GT(dump.size(), dump_pos);
152
153 // Reads each packet dump.
154 while (dump_pos < dump.size()) {
155 size_t packet_dump_length = 0;
156 if (!VerifyPacketDump(dump.data() + dump_pos,
157 dump.size() - dump_pos,
158 &packet_dump_length)) {
159 DVLOG(0) << "Failed to read the packet dump for packet "
160 << *packet_count << ", dump_pos = " << dump_pos
161 << ", dump_length = " << dump.size();
162 return false;
163 }
164
165 EXPECT_GE(dump.size(), dump_pos + packet_dump_length);
166 dump_pos += packet_dump_length;
167
168 (*packet_count)++;
169 }
170 return true;
171 }
172
173 // Tries to read one packet dump starting at |dump| and returns the size of
174 // the packet dump.
175 bool VerifyPacketDump(const uint8* dump,
176 size_t dump_length,
177 size_t* packet_dump_length) {
178 static const size_t kDumpHeaderLength = 8;
179
180 size_t dump_pos = 0;
181 base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos),
182 reinterpret_cast<uint16*>(packet_dump_length));
183 if (*packet_dump_length < kDumpHeaderLength + kMinimumRtpHeaderLength)
184 return false;
185
186 EXPECT_GE(dump_length, *packet_dump_length);
187 dump_pos += 2;
188
189 uint16 rtp_packet_length = 0;
190 base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos),
191 &rtp_packet_length);
192 if (rtp_packet_length < kMinimumRtpHeaderLength)
193 return false;
194
195 dump_pos += 2;
196
197 // Skips the elapsed time field.
198 dump_pos += 4;
199
200 return IsValidRtpHeader(dump + dump_pos,
201 *packet_dump_length - kDumpHeaderLength);
202 }
203
204 // Returns true if |header| is a valid RTP header.
205 bool IsValidRtpHeader(const uint8* header, size_t length) {
206 if ((header[0] & 0xC0) != 0x80)
207 return false;
208
209 size_t cc_count = header[0] & 0x0F;
210 size_t header_length_without_extn = kMinimumRtpHeaderLength + 4 * cc_count;
211
212 if (length < header_length_without_extn)
213 return false;
214
215 uint16 extension_count = 0;
216 base::ReadBigEndian(
217 reinterpret_cast<const char*>(header + header_length_without_extn + 2),
218 &extension_count);
219
220 if (length < (extension_count + 1) * 4 + header_length_without_extn)
221 return false;
222
223 return true;
224 }
225
226 content::TestBrowserThreadBundle thread_bundle_;
227 scoped_ptr<base::ScopedTempDir> temp_dir_;
228 base::FilePath incoming_dump_path_;
229 base::FilePath outgoing_dump_path_;
230 scoped_ptr<WebRtcRtpDumpWriter> writer_;
231 };
232
233 TEST_F(WebRtcRtpDumpWriterTest, NoDumpFileIfNoPacketDumped) {
234 {
235 EXPECT_CALL(*this, OnEndDumpDone(false, false));
236
237 writer_->EndDump(RTP_DUMP_BOTH,
238 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
239 base::Unretained(this)));
240
241 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
242 base::RunLoop().RunUntilIdle();
243 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
244 base::RunLoop().RunUntilIdle();
245 }
246 EXPECT_FALSE(base::PathExists(incoming_dump_path_));
247 EXPECT_FALSE(base::PathExists(outgoing_dump_path_));
248 }
249
250 TEST_F(WebRtcRtpDumpWriterTest, WriteAndFlushSmallSizeDump) {
251 std::vector<uint8> packet_header;
252 CreateFakeRtpPacketHeader(1, 2, &packet_header);
253
254 writer_->WriteRtpPacket(
255 packet_header.data(), packet_header.size(), 100, true);
256
257 writer_->WriteRtpPacket(
258 packet_header.data(), packet_header.size(), 100, false);
259 {
260 EXPECT_CALL(*this, OnEndDumpDone(true, true));
261
262 writer_->EndDump(RTP_DUMP_BOTH,
263 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
264 base::Unretained(this)));
265
266 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
267 base::RunLoop().RunUntilIdle();
268 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
269 base::RunLoop().RunUntilIdle();
270 }
271
272 EXPECT_TRUE(VerifyDumps(1, 1));
273 }
274
275 TEST_F(WebRtcRtpDumpWriterTest, WriteOverMaxLimit) {
276 // Reset the writer with a small max size limit.
277 writer_.reset(new WebRtcRtpDumpWriter(
278 incoming_dump_path_,
279 outgoing_dump_path_,
280 100,
281 base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached,
282 base::Unretained(this))));
283
284 std::vector<uint8> packet_header;
285 CreateFakeRtpPacketHeader(3, 4, &packet_header);
286
287 const size_t kPacketCount = 200;
288 {
289 EXPECT_CALL(*this, OnMaxSizeReached()).Times(testing::AtLeast(1));
290
291 // Write enough packets to overflow the in-memory buffer and max limit.
292 for (size_t i = 0; i < kPacketCount; ++i) {
293 writer_->WriteRtpPacket(
294 packet_header.data(), packet_header.size(), 100, true);
295
296 writer_->WriteRtpPacket(
297 packet_header.data(), packet_header.size(), 100, false);
298 }
299
300 EXPECT_CALL(*this, OnEndDumpDone(true, true));
301
302 writer_->EndDump(RTP_DUMP_BOTH,
303 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
304 base::Unretained(this)));
305
306 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
307 base::RunLoop().RunUntilIdle();
308 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
309 base::RunLoop().RunUntilIdle();
310 }
311 EXPECT_TRUE(VerifyDumps(kPacketCount, kPacketCount));
312 }
313
314 TEST_F(WebRtcRtpDumpWriterTest, DestroyWriterBeforeEndDumpCallback) {
315 EXPECT_CALL(*this, OnEndDumpDone(testing::_, testing::_)).Times(0);
316
317 writer_->EndDump(RTP_DUMP_BOTH,
318 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
319 base::Unretained(this)));
320
321 writer_.reset();
322
323 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
324 base::RunLoop().RunUntilIdle();
325 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
326 base::RunLoop().RunUntilIdle();
327 }
328
329 TEST_F(WebRtcRtpDumpWriterTest, EndDumpsSeparately) {
330 std::vector<uint8> packet_header;
331 CreateFakeRtpPacketHeader(1, 2, &packet_header);
332
333 writer_->WriteRtpPacket(
334 packet_header.data(), packet_header.size(), 100, true);
335 writer_->WriteRtpPacket(
336 packet_header.data(), packet_header.size(), 100, true);
337
338 writer_->WriteRtpPacket(
339 packet_header.data(), packet_header.size(), 100, false);
340
341 {
342 EXPECT_CALL(*this, OnEndDumpDone(true, false));
343 EXPECT_CALL(*this, OnEndDumpDone(false, true));
344
345 writer_->EndDump(RTP_DUMP_INCOMING,
346 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
347 base::Unretained(this)));
348
349 writer_->EndDump(RTP_DUMP_OUTGOING,
350 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone,
351 base::Unretained(this)));
352
353 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
354 base::RunLoop().RunUntilIdle();
355 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
356 base::RunLoop().RunUntilIdle();
357 }
358
359 EXPECT_TRUE(VerifyDumps(2, 1));
360 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698