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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
// 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/browser/renderer_host/media/audio_input_renderer_host.h"
#include "base/bind.h"
#include "base/metrics/histogram.h"
#include "base/process.h"
#include "base/shared_memory.h"
#include "content/browser/renderer_host/media/audio_input_device_manager.h"
#include "content/browser/renderer_host/media/audio_input_sync_writer.h"
#include "content/browser/renderer_host/media/media_stream_manager.h"
#include "content/common/media/audio_messages.h"
using content::BrowserMessageFilter;
using content::BrowserThread;
AudioInputRendererHost::AudioEntry::AudioEntry()
: stream_id(0),
pending_close(false) {
}
AudioInputRendererHost::AudioEntry::~AudioEntry() {}
AudioInputRendererHost::AudioInputRendererHost(
content::ResourceContext* resource_context,
AudioManager* audio_manager)
: resource_context_(resource_context),
audio_manager_(audio_manager) {
}
AudioInputRendererHost::~AudioInputRendererHost() {
DCHECK(audio_entries_.empty());
}
void AudioInputRendererHost::OnChannelClosing() {
BrowserMessageFilter::OnChannelClosing();
// Since the IPC channel is gone, close all requested audio streams.
DeleteEntries();
}
void AudioInputRendererHost::OnDestruct() const {
BrowserThread::DeleteOnIOThread::Destruct(this);
}
void AudioInputRendererHost::OnCreated(
media::AudioInputController* controller) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(
&AudioInputRendererHost::DoCompleteCreation,
this,
make_scoped_refptr(controller)));
}
void AudioInputRendererHost::OnRecording(
media::AudioInputController* controller) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(
&AudioInputRendererHost::DoSendRecordingMessage,
this,
make_scoped_refptr(controller)));
}
void AudioInputRendererHost::OnError(
media::AudioInputController* controller,
int error_code) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&AudioInputRendererHost::DoHandleError,
this, make_scoped_refptr(controller), error_code));
}
void AudioInputRendererHost::OnData(media::AudioInputController* controller,
const uint8* data,
uint32 size) {
NOTREACHED() << "Only low-latency mode is supported.";
}
void AudioInputRendererHost::DoCompleteCreation(
media::AudioInputController* controller) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
AudioEntry* entry = LookupByController(controller);
if (!entry)
return;
if (!peer_handle()) {
NOTREACHED() << "Renderer process handle is invalid.";
DeleteEntryOnError(entry);
return;
}
if (!entry->controller->LowLatencyMode()) {
NOTREACHED() << "Only low-latency mode is supported.";
DeleteEntryOnError(entry);
return;
}
// Once the audio stream is created then complete the creation process by
// mapping shared memory and sharing with the renderer process.
base::SharedMemoryHandle foreign_memory_handle;
if (!entry->shared_memory.ShareToProcess(peer_handle(),
&foreign_memory_handle)) {
// If we failed to map and share the shared memory then close the audio
// stream and send an error message.
DeleteEntryOnError(entry);
return;
}
AudioInputSyncWriter* writer =
static_cast<AudioInputSyncWriter*>(entry->writer.get());
#if defined(OS_WIN)
base::SyncSocket::Handle foreign_socket_handle;
#else
base::FileDescriptor foreign_socket_handle;
#endif
// If we failed to prepare the sync socket for the renderer then we fail
// the construction of audio input stream.
if (!writer->PrepareForeignSocketHandle(peer_handle(),
&foreign_socket_handle)) {
DeleteEntryOnError(entry);
return;
}
Send(new AudioInputMsg_NotifyStreamCreated(entry->stream_id,
foreign_memory_handle, foreign_socket_handle,
entry->shared_memory.created_size()));
}
void AudioInputRendererHost::DoSendRecordingMessage(
media::AudioInputController* controller) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// TODO(henrika): TBI?
NOTIMPLEMENTED();
}
void AudioInputRendererHost::DoSendPausedMessage(
media::AudioInputController* controller) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// TODO(henrika): TBI?
NOTREACHED();
}
void AudioInputRendererHost::DoHandleError(
media::AudioInputController* controller,
int error_code) {
DLOG(WARNING) << "AudioInputRendererHost::DoHandleError(error_code="
<< error_code << ")";
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
AudioEntry* entry = LookupByController(controller);
if (!entry)
return;
DeleteEntryOnError(entry);
}
bool AudioInputRendererHost::OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_EX(AudioInputRendererHost, message, *message_was_ok)
IPC_MESSAGE_HANDLER(AudioInputHostMsg_StartDevice, OnStartDevice)
IPC_MESSAGE_HANDLER(AudioInputHostMsg_CreateStream, OnCreateStream)
IPC_MESSAGE_HANDLER(AudioInputHostMsg_RecordStream, OnRecordStream)
IPC_MESSAGE_HANDLER(AudioInputHostMsg_CloseStream, OnCloseStream)
IPC_MESSAGE_HANDLER(AudioInputHostMsg_GetVolume, OnGetVolume)
IPC_MESSAGE_HANDLER(AudioInputHostMsg_SetVolume, OnSetVolume)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP_EX()
return handled;
}
void AudioInputRendererHost::OnStartDevice(int stream_id, int session_id) {
VLOG(1) << "AudioInputRendererHost::OnStartDevice(stream_id="
<< stream_id << ", session_id = " << session_id << ")";
// Get access to the AudioInputDeviceManager to start the device.
media_stream::AudioInputDeviceManager* audio_input_man =
media_stream::MediaStreamManager::GetForResourceContext(
resource_context_, audio_manager_)->audio_input_device_manager();
// Add the session entry to the map.
session_entries_[session_id] = stream_id;
// Start the device with the session_id. If the device is started
// successfully, OnDeviceStarted() callback will be triggered.
audio_input_man->Start(session_id, this);
}
void AudioInputRendererHost::OnCreateStream(int stream_id,
const AudioParameters& params,
const std::string& device_id) {
VLOG(1) << "AudioInputRendererHost::OnCreateStream(stream_id="
<< stream_id << ")";
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(LookupById(stream_id) == NULL);
AudioParameters audio_params(params);
DCHECK_GT(audio_params.frames_per_buffer(), 0);
uint32 buffer_size = audio_params.GetBytesPerBuffer();
// Create a new AudioEntry structure.
scoped_ptr<AudioEntry> entry(new AudioEntry());
// Create the shared memory and share it with the renderer process
// using a new SyncWriter object.
if (!entry->shared_memory.CreateAndMapAnonymous(buffer_size)) {
// If creation of shared memory failed then send an error message.
SendErrorMessage(stream_id);
return;
}
scoped_ptr<AudioInputSyncWriter> writer(
new AudioInputSyncWriter(&entry->shared_memory));
if (!writer->Init()) {
SendErrorMessage(stream_id);
return;
}
// If we have successfully created the SyncWriter then assign it to the
// entry and construct an AudioInputController.
// TODO(henrika): replace CreateLowLatency() with Create() as soon
// as satish has ensured that Speech Input also uses the default low-
// latency path. See crbug.com/112472 for details.
entry->writer.reset(writer.release());
entry->controller = media::AudioInputController::CreateLowLatency(
audio_manager_,
this,
audio_params,
device_id,
entry->writer.get());
if (!entry->controller) {
SendErrorMessage(stream_id);
return;
}
// If we have created the controller successfully create a entry and add it
// to the map.
entry->stream_id = stream_id;
audio_entries_.insert(std::make_pair(stream_id, entry.release()));
}
void AudioInputRendererHost::OnRecordStream(int stream_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
AudioEntry* entry = LookupById(stream_id);
if (!entry) {
SendErrorMessage(stream_id);
return;
}
entry->controller->Record();
}
void AudioInputRendererHost::OnCloseStream(int stream_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
AudioEntry* entry = LookupById(stream_id);
if (entry)
CloseAndDeleteStream(entry);
int session_id = LookupSessionById(stream_id);
if (session_id)
StopAndDeleteDevice(session_id);
}
void AudioInputRendererHost::OnSetVolume(int stream_id, double volume) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
AudioEntry* entry = LookupById(stream_id);
if (!entry) {
SendErrorMessage(stream_id);
return;
}
// TODO(henrika): TBI.
NOTIMPLEMENTED();
}
void AudioInputRendererHost::OnGetVolume(int stream_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
AudioEntry* entry = LookupById(stream_id);
if (!entry) {
SendErrorMessage(stream_id);
return;
}
// TODO(henrika): TBI.
NOTIMPLEMENTED();
}
void AudioInputRendererHost::SendErrorMessage(int stream_id) {
Send(new AudioInputMsg_NotifyStreamStateChanged(stream_id,
kAudioStreamError));
}
void AudioInputRendererHost::DeleteEntries() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
for (AudioEntryMap::iterator i = audio_entries_.begin();
i != audio_entries_.end(); ++i) {
CloseAndDeleteStream(i->second);
}
}
void AudioInputRendererHost::OnDeviceStarted(
int session_id, const std::string& device_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
SessionEntryMap::iterator it = session_entries_.find(session_id);
if (it == session_entries_.end()) {
DLOG(WARNING) << "AudioInputRendererHost::OnDeviceStarted()"
" session does not exist.";
return;
}
// Notify the renderer with the id of the opened device.
Send(new AudioInputMsg_NotifyDeviceStarted(it->second, device_id));
}
void AudioInputRendererHost::OnDeviceStopped(int session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
SessionEntryMap::iterator it = session_entries_.find(session_id);
// Return if the stream has been closed.
if (it == session_entries_.end())
return;
int stream_id = it->second;
AudioEntry* entry = LookupById(stream_id);
if (entry) {
// Device has been stopped, close the input stream.
CloseAndDeleteStream(entry);
// Notify the renderer that the state of the input stream has changed.
Send(new AudioInputMsg_NotifyStreamStateChanged(stream_id,
kAudioStreamPaused));
}
// Delete the session entry.
session_entries_.erase(it);
}
void AudioInputRendererHost::StopAndDeleteDevice(int session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
media_stream::AudioInputDeviceManager* audio_input_man =
media_stream::MediaStreamManager::GetForResourceContext(
resource_context_, audio_manager_)->audio_input_device_manager();
audio_input_man->Stop(session_id);
// Delete the session entry.
session_entries_.erase(session_id);
}
void AudioInputRendererHost::CloseAndDeleteStream(AudioEntry* entry) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!entry->pending_close) {
entry->controller->Close(base::Bind(&AudioInputRendererHost::DeleteEntry,
this, entry));
entry->pending_close = true;
}
}
void AudioInputRendererHost::DeleteEntry(AudioEntry* entry) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Delete the entry when this method goes out of scope.
scoped_ptr<AudioEntry> entry_deleter(entry);
// Erase the entry from the map.
audio_entries_.erase(entry->stream_id);
}
void AudioInputRendererHost::DeleteEntryOnError(AudioEntry* entry) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Sends the error message first before we close the stream because
// |entry| is destroyed in DeleteEntry().
SendErrorMessage(entry->stream_id);
CloseAndDeleteStream(entry);
}
AudioInputRendererHost::AudioEntry* AudioInputRendererHost::LookupById(
int stream_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
AudioEntryMap::iterator i = audio_entries_.find(stream_id);
if (i != audio_entries_.end())
return i->second;
return NULL;
}
AudioInputRendererHost::AudioEntry* AudioInputRendererHost::LookupByController(
media::AudioInputController* controller) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Iterate the map of entries.
// TODO(hclam): Implement a faster look up method.
for (AudioEntryMap::iterator i = audio_entries_.begin();
i != audio_entries_.end(); ++i) {
if (controller == i->second->controller.get())
return i->second;
}
return NULL;
}
int AudioInputRendererHost::LookupSessionById(int stream_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
for (SessionEntryMap::iterator it = session_entries_.begin();
it != session_entries_.end(); ++it) {
if (stream_id == it->second) {
return it->first;
}
}
return 0;
}
|