diff options
author | kylep@chromium.org <kylep@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-22 22:44:20 +0000 |
---|---|---|
committer | kylep@chromium.org <kylep@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-22 22:44:20 +0000 |
commit | e42a3b7d1233096f25a570bc4bad7acc2f2fee0b (patch) | |
tree | 197542bedfe5196a56f9d7e9089d00c839a229e5 /media | |
parent | a28e65042b2dfb6c682170f8bb41de12210694d5 (diff) | |
download | chromium_src-e42a3b7d1233096f25a570bc4bad7acc2f2fee0b.zip chromium_src-e42a3b7d1233096f25a570bc4bad7acc2f2fee0b.tar.gz chromium_src-e42a3b7d1233096f25a570bc4bad7acc2f2fee0b.tar.bz2 |
Cleaned up AudioRendererAlgorithmBase: moved data members to private section and added accessors.
Review URL: http://codereview.chromium.org/140080
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18974 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media')
-rw-r--r-- | media/filters/audio_renderer_algorithm_base.cc | 44 | ||||
-rw-r--r-- | media/filters/audio_renderer_algorithm_base.h | 42 | ||||
-rw-r--r-- | media/filters/audio_renderer_algorithm_default.cc | 25 | ||||
-rw-r--r-- | media/filters/audio_renderer_algorithm_default.h | 6 |
4 files changed, 71 insertions, 46 deletions
diff --git a/media/filters/audio_renderer_algorithm_base.cc b/media/filters/audio_renderer_algorithm_base.cc index 5a521cb..f586c4a 100644 --- a/media/filters/audio_renderer_algorithm_base.cc +++ b/media/filters/audio_renderer_algorithm_base.cc @@ -19,8 +19,7 @@ AudioRendererAlgorithmBase::AudioRendererAlgorithmBase() : channels_(0), sample_rate_(0), sample_bits_(0), - playback_rate_(0.0f), - expected_queue_size_(0) { + playback_rate_(0.0f) { } AudioRendererAlgorithmBase::~AudioRendererAlgorithmBase() {} @@ -33,15 +32,17 @@ void AudioRendererAlgorithmBase::Initialize(int channels, DCHECK_GT(channels, 0); DCHECK_GT(sample_rate, 0); DCHECK_GT(sample_bits, 0); - DCHECK_GE(initial_playback_rate, 0.0); DCHECK(callback); channels_ = channels; sample_rate_ = sample_rate; sample_bits_ = sample_bits; - playback_rate_ = initial_playback_rate; request_read_callback_.reset(callback); - FillQueue(); + + set_playback_rate(initial_playback_rate); + + for (size_t i = 0; i < kDefaultMaxQueueSize; ++i) + request_read_callback_->Run(); } void AudioRendererAlgorithmBase::FlushBuffers() { @@ -57,13 +58,38 @@ void AudioRendererAlgorithmBase::EnqueueBuffer(Buffer* buffer_in) { } } -void AudioRendererAlgorithmBase::SetPlaybackRate(float new_rate) { +float AudioRendererAlgorithmBase::playback_rate() { + return playback_rate_; +} + +void AudioRendererAlgorithmBase::set_playback_rate(float new_rate) { + DCHECK_GE(new_rate, 0.0); playback_rate_ = new_rate; } -void AudioRendererAlgorithmBase::FillQueue() { - for ( ; expected_queue_size_ < kDefaultMaxQueueSize; ++expected_queue_size_) - request_read_callback_->Run(); +bool AudioRendererAlgorithmBase::IsQueueEmpty() { + return queue_.empty(); +} + +scoped_refptr<Buffer> AudioRendererAlgorithmBase::FrontQueue() { + return queue_.front(); +} + +void AudioRendererAlgorithmBase::PopFrontQueue() { + queue_.pop_front(); + request_read_callback_->Run(); +} + +int AudioRendererAlgorithmBase::channels() { + return channels_; +} + +int AudioRendererAlgorithmBase::sample_rate() { + return sample_rate_; +} + +int AudioRendererAlgorithmBase::sample_bits() { + return sample_bits_; } } // namespace media diff --git a/media/filters/audio_renderer_algorithm_base.h b/media/filters/audio_renderer_algorithm_base.h index 4b0aec3..47659a6 100644 --- a/media/filters/audio_renderer_algorithm_base.h +++ b/media/filters/audio_renderer_algorithm_base.h @@ -7,9 +7,8 @@ // subclasses may need more than one to produce output. Subclasses // must implement the following method: // -// Process() - fills the buffer passed to it & returns how many bytes copied. -// -// Subclasses must also decrement |expected_queue_size_| after a dequeue. +// FillBuffer() - fills the buffer passed to it & returns how many bytes +// copied. // // The general assumption is that the owner of this class will provide us with // Buffers and a playback speed, and we will fill an output buffer when our @@ -22,13 +21,10 @@ // enqueues and processes do not cause an unpredictable |queue_| size. // // Most of ARAB is nonvirtual as the only subclasses we expect are those to -// define Process(). |queue_| management and common property management +// define FillBuffer(). |queue_| management and common property management // should not have to change for subclasses, so both are implemented here // non-virtually. -// TODO(kylep): Clean this whole class up. Refactor out the |queue_| into a -// QueueManager or something like that. - #ifndef MEDIA_FILTERS_AUDIO_RENDERER_ALGORITHM_BASE_H_ #define MEDIA_FILTERS_AUDIO_RENDERER_ALGORITHM_BASE_H_ @@ -61,8 +57,7 @@ class AudioRendererAlgorithmBase { // Fills |buffer_out| with possibly scaled data from our |queue_|. // |buffer_out| must be initialized and have a datasize. // Returns the number of bytes copied into |buffer_out|. - // TODO(kylep): Think of a better name. - virtual size_t Process(DataBuffer* buffer_out) = 0; + virtual size_t FillBuffer(DataBuffer* buffer_out) = 0; // Clears |queue_|. void FlushBuffers(); @@ -71,15 +66,26 @@ class AudioRendererAlgorithmBase { // of the algorithm after a read completes. void EnqueueBuffer(Buffer* buffer_in); - // Sets the playback rate. - void SetPlaybackRate(float new_rate); + // Getter/setter for |playback_rate_|. + float playback_rate(); + void set_playback_rate(float new_rate); protected: - // Fills the |queue_|. This is a helper function called from - // Initialize() to initially fill |queue_| and from subclasses' - // Process() method to refill |queue_| after buffers have been used. - void FillQueue(); + // Returns whether |queue_| is empty. + bool IsQueueEmpty(); + + // Returns a reference to the first element of the |queue_|. + scoped_refptr<Buffer> FrontQueue(); + + // Pops the front of the |queue_| and schedules a read. + void PopFrontQueue(); + // Audio property getters. + int channels(); + int sample_rate(); + int sample_bits(); + + private: // Audio properties. int channels_; int sample_rate_; @@ -91,16 +97,10 @@ class AudioRendererAlgorithmBase { // Used to request more data. scoped_ptr<RequestReadCallback> request_read_callback_; - // Keeps track of the number of buffers we will have in |queue_| - // after all reads complete. Subclasses MUST decrement this after - // dequeuing. - size_t expected_queue_size_; - // Queued audio data. typedef std::deque< scoped_refptr<Buffer> > BufferQueue; BufferQueue queue_; - private: DISALLOW_COPY_AND_ASSIGN(AudioRendererAlgorithmBase); }; diff --git a/media/filters/audio_renderer_algorithm_default.cc b/media/filters/audio_renderer_algorithm_default.cc index 3b46af5..74c0746 100644 --- a/media/filters/audio_renderer_algorithm_default.cc +++ b/media/filters/audio_renderer_algorithm_default.cc @@ -13,16 +13,16 @@ AudioRendererAlgorithmDefault::AudioRendererAlgorithmDefault() AudioRendererAlgorithmDefault::~AudioRendererAlgorithmDefault() { } -size_t AudioRendererAlgorithmDefault::Process(DataBuffer* buffer_out) { +size_t AudioRendererAlgorithmDefault::FillBuffer(DataBuffer* buffer_out) { size_t dest_written = 0; - if (playback_rate_ == 0.0f) { + if (playback_rate() == 0.0f) { return 0; } - if (playback_rate_ == 1.0f) { + if (playback_rate() == 1.0f) { size_t dest_length = buffer_out->GetDataSize(); uint8* dest = buffer_out->GetWritableData(dest_length); - while (dest_length > 0 && !queue_.empty()) { - scoped_refptr<Buffer> buffer = queue_.front(); + while (dest_length > 0 && !IsQueueEmpty()) { + scoped_refptr<Buffer> buffer = FrontQueue(); size_t data_length = buffer->GetDataSize() - data_offset_; // Prevent writing past end of the buffer. @@ -39,7 +39,7 @@ size_t AudioRendererAlgorithmDefault::Process(DataBuffer* buffer_out) { DCHECK_GE(buffer->GetDataSize(), data_offset_); // but if we've reached the end, dequeue it. if (buffer->GetDataSize() - data_offset_ == 0) { - queue_.pop_front(); + PopFrontQueue(); data_offset_ = 0; } } @@ -51,26 +51,25 @@ size_t AudioRendererAlgorithmDefault::Process(DataBuffer* buffer_out) { // Discard any buffers that should be "used". size_t scaled_dest_length_remaining = - static_cast<size_t>(dest_written * playback_rate_); - while (scaled_dest_length_remaining > 0 && !queue_.empty()) { - size_t data_length = queue_.front()->GetDataSize() - data_offset_; + static_cast<size_t>(dest_written * playback_rate()); + while (scaled_dest_length_remaining > 0 && !IsQueueEmpty()) { + size_t data_length = FrontQueue()->GetDataSize() - data_offset_; // Last buffer we need. if (data_length > scaled_dest_length_remaining) { data_offset_ += scaled_dest_length_remaining; } else { scaled_dest_length_remaining -= data_length; - queue_.pop_front(); + PopFrontQueue(); data_offset_ = 0; } } // If we ran out, don't report we used more than we did. - if (queue_.empty()) { + if (IsQueueEmpty()) { dest_written -= - static_cast<size_t>(scaled_dest_length_remaining / playback_rate_); + static_cast<size_t>(scaled_dest_length_remaining / playback_rate()); } } - FillQueue(); return dest_written; } diff --git a/media/filters/audio_renderer_algorithm_default.h b/media/filters/audio_renderer_algorithm_default.h index 073c83b..896f165 100644 --- a/media/filters/audio_renderer_algorithm_default.h +++ b/media/filters/audio_renderer_algorithm_default.h @@ -3,8 +3,8 @@ // found in the LICENSE file. // AudioRendererAlgorithmDefault is the default implementation of -// AudioRendererAlgorithmBase. For speeds other than 1.0f, Process() fills -// |buffer_out| with 0s and returns the expected size. As ARAB is +// AudioRendererAlgorithmBase. For speeds other than 1.0f, FillBuffer() +// fills |buffer_out| with 0s and returns the expected size. As ARAB is // thread-unsafe, so is ARAD. #ifndef MEDIA_FILTERS_AUDIO_RENDERER_ALGORITHM_DEFAULT_H_ @@ -22,7 +22,7 @@ class AudioRendererAlgorithmDefault : public AudioRendererAlgorithmBase { virtual ~AudioRendererAlgorithmDefault(); // AudioRendererAlgorithmBase implementation - virtual size_t Process(DataBuffer* buffer_out); + virtual size_t FillBuffer(DataBuffer* buffer_out); private: // Remembers the amount of remaining audio data for the front buffer. |