Consolidate and improve audio decoding test for all decoders.

- Consolidates FFmpegAudioDecoder and OpusAudioDecoder unittests
since they were identical anyways.
- Extends the AudioFileReader unittests to perform packet consistency
checks between seeks.
- Extends the new consolidated tests for WAV, FLAC, MP3, and AAC.
- Adds decoded output consistency checks using MD5 for all files.
- Removes old tests which end up duplicating efforts.
- Expands tests for bad decoder configs and buffers w/o timestamps.
- Expands tests to include AudioDiscardHelper usage.

BUG=381356
TEST=shiny new tests!

Review URL: https://codereview.chromium.org/311373004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278667 0039d316-1c4b-4281-b951-d872f2087c98
parent 7896ddcc
This diff is collapsed.
...@@ -28,6 +28,12 @@ AudioFileReader::~AudioFileReader() { ...@@ -28,6 +28,12 @@ AudioFileReader::~AudioFileReader() {
} }
bool AudioFileReader::Open() { bool AudioFileReader::Open() {
if (!OpenDemuxer())
return false;
return OpenDecoder();
}
bool AudioFileReader::OpenDemuxer() {
glue_.reset(new FFmpegGlue(protocol_)); glue_.reset(new FFmpegGlue(protocol_));
AVFormatContext* format_context = glue_->format_context(); AVFormatContext* format_context = glue_->format_context();
...@@ -52,20 +58,21 @@ bool AudioFileReader::Open() { ...@@ -52,20 +58,21 @@ bool AudioFileReader::Open() {
if (!codec_context_) if (!codec_context_)
return false; return false;
int result = avformat_find_stream_info(format_context, NULL); const int result = avformat_find_stream_info(format_context, NULL);
if (result < 0) { DLOG_IF(WARNING, result < 0)
DLOG(WARNING) << "AudioFileReader::Open() : error in avformat_find_stream_info()";
<< "AudioFileReader::Open() : error in avformat_find_stream_info()"; return result >= 0;
return false; }
}
bool AudioFileReader::OpenDecoder() {
AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
if (codec) { if (codec) {
// MP3 decodes to S16P which we don't support, tell it to use S16 instead. // MP3 decodes to S16P which we don't support, tell it to use S16 instead.
if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P)
codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16;
if ((result = avcodec_open2(codec_context_, codec, NULL)) < 0) { const int result = avcodec_open2(codec_context_, codec, NULL);
if (result < 0) {
DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -"
<< " result: " << result; << " result: " << result;
return false; return false;
...@@ -79,8 +86,7 @@ bool AudioFileReader::Open() { ...@@ -79,8 +86,7 @@ bool AudioFileReader::Open() {
return false; return false;
} }
} else { } else {
DLOG(WARNING) << "AudioFileReader::Open() : could not find codec -" DLOG(WARNING) << "AudioFileReader::Open() : could not find codec.";
<< " result: " << result;
return false; return false;
} }
...@@ -96,7 +102,6 @@ bool AudioFileReader::Open() { ...@@ -96,7 +102,6 @@ bool AudioFileReader::Open() {
channels_ = codec_context_->channels; channels_ = codec_context_->channels;
sample_rate_ = codec_context_->sample_rate; sample_rate_ = codec_context_->sample_rate;
av_sample_format_ = codec_context_->sample_fmt; av_sample_format_ = codec_context_->sample_fmt;
return true; return true;
} }
...@@ -240,6 +245,10 @@ int AudioFileReader::GetNumberOfFrames() const { ...@@ -240,6 +245,10 @@ int AudioFileReader::GetNumberOfFrames() const {
return static_cast<int>(ceil(GetDuration().InSecondsF() * sample_rate())); return static_cast<int>(ceil(GetDuration().InSecondsF() * sample_rate()));
} }
bool AudioFileReader::OpenDemuxerForTesting() {
return OpenDemuxer();
}
bool AudioFileReader::ReadPacketForTesting(AVPacket* output_packet) { bool AudioFileReader::ReadPacketForTesting(AVPacket* output_packet) {
return ReadPacket(output_packet); return ReadPacket(output_packet);
} }
...@@ -257,4 +266,15 @@ bool AudioFileReader::ReadPacket(AVPacket* output_packet) { ...@@ -257,4 +266,15 @@ bool AudioFileReader::ReadPacket(AVPacket* output_packet) {
return false; return false;
} }
bool AudioFileReader::SeekForTesting(base::TimeDelta seek_time) {
return av_seek_frame(glue_->format_context(),
stream_index_,
ConvertToTimeBase(codec_context_->time_base, seek_time),
AVSEEK_FLAG_BACKWARD) >= 0;
}
const AVStream* AudioFileReader::GetAVStreamForTesting() const {
return glue_->format_context()->streams[stream_index_];
}
} // namespace media } // namespace media
...@@ -8,16 +8,17 @@ ...@@ -8,16 +8,17 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "media/base/media_export.h" #include "media/base/media_export.h"
#include "media/filters/ffmpeg_glue.h"
struct AVCodecContext; struct AVCodecContext;
struct AVPacket; struct AVPacket;
struct AVStream;
namespace base { class TimeDelta; } namespace base { class TimeDelta; }
namespace media { namespace media {
class AudioBus; class AudioBus;
class FFmpegGlue;
class FFmpegURLProtocol; class FFmpegURLProtocol;
class MEDIA_EXPORT AudioFileReader { class MEDIA_EXPORT AudioFileReader {
...@@ -55,17 +56,29 @@ class MEDIA_EXPORT AudioFileReader { ...@@ -55,17 +56,29 @@ class MEDIA_EXPORT AudioFileReader {
base::TimeDelta GetDuration() const; base::TimeDelta GetDuration() const;
int GetNumberOfFrames() const; int GetNumberOfFrames() const;
// Helper methods which allows AudioFileReader to double as a test utility for // The methods below are helper methods which allow AudioFileReader to double
// demuxing audio files. Returns true if a packet could be demuxed from the // as a test utility for demuxing audio files.
// first audio stream in the file, |output_packet| will contain the demuxed // --------------------------------------------------------------------------
// packet then.
// Similar to Open() but does not initialize the decoder.
bool OpenDemuxerForTesting();
// Returns true if a packet could be demuxed from the first audio stream in
// the file, |output_packet| will contain the demuxed packet then.
bool ReadPacketForTesting(AVPacket* output_packet); bool ReadPacketForTesting(AVPacket* output_packet);
// Seeks to the given point and returns true if successful. |seek_time| will
// be converted to the stream's time base automatically.
bool SeekForTesting(base::TimeDelta seek_time);
const AVStream* GetAVStreamForTesting() const;
const AVCodecContext* codec_context_for_testing() const { const AVCodecContext* codec_context_for_testing() const {
return codec_context_; return codec_context_;
} }
private: private:
bool OpenDemuxer();
bool OpenDecoder();
bool ReadPacket(AVPacket* output_packet); bool ReadPacket(AVPacket* output_packet);
scoped_ptr<FFmpegGlue> glue_; scoped_ptr<FFmpegGlue> glue_;
......
...@@ -3,12 +3,14 @@ ...@@ -3,12 +3,14 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "base/logging.h" #include "base/logging.h"
#include "base/md5.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "media/base/audio_bus.h" #include "media/base/audio_bus.h"
#include "media/base/audio_hash.h" #include "media/base/audio_hash.h"
#include "media/base/decoder_buffer.h" #include "media/base/decoder_buffer.h"
#include "media/base/test_data_util.h" #include "media/base/test_data_util.h"
#include "media/ffmpeg/ffmpeg_common.h"
#include "media/filters/audio_file_reader.h" #include "media/filters/audio_file_reader.h"
#include "media/filters/in_memory_url_protocol.h" #include "media/filters/in_memory_url_protocol.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -17,20 +19,20 @@ namespace media { ...@@ -17,20 +19,20 @@ namespace media {
class AudioFileReaderTest : public testing::Test { class AudioFileReaderTest : public testing::Test {
public: public:
AudioFileReaderTest() {} AudioFileReaderTest() : packet_verification_disabled_(false) {}
virtual ~AudioFileReaderTest() {} virtual ~AudioFileReaderTest() {}
void Initialize(const char* filename) { void Initialize(const char* filename) {
data_ = ReadTestDataFile(filename); data_ = ReadTestDataFile(filename);
protocol_.reset(new InMemoryUrlProtocol( protocol_.reset(
data_->data(), data_->data_size(), false)); new InMemoryUrlProtocol(data_->data(), data_->data_size(), false));
reader_.reset(new AudioFileReader(protocol_.get())); reader_.reset(new AudioFileReader(protocol_.get()));
} }
// Reads and the entire file provided to Initialize(). // Reads and the entire file provided to Initialize().
void ReadAndVerify(const char* expected_audio_hash, int expected_frames) { void ReadAndVerify(const char* expected_audio_hash, int expected_frames) {
scoped_ptr<AudioBus> decoded_audio_data = AudioBus::Create( scoped_ptr<AudioBus> decoded_audio_data =
reader_->channels(), reader_->GetNumberOfFrames()); AudioBus::Create(reader_->channels(), reader_->GetNumberOfFrames());
int actual_frames = reader_->Read(decoded_audio_data.get()); int actual_frames = reader_->Read(decoded_audio_data.get());
ASSERT_LE(actual_frames, decoded_audio_data->frames()); ASSERT_LE(actual_frames, decoded_audio_data->frames());
ASSERT_EQ(expected_frames, actual_frames); ASSERT_EQ(expected_frames, actual_frames);
...@@ -40,8 +42,50 @@ class AudioFileReaderTest : public testing::Test { ...@@ -40,8 +42,50 @@ class AudioFileReaderTest : public testing::Test {
EXPECT_EQ(expected_audio_hash, audio_hash.ToString()); EXPECT_EQ(expected_audio_hash, audio_hash.ToString());
} }
void RunTest(const char* fn, const char* hash, int channels, int sample_rate, // Verify packets are consistent across demuxer runs. Reads the first few
base::TimeDelta duration, int frames, int trimmed_frames) { // packets and then seeks back to the start timestamp and verifies that the
// hashes match on the packets just read.
void VerifyPackets() {
const int kReads = 3;
const int kTestPasses = 2;
AVPacket packet;
base::TimeDelta start_timestamp;
std::vector<std::string> packet_md5_hashes_;
for (int i = 0; i < kTestPasses; ++i) {
for (int j = 0; j < kReads; ++j) {
ASSERT_TRUE(reader_->ReadPacketForTesting(&packet));
// Remove metadata from the packet data section before hashing.
av_packet_split_side_data(&packet);
// On the first pass save the MD5 hash of each packet, on subsequent
// passes ensure it matches.
const std::string md5_hash = base::MD5String(base::StringPiece(
reinterpret_cast<char*>(packet.data), packet.size));
if (i == 0) {
packet_md5_hashes_.push_back(md5_hash);
if (j == 0) {
start_timestamp = ConvertFromTimeBase(
reader_->codec_context_for_testing()->time_base, packet.pts);
}
} else {
EXPECT_EQ(packet_md5_hashes_[j], md5_hash) << "j = " << j;
}
av_free_packet(&packet);
}
ASSERT_TRUE(reader_->SeekForTesting(start_timestamp));
}
}
void RunTest(const char* fn,
const char* hash,
int channels,
int sample_rate,
base::TimeDelta duration,
int frames,
int trimmed_frames) {
Initialize(fn); Initialize(fn);
ASSERT_TRUE(reader_->Open()); ASSERT_TRUE(reader_->Open());
EXPECT_EQ(channels, reader_->channels()); EXPECT_EQ(channels, reader_->channels());
...@@ -49,6 +93,8 @@ class AudioFileReaderTest : public testing::Test { ...@@ -49,6 +93,8 @@ class AudioFileReaderTest : public testing::Test {
EXPECT_EQ(duration.InMicroseconds(), EXPECT_EQ(duration.InMicroseconds(),
reader_->GetDuration().InMicroseconds()); reader_->GetDuration().InMicroseconds());
EXPECT_EQ(frames, reader_->GetNumberOfFrames()); EXPECT_EQ(frames, reader_->GetNumberOfFrames());
if (!packet_verification_disabled_)
ASSERT_NO_FATAL_FAILURE(VerifyPackets());
ReadAndVerify(hash, trimmed_frames); ReadAndVerify(hash, trimmed_frames);
} }
...@@ -60,15 +106,20 @@ class AudioFileReaderTest : public testing::Test { ...@@ -60,15 +106,20 @@ class AudioFileReaderTest : public testing::Test {
void RunTestFailingDecode(const char* fn) { void RunTestFailingDecode(const char* fn) {
Initialize(fn); Initialize(fn);
EXPECT_TRUE(reader_->Open()); EXPECT_TRUE(reader_->Open());
scoped_ptr<AudioBus> decoded_audio_data = AudioBus::Create( scoped_ptr<AudioBus> decoded_audio_data =
reader_->channels(), reader_->GetNumberOfFrames()); AudioBus::Create(reader_->channels(), reader_->GetNumberOfFrames());
EXPECT_EQ(reader_->Read(decoded_audio_data.get()), 0); EXPECT_EQ(reader_->Read(decoded_audio_data.get()), 0);
} }
void disable_packet_verification() {
packet_verification_disabled_ = true;
}
protected: protected:
scoped_refptr<DecoderBuffer> data_; scoped_refptr<DecoderBuffer> data_;
scoped_ptr<InMemoryUrlProtocol> protocol_; scoped_ptr<InMemoryUrlProtocol> protocol_;
scoped_ptr<AudioFileReader> reader_; scoped_ptr<AudioFileReader> reader_;
bool packet_verification_disabled_;
DISALLOW_COPY_AND_ASSIGN(AudioFileReaderTest); DISALLOW_COPY_AND_ASSIGN(AudioFileReaderTest);
}; };
...@@ -82,49 +133,97 @@ TEST_F(AudioFileReaderTest, InvalidFile) { ...@@ -82,49 +133,97 @@ TEST_F(AudioFileReaderTest, InvalidFile) {
} }
TEST_F(AudioFileReaderTest, WithVideo) { TEST_F(AudioFileReaderTest, WithVideo) {
RunTest("bear.ogv", "-2.49,-0.75,0.38,1.60,0.70,-1.22,", 2, 44100, RunTest("bear.ogv",
base::TimeDelta::FromMicroseconds(1011520), 44609, 44609); "-2.49,-0.75,0.38,1.60,0.70,-1.22,",
2,
44100,
base::TimeDelta::FromMicroseconds(1011520),
44609,
44609);
} }
TEST_F(AudioFileReaderTest, Vorbis) { TEST_F(AudioFileReaderTest, Vorbis) {
RunTest("sfx.ogg", "4.36,4.81,4.84,4.45,4.61,4.63,", 1, 44100, RunTest("sfx.ogg",
base::TimeDelta::FromMicroseconds(350001), 15436, 15436); "4.36,4.81,4.84,4.45,4.61,4.63,",
1,
44100,
base::TimeDelta::FromMicroseconds(350001),
15436,
15436);
} }
TEST_F(AudioFileReaderTest, WaveU8) { TEST_F(AudioFileReaderTest, WaveU8) {
RunTest("sfx_u8.wav", "-1.23,-1.57,-1.14,-0.91,-0.87,-0.07,", 1, 44100, RunTest("sfx_u8.wav",
base::TimeDelta::FromMicroseconds(288414), 12720, 12719); "-1.23,-1.57,-1.14,-0.91,-0.87,-0.07,",
1,
44100,
base::TimeDelta::FromMicroseconds(288414),
12720,
12719);
} }
TEST_F(AudioFileReaderTest, WaveS16LE) { TEST_F(AudioFileReaderTest, WaveS16LE) {
RunTest("sfx_s16le.wav", "3.05,2.87,3.00,3.32,3.58,4.08,", 1, 44100, RunTest("sfx_s16le.wav",
base::TimeDelta::FromMicroseconds(288414), 12720, 12719); "3.05,2.87,3.00,3.32,3.58,4.08,",
1,
44100,
base::TimeDelta::FromMicroseconds(288414),
12720,
12719);
} }
TEST_F(AudioFileReaderTest, WaveS24LE) { TEST_F(AudioFileReaderTest, WaveS24LE) {
RunTest("sfx_s24le.wav", "3.03,2.86,2.99,3.31,3.57,4.06,", 1, 44100, RunTest("sfx_s24le.wav",
base::TimeDelta::FromMicroseconds(288414), 12720, 12719); "3.03,2.86,2.99,3.31,3.57,4.06,",
1,
44100,
base::TimeDelta::FromMicroseconds(288414),
12720,
12719);
} }
TEST_F(AudioFileReaderTest, WaveF32LE) { TEST_F(AudioFileReaderTest, WaveF32LE) {
RunTest("sfx_f32le.wav", "3.03,2.86,2.99,3.31,3.57,4.06,", 1, 44100, RunTest("sfx_f32le.wav",
base::TimeDelta::FromMicroseconds(288414), 12720, 12719); "3.03,2.86,2.99,3.31,3.57,4.06,",
1,
44100,
base::TimeDelta::FromMicroseconds(288414),
12720,
12719);
} }
#if defined(USE_PROPRIETARY_CODECS) #if defined(USE_PROPRIETARY_CODECS)
TEST_F(AudioFileReaderTest, MP3) { TEST_F(AudioFileReaderTest, MP3) {
RunTest("sfx.mp3", "3.05,2.87,3.00,3.32,3.58,4.08,", 1, 44100, RunTest("sfx.mp3",
base::TimeDelta::FromMicroseconds(313470), 13825, 12719); "3.05,2.87,3.00,3.32,3.58,4.08,",
1,
44100,
base::TimeDelta::FromMicroseconds(313470),
13825,
12719);
} }
TEST_F(AudioFileReaderTest, CorruptMP3) { TEST_F(AudioFileReaderTest, CorruptMP3) {
RunTest("corrupt.mp3", "-4.95,-2.95,-0.44,1.16,0.31,-2.21,", 1, 44100, // Disable packet verification since the file is corrupt and FFmpeg does not
base::TimeDelta::FromMicroseconds(1018826), 44931, 44928); // make any guarantees on packet consistency in this case.
disable_packet_verification();
RunTest("corrupt.mp3",
"-4.95,-2.95,-0.44,1.16,0.31,-2.21,",
1,
44100,
base::TimeDelta::FromMicroseconds(1018826),
44931,
44928);
} }
TEST_F(AudioFileReaderTest, AAC) { TEST_F(AudioFileReaderTest, AAC) {
RunTest("sfx.m4a", "1.81,1.66,2.32,3.27,4.46,3.36,", 1, 44100, RunTest("sfx.m4a",
base::TimeDelta::FromMicroseconds(312001), 13760, 13312); "1.81,1.66,2.32,3.27,4.46,3.36,",
1,
44100,
base::TimeDelta::FromMicroseconds(312001),
13760,
13312);
} }
TEST_F(AudioFileReaderTest, MidStreamConfigChangesFail) { TEST_F(AudioFileReaderTest, MidStreamConfigChangesFail) {
...@@ -137,8 +236,13 @@ TEST_F(AudioFileReaderTest, VorbisInvalidChannelLayout) { ...@@ -137,8 +236,13 @@ TEST_F(AudioFileReaderTest, VorbisInvalidChannelLayout) {
} }
TEST_F(AudioFileReaderTest, WaveValidFourChannelLayout) { TEST_F(AudioFileReaderTest, WaveValidFourChannelLayout) {
RunTest("4ch.wav", "131.71,38.02,130.31,44.89,135.98,42.52,", 4, 44100, RunTest("4ch.wav",
base::TimeDelta::FromMicroseconds(100001), 4411, 4410); "131.71,38.02,130.31,44.89,135.98,42.52,",
4,
44100,
base::TimeDelta::FromMicroseconds(100001),
4411,
4410);
} }
} // namespace media } // namespace media
...@@ -210,7 +210,6 @@ void FFmpegAudioDecoder::DecodeBuffer( ...@@ -210,7 +210,6 @@ void FFmpegAudioDecoder::DecodeBuffer(
DCHECK_NE(state_, kUninitialized); DCHECK_NE(state_, kUninitialized);
DCHECK_NE(state_, kDecodeFinished); DCHECK_NE(state_, kDecodeFinished);
DCHECK_NE(state_, kError); DCHECK_NE(state_, kError);
DCHECK(buffer); DCHECK(buffer);
// Make sure we are notified if http://crbug.com/49709 returns. Issue also // Make sure we are notified if http://crbug.com/49709 returns. Issue also
......
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <deque>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "media/base/audio_buffer.h"
#include "media/base/decoder_buffer.h"
#include "media/base/mock_filters.h"
#include "media/base/test_data_util.h"
#include "media/base/test_helpers.h"
#include "media/ffmpeg/ffmpeg_common.h"
#include "media/filters/ffmpeg_audio_decoder.h"
#include "media/filters/ffmpeg_glue.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::StrictMock;
namespace media {
class FFmpegAudioDecoderTest : public testing::Test {
public:
FFmpegAudioDecoderTest()
: decoder_(new FFmpegAudioDecoder(message_loop_.message_loop_proxy(),
LogCB())),
pending_decode_(false),
pending_reset_(false),
last_decode_status_(AudioDecoder::kOk) {
FFmpegGlue::InitializeFFmpeg();
vorbis_extradata_ = ReadTestDataFile("vorbis-extradata");
// Refer to media/test/data/README for details on vorbis test data.
for (int i = 0; i < 4; ++i) {
scoped_refptr<DecoderBuffer> buffer =
ReadTestDataFile(base::StringPrintf("vorbis-packet-%d", i));
if (i < 3) {
buffer->set_timestamp(base::TimeDelta());
} else {
buffer->set_timestamp(base::TimeDelta::FromMicroseconds(2902));
}
buffer->set_duration(base::TimeDelta());
encoded_audio_.push_back(buffer);
}
// Push in an EOS buffer.
encoded_audio_.push_back(DecoderBuffer::CreateEOSBuffer());
Initialize();
}
virtual ~FFmpegAudioDecoderTest() {
EXPECT_FALSE(pending_decode_);
EXPECT_FALSE(pending_reset_);
}
void Initialize() {
AudioDecoderConfig config(kCodecVorbis,
kSampleFormatPlanarF32,
CHANNEL_LAYOUT_STEREO,
44100,
vorbis_extradata_->data(),
vorbis_extradata_->data_size(),
false); // Not encrypted.
decoder_->Initialize(config,
NewExpectedStatusCB(PIPELINE_OK),
base::Bind(&FFmpegAudioDecoderTest::OnDecoderOutput,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
}
void SatisfyPendingDecode() {
base::RunLoop().RunUntilIdle();
}
void Decode() {
pending_decode_ = true;
scoped_refptr<DecoderBuffer> buffer(encoded_audio_.front());
encoded_audio_.pop_front();
decoder_->Decode(buffer,
base::Bind(&FFmpegAudioDecoderTest::DecodeFinished,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(pending_decode_);
EXPECT_EQ(AudioDecoder::kOk, last_decode_status_);
}
void Reset() {
pending_reset_ = true;
decoder_->Reset(base::Bind(
&FFmpegAudioDecoderTest::ResetFinished, base::Unretained(this)));
base::RunLoop().RunUntilIdle();
}
void Stop() {
decoder_->Stop();
base::RunLoop().RunUntilIdle();
}
void OnDecoderOutput(const scoped_refptr<AudioBuffer>& buffer) {
EXPECT_FALSE(buffer->end_of_stream());
decoded_audio_.push_back(buffer);
}
void DecodeFinished(AudioDecoder::Status status) {
EXPECT_TRUE(pending_decode_);
pending_decode_ = false;
last_decode_status_ = status;
}
void ResetFinished() {
EXPECT_TRUE(pending_reset_);
// Reset should always finish after Decode.
EXPECT_FALSE(pending_decode_);
pending_reset_ = false;
}
void ExpectDecodedAudio(size_t i, int64 timestamp, int64 duration) {
EXPECT_LT(i, decoded_audio_.size());
EXPECT_EQ(timestamp, decoded_audio_[i]->timestamp().InMicroseconds());
EXPECT_EQ(duration, decoded_audio_[i]->duration().InMicroseconds());
}
base::MessageLoop message_loop_;
scoped_ptr<FFmpegAudioDecoder> decoder_;
bool pending_decode_;
bool pending_reset_;
scoped_refptr<DecoderBuffer> vorbis_extradata_;
std::deque<scoped_refptr<DecoderBuffer> > encoded_audio_;
std::deque<scoped_refptr<AudioBuffer> > decoded_audio_;
AudioDecoder::Status last_decode_status_;
};
TEST_F(FFmpegAudioDecoderTest, Initialize) {
AudioDecoderConfig config(kCodecVorbis,
kSampleFormatPlanarF32,
CHANNEL_LAYOUT_STEREO,
44100,
vorbis_extradata_->data(),
vorbis_extradata_->data_size(),
false); // Not encrypted.
Stop();
}
TEST_F(FFmpegAudioDecoderTest, ProduceAudioSamples) {
// Vorbis requires N+1 packets to produce audio data for N packets.
//
// This will should result in the demuxer receiving three reads for two
// requests to produce audio samples.
Decode();
Decode();
Decode();
Decode();
ASSERT_EQ(3u, decoded_audio_.size());
ExpectDecodedAudio(0, 0, 2902);
ExpectDecodedAudio(1, 2902, 13061);
ExpectDecodedAudio(2, 15963, 23219);
// Call one more time with EOS.
Decode();
ASSERT_EQ(3u, decoded_audio_.size());
Stop();
}
TEST_F(FFmpegAudioDecoderTest, PendingDecode_Stop) {
Decode();
Stop();
SatisfyPendingDecode();
}
TEST_F(FFmpegAudioDecoderTest, PendingDecode_Reset) {
Decode();
Reset();
SatisfyPendingDecode();
Stop();
}
TEST_F(FFmpegAudioDecoderTest, PendingDecode_ResetStop) {
Decode();
Reset();
Stop();
SatisfyPendingDecode();
}
} // namespace media
...@@ -292,15 +292,16 @@ void OpusAudioDecoder::Stop() { ...@@ -292,15 +292,16 @@ void OpusAudioDecoder::Stop() {
CloseDecoder(); CloseDecoder();
} }
OpusAudioDecoder::~OpusAudioDecoder() {} OpusAudioDecoder::~OpusAudioDecoder() {
DCHECK(!opus_decoder_);
}
void OpusAudioDecoder::DecodeBuffer( void OpusAudioDecoder::DecodeBuffer(
const scoped_refptr<DecoderBuffer>& input, const scoped_refptr<DecoderBuffer>& input,
const DecodeCB& decode_cb) { const DecodeCB& decode_cb) {
DCHECK(task_runner_->BelongsToCurrentThread()); DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(!decode_cb.is_null()); DCHECK(!decode_cb.is_null());
DCHECK(input);
DCHECK(input.get());
// Libopus does not buffer output. Decoding is complete when an end of stream // Libopus does not buffer output. Decoding is complete when an end of stream
// input buffer is received. // input buffer is received.
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <deque>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "media/base/audio_buffer.h"
#include "media/base/decoder_buffer.h"
#include "media/base/test_data_util.h"
#include "media/base/test_helpers.h"
#include "media/ffmpeg/ffmpeg_common.h"
#include "media/filters/audio_file_reader.h"
#include "media/filters/in_memory_url_protocol.h"
#include "media/filters/opus_audio_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
class OpusAudioDecoderTest : public testing::Test {
public:
OpusAudioDecoderTest()
: decoder_(new OpusAudioDecoder(message_loop_.message_loop_proxy())),
pending_decode_(false),
pending_reset_(false) {}
virtual ~OpusAudioDecoderTest() {
EXPECT_FALSE(pending_decode_);
EXPECT_FALSE(pending_reset_);
}
protected:
void SatisfyPendingDecode() { base::RunLoop().RunUntilIdle(); }
void SendEndOfStream() {
pending_decode_ = true;
decoder_->Decode(DecoderBuffer::CreateEOSBuffer(),
base::Bind(&OpusAudioDecoderTest::DecodeFinished,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
}
void Initialize() {
// Load the test data file.
data_ = ReadTestDataFile("bear-opus.ogg");
protocol_.reset(
new InMemoryUrlProtocol(data_->data(), data_->data_size(), false));
reader_.reset(new AudioFileReader(protocol_.get()));
reader_->Open();
AudioDecoderConfig config;
AVCodecContextToAudioDecoderConfig(
reader_->codec_context_for_testing(), false, &config, false);
InitializeDecoder(config);
}
void InitializeDecoder(const AudioDecoderConfig& config) {
decoder_->Initialize(config,
NewExpectedStatusCB(PIPELINE_OK),
base::Bind(&OpusAudioDecoderTest::OnDecoderOutput,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
}
void Decode() {
pending_decode_ = true;
AVPacket packet;
ASSERT_TRUE(reader_->ReadPacketForTesting(&packet));
scoped_refptr<DecoderBuffer> buffer =
DecoderBuffer::CopyFrom(packet.data, packet.size);
buffer->set_timestamp(ConvertFromTimeBase(
reader_->codec_context_for_testing()->time_base, packet.pts));
buffer->set_duration(ConvertFromTimeBase(
reader_->codec_context_for_testing()->time_base, packet.duration));
decoder_->Decode(buffer,
base::Bind(&OpusAudioDecoderTest::DecodeFinished,
base::Unretained(this)));
av_free_packet(&packet);
base::RunLoop().RunUntilIdle();
}
void Reset() {
pending_reset_ = true;
decoder_->Reset(base::Bind(&OpusAudioDecoderTest::ResetFinished,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
}
void Stop() {
decoder_->Stop();
base::RunLoop().RunUntilIdle();
}
void OnDecoderOutput(const scoped_refptr<AudioBuffer>& buffer) {
decoded_audio_.push_back(buffer);
}
void DecodeFinished(AudioDecoder::Status status) {
EXPECT_TRUE(pending_decode_);
pending_decode_ = false;
// If we have a pending reset, we expect an abort.
if (pending_reset_) {
EXPECT_EQ(status, AudioDecoder::kAborted);
return;
}
EXPECT_EQ(status, AudioDecoder::kOk);
}
void ResetFinished() {
EXPECT_TRUE(pending_reset_);
// Reset should always finish after Decode.
EXPECT_FALSE(pending_decode_);
pending_reset_ = false;
}
void ExpectDecodedAudio(size_t i, int64 timestamp, int64 duration) {
EXPECT_LT(i, decoded_audio_.size());
EXPECT_EQ(timestamp, decoded_audio_[i]->timestamp().InMicroseconds());
EXPECT_EQ(duration, decoded_audio_[i]->duration().InMicroseconds());
EXPECT_FALSE(decoded_audio_[i]->end_of_stream());
}
size_t decoded_audio_size() const {
return decoded_audio_.size();
}
private:
base::MessageLoop message_loop_;
scoped_refptr<DecoderBuffer> data_;
scoped_ptr<InMemoryUrlProtocol> protocol_;
scoped_ptr<AudioFileReader> reader_;
scoped_ptr<OpusAudioDecoder> decoder_;
bool pending_decode_;
bool pending_reset_;
std::deque<scoped_refptr<AudioBuffer> > decoded_audio_;
DISALLOW_COPY_AND_ASSIGN(OpusAudioDecoderTest);
};
TEST_F(OpusAudioDecoderTest, Initialize) {
Initialize();
Stop();
}
TEST_F(OpusAudioDecoderTest, InitializeWithNoCodecDelay) {
const uint8_t kOpusExtraData[] = {
0x4f, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64, 0x01, 0x02,
// The next two bytes represent the codec delay.
0x00, 0x00, 0x80, 0xbb, 0x00, 0x00, 0x00, 0x00, 0x00};
AudioDecoderConfig decoder_config;
decoder_config.Initialize(kCodecOpus,
kSampleFormatF32,
CHANNEL_LAYOUT_STEREO,
48000,
kOpusExtraData,
ARRAYSIZE_UNSAFE(kOpusExtraData),
false,
false,
base::TimeDelta::FromMilliseconds(80),
0);
InitializeDecoder(decoder_config);
Stop();
}
TEST_F(OpusAudioDecoderTest, ProduceAudioSamples) {
Initialize();
Decode();
Decode();
Decode();
ASSERT_EQ(3u, decoded_audio_size());
ExpectDecodedAudio(0, 0, 3500);
ExpectDecodedAudio(1, 3500, 10000);
ExpectDecodedAudio(2, 13500, 10000);
// Call one more time with EOS.
SendEndOfStream();
ASSERT_EQ(3u, decoded_audio_size());
Stop();
}
TEST_F(OpusAudioDecoderTest, DecodeAbort) {
Initialize();
Decode();
Stop();
}
TEST_F(OpusAudioDecoderTest, PendingDecode_Stop) {
Initialize();
Decode();
Stop();
SatisfyPendingDecode();
}
TEST_F(OpusAudioDecoderTest, PendingDecode_Reset) {
Initialize();
Decode();
Reset();
SatisfyPendingDecode();
Stop();
}
TEST_F(OpusAudioDecoderTest, PendingDecode_ResetStop) {
Initialize();
Decode();
Reset();
Stop();
SatisfyPendingDecode();
}
} // namespace media
...@@ -1095,6 +1095,7 @@ ...@@ -1095,6 +1095,7 @@
'cdm/json_web_key_unittest.cc', 'cdm/json_web_key_unittest.cc',
'ffmpeg/ffmpeg_common_unittest.cc', 'ffmpeg/ffmpeg_common_unittest.cc',
'filters/audio_clock_unittest.cc', 'filters/audio_clock_unittest.cc',
'filters/audio_decoder_unittest.cc',
'filters/audio_decoder_selector_unittest.cc', 'filters/audio_decoder_selector_unittest.cc',
'filters/audio_file_reader_unittest.cc', 'filters/audio_file_reader_unittest.cc',
'filters/audio_renderer_algorithm_unittest.cc', 'filters/audio_renderer_algorithm_unittest.cc',
...@@ -1110,7 +1111,6 @@ ...@@ -1110,7 +1111,6 @@
'filters/fake_video_decoder.cc', 'filters/fake_video_decoder.cc',
'filters/fake_video_decoder.h', 'filters/fake_video_decoder.h',
'filters/fake_video_decoder_unittest.cc', 'filters/fake_video_decoder_unittest.cc',
'filters/ffmpeg_audio_decoder_unittest.cc',
'filters/ffmpeg_demuxer_unittest.cc', 'filters/ffmpeg_demuxer_unittest.cc',
'filters/ffmpeg_glue_unittest.cc', 'filters/ffmpeg_glue_unittest.cc',
'filters/ffmpeg_video_decoder_unittest.cc', 'filters/ffmpeg_video_decoder_unittest.cc',
...@@ -1119,7 +1119,6 @@ ...@@ -1119,7 +1119,6 @@
'filters/h264_bit_reader_unittest.cc', 'filters/h264_bit_reader_unittest.cc',
'filters/h264_parser_unittest.cc', 'filters/h264_parser_unittest.cc',
'filters/in_memory_url_protocol_unittest.cc', 'filters/in_memory_url_protocol_unittest.cc',
'filters/opus_audio_decoder_unittest.cc',
'filters/pipeline_integration_test.cc', 'filters/pipeline_integration_test.cc',
'filters/pipeline_integration_test_base.cc', 'filters/pipeline_integration_test_base.cc',
'filters/skcanvas_video_renderer_unittest.cc', 'filters/skcanvas_video_renderer_unittest.cc',
...@@ -1193,15 +1192,14 @@ ...@@ -1193,15 +1192,14 @@
'audio/audio_input_volume_unittest.cc', 'audio/audio_input_volume_unittest.cc',
'base/container_names_unittest.cc', 'base/container_names_unittest.cc',
'ffmpeg/ffmpeg_common_unittest.cc', 'ffmpeg/ffmpeg_common_unittest.cc',
'filters/audio_decoder_unittest.cc',
'filters/audio_file_reader_unittest.cc', 'filters/audio_file_reader_unittest.cc',
'filters/blocking_url_protocol_unittest.cc', 'filters/blocking_url_protocol_unittest.cc',
'filters/ffmpeg_audio_decoder_unittest.cc',
'filters/ffmpeg_demuxer_unittest.cc', 'filters/ffmpeg_demuxer_unittest.cc',
'filters/ffmpeg_glue_unittest.cc', 'filters/ffmpeg_glue_unittest.cc',
'filters/ffmpeg_h264_to_annex_b_bitstream_converter_unittest.cc', 'filters/ffmpeg_h264_to_annex_b_bitstream_converter_unittest.cc',
'filters/ffmpeg_video_decoder_unittest.cc', 'filters/ffmpeg_video_decoder_unittest.cc',
'filters/in_memory_url_protocol_unittest.cc', 'filters/in_memory_url_protocol_unittest.cc',
'filters/opus_audio_decoder_unittest.cc',
'filters/pipeline_integration_test.cc', 'filters/pipeline_integration_test.cc',
'filters/pipeline_integration_test_base.cc', 'filters/pipeline_integration_test_base.cc',
], ],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment