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

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: bbudge's comment 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))
palmer 2015/09/17 20:31:57 It would be good to add a check here that validate
Guido Urdaneta 2015/09/18 09:23:07 Done. Note that I made the |device_id| check retur
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::OnDeviceAuthorized, this, stream_id,
389 device_id, security_origin));
390 }
391
392 void AudioRendererHost::OnDeviceAuthorized(int stream_id,
393 const std::string& device_id,
394 const GURL& security_origin,
395 bool have_access) {
396 DCHECK_CURRENTLY_ON(BrowserThread::IO);
397 const auto& auth_data = authorizations_.find(stream_id);
398
399 // A close request was received while access check was in progress.
400 if (auth_data == authorizations_.end())
401 return;
402
403 if (!have_access) {
404 authorizations_.erase(auth_data);
405 Send(new AudioMsg_NotifyDeviceAuthorized(stream_id, false, DummyParams()));
406 return;
407 }
408
409 TranslateDeviceID(
410 device_id, security_origin,
411 base::Bind(&AudioRendererHost::OnDeviceIDTranslated, this, stream_id));
412 }
413
414 void AudioRendererHost::OnDeviceIDTranslated(
415 int stream_id,
416 bool device_found,
417 const AudioOutputDeviceInfo& device_info) {
418 DCHECK_CURRENTLY_ON(BrowserThread::IO);
419 const auto& auth_data = authorizations_.find(stream_id);
420
421 // A close request was received while translation was in progress
422 if (auth_data == authorizations_.end())
423 return;
424
425 if (!device_found) {
426 authorizations_.erase(auth_data);
427 Send(new AudioMsg_NotifyDeviceAuthorized(stream_id, false, DummyParams()));
428 return;
429 }
430
431 auth_data->second.first = true;
432 auth_data->second.second = device_info.unique_id;
433 Send(new AudioMsg_NotifyDeviceAuthorized(stream_id, true,
434 device_info.output_params));
435 }
436
327 void AudioRendererHost::OnCreateStream(int stream_id, 437 void AudioRendererHost::OnCreateStream(int stream_id,
328 int render_frame_id, 438 int render_frame_id,
329 int session_id,
330 const media::AudioParameters& params) { 439 const media::AudioParameters& params) {
331 DCHECK_CURRENTLY_ON(BrowserThread::IO); 440 DCHECK_CURRENTLY_ON(BrowserThread::IO);
441 DVLOG(1) << "AudioRendererHost@" << this << "::OnCreateStream"
442 << "(stream_id=" << stream_id << ")";
332 443
333 DVLOG(1) << "AudioRendererHost@" << this 444 const auto& auth_data = authorizations_.find(stream_id);
334 << "::OnCreateStream(stream_id=" << stream_id 445
335 << ", render_frame_id=" << render_frame_id 446 // If no previous authorization requested, assume default device
336 << ", session_id=" << session_id << ")"; 447 if (auth_data == authorizations_.end()) {
337 DCHECK_GT(render_frame_id, 0); 448 DoCreateStream(stream_id, render_frame_id, params, std::string());
449 return;
450 }
451
452 CHECK(auth_data->second.first);
453 DoCreateStream(stream_id, render_frame_id, params, auth_data->second.second);
454 authorizations_.erase(auth_data);
455 }
456
457 void AudioRendererHost::DoCreateStream(int stream_id,
458 int render_frame_id,
459 const media::AudioParameters& params,
460 const std::string& device_unique_id) {
461 DCHECK_CURRENTLY_ON(BrowserThread::IO);
338 462
339 // media::AudioParameters is validated in the deserializer. 463 // media::AudioParameters is validated in the deserializer.
340 if (LookupById(stream_id) != NULL) { 464 if (LookupById(stream_id) != NULL) {
341 SendErrorMessage(stream_id); 465 SendErrorMessage(stream_id);
342 return; 466 return;
343 } 467 }
344 468
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. 469 // Create the shared memory and share with the renderer process.
355 uint32 shared_memory_size = AudioBus::CalculateMemorySize(params); 470 uint32 shared_memory_size = AudioBus::CalculateMemorySize(params);
356 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); 471 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
357 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) { 472 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) {
358 SendErrorMessage(stream_id); 473 SendErrorMessage(stream_id);
359 return; 474 return;
360 } 475 }
361 476
362 scoped_ptr<AudioSyncReader> reader( 477 scoped_ptr<AudioSyncReader> reader(
363 new AudioSyncReader(shared_memory.get(), params)); 478 new AudioSyncReader(shared_memory.get(), params));
364 if (!reader->Init()) { 479 if (!reader->Init()) {
365 SendErrorMessage(stream_id); 480 SendErrorMessage(stream_id);
366 return; 481 return;
367 } 482 }
368 483
369 MediaObserver* const media_observer = 484 MediaObserver* const media_observer =
370 GetContentClient()->browser()->GetMediaObserver(); 485 GetContentClient()->browser()->GetMediaObserver();
371 if (media_observer) 486 if (media_observer)
372 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id); 487 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id);
373 488
374 scoped_ptr<AudioEntry> entry(new AudioEntry(this, 489 scoped_ptr<AudioEntry> entry(
375 stream_id, 490 new AudioEntry(this, stream_id, render_frame_id, params, device_unique_id,
376 render_frame_id, 491 shared_memory.Pass(), reader.Pass()));
377 params,
378 output_device_id,
379 shared_memory.Pass(),
380 reader.Pass()));
381 if (mirroring_manager_) { 492 if (mirroring_manager_) {
382 mirroring_manager_->AddDiverter( 493 mirroring_manager_->AddDiverter(
383 render_process_id_, entry->render_frame_id(), entry->controller()); 494 render_process_id_, entry->render_frame_id(), entry->controller());
384 } 495 }
385 audio_entries_.insert(std::make_pair(stream_id, entry.release())); 496 audio_entries_.insert(std::make_pair(stream_id, entry.release()));
386 audio_log_->OnCreated(stream_id, params, output_device_id); 497
498 audio_log_->OnCreated(stream_id, params, device_unique_id);
387 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( 499 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry(
388 stream_id, render_process_id_, render_frame_id, audio_log_.get()); 500 stream_id, render_process_id_, render_frame_id, audio_log_.get());
389 } 501 }
390 502
391 void AudioRendererHost::OnPlayStream(int stream_id) { 503 void AudioRendererHost::OnPlayStream(int stream_id) {
392 DCHECK_CURRENTLY_ON(BrowserThread::IO); 504 DCHECK_CURRENTLY_ON(BrowserThread::IO);
393 505
394 AudioEntry* entry = LookupById(stream_id); 506 AudioEntry* entry = LookupById(stream_id);
395 if (!entry) { 507 if (!entry) {
396 SendErrorMessage(stream_id); 508 SendErrorMessage(stream_id);
(...skipping 29 matching lines...) Expand all
426 // Make sure the volume is valid. 538 // Make sure the volume is valid.
427 if (volume < 0 || volume > 1.0) 539 if (volume < 0 || volume > 1.0)
428 return; 540 return;
429 entry->controller()->SetVolume(volume); 541 entry->controller()->SetVolume(volume);
430 audio_log_->OnSetVolume(stream_id, volume); 542 audio_log_->OnSetVolume(stream_id, volume);
431 } 543 }
432 544
433 void AudioRendererHost::OnSwitchOutputDevice(int stream_id, 545 void AudioRendererHost::OnSwitchOutputDevice(int stream_id,
434 int render_frame_id, 546 int render_frame_id,
435 const std::string& device_id, 547 const std::string& device_id,
436 const GURL& security_origin, 548 const GURL& security_origin) {
437 int request_id) {
438 DCHECK_CURRENTLY_ON(BrowserThread::IO); 549 DCHECK_CURRENTLY_ON(BrowserThread::IO);
439 DVLOG(1) << "AudioRendererHost@" << this 550 DVLOG(1) << "AudioRendererHost@" << this
440 << "::OnSwitchOutputDevice(stream_id=" << stream_id 551 << "::OnSwitchOutputDevice(stream_id=" << stream_id
441 << ", render_frame_id=" << render_frame_id 552 << ", render_frame_id=" << render_frame_id
442 << ", device_id=" << device_id 553 << ", device_id=" << device_id
443 << ", security_origin=" << security_origin 554 << ", security_origin=" << security_origin << ")";
444 << ", request_id=" << request_id << ")"; 555 CheckOutputDeviceAccess(
445 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( 556 render_frame_id, device_id, security_origin,
446 render_process_id_, security_origin)) { 557 base::Bind(&AudioRendererHost::OnSwitchDeviceAuthorized, this, stream_id,
447 content::bad_message::ReceivedBadMessage(this, 558 device_id, security_origin));
448 bad_message::ARH_UNAUTHORIZED_URL); 559 }
560
561 void AudioRendererHost::OnSwitchDeviceAuthorized(int stream_id,
562 const std::string& device_id,
563 const GURL& security_origin,
564 bool have_access) {
565 DCHECK_CURRENTLY_ON(BrowserThread::IO);
566 if (!have_access) {
567 Send(new AudioMsg_NotifyOutputDeviceSwitched(
568 stream_id, media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED));
449 return; 569 return;
450 } 570 }
451 571
452 if (device_id.empty()) { 572 // TODO(guidou): Ensure that output parameters of the new device match the
453 DVLOG(1) << __FUNCTION__ << ": default output device requested. " 573 // output parameters of the current device to avoid shared-memory sizing
454 << "No permissions check or device translation/validation needed."; 574 // issues. http://crbug.com/526657
455 DoSwitchOutputDevice(stream_id, device_id, request_id); 575 TranslateDeviceID(device_id, security_origin,
456 } else { 576 base::Bind(&AudioRendererHost::OnSwitchDeviceIDTranslated,
457 // Check that MediaStream device permissions have been granted, 577 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 } 578 }
474 579
475 void AudioRendererHost::OutputDeviceAccessChecked( 580 void AudioRendererHost::OnSwitchDeviceIDTranslated(
476 scoped_ptr<MediaStreamUIProxy> ui_proxy,
477 int stream_id, 581 int stream_id,
478 const std::string& device_id, 582 bool device_found,
479 const GURL& security_origin, 583 const AudioOutputDeviceInfo& device_info) {
480 int render_frame_id,
481 int request_id,
482 bool have_access) {
483 DCHECK_CURRENTLY_ON(BrowserThread::IO); 584 DCHECK_CURRENTLY_ON(BrowserThread::IO);
484 DVLOG(1) << __FUNCTION__; 585 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( 586 Send(new AudioMsg_NotifyOutputDeviceSwitched(
489 stream_id, request_id, 587 stream_id, media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_FOUND));
490 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED));
491 return; 588 return;
492 } 589 }
493 590
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); 591 AudioEntry* entry = LookupById(stream_id);
562 if (!entry) { 592 if (!entry) {
563 Send(new AudioMsg_NotifyOutputDeviceSwitched( 593 Send(new AudioMsg_NotifyOutputDeviceSwitched(
564 stream_id, request_id, 594 stream_id, media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_INTERNAL));
565 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE));
566 return; 595 return;
567 } 596 }
568 597
569 entry->controller()->SwitchOutputDevice( 598 entry->controller()->SwitchOutputDevice(
570 raw_device_id, base::Bind(&AudioRendererHost::DoOutputDeviceSwitched, 599 device_info.unique_id,
571 this, stream_id, request_id)); 600 base::Bind(&AudioRendererHost::OnDeviceSwitched, this, stream_id));
572 audio_log_->OnSwitchOutputDevice(entry->stream_id(), raw_device_id); 601 audio_log_->OnSwitchOutputDevice(entry->stream_id(), device_info.unique_id);
573 } 602 }
574 603
575 void AudioRendererHost::DoOutputDeviceSwitched(int stream_id, int request_id) { 604 void AudioRendererHost::OnDeviceSwitched(int stream_id) {
576 DCHECK_CURRENTLY_ON(BrowserThread::IO); 605 DCHECK_CURRENTLY_ON(BrowserThread::IO);
577 DVLOG(1) << __FUNCTION__ << "(" << stream_id << ", " << request_id << ")";
578 Send(new AudioMsg_NotifyOutputDeviceSwitched( 606 Send(new AudioMsg_NotifyOutputDeviceSwitched(
579 stream_id, request_id, media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS)); 607 stream_id, media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS));
580 } 608 }
581 609
582 void AudioRendererHost::SendErrorMessage(int stream_id) { 610 void AudioRendererHost::SendErrorMessage(int stream_id) {
583 Send(new AudioMsg_NotifyStreamStateChanged( 611 Send(new AudioMsg_NotifyStreamStateChanged(
584 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR)); 612 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR));
585 } 613 }
586 614
587 void AudioRendererHost::OnCloseStream(int stream_id) { 615 void AudioRendererHost::OnCloseStream(int stream_id) {
588 DCHECK_CURRENTLY_ON(BrowserThread::IO); 616 DCHECK_CURRENTLY_ON(BrowserThread::IO);
617 authorizations_.erase(stream_id);
589 618
590 // Prevent oustanding callbacks from attempting to close/delete the same 619 // Prevent oustanding callbacks from attempting to close/delete the same
591 // AudioEntry twice. 620 // AudioEntry twice.
592 AudioEntryMap::iterator i = audio_entries_.find(stream_id); 621 AudioEntryMap::iterator i = audio_entries_.find(stream_id);
593 if (i == audio_entries_.end()) 622 if (i == audio_entries_.end())
594 return; 623 return;
595 scoped_ptr<AudioEntry> entry(i->second); 624 scoped_ptr<AudioEntry> entry(i->second);
596 audio_entries_.erase(i); 625 audio_entries_.erase(i);
597 626
598 media::AudioOutputController* const controller = entry->controller(); 627 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(); 705 for (AudioEntryMap::const_iterator it = audio_entries_.begin();
677 it != audio_entries_.end(); 706 it != audio_entries_.end();
678 ++it) { 707 ++it) {
679 AudioEntry* entry = it->second; 708 AudioEntry* entry = it->second;
680 if (entry->render_frame_id() == render_frame_id && entry->playing()) 709 if (entry->render_frame_id() == render_frame_id && entry->playing())
681 return true; 710 return true;
682 } 711 }
683 return false; 712 return false;
684 } 713 }
685 714
715 void AudioRendererHost::CheckOutputDeviceAccess(
716 int render_frame_id,
717 const std::string& device_id,
718 const GURL& security_origin,
719 const OutputDeviceAccessCB& callback) {
720 DCHECK_CURRENTLY_ON(BrowserThread::IO);
721
722 // Skip origin check in requests for the default device with an empty
723 // security origin.
724 bool skip_origin_check = security_origin.is_empty() && device_id.empty();
725 if (!skip_origin_check &&
726 !ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
727 render_process_id_, security_origin)) {
728 content::bad_message::ReceivedBadMessage(this,
729 bad_message::ARH_UNAUTHORIZED_URL);
730 return;
731 }
732
733 if (device_id.empty()) {
734 callback.Run(true);
735 } else {
736 // Check that MediaStream device permissions have been granted,
737 // hence the use of a MediaStreamUIProxy.
738 scoped_ptr<MediaStreamUIProxy> ui_proxy = MediaStreamUIProxy::Create();
739
740 // Use MEDIA_DEVICE_AUDIO_CAPTURE instead of MEDIA_DEVICE_AUDIO_OUTPUT
741 // because MediaStreamUIProxy::CheckAccess does not currently support
742 // MEDIA_DEVICE_AUDIO_OUTPUT.
743 // TODO(guidou): Change to MEDIA_DEVICE_AUDIO_OUTPUT when support becomes
744 // available. http://crbug.com/498675
745 ui_proxy->CheckAccess(security_origin, MEDIA_DEVICE_AUDIO_CAPTURE,
746 render_process_id_, render_frame_id,
747 base::Bind(&AudioRendererHost::AccessChecked, this,
748 base::Passed(&ui_proxy), callback));
749 }
750 }
751
752 void AudioRendererHost::AccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy,
753 const OutputDeviceAccessCB& callback,
754 bool have_access) {
755 DCHECK_CURRENTLY_ON(BrowserThread::IO);
756 callback.Run(have_access);
757 }
758
759 void AudioRendererHost::TranslateDeviceID(const std::string& device_id,
760 const GURL& security_origin,
761 const OutputDeviceInfoCB& callback) {
762 DCHECK_CURRENTLY_ON(BrowserThread::IO);
763 AudioOutputDeviceEnumerator* enumerator =
764 media_stream_manager_->audio_output_device_enumerator();
765 enumerator->Enumerate(base::Bind(&AudioRendererHost::FinishTranslateDeviceID,
766 this, device_id, security_origin, callback));
767 }
768
769 void AudioRendererHost::FinishTranslateDeviceID(
770 const std::string& device_id,
771 const GURL& security_origin,
772 const OutputDeviceInfoCB& callback,
773 const AudioOutputDeviceEnumeration& device_infos) {
774 DCHECK_CURRENTLY_ON(BrowserThread::IO);
775 bool use_default_device = device_id.empty();
776 for (const AudioOutputDeviceInfo& device_info : device_infos) {
777 if (use_default_device) {
778 if (device_info.unique_id == media::AudioManagerBase::kDefaultDeviceId) {
779 callback.Run(true, device_info);
780 return;
781 }
782 } else {
783 const std::string candidate_device_id = content::GetHMACForMediaDeviceID(
784 salt_callback_, security_origin, device_info.unique_id);
785 if (candidate_device_id == device_id) {
786 callback.Run(true, device_info);
787 return;
788 }
789 }
790 }
791 DCHECK(!use_default_device); // Default device must always be found
792 AudioOutputDeviceInfo device_info = {std::string(), std::string(),
793 DummyParams()};
794 callback.Run(false, device_info);
795 }
796
797 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) {
798 DCHECK_CURRENTLY_ON(BrowserThread::IO);
799 const auto& i = authorizations_.find(stream_id);
800 return i != authorizations_.end();
801 }
802
686 } // namespace content 803 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698