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

Unified Diff: media/base/audio_fifo.cc

Issue 10912079: Adds AudioFifo class to Chrome media. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes based on review by Dale Created 8 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
Index: media/base/audio_fifo.cc
diff --git a/media/base/audio_fifo.cc b/media/base/audio_fifo.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e00f5baa941bbae7acc3b27f7d7ac4bdadc21454
--- /dev/null
+++ b/media/base/audio_fifo.cc
@@ -0,0 +1,116 @@
+// Copyright (c) 2012 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_fifo.h"
+
+#include "media/audio/audio_parameters.h"
+#include "base/logging.h"
+
+namespace media {
+
+static void GetSizes(
+ int pos, int max_size, int src_size, int* size, int* wrap_size) {
+ if (pos + src_size > max_size) {
+ // Wrapping is required => derive size of each segment.
+ *size = max_size - pos;
+ *wrap_size = src_size - *size;
+ } else {
+ // Wrapping is not required.
+ *size = src_size;
+ *wrap_size = 0;
+ }
+}
+
+static int UpdatePos(int pos, int step, int max_size) {
+ return ((pos + step) % max_size);
+}
+
+AudioFifo::AudioFifo(int channels, int frames)
+ : audio_bus_(AudioBus::Create(channels, frames)),
+ max_frames_in_fifo_(frames),
+ frames_in_fifo_(0),
+ read_pos_(0),
+ write_pos_(0) {}
+
+AudioFifo::~AudioFifo() {}
+
+bool AudioFifo::Push(const AudioBus* source) {
+ DCHECK(source);
+ DCHECK_EQ(source->channels(), audio_bus_->channels());
+
+ // Ensure that there is space for the new data in the FIFO.
+ const int source_size = source->frames();
+ if (frames_in_fifo_ + source_size > max_frames()) {
+ DLOG(ERROR) << "FIFO overflow.";
+ return false;
+ }
+
+ // Figure out if wrapping is needed and if so what segment sizes we need
+ // when adding the new audio bus content to the FIFO.
+ int append_size = 0;
+ int wrap_size = 0;
+ GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size);
+
+ // Copy all channels from the source to the FIFO. Wrap around if needed.
+ for (int ch = 0; ch < source->channels(); ++ch) {
+ float* dest = audio_bus_->channel(ch);
+ const float* src = source->channel(ch);
+
+ // Append part of (or the complete) source to the FIFO.
+ memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0]));
+ if (wrap_size > 0) {
+ // Wrapping is needed: copy remaining part from the source to the FIFO.
+ memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0]));
+ }
+ }
+
+ frames_in_fifo_ += source_size;
+ DCHECK_LE(frames_in_fifo_, max_frames());
+ write_pos_ = UpdatePos(write_pos_, source_size, max_frames());
+ return true;
+}
+
+bool AudioFifo::Consume(AudioBus* destination) {
+ DCHECK(destination);
+ DCHECK_EQ(destination->channels(), audio_bus_->channels());
+
+ // It is not possible to ask for more data than what is available in the FIFO.
+ const int dest_size = destination->frames();
+ if (dest_size > frames_in_fifo_) {
+ DLOG(ERROR) << "FIFO underrun.";
+ return false;
+ }
+
+ // Figure out if wrapping is needed and if so what segment sizes we need
+ // when removing audio bus content from the FIFO.
+ int remove_size = 0;
+ int wrap_size = 0;
+ GetSizes(read_pos_, max_frames(), dest_size, &remove_size, &wrap_size);
+
+ // For all channels, remove the requested amount of data from the FIFO
+ // and copy the content to the destination. Wrap around if needed.
+ for (int ch = 0; ch < destination->channels(); ++ch) {
+ float* dest = destination->channel(ch);
+ const float* src = audio_bus_->channel(ch);
+
+ // Append part of (or the complete) source to the destination.
+ memcpy(&dest[0], &src[read_pos_], remove_size * sizeof(src[0]));
+ if (wrap_size > 0) {
+ // Wrapping is needed: copy remaining part to the destination.
+ memcpy(&dest[wrap_size], &src[0], wrap_size * sizeof(src[0]));
+ }
+ }
+
+ frames_in_fifo_ -= dest_size;
+ read_pos_ = UpdatePos(read_pos_, dest_size, max_frames());
+ return true;
+}
+
+void AudioFifo::Clear() {
+ frames_in_fifo_ = 0;
+ read_pos_ = 0;
+ write_pos_ = 0;
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698