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 MaybeFixAudioParameters( | |
|
Guido Urdaneta
2016/11/16 12:40:18
If you are calling this function only when paramet
Max Morin
2016/11/16 14:12:19
Well, it's not guaranteed that it will fix the aud
| |
| 17 const media::AudioParameters& params) { | |
| 18 media::AudioParameters params_copy(params); | |
| 19 | |
| 20 // If the number of output channels is greater than the maximum, use the | |
| 21 // maximum allowed value. Hardware channels are ignored upstream, so it is | |
| 22 // better to report a valid value if this is the only problem. | |
| 23 if (params.channels() > media::limits::kMaxChannels) { | |
| 24 DCHECK(params.channel_layout() == media::CHANNEL_LAYOUT_DISCRETE); | |
| 25 params_copy.set_channels_for_discrete(media::limits::kMaxChannels); | |
| 26 } | |
| 27 | |
| 28 // If hardware parameters are still invalid, use dummy parameters with | |
| 29 // fake audio path and let the client handle the error. | |
| 30 return params_copy.IsValid() | |
| 31 ? params_copy | |
| 32 : media::AudioParameters::UnavailableDeviceParams(); | |
| 33 } | |
| 34 | |
| 35 media::AudioParameters GetDeviceParametersOnDeviceThread( | |
| 36 const std::string& unique_id) { | |
| 37 media::AudioManager* audio_manager = media::AudioManager::Get(); | |
| 38 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); | |
| 39 | |
| 40 return media::AudioDeviceDescription::IsDefaultDevice(unique_id) | |
| 41 ? audio_manager->GetDefaultOutputStreamParameters() | |
| 42 : audio_manager->GetOutputStreamParameters(unique_id); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 namespace content { | |
| 48 | |
| 49 AudioOutputAuthorizationHandler::AudioOutputAuthorizationHandler( | |
| 50 MediaStreamManager* media_stream_manager, | |
| 51 int render_process_id, | |
| 52 const std::string& salt) | |
| 53 : media_stream_manager_(media_stream_manager), | |
| 54 permission_checker_(new MediaDevicesPermissionChecker()), | |
| 55 render_process_id_(render_process_id), | |
| 56 salt_(salt), | |
| 57 weak_factory_(this) { | |
| 58 DCHECK(media_stream_manager_); | |
| 59 } | |
| 60 | |
| 61 void AudioOutputAuthorizationHandler::OverridePermissionsForTesting( | |
| 62 bool override_value) { | |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 64 DCHECK(permission_checker_); | |
| 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 | |
|
Guido Urdaneta
2016/11/16 12:40:18
weak_factory->|weak_factory_|. Apply to other comm
Max Morin
2016/11/16 14:12:19
Done.
| |
| 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 } else { | |
| 111 LOG(WARNING) << "Session id used for device selection but no device info" | |
|
Guido Urdaneta
2016/11/16 12:40:18
I don't think this is a warning situation. It woul
Max Morin
2016/11/16 14:12:19
I made it into a comment instead.
| |
| 112 << "found. Default device used instead."; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { | |
| 117 AccessChecked(std::move(cb), device_id, security_origin, true); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 // Check security origin if nondefault device is requested. | |
| 122 // Ignore check for default device, which is always authorized. | |
|
Guido Urdaneta
2016/11/16 12:40:18
Remove the second part of the comment. Maybe move
Max Morin
2016/11/16 14:12:19
Done.
| |
| 123 if (!MediaStreamManager::IsOriginAllowed(render_process_id_, | |
| 124 security_origin)) { | |
| 125 bad_message::ReceivedBadMessage(render_process_id_, | |
| 126 bad_message::AOAH_UNAUTHORIZED_URL); | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 // Check that MediaStream device permissions have been granted for | |
| 131 // nondefault devices. | |
| 132 permission_checker_->CheckPermission( | |
| 133 MEDIA_DEVICE_TYPE_AUDIO_OUTPUT, render_process_id_, render_frame_id, | |
| 134 security_origin, | |
| 135 base::Bind(&AudioOutputAuthorizationHandler::AccessChecked, | |
| 136 weak_factory_.GetWeakPtr(), std::move(cb), device_id, | |
| 137 security_origin)); | |
| 138 } | |
| 139 | |
| 140 void AudioOutputAuthorizationHandler::AccessChecked( | |
| 141 AuthorizationCompletedCallback cb, | |
| 142 const std::string& device_id, | |
| 143 const url::Origin& security_origin, | |
| 144 bool has_access) const { | |
| 145 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 146 | |
| 147 if (!has_access) { | |
| 148 cb.Run(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED, false, | |
| 149 media::AudioParameters::UnavailableDeviceParams(), std::string()); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 // For default device, read output parameters directly. Nondefault | |
| 154 // devices require translation first. | |
| 155 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { | |
| 156 GetDeviceParameters(std::move(cb), | |
| 157 media::AudioDeviceDescription::kDefaultDeviceId); | |
| 158 } else { | |
| 159 MediaDevicesManager::BoolDeviceTypes devices_to_enumerate; | |
| 160 devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true; | |
| 161 media_stream_manager_->media_devices_manager()->EnumerateDevices( | |
| 162 devices_to_enumerate, | |
| 163 base::Bind(&AudioOutputAuthorizationHandler::TranslateDeviceID, | |
| 164 weak_factory_.GetWeakPtr(), std::move(cb), device_id, | |
| 165 security_origin)); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 void AudioOutputAuthorizationHandler::TranslateDeviceID( | |
| 170 AuthorizationCompletedCallback cb, | |
| 171 const std::string& device_id, | |
| 172 const url::Origin& security_origin, | |
| 173 const MediaDeviceEnumeration& enumeration) const { | |
| 174 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 175 DCHECK(!media::AudioDeviceDescription::IsDefaultDevice(device_id)); | |
| 176 for (const MediaDeviceInfo& device_info : | |
| 177 enumeration[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT]) { | |
| 178 if (content::DoesMediaDeviceIDMatchHMAC(salt_, security_origin, device_id, | |
| 179 device_info.device_id)) { | |
| 180 GetDeviceParameters(std::move(cb), device_info.device_id); | |
| 181 return; | |
| 182 } | |
| 183 } | |
| 184 cb.Run(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, false, | |
| 185 media::AudioParameters::UnavailableDeviceParams(), std::string()); | |
| 186 } | |
| 187 | |
| 188 void AudioOutputAuthorizationHandler::GetDeviceParameters( | |
| 189 AuthorizationCompletedCallback cb, | |
| 190 const std::string& translated_device_id) const { | |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 192 DCHECK(!translated_device_id.empty()); | |
| 193 base::PostTaskAndReplyWithResult( | |
| 194 media::AudioManager::Get()->GetTaskRunner(), FROM_HERE, | |
| 195 base::Bind(&GetDeviceParametersOnDeviceThread, translated_device_id), | |
| 196 base::Bind(&AudioOutputAuthorizationHandler::DeviceParametersReceived, | |
| 197 weak_factory_.GetWeakPtr(), std::move(cb), false, | |
| 198 translated_device_id)); | |
| 199 } | |
| 200 | |
| 201 void AudioOutputAuthorizationHandler::DeviceParametersReceived( | |
| 202 AuthorizationCompletedCallback cb, | |
| 203 bool should_send_id, | |
| 204 const std::string& translated_device_id, | |
|
Guido Urdaneta
2016/11/16 12:40:19
translate_device_id -> raw_device_id
Max Morin
2016/11/16 14:12:19
Done.
| |
| 205 const media::AudioParameters& output_params) const { | |
| 206 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 207 DCHECK(!translated_device_id.empty()); | |
| 208 | |
| 209 cb.Run(media::OUTPUT_DEVICE_STATUS_OK, should_send_id, | |
| 210 output_params.IsValid() ? output_params | |
| 211 : MaybeFixAudioParameters(output_params), | |
| 212 translated_device_id); | |
| 213 } | |
| 214 | |
| 215 } // namespace content | |
| OLD | NEW |