Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(896)

Unified Diff: media/base/audio_block_fifo.cc

Issue 389623002: Add a block based Audio FIFO. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/base/audio_block_fifo.h ('k') | media/base/audio_block_fifo_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/audio_block_fifo.cc
diff --git a/media/base/audio_block_fifo.cc b/media/base/audio_block_fifo.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b8cecfa164e6890c5c4c1b2f838860d7da64a953
--- /dev/null
+++ b/media/base/audio_block_fifo.cc
@@ -0,0 +1,83 @@
+// 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 "media/base/audio_block_fifo.h"
+
+#include "base/logging.h"
+
+namespace media {
+
+AudioBlockFifo::AudioBlockFifo(int channels, int frames, int blocks)
+ : block_frames_(frames),
+ write_block_(0),
+ read_block_(0),
+ available_blocks_(0),
+ write_pos_(0) {
+ // Create |blocks| of audio buses and push them to the containers.
+ audio_blocks_.reserve(blocks);
+ for (int i = 0; i < blocks; ++i) {
+ scoped_ptr<AudioBus> audio_bus = AudioBus::Create(channels, frames);
+ audio_blocks_.push_back(audio_bus.release());
+ }
+}
+
+AudioBlockFifo::~AudioBlockFifo() {}
+
+void AudioBlockFifo::Push(const void* source,
+ int frames,
+ int bytes_per_sample) {
+ DCHECK(source);
+ DCHECK_GT(frames, 0);
+ DCHECK_GT(bytes_per_sample, 0);
+ DCHECK_LT(available_blocks_, static_cast<int>(audio_blocks_.size()));
+
+ const uint8* source_ptr = static_cast<const uint8*>(source);
+ int frames_to_push = frames;
+ while (frames_to_push) {
+ // Get the current write block.
+ AudioBus* current_block = audio_blocks_[write_block_];
+
+ // Figure out what segment sizes we need when adding the new content to
+ // the FIFO.
+ const int push_frames =
+ std::min(block_frames_ - write_pos_, frames_to_push);
+
+ // Deinterleave the content to the FIFO and update the |write_pos_|.
+ current_block->FromInterleaved(source_ptr, push_frames, bytes_per_sample);
+ write_pos_ = (write_pos_ + push_frames) % block_frames_;
+ if (!write_pos_) {
+ // The current block is completely filled, increment |write_block_| and
+ // |available_blocks_|.
+ write_block_ = (write_block_ + 1) % audio_blocks_.size();
+ ++available_blocks_;
+ }
+
+ source_ptr += push_frames * bytes_per_sample * current_block->channels();
+ frames_to_push -= push_frames;
+ }
+}
+
+const AudioBus* AudioBlockFifo::Consume() {
+ DCHECK(available_blocks_);
+ AudioBus* audio_bus = audio_blocks_[read_block_];
+ read_block_ = (read_block_ + 1) % audio_blocks_.size();
+ --available_blocks_;
+ return audio_bus;
+}
+
+void AudioBlockFifo::Clear() {
+ write_pos_ = 0;
+ write_block_ = 0;
+ read_block_ = 0;
+ available_blocks_ = 0;
+}
+
+int AudioBlockFifo::GetUnfilledFrames() const {
+ const int unfilled_frames =
+ (audio_blocks_.size() - available_blocks_) * block_frames_ - write_pos_;
+ DCHECK_GE(unfilled_frames, 0);
+ return unfilled_frames;
+}
+
+} // namespace media
« no previous file with comments | « media/base/audio_block_fifo.h ('k') | media/base/audio_block_fifo_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698