Chromium Code Reviews| Index: content/browser/renderer_host/media/audio_output_authorization_handler.cc |
| diff --git a/content/browser/renderer_host/media/audio_output_authorization_handler.cc b/content/browser/renderer_host/media/audio_output_authorization_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d107347bbb2be9d8d2d6b54696b70d52e3749a16 |
| --- /dev/null |
| +++ b/content/browser/renderer_host/media/audio_output_authorization_handler.cc |
| @@ -0,0 +1,207 @@ |
| +// Copyright 2016 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_output_authorization_handler.h" |
| + |
| +#include "base/task_runner_util.h" |
| +#include "content/browser/bad_message.h" |
| +#include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/media_device_id.h" |
| +#include "media/base/limits.h" |
| + |
| +namespace { |
| + |
| +void MaybeFixAudioParameters(media::AudioParameters* params) { |
| + // If the number of output channels is greater than the maximum, use the |
| + // maximum allowed value. Hardware channels are ignored upstream, so it is |
| + // better to report a valid value if this is the only problem. |
| + if (params->channels() > media::limits::kMaxChannels) { |
| + DCHECK(params->channel_layout() == media::CHANNEL_LAYOUT_DISCRETE); |
| + params->set_channels_for_discrete(media::limits::kMaxChannels); |
| + } |
| + |
| + // If hardware parameters are still invalid, use dummy parameters with |
| + // fake audio path and let the client handle the error. |
| + if (!params->IsValid()) |
| + *params = media::AudioParameters::UnavailableDeviceParams(); |
| +} |
| + |
| +media::AudioParameters GetDeviceParametersOnDeviceThread( |
| + const std::string& unique_id) { |
| + media::AudioManager* audio_manager = media::AudioManager::Get(); |
| + DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| + |
| + return media::AudioDeviceDescription::IsDefaultDevice(unique_id) |
| + ? audio_manager->GetDefaultOutputStreamParameters() |
| + : audio_manager->GetOutputStreamParameters(unique_id); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace content { |
| + |
| +AudioOutputAuthorizationHandler::AudioOutputAuthorizationHandler( |
| + MediaStreamManager* media_stream_manager, |
| + int render_process_id, |
| + const std::string& salt) |
| + : media_stream_manager_(media_stream_manager), |
| + render_process_id_(render_process_id), |
| + salt_(salt), |
| + weak_factory_(this) { |
| + DCHECK(media_stream_manager_); |
| +} |
| + |
| +AudioOutputAuthorizationHandler::~AudioOutputAuthorizationHandler() = default; |
| + |
| +void AudioOutputAuthorizationHandler::RequestDeviceAuthorization( |
| + int render_frame_id, |
| + int session_id, |
| + const std::string& device_id, |
| + const url::Origin& security_origin, |
| + AuthorizationCompletedCallback cb) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + if (!IsValidDeviceId(device_id)) { |
| + bad_message::ReceivedBadMessage(render_process_id_, |
| + bad_message::AOAH_NONSENSE_DEVICE_ID); |
| + return; |
| + } |
| + |
| + // If |session_id| should be used for output device selection and such output |
| + // device is found, reuse the input device permissions. |
| + if (media::AudioDeviceDescription::UseSessionIdToSelectDevice(session_id, |
| + device_id)) { |
| + const StreamDeviceInfo* info = |
| + media_stream_manager_->audio_input_device_manager() |
| + ->GetOpenedDeviceInfoById(session_id); |
| + if (info) { |
| + media::AudioParameters output_params( |
| + media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| + static_cast<media::ChannelLayout>( |
| + info->device.matched_output.channel_layout), |
| + info->device.matched_output.sample_rate, 16, |
| + info->device.matched_output.frames_per_buffer); |
| + output_params.set_effects(info->device.matched_output.effects); |
| + MaybeFixAudioParameters(&output_params); |
| + |
| + cb.Run(media::OUTPUT_DEVICE_STATUS_OK, std::move(output_params), |
|
o1ka
2016/11/03 10:07:22
remove "move"?
Max Morin
2016/11/10 14:59:52
Done.
|
| + GetHMACForMediaDeviceID(salt_, security_origin, |
| + info->device.matched_output_device_id)); |
| + return; |
| + } |
| + } |
| + |
| + CheckOutputDeviceAccess(std::move(cb), render_frame_id, device_id, |
| + security_origin); |
| +} |
| + |
| +void AudioOutputAuthorizationHandler::CheckOutputDeviceAccess( |
| + AuthorizationCompletedCallback cb, |
| + int render_frame_id, |
| + const std::string& device_id, |
| + const url::Origin& security_origin) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + // Check security origin if nondefault device is requested. |
| + // Ignore check for default device, which is always authorized. |
| + if (!media::AudioDeviceDescription::IsDefaultDevice(device_id) && |
| + !MediaStreamManager::IsOriginAllowed(render_process_id_, |
| + security_origin)) { |
| + bad_message::ReceivedBadMessage(render_process_id_, |
| + bad_message::AOAH_UNAUTHORIZED_URL); |
| + return; |
| + } |
| + |
| + if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { |
| + AccessChecked(std::move(cb), device_id, security_origin, true); |
| + } else { |
| + // Check that MediaStream device permissions have been granted for |
| + // nondefault devices. |
| + permission_checker_.CheckPermission( |
| + MEDIA_DEVICE_TYPE_AUDIO_OUTPUT, render_process_id_, render_frame_id, |
| + security_origin, |
| + base::Bind(&AudioOutputAuthorizationHandler::AccessChecked, |
| + weak_factory_.GetWeakPtr(), std::move(cb), device_id, |
| + security_origin)); |
| + } |
| +} |
| + |
| +void AudioOutputAuthorizationHandler::AccessChecked( |
| + AuthorizationCompletedCallback cb, |
| + const std::string& device_id, |
| + const url::Origin& security_origin, |
| + bool have_access) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + if (!have_access) { |
| + cb.Run(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED, |
| + media::AudioParameters::UnavailableDeviceParams(), std::string()); |
| + return; |
| + } |
| + |
| + // For default device, read output parameters directly. Nondefault devices |
| + // require translation first. |
| + if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { |
| + base::PostTaskAndReplyWithResult( |
| + media::AudioManager::Get()->GetTaskRunner(), FROM_HERE, |
| + base::Bind(&GetDeviceParametersOnDeviceThread, |
| + media::AudioDeviceDescription::kDefaultDeviceId), |
| + base::Bind(&AudioOutputAuthorizationHandler::DeviceParametersReceived, |
| + weak_factory_.GetWeakPtr(), std::move(cb), true, |
| + media::AudioDeviceDescription::kDefaultDeviceId)); |
| + } else { |
| + MediaDevicesManager::BoolDeviceTypes devices_to_enumerate; |
| + devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true; |
| + media_stream_manager_->media_devices_manager()->EnumerateDevices( |
| + devices_to_enumerate, |
| + base::Bind(&AudioOutputAuthorizationHandler::TranslateDeviceID, |
| + weak_factory_.GetWeakPtr(), std::move(cb), device_id, |
| + security_origin)); |
| + } |
| +} |
| + |
| +void AudioOutputAuthorizationHandler::TranslateDeviceID( |
| + AuthorizationCompletedCallback cb, |
| + const std::string& device_id, |
| + const url::Origin& security_origin, |
| + const MediaDeviceEnumeration& enumeration) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + DCHECK(!media::AudioDeviceDescription::IsDefaultDevice(device_id)); |
| + for (const MediaDeviceInfo& device_info : |
| + enumeration[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT]) { |
| + if (content::DoesMediaDeviceIDMatchHMAC(salt_, security_origin, device_id, |
| + device_info.device_id)) { |
| + base::PostTaskAndReplyWithResult( |
| + media::AudioManager::Get()->GetTaskRunner(), FROM_HERE, |
| + base::Bind(&GetDeviceParametersOnDeviceThread, device_info.device_id), |
| + base::Bind(&AudioOutputAuthorizationHandler::DeviceParametersReceived, |
| + weak_factory_.GetWeakPtr(), std::move(cb), true, |
| + device_info.device_id)); |
|
o1ka
2016/11/03 10:07:22
+guidou@ - I'm a bit confused: shouldn't we return
Guido Urdaneta
2016/11/03 10:11:52
We should use device_id instead of device_info.dev
Max Morin
2016/11/10 14:59:52
ARH actually needs the translated id. I'll make th
Guido Urdaneta
2016/11/10 15:26:04
Actually you can provide the empty string, as in t
|
| + return; |
| + } |
| + } |
| + DeviceParametersReceived(std::move(cb), false, std::string(), |
| + media::AudioParameters::UnavailableDeviceParams()); |
| +} |
| + |
| +void AudioOutputAuthorizationHandler::DeviceParametersReceived( |
| + AuthorizationCompletedCallback cb, |
| + bool device_found, |
| + const std::string& unique_id, |
|
o1ka
2016/11/03 10:07:22
we need to name this appropriately (see my comment
Max Morin
2016/11/10 14:59:52
I changed this to translated_device_id.
Guido Urdaneta
2016/11/10 15:26:04
Note that in the original code, |unique_id| was us
|
| + const media::AudioParameters& output_params) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + if (!device_found) { |
| + cb.Run(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, |
| + media::AudioParameters::UnavailableDeviceParams(), ""); |
|
o1ka
2016/11/03 10:07:22
std::string()?
Max Morin
2016/11/10 14:59:52
Done.
|
| + return; |
| + } |
| + |
| + media::AudioParameters output_params_copy(output_params); |
| + MaybeFixAudioParameters(&output_params_copy); |
| + cb.Run(media::OUTPUT_DEVICE_STATUS_OK, output_params_copy, unique_id); |
| +} |
| + |
| +} // namespace content |