Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1530)

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host.cc

Issue 1323403005: Allow AudioOutputDevice objects to be initialized with a specific hardware output device and store … (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_renderer_host.h" 5 #include "content/browser/renderer_host/media/audio_renderer_host.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/shared_memory.h" 10 #include "base/memory/shared_memory.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 bool is_playing, 54 bool is_playing,
55 int render_view_id) { 55 int render_view_id) {
56 DCHECK_CURRENTLY_ON(BrowserThread::IO); 56 DCHECK_CURRENTLY_ON(BrowserThread::IO);
57 if (render_view_id == MSG_ROUTING_NONE || !ResourceDispatcherHostImpl::Get()) 57 if (render_view_id == MSG_ROUTING_NONE || !ResourceDispatcherHostImpl::Get())
58 return; 58 return;
59 59
60 ResourceDispatcherHostImpl::Get()->OnAudioRenderHostStreamStateChanged( 60 ResourceDispatcherHostImpl::Get()->OnAudioRenderHostStreamStateChanged(
61 render_process_id, render_view_id, is_playing); 61 render_process_id, render_view_id, is_playing);
62 } 62 }
63 63
64 media::AudioParameters DummyParams() {
65 return media::AudioParameters(media::AudioParameters::AUDIO_PCM_LINEAR,
66 media::CHANNEL_LAYOUT_STEREO,
67 media::limits::kMinSampleRate, 1, 1);
68 }
69
70 std::pair<int, std::pair<bool, std::string>> MakeAuthorizationData(
71 int stream_id,
72 bool authorized,
73 const std::string& device_unique_id) {
74 return std::make_pair(stream_id,
75 std::make_pair(authorized, device_unique_id));
76 }
77
64 } // namespace 78 } // namespace
65 79
66 class AudioRendererHost::AudioEntry 80 class AudioRendererHost::AudioEntry
67 : public media::AudioOutputController::EventHandler { 81 : public media::AudioOutputController::EventHandler {
68 public: 82 public:
69 AudioEntry(AudioRendererHost* host, 83 AudioEntry(AudioRendererHost* host,
70 int stream_id, 84 int stream_id,
71 int render_frame_id, 85 int render_frame_id,
72 const media::AudioParameters& params, 86 const media::AudioParameters& params,
73 const std::string& output_device_id, 87 const std::string& output_device_id,
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 BrowserThread::IO, FROM_HERE, 191 BrowserThread::IO, FROM_HERE,
178 base::Bind(&AudioRendererHost::DoGetOutputControllers, this), callback); 192 base::Bind(&AudioRendererHost::DoGetOutputControllers, this), callback);
179 } 193 }
180 194
181 void AudioRendererHost::OnChannelClosing() { 195 void AudioRendererHost::OnChannelClosing() {
182 // Since the IPC sender is gone, close all requested audio streams. 196 // Since the IPC sender is gone, close all requested audio streams.
183 while (!audio_entries_.empty()) { 197 while (!audio_entries_.empty()) {
184 // Note: OnCloseStream() removes the entries from audio_entries_. 198 // Note: OnCloseStream() removes the entries from audio_entries_.
185 OnCloseStream(audio_entries_.begin()->first); 199 OnCloseStream(audio_entries_.begin()->first);
186 } 200 }
201
202 // Remove any authorizations for streams that were not yet created
203 authorizations_.clear();
187 } 204 }
188 205
189 void AudioRendererHost::OnDestruct() const { 206 void AudioRendererHost::OnDestruct() const {
190 BrowserThread::DeleteOnIOThread::Destruct(this); 207 BrowserThread::DeleteOnIOThread::Destruct(this);
191 } 208 }
192 209
193 void AudioRendererHost::AudioEntry::OnCreated() { 210 void AudioRendererHost::AudioEntry::OnCreated() {
194 BrowserThread::PostTask( 211 BrowserThread::PostTask(
195 BrowserThread::IO, 212 BrowserThread::IO,
196 FROM_HERE, 213 FROM_HERE,
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 } 322 }
306 323
307 return controllers; 324 return controllers;
308 } 325 }
309 326
310 /////////////////////////////////////////////////////////////////////////////// 327 ///////////////////////////////////////////////////////////////////////////////
311 // IPC Messages handler 328 // IPC Messages handler
312 bool AudioRendererHost::OnMessageReceived(const IPC::Message& message) { 329 bool AudioRendererHost::OnMessageReceived(const IPC::Message& message) {
313 bool handled = true; 330 bool handled = true;
314 IPC_BEGIN_MESSAGE_MAP(AudioRendererHost, message) 331 IPC_BEGIN_MESSAGE_MAP(AudioRendererHost, message)
332 IPC_MESSAGE_HANDLER(AudioHostMsg_RequestDeviceAuthorization,
333 OnRequestDeviceAuthorization)
315 IPC_MESSAGE_HANDLER(AudioHostMsg_CreateStream, OnCreateStream) 334 IPC_MESSAGE_HANDLER(AudioHostMsg_CreateStream, OnCreateStream)
316 IPC_MESSAGE_HANDLER(AudioHostMsg_PlayStream, OnPlayStream) 335 IPC_MESSAGE_HANDLER(AudioHostMsg_PlayStream, OnPlayStream)
317 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream) 336 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream)
318 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) 337 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream)
319 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) 338 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume)
320 IPC_MESSAGE_HANDLER(AudioHostMsg_SwitchOutputDevice, OnSwitchOutputDevice) 339 IPC_MESSAGE_HANDLER(AudioHostMsg_SwitchOutputDevice, OnSwitchOutputDevice)
321 IPC_MESSAGE_UNHANDLED(handled = false) 340 IPC_MESSAGE_UNHANDLED(handled = false)
322 IPC_END_MESSAGE_MAP() 341 IPC_END_MESSAGE_MAP()
323 342
324 return handled; 343 return handled;
325 } 344 }
326 345
346 void AudioRendererHost::OnRequestDeviceAuthorization(
347 int stream_id,
348 int render_frame_id,
349 int session_id,
350 const std::string& device_id,
351 const GURL& security_origin) {
352 DCHECK_CURRENTLY_ON(BrowserThread::IO);
353 DVLOG(1) << "AudioRendererHost@" << this << "::OnRequestDeviceAuthorization"
354 << "(stream_id=" << stream_id
355 << ", render_frame_id=" << render_frame_id
356 << ", session_id=" << session_id << ", device_id=" << device_id
357 << ", security_origin=" << security_origin << ")";
358
359 if (LookupById(stream_id) || IsAuthorizationStarted(stream_id))
360 return;
361
362 // If attempting to use the output device associated to an opened input
363 // device and the output device is found, reuse the input device
364 // permissions.
365 if (session_id != 0) {
366 const StreamDeviceInfo* info =
367 media_stream_manager_->audio_input_device_manager()
368 ->GetOpenedDeviceInfoById(session_id);
369 if (info) {
370 media::AudioParameters output_params(
371 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
372 static_cast<media::ChannelLayout>(
373 info->device.matched_output.channel_layout),
374 info->device.matched_output.sample_rate, 16,
375 info->device.matched_output.frames_per_buffer);
376 output_params.set_effects(info->device.matched_output.effects);
377 authorizations_.insert(MakeAuthorizationData(
378 stream_id, true, info->device.matched_output_device_id));
379 Send(new AudioMsg_NotifyDeviceAuthorized(stream_id, true, output_params));
380 return;
381 }
382 }
383
384 authorizations_.insert(
385 MakeAuthorizationData(stream_id, false, std::string()));
386 CheckOutputDeviceAccess(
387 render_frame_id, device_id, security_origin,
388 base::Bind(&AudioRendererHost::RequestDeviceAuthorizationAccessChecked,
389 this, stream_id, device_id, security_origin));
390 }
391
392 void AudioRendererHost::RequestDeviceAuthorizationAccessChecked(
DaleCurtis 2015/09/12 01:17:19 OnDeviceAuthorized?
Guido Urdaneta 2015/09/14 11:35:48 Done.
393 int stream_id,
394 const std::string& device_id,
395 const GURL& security_origin,
396 bool have_access) {
397 DCHECK_CURRENTLY_ON(BrowserThread::IO);
398 const auto& auth_data = authorizations_.find(stream_id);
399
400 // A close request was received while access check was in progress.
401 if (auth_data == authorizations_.end())
402 return;
403
404 if (!have_access) {
405 authorizations_.erase(auth_data);
406 Send(new AudioMsg_NotifyDeviceAuthorized(stream_id, false, DummyParams()));
407 return;
408 }
409
410 TranslateDeviceID(
411 device_id, security_origin,
412 base::Bind(&AudioRendererHost::RequestDeviceAuthorizationTranslated, this,
413 stream_id));
414 }
415
416 void AudioRendererHost::RequestDeviceAuthorizationTranslated(
DaleCurtis 2015/09/12 01:17:19 OnDeviceIDTranslated?
Guido Urdaneta 2015/09/14 11:35:48 Done.
417 int stream_id,
418 bool device_found,
419 const AudioOutputDeviceInfo& device_info) {
420 DCHECK_CURRENTLY_ON(BrowserThread::IO);
421 const auto& auth_data = authorizations_.find(stream_id);
422
423 // A close request was received while translation was in progress
424 if (auth_data == authorizations_.end())
425 return;
426
427 if (!device_found) {
428 authorizations_.erase(auth_data);
429 Send(new AudioMsg_NotifyDeviceAuthorized(stream_id, false, DummyParams()));
430 return;
431 }
432
433 auth_data->second.first = true;
434 auth_data->second.second = device_info.unique_id;
435 Send(new AudioMsg_NotifyDeviceAuthorized(stream_id, true,
436 device_info.output_params));
437 }
438
327 void AudioRendererHost::OnCreateStream(int stream_id, 439 void AudioRendererHost::OnCreateStream(int stream_id,
328 int render_frame_id, 440 int render_frame_id,
329 int session_id,
330 const media::AudioParameters& params) { 441 const media::AudioParameters& params) {
331 DCHECK_CURRENTLY_ON(BrowserThread::IO); 442 DCHECK_CURRENTLY_ON(BrowserThread::IO);
443 DVLOG(1) << "AudioRendererHost@" << this << "::OnCreateStream"
444 << "(stream_id=" << stream_id << ")";
332 445
333 DVLOG(1) << "AudioRendererHost@" << this 446 const auto& auth_data = authorizations_.find(stream_id);
334 << "::OnCreateStream(stream_id=" << stream_id 447
335 << ", render_frame_id=" << render_frame_id 448 // If no previous authorization requested, assume default device
336 << ", session_id=" << session_id << ")"; 449 if (auth_data == authorizations_.end()) {
337 DCHECK_GT(render_frame_id, 0); 450 DoCreateStream(stream_id, render_frame_id, params, std::string());
451 return;
452 }
453
454 // If authorization is not finished, ignore this request, as it is invalid.
DaleCurtis 2015/09/12 01:17:19 Can we CHECK this?
Guido Urdaneta 2015/09/14 11:35:48 Done.
455 if (!auth_data->second.first)
456 return;
457
458 DoCreateStream(stream_id, render_frame_id, params, auth_data->second.second);
459 authorizations_.erase(auth_data);
460 }
461
462 void AudioRendererHost::DoCreateStream(int stream_id,
463 int render_frame_id,
464 const media::AudioParameters& params,
465 const std::string& device_unique_id) {
466 DCHECK_CURRENTLY_ON(BrowserThread::IO);
338 467
339 // media::AudioParameters is validated in the deserializer. 468 // media::AudioParameters is validated in the deserializer.
340 if (LookupById(stream_id) != NULL) { 469 if (LookupById(stream_id) != NULL) {
341 SendErrorMessage(stream_id); 470 SendErrorMessage(stream_id);
342 return; 471 return;
343 } 472 }
344 473
345 // Initialize the |output_device_id| to an empty string which indicates that
346 // the default device should be used. If a StreamDeviceInfo instance was found
347 // though, then we use the matched output device.
348 std::string output_device_id;
349 const StreamDeviceInfo* info = media_stream_manager_->
350 audio_input_device_manager()->GetOpenedDeviceInfoById(session_id);
351 if (info)
352 output_device_id = info->device.matched_output_device_id;
353
354 // Create the shared memory and share with the renderer process. 474 // Create the shared memory and share with the renderer process.
355 uint32 shared_memory_size = AudioBus::CalculateMemorySize(params); 475 uint32 shared_memory_size = AudioBus::CalculateMemorySize(params);
356 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); 476 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
357 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) { 477 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) {
358 SendErrorMessage(stream_id); 478 SendErrorMessage(stream_id);
359 return; 479 return;
360 } 480 }
361 481
362 scoped_ptr<AudioSyncReader> reader( 482 scoped_ptr<AudioSyncReader> reader(
363 new AudioSyncReader(shared_memory.get(), params)); 483 new AudioSyncReader(shared_memory.get(), params));
364 if (!reader->Init()) { 484 if (!reader->Init()) {
365 SendErrorMessage(stream_id); 485 SendErrorMessage(stream_id);
366 return; 486 return;
367 } 487 }
368 488
369 MediaObserver* const media_observer = 489 MediaObserver* const media_observer =
370 GetContentClient()->browser()->GetMediaObserver(); 490 GetContentClient()->browser()->GetMediaObserver();
371 if (media_observer) 491 if (media_observer)
372 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id); 492 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id);
373 493
374 scoped_ptr<AudioEntry> entry(new AudioEntry(this, 494 scoped_ptr<AudioEntry> entry(
375 stream_id, 495 new AudioEntry(this, stream_id, render_frame_id, params, device_unique_id,
376 render_frame_id, 496 shared_memory.Pass(), reader.Pass()));
377 params,
378 output_device_id,
379 shared_memory.Pass(),
380 reader.Pass()));
381 if (mirroring_manager_) { 497 if (mirroring_manager_) {
382 mirroring_manager_->AddDiverter( 498 mirroring_manager_->AddDiverter(
383 render_process_id_, entry->render_frame_id(), entry->controller()); 499 render_process_id_, entry->render_frame_id(), entry->controller());
384 } 500 }
385 audio_entries_.insert(std::make_pair(stream_id, entry.release())); 501 audio_entries_.insert(std::make_pair(stream_id, entry.release()));
386 audio_log_->OnCreated(stream_id, params, output_device_id); 502
503 audio_log_->OnCreated(stream_id, params, device_unique_id);
387 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( 504 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry(
388 stream_id, render_process_id_, render_frame_id, audio_log_.get()); 505 stream_id, render_process_id_, render_frame_id, audio_log_.get());
389 } 506 }
390 507
391 void AudioRendererHost::OnPlayStream(int stream_id) { 508 void AudioRendererHost::OnPlayStream(int stream_id) {
392 DCHECK_CURRENTLY_ON(BrowserThread::IO); 509 DCHECK_CURRENTLY_ON(BrowserThread::IO);
393 510
394 AudioEntry* entry = LookupById(stream_id); 511 AudioEntry* entry = LookupById(stream_id);
395 if (!entry) { 512 if (!entry) {
396 SendErrorMessage(stream_id); 513 SendErrorMessage(stream_id);
(...skipping 29 matching lines...) Expand all
426 // Make sure the volume is valid. 543 // Make sure the volume is valid.
427 if (volume < 0 || volume > 1.0) 544 if (volume < 0 || volume > 1.0)
428 return; 545 return;
429 entry->controller()->SetVolume(volume); 546 entry->controller()->SetVolume(volume);
430 audio_log_->OnSetVolume(stream_id, volume); 547 audio_log_->OnSetVolume(stream_id, volume);
431 } 548 }
432 549
433 void AudioRendererHost::OnSwitchOutputDevice(int stream_id, 550 void AudioRendererHost::OnSwitchOutputDevice(int stream_id,
434 int render_frame_id, 551 int render_frame_id,
435 const std::string& device_id, 552 const std::string& device_id,
436 const GURL& security_origin, 553 const GURL& security_origin) {
437 int request_id) {
438 DCHECK_CURRENTLY_ON(BrowserThread::IO); 554 DCHECK_CURRENTLY_ON(BrowserThread::IO);
439 DVLOG(1) << "AudioRendererHost@" << this 555 DVLOG(1) << "AudioRendererHost@" << this
440 << "::OnSwitchOutputDevice(stream_id=" << stream_id 556 << "::OnSwitchOutputDevice(stream_id=" << stream_id
441 << ", render_frame_id=" << render_frame_id 557 << ", render_frame_id=" << render_frame_id
442 << ", device_id=" << device_id 558 << ", device_id=" << device_id
443 << ", security_origin=" << security_origin 559 << ", security_origin=" << security_origin << ")";
444 << ", request_id=" << request_id << ")"; 560 CheckOutputDeviceAccess(
445 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( 561 render_frame_id, device_id, security_origin,
446 render_process_id_, security_origin)) { 562 base::Bind(&AudioRendererHost::SwitchOutputDeviceAccessChecked, this,
447 content::bad_message::ReceivedBadMessage(this, 563 stream_id, device_id, security_origin));
448 bad_message::ARH_UNAUTHORIZED_URL); 564 }
565
566 void AudioRendererHost::SwitchOutputDeviceAccessChecked(
567 int stream_id,
568 const std::string& device_id,
569 const GURL& security_origin,
570 bool have_access) {
571 DCHECK_CURRENTLY_ON(BrowserThread::IO);
572 if (!have_access) {
573 Send(new AudioMsg_NotifyOutputDeviceSwitched(
574 stream_id, media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED,
575 DummyParams()));
449 return; 576 return;
450 } 577 }
451 578
452 if (device_id.empty()) { 579 // TODO(guidou): Ensure that output parameters of the new device match the
453 DVLOG(1) << __FUNCTION__ << ": default output device requested. " 580 // output parameters of the current device to avoid shared-memory sizing
454 << "No permissions check or device translation/validation needed."; 581 // issues. http://crbug.com/526657
455 DoSwitchOutputDevice(stream_id, device_id, request_id); 582 TranslateDeviceID(device_id, security_origin,
456 } else { 583 base::Bind(&AudioRendererHost::SwitchOutputDeviceTranslated,
457 // Check that MediaStream device permissions have been granted, 584 this, stream_id));
458 // hence the use of a MediaStreamUIProxy.
459 scoped_ptr<MediaStreamUIProxy> ui_proxy = MediaStreamUIProxy::Create();
460
461 // Use MEDIA_DEVICE_AUDIO_CAPTURE instead of MEDIA_DEVICE_AUDIO_OUTPUT
462 // because MediaStreamUIProxy::CheckAccess does not currently support
463 // MEDIA_DEVICE_AUDIO_OUTPUT.
464 // TODO(guidou): Change to MEDIA_DEVICE_AUDIO_OUTPUT when support becomes
465 // available. http://crbug.com/498675
466 ui_proxy->CheckAccess(
467 security_origin, MEDIA_DEVICE_AUDIO_CAPTURE,
468 render_process_id_, render_frame_id,
469 base::Bind(&AudioRendererHost::OutputDeviceAccessChecked, this,
470 base::Passed(&ui_proxy), stream_id, device_id,
471 security_origin, render_frame_id, request_id));
472 }
473 } 585 }
474 586
475 void AudioRendererHost::OutputDeviceAccessChecked( 587 void AudioRendererHost::SwitchOutputDeviceTranslated(
476 scoped_ptr<MediaStreamUIProxy> ui_proxy,
477 int stream_id, 588 int stream_id,
478 const std::string& device_id, 589 bool device_found,
479 const GURL& security_origin, 590 const AudioOutputDeviceInfo& device_info) {
480 int render_frame_id,
481 int request_id,
482 bool have_access) {
483 DCHECK_CURRENTLY_ON(BrowserThread::IO); 591 DCHECK_CURRENTLY_ON(BrowserThread::IO);
484 DVLOG(1) << __FUNCTION__; 592 if (!device_found) {
485 if (!have_access) {
486 DVLOG(0) << __FUNCTION__
487 << ": Have no access to media devices. Not switching device.";
488 Send(new AudioMsg_NotifyOutputDeviceSwitched( 593 Send(new AudioMsg_NotifyOutputDeviceSwitched(
489 stream_id, request_id, 594 stream_id, media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_FOUND,
490 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED)); 595 DummyParams()));
491 return; 596 return;
492 } 597 }
493 598
494 scoped_refptr<base::SingleThreadTaskRunner> audio_worker_runner =
495 AudioManager::Get()->GetWorkerTaskRunner();
496 audio_worker_runner->PostTask(
497 FROM_HERE,
498 base::Bind(&AudioRendererHost::StartTranslateOutputDeviceName, this,
499 stream_id, device_id, security_origin, request_id));
500 }
501
502 void AudioRendererHost::StartTranslateOutputDeviceName(
503 int stream_id,
504 const std::string& device_id,
505 const GURL& security_origin,
506 int request_id) {
507 DCHECK(AudioManager::Get()->GetWorkerTaskRunner()->BelongsToCurrentThread());
508 DCHECK(!device_id.empty());
509 DVLOG(1) << __FUNCTION__;
510
511 media::AudioDeviceNames* device_names(new media::AudioDeviceNames);
512 AudioManager::Get()->GetAudioOutputDeviceNames(device_names);
513
514 BrowserThread::PostTask(
515 BrowserThread::IO, FROM_HERE,
516 base::Bind(&AudioRendererHost::FinishTranslateOutputDeviceName, this,
517 stream_id, device_id, security_origin, request_id,
518 base::Owned(device_names)));
519 }
520
521 void AudioRendererHost::FinishTranslateOutputDeviceName(
522 int stream_id,
523 const std::string& device_id,
524 const GURL& security_origin,
525 int request_id,
526 media::AudioDeviceNames* device_names) {
527 DCHECK_CURRENTLY_ON(BrowserThread::IO);
528 DCHECK(!device_id.empty());
529 DVLOG(1) << __FUNCTION__;
530
531 std::string raw_device_id;
532 // Process the enumeration here because |salt_callback_| can run
533 // only on the IO thread
534 for (const auto& device_name : *device_names) {
535 const std::string candidate_device_id = content::GetHMACForMediaDeviceID(
536 salt_callback_, security_origin, device_name.unique_id);
537 if (candidate_device_id == device_id) {
538 DVLOG(1) << "Requested device " << device_name.unique_id << " - "
539 << device_name.device_name;
540 raw_device_id = device_name.unique_id;
541 }
542 }
543
544 if (raw_device_id.empty()) {
545 DVLOG(1) << "Requested device " << device_id << " could not be found.";
546 Send(new AudioMsg_NotifyOutputDeviceSwitched(
547 stream_id, request_id,
548 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_FOUND));
549 return;
550 }
551
552 DoSwitchOutputDevice(stream_id, raw_device_id, request_id);
553 }
554
555 void AudioRendererHost::DoSwitchOutputDevice(int stream_id,
556 const std::string& raw_device_id,
557 int request_id) {
558 DCHECK_CURRENTLY_ON(BrowserThread::IO);
559 DVLOG(1) << __FUNCTION__ << "(" << stream_id << ", " << raw_device_id << ", "
560 << request_id << ")";
561 AudioEntry* entry = LookupById(stream_id); 599 AudioEntry* entry = LookupById(stream_id);
562 if (!entry) { 600 if (!entry) {
563 Send(new AudioMsg_NotifyOutputDeviceSwitched( 601 Send(new AudioMsg_NotifyOutputDeviceSwitched(
564 stream_id, request_id, 602 stream_id, media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_INTERNAL,
565 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE)); 603 DummyParams()));
566 return; 604 return;
567 } 605 }
568 606
569 entry->controller()->SwitchOutputDevice( 607 entry->controller()->SwitchOutputDevice(
570 raw_device_id, base::Bind(&AudioRendererHost::DoOutputDeviceSwitched, 608 device_info.unique_id,
571 this, stream_id, request_id)); 609 base::Bind(&AudioRendererHost::OutputDeviceSwitched, this, stream_id,
572 audio_log_->OnSwitchOutputDevice(entry->stream_id(), raw_device_id); 610 device_info.output_params));
611 audio_log_->OnSwitchOutputDevice(entry->stream_id(), device_info.unique_id);
573 } 612 }
574 613
575 void AudioRendererHost::DoOutputDeviceSwitched(int stream_id, int request_id) { 614 void AudioRendererHost::OutputDeviceSwitched(
DaleCurtis 2015/09/12 01:17:19 OnDeviceSwitched?
Guido Urdaneta 2015/09/14 11:35:48 Done.
615 int stream_id,
616 const media::AudioParameters& output_params) {
576 DCHECK_CURRENTLY_ON(BrowserThread::IO); 617 DCHECK_CURRENTLY_ON(BrowserThread::IO);
577 DVLOG(1) << __FUNCTION__ << "(" << stream_id << ", " << request_id << ")";
578 Send(new AudioMsg_NotifyOutputDeviceSwitched( 618 Send(new AudioMsg_NotifyOutputDeviceSwitched(
579 stream_id, request_id, media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS)); 619 stream_id, media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS, output_params));
580 } 620 }
581 621
582 void AudioRendererHost::SendErrorMessage(int stream_id) { 622 void AudioRendererHost::SendErrorMessage(int stream_id) {
583 Send(new AudioMsg_NotifyStreamStateChanged( 623 Send(new AudioMsg_NotifyStreamStateChanged(
584 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR)); 624 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR));
585 } 625 }
586 626
587 void AudioRendererHost::OnCloseStream(int stream_id) { 627 void AudioRendererHost::OnCloseStream(int stream_id) {
588 DCHECK_CURRENTLY_ON(BrowserThread::IO); 628 DCHECK_CURRENTLY_ON(BrowserThread::IO);
629 authorizations_.erase(stream_id);
589 630
590 // Prevent oustanding callbacks from attempting to close/delete the same 631 // Prevent oustanding callbacks from attempting to close/delete the same
591 // AudioEntry twice. 632 // AudioEntry twice.
592 AudioEntryMap::iterator i = audio_entries_.find(stream_id); 633 AudioEntryMap::iterator i = audio_entries_.find(stream_id);
593 if (i == audio_entries_.end()) 634 if (i == audio_entries_.end())
594 return; 635 return;
595 scoped_ptr<AudioEntry> entry(i->second); 636 scoped_ptr<AudioEntry> entry(i->second);
596 audio_entries_.erase(i); 637 audio_entries_.erase(i);
597 638
598 media::AudioOutputController* const controller = entry->controller(); 639 media::AudioOutputController* const controller = entry->controller();
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 for (AudioEntryMap::const_iterator it = audio_entries_.begin(); 717 for (AudioEntryMap::const_iterator it = audio_entries_.begin();
677 it != audio_entries_.end(); 718 it != audio_entries_.end();
678 ++it) { 719 ++it) {
679 AudioEntry* entry = it->second; 720 AudioEntry* entry = it->second;
680 if (entry->render_frame_id() == render_frame_id && entry->playing()) 721 if (entry->render_frame_id() == render_frame_id && entry->playing())
681 return true; 722 return true;
682 } 723 }
683 return false; 724 return false;
684 } 725 }
685 726
727 void AudioRendererHost::CheckOutputDeviceAccess(
728 int render_frame_id,
729 const std::string& device_id,
730 const GURL& security_origin,
731 const OutputDeviceAccessCB& callback) {
732 DCHECK_CURRENTLY_ON(BrowserThread::IO);
733
734 // Skip origin check in requests for the default device with an empty
735 // security origin.
736 bool skip_origin_check = security_origin.is_empty() && device_id.empty();
737 if (!skip_origin_check &&
738 !ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
739 render_process_id_, security_origin)) {
740 content::bad_message::ReceivedBadMessage(this,
741 bad_message::ARH_UNAUTHORIZED_URL);
742 return;
743 }
744
745 if (device_id.empty()) {
746 callback.Run(true);
747 } else {
748 // Check that MediaStream device permissions have been granted,
749 // hence the use of a MediaStreamUIProxy.
750 scoped_ptr<MediaStreamUIProxy> ui_proxy = MediaStreamUIProxy::Create();
751
752 // Use MEDIA_DEVICE_AUDIO_CAPTURE instead of MEDIA_DEVICE_AUDIO_OUTPUT
753 // because MediaStreamUIProxy::CheckAccess does not currently support
754 // MEDIA_DEVICE_AUDIO_OUTPUT.
755 // TODO(guidou): Change to MEDIA_DEVICE_AUDIO_OUTPUT when support becomes
756 // available. http://crbug.com/498675
757 ui_proxy->CheckAccess(security_origin, MEDIA_DEVICE_AUDIO_CAPTURE,
758 render_process_id_, render_frame_id,
759 base::Bind(&AudioRendererHost::AccessChecked, this,
760 base::Passed(&ui_proxy), callback));
761 }
762 }
763
764 void AudioRendererHost::AccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy,
765 const OutputDeviceAccessCB& callback,
766 bool have_access) {
767 DCHECK_CURRENTLY_ON(BrowserThread::IO);
768 callback.Run(have_access);
769 }
770
771 void AudioRendererHost::TranslateDeviceID(const std::string& device_id,
772 const GURL& security_origin,
773 const OutputDeviceInfoCB& callback) {
774 DCHECK_CURRENTLY_ON(BrowserThread::IO);
775 AudioOutputDeviceEnumerator* enumerator =
776 media_stream_manager_->audio_output_device_enumerator();
777 enumerator->Enumerate(base::Bind(&AudioRendererHost::FinishTranslateDeviceID,
778 this, device_id, security_origin, callback));
779 }
780
781 void AudioRendererHost::FinishTranslateDeviceID(
782 const std::string& device_id,
783 const GURL& security_origin,
784 const OutputDeviceInfoCB& callback,
785 const AudioOutputDeviceEnumeration& device_infos) {
786 DCHECK_CURRENTLY_ON(BrowserThread::IO);
787 bool use_default_device = device_id.empty();
788 for (const AudioOutputDeviceInfo& device_info : device_infos) {
789 if (use_default_device) {
790 if (device_info.unique_id == media::AudioManagerBase::kDefaultDeviceId) {
791 callback.Run(true, device_info);
792 return;
793 }
794 } else {
795 const std::string candidate_device_id = content::GetHMACForMediaDeviceID(
796 salt_callback_, security_origin, device_info.unique_id);
797 if (candidate_device_id == device_id) {
798 callback.Run(true, device_info);
799 return;
800 }
801 }
802 }
803 DCHECK(!use_default_device); // Default device must always be found
804 AudioOutputDeviceInfo device_info = {std::string(), std::string(),
805 DummyParams()};
806 callback.Run(false, device_info);
807 }
808
809 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) {
810 DCHECK_CURRENTLY_ON(BrowserThread::IO);
811 const auto& i = authorizations_.find(stream_id);
812 return i != authorizations_.end();
813 }
814
686 } // namespace content 815 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698