OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/audio_block_fifo.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace media { |
| 10 |
| 11 AudioBlockFifo::AudioBlockFifo(int channels, int frames, int blocks) |
| 12 : block_frames_(frames), |
| 13 write_block_(0), |
| 14 read_block_(0), |
| 15 available_blocks_(0), |
| 16 write_pos_(0) { |
| 17 // Create |blocks| of audio buses and push them to the containers. |
| 18 audio_blocks_.reserve(blocks); |
| 19 for (int i = 0; i < blocks; ++i) { |
| 20 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(channels, frames); |
| 21 audio_blocks_.push_back(audio_bus.release()); |
| 22 } |
| 23 } |
| 24 |
| 25 AudioBlockFifo::~AudioBlockFifo() {} |
| 26 |
| 27 void AudioBlockFifo::Push(const void* source, |
| 28 int frames, |
| 29 int bytes_per_sample) { |
| 30 DCHECK(source); |
| 31 DCHECK_GT(frames, 0); |
| 32 DCHECK_GT(bytes_per_sample, 0); |
| 33 DCHECK_LT(available_blocks_, static_cast<int>(audio_blocks_.size())); |
| 34 |
| 35 const uint8* source_ptr = static_cast<const uint8*>(source); |
| 36 int frames_to_push = frames; |
| 37 while (frames_to_push) { |
| 38 // Get the current write block. |
| 39 AudioBus* current_block = audio_blocks_[write_block_]; |
| 40 |
| 41 // Figure out what segment sizes we need when adding the new content to |
| 42 // the FIFO. |
| 43 const int push_frames = |
| 44 std::min(block_frames_ - write_pos_, frames_to_push); |
| 45 |
| 46 // Deinterleave the content to the FIFO and update the |write_pos_|. |
| 47 current_block->FromInterleaved(source_ptr, push_frames, bytes_per_sample); |
| 48 write_pos_ = (write_pos_ + push_frames) % block_frames_; |
| 49 if (!write_pos_) { |
| 50 // The current block is completely filled, increment |write_block_| and |
| 51 // |available_blocks_|. |
| 52 write_block_ = (write_block_ + 1) % audio_blocks_.size(); |
| 53 ++available_blocks_; |
| 54 } |
| 55 |
| 56 source_ptr += push_frames * bytes_per_sample * current_block->channels(); |
| 57 frames_to_push -= push_frames; |
| 58 } |
| 59 } |
| 60 |
| 61 const AudioBus* AudioBlockFifo::Consume() { |
| 62 DCHECK(available_blocks_); |
| 63 AudioBus* audio_bus = audio_blocks_[read_block_]; |
| 64 read_block_ = (read_block_ + 1) % audio_blocks_.size(); |
| 65 --available_blocks_; |
| 66 return audio_bus; |
| 67 } |
| 68 |
| 69 void AudioBlockFifo::Clear() { |
| 70 write_pos_ = 0; |
| 71 write_block_ = 0; |
| 72 read_block_ = 0; |
| 73 available_blocks_ = 0; |
| 74 } |
| 75 |
| 76 int AudioBlockFifo::GetUnfilledFrames() const { |
| 77 const int unfilled_frames = |
| 78 (audio_blocks_.size() - available_blocks_) * block_frames_ - write_pos_; |
| 79 DCHECK_GE(unfilled_frames, 0); |
| 80 return unfilled_frames; |
| 81 } |
| 82 |
| 83 } // namespace media |
OLD | NEW |