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/audio_input_controller.h" | 5 #include "media/audio/audio_input_controller.h" |
6 | 6 |
7 #include <inttypes.h> | 7 #include <inttypes.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <limits> | 10 #include <limits> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
| 23 #include "media/base/media_switches.h" |
22 #include "media/base/user_input_monitor.h" | 24 #include "media/base/user_input_monitor.h" |
23 | 25 |
24 namespace media { | 26 namespace media { |
25 namespace { | 27 namespace { |
26 | 28 |
27 const int kMaxInputChannels = 3; | 29 const int kMaxInputChannels = 3; |
28 | 30 |
29 #if defined(AUDIO_POWER_MONITORING) | 31 #if defined(AUDIO_POWER_MONITORING) |
30 // Time in seconds between two successive measurements of audio power levels. | 32 // Time in seconds between two successive measurements of audio power levels. |
31 const int kPowerMonitorLogIntervalSeconds = 15; | 33 const int kPowerMonitorLogIntervalSeconds = 15; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 void PerformOptionalDebugRecording(const AudioBus* source) { | 137 void PerformOptionalDebugRecording(const AudioBus* source) { |
136 // Called on the hw callback thread while recording is enabled. | 138 // Called on the hw callback thread while recording is enabled. |
137 if (!controller_->debug_writer_ || !controller_->debug_writer_->WillWrite()) | 139 if (!controller_->debug_writer_ || !controller_->debug_writer_->WillWrite()) |
138 return; | 140 return; |
139 | 141 |
140 // TODO(tommi): This is costly. AudioBus heap allocs and we create a new | 142 // TODO(tommi): This is costly. AudioBus heap allocs and we create a new |
141 // one for every callback. We could instead have a pool of bus objects | 143 // one for every callback. We could instead have a pool of bus objects |
142 // that get returned to us somehow. | 144 // that get returned to us somehow. |
143 // We should also avoid calling PostTask here since the implementation | 145 // We should also avoid calling PostTask here since the implementation |
144 // of the debug writer will basically do a PostTask straight away anyway. | 146 // of the debug writer will basically do a PostTask straight away anyway. |
145 // Might require some modifications to AudioFileWriter though since | 147 // Might require some modifications to AudioDebugFileWriter though since |
146 // there are some threading concerns there and AudioFileWriter's | 148 // there are some threading concerns there and AudioDebugFileWriter's |
147 // lifetime guarantees need to be longer than that of associated active | 149 // lifetime guarantees need to be longer than that of associated active |
148 // audio streams. | 150 // audio streams. |
149 std::unique_ptr<AudioBus> source_copy = | 151 std::unique_ptr<AudioBus> source_copy = |
150 AudioBus::Create(source->channels(), source->frames()); | 152 AudioBus::Create(source->channels(), source->frames()); |
151 source->CopyTo(source_copy.get()); | 153 source->CopyTo(source_copy.get()); |
152 controller_->task_runner_->PostTask( | 154 controller_->task_runner_->PostTask( |
153 FROM_HERE, base::Bind(&AudioInputController::WriteInputDataForDebugging, | 155 FROM_HERE, base::Bind(&AudioInputController::WriteInputDataForDebugging, |
154 weak_controller_, base::Passed(&source_copy))); | 156 weak_controller_, base::Passed(&source_copy))); |
155 } | 157 } |
156 | 158 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 bool error_during_callback_ = false; | 190 bool error_during_callback_ = false; |
189 }; | 191 }; |
190 | 192 |
191 // static | 193 // static |
192 AudioInputController::Factory* AudioInputController::factory_ = nullptr; | 194 AudioInputController::Factory* AudioInputController::factory_ = nullptr; |
193 | 195 |
194 AudioInputController::AudioInputController( | 196 AudioInputController::AudioInputController( |
195 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 197 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
196 EventHandler* handler, | 198 EventHandler* handler, |
197 SyncWriter* sync_writer, | 199 SyncWriter* sync_writer, |
198 std::unique_ptr<AudioFileWriter> debug_writer, | |
199 UserInputMonitor* user_input_monitor, | 200 UserInputMonitor* user_input_monitor, |
200 StreamType type) | 201 const AudioParameters& params, |
| 202 StreamType type, |
| 203 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
201 : creator_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 204 : creator_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
202 task_runner_(std::move(task_runner)), | 205 task_runner_(std::move(task_runner)), |
203 handler_(handler), | 206 handler_(handler), |
204 stream_(nullptr), | 207 stream_(nullptr), |
205 sync_writer_(sync_writer), | 208 sync_writer_(sync_writer), |
206 type_(type), | 209 type_(type), |
207 user_input_monitor_(user_input_monitor), | 210 user_input_monitor_(user_input_monitor), |
208 debug_writer_(std::move(debug_writer)), | 211 #if BUILDFLAG(ENABLE_WEBRTC) |
| 212 debug_writer_( |
| 213 base::MakeUnique<AudioDebugFileWriter>(params, |
| 214 std::move(file_task_runner))), |
| 215 #endif |
209 weak_ptr_factory_(this) { | 216 weak_ptr_factory_(this) { |
210 DCHECK(creator_task_runner_.get()); | 217 DCHECK(creator_task_runner_.get()); |
211 DCHECK(handler_); | 218 DCHECK(handler_); |
212 DCHECK(sync_writer_); | 219 DCHECK(sync_writer_); |
213 } | 220 } |
214 | 221 |
215 AudioInputController::~AudioInputController() { | 222 AudioInputController::~AudioInputController() { |
216 DCHECK(!audio_callback_); | 223 DCHECK(!audio_callback_); |
217 DCHECK(!stream_); | 224 DCHECK(!stream_); |
218 } | 225 } |
219 | 226 |
220 // static | 227 // static |
221 scoped_refptr<AudioInputController> AudioInputController::Create( | 228 scoped_refptr<AudioInputController> AudioInputController::Create( |
222 AudioManager* audio_manager, | 229 AudioManager* audio_manager, |
223 EventHandler* event_handler, | 230 EventHandler* event_handler, |
224 SyncWriter* sync_writer, | 231 SyncWriter* sync_writer, |
225 UserInputMonitor* user_input_monitor, | 232 UserInputMonitor* user_input_monitor, |
226 std::unique_ptr<AudioFileWriter> debug_writer, | |
227 const AudioParameters& params, | 233 const AudioParameters& params, |
228 const std::string& device_id, | 234 const std::string& device_id, |
229 bool enable_agc) { | 235 bool enable_agc, |
| 236 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
230 DCHECK(audio_manager); | 237 DCHECK(audio_manager); |
231 DCHECK(sync_writer); | 238 DCHECK(sync_writer); |
232 DCHECK(event_handler); | 239 DCHECK(event_handler); |
233 | 240 |
234 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) | 241 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) |
235 return nullptr; | 242 return nullptr; |
236 | 243 |
237 if (factory_) { | 244 if (factory_) { |
238 return factory_->Create(audio_manager->GetTaskRunner(), sync_writer, | 245 return factory_->Create(audio_manager->GetTaskRunner(), sync_writer, |
239 audio_manager, event_handler, params, | 246 audio_manager, event_handler, params, |
240 user_input_monitor, ParamsToStreamType(params)); | 247 user_input_monitor, ParamsToStreamType(params)); |
241 } | 248 } |
242 | 249 |
243 // Create the AudioInputController object and ensure that it runs on | 250 // Create the AudioInputController object and ensure that it runs on |
244 // the audio-manager thread. | 251 // the audio-manager thread. |
245 scoped_refptr<AudioInputController> controller(new AudioInputController( | 252 scoped_refptr<AudioInputController> controller(new AudioInputController( |
246 audio_manager->GetTaskRunner(), event_handler, sync_writer, | 253 audio_manager->GetTaskRunner(), event_handler, sync_writer, |
247 std::move(debug_writer), user_input_monitor, ParamsToStreamType(params))); | 254 user_input_monitor, params, ParamsToStreamType(params), |
| 255 std::move(file_task_runner))); |
248 | 256 |
249 // Create and open a new audio input stream from the existing | 257 // Create and open a new audio input stream from the existing |
250 // audio-device thread. Use the provided audio-input device. | 258 // audio-device thread. Use the provided audio-input device. |
251 if (!controller->task_runner_->PostTask( | 259 if (!controller->task_runner_->PostTask( |
252 FROM_HERE, base::Bind(&AudioInputController::DoCreate, controller, | 260 FROM_HERE, base::Bind(&AudioInputController::DoCreate, controller, |
253 base::Unretained(audio_manager), params, | 261 base::Unretained(audio_manager), params, |
254 device_id, enable_agc))) { | 262 device_id, enable_agc))) { |
255 controller = nullptr; | 263 controller = nullptr; |
256 } | 264 } |
257 | 265 |
258 return controller; | 266 return controller; |
259 } | 267 } |
260 | 268 |
261 // static | 269 // static |
262 scoped_refptr<AudioInputController> AudioInputController::CreateForStream( | 270 scoped_refptr<AudioInputController> AudioInputController::CreateForStream( |
263 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 271 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
264 EventHandler* event_handler, | 272 EventHandler* event_handler, |
265 AudioInputStream* stream, | 273 AudioInputStream* stream, |
266 SyncWriter* sync_writer, | 274 SyncWriter* sync_writer, |
267 std::unique_ptr<AudioFileWriter> debug_writer, | 275 UserInputMonitor* user_input_monitor, |
268 UserInputMonitor* user_input_monitor) { | 276 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 277 const AudioParameters& params) { |
269 DCHECK(sync_writer); | 278 DCHECK(sync_writer); |
270 DCHECK(stream); | 279 DCHECK(stream); |
271 DCHECK(event_handler); | 280 DCHECK(event_handler); |
272 | 281 |
273 if (factory_) { | 282 if (factory_) { |
274 return factory_->Create(task_runner, sync_writer, AudioManager::Get(), | 283 return factory_->Create(task_runner, sync_writer, AudioManager::Get(), |
275 event_handler, | 284 event_handler, |
276 AudioParameters::UnavailableDeviceParams(), | 285 AudioParameters::UnavailableDeviceParams(), |
277 user_input_monitor, VIRTUAL); | 286 user_input_monitor, VIRTUAL); |
278 } | 287 } |
279 | 288 |
280 // Create the AudioInputController object and ensure that it runs on | 289 // Create the AudioInputController object and ensure that it runs on |
281 // the audio-manager thread. | 290 // the audio-manager thread. |
282 scoped_refptr<AudioInputController> controller(new AudioInputController( | 291 scoped_refptr<AudioInputController> controller(new AudioInputController( |
283 task_runner, event_handler, sync_writer, std::move(debug_writer), | 292 task_runner, event_handler, sync_writer, user_input_monitor, params, |
284 user_input_monitor, VIRTUAL)); | 293 VIRTUAL, std::move(file_task_runner))); |
285 | 294 |
286 if (!controller->task_runner_->PostTask( | 295 if (!controller->task_runner_->PostTask( |
287 FROM_HERE, base::Bind(&AudioInputController::DoCreateForStream, | 296 FROM_HERE, base::Bind(&AudioInputController::DoCreateForStream, |
288 controller, stream, /*enable_agc*/ false))) { | 297 controller, stream, /*enable_agc*/ false))) { |
289 controller = nullptr; | 298 controller = nullptr; |
290 } | 299 } |
291 | 300 |
292 return controller; | 301 return controller; |
293 } | 302 } |
294 | 303 |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 case AudioParameters::Format::AUDIO_PCM_LOW_LATENCY: | 689 case AudioParameters::Format::AUDIO_PCM_LOW_LATENCY: |
681 return AudioInputController::StreamType::LOW_LATENCY; | 690 return AudioInputController::StreamType::LOW_LATENCY; |
682 default: | 691 default: |
683 // Currently, the remaining supported type is fake. Reconsider if other | 692 // Currently, the remaining supported type is fake. Reconsider if other |
684 // formats become supported. | 693 // formats become supported. |
685 return AudioInputController::StreamType::FAKE; | 694 return AudioInputController::StreamType::FAKE; |
686 } | 695 } |
687 } | 696 } |
688 | 697 |
689 } // namespace media | 698 } // namespace media |
OLD | NEW |