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

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: Added frames_to_consum to Consume() 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..be3aaa8a5c63756ec66a9f3c04c8945ba4527ce5
--- /dev/null
+++ b/media/base/audio_fifo.cc
@@ -0,0 +1,123 @@
+// 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"
DaleCurtis 2012/09/06 11:14:13 alphabetical order (should be below base include)
henrika (OOO until Aug 14) 2012/09/06 11:52:46 Fixed. Don't need this one at all actually.
scherkus (not reviewing) 2012/09/06 13:53:57 a->z ordering
henrika (OOO until Aug 14) 2012/09/07 14:10:37 Done.
+#include "base/logging.h"
+
+namespace media {
+
+static void GetSizes(
DaleCurtis 2012/09/06 11:14:13 Docs.
henrika (OOO until Aug 14) 2012/09/06 11:52:46 Done.
scherkus (not reviewing) 2012/09/06 13:53:57 docs please
henrika (OOO until Aug 14) 2012/09/07 14:10:37 Done.
+ int pos, int max_size, int in_size, int* size, int* wrap_size) {
+ if (pos + in_size > max_size) {
+ // Wrapping is required => derive size of each segment.
+ *size = max_size - pos;
+ *wrap_size = in_size - *size;
+ } else {
+ // Wrapping is not required.
+ *size = in_size;
+ *wrap_size = 0;
+ }
+}
+
+static int UpdatePos(int pos, int step, int max_size) {
DaleCurtis 2012/09/06 11:14:13 Docs.
henrika (OOO until Aug 14) 2012/09/06 11:52:46 Done.
scherkus (not reviewing) 2012/09/06 13:53:57 docs
henrika (OOO until Aug 14) 2012/09/07 14:10:37 Done.
+ 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, int frames_to_consume) {
+ 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.
+ if (frames_to_consume > frames_in_fifo_) {
+ DLOG(ERROR) << "FIFO underrun.";
scherkus (not reviewing) 2012/09/06 13:53:57 this is something that I feel we may want to bump
henrika (OOO until Aug 14) 2012/09/07 14:10:37 Got it. Will modify.
+ return false;
+ }
+
+ // A copy from the FIFO to |destination| will only be performed if the
+ // allocated memory in |destination| is sufficient.
+ if (frames_to_consume > destination->frames()) {
+ DLOG(ERROR) << "Insufficient space in destination.";
scherkus (not reviewing) 2012/09/06 13:53:57 ditto
henrika (OOO until Aug 14) 2012/09/07 14:10:37 see above
+ 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 consume_size = 0;
+ int wrap_size = 0;
+ GetSizes(read_pos_, max_frames(), frames_to_consume,
+ &consume_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);
+
+ // Copy a selected part of the FIFO to the destination.
+ memcpy(&dest[0], &src[read_pos_], consume_size * sizeof(src[0]));
+ if (wrap_size > 0) {
+ // Wrapping is needed: copy remaining part to the destination.
+ memcpy(&dest[consume_size], &src[0], wrap_size * sizeof(src[0]));
+ }
+ }
+
+ frames_in_fifo_ -= frames_to_consume;
+ read_pos_ = UpdatePos(read_pos_, frames_to_consume, 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