Chromium Code Reviews| 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 // MSVC++ requires this to be set before any other includes to get M_PI. | 4 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 5 #define _USE_MATH_DEFINES | 5 #define _USE_MATH_DEFINES |
| 6 #include <cmath> | 6 #include <cmath> |
| 7 | 7 |
| 8 #include "media/audio/simple_sources.h" | 8 #include "media/audio/simple_sources.h" |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 | 11 |
| 12 #include "base/files/file.h" | |
| 13 #include "base/lazy_instance.h" | |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "media/audio/sounds/wav_audio_handler.h" | |
| 16 #include "media/base/audio_bus.h" | |
| 13 | 17 |
| 14 namespace media { | 18 namespace media { |
| 15 | 19 |
| 20 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.
| |
| 21 | |
| 22 // Opens |wav_filename|, reads it and loads it as a wav file. This function will | |
| 23 // bluntly trigger CHECKs if we can't read the file or if it's malformed. The | |
| 24 // caller takes ownership of the returned data. The size of the data is stored | |
| 25 // in |read_length|. | |
| 26 scoped_ptr<uint8[]> ReadWavFile(const base::FilePath& wav_filename, | |
| 27 size_t* file_length) { | |
| 28 base::File wav_file( | |
| 29 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 30 if (!wav_file.IsValid()) { | |
| 31 CHECK(false) << "Failed to read " << wav_filename.value() << " as input to " | |
| 32 << "the fake device."; | |
| 33 return nullptr; | |
| 34 } | |
| 35 | |
| 36 size_t wav_file_length = wav_file.GetLength(); | |
| 37 CHECK_GT(wav_file_length, 0u) | |
| 38 << "Input file to fake device is empty: " << wav_filename.value(); | |
| 39 | |
| 40 uint8* wav_file_data = new uint8[wav_file_length]; | |
| 41 size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data), | |
| 42 wav_file_length); | |
| 43 if (read_bytes != wav_file_length) { | |
| 44 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.
| |
| 45 return nullptr; | |
| 46 } | |
| 47 *file_length = wav_file_length; | |
| 48 return scoped_ptr<uint8[]>(wav_file_data); | |
| 49 } | |
| 50 | |
| 51 // Opens |wav_filename|, reads it and loads it as a Wav file. This function will | |
| 52 // bluntly trigger CHECKs if we can't read the file or if it's malformed. | |
| 53 scoped_ptr<media::WavAudioHandler> CreateWavAudioHandler( | |
| 54 const base::FilePath& wav_filename, const uint8* wav_file_data, | |
| 55 size_t wav_file_length, const AudioParameters& expected_params) { | |
| 56 base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data), | |
| 57 wav_file_length); | |
| 58 scoped_ptr<media::WavAudioHandler> wav_audio_handler( | |
| 59 new media::WavAudioHandler(wav_data)); | |
| 60 return wav_audio_handler.Pass(); | |
| 61 } | |
| 62 | |
| 63 // These values are based on experiments for local-to-local | |
| 64 // PeerConnection to demonstrate audio/video synchronization. | |
| 65 const int kBeepDurationMilliseconds = 20; | |
| 66 const int kBeepFrequency = 400; | |
| 67 | |
| 68 // Intervals between two automatic beeps. | |
| 69 const int kAutomaticBeepIntervalInMs = 500; | |
| 70 | |
| 71 // Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless | |
| 72 // users explicitly call BeepOnce(), which will disable the automatic beep. | |
| 73 class BeepContext { | |
| 74 public: | |
| 75 BeepContext() : beep_once_(false), automatic_beep_(true) {} | |
| 76 | |
| 77 void SetBeepOnce(bool enable) { | |
| 78 base::AutoLock auto_lock(lock_); | |
| 79 beep_once_ = enable; | |
| 80 | |
| 81 // Disable the automatic beep if users explicit set |beep_once_| to true. | |
| 82 if (enable) | |
| 83 automatic_beep_ = false; | |
| 84 } | |
| 85 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.
| |
| 86 base::AutoLock auto_lock(lock_); | |
| 87 return beep_once_; | |
| 88 } | |
| 89 bool automatic_beep() const { | |
| 90 base::AutoLock auto_lock(lock_); | |
| 91 return automatic_beep_; | |
| 92 } | |
| 93 | |
| 94 private: | |
| 95 mutable base::Lock lock_; | |
| 96 bool beep_once_; | |
| 97 bool automatic_beep_; | |
| 98 }; | |
| 99 | |
| 100 static base::LazyInstance<BeepContext> g_beep_context = | |
| 101 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.
| |
| 102 | |
| 103 } // namespace | |
| 104 | |
| 16 ////////////////////////////////////////////////////////////////////////////// | 105 ////////////////////////////////////////////////////////////////////////////// |
| 17 // SineWaveAudioSource implementation. | 106 // SineWaveAudioSource implementation. |
| 18 | 107 |
| 19 SineWaveAudioSource::SineWaveAudioSource(int channels, | 108 SineWaveAudioSource::SineWaveAudioSource(int channels, |
| 20 double freq, double sample_freq) | 109 double freq, double sample_freq) |
| 21 : channels_(channels), | 110 : channels_(channels), |
| 22 f_(freq / sample_freq), | 111 f_(freq / sample_freq), |
| 23 time_state_(0), | 112 time_state_(0), |
| 24 cap_(0), | 113 cap_(0), |
| 25 callbacks_(0), | 114 callbacks_(0), |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 56 base::AutoLock auto_lock(time_lock_); | 145 base::AutoLock auto_lock(time_lock_); |
| 57 DCHECK_GT(cap, 0); | 146 DCHECK_GT(cap, 0); |
| 58 cap_ = cap; | 147 cap_ = cap; |
| 59 } | 148 } |
| 60 | 149 |
| 61 void SineWaveAudioSource::Reset() { | 150 void SineWaveAudioSource::Reset() { |
| 62 base::AutoLock auto_lock(time_lock_); | 151 base::AutoLock auto_lock(time_lock_); |
| 63 time_state_ = 0; | 152 time_state_ = 0; |
| 64 } | 153 } |
| 65 | 154 |
| 155 FileSource::FileSource(const AudioParameters& params, | |
| 156 const base::FilePath& path_to_wav_file) | |
| 157 : params_(params), | |
| 158 path_to_wav_file_(path_to_wav_file), | |
| 159 wav_file_read_pos_(0), | |
| 160 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
| |
| 161 params.frames_per_buffer() / 8) { | |
| 162 } | |
| 163 | |
| 164 FileSource::~FileSource() { | |
| 165 } | |
| 166 | |
| 167 void FileSource::Open() { | |
| 168 // Read the file, and put its data in a scoped_ptr so it gets deleted later. | |
| 169 size_t file_length = 0; | |
| 170 wav_file_data_ = ReadWavFile(path_to_wav_file_, &file_length); | |
| 171 wav_audio_handler_ = CreateWavAudioHandler( | |
| 172 path_to_wav_file_, wav_file_data_.get(), file_length, params_); | |
| 173 | |
| 174 // Hook us up so we pull in data from the file into the converter. We need to | |
| 175 // modify the wav file's audio parameters since we'll be reading small slices | |
| 176 // of it at a time and not the whole thing (like 10 ms at a time). | |
| 177 AudioParameters file_audio_slice( | |
| 178 wav_audio_handler_->params().format(), | |
| 179 wav_audio_handler_->params().channel_layout(), | |
| 180 wav_audio_handler_->params().sample_rate(), | |
| 181 wav_audio_handler_->params().bits_per_sample(), | |
| 182 params_.frames_per_buffer()); | |
| 183 | |
| 184 file_audio_converter_.reset( | |
| 185 new AudioConverter(file_audio_slice, params_, false)); | |
| 186 file_audio_converter_->AddInput(this); | |
| 187 } | |
| 188 | |
| 189 int FileSource::OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) { | |
| 190 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.
| |
| 191 | |
| 192 // Stop playing if we've played out the whole file. | |
| 193 if (wav_audio_handler_->AtEnd(wav_file_read_pos_)) | |
| 194 return 0; | |
| 195 | |
| 196 // This pulls data from ProvideInput. | |
| 197 file_audio_converter_->Convert(audio_bus); | |
| 198 return audio_bus->frames(); | |
| 199 } | |
| 200 | |
| 201 double FileSource::ProvideInput(AudioBus* audio_bus_into_converter, | |
| 202 base::TimeDelta buffer_delay) { | |
| 203 // Unfilled frames will be zeroed by CopyTo. | |
| 204 size_t bytes_written; | |
| 205 wav_audio_handler_->CopyTo(audio_bus_into_converter, wav_file_read_pos_, | |
| 206 &bytes_written); | |
| 207 wav_file_read_pos_ += bytes_written; | |
| 208 return 1.0; | |
| 209 }; | |
| 210 | |
| 211 BeepingSource::BeepingSource(const AudioParameters& params) | |
| 212 : 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.
| |
| 213 params.frames_per_buffer() / 8), | |
| 214 params_(params), | |
| 215 beep_duration_in_buffers_(kBeepDurationMilliseconds * | |
| 216 params.sample_rate() / | |
| 217 params.frames_per_buffer() / | |
| 218 1000), | |
| 219 beep_generated_in_buffers_(0), | |
| 220 beep_period_in_frames_(params.sample_rate() / kBeepFrequency) { | |
| 221 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.
| |
| 222 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.
| |
| 223 } | |
| 224 | |
| 225 BeepingSource::~BeepingSource() { | |
| 226 } | |
| 227 | |
| 228 int BeepingSource::OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) { | |
| 229 // Accumulate the time from the last beep. | |
| 230 interval_from_last_beep_ += base::TimeTicks::Now() - last_callback_time_; | |
| 231 last_callback_time_ = base::TimeTicks::Now(); | |
| 232 | |
| 233 memset(buffer_.get(), 0, buffer_size_); | |
| 234 bool should_beep = false; | |
| 235 { | |
|
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.
| |
| 236 BeepContext* beep_context = g_beep_context.Pointer(); | |
| 237 if (beep_context->automatic_beep()) { | |
| 238 base::TimeDelta delta = interval_from_last_beep_ - | |
| 239 base::TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs); | |
| 240 if (delta > base::TimeDelta()) { | |
| 241 should_beep = true; | |
| 242 interval_from_last_beep_ = delta; | |
| 243 } | |
| 244 } else { | |
| 245 should_beep = beep_context->beep_once(); | |
| 246 beep_context->SetBeepOnce(false); | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 // If this object was instructed to generate a beep or has started to | |
| 251 // generate a beep sound. | |
| 252 if (should_beep || beep_generated_in_buffers_) { | |
| 253 // Compute the number of frames to output high value. Then compute the | |
| 254 // number of bytes based on channels and bits per channel. | |
| 255 int high_frames = beep_period_in_frames_ / 2; | |
| 256 int high_bytes = high_frames * params_.bits_per_sample() * | |
| 257 params_.channels() / 8; | |
| 258 | |
| 259 // Separate high and low with the same number of bytes to generate a | |
| 260 // square wave. | |
| 261 int position = 0; | |
| 262 while (position + high_bytes <= buffer_size_) { | |
| 263 // Write high values first. | |
| 264 memset(buffer_.get() + position, 128, high_bytes); | |
| 265 // Then leave low values in the buffer with |high_bytes|. | |
| 266 position += high_bytes * 2; | |
| 267 } | |
| 268 | |
| 269 ++beep_generated_in_buffers_; | |
| 270 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_) | |
| 271 beep_generated_in_buffers_ = 0; | |
| 272 } | |
| 273 | |
| 274 audio_bus->FromInterleaved( | |
| 275 buffer_.get(), audio_bus->frames(), params_.bits_per_sample() / 8); | |
| 276 return audio_bus->frames(); | |
| 277 } | |
| 278 | |
|
DaleCurtis
2015/02/19 19:54:53
Extra space.
phoglund_chromium
2015/02/20 14:21:59
Done.
| |
| 279 | |
| 280 void BeepingSource::BeepOnce() { | |
| 281 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.
| |
| 282 beep_context->SetBeepOnce(true); | |
| 283 } | |
| 284 | |
| 66 } // namespace media | 285 } // namespace media |
| OLD | NEW |