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 | |
Raphael Kubo da Costa | 7ce3091 | 2018-04-16 11:17:10 +0200 | [diff] [blame] | 11 | #include <string.h> |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 12 | #include <limits> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "common_audio/wav_header.h" |
| 15 | #include "test/gtest.h" |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 16 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | |
| 19 | // Doesn't take ownership of the buffer. |
| 20 | class ReadableWavBuffer : public ReadableWav { |
| 21 | public: |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 22 | ReadableWavBuffer(const uint8_t* buf, size_t size, bool check_read_size) |
| 23 | : buf_(buf), |
| 24 | size_(size), |
| 25 | pos_(0), |
| 26 | buf_exhausted_(false), |
| 27 | check_read_size_(check_read_size) {} |
| 28 | |
Mirko Bonadei | 91df091 | 2018-07-17 11:08:15 +0200 | [diff] [blame] | 29 | ~ReadableWavBuffer() override { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 30 | // Verify the entire buffer has been read. |
| 31 | if (check_read_size_) |
| 32 | EXPECT_EQ(size_, pos_); |
| 33 | } |
| 34 | |
Mirko Bonadei | 91df091 | 2018-07-17 11:08:15 +0200 | [diff] [blame] | 35 | size_t Read(void* buf, size_t num_bytes) override { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 36 | // Verify we don't try to read outside of a properly sized header. |
| 37 | if (size_ >= kWavHeaderSize) |
| 38 | EXPECT_GE(size_, pos_ + num_bytes); |
| 39 | EXPECT_FALSE(buf_exhausted_); |
| 40 | |
| 41 | const size_t bytes_remaining = size_ - pos_; |
| 42 | if (num_bytes > bytes_remaining) { |
| 43 | // The caller is signalled about an exhausted buffer when we return fewer |
| 44 | // bytes than requested. There should not be another read attempt after |
| 45 | // this point. |
| 46 | buf_exhausted_ = true; |
| 47 | num_bytes = bytes_remaining; |
| 48 | } |
| 49 | memcpy(buf, &buf_[pos_], num_bytes); |
| 50 | pos_ += num_bytes; |
| 51 | return num_bytes; |
| 52 | } |
| 53 | |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 54 | bool Eof() const override { return pos_ == size_; } |
| 55 | |
| 56 | bool SeekForward(uint32_t num_bytes) override { |
| 57 | // Verify we don't try to read outside of a properly sized header. |
| 58 | if (size_ >= kWavHeaderSize) |
| 59 | EXPECT_GE(size_, pos_ + num_bytes); |
| 60 | EXPECT_FALSE(buf_exhausted_); |
| 61 | |
| 62 | const size_t bytes_remaining = size_ - pos_; |
| 63 | if (num_bytes > bytes_remaining) { |
| 64 | // Error: cannot seek beyond EOF. |
| 65 | return false; |
| 66 | } |
| 67 | if (num_bytes == bytes_remaining) { |
| 68 | // There should not be another read attempt after this point. |
| 69 | buf_exhausted_ = true; |
| 70 | } |
| 71 | pos_ += num_bytes; |
| 72 | return true; |
| 73 | } |
| 74 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 75 | private: |
| 76 | const uint8_t* buf_; |
| 77 | const size_t size_; |
| 78 | size_t pos_; |
| 79 | bool buf_exhausted_; |
| 80 | const bool check_read_size_; |
| 81 | }; |
| 82 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 83 | // Try various choices of WAV header parameters, and make sure that the good |
| 84 | // ones are accepted and the bad ones rejected. |
| 85 | TEST(WavHeaderTest, CheckWavParameters) { |
| 86 | // Try some really stupid values for one parameter at a time. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 87 | EXPECT_TRUE(CheckWavParameters(1, 8000, kWavFormatPcm, 1, 0)); |
| 88 | EXPECT_FALSE(CheckWavParameters(0, 8000, kWavFormatPcm, 1, 0)); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 89 | EXPECT_FALSE(CheckWavParameters(0x10000, 8000, kWavFormatPcm, 1, 0)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 90 | EXPECT_FALSE(CheckWavParameters(1, 0, kWavFormatPcm, 1, 0)); |
| 91 | EXPECT_FALSE(CheckWavParameters(1, 8000, WavFormat(0), 1, 0)); |
| 92 | EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 0, 0)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 93 | |
| 94 | // Try invalid format/bytes-per-sample combinations. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 95 | EXPECT_TRUE(CheckWavParameters(1, 8000, kWavFormatPcm, 2, 0)); |
| 96 | EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 4, 0)); |
| 97 | EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatALaw, 2, 0)); |
| 98 | EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatMuLaw, 2, 0)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 99 | |
| 100 | // Too large values. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 101 | EXPECT_FALSE(CheckWavParameters(1 << 20, 1 << 20, kWavFormatPcm, 1, 0)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 102 | EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 1, |
| 103 | std::numeric_limits<uint32_t>::max())); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 104 | |
| 105 | // Not the same number of samples for each channel. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 106 | EXPECT_FALSE(CheckWavParameters(3, 8000, kWavFormatPcm, 1, 5)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 109 | TEST(WavHeaderTest, ReadWavHeaderWithErrors) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 110 | size_t num_channels = 0; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 111 | int sample_rate = 0; |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 112 | WavFormat format = kWavFormatPcm; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 113 | size_t bytes_per_sample = 0; |
| 114 | size_t num_samples = 0; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 115 | |
| 116 | // Test a few ways the header can be invalid. We start with the valid header |
| 117 | // used in WriteAndReadWavHeader, and invalidate one field per test. The |
| 118 | // invalid field is indicated in the array name, and in the comments with |
| 119 | // *BAD*. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 120 | { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 121 | constexpr uint8_t kBadRiffID[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 122 | // clang-format off |
| 123 | // clang formatting doesn't respect inline comments. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 124 | 'R', 'i', 'f', 'f', // *BAD* |
| 125 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 126 | 'W', 'A', 'V', 'E', |
| 127 | 'f', 'm', 't', ' ', |
| 128 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 129 | 6, 0, // format: A-law (6) |
| 130 | 17, 0, // channels: 17 |
| 131 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 132 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 133 | 17, 0, // block align: NumChannels * BytesPerSample |
| 134 | 8, 0, // bits per sample: 1 * 8 |
| 135 | 'd', 'a', 't', 'a', |
| 136 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 137 | // clang-format on |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 138 | }; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 139 | ReadableWavBuffer r(kBadRiffID, sizeof(kBadRiffID), |
| 140 | /*check_read_size=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 141 | EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 142 | &bytes_per_sample, &num_samples)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 143 | } |
| 144 | { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 145 | constexpr uint8_t kBadBitsPerSample[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | // clang-format off |
| 147 | // clang formatting doesn't respect inline comments. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 148 | 'R', 'I', 'F', 'F', |
| 149 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 150 | 'W', 'A', 'V', 'E', |
| 151 | 'f', 'm', 't', ' ', |
| 152 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 153 | 6, 0, // format: A-law (6) |
| 154 | 17, 0, // channels: 17 |
| 155 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 156 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 157 | 17, 0, // block align: NumChannels * BytesPerSample |
| 158 | 1, 0, // bits per sample: *BAD* |
| 159 | 'd', 'a', 't', 'a', |
| 160 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 161 | // clang-format on |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 162 | }; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 163 | ReadableWavBuffer r(kBadBitsPerSample, sizeof(kBadBitsPerSample), |
| 164 | /*check_read_size=*/true); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 165 | EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 166 | &bytes_per_sample, &num_samples)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 167 | } |
| 168 | { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 169 | constexpr uint8_t kBadByteRate[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 170 | // clang-format off |
| 171 | // clang formatting doesn't respect inline comments. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 172 | 'R', 'I', 'F', 'F', |
| 173 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 174 | 'W', 'A', 'V', 'E', |
| 175 | 'f', 'm', 't', ' ', |
| 176 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 177 | 6, 0, // format: A-law (6) |
| 178 | 17, 0, // channels: 17 |
| 179 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 180 | 0x00, 0x33, 0x03, 0, // byte rate: *BAD* |
| 181 | 17, 0, // block align: NumChannels * BytesPerSample |
| 182 | 8, 0, // bits per sample: 1 * 8 |
| 183 | 'd', 'a', 't', 'a', |
| 184 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 185 | // clang-format on |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 186 | }; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 187 | ReadableWavBuffer r(kBadByteRate, sizeof(kBadByteRate), |
| 188 | /*check_read_size=*/true); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 189 | EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 190 | &bytes_per_sample, &num_samples)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 191 | } |
| 192 | { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 193 | constexpr uint8_t kBadFmtHeaderSize[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 194 | // clang-format off |
| 195 | // clang formatting doesn't respect inline comments. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 196 | 'R', 'I', 'F', 'F', |
| 197 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 198 | 'W', 'A', 'V', 'E', |
| 199 | 'f', 'm', 't', ' ', |
| 200 | 17, 0, 0, 0, // size of fmt block *BAD*. Only 16 and 18 permitted. |
| 201 | 6, 0, // format: A-law (6) |
| 202 | 17, 0, // channels: 17 |
| 203 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 204 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 205 | 17, 0, // block align: NumChannels * BytesPerSample |
| 206 | 8, 0, // bits per sample: 1 * 8 |
| 207 | 0, // extra (though invalid) header byte |
| 208 | 'd', 'a', 't', 'a', |
| 209 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 210 | // clang-format on |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 211 | }; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 212 | ReadableWavBuffer r(kBadFmtHeaderSize, sizeof(kBadFmtHeaderSize), |
| 213 | /*check_read_size=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 214 | EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 215 | &bytes_per_sample, &num_samples)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 216 | } |
| 217 | { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 218 | constexpr uint8_t kNonZeroExtensionField[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 219 | // clang-format off |
| 220 | // clang formatting doesn't respect inline comments. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 221 | 'R', 'I', 'F', 'F', |
| 222 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 223 | 'W', 'A', 'V', 'E', |
| 224 | 'f', 'm', 't', ' ', |
| 225 | 18, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 226 | 6, 0, // format: A-law (6) |
| 227 | 17, 0, // channels: 17 |
| 228 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 229 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 230 | 17, 0, // block align: NumChannels * BytesPerSample |
| 231 | 8, 0, // bits per sample: 1 * 8 |
| 232 | 1, 0, // non-zero extension field *BAD* |
| 233 | 'd', 'a', 't', 'a', |
| 234 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 235 | // clang-format on |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 236 | }; |
| 237 | ReadableWavBuffer r(kNonZeroExtensionField, sizeof(kNonZeroExtensionField), |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 238 | /*check_read_size=*/false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 239 | EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 240 | &bytes_per_sample, &num_samples)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 241 | } |
| 242 | { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 243 | constexpr uint8_t kMissingDataChunk[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 244 | // clang-format off |
| 245 | // clang formatting doesn't respect inline comments. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 246 | 'R', 'I', 'F', 'F', |
| 247 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 248 | 'W', 'A', 'V', 'E', |
| 249 | 'f', 'm', 't', ' ', |
| 250 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 251 | 6, 0, // format: A-law (6) |
| 252 | 17, 0, // channels: 17 |
| 253 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 254 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 255 | 17, 0, // block align: NumChannels * BytesPerSample |
| 256 | 8, 0, // bits per sample: 1 * 8 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 257 | // clang-format on |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 258 | }; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 259 | ReadableWavBuffer r(kMissingDataChunk, sizeof(kMissingDataChunk), |
| 260 | /*check_read_size=*/true); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 261 | EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 262 | &bytes_per_sample, &num_samples)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 263 | } |
| 264 | { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 265 | constexpr uint8_t kMissingFmtAndDataChunks[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 266 | // clang-format off |
| 267 | // clang formatting doesn't respect inline comments. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 268 | 'R', 'I', 'F', 'F', |
| 269 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 270 | 'W', 'A', 'V', 'E', |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 271 | // clang-format on |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 272 | }; |
| 273 | ReadableWavBuffer r(kMissingFmtAndDataChunks, |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 274 | sizeof(kMissingFmtAndDataChunks), |
| 275 | /*check_read_size=*/true); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 276 | EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 277 | &bytes_per_sample, &num_samples)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 278 | } |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 279 | } |
| 280 | |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 281 | // 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] | 282 | TEST(WavHeaderTest, WriteAndReadWavHeader) { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 283 | constexpr int kSize = 4 + kWavHeaderSize + 4; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 284 | uint8_t buf[kSize]; |
| 285 | memset(buf, 0xa4, sizeof(buf)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 286 | WriteWavHeader(buf + 4, 17, 12345, kWavFormatALaw, 1, 123457689); |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 287 | constexpr uint8_t kExpectedBuf[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 288 | // clang-format off |
| 289 | // clang formatting doesn't respect inline comments. |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 290 | 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes before header |
| 291 | 'R', 'I', 'F', 'F', |
| 292 | 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 |
| 293 | 'W', 'A', 'V', 'E', |
| 294 | 'f', 'm', 't', ' ', |
| 295 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 296 | 6, 0, // format: A-law (6) |
| 297 | 17, 0, // channels: 17 |
| 298 | 0x39, 0x30, 0, 0, // sample rate: 12345 |
| 299 | 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 |
| 300 | 17, 0, // block align: NumChannels * BytesPerSample |
| 301 | 8, 0, // bits per sample: 1 * 8 |
| 302 | 'd', 'a', 't', 'a', |
| 303 | 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 |
| 304 | 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes after header |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 305 | // clang-format on |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 306 | }; |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 307 | static_assert(sizeof(kExpectedBuf) == kSize, "buffer size"); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 308 | EXPECT_EQ(0, memcmp(kExpectedBuf, buf, kSize)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 309 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 310 | size_t num_channels = 0; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 311 | int sample_rate = 0; |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 312 | WavFormat format = kWavFormatPcm; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 313 | size_t bytes_per_sample = 0; |
| 314 | size_t num_samples = 0; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 315 | ReadableWavBuffer r(buf + 4, sizeof(buf) - 8, |
| 316 | /*check_read_size=*/true); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 317 | EXPECT_TRUE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 318 | &bytes_per_sample, &num_samples)); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 319 | EXPECT_EQ(17u, num_channels); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 320 | EXPECT_EQ(12345, sample_rate); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 321 | EXPECT_EQ(kWavFormatALaw, format); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 322 | EXPECT_EQ(1u, bytes_per_sample); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 323 | EXPECT_EQ(123457689u, num_samples); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 324 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 325 | |
| 326 | // Try reading an atypical but valid WAV header and make sure it's parsed OK. |
| 327 | TEST(WavHeaderTest, ReadAtypicalWavHeader) { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 328 | constexpr uint8_t kBuf[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 329 | // clang-format off |
| 330 | // clang formatting doesn't respect inline comments. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 331 | 'R', 'I', 'F', 'F', |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 332 | 0xbf, 0xd0, 0x5b, 0x07, // Size of whole file - 8 + extra 2 bytes of zero |
| 333 | // extension: 123457689 + 44 - 8 + 2 (atypical). |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 334 | 'W', 'A', 'V', 'E', |
| 335 | 'f', 'm', 't', ' ', |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 336 | 18, 0, 0, 0, // Size of fmt block (with an atypical extension |
| 337 | // size field). |
| 338 | 6, 0, // Format: A-law (6). |
| 339 | 17, 0, // Channels: 17. |
| 340 | 0x39, 0x30, 0, 0, // Sample rate: 12345. |
| 341 | 0xc9, 0x33, 0x03, 0, // Byte rate: 1 * 17 * 12345. |
| 342 | 17, 0, // Block align: NumChannels * BytesPerSample. |
| 343 | 8, 0, // Bits per sample: 1 * 8. |
| 344 | 0, 0, // Zero extension size field (atypical). |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 345 | 'd', 'a', 't', 'a', |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 346 | 0x99, 0xd0, 0x5b, 0x07, // Size of payload: 123457689. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 347 | // clang-format on |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 350 | size_t num_channels = 0; |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 351 | int sample_rate = 0; |
| 352 | WavFormat format = kWavFormatPcm; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 353 | size_t bytes_per_sample = 0; |
| 354 | size_t num_samples = 0; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 355 | ReadableWavBuffer r(kBuf, sizeof(kBuf), /*check_read_size=*/true); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 356 | EXPECT_TRUE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 357 | &bytes_per_sample, &num_samples)); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 358 | EXPECT_EQ(17u, num_channels); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 359 | EXPECT_EQ(12345, sample_rate); |
| 360 | EXPECT_EQ(kWavFormatALaw, format); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 361 | EXPECT_EQ(1u, bytes_per_sample); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 362 | EXPECT_EQ(123457689u, num_samples); |
| 363 | } |
| 364 | |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 365 | // Try reading a valid WAV header which contains an optional chunk and make sure |
| 366 | // it's parsed OK. |
| 367 | TEST(WavHeaderTest, ReadWavHeaderWithOptionalChunk) { |
| 368 | constexpr uint8_t kBuf[] = { |
| 369 | // clang-format off |
| 370 | // clang formatting doesn't respect inline comments. |
| 371 | 'R', 'I', 'F', 'F', |
| 372 | 0xcd, 0xd0, 0x5b, 0x07, // Size of whole file - 8 + an extra 16 bytes of |
| 373 | // "metadata" (8 bytes header, 16 bytes payload): |
| 374 | // 123457689 + 44 - 8 + 16. |
| 375 | 'W', 'A', 'V', 'E', |
| 376 | 'f', 'm', 't', ' ', |
| 377 | 16, 0, 0, 0, // Size of fmt block. |
| 378 | 6, 0, // Format: A-law (6). |
| 379 | 17, 0, // Channels: 17. |
| 380 | 0x39, 0x30, 0, 0, // Sample rate: 12345. |
| 381 | 0xc9, 0x33, 0x03, 0, // Byte rate: 1 * 17 * 12345. |
| 382 | 17, 0, // Block align: NumChannels * BytesPerSample. |
| 383 | 8, 0, // Bits per sample: 1 * 8. |
| 384 | 'L', 'I', 'S', 'T', // Metadata chunk ID. |
| 385 | 16, 0, 0, 0, // Metadata chunk payload size. |
| 386 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Metadata (16 bytes). |
| 387 | 'd', 'a', 't', 'a', |
| 388 | 0x99, 0xd0, 0x5b, 0x07, // Size of payload: 123457689. |
| 389 | // clang-format on |
| 390 | }; |
| 391 | |
| 392 | size_t num_channels = 0; |
| 393 | int sample_rate = 0; |
| 394 | WavFormat format = kWavFormatPcm; |
| 395 | size_t bytes_per_sample = 0; |
| 396 | size_t num_samples = 0; |
| 397 | ReadableWavBuffer r(kBuf, sizeof(kBuf), /*check_read_size=*/true); |
| 398 | EXPECT_TRUE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 399 | &bytes_per_sample, &num_samples)); |
| 400 | EXPECT_EQ(17u, num_channels); |
| 401 | EXPECT_EQ(12345, sample_rate); |
| 402 | EXPECT_EQ(kWavFormatALaw, format); |
| 403 | EXPECT_EQ(1u, bytes_per_sample); |
| 404 | EXPECT_EQ(123457689u, num_samples); |
| 405 | } |
| 406 | |
| 407 | // Try reading an invalid WAV header which has the the data chunk before the |
| 408 | // format one and make sure it's not parsed. |
| 409 | TEST(WavHeaderTest, ReadWavHeaderWithDataBeforeFormat) { |
| 410 | constexpr uint8_t kBuf[] = { |
| 411 | // clang-format off |
| 412 | // clang formatting doesn't respect inline comments. |
| 413 | 'R', 'I', 'F', 'F', |
| 414 | 52, 0, 0, 0, // Size of whole file - 8: 16 + 44 - 8. |
| 415 | 'W', 'A', 'V', 'E', |
| 416 | 'd', 'a', 't', 'a', |
| 417 | 16, 0, 0, 0, // Data chunk payload size. |
| 418 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Data 16 bytes. |
| 419 | 'f', 'm', 't', ' ', |
| 420 | 16, 0, 0, 0, // Size of fmt block. |
| 421 | 6, 0, // Format: A-law (6). |
| 422 | 1, 0, // Channels: 1. |
| 423 | 60, 0, 0, 0, // Sample rate: 60. |
| 424 | 60, 0, 0, 0, // Byte rate: 1 * 1 * 60. |
| 425 | 1, 0, // Block align: NumChannels * BytesPerSample. |
| 426 | 8, 0, // Bits per sample: 1 * 8. |
| 427 | // clang-format on |
| 428 | }; |
| 429 | |
| 430 | size_t num_channels = 0; |
| 431 | int sample_rate = 0; |
| 432 | WavFormat format = kWavFormatPcm; |
| 433 | size_t bytes_per_sample = 0; |
| 434 | size_t num_samples = 0; |
| 435 | ReadableWavBuffer r(kBuf, sizeof(kBuf), /*check_read_size=*/false); |
| 436 | EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format, |
| 437 | &bytes_per_sample, &num_samples)); |
| 438 | } |
| 439 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 440 | } // namespace webrtc |