Ensure the wedge timer isn't started until after StartStream().
[chromium-blink-merge.git] / media / audio / audio_output_controller.cc
blobd46dfda6be15845a45170d876f222d4059a6922d
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
3 // found in the LICENSE file.
5 #include "media/audio/audio_output_controller.h"
7 #include "base/bind.h"
8 #include "base/debug/trace_event.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/metrics/histogram.h"
11 #include "base/task_runner_util.h"
12 #include "base/threading/platform_thread.h"
13 #include "base/time/time.h"
14 #include "build/build_config.h"
15 #include "media/base/scoped_histogram_timer.h"
17 using base::Time;
18 using base::TimeDelta;
20 namespace media {
22 #if defined(AUDIO_POWER_MONITORING)
23 // Time constant for AudioPowerMonitor. See AudioPowerMonitor ctor comments for
24 // semantics. This value was arbitrarily chosen, but seems to work well.
25 static const int kPowerMeasurementTimeConstantMillis = 10;
27 // Desired frequency of calls to EventHandler::OnPowerMeasured() for reporting
28 // power levels in the audio signal.
29 static const int kPowerMeasurementsPerSecond = 4;
30 #endif
32 // Polling-related constants.
33 const int AudioOutputController::kPollNumAttempts = 3;
34 const int AudioOutputController::kPollPauseInMilliseconds = 3;
36 AudioOutputController::AudioOutputController(
37 AudioManager* audio_manager,
38 EventHandler* handler,
39 const AudioParameters& params,
40 const std::string& output_device_id,
41 const std::string& input_device_id,
42 SyncReader* sync_reader)
43 : audio_manager_(audio_manager),
44 params_(params),
45 handler_(handler),
46 output_device_id_(output_device_id),
47 input_device_id_(input_device_id),
48 stream_(NULL),
49 diverting_to_stream_(NULL),
50 volume_(1.0),
51 state_(kEmpty),
52 num_allowed_io_(0),
53 sync_reader_(sync_reader),
54 message_loop_(audio_manager->GetMessageLoop()),
55 #if defined(AUDIO_POWER_MONITORING)
56 power_monitor_(
57 params.sample_rate(),
58 TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMillis)),
59 #endif
60 on_more_io_data_called_(0) {
61 DCHECK(audio_manager);
62 DCHECK(handler_);
63 DCHECK(sync_reader_);
64 DCHECK(message_loop_.get());
67 AudioOutputController::~AudioOutputController() {
68 DCHECK_EQ(kClosed, state_);
71 // static
72 scoped_refptr<AudioOutputController> AudioOutputController::Create(
73 AudioManager* audio_manager,
74 EventHandler* event_handler,
75 const AudioParameters& params,
76 const std::string& output_device_id,
77 const std::string& input_device_id,
78 SyncReader* sync_reader) {
79 DCHECK(audio_manager);
80 DCHECK(sync_reader);
82 if (!params.IsValid() || !audio_manager)
83 return NULL;
85 scoped_refptr<AudioOutputController> controller(new AudioOutputController(
86 audio_manager, event_handler, params, output_device_id, input_device_id,
87 sync_reader));
88 controller->message_loop_->PostTask(FROM_HERE, base::Bind(
89 &AudioOutputController::DoCreate, controller, false));
90 return controller;
93 void AudioOutputController::Play() {
94 message_loop_->PostTask(FROM_HERE, base::Bind(
95 &AudioOutputController::DoPlay, this));
98 void AudioOutputController::Pause() {
99 message_loop_->PostTask(FROM_HERE, base::Bind(
100 &AudioOutputController::DoPause, this));
103 void AudioOutputController::Close(const base::Closure& closed_task) {
104 DCHECK(!closed_task.is_null());
105 message_loop_->PostTaskAndReply(FROM_HERE, base::Bind(
106 &AudioOutputController::DoClose, this), closed_task);
109 void AudioOutputController::SetVolume(double volume) {
110 message_loop_->PostTask(FROM_HERE, base::Bind(
111 &AudioOutputController::DoSetVolume, this, volume));
114 void AudioOutputController::GetOutputDeviceId(
115 base::Callback<void(const std::string&)> callback) const {
116 base::PostTaskAndReplyWithResult(
117 message_loop_.get(),
118 FROM_HERE,
119 base::Bind(&AudioOutputController::DoGetOutputDeviceId, this),
120 callback);
123 void AudioOutputController::SwitchOutputDevice(
124 const std::string& output_device_id, const base::Closure& callback) {
125 message_loop_->PostTaskAndReply(
126 FROM_HERE,
127 base::Bind(&AudioOutputController::DoSwitchOutputDevice, this,
128 output_device_id),
129 callback);
132 void AudioOutputController::DoCreate(bool is_for_device_change) {
133 DCHECK(message_loop_->BelongsToCurrentThread());
134 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CreateTime");
135 TRACE_EVENT0("audio", "AudioOutputController::DoCreate");
137 // Close() can be called before DoCreate() is executed.
138 if (state_ == kClosed)
139 return;
141 DoStopCloseAndClearStream(); // Calls RemoveOutputDeviceChangeListener().
142 DCHECK_EQ(kEmpty, state_);
144 stream_ = diverting_to_stream_ ?
145 diverting_to_stream_ :
146 audio_manager_->MakeAudioOutputStreamProxy(params_, output_device_id_,
147 input_device_id_);
148 if (!stream_) {
149 state_ = kError;
150 handler_->OnError();
151 return;
154 if (!stream_->Open()) {
155 DoStopCloseAndClearStream();
156 state_ = kError;
157 handler_->OnError();
158 return;
161 // Everything started okay, so re-register for state change callbacks if
162 // stream_ was created via AudioManager.
163 if (stream_ != diverting_to_stream_)
164 audio_manager_->AddOutputDeviceChangeListener(this);
166 // We have successfully opened the stream. Set the initial volume.
167 stream_->SetVolume(volume_);
169 // Finally set the state to kCreated.
170 state_ = kCreated;
172 // And then report we have been created if we haven't done so already.
173 if (!is_for_device_change)
174 handler_->OnCreated();
177 void AudioOutputController::DoPlay() {
178 DCHECK(message_loop_->BelongsToCurrentThread());
179 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.PlayTime");
180 TRACE_EVENT0("audio", "AudioOutputController::DoPlay");
182 // We can start from created or paused state.
183 if (state_ != kCreated && state_ != kPaused)
184 return;
186 // Ask for first packet.
187 sync_reader_->UpdatePendingBytes(0);
189 state_ = kPlaying;
191 #if defined(AUDIO_POWER_MONITORING)
192 power_monitor_.Reset();
193 power_poll_callback_.Reset(
194 base::Bind(&AudioOutputController::ReportPowerMeasurementPeriodically,
195 this));
196 // Run the callback to send an initial notification that we're starting in
197 // silence, and to schedule periodic callbacks.
198 power_poll_callback_.callback().Run();
199 #endif
201 on_more_io_data_called_ = 0;
202 AllowEntryToOnMoreIOData();
203 stream_->Start(this);
205 // For UMA tracking purposes, start the wedge detection timer. This allows us
206 // to record statistics about the number of wedged playbacks in the field.
208 // WedgeCheck() will look to see if |on_more_io_data_called_| is true after
209 // the timeout expires. Care must be taken to ensure the wedge check delay is
210 // large enough that the value isn't queried while OnMoreDataIO() is setting
211 // it.
213 // Timer self-manages its lifetime and WedgeCheck() will only record the UMA
214 // statistic if state is still kPlaying. Additional Start() calls will
215 // invalidate the previous timer.
216 wedge_timer_.reset(new base::OneShotTimer<AudioOutputController>());
217 wedge_timer_->Start(
218 FROM_HERE, TimeDelta::FromSeconds(5), this,
219 &AudioOutputController::WedgeCheck);
221 handler_->OnPlaying();
224 #if defined(AUDIO_POWER_MONITORING)
225 void AudioOutputController::ReportPowerMeasurementPeriodically() {
226 DCHECK(message_loop_->BelongsToCurrentThread());
227 const std::pair<float, bool>& reading =
228 power_monitor_.ReadCurrentPowerAndClip();
229 handler_->OnPowerMeasured(reading.first, reading.second);
230 message_loop_->PostDelayedTask(
231 FROM_HERE, power_poll_callback_.callback(),
232 TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond);
234 #endif
236 void AudioOutputController::StopStream() {
237 DCHECK(message_loop_->BelongsToCurrentThread());
239 if (state_ == kPlaying) {
240 wedge_timer_.reset();
241 stream_->Stop();
242 DisallowEntryToOnMoreIOData();
244 #if defined(AUDIO_POWER_MONITORING)
245 power_poll_callback_.Cancel();
246 #endif
248 state_ = kPaused;
252 void AudioOutputController::DoPause() {
253 DCHECK(message_loop_->BelongsToCurrentThread());
254 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.PauseTime");
255 TRACE_EVENT0("audio", "AudioOutputController::DoPause");
257 StopStream();
259 if (state_ != kPaused)
260 return;
262 // Let the renderer know we've stopped. Necessary to let PPAPI clients know
263 // audio has been shutdown. TODO(dalecurtis): This stinks. PPAPI should have
264 // a better way to know when it should exit PPB_Audio_Shared::Run().
265 sync_reader_->UpdatePendingBytes(-1);
267 #if defined(AUDIO_POWER_MONITORING)
268 // Paused means silence follows.
269 handler_->OnPowerMeasured(AudioPowerMonitor::zero_power(), false);
270 #endif
272 handler_->OnPaused();
275 void AudioOutputController::DoClose() {
276 DCHECK(message_loop_->BelongsToCurrentThread());
277 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CloseTime");
278 TRACE_EVENT0("audio", "AudioOutputController::DoClose");
280 if (state_ != kClosed) {
281 DoStopCloseAndClearStream();
282 sync_reader_->Close();
283 state_ = kClosed;
287 void AudioOutputController::DoSetVolume(double volume) {
288 DCHECK(message_loop_->BelongsToCurrentThread());
290 // Saves the volume to a member first. We may not be able to set the volume
291 // right away but when the stream is created we'll set the volume.
292 volume_ = volume;
294 switch (state_) {
295 case kCreated:
296 case kPlaying:
297 case kPaused:
298 stream_->SetVolume(volume_);
299 break;
300 default:
301 return;
305 std::string AudioOutputController::DoGetOutputDeviceId() const {
306 DCHECK(message_loop_->BelongsToCurrentThread());
307 return output_device_id_;
310 void AudioOutputController::DoSwitchOutputDevice(
311 const std::string& output_device_id) {
312 DCHECK(message_loop_->BelongsToCurrentThread());
314 if (state_ == kClosed)
315 return;
317 output_device_id_ = output_device_id;
319 // If output is currently diverted, we must not call OnDeviceChange
320 // since it would break the diverted setup. Once diversion is
321 // finished using StopDiverting() the output will switch to the new
322 // device ID.
323 if (stream_ != diverting_to_stream_)
324 OnDeviceChange();
327 void AudioOutputController::DoReportError() {
328 DCHECK(message_loop_->BelongsToCurrentThread());
329 if (state_ != kClosed)
330 handler_->OnError();
333 int AudioOutputController::OnMoreData(AudioBus* dest,
334 AudioBuffersState buffers_state) {
335 return OnMoreIOData(NULL, dest, buffers_state);
338 int AudioOutputController::OnMoreIOData(AudioBus* source,
339 AudioBus* dest,
340 AudioBuffersState buffers_state) {
341 DisallowEntryToOnMoreIOData();
342 TRACE_EVENT0("audio", "AudioOutputController::OnMoreIOData");
344 // Indicate that we haven't wedged (at least not indefinitely, WedgeCheck()
345 // may have already fired if OnMoreIOData() took an abnormal amount of time).
346 // Since this thread is the only writer of |on_more_io_data_called_| once the
347 // thread starts, its safe to compare and then increment.
348 if (base::AtomicRefCountIsZero(&on_more_io_data_called_))
349 base::AtomicRefCountInc(&on_more_io_data_called_);
351 sync_reader_->Read(source, dest);
353 const int frames = dest->frames();
354 sync_reader_->UpdatePendingBytes(
355 buffers_state.total_bytes() + frames * params_.GetBytesPerFrame());
357 #if defined(AUDIO_POWER_MONITORING)
358 power_monitor_.Scan(*dest, frames);
359 #endif
361 AllowEntryToOnMoreIOData();
362 return frames;
365 void AudioOutputController::OnError(AudioOutputStream* stream) {
366 // Handle error on the audio controller thread.
367 message_loop_->PostTask(FROM_HERE, base::Bind(
368 &AudioOutputController::DoReportError, this));
371 void AudioOutputController::DoStopCloseAndClearStream() {
372 DCHECK(message_loop_->BelongsToCurrentThread());
374 // Allow calling unconditionally and bail if we don't have a stream_ to close.
375 if (stream_) {
376 // De-register from state change callbacks if stream_ was created via
377 // AudioManager.
378 if (stream_ != diverting_to_stream_)
379 audio_manager_->RemoveOutputDeviceChangeListener(this);
381 StopStream();
382 stream_->Close();
383 if (stream_ == diverting_to_stream_)
384 diverting_to_stream_ = NULL;
385 stream_ = NULL;
388 state_ = kEmpty;
391 void AudioOutputController::OnDeviceChange() {
392 DCHECK(message_loop_->BelongsToCurrentThread());
393 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.DeviceChangeTime");
394 TRACE_EVENT0("audio", "AudioOutputController::OnDeviceChange");
396 // TODO(dalecurtis): Notify the renderer side that a device change has
397 // occurred. Currently querying the hardware information here will lead to
398 // crashes on OSX. See http://crbug.com/158170.
400 // Recreate the stream (DoCreate() will first shut down an existing stream).
401 // Exit if we ran into an error.
402 const State original_state = state_;
403 DoCreate(true);
404 if (!stream_ || state_ == kError)
405 return;
407 // Get us back to the original state or an equivalent state.
408 switch (original_state) {
409 case kPlaying:
410 DoPlay();
411 return;
412 case kCreated:
413 case kPaused:
414 // From the outside these two states are equivalent.
415 return;
416 default:
417 NOTREACHED() << "Invalid original state.";
421 const AudioParameters& AudioOutputController::GetAudioParameters() {
422 return params_;
425 void AudioOutputController::StartDiverting(AudioOutputStream* to_stream) {
426 message_loop_->PostTask(
427 FROM_HERE,
428 base::Bind(&AudioOutputController::DoStartDiverting, this, to_stream));
431 void AudioOutputController::StopDiverting() {
432 message_loop_->PostTask(
433 FROM_HERE, base::Bind(&AudioOutputController::DoStopDiverting, this));
436 void AudioOutputController::DoStartDiverting(AudioOutputStream* to_stream) {
437 DCHECK(message_loop_->BelongsToCurrentThread());
439 if (state_ == kClosed)
440 return;
442 DCHECK(!diverting_to_stream_);
443 diverting_to_stream_ = to_stream;
444 // Note: OnDeviceChange() will engage the "re-create" process, which will
445 // detect and use the alternate AudioOutputStream rather than create a new one
446 // via AudioManager.
447 OnDeviceChange();
450 void AudioOutputController::DoStopDiverting() {
451 DCHECK(message_loop_->BelongsToCurrentThread());
453 if (state_ == kClosed)
454 return;
456 // Note: OnDeviceChange() will cause the existing stream (the consumer of the
457 // diverted audio data) to be closed, and diverting_to_stream_ will be set
458 // back to NULL.
459 OnDeviceChange();
460 DCHECK(!diverting_to_stream_);
463 void AudioOutputController::AllowEntryToOnMoreIOData() {
464 DCHECK(base::AtomicRefCountIsZero(&num_allowed_io_));
465 base::AtomicRefCountInc(&num_allowed_io_);
468 void AudioOutputController::DisallowEntryToOnMoreIOData() {
469 const bool is_zero = !base::AtomicRefCountDec(&num_allowed_io_);
470 DCHECK(is_zero);
473 void AudioOutputController::WedgeCheck() {
474 DCHECK(message_loop_->BelongsToCurrentThread());
476 // If we should be playing and we haven't, that's a wedge.
477 if (state_ == kPlaying) {
478 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess",
479 base::AtomicRefCountIsOne(&on_more_io_data_called_));
481 // TODO(dalecurtis): Temporary debugging measure, see crbug.com/316376
482 CHECK(base::AtomicRefCountIsOne(&on_more_io_data_called_));
486 } // namespace media