1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/audio_file_reader.h"
9 #include "base/hash/md5.h"
10 #include "base/time/time.h"
11 #include "build/build_config.h"
12 #include "media/base/audio_bus.h"
13 #include "media/base/audio_hash.h"
14 #include "media/base/decoder_buffer.h"
15 #include "media/base/test_data_util.h"
16 #include "media/ffmpeg/ffmpeg_common.h"
17 #include "media/filters/in_memory_url_protocol.h"
18 #include "media/media_buildflags.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 class AudioFileReaderTest : public testing::Test {
25 AudioFileReaderTest() : packet_verification_disabled_(false) {}
27 AudioFileReaderTest(const AudioFileReaderTest&) = delete;
28 AudioFileReaderTest& operator=(const AudioFileReaderTest&) = delete;
30 ~AudioFileReaderTest() override = default;
32 void Initialize(const char* filename) {
33 data_ = ReadTestDataFile(filename);
34 protocol_ = std::make_unique<InMemoryUrlProtocol>(
35 data_->data(), data_->data_size(), false);
36 reader_ = std::make_unique<AudioFileReader>(protocol_.get());
39 // Reads and the entire file provided to Initialize().
40 void ReadAndVerify(const char* expected_audio_hash, int expected_frames) {
41 std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
42 int actual_frames = reader_->Read(&decoded_audio_packets);
43 std::unique_ptr<AudioBus> decoded_audio_data =
44 AudioBus::Create(reader_->channels(), actual_frames);
45 int dest_start_frame = 0;
46 for (size_t k = 0; k < decoded_audio_packets.size(); ++k) {
47 const AudioBus* packet = decoded_audio_packets[k].get();
48 int frame_count = packet->frames();
49 packet->CopyPartialFramesTo(0, frame_count, dest_start_frame,
50 decoded_audio_data.get());
51 dest_start_frame += frame_count;
53 ASSERT_LE(actual_frames, decoded_audio_data->frames());
54 ASSERT_EQ(expected_frames, actual_frames);
57 audio_hash.Update(decoded_audio_data.get(), actual_frames);
58 EXPECT_EQ(expected_audio_hash, audio_hash.ToString());
61 // Verify packets are consistent across demuxer runs. Reads the first few
62 // packets and then seeks back to the start timestamp and verifies that the
63 // hashes match on the packets just read.
64 void VerifyPackets() {
66 const int kTestPasses = 2;
69 base::TimeDelta start_timestamp;
70 std::vector<std::string> packet_md5_hashes_;
71 for (int i = 0; i < kTestPasses; ++i) {
72 for (int j = 0; j < kReads; ++j) {
73 ASSERT_TRUE(reader_->ReadPacketForTesting(&packet));
75 // On the first pass save the MD5 hash of each packet, on subsequent
76 // passes ensure it matches.
77 const std::string md5_hash = base::MD5String(base::StringPiece(
78 reinterpret_cast<char*>(packet.data), packet.size));
80 packet_md5_hashes_.push_back(md5_hash);
82 start_timestamp = ConvertFromTimeBase(
83 reader_->codec_context_for_testing()->time_base, packet.pts);
86 EXPECT_EQ(packet_md5_hashes_[j], md5_hash) << "j = " << j;
89 av_packet_unref(&packet);
91 ASSERT_TRUE(reader_->SeekForTesting(start_timestamp));
95 void RunTest(const char* fn,
99 base::TimeDelta duration,
101 int expected_frames) {
103 ASSERT_TRUE(reader_->Open());
104 EXPECT_EQ(channels, reader_->channels());
105 EXPECT_EQ(sample_rate, reader_->sample_rate());
107 EXPECT_EQ(duration.InMicroseconds(),
108 reader_->GetDuration().InMicroseconds());
109 EXPECT_EQ(frames, reader_->GetNumberOfFrames());
110 EXPECT_EQ(reader_->HasKnownDuration(), true);
112 EXPECT_EQ(reader_->HasKnownDuration(), false);
114 if (!packet_verification_disabled_)
115 ASSERT_NO_FATAL_FAILURE(VerifyPackets());
116 ReadAndVerify(hash, expected_frames);
119 void RunTestFailingDemux(const char* fn) {
121 EXPECT_FALSE(reader_->Open());
124 void RunTestFailingDecode(const char* fn, int expect_read = 0) {
126 EXPECT_TRUE(reader_->Open());
127 std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
128 EXPECT_EQ(reader_->Read(&decoded_audio_packets), expect_read);
131 void RunTestPartialDecode(const char* fn) {
133 EXPECT_TRUE(reader_->Open());
134 std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
135 constexpr int packets_to_read = 1;
136 reader_->Read(&decoded_audio_packets, packets_to_read);
137 EXPECT_EQ(static_cast<int>(decoded_audio_packets.size()), packets_to_read);
140 void disable_packet_verification() { packet_verification_disabled_ = true; }
143 scoped_refptr<DecoderBuffer> data_;
144 std::unique_ptr<InMemoryUrlProtocol> protocol_;
145 std::unique_ptr<AudioFileReader> reader_;
146 bool packet_verification_disabled_;
149 TEST_F(AudioFileReaderTest, WithoutOpen) {
150 Initialize("bear.ogv");
153 TEST_F(AudioFileReaderTest, InvalidFile) {
154 RunTestFailingDemux("ten_byte_file");
157 TEST_F(AudioFileReaderTest, UnknownDuration) {
158 RunTest("bear-320x240-live.webm", "-3.59,-2.06,-0.43,2.15,0.77,-0.95,", 2,
159 44100, base::Microseconds(-1), -1, 121024);
162 TEST_F(AudioFileReaderTest, WithVideo) {
163 RunTest("bear.ogv", "-0.73,0.92,0.48,-0.07,-0.92,-0.88,", 2, 44100,
164 base::Microseconds(1011520), 44609, 45632);
167 TEST_F(AudioFileReaderTest, Vorbis) {
168 RunTest("sfx.ogg", "2.17,3.31,5.15,6.33,5.97,4.35,", 1, 44100,
169 base::Microseconds(350001), 15436, 15936);
172 TEST_F(AudioFileReaderTest, WaveU8) {
173 RunTest("sfx_u8.wav", "-1.23,-1.57,-1.14,-0.91,-0.87,-0.07,", 1, 44100,
174 base::Microseconds(288414), 12720, 12719);
177 TEST_F(AudioFileReaderTest, WaveS16LE) {
178 RunTest("sfx_s16le.wav", "3.05,2.87,3.00,3.32,3.58,4.08,", 1, 44100,
179 base::Microseconds(288414), 12720, 12719);
182 TEST_F(AudioFileReaderTest, WaveS24LE) {
183 RunTest("sfx_s24le.wav", "3.03,2.86,2.99,3.31,3.57,4.06,", 1, 44100,
184 base::Microseconds(288414), 12720, 12719);
187 TEST_F(AudioFileReaderTest, WaveF32LE) {
188 RunTest("sfx_f32le.wav", "3.03,2.86,2.99,3.31,3.57,4.06,", 1, 44100,
189 base::Microseconds(288414), 12720, 12719);
192 TEST_F(AudioFileReaderTest, MP3) {
193 RunTest("sfx.mp3", "1.30,2.72,4.56,5.08,3.74,2.03,", 1, 44100,
194 base::Microseconds(313470), 13825, 11025);
197 TEST_F(AudioFileReaderTest, CorruptMP3) {
198 // Disable packet verification since the file is corrupt and FFmpeg does not
199 // make any guarantees on packet consistency in this case.
200 disable_packet_verification();
201 RunTest("corrupt.mp3", "-4.95,-2.95,-0.44,1.16,0.31,-2.21,", 1, 44100,
202 base::Microseconds(1018801), 44930, 44928);
205 #if BUILDFLAG(USE_PROPRIETARY_CODECS)
206 TEST_F(AudioFileReaderTest, AAC) {
207 RunTest("sfx.m4a", "0.79,2.31,4.15,4.92,4.04,1.44,", 1, 44100,
208 base::Microseconds(347665), 15333, 12701);
211 TEST_F(AudioFileReaderTest, AAC_SinglePacket) {
212 RunTest("440hz-10ms.m4a", "3.77,4.53,4.75,3.48,3.67,3.76,", 1, 44100,
213 base::Microseconds(69660), 3073, 441);
216 TEST_F(AudioFileReaderTest, AAC_ADTS) {
217 RunTest("sfx.adts", "1.80,1.66,2.31,3.26,4.46,3.36,", 1, 44100,
218 base::Microseconds(384733), 16967, 13312);
221 TEST_F(AudioFileReaderTest, MidStreamConfigChangesFail) {
222 RunTestFailingDecode("midstream_config_change.mp3", 42624);
226 TEST_F(AudioFileReaderTest, VorbisInvalidChannelLayout) {
227 RunTestFailingDemux("9ch.ogg");
230 TEST_F(AudioFileReaderTest, WaveValidFourChannelLayout) {
231 RunTest("4ch.wav", "131.71,38.02,130.31,44.89,135.98,42.52,", 4, 44100,
232 base::Microseconds(100001), 4411, 4410);
235 TEST_F(AudioFileReaderTest, ReadPartialMP3) {
236 RunTestPartialDecode("sfx.mp3");