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