Skip to content

Commit

Permalink
Replace AudioDecoder::media_format() with AudioDecoderConfig.
Browse files Browse the repository at this point in the history
Also includes some cleanup for AudioParameter users including a new helper method GetBytesPerSecond().

BUG=28206
TEST=media_unittests, test_shell_tests, etc...

Review URL: http://codereview.chromium.org/6903007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@83098 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
scherkus@chromium.org committed Apr 26, 2011
1 parent b481edb commit 08bff2c
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 121 deletions.
20 changes: 6 additions & 14 deletions content/renderer/media/audio_renderer_impl.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 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.

Expand Down Expand Up @@ -50,22 +50,14 @@ base::TimeDelta AudioRendererImpl::ConvertToDuration(int bytes) {
return base::TimeDelta();
}

bool AudioRendererImpl::OnInitialize(const media::MediaFormat& media_format) {
// Parse integer values in MediaFormat.
if (!ParseMediaFormat(media_format,
&params_.channels,
&params_.sample_rate,
&params_.bits_per_sample)) {
return false;
}
params_.format = AudioParameters::AUDIO_PCM_LINEAR;
bool AudioRendererImpl::OnInitialize(const media::AudioDecoderConfig& config) {
AudioParameters params(config);
params.format = AudioParameters::AUDIO_PCM_LINEAR;

// Calculate the number of bytes per second using information of the stream.
bytes_per_second_ = params_.sample_rate * params_.channels *
params_.bits_per_sample / 8;
bytes_per_second_ = params.GetBytesPerSecond();

io_loop_->PostTask(FROM_HERE,
NewRunnableMethod(this, &AudioRendererImpl::CreateStreamTask, params_));
NewRunnableMethod(this, &AudioRendererImpl::CreateStreamTask, params));
return true;
}

Expand Down
5 changes: 2 additions & 3 deletions content/renderer/media/audio_renderer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class AudioRendererImpl : public media::AudioRendererBase,
protected:
// Methods called on audio renderer thread ----------------------------------
// These methods are called from AudioRendererBase.
virtual bool OnInitialize(const media::MediaFormat& media_format);
virtual bool OnInitialize(const media::AudioDecoderConfig& config);
virtual void OnStop();

// Called when the decoder completes a Read().
Expand Down Expand Up @@ -114,8 +114,7 @@ class AudioRendererImpl : public media::AudioRendererBase,
// Called on IO thread when message loop is dying.
virtual void WillDestroyCurrentMessageLoop();

// Information about the audio stream.
AudioParameters params_;
// Used to calculate audio delay given bytes.
uint32 bytes_per_second_;

scoped_refptr<AudioMessageFilter> filter_;
Expand Down
12 changes: 3 additions & 9 deletions content/renderer/media/audio_renderer_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
#include "content/common/audio_messages.h"
#include "content/renderer/media/audio_renderer_impl.h"
#include "media/base/data_buffer.h"
#include "media/base/media_format.h"
#include "media/base/mock_callback.h"
#include "media/base/mock_filter_host.h"
#include "media/base/mock_filters.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::ReturnRef;
using ::testing::Return;

class AudioRendererImplTest : public ::testing::Test {
public:
Expand All @@ -33,12 +32,8 @@ class AudioRendererImplTest : public ::testing::Test {
// Setup expectations for initialization.
decoder_ = new media::MockAudioDecoder();

// Associate media format with decoder
decoder_media_format_.SetAsInteger(media::MediaFormat::kChannels, 2);
decoder_media_format_.SetAsInteger(media::MediaFormat::kSampleRate, 48000);
decoder_media_format_.SetAsInteger(media::MediaFormat::kSampleBits, 16);
EXPECT_CALL(*decoder_, media_format())
.WillRepeatedly(ReturnRef(decoder_media_format_));
ON_CALL(*decoder_, config())
.WillByDefault(Return(media::AudioDecoderConfig(16, 1, 44100)));

// Create and initialize audio renderer.
renderer_ = new AudioRendererImpl(filter_);
Expand Down Expand Up @@ -68,7 +63,6 @@ class AudioRendererImplTest : public ::testing::Test {
media::MockFilterHost host_;
scoped_refptr<media::MockAudioDecoder> decoder_;
scoped_refptr<AudioRendererImpl> renderer_;
media::MediaFormat decoder_media_format_;

private:
DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest);
Expand Down
14 changes: 13 additions & 1 deletion media/audio/audio_parameters.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 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.

Expand All @@ -14,6 +14,14 @@ AudioParameters::AudioParameters()
samples_per_packet(0) {
}

AudioParameters::AudioParameters(const media::AudioDecoderConfig& config)
: format(AUDIO_PCM_LINEAR),
channels(config.channels_per_sample),
sample_rate(config.sample_rate),
bits_per_sample(config.bits_per_channel),
samples_per_packet(0) {
}

AudioParameters::AudioParameters(Format format, int channels,
int sample_rate, int bits_per_sample,
int samples_per_packet)
Expand All @@ -38,6 +46,10 @@ int AudioParameters::GetPacketSize() const {
return samples_per_packet * channels * bits_per_sample / 8;
}

int AudioParameters::GetBytesPerSecond() const {
return sample_rate * channels * bits_per_sample / 8;
}

bool AudioParameters::Compare::operator()(
const AudioParameters& a,
const AudioParameters& b) const {
Expand Down
8 changes: 7 additions & 1 deletion media/audio/audio_parameters.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 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.

#ifndef MEDIA_AUDIO_AUDIO_PARAMETERS_H_
#define MEDIA_AUDIO_AUDIO_PARAMETERS_H_

#include "base/basictypes.h"
#include "media/base/audio_decoder_config.h"

struct AudioParameters {
// Compare is useful when AudioParameters is used as a key in std::map.
Expand All @@ -30,6 +31,8 @@ struct AudioParameters {

AudioParameters();

explicit AudioParameters(const media::AudioDecoderConfig& config);

AudioParameters(Format format, int channels, int sample_rate,
int bits_per_sample, int samples_per_packet);

Expand All @@ -40,6 +43,9 @@ struct AudioParameters {
// Returns size of audio packets in bytes.
int GetPacketSize() const;

// Returns the number of bytes representing one second of audio.
int GetBytesPerSecond() const;

Format format; // Format of the stream.
int channels; // Number of channels.
int sample_rate; // Sampling frequency/rate.
Expand Down
50 changes: 49 additions & 1 deletion media/audio/audio_parameters_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 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.

Expand All @@ -7,6 +7,41 @@
#include "media/audio/audio_parameters.h"
#include "testing/gtest/include/gtest/gtest.h"

using media::AudioDecoderConfig;

TEST(AudioParameters, Constructor_Default) {
AudioParameters::Format expected_format = AudioParameters::AUDIO_PCM_LINEAR;
int expected_bits = 0;
int expected_channels = 0;
int expected_rate = 0;
int expected_samples = 0;

AudioParameters params;

EXPECT_EQ(expected_format, params.format);
EXPECT_EQ(expected_bits, params.bits_per_sample);
EXPECT_EQ(expected_channels, params.channels);
EXPECT_EQ(expected_rate, params.sample_rate);
EXPECT_EQ(expected_samples, params.samples_per_packet);
}

TEST(AudioParameters, Constructor_AudioDecoderConfig) {
AudioParameters::Format expected_format = AudioParameters::AUDIO_PCM_LINEAR;
int expected_bits = 8;
int expected_channels = 2;
int expected_rate = 44000;
int expected_samples = 0;

AudioDecoderConfig config(expected_bits, expected_channels, expected_rate);
AudioParameters params(config);

EXPECT_EQ(expected_format, params.format);
EXPECT_EQ(expected_bits, params.bits_per_sample);
EXPECT_EQ(expected_channels, params.channels);
EXPECT_EQ(expected_rate, params.sample_rate);
EXPECT_EQ(expected_samples, params.samples_per_packet);
}

TEST(AudioParameters, GetPacketSize) {
EXPECT_EQ(100, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
1, 1000, 8, 100).GetPacketSize());
Expand All @@ -20,6 +55,19 @@ TEST(AudioParameters, GetPacketSize) {
2, 1000, 16, 200).GetPacketSize());
}

TEST(AudioParameters, GetBytesPerSecond) {
EXPECT_EQ(0, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
0, 0, 0, 0).GetBytesPerSecond());
EXPECT_EQ(0, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
2, 0, 0, 0).GetBytesPerSecond());
EXPECT_EQ(0, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
0, 100, 0, 0).GetBytesPerSecond());
EXPECT_EQ(0, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
0, 0, 8, 0).GetBytesPerSecond());
EXPECT_EQ(200, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
2, 100, 8, 0).GetBytesPerSecond());
}

TEST(AudioParameters, Compare) {
AudioParameters values[] = {
AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 1, 1000, 8, 100),
Expand Down
24 changes: 24 additions & 0 deletions media/base/audio_decoder_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2011 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.

#ifndef MEDIA_BASE_AUDIO_DECODER_CONFIG_H_
#define MEDIA_BASE_AUDIO_DECODER_CONFIG_H_

namespace media {

struct AudioDecoderConfig {
AudioDecoderConfig(int bits, int channels, int rate)
: bits_per_channel(bits),
channels_per_sample(channels),
sample_rate(rate) {
}

int bits_per_channel;
int channels_per_sample;
int sample_rate;
};

} // namespace media

#endif // MEDIA_BASE_AUDIO_DECODER_CONFIG_H_
6 changes: 3 additions & 3 deletions media/base/filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/time.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/media_format.h"
#include "media/base/video_frame.h"

Expand Down Expand Up @@ -238,6 +239,8 @@ class AudioDecoder : public Filter {
virtual void Initialize(DemuxerStream* stream, FilterCallback* callback,
StatisticsCallback* stats_callback) = 0;

virtual AudioDecoderConfig config() = 0;

// |set_fill_buffer_done_callback| install permanent callback from downstream
// filter (i.e. Renderer). The callback is used to deliver buffers at
// runtime to downstream filter.
Expand All @@ -255,9 +258,6 @@ class AudioDecoder : public Filter {
// We could also pass empty pointer here to let decoder provide buffers pool.
virtual void ProduceAudioSamples(scoped_refptr<Buffer> buffer) = 0;

// Returns the media format produced by this decoder.
virtual const MediaFormat& media_format() = 0;

protected:
AudioDecoder();
~AudioDecoder();
Expand Down
2 changes: 1 addition & 1 deletion media/base/mock_filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class MockAudioDecoder : public AudioDecoder {
MOCK_METHOD3(Initialize, void(DemuxerStream* stream,
FilterCallback* callback,
StatisticsCallback* stats_callback));
MOCK_METHOD0(media_format, const MediaFormat&());
MOCK_METHOD0(config, AudioDecoderConfig());
MOCK_METHOD1(ProduceAudioSamples, void(scoped_refptr<Buffer>));

// change to public to allow unittest for access;
Expand Down
30 changes: 5 additions & 25 deletions media/filters/audio_renderer_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,6 @@ void AudioRendererBase::Initialize(AudioDecoder* decoder,

decoder_->set_consume_audio_samples_callback(
NewCallback(this, &AudioRendererBase::ConsumeAudioSamples));
// Get the media properties to initialize our algorithms.
int channels = 0;
int sample_rate = 0;
int sample_bits = 0;
if (!ParseMediaFormat(decoder_->media_format(), &channels, &sample_rate,
&sample_bits)) {
host()->SetError(PIPELINE_ERROR_INITIALIZATION_FAILED);
callback->Run();
return;
}

// Create a callback so our algorithm can request more reads.
AudioRendererAlgorithmBase::RequestReadCallback* cb =
Expand All @@ -110,14 +100,15 @@ void AudioRendererBase::Initialize(AudioDecoder* decoder,

// Initialize our algorithm with media properties, initial playback rate,
// and a callback to request more reads from the data source.
algorithm_->Initialize(channels,
sample_rate,
sample_bits,
AudioDecoderConfig config = decoder_->config();
algorithm_->Initialize(config.channels_per_sample,
config.sample_rate,
config.bits_per_channel,
0.0f,
cb);

// Give the subclass an opportunity to initialize itself.
if (!OnInitialize(decoder_->media_format())) {
if (!OnInitialize(config)) {
host()->SetError(PIPELINE_ERROR_INITIALIZATION_FAILED);
callback->Run();
return;
Expand Down Expand Up @@ -253,17 +244,6 @@ void AudioRendererBase::ScheduleRead_Locked() {
decoder_->ProduceAudioSamples(buffer);
}

// static
bool AudioRendererBase::ParseMediaFormat(const MediaFormat& media_format,
int* channels_out,
int* sample_rate_out,
int* sample_bits_out) {
// TODO(scherkus): might be handy to support NULL parameters.
return media_format.GetAsInteger(MediaFormat::kChannels, channels_out) &&
media_format.GetAsInteger(MediaFormat::kSampleRate, sample_rate_out) &&
media_format.GetAsInteger(MediaFormat::kSampleBits, sample_bits_out);
}

void AudioRendererBase::SetPlaybackRate(float playback_rate) {
algorithm_->set_playback_rate(playback_rate);
}
Expand Down
11 changes: 2 additions & 9 deletions media/filters/audio_renderer_base.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 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.

Expand Down Expand Up @@ -44,10 +44,9 @@ class AudioRendererBase : public AudioRenderer {
virtual bool HasEnded();

protected:
// Called by Initialize(). |media_format| is the format of the AudioDecoder.
// Subclasses should return true if they were able to initialize, false
// otherwise.
virtual bool OnInitialize(const MediaFormat& media_format) = 0;
virtual bool OnInitialize(const AudioDecoderConfig& config) = 0;

// Called by Stop(). Subclasses should perform any necessary cleanup during
// this time, such as stopping any running threads.
Expand Down Expand Up @@ -79,12 +78,6 @@ class AudioRendererBase : public AudioRenderer {
const base::TimeDelta& playback_delay,
bool buffers_empty);

// Helper to parse a media format and return whether we were successful
// retrieving all the information we care about.
static bool ParseMediaFormat(const MediaFormat& media_format,
int* channels_out, int* sample_rate_out,
int* sample_bits_out);

// Get/Set the playback rate of |algorithm_|.
virtual void SetPlaybackRate(float playback_rate);
virtual float GetPlaybackRate();
Expand Down
Loading

0 comments on commit 08bff2c

Please sign in to comment.