This source file includes following definitions.
- on_more_io_data_called_
- Create
- Play
- Pause
- Close
- SetVolume
- GetOutputDeviceId
- SwitchOutputDevice
- DoCreate
- DoPlay
- StopStream
- DoPause
- DoClose
- DoSetVolume
- DoGetOutputDeviceId
- DoSwitchOutputDevice
- DoReportError
- OnMoreData
- OnMoreIOData
- OnError
- DoStopCloseAndClearStream
- OnDeviceChange
- GetAudioParameters
- StartDiverting
- StopDiverting
- DoStartDiverting
- DoStopDiverting
- ReadCurrentPowerAndClip
- WedgeCheck
#include "media/audio/audio_output_controller.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/metrics/histogram.h"
#include "base/task_runner_util.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "media/base/scoped_histogram_timer.h"
using base::TimeDelta;
namespace media {
#if defined(AUDIO_POWER_MONITORING)
static const int kPowerMeasurementTimeConstantMillis = 10;
#endif
const int AudioOutputController::kPollNumAttempts = 3;
const int AudioOutputController::kPollPauseInMilliseconds = 3;
AudioOutputController::AudioOutputController(
AudioManager* audio_manager,
EventHandler* handler,
const AudioParameters& params,
const std::string& output_device_id,
SyncReader* sync_reader)
: audio_manager_(audio_manager),
params_(params),
handler_(handler),
output_device_id_(output_device_id),
stream_(NULL),
diverting_to_stream_(NULL),
volume_(1.0),
state_(kEmpty),
not_currently_in_on_more_io_data_(1),
sync_reader_(sync_reader),
message_loop_(audio_manager->GetTaskRunner()),
#if defined(AUDIO_POWER_MONITORING)
power_monitor_(
params.sample_rate(),
TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMillis)),
#endif
on_more_io_data_called_(0) {
DCHECK(audio_manager);
DCHECK(handler_);
DCHECK(sync_reader_);
DCHECK(message_loop_.get());
}
AudioOutputController::~AudioOutputController() {
DCHECK_EQ(kClosed, state_);
CHECK(!base::AtomicRefCountDec(¬_currently_in_on_more_io_data_));
}
scoped_refptr<AudioOutputController> AudioOutputController::Create(
AudioManager* audio_manager,
EventHandler* event_handler,
const AudioParameters& params,
const std::string& output_device_id,
SyncReader* sync_reader) {
DCHECK(audio_manager);
DCHECK(sync_reader);
if (!params.IsValid() || !audio_manager)
return NULL;
scoped_refptr<AudioOutputController> controller(new AudioOutputController(
audio_manager, event_handler, params, output_device_id, sync_reader));
controller->message_loop_->PostTask(FROM_HERE, base::Bind(
&AudioOutputController::DoCreate, controller, false));
return controller;
}
void AudioOutputController::Play() {
message_loop_->PostTask(FROM_HERE, base::Bind(
&AudioOutputController::DoPlay, this));
}
void AudioOutputController::Pause() {
message_loop_->PostTask(FROM_HERE, base::Bind(
&AudioOutputController::DoPause, this));
}
void AudioOutputController::Close(const base::Closure& closed_task) {
DCHECK(!closed_task.is_null());
message_loop_->PostTaskAndReply(FROM_HERE, base::Bind(
&AudioOutputController::DoClose, this), closed_task);
}
void AudioOutputController::SetVolume(double volume) {
message_loop_->PostTask(FROM_HERE, base::Bind(
&AudioOutputController::DoSetVolume, this, volume));
}
void AudioOutputController::GetOutputDeviceId(
base::Callback<void(const std::string&)> callback) const {
base::PostTaskAndReplyWithResult(
message_loop_.get(),
FROM_HERE,
base::Bind(&AudioOutputController::DoGetOutputDeviceId, this),
callback);
}
void AudioOutputController::SwitchOutputDevice(
const std::string& output_device_id, const base::Closure& callback) {
message_loop_->PostTaskAndReply(
FROM_HERE,
base::Bind(&AudioOutputController::DoSwitchOutputDevice, this,
output_device_id),
callback);
}
void AudioOutputController::DoCreate(bool is_for_device_change) {
DCHECK(message_loop_->BelongsToCurrentThread());
SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CreateTime");
TRACE_EVENT0("audio", "AudioOutputController::DoCreate");
if (state_ == kClosed)
return;
DoStopCloseAndClearStream();
DCHECK_EQ(kEmpty, state_);
stream_ = diverting_to_stream_ ?
diverting_to_stream_ :
audio_manager_->MakeAudioOutputStreamProxy(params_, output_device_id_);
if (!stream_) {
state_ = kError;
handler_->OnError();
return;
}
if (!stream_->Open()) {
DoStopCloseAndClearStream();
state_ = kError;
handler_->OnError();
return;
}
if (stream_ != diverting_to_stream_)
audio_manager_->AddOutputDeviceChangeListener(this);
stream_->SetVolume(volume_);
state_ = kCreated;
if (!is_for_device_change)
handler_->OnCreated();
}
void AudioOutputController::DoPlay() {
DCHECK(message_loop_->BelongsToCurrentThread());
SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.PlayTime");
TRACE_EVENT0("audio", "AudioOutputController::DoPlay");
if (state_ != kCreated && state_ != kPaused)
return;
sync_reader_->UpdatePendingBytes(0);
state_ = kPlaying;
stream_->Start(this);
wedge_timer_.reset(new base::OneShotTimer<AudioOutputController>());
wedge_timer_->Start(
FROM_HERE, TimeDelta::FromSeconds(5), this,
&AudioOutputController::WedgeCheck);
handler_->OnPlaying();
}
void AudioOutputController::StopStream() {
DCHECK(message_loop_->BelongsToCurrentThread());
if (state_ == kPlaying) {
wedge_timer_.reset();
stream_->Stop();
#if defined(AUDIO_POWER_MONITORING)
power_monitor_.Reset();
#endif
state_ = kPaused;
}
}
void AudioOutputController::DoPause() {
DCHECK(message_loop_->BelongsToCurrentThread());
SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.PauseTime");
TRACE_EVENT0("audio", "AudioOutputController::DoPause");
StopStream();
if (state_ != kPaused)
return;
sync_reader_->UpdatePendingBytes(-1);
handler_->OnPaused();
}
void AudioOutputController::DoClose() {
DCHECK(message_loop_->BelongsToCurrentThread());
SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CloseTime");
TRACE_EVENT0("audio", "AudioOutputController::DoClose");
if (state_ != kClosed) {
DoStopCloseAndClearStream();
sync_reader_->Close();
state_ = kClosed;
}
}
void AudioOutputController::DoSetVolume(double volume) {
DCHECK(message_loop_->BelongsToCurrentThread());
volume_ = volume;
switch (state_) {
case kCreated:
case kPlaying:
case kPaused:
stream_->SetVolume(volume_);
break;
default:
return;
}
}
std::string AudioOutputController::DoGetOutputDeviceId() const {
DCHECK(message_loop_->BelongsToCurrentThread());
return output_device_id_;
}
void AudioOutputController::DoSwitchOutputDevice(
const std::string& output_device_id) {
DCHECK(message_loop_->BelongsToCurrentThread());
if (state_ == kClosed)
return;
if (output_device_id == output_device_id_)
return;
output_device_id_ = output_device_id;
if (stream_ != diverting_to_stream_)
OnDeviceChange();
}
void AudioOutputController::DoReportError() {
DCHECK(message_loop_->BelongsToCurrentThread());
if (state_ != kClosed)
handler_->OnError();
}
int AudioOutputController::OnMoreData(AudioBus* dest,
AudioBuffersState buffers_state) {
return OnMoreIOData(NULL, dest, buffers_state);
}
int AudioOutputController::OnMoreIOData(AudioBus* source,
AudioBus* dest,
AudioBuffersState buffers_state) {
CHECK(!base::AtomicRefCountDec(¬_currently_in_on_more_io_data_));
TRACE_EVENT0("audio", "AudioOutputController::OnMoreIOData");
if (base::AtomicRefCountIsZero(&on_more_io_data_called_))
base::AtomicRefCountInc(&on_more_io_data_called_);
sync_reader_->Read(source, dest);
const int frames = dest->frames();
sync_reader_->UpdatePendingBytes(
buffers_state.total_bytes() + frames * params_.GetBytesPerFrame());
#if defined(AUDIO_POWER_MONITORING)
power_monitor_.Scan(*dest, frames);
#endif
base::AtomicRefCountInc(¬_currently_in_on_more_io_data_);
return frames;
}
void AudioOutputController::OnError(AudioOutputStream* stream) {
message_loop_->PostTask(FROM_HERE, base::Bind(
&AudioOutputController::DoReportError, this));
}
void AudioOutputController::DoStopCloseAndClearStream() {
DCHECK(message_loop_->BelongsToCurrentThread());
if (stream_) {
if (stream_ != diverting_to_stream_)
audio_manager_->RemoveOutputDeviceChangeListener(this);
StopStream();
stream_->Close();
if (stream_ == diverting_to_stream_)
diverting_to_stream_ = NULL;
stream_ = NULL;
}
state_ = kEmpty;
}
void AudioOutputController::OnDeviceChange() {
DCHECK(message_loop_->BelongsToCurrentThread());
SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.DeviceChangeTime");
TRACE_EVENT0("audio", "AudioOutputController::OnDeviceChange");
const State original_state = state_;
DoCreate(true);
if (!stream_ || state_ == kError)
return;
switch (original_state) {
case kPlaying:
DoPlay();
return;
case kCreated:
case kPaused:
return;
default:
NOTREACHED() << "Invalid original state.";
}
}
const AudioParameters& AudioOutputController::GetAudioParameters() {
return params_;
}
void AudioOutputController::StartDiverting(AudioOutputStream* to_stream) {
message_loop_->PostTask(
FROM_HERE,
base::Bind(&AudioOutputController::DoStartDiverting, this, to_stream));
}
void AudioOutputController::StopDiverting() {
message_loop_->PostTask(
FROM_HERE, base::Bind(&AudioOutputController::DoStopDiverting, this));
}
void AudioOutputController::DoStartDiverting(AudioOutputStream* to_stream) {
DCHECK(message_loop_->BelongsToCurrentThread());
if (state_ == kClosed)
return;
DCHECK(!diverting_to_stream_);
diverting_to_stream_ = to_stream;
OnDeviceChange();
}
void AudioOutputController::DoStopDiverting() {
DCHECK(message_loop_->BelongsToCurrentThread());
if (state_ == kClosed)
return;
OnDeviceChange();
DCHECK(!diverting_to_stream_);
}
std::pair<float, bool> AudioOutputController::ReadCurrentPowerAndClip() {
#if defined(AUDIO_POWER_MONITORING)
return power_monitor_.ReadCurrentPowerAndClip();
#else
NOTREACHED();
return std::make_pair(AudioPowerMonitor::zero_power(), false);
#endif
}
void AudioOutputController::WedgeCheck() {
DCHECK(message_loop_->BelongsToCurrentThread());
if (state_ == kPlaying) {
const bool playback_success =
base::AtomicRefCountIsOne(&on_more_io_data_called_);
UMA_HISTOGRAM_BOOLEAN(
"Media.AudioOutputControllerPlaybackStartupSuccess", playback_success);
if (!playback_success)
audio_manager_->FixWedgedAudio();
}
}
}