1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "build/build_config.h"
8 #include "media/base/audio_bus.h"
9 #include "media/base/audio_hash.h"
10 #include "media/base/decoder_buffer.h"
11 #include "media/base/test_data_util.h"
12 #include "media/filters/audio_file_reader.h"
13 #include "media/filters/in_memory_url_protocol.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class AudioFileReaderTest
: public testing::Test
{
20 AudioFileReaderTest() {}
21 virtual ~AudioFileReaderTest() {}
23 void Initialize(const char* filename
) {
24 data_
= ReadTestDataFile(filename
);
25 protocol_
.reset(new InMemoryUrlProtocol(
26 data_
->GetData(), data_
->GetDataSize(), false));
27 reader_
.reset(new AudioFileReader(protocol_
.get()));
30 // Reads and the entire file provided to Initialize().
31 void ReadAndVerify(const char* expected_audio_hash
, int expected_frames
) {
32 scoped_ptr
<AudioBus
> decoded_audio_data
= AudioBus::Create(
33 reader_
->channels(), reader_
->number_of_frames());
34 int actual_frames
= reader_
->Read(decoded_audio_data
.get());
35 ASSERT_LE(actual_frames
, decoded_audio_data
->frames());
36 ASSERT_EQ(expected_frames
, actual_frames
);
39 audio_hash
.Update(decoded_audio_data
.get(), actual_frames
);
40 EXPECT_EQ(expected_audio_hash
, audio_hash
.ToString());
43 void RunTest(const char* fn
, const char* hash
, int channels
, int sample_rate
,
44 base::TimeDelta duration
, int frames
, int trimmed_frames
) {
46 ASSERT_TRUE(reader_
->Open());
47 EXPECT_EQ(channels
, reader_
->channels());
48 EXPECT_EQ(sample_rate
, reader_
->sample_rate());
49 EXPECT_EQ(duration
.InMicroseconds(), reader_
->duration().InMicroseconds());
50 EXPECT_EQ(frames
, reader_
->number_of_frames());
51 ReadAndVerify(hash
, trimmed_frames
);
54 void RunTestFailingDemux(const char* fn
) {
56 EXPECT_FALSE(reader_
->Open());
59 void RunTestFailingDecode(const char* fn
) {
61 EXPECT_TRUE(reader_
->Open());
62 scoped_ptr
<AudioBus
> decoded_audio_data
= AudioBus::Create(
63 reader_
->channels(), reader_
->number_of_frames());
64 EXPECT_EQ(reader_
->Read(decoded_audio_data
.get()), 0);
68 scoped_refptr
<DecoderBuffer
> data_
;
69 scoped_ptr
<InMemoryUrlProtocol
> protocol_
;
70 scoped_ptr
<AudioFileReader
> reader_
;
72 DISALLOW_COPY_AND_ASSIGN(AudioFileReaderTest
);
75 TEST_F(AudioFileReaderTest
, WithoutOpen
) {
76 Initialize("bear.ogv");
79 TEST_F(AudioFileReaderTest
, InvalidFile
) {
80 RunTestFailingDemux("ten_byte_file");
83 TEST_F(AudioFileReaderTest
, WithVideo
) {
84 RunTest("bear.ogv", "-2.49,-0.75,0.38,1.60,-0.15,-1.22,", 2, 44100,
85 base::TimeDelta::FromMicroseconds(1011520), 44608, 44608);
88 TEST_F(AudioFileReaderTest
, Vorbis
) {
89 RunTest("sfx.ogg", "4.36,4.81,4.84,4.34,4.61,4.63,", 1, 44100,
90 base::TimeDelta::FromMicroseconds(350001), 15435, 15435);
93 TEST_F(AudioFileReaderTest
, WaveU8
) {
94 RunTest("sfx_u8.wav", "-1.23,-1.57,-1.14,-0.91,-0.87,-0.07,", 1, 44100,
95 base::TimeDelta::FromMicroseconds(288414), 12719, 12719);
98 TEST_F(AudioFileReaderTest
, WaveS16LE
) {
99 RunTest("sfx_s16le.wav", "3.05,2.87,3.00,3.32,3.58,4.08,", 1, 44100,
100 base::TimeDelta::FromMicroseconds(288414), 12719, 12719);
103 TEST_F(AudioFileReaderTest
, WaveS24LE
) {
104 RunTest("sfx_s24le.wav", "3.03,2.86,2.99,3.31,3.57,4.06,", 1, 44100,
105 base::TimeDelta::FromMicroseconds(288414), 12719, 12719);
108 TEST_F(AudioFileReaderTest
, WaveF32LE
) {
109 RunTest("sfx_f32le.wav", "3.03,2.86,2.99,3.31,3.57,4.06,", 1, 44100,
110 base::TimeDelta::FromMicroseconds(288414), 12719, 12719);
113 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_PROPRIETARY_CODECS)
114 TEST_F(AudioFileReaderTest
, MP3
) {
115 RunTest("sfx.mp3", "3.05,2.87,3.00,3.32,3.58,4.08,", 1, 44100,
116 base::TimeDelta::FromMicroseconds(313470), 13824, 12719);
119 TEST_F(AudioFileReaderTest
, AAC
) {
120 RunTest("sfx.m4a", "1.81,1.66,2.32,3.27,4.46,3.36,", 1, 44100,
121 base::TimeDelta::FromMicroseconds(312001), 13759, 13312);
124 TEST_F(AudioFileReaderTest
, MidStreamConfigChangesFail
) {
125 RunTestFailingDecode("midstream_config_change.mp3");
129 TEST_F(AudioFileReaderTest
, VorbisInvalidChannelLayout
) {
130 RunTestFailingDemux("9ch.ogg");
133 TEST_F(AudioFileReaderTest
, WaveValidFourChannelLayout
) {
134 RunTest("4ch.wav", "131.71,38.02,130.31,44.89,135.98,42.52,", 4, 44100,
135 base::TimeDelta::FromMicroseconds(100001), 4410, 4410);