Chromium Code Reviews| 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 DCHECK(permission_checker_); | |
|
o1ka
2016/11/16 15:11:28
What is the point for this check?
Max Morin
2016/11/16 15:54:36
Not really necessary. Removed.
| |
| 66 permission_checker_.reset(new MediaDevicesPermissionChecker(override_value)); | |
| 67 } | |
| 68 | |
| 69 AudioOutputAuthorizationHandler::~AudioOutputAuthorizationHandler() { | |
| 70 // |weak_factory| is not thread safe. Make sure it's destructed on the | |
| 71 // right thread. | |
| 72 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 73 } | |
| 74 | |
| 75 void AudioOutputAuthorizationHandler::RequestDeviceAuthorization( | |
| 76 int render_frame_id, | |
| 77 int session_id, | |
| 78 const std::string& device_id, | |
| 79 const url::Origin& security_origin, | |
| 80 AuthorizationCompletedCallback cb) const { | |
| 81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 82 | |
| 83 if (!IsValidDeviceId(device_id)) { | |
| 84 bad_message::ReceivedBadMessage(render_process_id_, | |
| 85 bad_message::AOAH_NONSENSE_DEVICE_ID); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 // If |session_id| should be used for output device selection and such an | |
| 90 // output device is found, reuse the input device permissions. | |
| 91 if (media::AudioDeviceDescription::UseSessionIdToSelectDevice(session_id, | |
| 92 device_id)) { | |
| 93 const StreamDeviceInfo* info = | |
| 94 media_stream_manager_->audio_input_device_manager() | |
| 95 ->GetOpenedDeviceInfoById(session_id); | |
| 96 if (info && !info->device.matched_output_device_id.empty()) { | |
| 97 media::AudioParameters output_params( | |
| 98 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 99 static_cast<media::ChannelLayout>( | |
| 100 info->device.matched_output.channel_layout), | |
| 101 info->device.matched_output.sample_rate, 16, | |
| 102 info->device.matched_output.frames_per_buffer); | |
| 103 output_params.set_effects(info->device.matched_output.effects); | |
| 104 | |
| 105 // We already have the unhashed id and the parameters, so jump to | |
| 106 // DeviceParametersReceived. | |
| 107 DeviceParametersReceived(std::move(cb), true, | |
| 108 info->device.matched_output_device_id, | |
| 109 output_params); | |
| 110 return; | |
| 111 } | |
| 112 // Otherwise, the default device is used. | |
| 113 } | |
| 114 | |
| 115 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { | |
| 116 // Default device doesn't need authorization. | |
| 117 AccessChecked(std::move(cb), device_id, security_origin, true); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 // Check security origin if nondefault device is requested. | |
| 122 if (!MediaStreamManager::IsOriginAllowed(render_process_id_, | |
| 123 security_origin)) { | |
| 124 bad_message::ReceivedBadMessage(render_process_id_, | |
| 125 bad_message::AOAH_UNAUTHORIZED_URL); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 // Check that MediaStream device permissions have been granted for | |
| 130 // nondefault devices. | |
| 131 permission_checker_->CheckPermission( | |
| 132 MEDIA_DEVICE_TYPE_AUDIO_OUTPUT, render_process_id_, render_frame_id, | |
| 133 security_origin, | |
| 134 base::Bind(&AudioOutputAuthorizationHandler::AccessChecked, | |
| 135 weak_factory_.GetWeakPtr(), std::move(cb), device_id, | |
| 136 security_origin)); | |
| 137 } | |
| 138 | |
| 139 void AudioOutputAuthorizationHandler::AccessChecked( | |
| 140 AuthorizationCompletedCallback cb, | |
| 141 const std::string& device_id, | |
| 142 const url::Origin& security_origin, | |
| 143 bool has_access) const { | |
| 144 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 145 | |
| 146 if (!has_access) { | |
| 147 cb.Run(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED, false, | |
| 148 media::AudioParameters::UnavailableDeviceParams(), std::string()); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 // For default device, read output parameters directly. Nondefault | |
| 153 // devices require translation first. | |
| 154 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { | |
| 155 GetDeviceParameters(std::move(cb), | |
| 156 media::AudioDeviceDescription::kDefaultDeviceId); | |
| 157 } else { | |
| 158 MediaDevicesManager::BoolDeviceTypes devices_to_enumerate; | |
| 159 devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true; | |
| 160 media_stream_manager_->media_devices_manager()->EnumerateDevices( | |
| 161 devices_to_enumerate, | |
| 162 base::Bind(&AudioOutputAuthorizationHandler::TranslateDeviceID, | |
| 163 weak_factory_.GetWeakPtr(), std::move(cb), device_id, | |
| 164 security_origin)); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 void AudioOutputAuthorizationHandler::TranslateDeviceID( | |
| 169 AuthorizationCompletedCallback cb, | |
| 170 const std::string& device_id, | |
| 171 const url::Origin& security_origin, | |
| 172 const MediaDeviceEnumeration& enumeration) const { | |
| 173 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 174 DCHECK(!media::AudioDeviceDescription::IsDefaultDevice(device_id)); | |
| 175 for (const MediaDeviceInfo& device_info : | |
| 176 enumeration[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT]) { | |
| 177 if (content::DoesMediaDeviceIDMatchHMAC(salt_, security_origin, device_id, | |
| 178 device_info.device_id)) { | |
| 179 GetDeviceParameters(std::move(cb), device_info.device_id); | |
| 180 return; | |
| 181 } | |
| 182 } | |
| 183 cb.Run(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, false, | |
| 184 media::AudioParameters::UnavailableDeviceParams(), std::string()); | |
| 185 } | |
| 186 | |
| 187 void AudioOutputAuthorizationHandler::GetDeviceParameters( | |
| 188 AuthorizationCompletedCallback cb, | |
| 189 const std::string& raw_device_id) const { | |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 191 DCHECK(!raw_device_id.empty()); | |
| 192 base::PostTaskAndReplyWithResult( | |
| 193 media::AudioManager::Get()->GetTaskRunner(), FROM_HERE, | |
| 194 base::Bind(&GetDeviceParametersOnDeviceThread, raw_device_id), | |
| 195 base::Bind(&AudioOutputAuthorizationHandler::DeviceParametersReceived, | |
| 196 weak_factory_.GetWeakPtr(), std::move(cb), false, | |
| 197 raw_device_id)); | |
| 198 } | |
| 199 | |
| 200 void AudioOutputAuthorizationHandler::DeviceParametersReceived( | |
| 201 AuthorizationCompletedCallback cb, | |
| 202 bool should_send_id, | |
| 203 const std::string& raw_device_id, | |
| 204 const media::AudioParameters& output_params) const { | |
| 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 206 DCHECK(!raw_device_id.empty()); | |
| 207 | |
| 208 cb.Run(media::OUTPUT_DEVICE_STATUS_OK, should_send_id, | |
| 209 output_params.IsValid() ? output_params | |
| 210 : TryToFixAudioParameters(output_params), | |
| 211 raw_device_id); | |
| 212 } | |
| 213 | |
| 214 } // namespace content | |
| OLD | NEW |