| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/renderer_host/media/audio_output_authorization_handler
.h" |
| 6 |
| 7 #include "base/task_runner_util.h" |
| 8 #include "content/browser/bad_message.h" |
| 9 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/media_device_id.h" |
| 12 #include "media/base/limits.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 media::AudioParameters TryToFixAudioParameters( |
| 17 const media::AudioParameters& params) { |
| 18 DCHECK(!params.IsValid()); |
| 19 media::AudioParameters params_copy(params); |
| 20 |
| 21 // If the number of output channels is greater than the maximum, use the |
| 22 // maximum allowed value. Hardware channels are ignored upstream, so it is |
| 23 // better to report a valid value if this is the only problem. |
| 24 if (params.channels() > media::limits::kMaxChannels) { |
| 25 DCHECK(params.channel_layout() == media::CHANNEL_LAYOUT_DISCRETE); |
| 26 params_copy.set_channels_for_discrete(media::limits::kMaxChannels); |
| 27 } |
| 28 |
| 29 // If hardware parameters are still invalid, use dummy parameters with |
| 30 // fake audio path and let the client handle the error. |
| 31 return params_copy.IsValid() |
| 32 ? params_copy |
| 33 : media::AudioParameters::UnavailableDeviceParams(); |
| 34 } |
| 35 |
| 36 media::AudioParameters GetDeviceParametersOnDeviceThread( |
| 37 const std::string& unique_id) { |
| 38 media::AudioManager* audio_manager = media::AudioManager::Get(); |
| 39 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 40 |
| 41 return media::AudioDeviceDescription::IsDefaultDevice(unique_id) |
| 42 ? audio_manager->GetDefaultOutputStreamParameters() |
| 43 : audio_manager->GetOutputStreamParameters(unique_id); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 namespace content { |
| 49 |
| 50 AudioOutputAuthorizationHandler::AudioOutputAuthorizationHandler( |
| 51 MediaStreamManager* media_stream_manager, |
| 52 int render_process_id, |
| 53 const std::string& salt) |
| 54 : media_stream_manager_(media_stream_manager), |
| 55 permission_checker_(new MediaDevicesPermissionChecker()), |
| 56 render_process_id_(render_process_id), |
| 57 salt_(salt), |
| 58 weak_factory_(this) { |
| 59 DCHECK(media_stream_manager_); |
| 60 } |
| 61 |
| 62 void AudioOutputAuthorizationHandler::OverridePermissionsForTesting( |
| 63 bool override_value) { |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 65 permission_checker_.reset(new MediaDevicesPermissionChecker(override_value)); |
| 66 } |
| 67 |
| 68 AudioOutputAuthorizationHandler::~AudioOutputAuthorizationHandler() { |
| 69 // |weak_factory| is not thread safe. Make sure it's destructed on the |
| 70 // right thread. |
| 71 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 72 } |
| 73 |
| 74 void AudioOutputAuthorizationHandler::RequestDeviceAuthorization( |
| 75 int render_frame_id, |
| 76 int session_id, |
| 77 const std::string& device_id, |
| 78 const url::Origin& security_origin, |
| 79 AuthorizationCompletedCallback cb) const { |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 81 |
| 82 if (!IsValidDeviceId(device_id)) { |
| 83 bad_message::ReceivedBadMessage(render_process_id_, |
| 84 bad_message::AOAH_NONSENSE_DEVICE_ID); |
| 85 return; |
| 86 } |
| 87 |
| 88 // If |session_id| should be used for output device selection and such an |
| 89 // output device is found, reuse the input device permissions. |
| 90 if (media::AudioDeviceDescription::UseSessionIdToSelectDevice(session_id, |
| 91 device_id)) { |
| 92 const StreamDeviceInfo* info = |
| 93 media_stream_manager_->audio_input_device_manager() |
| 94 ->GetOpenedDeviceInfoById(session_id); |
| 95 if (info && !info->device.matched_output_device_id.empty()) { |
| 96 media::AudioParameters output_params( |
| 97 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 98 static_cast<media::ChannelLayout>( |
| 99 info->device.matched_output.channel_layout), |
| 100 info->device.matched_output.sample_rate, 16, |
| 101 info->device.matched_output.frames_per_buffer); |
| 102 output_params.set_effects(info->device.matched_output.effects); |
| 103 |
| 104 // We already have the unhashed id and the parameters, so jump to |
| 105 // DeviceParametersReceived. |
| 106 DeviceParametersReceived(std::move(cb), true, |
| 107 info->device.matched_output_device_id, |
| 108 output_params); |
| 109 return; |
| 110 } |
| 111 // Otherwise, the default device is used. |
| 112 } |
| 113 |
| 114 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { |
| 115 // Default device doesn't need authorization. |
| 116 AccessChecked(std::move(cb), device_id, security_origin, true); |
| 117 return; |
| 118 } |
| 119 |
| 120 // Check security origin if nondefault device is requested. |
| 121 if (!MediaStreamManager::IsOriginAllowed(render_process_id_, |
| 122 security_origin)) { |
| 123 bad_message::ReceivedBadMessage(render_process_id_, |
| 124 bad_message::AOAH_UNAUTHORIZED_URL); |
| 125 return; |
| 126 } |
| 127 |
| 128 // Check that MediaStream device permissions have been granted for |
| 129 // nondefault devices. |
| 130 permission_checker_->CheckPermission( |
| 131 MEDIA_DEVICE_TYPE_AUDIO_OUTPUT, render_process_id_, render_frame_id, |
| 132 security_origin, |
| 133 base::Bind(&AudioOutputAuthorizationHandler::AccessChecked, |
| 134 weak_factory_.GetWeakPtr(), std::move(cb), device_id, |
| 135 security_origin)); |
| 136 } |
| 137 |
| 138 void AudioOutputAuthorizationHandler::AccessChecked( |
| 139 AuthorizationCompletedCallback cb, |
| 140 const std::string& device_id, |
| 141 const url::Origin& security_origin, |
| 142 bool has_access) const { |
| 143 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 144 |
| 145 if (!has_access) { |
| 146 cb.Run(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED, false, |
| 147 media::AudioParameters::UnavailableDeviceParams(), std::string()); |
| 148 return; |
| 149 } |
| 150 |
| 151 // For default device, read output parameters directly. Nondefault |
| 152 // devices require translation first. |
| 153 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { |
| 154 GetDeviceParameters(std::move(cb), |
| 155 media::AudioDeviceDescription::kDefaultDeviceId); |
| 156 } else { |
| 157 MediaDevicesManager::BoolDeviceTypes devices_to_enumerate; |
| 158 devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true; |
| 159 media_stream_manager_->media_devices_manager()->EnumerateDevices( |
| 160 devices_to_enumerate, |
| 161 base::Bind(&AudioOutputAuthorizationHandler::TranslateDeviceID, |
| 162 weak_factory_.GetWeakPtr(), std::move(cb), device_id, |
| 163 security_origin)); |
| 164 } |
| 165 } |
| 166 |
| 167 void AudioOutputAuthorizationHandler::TranslateDeviceID( |
| 168 AuthorizationCompletedCallback cb, |
| 169 const std::string& device_id, |
| 170 const url::Origin& security_origin, |
| 171 const MediaDeviceEnumeration& enumeration) const { |
| 172 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 173 DCHECK(!media::AudioDeviceDescription::IsDefaultDevice(device_id)); |
| 174 for (const MediaDeviceInfo& device_info : |
| 175 enumeration[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT]) { |
| 176 if (content::DoesMediaDeviceIDMatchHMAC(salt_, security_origin, device_id, |
| 177 device_info.device_id)) { |
| 178 GetDeviceParameters(std::move(cb), device_info.device_id); |
| 179 return; |
| 180 } |
| 181 } |
| 182 cb.Run(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, false, |
| 183 media::AudioParameters::UnavailableDeviceParams(), std::string()); |
| 184 } |
| 185 |
| 186 void AudioOutputAuthorizationHandler::GetDeviceParameters( |
| 187 AuthorizationCompletedCallback cb, |
| 188 const std::string& raw_device_id) const { |
| 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 190 DCHECK(!raw_device_id.empty()); |
| 191 base::PostTaskAndReplyWithResult( |
| 192 media::AudioManager::Get()->GetTaskRunner(), FROM_HERE, |
| 193 base::Bind(&GetDeviceParametersOnDeviceThread, raw_device_id), |
| 194 base::Bind(&AudioOutputAuthorizationHandler::DeviceParametersReceived, |
| 195 weak_factory_.GetWeakPtr(), std::move(cb), false, |
| 196 raw_device_id)); |
| 197 } |
| 198 |
| 199 void AudioOutputAuthorizationHandler::DeviceParametersReceived( |
| 200 AuthorizationCompletedCallback cb, |
| 201 bool should_send_id, |
| 202 const std::string& raw_device_id, |
| 203 const media::AudioParameters& output_params) const { |
| 204 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 205 DCHECK(!raw_device_id.empty()); |
| 206 |
| 207 cb.Run(media::OUTPUT_DEVICE_STATUS_OK, should_send_id, |
| 208 output_params.IsValid() ? output_params |
| 209 : TryToFixAudioParameters(output_params), |
| 210 raw_device_id); |
| 211 } |
| 212 |
| 213 } // namespace content |
| OLD | NEW |