| 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/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 const TimeTicks now = TimeTicks::Now(); | 178 const TimeTicks now = TimeTicks::Now(); |
| 179 base::TimeDelta next_callback_time = | 179 base::TimeDelta next_callback_time = |
| 180 last_callback_time_ + callback_interval_ * 2 - now; | 180 last_callback_time_ + callback_interval_ * 2 - now; |
| 181 | 181 |
| 182 // If we are falling behind, try to catch up as much as we can in the next | 182 // If we are falling behind, try to catch up as much as we can in the next |
| 183 // callback. | 183 // callback. |
| 184 if (next_callback_time < base::TimeDelta()) | 184 if (next_callback_time < base::TimeDelta()) |
| 185 next_callback_time = base::TimeDelta(); | 185 next_callback_time = base::TimeDelta(); |
| 186 | 186 |
| 187 if (PlayingFromFile()) { | 187 if (PlayingFromFile()) { |
| 188 PlayFileLooping(); | 188 PlayFile(); |
| 189 } else { | 189 } else { |
| 190 PlayBeep(); | 190 PlayBeep(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 last_callback_time_ = now; | 193 last_callback_time_ = now; |
| 194 | 194 |
| 195 task_runner_->PostDelayedTask( | 195 task_runner_->PostDelayedTask( |
| 196 FROM_HERE, | 196 FROM_HERE, |
| 197 base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()), | 197 base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()), |
| 198 next_callback_time); | 198 next_callback_time); |
| 199 } | 199 } |
| 200 | 200 |
| 201 void FakeAudioInputStream::OpenInFileMode(const base::FilePath& wav_filename) { | 201 void FakeAudioInputStream::OpenInFileMode(const base::FilePath& wav_filename) { |
| 202 CHECK(!wav_filename.empty()) | 202 CHECK(!wav_filename.empty()) |
| 203 << "You must pass the file to use as argument to --" | 203 << "You must pass the file to use as argument to --" |
| 204 << switches::kUseFileForFakeAudioCapture << "."; | 204 << switches::kUseFileForFakeAudioCapture << "."; |
| 205 | 205 |
| 206 // Read the file, and put its data in a scoped_ptr so it gets deleted later. | 206 // Read the file, and put its data in a scoped_ptr so it gets deleted later. |
| 207 size_t file_length = 0; | 207 size_t file_length = 0; |
| 208 wav_file_data_ = ReadWavFile(wav_filename, &file_length); | 208 wav_file_data_ = ReadWavFile(wav_filename, &file_length); |
| 209 wav_audio_handler_ = CreateWavAudioHandler( | 209 wav_audio_handler_ = CreateWavAudioHandler( |
| 210 wav_filename, wav_file_data_.get(), file_length, params_); | 210 wav_filename, wav_file_data_.get(), file_length, params_); |
| 211 } | 211 } |
| 212 | 212 |
| 213 bool FakeAudioInputStream::PlayingFromFile() { | 213 bool FakeAudioInputStream::PlayingFromFile() { |
| 214 return wav_audio_handler_.get() != nullptr; | 214 return wav_audio_handler_.get() != nullptr; |
| 215 } | 215 } |
| 216 | 216 |
| 217 void FakeAudioInputStream::PlayFileLooping() { | 217 void FakeAudioInputStream::PlayFile() { |
| 218 // Stop playing if we've played out the whole file. |
| 219 if (wav_audio_handler_->AtEnd(wav_file_read_pos_)) |
| 220 return; |
| 221 |
| 218 // Unfilled frames will be zeroed by CopyTo. | 222 // Unfilled frames will be zeroed by CopyTo. |
| 219 size_t bytes_written; | 223 size_t bytes_written; |
| 220 wav_audio_handler_->CopyTo(audio_bus_.get(), wav_file_read_pos_, | 224 wav_audio_handler_->CopyTo(audio_bus_.get(), wav_file_read_pos_, |
| 221 &bytes_written); | 225 &bytes_written); |
| 222 wav_file_read_pos_ += bytes_written; | 226 wav_file_read_pos_ += bytes_written; |
| 223 if (wav_audio_handler_->AtEnd(wav_file_read_pos_)) | |
| 224 wav_file_read_pos_ = 0; | |
| 225 callback_->OnData(this, audio_bus_.get(), buffer_size_, 1.0); | 227 callback_->OnData(this, audio_bus_.get(), buffer_size_, 1.0); |
| 226 } | 228 } |
| 227 | 229 |
| 228 void FakeAudioInputStream::PlayBeep() { | 230 void FakeAudioInputStream::PlayBeep() { |
| 229 // Accumulate the time from the last beep. | 231 // Accumulate the time from the last beep. |
| 230 interval_from_last_beep_ += TimeTicks::Now() - last_callback_time_; | 232 interval_from_last_beep_ += TimeTicks::Now() - last_callback_time_; |
| 231 | 233 |
| 232 memset(buffer_.get(), 0, buffer_size_); | 234 memset(buffer_.get(), 0, buffer_size_); |
| 233 bool should_beep = false; | 235 bool should_beep = false; |
| 234 { | 236 { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 return false; | 315 return false; |
| 314 } | 316 } |
| 315 | 317 |
| 316 // static | 318 // static |
| 317 void FakeAudioInputStream::BeepOnce() { | 319 void FakeAudioInputStream::BeepOnce() { |
| 318 BeepContext* beep_context = g_beep_context.Pointer(); | 320 BeepContext* beep_context = g_beep_context.Pointer(); |
| 319 beep_context->SetBeepOnce(true); | 321 beep_context->SetBeepOnce(true); |
| 320 } | 322 } |
| 321 | 323 |
| 322 } // namespace media | 324 } // namespace media |
| OLD | NEW |