1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/pepper/pepper_platform_audio_input_impl.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop_proxy.h"
#include "build/build_config.h"
#include "content/common/child_process.h"
#include "content/common/media/audio_messages.h"
#include "content/renderer/pepper/pepper_plugin_delegate_impl.h"
#include "content/renderer/render_thread_impl.h"
#include "media/audio/audio_manager_base.h"
PepperPlatformAudioInputImpl::PepperPlatformAudioInputImpl()
: client_(NULL),
stream_id_(0),
main_message_loop_proxy_(base::MessageLoopProxy::current()),
shutdown_called_(false) {
filter_ = RenderThreadImpl::current()->audio_input_message_filter();
}
PepperPlatformAudioInputImpl::~PepperPlatformAudioInputImpl() {
// Make sure we have been shut down. Warning: this may happen on the I/O
// thread!
// Although these members should be accessed on a specific thread (either the
// main thread or the I/O thread), it should be fine to examine their value
// here.
DCHECK_EQ(0, stream_id_);
DCHECK(!client_);
DCHECK(label_.empty());
DCHECK(shutdown_called_);
}
// static
PepperPlatformAudioInputImpl* PepperPlatformAudioInputImpl::Create(
const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate,
const std::string& device_id,
int sample_rate,
int frames_per_buffer,
webkit::ppapi::PluginDelegate::PlatformAudioInputClient* client) {
scoped_refptr<PepperPlatformAudioInputImpl> audio_input(
new PepperPlatformAudioInputImpl);
if (audio_input->Initialize(plugin_delegate, device_id, sample_rate,
frames_per_buffer, client)) {
// Balanced by Release invoked in
// PepperPlatformAudioInputImpl::ShutDownOnIOThread().
return audio_input.release();
}
return NULL;
}
void PepperPlatformAudioInputImpl::StartCapture() {
DCHECK(main_message_loop_proxy_->BelongsToCurrentThread());
ChildProcess::current()->io_message_loop()->PostTask(
FROM_HERE,
base::Bind(&PepperPlatformAudioInputImpl::StartCaptureOnIOThread, this));
}
void PepperPlatformAudioInputImpl::StopCapture() {
DCHECK(main_message_loop_proxy_->BelongsToCurrentThread());
ChildProcess::current()->io_message_loop()->PostTask(
FROM_HERE,
base::Bind(&PepperPlatformAudioInputImpl::StopCaptureOnIOThread, this));
}
void PepperPlatformAudioInputImpl::ShutDown() {
DCHECK(main_message_loop_proxy_->BelongsToCurrentThread());
// Called on the main thread to stop all audio callbacks. We must only change
// the client on the main thread, and the delegates from the I/O thread.
client_ = NULL;
ChildProcess::current()->io_message_loop()->PostTask(
FROM_HERE,
base::Bind(&PepperPlatformAudioInputImpl::ShutDownOnIOThread, this));
}
bool PepperPlatformAudioInputImpl::Initialize(
const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate,
const std::string& device_id,
int sample_rate,
int frames_per_buffer,
webkit::ppapi::PluginDelegate::PlatformAudioInputClient* client) {
DCHECK(main_message_loop_proxy_->BelongsToCurrentThread());
if (!plugin_delegate || !client)
return false;
plugin_delegate_ = plugin_delegate;
client_ = client;
params_.Reset(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
sample_rate, 16, frames_per_buffer);
if (device_id.empty()) {
// Use the default device.
ChildProcess::current()->io_message_loop()->PostTask(
FROM_HERE,
base::Bind(&PepperPlatformAudioInputImpl::InitializeOnIOThread,
this, 0));
} else {
// We need to open the device and obtain the label and session ID before
// initializing.
plugin_delegate_->OpenDevice(
PP_DEVICETYPE_DEV_AUDIOCAPTURE, device_id,
base::Bind(&PepperPlatformAudioInputImpl::OnDeviceOpened, this));
}
return true;
}
void PepperPlatformAudioInputImpl::InitializeOnIOThread(int session_id) {
DCHECK(ChildProcess::current()->io_message_loop_proxy()->
BelongsToCurrentThread());
if (shutdown_called_)
return;
// Make sure we don't call init more than once.
DCHECK_EQ(0, stream_id_);
stream_id_ = filter_->AddDelegate(this);
DCHECK_NE(0, stream_id_);
if (!session_id) {
// We will be notified by OnStreamCreated().
filter_->Send(new AudioInputHostMsg_CreateStream(
stream_id_, params_, AudioManagerBase::kDefaultDeviceId, false));
} else {
// We will be notified by OnDeviceReady().
filter_->Send(new AudioInputHostMsg_StartDevice(stream_id_, session_id));
}
}
void PepperPlatformAudioInputImpl::StartCaptureOnIOThread() {
DCHECK(ChildProcess::current()->io_message_loop_proxy()->
BelongsToCurrentThread());
if (stream_id_)
filter_->Send(new AudioInputHostMsg_RecordStream(stream_id_));
}
void PepperPlatformAudioInputImpl::StopCaptureOnIOThread() {
DCHECK(ChildProcess::current()->io_message_loop_proxy()->
BelongsToCurrentThread());
// TODO(yzshen): We cannot re-start capturing if the stream is closed.
if (stream_id_)
filter_->Send(new AudioInputHostMsg_CloseStream(stream_id_));
}
void PepperPlatformAudioInputImpl::ShutDownOnIOThread() {
DCHECK(ChildProcess::current()->io_message_loop_proxy()->
BelongsToCurrentThread());
// Make sure we don't call shutdown more than once.
if (shutdown_called_)
return;
shutdown_called_ = true;
if (stream_id_) {
filter_->Send(new AudioInputHostMsg_CloseStream(stream_id_));
filter_->RemoveDelegate(stream_id_);
stream_id_ = 0;
}
main_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&PepperPlatformAudioInputImpl::CloseDevice, this));
Release(); // Release for the delegate, balances out the reference taken in
// PepperPluginDelegateImpl::CreateAudioInput.
}
void PepperPlatformAudioInputImpl::OnStreamCreated(
base::SharedMemoryHandle handle,
base::SyncSocket::Handle socket_handle,
uint32 length) {
#if defined(OS_WIN)
DCHECK(handle);
DCHECK(socket_handle);
#else
DCHECK_NE(-1, handle.fd);
DCHECK_NE(-1, socket_handle);
#endif
DCHECK(length);
if (base::MessageLoopProxy::current() != main_message_loop_proxy_) {
// No need to check |shutdown_called_| here. If shutdown has occurred,
// |client_| will be NULL and the handles will be cleaned up on the main
// thread.
main_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&PepperPlatformAudioInputImpl::OnStreamCreated, this,
handle, socket_handle, length));
} else {
// Must dereference the client only on the main thread. Shutdown may have
// occurred while the request was in-flight, so we need to NULL check.
if (client_) {
client_->StreamCreated(handle, length, socket_handle);
} else {
// Clean up the handles.
base::SyncSocket temp_socket(socket_handle);
base::SharedMemory temp_shared_memory(handle, false);
}
}
}
void PepperPlatformAudioInputImpl::OnVolume(double volume) {
}
void PepperPlatformAudioInputImpl::OnStateChanged(AudioStreamState state) {
}
void PepperPlatformAudioInputImpl::OnDeviceReady(const std::string& device_id) {
DCHECK(ChildProcess::current()->io_message_loop_proxy()->
BelongsToCurrentThread());
if (shutdown_called_)
return;
if (device_id.empty()) {
main_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&PepperPlatformAudioInputImpl::NotifyStreamCreationFailed,
this));
} else {
// We will be notified by OnStreamCreated().
filter_->Send(new AudioInputHostMsg_CreateStream(stream_id_, params_,
device_id, false));
}
}
void PepperPlatformAudioInputImpl::OnDeviceOpened(int request_id,
bool succeeded,
const std::string& label) {
DCHECK(main_message_loop_proxy_->BelongsToCurrentThread());
if (succeeded && plugin_delegate_) {
DCHECK(!label.empty());
label_ = label;
if (client_) {
int session_id = plugin_delegate_->GetSessionID(
PP_DEVICETYPE_DEV_AUDIOCAPTURE, label);
ChildProcess::current()->io_message_loop()->PostTask(
FROM_HERE,
base::Bind(&PepperPlatformAudioInputImpl::InitializeOnIOThread,
this, session_id));
} else {
// Shutdown has occurred.
CloseDevice();
}
} else {
NotifyStreamCreationFailed();
}
}
void PepperPlatformAudioInputImpl::CloseDevice() {
DCHECK(main_message_loop_proxy_->BelongsToCurrentThread());
if (plugin_delegate_ && !label_.empty()) {
plugin_delegate_->CloseDevice(label_);
label_.clear();
}
}
void PepperPlatformAudioInputImpl::NotifyStreamCreationFailed() {
DCHECK(main_message_loop_proxy_->BelongsToCurrentThread());
if (client_)
client_->StreamCreationFailed();
}
|