OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/media/audio_input_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_input_renderer_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/process.h" | 9 #include "base/process.h" |
10 #include "base/shared_memory.h" | 10 #include "base/shared_memory.h" |
11 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 11 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
12 #include "content/browser/renderer_host/media/audio_input_sync_writer.h" | 12 #include "content/browser/renderer_host/media/audio_input_sync_writer.h" |
13 #include "content/browser/renderer_host/media/media_stream_manager.h" | 13 #include "content/browser/renderer_host/media/media_stream_manager.h" |
14 #include "content/browser/renderer_host/media/web_contents_audio_input_stream.h" | 14 #include "content/browser/renderer_host/media/web_contents_audio_input_stream.h" |
15 #include "content/browser/renderer_host/media/web_contents_capture_util.h" | 15 #include "content/browser/renderer_host/media/web_contents_capture_util.h" |
16 #include "content/common/media/audio_messages.h" | 16 #include "content/common/media/audio_messages.h" |
| 17 #include "media/audio/audio_manager_base.h" |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 | 20 |
20 struct AudioInputRendererHost::AudioEntry { | 21 struct AudioInputRendererHost::AudioEntry { |
21 AudioEntry(); | 22 AudioEntry(); |
22 ~AudioEntry(); | 23 ~AudioEntry(); |
23 | 24 |
24 // The AudioInputController that manages the audio input stream. | 25 // The AudioInputController that manages the audio input stream. |
25 scoped_refptr<media::AudioInputController> controller; | 26 scoped_refptr<media::AudioInputController> controller; |
26 | 27 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 if (!entry) | 181 if (!entry) |
181 return; | 182 return; |
182 | 183 |
183 DeleteEntryOnError(entry); | 184 DeleteEntryOnError(entry); |
184 } | 185 } |
185 | 186 |
186 bool AudioInputRendererHost::OnMessageReceived(const IPC::Message& message, | 187 bool AudioInputRendererHost::OnMessageReceived(const IPC::Message& message, |
187 bool* message_was_ok) { | 188 bool* message_was_ok) { |
188 bool handled = true; | 189 bool handled = true; |
189 IPC_BEGIN_MESSAGE_MAP_EX(AudioInputRendererHost, message, *message_was_ok) | 190 IPC_BEGIN_MESSAGE_MAP_EX(AudioInputRendererHost, message, *message_was_ok) |
190 IPC_MESSAGE_HANDLER(AudioInputHostMsg_StartDevice, OnStartDevice) | |
191 IPC_MESSAGE_HANDLER(AudioInputHostMsg_CreateStream, OnCreateStream) | 191 IPC_MESSAGE_HANDLER(AudioInputHostMsg_CreateStream, OnCreateStream) |
192 IPC_MESSAGE_HANDLER(AudioInputHostMsg_AssociateStreamWithConsumer, | 192 IPC_MESSAGE_HANDLER(AudioInputHostMsg_AssociateStreamWithConsumer, |
193 OnAssociateStreamWithConsumer) | 193 OnAssociateStreamWithConsumer) |
194 IPC_MESSAGE_HANDLER(AudioInputHostMsg_RecordStream, OnRecordStream) | 194 IPC_MESSAGE_HANDLER(AudioInputHostMsg_RecordStream, OnRecordStream) |
195 IPC_MESSAGE_HANDLER(AudioInputHostMsg_CloseStream, OnCloseStream) | 195 IPC_MESSAGE_HANDLER(AudioInputHostMsg_CloseStream, OnCloseStream) |
196 IPC_MESSAGE_HANDLER(AudioInputHostMsg_SetVolume, OnSetVolume) | 196 IPC_MESSAGE_HANDLER(AudioInputHostMsg_SetVolume, OnSetVolume) |
197 IPC_MESSAGE_UNHANDLED(handled = false) | 197 IPC_MESSAGE_UNHANDLED(handled = false) |
198 IPC_END_MESSAGE_MAP_EX() | 198 IPC_END_MESSAGE_MAP_EX() |
199 | 199 |
200 return handled; | 200 return handled; |
201 } | 201 } |
202 | 202 |
203 void AudioInputRendererHost::OnStartDevice(int stream_id, int session_id) { | |
204 VLOG(1) << "AudioInputRendererHost::OnStartDevice(stream_id=" | |
205 << stream_id << ", session_id = " << session_id << ")"; | |
206 | |
207 // Add the session entry to the map. | |
208 session_entries_[session_id] = stream_id; | |
209 | |
210 // Start the device with the session_id. If the device is started | |
211 // successfully, OnDeviceStarted() callback will be triggered. | |
212 media_stream_manager_->audio_input_device_manager()->Start(session_id, this); | |
213 } | |
214 | |
215 void AudioInputRendererHost::OnCreateStream( | 203 void AudioInputRendererHost::OnCreateStream( |
216 int stream_id, | 204 int stream_id, |
| 205 int session_id, |
217 const media::AudioParameters& params, | 206 const media::AudioParameters& params, |
218 const std::string& device_id, | |
219 bool automatic_gain_control, | 207 bool automatic_gain_control, |
220 int shared_memory_count) { | 208 int shared_memory_count) { |
221 VLOG(1) << "AudioInputRendererHost::OnCreateStream(stream_id=" | 209 VLOG(1) << "AudioInputRendererHost::OnCreateStream(stream_id=" |
222 << stream_id << ")"; | 210 << stream_id << ", session_id=" << session_id << ")"; |
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
224 // media::AudioParameters is validated in the deserializer. | 212 // media::AudioParameters is validated in the deserializer. |
225 if (LookupById(stream_id) != NULL) { | 213 if (LookupById(stream_id) != NULL) { |
226 SendErrorMessage(stream_id); | 214 SendErrorMessage(stream_id); |
227 return; | 215 return; |
228 } | 216 } |
229 | 217 |
| 218 // Check if we have the permission to open the device and which device to use. |
| 219 std::string device_id = media::AudioManagerBase::kDefaultDeviceId; |
| 220 if (session_id != AudioInputDeviceManager::kFakeOpenSessionId) { |
| 221 const StreamDeviceInfo& info = media_stream_manager_-> |
| 222 audio_input_device_manager()->GetOpenedDeviceInfoById(session_id); |
| 223 if (info.device.id.empty()) { |
| 224 // An empty device info indicates that no permission has been granted. |
| 225 SendErrorMessage(stream_id); |
| 226 DLOG(WARNING) << "No permission has been granted to input stream with " |
| 227 << "session_id=" << session_id; |
| 228 return; |
| 229 } |
| 230 |
| 231 device_id = info.device.id; |
| 232 } |
| 233 |
230 media::AudioParameters audio_params(params); | 234 media::AudioParameters audio_params(params); |
231 | |
232 if (media_stream_manager_->audio_input_device_manager()-> | 235 if (media_stream_manager_->audio_input_device_manager()-> |
233 ShouldUseFakeDevice()) { | 236 ShouldUseFakeDevice()) { |
234 audio_params.Reset(media::AudioParameters::AUDIO_FAKE, | 237 audio_params.Reset(media::AudioParameters::AUDIO_FAKE, |
235 params.channel_layout(), params.channels(), 0, | 238 params.channel_layout(), params.channels(), 0, |
236 params.sample_rate(), | 239 params.sample_rate(), |
237 params.bits_per_sample(), params.frames_per_buffer()); | 240 params.bits_per_sample(), params.frames_per_buffer()); |
238 } | 241 } |
239 | 242 |
240 uint32 buffer_size = audio_params.GetBytesPerBuffer(); | 243 uint32 buffer_size = audio_params.GetBytesPerBuffer(); |
241 | 244 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 entry->controller->Record(); | 326 entry->controller->Record(); |
324 } | 327 } |
325 | 328 |
326 void AudioInputRendererHost::OnCloseStream(int stream_id) { | 329 void AudioInputRendererHost::OnCloseStream(int stream_id) { |
327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
328 | 331 |
329 AudioEntry* entry = LookupById(stream_id); | 332 AudioEntry* entry = LookupById(stream_id); |
330 | 333 |
331 if (entry) | 334 if (entry) |
332 CloseAndDeleteStream(entry); | 335 CloseAndDeleteStream(entry); |
333 | |
334 int session_id = LookupSessionById(stream_id); | |
335 | |
336 if (session_id) | |
337 StopAndDeleteDevice(session_id); | |
338 } | 336 } |
339 | 337 |
340 void AudioInputRendererHost::OnSetVolume(int stream_id, double volume) { | 338 void AudioInputRendererHost::OnSetVolume(int stream_id, double volume) { |
341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
342 | 340 |
343 AudioEntry* entry = LookupById(stream_id); | 341 AudioEntry* entry = LookupById(stream_id); |
344 if (!entry) { | 342 if (!entry) { |
345 SendErrorMessage(stream_id); | 343 SendErrorMessage(stream_id); |
346 return; | 344 return; |
347 } | 345 } |
348 | 346 |
349 entry->controller->SetVolume(volume); | 347 entry->controller->SetVolume(volume); |
350 } | 348 } |
351 | 349 |
352 void AudioInputRendererHost::SendErrorMessage(int stream_id) { | 350 void AudioInputRendererHost::SendErrorMessage(int stream_id) { |
353 Send(new AudioInputMsg_NotifyStreamStateChanged( | 351 Send(new AudioInputMsg_NotifyStreamStateChanged( |
354 stream_id, media::AudioInputIPCDelegate::kError)); | 352 stream_id, media::AudioInputIPCDelegate::kError)); |
355 } | 353 } |
356 | 354 |
357 void AudioInputRendererHost::DeleteEntries() { | 355 void AudioInputRendererHost::DeleteEntries() { |
358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
359 | 357 |
360 for (AudioEntryMap::iterator i = audio_entries_.begin(); | 358 for (AudioEntryMap::iterator i = audio_entries_.begin(); |
361 i != audio_entries_.end(); ++i) { | 359 i != audio_entries_.end(); ++i) { |
362 CloseAndDeleteStream(i->second); | 360 CloseAndDeleteStream(i->second); |
363 } | 361 } |
364 } | 362 } |
365 | 363 |
366 void AudioInputRendererHost::OnDeviceStarted( | |
367 int session_id, const std::string& device_id) { | |
368 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
369 SessionEntryMap::iterator it = session_entries_.find(session_id); | |
370 if (it == session_entries_.end()) { | |
371 DLOG(WARNING) << "AudioInputRendererHost::OnDeviceStarted()" | |
372 " session does not exist."; | |
373 return; | |
374 } | |
375 | |
376 // Notify the renderer with the id of the opened device. | |
377 Send(new AudioInputMsg_NotifyDeviceStarted(it->second, device_id)); | |
378 } | |
379 | |
380 void AudioInputRendererHost::OnDeviceStopped(int session_id) { | |
381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
382 | |
383 SessionEntryMap::iterator it = session_entries_.find(session_id); | |
384 // Return if the stream has been closed. | |
385 if (it == session_entries_.end()) | |
386 return; | |
387 | |
388 int stream_id = it->second; | |
389 AudioEntry* entry = LookupById(stream_id); | |
390 | |
391 if (entry) { | |
392 // Device has been stopped, close the input stream. | |
393 CloseAndDeleteStream(entry); | |
394 // Notify the renderer that the state of the input stream has changed. | |
395 Send(new AudioInputMsg_NotifyStreamStateChanged( | |
396 stream_id, media::AudioInputIPCDelegate::kStopped)); | |
397 } | |
398 | |
399 // Delete the session entry. | |
400 session_entries_.erase(it); | |
401 } | |
402 | |
403 void AudioInputRendererHost::StopAndDeleteDevice(int session_id) { | |
404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
405 | |
406 media_stream_manager_->audio_input_device_manager()->Stop(session_id); | |
407 | |
408 // Delete the session entry. | |
409 session_entries_.erase(session_id); | |
410 } | |
411 | |
412 void AudioInputRendererHost::CloseAndDeleteStream(AudioEntry* entry) { | 364 void AudioInputRendererHost::CloseAndDeleteStream(AudioEntry* entry) { |
413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
414 | 366 |
415 if (!entry->pending_close) { | 367 if (!entry->pending_close) { |
416 entry->controller->Close(base::Bind(&AudioInputRendererHost::DeleteEntry, | 368 entry->controller->Close(base::Bind(&AudioInputRendererHost::DeleteEntry, |
417 this, entry)); | 369 this, entry)); |
418 entry->pending_close = true; | 370 entry->pending_close = true; |
419 } | 371 } |
420 } | 372 } |
421 | 373 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 // Iterate the map of entries. | 407 // Iterate the map of entries. |
456 // TODO(hclam): Implement a faster look up method. | 408 // TODO(hclam): Implement a faster look up method. |
457 for (AudioEntryMap::iterator i = audio_entries_.begin(); | 409 for (AudioEntryMap::iterator i = audio_entries_.begin(); |
458 i != audio_entries_.end(); ++i) { | 410 i != audio_entries_.end(); ++i) { |
459 if (controller == i->second->controller.get()) | 411 if (controller == i->second->controller.get()) |
460 return i->second; | 412 return i->second; |
461 } | 413 } |
462 return NULL; | 414 return NULL; |
463 } | 415 } |
464 | 416 |
465 int AudioInputRendererHost::LookupSessionById(int stream_id) { | |
466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
467 | |
468 for (SessionEntryMap::iterator it = session_entries_.begin(); | |
469 it != session_entries_.end(); ++it) { | |
470 if (stream_id == it->second) { | |
471 return it->first; | |
472 } | |
473 } | |
474 return 0; | |
475 } | |
476 | |
477 } // namespace content | 417 } // namespace content |
OLD | NEW |