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