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

Unified Diff: media/base/audio_rechunker.cc

Issue 1647773002: MediaStream audio sourcing: Bypass audio processing for non-WebRTC cases. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: NOT FOR REVIEW -- This will be broken-up across multiple CLs. Created 4 years, 10 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_rechunker.h ('k') | media/base/audio_rechunker_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/audio_rechunker.cc
diff --git a/media/base/audio_rechunker.cc b/media/base/audio_rechunker.cc
new file mode 100644
index 0000000000000000000000000000000000000000..72326028db69535f299b1dc16c0c55aefe3b585d
--- /dev/null
+++ b/media/base/audio_rechunker.cc
@@ -0,0 +1,80 @@
+// Copyright 2016 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_rechunker.h"
+
+namespace media {
+
+AudioRechunker::AudioRechunker(base::TimeDelta output_duration,
+ const RechunkedAudioCallback& callback)
+ : output_duration_(output_duration), callback_(callback),
+ sample_rate_(0), output_frames_(0) {
+ DCHECK_GT(output_duration_, base::TimeDelta());
+ DCHECK(!callback_.is_null());
+}
+
+AudioRechunker::~AudioRechunker() {}
+
+bool AudioRechunker::SetSampleRate(int sample_rate) {
+ DCHECK_GT(sample_rate, 0);
+
+ sample_rate_ = sample_rate;
+ audio_queue_.reset();
+ pending_frames_= 0;
+
+ const int64_t numerator = sample_rate * output_duration_.InMicroseconds();
+ output_frames_ = numerator / base::Time::kMicrosecondsPerSecond;
+ DCHECK_GT(output_frames_, 0);
+ return ((numerator % base::Time::kMicrosecondsPerSecond) == 0);
+}
+
+void AudioRechunker::Push(const AudioBus& input_bus,
+ base::TimeDelta reference_timestamp) {
+ DCHECK_GT(output_frames_, 0);
+
+ // Fast path: No buffering required.
+ if ((pending_frames_ == 0) && (input_bus.frames() == output_frames_)) {
+ callback_.Run(input_bus, reference_timestamp);
+ return;
+ }
+
+ // Lazy-create the |audio_queue_| if needed.
+ if (!audio_queue_ || audio_queue_->channels() != input_bus.channels())
+ audio_queue_ = AudioBus::Create(input_bus.channels(), output_frames_);
+
+ // Adjust |reference_timestamp| to be that of the first sample in
+ // |audio_queue_| since that will be the first sample delivered via the
+ // |callback_|.
+ reference_timestamp -= base::TimeDelta::FromMicroseconds(
+ pending_frames_ * base::Time::kMicrosecondsPerSecond / sample_rate_);
+
+ // Repeatedly fill up |audio_queue_| with more sample frames from |input_bus|
+ // and deliver batches until all sample frames in |input_bus| have been
+ // consumed.
+ int source_pos = 0;
+ do {
+ // Attempt to fill |audio_queue_| completely.
+ const int frames_to_push =
+ std::min(static_cast<int>(input_bus.frames() - source_pos),
+ output_frames_ - pending_frames_);
+ if (frames_to_push > 0) {
+ DVLOG(2) << "Enqueuing " << frames_to_push << " frames.";
+ input_bus.CopyPartialFramesTo(source_pos, frames_to_push, pending_frames_,
+ audio_queue_.get());
+ pending_frames_ += frames_to_push;
+ source_pos += frames_to_push;
+ }
+
+ // If |audio_queue_| has been filled completely, deliver the re-chunked
+ // audio to the consumer.
+ if (pending_frames_ == output_frames_) {
+ DVLOG(2) << "Delivering another " << pending_frames_ << " frames.";
+ callback_.Run(*audio_queue_, reference_timestamp);
+ reference_timestamp += output_duration_;
+ pending_frames_ = 0;
+ }
+ } while (source_pos < input_bus.frames());
+}
+
+} // namespace media
« no previous file with comments | « media/base/audio_rechunker.h ('k') | media/base/audio_rechunker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698