| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 bool buf_exhausted_; | 63 bool buf_exhausted_; |
| 64 const bool check_read_size_; | 64 const bool check_read_size_; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 // Try various choices of WAV header parameters, and make sure that the good | 67 // Try various choices of WAV header parameters, and make sure that the good |
| 68 // ones are accepted and the bad ones rejected. | 68 // ones are accepted and the bad ones rejected. |
| 69 TEST(WavHeaderTest, CheckWavParameters) { | 69 TEST(WavHeaderTest, CheckWavParameters) { |
| 70 // Try some really stupid values for one parameter at a time. | 70 // Try some really stupid values for one parameter at a time. |
| 71 EXPECT_TRUE(CheckWavParameters(1, 8000, kWavFormatPcm, 1, 0)); | 71 EXPECT_TRUE(CheckWavParameters(1, 8000, kWavFormatPcm, 1, 0)); |
| 72 EXPECT_FALSE(CheckWavParameters(0, 8000, kWavFormatPcm, 1, 0)); | 72 EXPECT_FALSE(CheckWavParameters(0, 8000, kWavFormatPcm, 1, 0)); |
| 73 EXPECT_FALSE(CheckWavParameters(-1, 8000, kWavFormatPcm, 1, 0)); | 73 EXPECT_FALSE(CheckWavParameters(0x10000, 8000, kWavFormatPcm, 1, 0)); |
| 74 EXPECT_FALSE(CheckWavParameters(1, 0, kWavFormatPcm, 1, 0)); | 74 EXPECT_FALSE(CheckWavParameters(1, 0, kWavFormatPcm, 1, 0)); |
| 75 EXPECT_FALSE(CheckWavParameters(1, 8000, WavFormat(0), 1, 0)); | 75 EXPECT_FALSE(CheckWavParameters(1, 8000, WavFormat(0), 1, 0)); |
| 76 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 0, 0)); | 76 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 0, 0)); |
| 77 | 77 |
| 78 // Try invalid format/bytes-per-sample combinations. | 78 // Try invalid format/bytes-per-sample combinations. |
| 79 EXPECT_TRUE(CheckWavParameters(1, 8000, kWavFormatPcm, 2, 0)); | 79 EXPECT_TRUE(CheckWavParameters(1, 8000, kWavFormatPcm, 2, 0)); |
| 80 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 4, 0)); | 80 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 4, 0)); |
| 81 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatALaw, 2, 0)); | 81 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatALaw, 2, 0)); |
| 82 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatMuLaw, 2, 0)); | 82 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatMuLaw, 2, 0)); |
| 83 | 83 |
| 84 // Too large values. | 84 // Too large values. |
| 85 EXPECT_FALSE(CheckWavParameters(1 << 20, 1 << 20, kWavFormatPcm, 1, 0)); | 85 EXPECT_FALSE(CheckWavParameters(1 << 20, 1 << 20, kWavFormatPcm, 1, 0)); |
| 86 EXPECT_FALSE(CheckWavParameters( | 86 EXPECT_FALSE(CheckWavParameters( |
| 87 1, 8000, kWavFormatPcm, 1, std::numeric_limits<uint32_t>::max())); | 87 1, 8000, kWavFormatPcm, 1, std::numeric_limits<uint32_t>::max())); |
| 88 | 88 |
| 89 // Not the same number of samples for each channel. | 89 // Not the same number of samples for each channel. |
| 90 EXPECT_FALSE(CheckWavParameters(3, 8000, kWavFormatPcm, 1, 5)); | 90 EXPECT_FALSE(CheckWavParameters(3, 8000, kWavFormatPcm, 1, 5)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 TEST(WavHeaderTest, ReadWavHeaderWithErrors) { | 93 TEST(WavHeaderTest, ReadWavHeaderWithErrors) { |
| 94 int num_channels = 0; | 94 int num_channels = 0; |
| 95 int sample_rate = 0; | 95 int sample_rate = 0; |
| 96 WavFormat format = kWavFormatPcm; | 96 WavFormat format = kWavFormatPcm; |
| 97 int bytes_per_sample = 0; | 97 size_t bytes_per_sample = 0; |
| 98 uint32_t num_samples = 0; | 98 size_t num_samples = 0; |
| 99 | 99 |
| 100 // Test a few ways the header can be invalid. We start with the valid header | 100 // Test a few ways the header can be invalid. We start with the valid header |
| 101 // used in WriteAndReadWavHeader, and invalidate one field per test. The | 101 // used in WriteAndReadWavHeader, and invalidate one field per test. The |
| 102 // invalid field is indicated in the array name, and in the comments with | 102 // invalid field is indicated in the array name, and in the comments with |
| 103 // *BAD*. | 103 // *BAD*. |
| 104 { | 104 { |
| 105 static const uint8_t kBadRiffID[] = { | 105 static const uint8_t kBadRiffID[] = { |
| 106 'R', 'i', 'f', 'f', // *BAD* | 106 'R', 'i', 'f', 'f', // *BAD* |
| 107 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 | 107 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 108 'W', 'A', 'V', 'E', | 108 'W', 'A', 'V', 'E', |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 'd', 'a', 't', 'a', | 264 'd', 'a', 't', 'a', |
| 265 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 | 265 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
| 266 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes after header | 266 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes after header |
| 267 }; | 267 }; |
| 268 static_assert(sizeof(kExpectedBuf) == kSize, "buffer size"); | 268 static_assert(sizeof(kExpectedBuf) == kSize, "buffer size"); |
| 269 EXPECT_EQ(0, memcmp(kExpectedBuf, buf, kSize)); | 269 EXPECT_EQ(0, memcmp(kExpectedBuf, buf, kSize)); |
| 270 | 270 |
| 271 int num_channels = 0; | 271 int num_channels = 0; |
| 272 int sample_rate = 0; | 272 int sample_rate = 0; |
| 273 WavFormat format = kWavFormatPcm; | 273 WavFormat format = kWavFormatPcm; |
| 274 int bytes_per_sample = 0; | 274 size_t bytes_per_sample = 0; |
| 275 uint32_t num_samples = 0; | 275 size_t num_samples = 0; |
| 276 ReadableWavBuffer r(buf + 4, sizeof(buf) - 8); | 276 ReadableWavBuffer r(buf + 4, sizeof(buf) - 8); |
| 277 EXPECT_TRUE( | 277 EXPECT_TRUE( |
| 278 ReadWavHeader(&r, &num_channels, &sample_rate, &format, | 278 ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 279 &bytes_per_sample, &num_samples)); | 279 &bytes_per_sample, &num_samples)); |
| 280 EXPECT_EQ(17, num_channels); | 280 EXPECT_EQ(17, num_channels); |
| 281 EXPECT_EQ(12345, sample_rate); | 281 EXPECT_EQ(12345, sample_rate); |
| 282 EXPECT_EQ(kWavFormatALaw, format); | 282 EXPECT_EQ(kWavFormatALaw, format); |
| 283 EXPECT_EQ(1, bytes_per_sample); | 283 EXPECT_EQ(1u, bytes_per_sample); |
| 284 EXPECT_EQ(123457689u, num_samples); | 284 EXPECT_EQ(123457689u, num_samples); |
| 285 } | 285 } |
| 286 | 286 |
| 287 // Try reading an atypical but valid WAV header and make sure it's parsed OK. | 287 // Try reading an atypical but valid WAV header and make sure it's parsed OK. |
| 288 TEST(WavHeaderTest, ReadAtypicalWavHeader) { | 288 TEST(WavHeaderTest, ReadAtypicalWavHeader) { |
| 289 static const uint8_t kBuf[] = { | 289 static const uint8_t kBuf[] = { |
| 290 'R', 'I', 'F', 'F', | 290 'R', 'I', 'F', 'F', |
| 291 0x3d, 0xd1, 0x5b, 0x07, // size of whole file - 8 + an extra 128 bytes of | 291 0x3d, 0xd1, 0x5b, 0x07, // size of whole file - 8 + an extra 128 bytes of |
| 292 // "metadata": 123457689 + 44 - 8 + 128. (atypical) | 292 // "metadata": 123457689 + 44 - 8 + 128. (atypical) |
| 293 'W', 'A', 'V', 'E', | 293 'W', 'A', 'V', 'E', |
| 294 'f', 'm', 't', ' ', | 294 'f', 'm', 't', ' ', |
| 295 18, 0, 0, 0, // size of fmt block (with an atypical extension size field) | 295 18, 0, 0, 0, // size of fmt block (with an atypical extension size field) |
| 296 6, 0, // format: A-law (6) | 296 6, 0, // format: A-law (6) |
| 297 17, 0, // channels: 17 | 297 17, 0, // channels: 17 |
| 298 0x39, 0x30, 0, 0, // sample rate: 12345 | 298 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 299 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 | 299 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 300 17, 0, // block align: NumChannels * BytesPerSample | 300 17, 0, // block align: NumChannels * BytesPerSample |
| 301 8, 0, // bits per sample: 1 * 8 | 301 8, 0, // bits per sample: 1 * 8 |
| 302 0, 0, // zero extension size field (atypical) | 302 0, 0, // zero extension size field (atypical) |
| 303 'd', 'a', 't', 'a', | 303 'd', 'a', 't', 'a', |
| 304 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 | 304 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
| 305 }; | 305 }; |
| 306 | 306 |
| 307 int num_channels = 0; | 307 int num_channels = 0; |
| 308 int sample_rate = 0; | 308 int sample_rate = 0; |
| 309 WavFormat format = kWavFormatPcm; | 309 WavFormat format = kWavFormatPcm; |
| 310 int bytes_per_sample = 0; | 310 size_t bytes_per_sample = 0; |
| 311 uint32_t num_samples = 0; | 311 size_t num_samples = 0; |
| 312 ReadableWavBuffer r(kBuf, sizeof(kBuf)); | 312 ReadableWavBuffer r(kBuf, sizeof(kBuf)); |
| 313 EXPECT_TRUE( | 313 EXPECT_TRUE( |
| 314 ReadWavHeader(&r, &num_channels, &sample_rate, &format, | 314 ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 315 &bytes_per_sample, &num_samples)); | 315 &bytes_per_sample, &num_samples)); |
| 316 EXPECT_EQ(17, num_channels); | 316 EXPECT_EQ(17, num_channels); |
| 317 EXPECT_EQ(12345, sample_rate); | 317 EXPECT_EQ(12345, sample_rate); |
| 318 EXPECT_EQ(kWavFormatALaw, format); | 318 EXPECT_EQ(kWavFormatALaw, format); |
| 319 EXPECT_EQ(1, bytes_per_sample); | 319 EXPECT_EQ(1u, bytes_per_sample); |
| 320 EXPECT_EQ(123457689u, num_samples); | 320 EXPECT_EQ(123457689u, num_samples); |
| 321 } | 321 } |
| 322 | 322 |
| 323 } // namespace webrtc | 323 } // namespace webrtc |
| OLD | NEW |