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..311a7d50e7578d69d87d3a4521ed44aea52f04f6 |
--- /dev/null |
+++ b/media/base/audio_block_fifo.cc |
@@ -0,0 +1,81 @@ |
+// 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_pos_(0) { |
+ // Create |blocks| of audio buses and push them to the containers. |
DaleCurtis
2014/07/14 20:14:27
Since you know the capacity, call audio_blocks_.re
no longer working on chromium
2014/07/15 21:43:13
Done.
|
+ for (int i = 0; i < blocks; ++i) { |
+ scoped_ptr<AudioBus> audio_bus = AudioBus::Create(channels, frames); |
+ audio_blocks_.push_back(audio_bus.release()); |
+ unfilled_blocks_.push(audio_blocks_.back()); |
+ } |
+} |
+ |
+AudioBlockFifo::~AudioBlockFifo() {} |
+ |
+void AudioBlockFifo::Push(const void* source, int frames, |
DaleCurtis
2014/07/14 20:14:27
Is this actually allowed clang-format style? I tho
no longer working on chromium
2014/07/15 21:43:13
I saw both in the current chromium code, so I gues
|
+ int bytes_per_sample) { |
+ DCHECK(source); |
+ DCHECK_GT(frames, 0); |
+ DCHECK_GT(bytes_per_sample, 0); |
+ |
+ 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 = unfilled_blocks_.front(); |
+ CHECK(current_block); |
DaleCurtis
2014/07/14 20:14:27
Seems unnecessary? Just add a DCHECK(!unfilled_bl
no longer working on chromium
2014/07/15 21:43:13
Done.
|
+ DCHECK_EQ(current_block->frames(), block_frames_); |
+ |
+ // 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, move it from |empety_blocks_| |
DaleCurtis
2014/07/14 20:14:27
empty_blocks_
no longer working on chromium
2014/07/15 21:43:12
comment was updated.
|
+ // to |filled_blocks_|. |
+ filled_blocks_.push(current_block); |
+ unfilled_blocks_.pop(); |
+ } |
+ |
+ 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 = filled_blocks_.front(); |
+ unfilled_blocks_.push(audio_bus); |
+ filled_blocks_.pop(); |
+ return audio_bus; |
+} |
+ |
+void AudioBlockFifo::Clear() { |
+ write_pos_ = 0; |
+ while (!filled_blocks_.empty()) { |
+ AudioBus* audio_bus = filled_blocks_.front(); |
+ unfilled_blocks_.push(audio_bus); |
+ filled_blocks_.pop(); |
+ } |
+} |
+ |
+int AudioBlockFifo::unfilled_frames() const { |
+ DCHECK_GE(static_cast<int>(unfilled_blocks_.size() * block_frames_), |
DaleCurtis
2014/07/14 20:14:27
Since you're making calculations here, this can't
no longer working on chromium
2014/07/15 21:43:13
GetUnfilledFrames() sounds good since available_bl
|
+ write_pos_); |
+ return unfilled_blocks_.size() * block_frames_ - write_pos_; |
+} |
+ |
+} // namespace media |