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..64ff70b36efeb299b125a7ab2a50f90cc05da9c6 |
--- /dev/null |
+++ b/media/base/audio_block_fifo.cc |
@@ -0,0 +1,90 @@ |
+// 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" |
+#include "media/base/audio_fifo.h" |
+ |
+namespace media { |
+ |
+AudioBlockFifo::AudioBlockFifo(int channels, int frames, int blocks) |
+ : block_frames_(frames), |
+ write_pos_(0) { |
+ // Create |blocks| of audio buses and push them to the containers. |
+ for (int i = 0; i < blocks; ++i) { |
+ scoped_ptr<AudioBus> audio_bus = AudioBus::Create(channels, frames); |
+ audio_blocks_.push_back(audio_bus.release()); |
+ empty_blocks_.push(audio_blocks_.back()); |
+ } |
+} |
+ |
+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); |
+ |
+ // Get the current write block. |
+ AudioBus* current_block = empty_blocks_.front(); |
+ CHECK(current_block); |
+ DCHECK_EQ(current_block->frames(), block_frames_); |
+ |
+ // Figure out if the current written block is enough to store |frames| of new |
+ // content, or what segment sizes we need when adding the new content to the |
+ // FIFO. |
+ int append_frames = 0; |
+ int remain_frames = 0; |
+ AudioFifo::GetSizes(write_pos_, block_frames_, frames, &append_frames, |
DaleCurtis
2014/07/11 22:33:32
You can simplify this entire method to just the fo
no longer working on chromium
2014/07/14 11:28:26
Done with using a write loop instead of recursive
|
+ &remain_frames); |
+ CHECK_LE(write_pos_ + append_frames, block_frames_); |
+ |
+ // Deinterleave the content to the FIFO. |
+ current_block->FromInterleaved(source, append_frames, bytes_per_sample); |
+ |
+ write_pos_ = AudioFifo::UpdatePos(write_pos_, append_frames, block_frames_); |
+ if (!write_pos_) { |
+ // The current block is completely filled, move it from |empety_blocks_| |
+ // to |filled_blocks_|. |
+ filled_blocks_.push(current_block); |
+ empty_blocks_.pop(); |
+ } |
+ |
+ if (remain_frames) { |
+ // Recursively write the remain frames to the FIFO. |
+ const uint8* remain_source = static_cast<const uint8*>(source) + |
+ append_frames * bytes_per_sample * current_block->channels(); |
+ Push(remain_source, remain_frames, bytes_per_sample); |
+ } |
+} |
+ |
+const AudioBus* AudioBlockFifo::Consume() { |
+ DCHECK(filled_blocks()); |
DaleCurtis
2014/07/11 22:33:32
Likewise, this just becomes:
CHECK_LE(tail, head)
no longer working on chromium
2014/07/14 11:28:26
Lets move the discussion to the relevant comment i
|
+ AudioBus* audio_bus = filled_blocks_.front(); |
+ empty_blocks_.push(audio_bus); |
+ filled_blocks_.pop(); |
+ return audio_bus; |
+} |
+ |
+void AudioBlockFifo::Clear() { |
+ while (!filled_blocks_.empty()) { |
DaleCurtis
2014/07/11 22:33:32
And finally: write_pos_ = tail = head = 0;
|
+ AudioBus* audio_bus = filled_blocks_.front(); |
+ empty_blocks_.push(audio_bus); |
+ filled_blocks_.pop(); |
+ write_pos_ = 0; |
+ } |
+} |
+ |
+int AudioBlockFifo::filled_blocks() const { |
+ return filled_blocks_.size(); |
+} |
+ |
+int AudioBlockFifo::empty_frames() const { |
+ DCHECK_GE(static_cast<int>(empty_blocks_.size() * block_frames_), write_pos_); |
DaleCurtis
2014/07/11 22:33:32
I'd just make this frames_ - write_pos_;
no longer working on chromium
2014/07/14 11:28:27
The comment is addressed in the head unfilled_fram
|
+ return empty_blocks_.size() * block_frames_ - write_pos_; |
+} |
+ |
+} // namespace media |