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

Side by Side Diff: media/base/audio_block_fifo.cc

Issue 557693003: Dynamically allocate more memory to AudioBlockFifo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed the comments and added unittests. Created 6 years, 3 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 unified diff | Download patch
OLDNEW
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 : channels_(channels),
13 block_frames_(frames),
13 write_block_(0), 14 write_block_(0),
14 read_block_(0), 15 read_block_(0),
15 available_blocks_(0), 16 available_blocks_(0),
16 write_pos_(0) { 17 write_pos_(0) {
17 // Create |blocks| of audio buses and push them to the containers. 18 IncreaseCapacity(blocks);
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 } 19 }
24 20
25 AudioBlockFifo::~AudioBlockFifo() {} 21 AudioBlockFifo::~AudioBlockFifo() {}
26 22
27 void AudioBlockFifo::Push(const void* source, 23 void AudioBlockFifo::Push(const void* source,
28 int frames, 24 int frames,
29 int bytes_per_sample) { 25 int bytes_per_sample) {
30 DCHECK(source); 26 DCHECK(source);
31 DCHECK_GT(frames, 0); 27 DCHECK_GT(frames, 0);
32 DCHECK_GT(bytes_per_sample, 0); 28 DCHECK_GT(bytes_per_sample, 0);
33 DCHECK_LT(available_blocks_, static_cast<int>(audio_blocks_.size())); 29 DCHECK_LT(available_blocks_, static_cast<int>(audio_blocks_.size()));
30 CHECK_LE(frames, GetUnfilledFrames());
34 31
35 const uint8* source_ptr = static_cast<const uint8*>(source); 32 const uint8* source_ptr = static_cast<const uint8*>(source);
36 int frames_to_push = frames; 33 int frames_to_push = frames;
37 while (frames_to_push) { 34 while (frames_to_push) {
38 // Get the current write block. 35 // Get the current write block.
39 AudioBus* current_block = audio_blocks_[write_block_]; 36 AudioBus* current_block = audio_blocks_[write_block_];
40 37
41 // Figure out what segment sizes we need when adding the new content to 38 // Figure out what segment sizes we need when adding the new content to
42 // the FIFO. 39 // the FIFO.
43 const int push_frames = 40 const int push_frames =
44 std::min(block_frames_ - write_pos_, frames_to_push); 41 std::min(block_frames_ - write_pos_, frames_to_push);
45 42
46 // Deinterleave the content to the FIFO and update the |write_pos_|. 43 // Deinterleave the content to the FIFO and update the |write_pos_|.
47 current_block->FromInterleavedPartial( 44 current_block->FromInterleavedPartial(
48 source_ptr, write_pos_, push_frames, bytes_per_sample); 45 source_ptr, write_pos_, push_frames, bytes_per_sample);
49 write_pos_ = (write_pos_ + push_frames) % block_frames_; 46 write_pos_ = (write_pos_ + push_frames) % block_frames_;
50 if (!write_pos_) { 47 if (!write_pos_) {
51 // The current block is completely filled, increment |write_block_| and 48 // The current block is completely filled, increment |write_block_| and
52 // |available_blocks_|. 49 // |available_blocks_|.
53 write_block_ = (write_block_ + 1) % audio_blocks_.size(); 50 write_block_ = (write_block_ + 1) % audio_blocks_.size();
54 ++available_blocks_; 51 ++available_blocks_;
55 } 52 }
56 53
57 source_ptr += push_frames * bytes_per_sample * current_block->channels(); 54 source_ptr += push_frames * bytes_per_sample * channels_;
58 frames_to_push -= push_frames; 55 frames_to_push -= push_frames;
59 DCHECK_GE(frames_to_push, 0); 56 DCHECK_GE(frames_to_push, 0);
60 } 57 }
61 } 58 }
62 59
63 const AudioBus* AudioBlockFifo::Consume() { 60 const AudioBus* AudioBlockFifo::Consume() {
64 DCHECK(available_blocks_); 61 DCHECK(available_blocks_);
65 AudioBus* audio_bus = audio_blocks_[read_block_]; 62 AudioBus* audio_bus = audio_blocks_[read_block_];
66 read_block_ = (read_block_ + 1) % audio_blocks_.size(); 63 read_block_ = (read_block_ + 1) % audio_blocks_.size();
67 --available_blocks_; 64 --available_blocks_;
(...skipping 11 matching lines...) Expand all
79 return available_blocks_ * block_frames_ + write_pos_; 76 return available_blocks_ * block_frames_ + write_pos_;
80 } 77 }
81 78
82 int AudioBlockFifo::GetUnfilledFrames() const { 79 int AudioBlockFifo::GetUnfilledFrames() const {
83 const int unfilled_frames = 80 const int unfilled_frames =
84 (audio_blocks_.size() - available_blocks_) * block_frames_ - write_pos_; 81 (audio_blocks_.size() - available_blocks_) * block_frames_ - write_pos_;
85 DCHECK_GE(unfilled_frames, 0); 82 DCHECK_GE(unfilled_frames, 0);
86 return unfilled_frames; 83 return unfilled_frames;
87 } 84 }
88 85
86 void AudioBlockFifo::IncreaseCapacity(int blocks) {
DaleCurtis 2014/09/16 19:36:53 DCHECK_GT(blocks, 0); ?
no longer working on chromium 2014/09/18 12:42:49 Done.
87 // Create |blocks| of audio buses and insert them to the containers.
88 audio_blocks_.reserve(audio_blocks_.size() + blocks);
89
DaleCurtis 2014/09/16 19:36:53 Instead of repeatedly shifting the elements due to
no longer working on chromium 2014/09/18 12:42:49 Done with using rotate. Though I don't really thi
DaleCurtis 2014/09/18 17:12:03 Vectors have contiguous memory layout and insert()
90 // Insert the new blocks of buffers right before the read block.
no longer working on chromium 2014/09/16 18:00:28 we insert the new buffers before the read pointer
91 ScopedVector<AudioBus>::iterator it = audio_blocks_.begin() + read_block_;
92 for (int i = 0; i < blocks; ++i) {
93 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(channels_, block_frames_);
94 it = audio_blocks_.insert(it, audio_bus.release());
95 }
96
97 // Update the write pointer if it is on top of the new inserted blocks.
98 if (write_block_ >= read_block_)
no longer working on chromium 2014/09/16 18:00:28 if write_block_ < read_block_, it means write poin
no longer working on chromium 2014/09/18 12:42:49 Done.
99 write_block_ += blocks % audio_blocks_.size();
100
101 // Update the read pointers correspondingly.
102 read_block_ += blocks % audio_blocks_.size();;
DaleCurtis 2014/09/16 19:36:53 This should never wrap. You can remove the modulus
no longer working on chromium 2014/09/18 12:42:49 Done.
103 }
104
89 } // namespace media 105 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698