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

Unified Diff: media/audio/simple_sources.cc

Issue 922663002: Moved the fake input stream's processing onto the audio worker thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updating GN files Created 5 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
Index: media/audio/simple_sources.cc
diff --git a/media/audio/simple_sources.cc b/media/audio/simple_sources.cc
index 28a0f5f881b7a4a84cda8bbb61d8fd962bcc1073..dc68622052eb5e0678c7f5b5f8f7f8af40df65e3 100644
--- a/media/audio/simple_sources.cc
+++ b/media/audio/simple_sources.cc
@@ -9,10 +9,99 @@
#include <algorithm>
+#include "base/files/file.h"
+#include "base/lazy_instance.h"
#include "base/logging.h"
+#include "media/audio/sounds/wav_audio_handler.h"
+#include "media/base/audio_bus.h"
namespace media {
+namespace {
DaleCurtis 2015/02/19 19:54:52 media/ code uses static functions instead of anony
phoglund_chromium 2015/02/20 14:22:00 Done.
+
+// Opens |wav_filename|, reads it and loads it as a wav file. This function will
+// bluntly trigger CHECKs if we can't read the file or if it's malformed. The
+// caller takes ownership of the returned data. The size of the data is stored
+// in |read_length|.
+scoped_ptr<uint8[]> ReadWavFile(const base::FilePath& wav_filename,
+ size_t* file_length) {
+ base::File wav_file(
+ wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ);
+ if (!wav_file.IsValid()) {
+ CHECK(false) << "Failed to read " << wav_filename.value() << " as input to "
+ << "the fake device.";
+ return nullptr;
+ }
+
+ size_t wav_file_length = wav_file.GetLength();
+ CHECK_GT(wav_file_length, 0u)
+ << "Input file to fake device is empty: " << wav_filename.value();
+
+ uint8* wav_file_data = new uint8[wav_file_length];
+ size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data),
+ wav_file_length);
+ if (read_bytes != wav_file_length) {
+ CHECK(false) << "Failed to read all bytes of " << wav_filename.value();
DaleCurtis 2015/02/19 19:54:53 No need to return() after check; just CHECK_EQ() h
phoglund_chromium 2015/02/20 14:22:00 Done.
+ return nullptr;
+ }
+ *file_length = wav_file_length;
+ return scoped_ptr<uint8[]>(wav_file_data);
+}
+
+// Opens |wav_filename|, reads it and loads it as a Wav file. This function will
+// bluntly trigger CHECKs if we can't read the file or if it's malformed.
+scoped_ptr<media::WavAudioHandler> CreateWavAudioHandler(
+ const base::FilePath& wav_filename, const uint8* wav_file_data,
+ size_t wav_file_length, const AudioParameters& expected_params) {
+ base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data),
+ wav_file_length);
+ scoped_ptr<media::WavAudioHandler> wav_audio_handler(
+ new media::WavAudioHandler(wav_data));
+ return wav_audio_handler.Pass();
+}
+
+// These values are based on experiments for local-to-local
+// PeerConnection to demonstrate audio/video synchronization.
+const int kBeepDurationMilliseconds = 20;
+const int kBeepFrequency = 400;
+
+// Intervals between two automatic beeps.
+const int kAutomaticBeepIntervalInMs = 500;
+
+// Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless
+// users explicitly call BeepOnce(), which will disable the automatic beep.
+class BeepContext {
+ public:
+ BeepContext() : beep_once_(false), automatic_beep_(true) {}
+
+ void SetBeepOnce(bool enable) {
+ base::AutoLock auto_lock(lock_);
+ beep_once_ = enable;
+
+ // Disable the automatic beep if users explicit set |beep_once_| to true.
+ if (enable)
+ automatic_beep_ = false;
+ }
+ bool beep_once() const {
DaleCurtis 2015/02/19 19:54:53 Add 1 line between each method.
phoglund_chromium 2015/02/20 14:22:00 Done.
+ base::AutoLock auto_lock(lock_);
+ return beep_once_;
+ }
+ bool automatic_beep() const {
+ base::AutoLock auto_lock(lock_);
+ return automatic_beep_;
+ }
+
+ private:
+ mutable base::Lock lock_;
+ bool beep_once_;
+ bool automatic_beep_;
+};
+
+static base::LazyInstance<BeepContext> g_beep_context =
+ LAZY_INSTANCE_INITIALIZER;
DaleCurtis 2015/02/19 19:54:53 Change this to a leaky lazy instance.
phoglund_chromium 2015/02/20 14:21:59 Done.
+
+} // namespace
+
//////////////////////////////////////////////////////////////////////////////
// SineWaveAudioSource implementation.
@@ -63,4 +152,134 @@ void SineWaveAudioSource::Reset() {
time_state_ = 0;
}
+FileSource::FileSource(const AudioParameters& params,
+ const base::FilePath& path_to_wav_file)
+ : params_(params),
+ path_to_wav_file_(path_to_wav_file),
+ wav_file_read_pos_(0),
+ buffer_size_(params.channels() * params.bits_per_sample() *
DaleCurtis 2015/02/19 19:54:53 params.GetBytesPerBuffer() ?
phoglund_chromium 2015/02/20 14:22:00 Yeah, I noticed I don't even need that anymore so
+ params.frames_per_buffer() / 8) {
+}
+
+FileSource::~FileSource() {
+}
+
+void FileSource::Open() {
+ // Read the file, and put its data in a scoped_ptr so it gets deleted later.
+ size_t file_length = 0;
+ wav_file_data_ = ReadWavFile(path_to_wav_file_, &file_length);
+ wav_audio_handler_ = CreateWavAudioHandler(
+ path_to_wav_file_, wav_file_data_.get(), file_length, params_);
+
+ // Hook us up so we pull in data from the file into the converter. We need to
+ // modify the wav file's audio parameters since we'll be reading small slices
+ // of it at a time and not the whole thing (like 10 ms at a time).
+ AudioParameters file_audio_slice(
+ wav_audio_handler_->params().format(),
+ wav_audio_handler_->params().channel_layout(),
+ wav_audio_handler_->params().sample_rate(),
+ wav_audio_handler_->params().bits_per_sample(),
+ params_.frames_per_buffer());
+
+ file_audio_converter_.reset(
+ new AudioConverter(file_audio_slice, params_, false));
+ file_audio_converter_->AddInput(this);
+}
+
+int FileSource::OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) {
+ DCHECK(wav_audio_handler_.get() != nullptr);
DaleCurtis 2015/02/19 19:54:53 Should be able to just DCHECK(wav_audio_handler_)
phoglund_chromium 2015/02/20 14:21:59 Done.
+
+ // Stop playing if we've played out the whole file.
+ if (wav_audio_handler_->AtEnd(wav_file_read_pos_))
+ return 0;
+
+ // This pulls data from ProvideInput.
+ file_audio_converter_->Convert(audio_bus);
+ return audio_bus->frames();
+}
+
+double FileSource::ProvideInput(AudioBus* audio_bus_into_converter,
+ base::TimeDelta buffer_delay) {
+ // Unfilled frames will be zeroed by CopyTo.
+ size_t bytes_written;
+ wav_audio_handler_->CopyTo(audio_bus_into_converter, wav_file_read_pos_,
+ &bytes_written);
+ wav_file_read_pos_ += bytes_written;
+ return 1.0;
+};
+
+BeepingSource::BeepingSource(const AudioParameters& params)
+ : buffer_size_(params.channels() * params.bits_per_sample() *
DaleCurtis 2015/02/19 19:54:53 Ditto.
phoglund_chromium 2015/02/20 14:22:00 Done.
phoglund_chromium 2015/02/20 14:22:00 Done.
+ params.frames_per_buffer() / 8),
+ params_(params),
+ beep_duration_in_buffers_(kBeepDurationMilliseconds *
+ params.sample_rate() /
+ params.frames_per_buffer() /
+ 1000),
+ beep_generated_in_buffers_(0),
+ beep_period_in_frames_(params.sample_rate() / kBeepFrequency) {
+ buffer_.reset(new uint8[buffer_size_]);
DaleCurtis 2015/02/19 19:54:53 Could just be a constructor param if you want.
phoglund_chromium 2015/02/20 14:21:59 What are you referring to, the buffer size?...
DaleCurtis 2015/02/20 18:45:05 Yeah, just saying you can move buffer_() into cons
phoglund_chromium 2015/02/23 09:16:41 Done.
+ memset(buffer_.get(), 0, buffer_size_);
DaleCurtis 2015/02/19 19:54:53 Unnecessary since you clear it at the start of Onm
phoglund_chromium 2015/02/20 14:22:00 Done.
+}
+
+BeepingSource::~BeepingSource() {
+}
+
+int BeepingSource::OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) {
+ // Accumulate the time from the last beep.
+ interval_from_last_beep_ += base::TimeTicks::Now() - last_callback_time_;
+ last_callback_time_ = base::TimeTicks::Now();
+
+ memset(buffer_.get(), 0, buffer_size_);
+ bool should_beep = false;
+ {
DaleCurtis 2015/02/19 19:54:54 Extra {} seem unnecessary, no scoped lock.
phoglund_chromium 2015/02/20 14:22:00 Done.
phoglund_chromium 2015/02/20 14:22:00 Done.
+ BeepContext* beep_context = g_beep_context.Pointer();
+ if (beep_context->automatic_beep()) {
+ base::TimeDelta delta = interval_from_last_beep_ -
+ base::TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs);
+ if (delta > base::TimeDelta()) {
+ should_beep = true;
+ interval_from_last_beep_ = delta;
+ }
+ } else {
+ should_beep = beep_context->beep_once();
+ beep_context->SetBeepOnce(false);
+ }
+ }
+
+ // If this object was instructed to generate a beep or has started to
+ // generate a beep sound.
+ if (should_beep || beep_generated_in_buffers_) {
+ // Compute the number of frames to output high value. Then compute the
+ // number of bytes based on channels and bits per channel.
+ int high_frames = beep_period_in_frames_ / 2;
+ int high_bytes = high_frames * params_.bits_per_sample() *
+ params_.channels() / 8;
+
+ // Separate high and low with the same number of bytes to generate a
+ // square wave.
+ int position = 0;
+ while (position + high_bytes <= buffer_size_) {
+ // Write high values first.
+ memset(buffer_.get() + position, 128, high_bytes);
+ // Then leave low values in the buffer with |high_bytes|.
+ position += high_bytes * 2;
+ }
+
+ ++beep_generated_in_buffers_;
+ if (beep_generated_in_buffers_ >= beep_duration_in_buffers_)
+ beep_generated_in_buffers_ = 0;
+ }
+
+ audio_bus->FromInterleaved(
+ buffer_.get(), audio_bus->frames(), params_.bits_per_sample() / 8);
+ return audio_bus->frames();
+}
+
DaleCurtis 2015/02/19 19:54:53 Extra space.
phoglund_chromium 2015/02/20 14:21:59 Done.
+
+void BeepingSource::BeepOnce() {
+ BeepContext* beep_context = g_beep_context.Pointer();
DaleCurtis 2015/02/19 19:54:53 Combine these two lines?
phoglund_chromium 2015/02/20 14:21:59 Done.
+ beep_context->SetBeepOnce(true);
+}
+
} // namespace media

Powered by Google App Engine
This is Rietveld 408576698