Add ICU message format support
[chromium-blink-merge.git] / content / renderer / pepper / pepper_platform_audio_output.cc
blob010b30f75db5c87f8aa86e17c9c1f9262ad7b77d
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 "content/renderer/pepper/pepper_platform_audio_output.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "build/build_config.h"
13 #include "content/child/child_process.h"
14 #include "content/common/media/audio_messages.h"
15 #include "content/renderer/media/audio_message_filter.h"
16 #include "content/renderer/pepper/audio_helper.h"
17 #include "content/renderer/render_thread_impl.h"
18 #include "media/base/audio_hardware_config.h"
19 #include "ppapi/shared_impl/ppb_audio_config_shared.h"
21 namespace content {
23 // static
24 PepperPlatformAudioOutput* PepperPlatformAudioOutput::Create(
25 int sample_rate,
26 int frames_per_buffer,
27 int source_render_frame_id,
28 AudioHelper* client) {
29 scoped_refptr<PepperPlatformAudioOutput> audio_output(
30 new PepperPlatformAudioOutput());
31 if (audio_output->Initialize(sample_rate,
32 frames_per_buffer,
33 source_render_frame_id,
34 client)) {
35 // Balanced by Release invoked in
36 // PepperPlatformAudioOutput::ShutDownOnIOThread().
37 audio_output->AddRef();
38 return audio_output.get();
40 return NULL;
43 bool PepperPlatformAudioOutput::StartPlayback() {
44 if (ipc_) {
45 io_task_runner_->PostTask(
46 FROM_HERE,
47 base::Bind(&PepperPlatformAudioOutput::StartPlaybackOnIOThread, this));
48 return true;
50 return false;
53 bool PepperPlatformAudioOutput::StopPlayback() {
54 if (ipc_) {
55 io_task_runner_->PostTask(
56 FROM_HERE,
57 base::Bind(&PepperPlatformAudioOutput::StopPlaybackOnIOThread, this));
58 return true;
60 return false;
63 void PepperPlatformAudioOutput::ShutDown() {
64 // Called on the main thread to stop all audio callbacks. We must only change
65 // the client on the main thread, and the delegates from the I/O thread.
66 client_ = NULL;
67 io_task_runner_->PostTask(
68 FROM_HERE,
69 base::Bind(&PepperPlatformAudioOutput::ShutDownOnIOThread, this));
72 void PepperPlatformAudioOutput::OnStateChanged(
73 media::AudioOutputIPCDelegateState state) {}
75 void PepperPlatformAudioOutput::OnStreamCreated(
76 base::SharedMemoryHandle handle,
77 base::SyncSocket::Handle socket_handle,
78 int length) {
79 #if defined(OS_WIN)
80 DCHECK(handle);
81 DCHECK(socket_handle);
82 #else
83 DCHECK(base::SharedMemory::IsHandleValid(handle));
84 DCHECK_NE(-1, socket_handle);
85 #endif
86 DCHECK(length);
88 if (base::ThreadTaskRunnerHandle::Get().get() == main_task_runner_.get()) {
89 // Must dereference the client only on the main thread. Shutdown may have
90 // occurred while the request was in-flight, so we need to NULL check.
91 if (client_)
92 client_->StreamCreated(handle, length, socket_handle);
93 } else {
94 main_task_runner_->PostTask(
95 FROM_HERE, base::Bind(&PepperPlatformAudioOutput::OnStreamCreated, this,
96 handle, socket_handle, length));
100 void PepperPlatformAudioOutput::OnOutputDeviceSwitched(
101 int request_id,
102 media::SwitchOutputDeviceResult result) {}
104 void PepperPlatformAudioOutput::OnIPCClosed() { ipc_.reset(); }
106 PepperPlatformAudioOutput::~PepperPlatformAudioOutput() {
107 // Make sure we have been shut down. Warning: this will usually happen on
108 // the I/O thread!
109 DCHECK(!ipc_);
110 DCHECK(!client_);
113 PepperPlatformAudioOutput::PepperPlatformAudioOutput()
114 : client_(NULL),
115 main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
116 io_task_runner_(ChildProcess::current()->io_task_runner()) {
119 bool PepperPlatformAudioOutput::Initialize(int sample_rate,
120 int frames_per_buffer,
121 int source_render_frame_id,
122 AudioHelper* client) {
123 DCHECK(client);
124 client_ = client;
126 RenderThreadImpl* const render_thread = RenderThreadImpl::current();
127 ipc_ = render_thread->audio_message_filter()->CreateAudioOutputIPC(
128 source_render_frame_id);
129 CHECK(ipc_);
131 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
132 media::CHANNEL_LAYOUT_STEREO,
133 sample_rate,
134 ppapi::kBitsPerAudioOutputSample,
135 frames_per_buffer);
137 io_task_runner_->PostTask(
138 FROM_HERE, base::Bind(&PepperPlatformAudioOutput::InitializeOnIOThread,
139 this, params));
140 return true;
143 void PepperPlatformAudioOutput::InitializeOnIOThread(
144 const media::AudioParameters& params) {
145 DCHECK(io_task_runner_->BelongsToCurrentThread());
146 const int kSessionId = 0;
147 if (ipc_)
148 ipc_->CreateStream(this, params, kSessionId);
151 void PepperPlatformAudioOutput::StartPlaybackOnIOThread() {
152 DCHECK(io_task_runner_->BelongsToCurrentThread());
153 if (ipc_)
154 ipc_->PlayStream();
157 void PepperPlatformAudioOutput::StopPlaybackOnIOThread() {
158 DCHECK(io_task_runner_->BelongsToCurrentThread());
159 if (ipc_)
160 ipc_->PauseStream();
163 void PepperPlatformAudioOutput::ShutDownOnIOThread() {
164 DCHECK(io_task_runner_->BelongsToCurrentThread());
166 // Make sure we don't call shutdown more than once.
167 if (!ipc_)
168 return;
170 ipc_->CloseStream();
171 ipc_.reset();
173 Release(); // Release for the delegate, balances out the reference taken in
174 // PepperPlatformAudioOutput::Create.
177 } // namespace content