OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <stddef.h> |
| 6 #include <stdint.h> |
| 7 #include <string.h> |
| 8 |
5 #include "base/big_endian.h" | 9 #include "base/big_endian.h" |
6 #include "base/bind.h" | 10 #include "base/bind.h" |
7 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
9 #include "base/macros.h" | 13 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
11 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
12 #include "chrome/browser/media/webrtc_rtp_dump_writer.h" | 16 #include "chrome/browser/media/webrtc_rtp_dump_writer.h" |
13 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/zlib/zlib.h" | 22 #include "third_party/zlib/zlib.h" |
19 | 23 |
20 static const size_t kMinimumRtpHeaderLength = 12; | 24 static const size_t kMinimumRtpHeaderLength = 12; |
21 | 25 |
22 static void CreateFakeRtpPacketHeader(size_t csrc_count, | 26 static void CreateFakeRtpPacketHeader(size_t csrc_count, |
23 size_t extension_header_count, | 27 size_t extension_header_count, |
24 std::vector<uint8>* packet_header) { | 28 std::vector<uint8_t>* packet_header) { |
25 packet_header->resize(kMinimumRtpHeaderLength + csrc_count * sizeof(uint32) + | 29 packet_header->resize(kMinimumRtpHeaderLength + |
26 (extension_header_count + 1) * sizeof(uint32)); | 30 csrc_count * sizeof(uint32_t) + |
| 31 (extension_header_count + 1) * sizeof(uint32_t)); |
27 | 32 |
28 memset(&(*packet_header)[0], 0, packet_header->size()); | 33 memset(&(*packet_header)[0], 0, packet_header->size()); |
29 | 34 |
30 // First byte format: vvpxcccc, where 'vv' is the version, 'p' is padding, 'x' | 35 // First byte format: vvpxcccc, where 'vv' is the version, 'p' is padding, 'x' |
31 // is the extension bit, 'cccc' is the CSRC count. | 36 // is the extension bit, 'cccc' is the CSRC count. |
32 (*packet_header)[0] = 0; | 37 (*packet_header)[0] = 0; |
33 (*packet_header)[0] |= (0x2 << 6); // version. | 38 (*packet_header)[0] |= (0x2 << 6); // version. |
34 // The extension bit. | 39 // The extension bit. |
35 (*packet_header)[0] |= (extension_header_count > 0 ? (0x1 << 4) : 0); | 40 (*packet_header)[0] |= (extension_header_count > 0 ? (0x1 << 4) : 0); |
36 (*packet_header)[0] |= (csrc_count & 0xf); | 41 (*packet_header)[0] |= (csrc_count & 0xf); |
37 | 42 |
38 // Set extension length. | 43 // Set extension length. |
39 size_t offset = kMinimumRtpHeaderLength + | 44 size_t offset = kMinimumRtpHeaderLength + |
40 (csrc_count & 0xf) * sizeof(uint32) + sizeof(uint16); | 45 (csrc_count & 0xf) * sizeof(uint32_t) + sizeof(uint16_t); |
41 base::WriteBigEndian(reinterpret_cast<char*>(&(*packet_header)[offset]), | 46 base::WriteBigEndian(reinterpret_cast<char*>(&(*packet_header)[offset]), |
42 static_cast<uint16>(extension_header_count)); | 47 static_cast<uint16_t>(extension_header_count)); |
43 } | 48 } |
44 | 49 |
45 class WebRtcRtpDumpWriterTest : public testing::Test { | 50 class WebRtcRtpDumpWriterTest : public testing::Test { |
46 public: | 51 public: |
47 WebRtcRtpDumpWriterTest() | 52 WebRtcRtpDumpWriterTest() |
48 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP | | 53 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP | |
49 content::TestBrowserThreadBundle::REAL_FILE_THREAD), | 54 content::TestBrowserThreadBundle::REAL_FILE_THREAD), |
50 temp_dir_(new base::ScopedTempDir()) {} | 55 temp_dir_(new base::ScopedTempDir()) {} |
51 | 56 |
52 virtual void SetUp() { | 57 virtual void SetUp() { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 } | 89 } |
85 | 90 |
86 MOCK_METHOD2(OnEndDumpDone, void(bool, bool)); | 91 MOCK_METHOD2(OnEndDumpDone, void(bool, bool)); |
87 MOCK_METHOD0(OnMaxSizeReached, void(void)); | 92 MOCK_METHOD0(OnMaxSizeReached, void(void)); |
88 | 93 |
89 protected: | 94 protected: |
90 // Verifies the compressed dump file contains the expected number of packets. | 95 // Verifies the compressed dump file contains the expected number of packets. |
91 bool VerifyCompressedDump(std::string* dump, size_t expected_packet_count) { | 96 bool VerifyCompressedDump(std::string* dump, size_t expected_packet_count) { |
92 EXPECT_GT(dump->size(), 0U); | 97 EXPECT_GT(dump->size(), 0U); |
93 | 98 |
94 std::vector<uint8> decompressed_dump; | 99 std::vector<uint8_t> decompressed_dump; |
95 EXPECT_TRUE(Decompress(dump, &decompressed_dump)); | 100 EXPECT_TRUE(Decompress(dump, &decompressed_dump)); |
96 | 101 |
97 size_t actual_packet_count = 0; | 102 size_t actual_packet_count = 0; |
98 EXPECT_TRUE(ReadDecompressedDump(decompressed_dump, &actual_packet_count)); | 103 EXPECT_TRUE(ReadDecompressedDump(decompressed_dump, &actual_packet_count)); |
99 EXPECT_EQ(expected_packet_count, actual_packet_count); | 104 EXPECT_EQ(expected_packet_count, actual_packet_count); |
100 | 105 |
101 return true; | 106 return true; |
102 } | 107 } |
103 | 108 |
104 // Decompresses the |input| into |output|. | 109 // Decompresses the |input| into |output|. |
105 bool Decompress(std::string* input, std::vector<uint8>* output) { | 110 bool Decompress(std::string* input, std::vector<uint8_t>* output) { |
106 z_stream stream = {0}; | 111 z_stream stream = {0}; |
107 | 112 |
108 int result = inflateInit2(&stream, 15 + 16); | 113 int result = inflateInit2(&stream, 15 + 16); |
109 EXPECT_EQ(Z_OK, result); | 114 EXPECT_EQ(Z_OK, result); |
110 | 115 |
111 output->resize(input->size() * 100); | 116 output->resize(input->size() * 100); |
112 | 117 |
113 stream.next_in = | 118 stream.next_in = |
114 reinterpret_cast<unsigned char*>(const_cast<char*>(&(*input)[0])); | 119 reinterpret_cast<unsigned char*>(const_cast<char*>(&(*input)[0])); |
115 stream.avail_in = input->size(); | 120 stream.avail_in = input->size(); |
116 stream.next_out = &(*output)[0]; | 121 stream.next_out = &(*output)[0]; |
117 stream.avail_out = output->size(); | 122 stream.avail_out = output->size(); |
118 | 123 |
119 result = inflate(&stream, Z_FINISH); | 124 result = inflate(&stream, Z_FINISH); |
120 DCHECK_EQ(Z_STREAM_END, result); | 125 DCHECK_EQ(Z_STREAM_END, result); |
121 result = inflateEnd(&stream); | 126 result = inflateEnd(&stream); |
122 DCHECK_EQ(Z_OK, result); | 127 DCHECK_EQ(Z_OK, result); |
123 | 128 |
124 output->resize(output->size() - stream.avail_out); | 129 output->resize(output->size() - stream.avail_out); |
125 return true; | 130 return true; |
126 } | 131 } |
127 | 132 |
128 // Tries to read |dump| as a rtpplay dump file and returns the number of | 133 // Tries to read |dump| as a rtpplay dump file and returns the number of |
129 // packets found in the dump. | 134 // packets found in the dump. |
130 bool ReadDecompressedDump(const std::vector<uint8>& dump, | 135 bool ReadDecompressedDump(const std::vector<uint8_t>& dump, |
131 size_t* packet_count) { | 136 size_t* packet_count) { |
132 static const char kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n"; | 137 static const char kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n"; |
133 static const size_t kDumpFileHeaderSize = 4 * sizeof(uint32); | 138 static const size_t kDumpFileHeaderSize = 4 * sizeof(uint32_t); |
134 | 139 |
135 *packet_count = 0; | 140 *packet_count = 0; |
136 size_t dump_pos = 0; | 141 size_t dump_pos = 0; |
137 | 142 |
138 // Verifies the first line. | 143 // Verifies the first line. |
139 EXPECT_EQ(memcmp(&dump[0], kFirstLine, arraysize(kFirstLine) - 1), 0); | 144 EXPECT_EQ(memcmp(&dump[0], kFirstLine, arraysize(kFirstLine) - 1), 0); |
140 | 145 |
141 dump_pos += arraysize(kFirstLine) - 1; | 146 dump_pos += arraysize(kFirstLine) - 1; |
142 EXPECT_GT(dump.size(), dump_pos); | 147 EXPECT_GT(dump.size(), dump_pos); |
143 | 148 |
(...skipping 16 matching lines...) Expand all Loading... |
160 EXPECT_GE(dump.size(), dump_pos + packet_dump_length); | 165 EXPECT_GE(dump.size(), dump_pos + packet_dump_length); |
161 dump_pos += packet_dump_length; | 166 dump_pos += packet_dump_length; |
162 | 167 |
163 (*packet_count)++; | 168 (*packet_count)++; |
164 } | 169 } |
165 return true; | 170 return true; |
166 } | 171 } |
167 | 172 |
168 // Tries to read one packet dump starting at |dump| and returns the size of | 173 // Tries to read one packet dump starting at |dump| and returns the size of |
169 // the packet dump. | 174 // the packet dump. |
170 bool VerifyPacketDump(const uint8* dump, | 175 bool VerifyPacketDump(const uint8_t* dump, |
171 size_t dump_length, | 176 size_t dump_length, |
172 size_t* packet_dump_length) { | 177 size_t* packet_dump_length) { |
173 static const size_t kDumpHeaderLength = 8; | 178 static const size_t kDumpHeaderLength = 8; |
174 | 179 |
175 size_t dump_pos = 0; | 180 size_t dump_pos = 0; |
176 base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos), | 181 base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos), |
177 reinterpret_cast<uint16*>(packet_dump_length)); | 182 reinterpret_cast<uint16_t*>(packet_dump_length)); |
178 if (*packet_dump_length < kDumpHeaderLength + kMinimumRtpHeaderLength) | 183 if (*packet_dump_length < kDumpHeaderLength + kMinimumRtpHeaderLength) |
179 return false; | 184 return false; |
180 | 185 |
181 EXPECT_GE(dump_length, *packet_dump_length); | 186 EXPECT_GE(dump_length, *packet_dump_length); |
182 dump_pos += sizeof(uint16); | 187 dump_pos += sizeof(uint16_t); |
183 | 188 |
184 uint16 rtp_packet_length = 0; | 189 uint16_t rtp_packet_length = 0; |
185 base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos), | 190 base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos), |
186 &rtp_packet_length); | 191 &rtp_packet_length); |
187 if (rtp_packet_length < kMinimumRtpHeaderLength) | 192 if (rtp_packet_length < kMinimumRtpHeaderLength) |
188 return false; | 193 return false; |
189 | 194 |
190 dump_pos += sizeof(uint16); | 195 dump_pos += sizeof(uint16_t); |
191 | 196 |
192 // Skips the elapsed time field. | 197 // Skips the elapsed time field. |
193 dump_pos += sizeof(uint32); | 198 dump_pos += sizeof(uint32_t); |
194 | 199 |
195 return IsValidRtpHeader(dump + dump_pos, | 200 return IsValidRtpHeader(dump + dump_pos, |
196 *packet_dump_length - kDumpHeaderLength); | 201 *packet_dump_length - kDumpHeaderLength); |
197 } | 202 } |
198 | 203 |
199 // Returns true if |header| is a valid RTP header. | 204 // Returns true if |header| is a valid RTP header. |
200 bool IsValidRtpHeader(const uint8* header, size_t length) { | 205 bool IsValidRtpHeader(const uint8_t* header, size_t length) { |
201 if ((header[0] & 0xC0) != 0x80) | 206 if ((header[0] & 0xC0) != 0x80) |
202 return false; | 207 return false; |
203 | 208 |
204 size_t cc_count = header[0] & 0x0F; | 209 size_t cc_count = header[0] & 0x0F; |
205 size_t header_length_without_extn = kMinimumRtpHeaderLength + 4 * cc_count; | 210 size_t header_length_without_extn = kMinimumRtpHeaderLength + 4 * cc_count; |
206 | 211 |
207 if (length < header_length_without_extn) | 212 if (length < header_length_without_extn) |
208 return false; | 213 return false; |
209 | 214 |
210 uint16 extension_count = 0; | 215 uint16_t extension_count = 0; |
211 base::ReadBigEndian( | 216 base::ReadBigEndian( |
212 reinterpret_cast<const char*>(header + header_length_without_extn + 2), | 217 reinterpret_cast<const char*>(header + header_length_without_extn + 2), |
213 &extension_count); | 218 &extension_count); |
214 | 219 |
215 if (length < (extension_count + 1) * 4 + header_length_without_extn) | 220 if (length < (extension_count + 1) * 4 + header_length_without_extn) |
216 return false; | 221 return false; |
217 | 222 |
218 return true; | 223 return true; |
219 } | 224 } |
220 | 225 |
(...skipping 17 matching lines...) Expand all Loading... |
238 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 243 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
239 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
240 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 245 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
241 base::RunLoop().RunUntilIdle(); | 246 base::RunLoop().RunUntilIdle(); |
242 } | 247 } |
243 EXPECT_FALSE(base::PathExists(incoming_dump_path_)); | 248 EXPECT_FALSE(base::PathExists(incoming_dump_path_)); |
244 EXPECT_FALSE(base::PathExists(outgoing_dump_path_)); | 249 EXPECT_FALSE(base::PathExists(outgoing_dump_path_)); |
245 } | 250 } |
246 | 251 |
247 TEST_F(WebRtcRtpDumpWriterTest, WriteAndFlushSmallSizeDump) { | 252 TEST_F(WebRtcRtpDumpWriterTest, WriteAndFlushSmallSizeDump) { |
248 std::vector<uint8> packet_header; | 253 std::vector<uint8_t> packet_header; |
249 CreateFakeRtpPacketHeader(1, 2, &packet_header); | 254 CreateFakeRtpPacketHeader(1, 2, &packet_header); |
250 | 255 |
251 writer_->WriteRtpPacket( | 256 writer_->WriteRtpPacket( |
252 &packet_header[0], packet_header.size(), 100, true); | 257 &packet_header[0], packet_header.size(), 100, true); |
253 writer_->WriteRtpPacket( | 258 writer_->WriteRtpPacket( |
254 &packet_header[0], packet_header.size(), 100, false); | 259 &packet_header[0], packet_header.size(), 100, false); |
255 | 260 |
256 // The scope is used to make sure the EXPECT_CALL is checked before exiting | 261 // The scope is used to make sure the EXPECT_CALL is checked before exiting |
257 // the scope. | 262 // the scope. |
258 { | 263 { |
(...skipping 14 matching lines...) Expand all Loading... |
273 | 278 |
274 TEST_F(WebRtcRtpDumpWriterTest, WriteOverMaxLimit) { | 279 TEST_F(WebRtcRtpDumpWriterTest, WriteOverMaxLimit) { |
275 // Reset the writer with a small max size limit. | 280 // Reset the writer with a small max size limit. |
276 writer_.reset(new WebRtcRtpDumpWriter( | 281 writer_.reset(new WebRtcRtpDumpWriter( |
277 incoming_dump_path_, | 282 incoming_dump_path_, |
278 outgoing_dump_path_, | 283 outgoing_dump_path_, |
279 100, | 284 100, |
280 base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached, | 285 base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached, |
281 base::Unretained(this)))); | 286 base::Unretained(this)))); |
282 | 287 |
283 std::vector<uint8> packet_header; | 288 std::vector<uint8_t> packet_header; |
284 CreateFakeRtpPacketHeader(3, 4, &packet_header); | 289 CreateFakeRtpPacketHeader(3, 4, &packet_header); |
285 | 290 |
286 const size_t kPacketCount = 200; | 291 const size_t kPacketCount = 200; |
287 // The scope is used to make sure the EXPECT_CALL is checked before exiting | 292 // The scope is used to make sure the EXPECT_CALL is checked before exiting |
288 // the scope. | 293 // the scope. |
289 { | 294 { |
290 EXPECT_CALL(*this, OnMaxSizeReached()).Times(testing::AtLeast(1)); | 295 EXPECT_CALL(*this, OnMaxSizeReached()).Times(testing::AtLeast(1)); |
291 | 296 |
292 // Write enough packets to overflow the in-memory buffer and max limit. | 297 // Write enough packets to overflow the in-memory buffer and max limit. |
293 for (size_t i = 0; i < kPacketCount; ++i) { | 298 for (size_t i = 0; i < kPacketCount; ++i) { |
(...skipping 27 matching lines...) Expand all Loading... |
321 | 326 |
322 writer_.reset(); | 327 writer_.reset(); |
323 | 328 |
324 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 329 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
325 base::RunLoop().RunUntilIdle(); | 330 base::RunLoop().RunUntilIdle(); |
326 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 331 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
327 base::RunLoop().RunUntilIdle(); | 332 base::RunLoop().RunUntilIdle(); |
328 } | 333 } |
329 | 334 |
330 TEST_F(WebRtcRtpDumpWriterTest, EndDumpsSeparately) { | 335 TEST_F(WebRtcRtpDumpWriterTest, EndDumpsSeparately) { |
331 std::vector<uint8> packet_header; | 336 std::vector<uint8_t> packet_header; |
332 CreateFakeRtpPacketHeader(1, 2, &packet_header); | 337 CreateFakeRtpPacketHeader(1, 2, &packet_header); |
333 | 338 |
334 writer_->WriteRtpPacket( | 339 writer_->WriteRtpPacket( |
335 &packet_header[0], packet_header.size(), 100, true); | 340 &packet_header[0], packet_header.size(), 100, true); |
336 writer_->WriteRtpPacket( | 341 writer_->WriteRtpPacket( |
337 &packet_header[0], packet_header.size(), 100, true); | 342 &packet_header[0], packet_header.size(), 100, true); |
338 writer_->WriteRtpPacket( | 343 writer_->WriteRtpPacket( |
339 &packet_header[0], packet_header.size(), 100, false); | 344 &packet_header[0], packet_header.size(), 100, false); |
340 | 345 |
341 // The scope is used to make sure the EXPECT_CALL is checked before exiting | 346 // The scope is used to make sure the EXPECT_CALL is checked before exiting |
(...skipping 11 matching lines...) Expand all Loading... |
353 base::Unretained(this))); | 358 base::Unretained(this))); |
354 | 359 |
355 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 360 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
356 base::RunLoop().RunUntilIdle(); | 361 base::RunLoop().RunUntilIdle(); |
357 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 362 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
358 base::RunLoop().RunUntilIdle(); | 363 base::RunLoop().RunUntilIdle(); |
359 } | 364 } |
360 | 365 |
361 VerifyDumps(2, 1); | 366 VerifyDumps(2, 1); |
362 } | 367 } |
OLD | NEW |