| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/fake_audio_input_stream.h" | 5 #include "media/audio/fake_audio_input_stream.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 9 #include "base/files/file.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/lazy_instance.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/time/time.h" |
| 11 #include "media/audio/audio_manager_base.h" | 13 #include "media/audio/audio_manager_base.h" |
| 14 #include "media/audio/simple_sources.h" |
| 12 #include "media/base/audio_bus.h" | 15 #include "media/base/audio_bus.h" |
| 13 #include "media/base/media_switches.h" | 16 #include "media/base/media_switches.h" |
| 14 | 17 |
| 15 using base::TimeTicks; | |
| 16 using base::TimeDelta; | |
| 17 | |
| 18 namespace media { | 18 namespace media { |
| 19 | 19 |
| 20 namespace { | |
| 21 | |
| 22 // These values are based on experiments for local-to-local | |
| 23 // PeerConnection to demonstrate audio/video synchronization. | |
| 24 const int kBeepDurationMilliseconds = 20; | |
| 25 const int kBeepFrequency = 400; | |
| 26 | |
| 27 // Intervals between two automatic beeps. | |
| 28 const int kAutomaticBeepIntervalInMs = 500; | |
| 29 | |
| 30 // Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless | |
| 31 // users explicitly call BeepOnce(), which will disable the automatic beep. | |
| 32 class BeepContext { | |
| 33 public: | |
| 34 BeepContext() : beep_once_(false), automatic_beep_(true) {} | |
| 35 | |
| 36 void SetBeepOnce(bool enable) { | |
| 37 base::AutoLock auto_lock(lock_); | |
| 38 beep_once_ = enable; | |
| 39 | |
| 40 // Disable the automatic beep if users explicit set |beep_once_| to true. | |
| 41 if (enable) | |
| 42 automatic_beep_ = false; | |
| 43 } | |
| 44 bool beep_once() const { | |
| 45 base::AutoLock auto_lock(lock_); | |
| 46 return beep_once_; | |
| 47 } | |
| 48 bool automatic_beep() const { | |
| 49 base::AutoLock auto_lock(lock_); | |
| 50 return automatic_beep_; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 mutable base::Lock lock_; | |
| 55 bool beep_once_; | |
| 56 bool automatic_beep_; | |
| 57 }; | |
| 58 | |
| 59 // Opens |wav_filename|, reads it and loads it as a wav file. This function will | |
| 60 // bluntly trigger CHECKs if we can't read the file or if it's malformed. The | |
| 61 // caller takes ownership of the returned data. The size of the data is stored | |
| 62 // in |read_length|. | |
| 63 scoped_ptr<uint8[]> ReadWavFile(const base::FilePath& wav_filename, | |
| 64 size_t* file_length) { | |
| 65 base::File wav_file( | |
| 66 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 67 if (!wav_file.IsValid()) { | |
| 68 CHECK(false) << "Failed to read " << wav_filename.value() << " as input to " | |
| 69 << "the fake device."; | |
| 70 return nullptr; | |
| 71 } | |
| 72 | |
| 73 size_t wav_file_length = wav_file.GetLength(); | |
| 74 CHECK_GT(wav_file_length, 0u) | |
| 75 << "Input file to fake device is empty: " << wav_filename.value(); | |
| 76 | |
| 77 uint8* wav_file_data = new uint8[wav_file_length]; | |
| 78 size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data), | |
| 79 wav_file_length); | |
| 80 if (read_bytes != wav_file_length) { | |
| 81 CHECK(false) << "Failed to read all bytes of " << wav_filename.value(); | |
| 82 return nullptr; | |
| 83 } | |
| 84 *file_length = wav_file_length; | |
| 85 return scoped_ptr<uint8[]>(wav_file_data); | |
| 86 } | |
| 87 | |
| 88 // Opens |wav_filename|, reads it and loads it as a Wav file. This function will | |
| 89 // bluntly trigger CHECKs if we can't read the file or if it's malformed. | |
| 90 scoped_ptr<media::WavAudioHandler> CreateWavAudioHandler( | |
| 91 const base::FilePath& wav_filename, const uint8* wav_file_data, | |
| 92 size_t wav_file_length, const AudioParameters& expected_params) { | |
| 93 base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data), | |
| 94 wav_file_length); | |
| 95 scoped_ptr<media::WavAudioHandler> wav_audio_handler( | |
| 96 new media::WavAudioHandler(wav_data)); | |
| 97 return wav_audio_handler.Pass(); | |
| 98 } | |
| 99 | |
| 100 static base::LazyInstance<BeepContext> g_beep_context = | |
| 101 LAZY_INSTANCE_INITIALIZER; | |
| 102 | |
| 103 } // namespace | |
| 104 | |
| 105 AudioInputStream* FakeAudioInputStream::MakeFakeStream( | 20 AudioInputStream* FakeAudioInputStream::MakeFakeStream( |
| 106 AudioManagerBase* manager, | 21 AudioManagerBase* manager, |
| 107 const AudioParameters& params) { | 22 const AudioParameters& params) { |
| 108 return new FakeAudioInputStream(manager, params); | 23 return new FakeAudioInputStream(manager, params); |
| 109 } | 24 } |
| 110 | 25 |
| 111 FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager, | 26 FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager, |
| 112 const AudioParameters& params) | 27 const AudioParameters& params) |
| 113 : audio_manager_(manager), | 28 : audio_manager_(manager), |
| 114 callback_(NULL), | 29 callback_(NULL), |
| 115 buffer_size_((params.channels() * params.bits_per_sample() * | 30 fake_audio_worker_(manager->GetWorkerTaskRunner(), params), |
| 116 params.frames_per_buffer()) / | |
| 117 8), | |
| 118 params_(params), | 31 params_(params), |
| 119 task_runner_(manager->GetTaskRunner()), | 32 audio_bus_(AudioBus::Create(params)) { |
| 120 callback_interval_(base::TimeDelta::FromMilliseconds( | |
| 121 (params.frames_per_buffer() * 1000) / params.sample_rate())), | |
| 122 beep_duration_in_buffers_(kBeepDurationMilliseconds * | |
| 123 params.sample_rate() / | |
| 124 params.frames_per_buffer() / | |
| 125 1000), | |
| 126 beep_generated_in_buffers_(0), | |
| 127 beep_period_in_frames_(params.sample_rate() / kBeepFrequency), | |
| 128 audio_bus_(AudioBus::Create(params)), | |
| 129 wav_file_read_pos_(0), | |
| 130 weak_factory_(this) { | |
| 131 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 33 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
| 132 } | 34 } |
| 133 | 35 |
| 134 FakeAudioInputStream::~FakeAudioInputStream() {} | 36 FakeAudioInputStream::~FakeAudioInputStream() { |
| 37 DCHECK(!callback_); |
| 38 } |
| 135 | 39 |
| 136 bool FakeAudioInputStream::Open() { | 40 bool FakeAudioInputStream::Open() { |
| 137 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 41 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
| 138 buffer_.reset(new uint8[buffer_size_]); | |
| 139 memset(buffer_.get(), 0, buffer_size_); | |
| 140 audio_bus_->Zero(); | 42 audio_bus_->Zero(); |
| 141 | 43 |
| 142 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 143 switches::kUseFileForFakeAudioCapture)) { | |
| 144 OpenInFileMode(base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( | |
| 145 switches::kUseFileForFakeAudioCapture)); | |
| 146 } | |
| 147 | |
| 148 return true; | 44 return true; |
| 149 } | 45 } |
| 150 | 46 |
| 151 void FakeAudioInputStream::Start(AudioInputCallback* callback) { | 47 void FakeAudioInputStream::Start(AudioInputCallback* callback) { |
| 152 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 48 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
| 153 DCHECK(!callback_); | |
| 154 callback_ = callback; | 49 callback_ = callback; |
| 155 last_callback_time_ = TimeTicks::Now(); | 50 fake_audio_worker_.Start(base::Bind( |
| 156 | 51 &FakeAudioInputStream::ReadAudioFromSource, base::Unretained(this))); |
| 157 task_runner_->PostDelayedTask( | |
| 158 FROM_HERE, | |
| 159 base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()), | |
| 160 callback_interval_); | |
| 161 } | |
| 162 | |
| 163 void FakeAudioInputStream::DoCallback() { | |
| 164 DCHECK(callback_); | |
| 165 | |
| 166 const TimeTicks now = TimeTicks::Now(); | |
| 167 base::TimeDelta next_callback_time = | |
| 168 last_callback_time_ + callback_interval_ * 2 - now; | |
| 169 | |
| 170 // If we are falling behind, try to catch up as much as we can in the next | |
| 171 // callback. | |
| 172 if (next_callback_time < base::TimeDelta()) | |
| 173 next_callback_time = base::TimeDelta(); | |
| 174 | |
| 175 if (PlayingFromFile()) { | |
| 176 PlayFile(); | |
| 177 } else { | |
| 178 PlayBeep(); | |
| 179 } | |
| 180 | |
| 181 last_callback_time_ = now; | |
| 182 | |
| 183 task_runner_->PostDelayedTask( | |
| 184 FROM_HERE, | |
| 185 base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()), | |
| 186 next_callback_time); | |
| 187 } | |
| 188 | |
| 189 void FakeAudioInputStream::OpenInFileMode(const base::FilePath& wav_filename) { | |
| 190 CHECK(!wav_filename.empty()) | |
| 191 << "You must pass the file to use as argument to --" | |
| 192 << switches::kUseFileForFakeAudioCapture << "."; | |
| 193 | |
| 194 // Read the file, and put its data in a scoped_ptr so it gets deleted later. | |
| 195 size_t file_length = 0; | |
| 196 wav_file_data_ = ReadWavFile(wav_filename, &file_length); | |
| 197 wav_audio_handler_ = CreateWavAudioHandler( | |
| 198 wav_filename, wav_file_data_.get(), file_length, params_); | |
| 199 | |
| 200 // Hook us up so we pull in data from the file into the converter. We need to | |
| 201 // modify the wav file's audio parameters since we'll be reading small slices | |
| 202 // of it at a time and not the whole thing (like 10 ms at a time). | |
| 203 AudioParameters file_audio_slice( | |
| 204 wav_audio_handler_->params().format(), | |
| 205 wav_audio_handler_->params().channel_layout(), | |
| 206 wav_audio_handler_->params().sample_rate(), | |
| 207 wav_audio_handler_->params().bits_per_sample(), | |
| 208 params_.frames_per_buffer()); | |
| 209 | |
| 210 file_audio_converter_.reset( | |
| 211 new AudioConverter(file_audio_slice, params_, false)); | |
| 212 file_audio_converter_->AddInput(this); | |
| 213 } | |
| 214 | |
| 215 bool FakeAudioInputStream::PlayingFromFile() { | |
| 216 return wav_audio_handler_.get() != nullptr; | |
| 217 } | |
| 218 | |
| 219 void FakeAudioInputStream::PlayFile() { | |
| 220 // Stop playing if we've played out the whole file. | |
| 221 if (wav_audio_handler_->AtEnd(wav_file_read_pos_)) | |
| 222 return; | |
| 223 | |
| 224 file_audio_converter_->Convert(audio_bus_.get()); | |
| 225 callback_->OnData(this, audio_bus_.get(), buffer_size_, 1.0); | |
| 226 } | |
| 227 | |
| 228 void FakeAudioInputStream::PlayBeep() { | |
| 229 // Accumulate the time from the last beep. | |
| 230 interval_from_last_beep_ += TimeTicks::Now() - last_callback_time_; | |
| 231 | |
| 232 memset(buffer_.get(), 0, buffer_size_); | |
| 233 bool should_beep = false; | |
| 234 { | |
| 235 BeepContext* beep_context = g_beep_context.Pointer(); | |
| 236 if (beep_context->automatic_beep()) { | |
| 237 base::TimeDelta delta = interval_from_last_beep_ - | |
| 238 TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs); | |
| 239 if (delta > base::TimeDelta()) { | |
| 240 should_beep = true; | |
| 241 interval_from_last_beep_ = delta; | |
| 242 } | |
| 243 } else { | |
| 244 should_beep = beep_context->beep_once(); | |
| 245 beep_context->SetBeepOnce(false); | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 // If this object was instructed to generate a beep or has started to | |
| 250 // generate a beep sound. | |
| 251 if (should_beep || beep_generated_in_buffers_) { | |
| 252 // Compute the number of frames to output high value. Then compute the | |
| 253 // number of bytes based on channels and bits per channel. | |
| 254 int high_frames = beep_period_in_frames_ / 2; | |
| 255 int high_bytes = high_frames * params_.bits_per_sample() * | |
| 256 params_.channels() / 8; | |
| 257 | |
| 258 // Separate high and low with the same number of bytes to generate a | |
| 259 // square wave. | |
| 260 int position = 0; | |
| 261 while (position + high_bytes <= buffer_size_) { | |
| 262 // Write high values first. | |
| 263 memset(buffer_.get() + position, 128, high_bytes); | |
| 264 // Then leave low values in the buffer with |high_bytes|. | |
| 265 position += high_bytes * 2; | |
| 266 } | |
| 267 | |
| 268 ++beep_generated_in_buffers_; | |
| 269 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_) | |
| 270 beep_generated_in_buffers_ = 0; | |
| 271 } | |
| 272 | |
| 273 audio_bus_->FromInterleaved( | |
| 274 buffer_.get(), audio_bus_->frames(), params_.bits_per_sample() / 8); | |
| 275 callback_->OnData(this, audio_bus_.get(), buffer_size_, 1.0); | |
| 276 } | 52 } |
| 277 | 53 |
| 278 void FakeAudioInputStream::Stop() { | 54 void FakeAudioInputStream::Stop() { |
| 279 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 55 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
| 280 weak_factory_.InvalidateWeakPtrs(); | 56 fake_audio_worker_.Stop(); |
| 281 callback_ = NULL; | 57 callback_ = NULL; |
| 282 } | 58 } |
| 283 | 59 |
| 284 void FakeAudioInputStream::Close() { | 60 void FakeAudioInputStream::Close() { |
| 285 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 61 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
| 62 DCHECK(!callback_); |
| 286 audio_manager_->ReleaseInputStream(this); | 63 audio_manager_->ReleaseInputStream(this); |
| 287 } | 64 } |
| 288 | 65 |
| 289 double FakeAudioInputStream::GetMaxVolume() { | 66 double FakeAudioInputStream::GetMaxVolume() { |
| 290 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 67 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
| 291 return 1.0; | 68 return 1.0; |
| 292 } | 69 } |
| 293 | 70 |
| 294 void FakeAudioInputStream::SetVolume(double volume) { | 71 void FakeAudioInputStream::SetVolume(double volume) { |
| 295 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 72 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 306 } | 83 } |
| 307 | 84 |
| 308 bool FakeAudioInputStream::SetAutomaticGainControl(bool enabled) { | 85 bool FakeAudioInputStream::SetAutomaticGainControl(bool enabled) { |
| 309 return false; | 86 return false; |
| 310 } | 87 } |
| 311 | 88 |
| 312 bool FakeAudioInputStream::GetAutomaticGainControl() { | 89 bool FakeAudioInputStream::GetAutomaticGainControl() { |
| 313 return false; | 90 return false; |
| 314 } | 91 } |
| 315 | 92 |
| 316 // static | 93 void FakeAudioInputStream::ReadAudioFromSource() { |
| 317 void FakeAudioInputStream::BeepOnce() { | 94 DCHECK(audio_manager_->GetWorkerTaskRunner()->BelongsToCurrentThread()); |
| 318 BeepContext* beep_context = g_beep_context.Pointer(); | 95 DCHECK(callback_); |
| 319 beep_context->SetBeepOnce(true); | 96 |
| 97 if (!audio_source_) |
| 98 audio_source_ = ChooseSource(); |
| 99 |
| 100 const int kNoDelay = 0; |
| 101 audio_source_->OnMoreData(audio_bus_.get(), kNoDelay); |
| 102 callback_->OnData(this, audio_bus_.get(), 0, 1.0); |
| 320 } | 103 } |
| 321 | 104 |
| 322 double FakeAudioInputStream::ProvideInput(AudioBus* audio_bus_into_converter, | 105 using AudioSourceCallback = AudioOutputStream::AudioSourceCallback; |
| 323 base::TimeDelta buffer_delay) { | 106 scoped_ptr<AudioSourceCallback> FakeAudioInputStream::ChooseSource() { |
| 324 // Unfilled frames will be zeroed by CopyTo. | 107 DCHECK(audio_manager_->GetWorkerTaskRunner()->BelongsToCurrentThread()); |
| 325 size_t bytes_written; | 108 |
| 326 wav_audio_handler_->CopyTo(audio_bus_into_converter, wav_file_read_pos_, | 109 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 327 &bytes_written); | 110 switches::kUseFileForFakeAudioCapture)) { |
| 328 wav_file_read_pos_ += bytes_written; | 111 base::FilePath path_to_wav_file = |
| 329 return 1.0; | 112 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 330 }; | 113 switches::kUseFileForFakeAudioCapture); |
| 114 CHECK(!path_to_wav_file.empty()) |
| 115 << "You must pass the file to use as argument to --" |
| 116 << switches::kUseFileForFakeAudioCapture << "."; |
| 117 |
| 118 return make_scoped_ptr(new FileSource(params_, path_to_wav_file)); |
| 119 } |
| 120 return make_scoped_ptr(new BeepingSource(params_)); |
| 121 } |
| 122 |
| 123 void FakeAudioInputStream::BeepOnce() { |
| 124 BeepingSource::BeepOnce(); |
| 125 } |
| 331 | 126 |
| 332 } // namespace media | 127 } // namespace media |
| OLD | NEW |