blob: 2e2eda5a6f2d08f23cfba5dcd49f9c3fc5a7a0dd [file] [log] [blame]
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +00001/*
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 Costa7ce30912018-04-16 11:17:10 +020011#include <string.h>
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000012#include <limits>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "common_audio/wav_header.h"
15#include "test/gtest.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000016
andrew@webrtc.org048c5022014-12-16 20:17:21 +000017namespace webrtc {
18
19// Doesn't take ownership of the buffer.
20class ReadableWavBuffer : public ReadableWav {
21 public:
andrew@webrtc.org048c5022014-12-16 20:17:21 +000022 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 Bonadei91df0912018-07-17 11:08:15 +020029 ~ReadableWavBuffer() override {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000030 // Verify the entire buffer has been read.
31 if (check_read_size_)
32 EXPECT_EQ(size_, pos_);
33 }
34
Mirko Bonadei91df0912018-07-17 11:08:15 +020035 size_t Read(void* buf, size_t num_bytes) override {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000036 // 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 Bazzicaa33c7af2018-11-08 12:16:11 +010054 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.org048c5022014-12-16 20:17:21 +000075 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.org877083c2014-08-20 07:42:46 +000083// Try various choices of WAV header parameters, and make sure that the good
84// ones are accepted and the bad ones rejected.
85TEST(WavHeaderTest, CheckWavParameters) {
86 // Try some really stupid values for one parameter at a time.
andrew@webrtc.org048c5022014-12-16 20:17:21 +000087 EXPECT_TRUE(CheckWavParameters(1, 8000, kWavFormatPcm, 1, 0));
88 EXPECT_FALSE(CheckWavParameters(0, 8000, kWavFormatPcm, 1, 0));
pkasting25702cb2016-01-08 13:50:27 -080089 EXPECT_FALSE(CheckWavParameters(0x10000, 8000, kWavFormatPcm, 1, 0));
andrew@webrtc.org048c5022014-12-16 20:17:21 +000090 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.org877083c2014-08-20 07:42:46 +000093
94 // Try invalid format/bytes-per-sample combinations.
andrew@webrtc.org048c5022014-12-16 20:17:21 +000095 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.org877083c2014-08-20 07:42:46 +000099
100 // Too large values.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000101 EXPECT_FALSE(CheckWavParameters(1 << 20, 1 << 20, kWavFormatPcm, 1, 0));
Yves Gerey665174f2018-06-19 15:03:05 +0200102 EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 1,
103 std::numeric_limits<uint32_t>::max()));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000104
105 // Not the same number of samples for each channel.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000106 EXPECT_FALSE(CheckWavParameters(3, 8000, kWavFormatPcm, 1, 5));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000107}
108
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +0000109TEST(WavHeaderTest, ReadWavHeaderWithErrors) {
Peter Kasting69558702016-01-12 16:26:35 -0800110 size_t num_channels = 0;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000111 int sample_rate = 0;
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000112 WavFormat format = kWavFormatPcm;
pkasting25702cb2016-01-08 13:50:27 -0800113 size_t bytes_per_sample = 0;
114 size_t num_samples = 0;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000115
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.org048c5022014-12-16 20:17:21 +0000120 {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100121 constexpr uint8_t kBadRiffID[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200122 // clang-format off
123 // clang formatting doesn't respect inline comments.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000124 '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 Gerey665174f2018-06-19 15:03:05 +0200137 // clang-format on
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000138 };
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100139 ReadableWavBuffer r(kBadRiffID, sizeof(kBadRiffID),
140 /*check_read_size=*/false);
Yves Gerey665174f2018-06-19 15:03:05 +0200141 EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
142 &bytes_per_sample, &num_samples));
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000143 }
144 {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100145 constexpr uint8_t kBadBitsPerSample[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200146 // clang-format off
147 // clang formatting doesn't respect inline comments.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000148 '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 Gerey665174f2018-06-19 15:03:05 +0200161 // clang-format on
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000162 };
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100163 ReadableWavBuffer r(kBadBitsPerSample, sizeof(kBadBitsPerSample),
164 /*check_read_size=*/true);
Yves Gerey665174f2018-06-19 15:03:05 +0200165 EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
166 &bytes_per_sample, &num_samples));
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000167 }
168 {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100169 constexpr uint8_t kBadByteRate[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200170 // clang-format off
171 // clang formatting doesn't respect inline comments.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000172 '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 Gerey665174f2018-06-19 15:03:05 +0200185 // clang-format on
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000186 };
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100187 ReadableWavBuffer r(kBadByteRate, sizeof(kBadByteRate),
188 /*check_read_size=*/true);
Yves Gerey665174f2018-06-19 15:03:05 +0200189 EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
190 &bytes_per_sample, &num_samples));
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000191 }
192 {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100193 constexpr uint8_t kBadFmtHeaderSize[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200194 // clang-format off
195 // clang formatting doesn't respect inline comments.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000196 '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 Gerey665174f2018-06-19 15:03:05 +0200210 // clang-format on
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000211 };
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100212 ReadableWavBuffer r(kBadFmtHeaderSize, sizeof(kBadFmtHeaderSize),
213 /*check_read_size=*/false);
Yves Gerey665174f2018-06-19 15:03:05 +0200214 EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
215 &bytes_per_sample, &num_samples));
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000216 }
217 {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100218 constexpr uint8_t kNonZeroExtensionField[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200219 // clang-format off
220 // clang formatting doesn't respect inline comments.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000221 '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 Gerey665174f2018-06-19 15:03:05 +0200235 // clang-format on
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000236 };
237 ReadableWavBuffer r(kNonZeroExtensionField, sizeof(kNonZeroExtensionField),
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100238 /*check_read_size=*/false);
Yves Gerey665174f2018-06-19 15:03:05 +0200239 EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
240 &bytes_per_sample, &num_samples));
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000241 }
242 {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100243 constexpr uint8_t kMissingDataChunk[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200244 // clang-format off
245 // clang formatting doesn't respect inline comments.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000246 '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 Gerey665174f2018-06-19 15:03:05 +0200257 // clang-format on
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000258 };
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100259 ReadableWavBuffer r(kMissingDataChunk, sizeof(kMissingDataChunk),
260 /*check_read_size=*/true);
Yves Gerey665174f2018-06-19 15:03:05 +0200261 EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
262 &bytes_per_sample, &num_samples));
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000263 }
264 {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100265 constexpr uint8_t kMissingFmtAndDataChunks[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200266 // clang-format off
267 // clang formatting doesn't respect inline comments.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000268 'R', 'I', 'F', 'F',
269 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8
270 'W', 'A', 'V', 'E',
Yves Gerey665174f2018-06-19 15:03:05 +0200271 // clang-format on
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000272 };
273 ReadableWavBuffer r(kMissingFmtAndDataChunks,
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100274 sizeof(kMissingFmtAndDataChunks),
275 /*check_read_size=*/true);
Yves Gerey665174f2018-06-19 15:03:05 +0200276 EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
277 &bytes_per_sample, &num_samples));
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000278 }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000279}
280
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +0000281// Try writing and reading a valid WAV header and make sure it looks OK.
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000282TEST(WavHeaderTest, WriteAndReadWavHeader) {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100283 constexpr int kSize = 4 + kWavHeaderSize + 4;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000284 uint8_t buf[kSize];
285 memset(buf, 0xa4, sizeof(buf));
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000286 WriteWavHeader(buf + 4, 17, 12345, kWavFormatALaw, 1, 123457689);
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100287 constexpr uint8_t kExpectedBuf[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200288 // clang-format off
289 // clang formatting doesn't respect inline comments.
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000290 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 Gerey665174f2018-06-19 15:03:05 +0200305 // clang-format on
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000306 };
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000307 static_assert(sizeof(kExpectedBuf) == kSize, "buffer size");
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000308 EXPECT_EQ(0, memcmp(kExpectedBuf, buf, kSize));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000309
Peter Kasting69558702016-01-12 16:26:35 -0800310 size_t num_channels = 0;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000311 int sample_rate = 0;
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000312 WavFormat format = kWavFormatPcm;
pkasting25702cb2016-01-08 13:50:27 -0800313 size_t bytes_per_sample = 0;
314 size_t num_samples = 0;
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100315 ReadableWavBuffer r(buf + 4, sizeof(buf) - 8,
316 /*check_read_size=*/true);
Yves Gerey665174f2018-06-19 15:03:05 +0200317 EXPECT_TRUE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
318 &bytes_per_sample, &num_samples));
Peter Kasting69558702016-01-12 16:26:35 -0800319 EXPECT_EQ(17u, num_channels);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000320 EXPECT_EQ(12345, sample_rate);
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000321 EXPECT_EQ(kWavFormatALaw, format);
pkasting25702cb2016-01-08 13:50:27 -0800322 EXPECT_EQ(1u, bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000323 EXPECT_EQ(123457689u, num_samples);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000324}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000325
326// Try reading an atypical but valid WAV header and make sure it's parsed OK.
327TEST(WavHeaderTest, ReadAtypicalWavHeader) {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100328 constexpr uint8_t kBuf[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200329 // clang-format off
330 // clang formatting doesn't respect inline comments.
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000331 'R', 'I', 'F', 'F',
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100332 0xbf, 0xd0, 0x5b, 0x07, // Size of whole file - 8 + extra 2 bytes of zero
333 // extension: 123457689 + 44 - 8 + 2 (atypical).
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000334 'W', 'A', 'V', 'E',
335 'f', 'm', 't', ' ',
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100336 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.org048c5022014-12-16 20:17:21 +0000345 'd', 'a', 't', 'a',
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100346 0x99, 0xd0, 0x5b, 0x07, // Size of payload: 123457689.
Yves Gerey665174f2018-06-19 15:03:05 +0200347 // clang-format on
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000348 };
349
Peter Kasting69558702016-01-12 16:26:35 -0800350 size_t num_channels = 0;
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000351 int sample_rate = 0;
352 WavFormat format = kWavFormatPcm;
pkasting25702cb2016-01-08 13:50:27 -0800353 size_t bytes_per_sample = 0;
354 size_t num_samples = 0;
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100355 ReadableWavBuffer r(kBuf, sizeof(kBuf), /*check_read_size=*/true);
Yves Gerey665174f2018-06-19 15:03:05 +0200356 EXPECT_TRUE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
357 &bytes_per_sample, &num_samples));
Peter Kasting69558702016-01-12 16:26:35 -0800358 EXPECT_EQ(17u, num_channels);
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000359 EXPECT_EQ(12345, sample_rate);
360 EXPECT_EQ(kWavFormatALaw, format);
pkasting25702cb2016-01-08 13:50:27 -0800361 EXPECT_EQ(1u, bytes_per_sample);
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000362 EXPECT_EQ(123457689u, num_samples);
363}
364
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +0100365// Try reading a valid WAV header which contains an optional chunk and make sure
366// it's parsed OK.
367TEST(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.
409TEST(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.org048c5022014-12-16 20:17:21 +0000440} // namespace webrtc