kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <limits> |
| 12 | |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "webrtc/common_audio/wav_header.h" |
| 15 | #include "webrtc/system_wrappers/interface/compile_assert.h" |
| 16 | |
| 17 | // Try various choices of WAV header parameters, and make sure that the good |
| 18 | // ones are accepted and the bad ones rejected. |
| 19 | TEST(WavHeaderTest, CheckWavParameters) { |
| 20 | // Try some really stupid values for one parameter at a time. |
| 21 | EXPECT_TRUE(webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatPcm, 1, 0)); |
| 22 | EXPECT_FALSE( |
| 23 | webrtc::CheckWavParameters(0, 8000, webrtc::kWavFormatPcm, 1, 0)); |
| 24 | EXPECT_FALSE( |
| 25 | webrtc::CheckWavParameters(-1, 8000, webrtc::kWavFormatPcm, 1, 0)); |
| 26 | EXPECT_FALSE(webrtc::CheckWavParameters(1, 0, webrtc::kWavFormatPcm, 1, 0)); |
| 27 | EXPECT_FALSE(webrtc::CheckWavParameters(1, 8000, webrtc::WavFormat(0), 1, 0)); |
| 28 | EXPECT_FALSE( |
| 29 | webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatPcm, 0, 0)); |
| 30 | |
| 31 | // Try invalid format/bytes-per-sample combinations. |
| 32 | EXPECT_TRUE(webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatPcm, 2, 0)); |
| 33 | EXPECT_FALSE( |
| 34 | webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatPcm, 4, 0)); |
| 35 | EXPECT_FALSE( |
| 36 | webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatALaw, 2, 0)); |
| 37 | EXPECT_FALSE( |
| 38 | webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatMuLaw, 2, 0)); |
| 39 | |
| 40 | // Too large values. |
| 41 | EXPECT_FALSE(webrtc::CheckWavParameters( |
| 42 | 1 << 20, 1 << 20, webrtc::kWavFormatPcm, 1, 0)); |
| 43 | EXPECT_FALSE(webrtc::CheckWavParameters( |
| 44 | 1, 8000, webrtc::kWavFormatPcm, 1, std::numeric_limits<uint32_t>::max())); |
| 45 | |
| 46 | // Not the same number of samples for each channel. |
| 47 | EXPECT_FALSE( |
| 48 | webrtc::CheckWavParameters(3, 8000, webrtc::kWavFormatPcm, 1, 5)); |
| 49 | } |
| 50 | |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 51 | TEST(WavHeaderTest, ReadWavHeaderWithErrors) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 52 | int num_channels = 0; |
| 53 | int sample_rate = 0; |
| 54 | webrtc::WavFormat format = webrtc::kWavFormatPcm; |
| 55 | int bytes_per_sample = 0; |
| 56 | uint32_t num_samples = 0; |
| 57 | |
| 58 | // Test a few ways the header can be invalid. We start with the valid header |
| 59 | // used in WriteAndReadWavHeader, and invalidate one field per test. The |
| 60 | // invalid field is indicated in the array name, and in the comments with |
| 61 | // *BAD*. |
| 62 | static const uint8_t kBadRiffID[] = { |
| 63 | 'R', 'i', 'f', 'f', // *BAD* |
| 64 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 65 | 'W', 'A', 'V', 'E', |
| 66 | 'f', 'm', 't', ' ', |
| 67 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 68 | 6, 0, // format: A-law (6) |
| 69 | 17, 0, // channels: 17 |
| 70 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 71 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 72 | 17, 0, // block align: NumChannels * BytesPerSample |
| 73 | 8, 0, // bits per sample: 1 * 8 |
| 74 | 'd', 'a', 't', 'a', |
| 75 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
| 76 | 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes after header |
| 77 | }; |
| 78 | EXPECT_FALSE( |
| 79 | webrtc::ReadWavHeader(kBadRiffID, &num_channels, &sample_rate, |
| 80 | &format, &bytes_per_sample, &num_samples)); |
| 81 | |
| 82 | static const uint8_t kBadBitsPerSample[] = { |
| 83 | 'R', 'I', 'F', 'F', |
| 84 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 85 | 'W', 'A', 'V', 'E', |
| 86 | 'f', 'm', 't', ' ', |
| 87 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 88 | 6, 0, // format: A-law (6) |
| 89 | 17, 0, // channels: 17 |
| 90 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 91 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 92 | 17, 0, // block align: NumChannels * BytesPerSample |
| 93 | 1, 0, // bits per sample: *BAD* |
| 94 | 'd', 'a', 't', 'a', |
| 95 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
| 96 | 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes after header |
| 97 | }; |
| 98 | EXPECT_FALSE( |
| 99 | webrtc::ReadWavHeader(kBadBitsPerSample, &num_channels, &sample_rate, |
| 100 | &format, &bytes_per_sample, &num_samples)); |
| 101 | |
| 102 | static const uint8_t kBadByteRate[] = { |
| 103 | 'R', 'I', 'F', 'F', |
| 104 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 105 | 'W', 'A', 'V', 'E', |
| 106 | 'f', 'm', 't', ' ', |
| 107 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 108 | 6, 0, // format: A-law (6) |
| 109 | 17, 0, // channels: 17 |
| 110 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 111 | 0x00, 0x33, 0x03, 0, // byte rate: *BAD* |
| 112 | 17, 0, // block align: NumChannels * BytesPerSample |
| 113 | 8, 0, // bits per sample: 1 * 8 |
| 114 | 'd', 'a', 't', 'a', |
| 115 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
| 116 | 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes after header |
| 117 | }; |
| 118 | EXPECT_FALSE( |
| 119 | webrtc::ReadWavHeader(kBadByteRate, &num_channels, &sample_rate, |
| 120 | &format, &bytes_per_sample, &num_samples)); |
| 121 | } |
| 122 | |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 123 | // Try writing and reading a valid WAV header and make sure it looks OK. |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 124 | TEST(WavHeaderTest, WriteAndReadWavHeader) { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 125 | static const int kSize = 4 + webrtc::kWavHeaderSize + 4; |
| 126 | uint8_t buf[kSize]; |
| 127 | memset(buf, 0xa4, sizeof(buf)); |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 128 | webrtc::WriteWavHeader( |
| 129 | buf + 4, 17, 12345, webrtc::kWavFormatALaw, 1, 123457689); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 130 | static const uint8_t kExpectedBuf[] = { |
| 131 | 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes before header |
| 132 | 'R', 'I', 'F', 'F', |
| 133 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 134 | 'W', 'A', 'V', 'E', |
| 135 | 'f', 'm', 't', ' ', |
| 136 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 137 | 6, 0, // format: A-law (6) |
| 138 | 17, 0, // channels: 17 |
| 139 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 140 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 141 | 17, 0, // block align: NumChannels * BytesPerSample |
| 142 | 8, 0, // bits per sample: 1 * 8 |
| 143 | 'd', 'a', 't', 'a', |
| 144 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
| 145 | 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes after header |
| 146 | }; |
| 147 | COMPILE_ASSERT(sizeof(kExpectedBuf) == kSize, buf_size); |
| 148 | EXPECT_EQ(0, memcmp(kExpectedBuf, buf, kSize)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 149 | |
| 150 | int num_channels = 0; |
| 151 | int sample_rate = 0; |
| 152 | webrtc::WavFormat format = webrtc::kWavFormatPcm; |
| 153 | int bytes_per_sample = 0; |
| 154 | uint32_t num_samples = 0; |
| 155 | EXPECT_TRUE( |
| 156 | webrtc::ReadWavHeader(buf + 4, &num_channels, &sample_rate, &format, |
| 157 | &bytes_per_sample, &num_samples)); |
| 158 | EXPECT_EQ(17, num_channels); |
| 159 | EXPECT_EQ(12345, sample_rate); |
| 160 | EXPECT_EQ(webrtc::kWavFormatALaw, format); |
| 161 | EXPECT_EQ(1, bytes_per_sample); |
| 162 | EXPECT_EQ(123457689u, num_samples); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 163 | } |