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

Unified 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. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/base/audio_block_fifo.h ('k') | media/base/audio_block_fifo_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/audio_block_fifo.cc
diff --git a/media/base/audio_block_fifo.cc b/media/base/audio_block_fifo.cc
index 5e8c9b37949babea29b10a464b2a6e3fd9189610..000d13100ca325969521b21ab286a65703fd4cf5 100644
--- a/media/base/audio_block_fifo.cc
+++ b/media/base/audio_block_fifo.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <algorithm>
+
#include "media/base/audio_block_fifo.h"
#include "base/logging.h"
@@ -9,17 +11,13 @@
namespace media {
AudioBlockFifo::AudioBlockFifo(int channels, int frames, int blocks)
- : block_frames_(frames),
+ : channels_(channels),
+ block_frames_(frames),
write_block_(0),
read_block_(0),
available_blocks_(0),
write_pos_(0) {
- // Create |blocks| of audio buses and push them to the containers.
- audio_blocks_.reserve(blocks);
- for (int i = 0; i < blocks; ++i) {
- scoped_ptr<AudioBus> audio_bus = AudioBus::Create(channels, frames);
- audio_blocks_.push_back(audio_bus.release());
- }
+ IncreaseCapacity(blocks);
}
AudioBlockFifo::~AudioBlockFifo() {}
@@ -31,6 +29,7 @@ void AudioBlockFifo::Push(const void* source,
DCHECK_GT(frames, 0);
DCHECK_GT(bytes_per_sample, 0);
DCHECK_LT(available_blocks_, static_cast<int>(audio_blocks_.size()));
+ CHECK_LE(frames, GetUnfilledFrames());
const uint8* source_ptr = static_cast<const uint8*>(source);
int frames_to_push = frames;
@@ -54,7 +53,7 @@ void AudioBlockFifo::Push(const void* source,
++available_blocks_;
}
- source_ptr += push_frames * bytes_per_sample * current_block->channels();
+ source_ptr += push_frames * bytes_per_sample * channels_;
frames_to_push -= push_frames;
DCHECK_GE(frames_to_push, 0);
}
@@ -86,4 +85,34 @@ int AudioBlockFifo::GetUnfilledFrames() const {
return unfilled_frames;
}
+void AudioBlockFifo::IncreaseCapacity(int blocks) {
+ DCHECK_GT(blocks, 0);
+
+ // Create |blocks| of audio buses and insert them to the containers.
+ audio_blocks_.reserve(audio_blocks_.size() + blocks);
+
+ const int original_size = audio_blocks_.size();
+ for (int i = 0; i < blocks; ++i) {
+ audio_blocks_.push_back(
+ AudioBus::Create(channels_, block_frames_).release());
+ }
+
+ if (!original_size)
+ return;
+
+ std::rotate(audio_blocks_.begin() + read_block_,
+ audio_blocks_.begin() + original_size,
+ audio_blocks_.end());
+
+ // Update the write pointer if it is on top of the new inserted blocks.
+ if (write_block_ >= read_block_)
+ write_block_ += blocks;
+
+ // Update the read pointers correspondingly.
+ read_block_ += blocks;
+
+ DCHECK_LT(read_block_, static_cast<int>(audio_blocks_.size()));
+ DCHECK_LT(write_block_, static_cast<int>(audio_blocks_.size()));
+}
+
} // namespace media
« no previous file with comments | « media/base/audio_block_fifo.h ('k') | media/base/audio_block_fifo_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698