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. 4 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" 15 16 namespace media { 17 18 class AudioFileReaderTest : public testing::Test { 19 public: 20 AudioFileReaderTest() {} 21 virtual ~AudioFileReaderTest() {} 22 23 void Initialize(const char* filename) { 24 data_ = ReadTestDataFile(filename); 25 protocol_.reset(new InMemoryUrlProtocol( 26 data_->data(), data_->data_size(), false)); 27 reader_.reset(new AudioFileReader(protocol_.get())); 28 } 29 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); 37 38 AudioHash audio_hash; 39 audio_hash.Update(decoded_audio_data.get(), actual_frames); 40 EXPECT_EQ(expected_audio_hash, audio_hash.ToString()); 41 } 42 43 void RunTest(const char* fn, const char* hash, int channels, int sample_rate, 44 base::TimeDelta duration, int frames, int trimmed_frames) { 45 Initialize(fn); 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); 52 } 53 54 void RunTestFailingDemux(const char* fn) { 55 Initialize(fn); 56 EXPECT_FALSE(reader_->Open()); 57 } 58 59 void RunTestFailingDecode(const char* fn) { 60 Initialize(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); 65 } 66 67 protected: 68 scoped_refptr<DecoderBuffer> data_; 69 scoped_ptr<InMemoryUrlProtocol> protocol_; 70 scoped_ptr<AudioFileReader> reader_; 71 72 DISALLOW_COPY_AND_ASSIGN(AudioFileReaderTest); 73 }; 74 75 TEST_F(AudioFileReaderTest, WithoutOpen) { 76 Initialize("bear.ogv"); 77 } 78 79 TEST_F(AudioFileReaderTest, InvalidFile) { 80 RunTestFailingDemux("ten_byte_file"); 81 } 82 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); 86 } 87 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); 91 } 92 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); 96 } 97 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); 101 } 102 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); 106 } 107 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); 111 } 112 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); 117 } 118 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); 122 } 123 124 TEST_F(AudioFileReaderTest, MidStreamConfigChangesFail) { 125 RunTestFailingDecode("midstream_config_change.mp3"); 126 } 127 #endif 128 129 TEST_F(AudioFileReaderTest, VorbisInvalidChannelLayout) { 130 RunTestFailingDemux("9ch.ogg"); 131 } 132 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); 136 } 137 138 } // namespace media 139