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.
19TEST(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
51// Try writing a WAV header and make sure it looks OK.
52TEST(WavHeaderTest, WriteWavHeader) {
53  static const int kSize = 4 + webrtc::kWavHeaderSize + 4;
54  uint8_t buf[kSize];
55  memset(buf, 0xa4, sizeof(buf));
56  webrtc::WriteWavHeader(
57      buf + 4, 17, 12345, webrtc::kWavFormatALaw, 1, 123457689);
58  static const uint8_t kExpectedBuf[] = {
59    0xa4, 0xa4, 0xa4, 0xa4,  // untouched bytes before header
60    'R', 'I', 'F', 'F',
61    0xbd, 0xd0, 0x5b, 0x07,  // size of whole file - 8: 123457689 + 44 - 8
62    'W', 'A', 'V', 'E',
63    'f', 'm', 't', ' ',
64    16, 0, 0, 0,  // size of fmt block - 8: 24 - 8
65    6, 0,  // format: A-law (6)
66    17, 0,  // channels: 17
67    0x39, 0x30, 0, 0,  // sample rate: 12345
68    0xc9, 0x33, 0x03, 0,  // byte rate: 1 * 17 * 12345
69    17, 0,  // block align: NumChannels * BytesPerSample
70    8, 0,  // bits per sample: 1 * 8
71    'd', 'a', 't', 'a',
72    0x99, 0xd0, 0x5b, 0x07,  // size of payload: 123457689
73    0xa4, 0xa4, 0xa4, 0xa4,  // untouched bytes after header
74  };
75  COMPILE_ASSERT(sizeof(kExpectedBuf) == kSize, buf_size);
76  EXPECT_EQ(0, memcmp(kExpectedBuf, buf, kSize));
77}
78