This source file includes following definitions.
- stopping_hack_
- InitializeUnifiedStream
- Initialize
- Start
- Stop
- Play
- Pause
- SetVolume
- CreateStreamOnIOThread
- PlayOnIOThread
- PauseOnIOThread
- ShutDownOnIOThread
- SetVolumeOnIOThread
- OnStateChanged
- OnStreamCreated
- OnIPCClosed
- WillDestroyCurrentMessageLoop
- render_callback_
- MapSharedMemory
- Process
#include "media/audio/audio_output_device.h"
#include "base/basictypes.h"
#include "base/debug/trace_event.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "media/audio/audio_output_controller.h"
#include "media/base/limits.h"
namespace media {
class AudioOutputDevice::AudioThreadCallback
: public AudioDeviceThread::Callback {
public:
AudioThreadCallback(const AudioParameters& audio_parameters,
base::SharedMemoryHandle memory,
int memory_length,
AudioRendererSink::RenderCallback* render_callback);
virtual ~AudioThreadCallback();
virtual void MapSharedMemory() OVERRIDE;
virtual void Process(int pending_data) OVERRIDE;
private:
AudioRendererSink::RenderCallback* render_callback_;
scoped_ptr<AudioBus> input_bus_;
scoped_ptr<AudioBus> output_bus_;
DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback);
};
AudioOutputDevice::AudioOutputDevice(
scoped_ptr<AudioOutputIPC> ipc,
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
: ScopedTaskRunnerObserver(io_task_runner),
callback_(NULL),
ipc_(ipc.Pass()),
state_(IDLE),
play_on_start_(true),
session_id_(-1),
stopping_hack_(false) {
CHECK(ipc_);
COMPILE_ASSERT(IPC_CLOSED < IDLE, invalid_enum_value_assignment_0);
COMPILE_ASSERT(IDLE < CREATING_STREAM, invalid_enum_value_assignment_1);
COMPILE_ASSERT(CREATING_STREAM < PAUSED, invalid_enum_value_assignment_2);
COMPILE_ASSERT(PAUSED < PLAYING, invalid_enum_value_assignment_3);
}
void AudioOutputDevice::InitializeUnifiedStream(const AudioParameters& params,
RenderCallback* callback,
int session_id) {
DCHECK(!callback_) << "Calling InitializeUnifiedStream() twice?";
DCHECK(params.IsValid());
audio_parameters_ = params;
callback_ = callback;
session_id_ = session_id;
}
void AudioOutputDevice::Initialize(const AudioParameters& params,
RenderCallback* callback) {
InitializeUnifiedStream(params, callback, 0);
}
AudioOutputDevice::~AudioOutputDevice() {
DCHECK(audio_thread_.IsStopped());
}
void AudioOutputDevice::Start() {
DCHECK(callback_) << "Initialize hasn't been called";
task_runner()->PostTask(FROM_HERE,
base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this,
audio_parameters_));
}
void AudioOutputDevice::Stop() {
{
base::AutoLock auto_lock(audio_thread_lock_);
audio_thread_.Stop(base::MessageLoop::current());
stopping_hack_ = true;
}
task_runner()->PostTask(FROM_HERE,
base::Bind(&AudioOutputDevice::ShutDownOnIOThread, this));
}
void AudioOutputDevice::Play() {
task_runner()->PostTask(FROM_HERE,
base::Bind(&AudioOutputDevice::PlayOnIOThread, this));
}
void AudioOutputDevice::Pause() {
task_runner()->PostTask(FROM_HERE,
base::Bind(&AudioOutputDevice::PauseOnIOThread, this));
}
bool AudioOutputDevice::SetVolume(double volume) {
if (volume < 0 || volume > 1.0)
return false;
if (!task_runner()->PostTask(FROM_HERE,
base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) {
return false;
}
return true;
}
void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) {
DCHECK(task_runner()->BelongsToCurrentThread());
if (state_ == IDLE) {
state_ = CREATING_STREAM;
ipc_->CreateStream(this, params, session_id_);
}
}
void AudioOutputDevice::PlayOnIOThread() {
DCHECK(task_runner()->BelongsToCurrentThread());
if (state_ == PAUSED) {
ipc_->PlayStream();
state_ = PLAYING;
play_on_start_ = false;
} else {
play_on_start_ = true;
}
}
void AudioOutputDevice::PauseOnIOThread() {
DCHECK(task_runner()->BelongsToCurrentThread());
if (state_ == PLAYING) {
ipc_->PauseStream();
state_ = PAUSED;
}
play_on_start_ = false;
}
void AudioOutputDevice::ShutDownOnIOThread() {
DCHECK(task_runner()->BelongsToCurrentThread());
if (state_ >= CREATING_STREAM) {
ipc_->CloseStream();
state_ = IDLE;
}
base::AutoLock auto_lock_(audio_thread_lock_);
base::ThreadRestrictions::ScopedAllowIO allow_io;
audio_thread_.Stop(NULL);
audio_callback_.reset();
stopping_hack_ = false;
}
void AudioOutputDevice::SetVolumeOnIOThread(double volume) {
DCHECK(task_runner()->BelongsToCurrentThread());
if (state_ >= CREATING_STREAM)
ipc_->SetVolume(volume);
}
void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) {
DCHECK(task_runner()->BelongsToCurrentThread());
if (state_ < CREATING_STREAM)
return;
switch (state) {
case AudioOutputIPCDelegate::kPlaying:
case AudioOutputIPCDelegate::kPaused:
break;
case AudioOutputIPCDelegate::kError:
DLOG(WARNING) << "AudioOutputDevice::OnStateChanged(kError)";
if (!audio_thread_.IsStopped())
callback_->OnRenderError();
break;
default:
NOTREACHED();
break;
}
}
void AudioOutputDevice::OnStreamCreated(
base::SharedMemoryHandle handle,
base::SyncSocket::Handle socket_handle,
int length) {
DCHECK(task_runner()->BelongsToCurrentThread());
#if defined(OS_WIN)
DCHECK(handle);
DCHECK(socket_handle);
#else
DCHECK_GE(handle.fd, 0);
DCHECK_GE(socket_handle, 0);
#endif
DCHECK_GT(length, 0);
if (state_ != CREATING_STREAM)
return;
base::AutoLock auto_lock(audio_thread_lock_);
if (stopping_hack_)
return;
DCHECK(audio_thread_.IsStopped());
audio_callback_.reset(new AudioOutputDevice::AudioThreadCallback(
audio_parameters_, handle, length, callback_));
audio_thread_.Start(
audio_callback_.get(), socket_handle, "AudioOutputDevice", true);
state_ = PAUSED;
if (play_on_start_)
PlayOnIOThread();
}
void AudioOutputDevice::OnIPCClosed() {
DCHECK(task_runner()->BelongsToCurrentThread());
state_ = IPC_CLOSED;
ipc_.reset();
}
void AudioOutputDevice::WillDestroyCurrentMessageLoop() {
LOG(ERROR) << "IO loop going away before the audio device has been stopped";
ShutDownOnIOThread();
}
AudioOutputDevice::AudioThreadCallback::AudioThreadCallback(
const AudioParameters& audio_parameters,
base::SharedMemoryHandle memory,
int memory_length,
AudioRendererSink::RenderCallback* render_callback)
: AudioDeviceThread::Callback(audio_parameters, memory, memory_length, 1),
render_callback_(render_callback) {}
AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() {
}
void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() {
CHECK_EQ(total_segments_, 1);
CHECK(shared_memory_.Map(memory_length_));
int output_memory_size = AudioBus::CalculateMemorySize(audio_parameters_);
int input_channels = audio_parameters_.input_channels();
int frames = audio_parameters_.frames_per_buffer();
int input_memory_size = AudioBus::CalculateMemorySize(input_channels, frames);
int io_size = output_memory_size + input_memory_size;
DCHECK_EQ(memory_length_, io_size);
output_bus_ =
AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory());
if (input_channels > 0) {
char* input_data =
static_cast<char*>(shared_memory_.memory()) + output_memory_size;
input_bus_ = AudioBus::WrapMemory(input_channels, frames, input_data);
}
}
void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) {
if (pending_data < 0)
return;
int audio_delay_milliseconds = pending_data / bytes_per_ms_;
TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback");
int input_channels = audio_parameters_.input_channels();
if (input_bus_ && input_channels > 0) {
render_callback_->RenderIO(
input_bus_.get(), output_bus_.get(), audio_delay_milliseconds);
} else {
render_callback_->Render(output_bus_.get(), audio_delay_milliseconds);
}
}
}