1 // Copyright 2018 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.
5 #include "content/browser/renderer_host/media/render_frame_audio_input_stream_factory.h"
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/task/post_task.h"
17 #include "base/trace_event/trace_event.h"
18 #include "base/unguessable_token.h"
19 #include "content/browser/media/audio_stream_broker.h"
20 #include "content/browser/media/capture/desktop_capture_device_uma_types.h"
21 #include "content/browser/media/forwarding_audio_stream_factory.h"
22 #include "content/browser/media/media_devices_permission_checker.h"
23 #include "content/browser/media/media_devices_util.h"
24 #include "content/browser/renderer_host/media/audio_input_device_manager.h"
25 #include "content/browser/renderer_host/media/media_devices_manager.h"
26 #include "content/browser/renderer_host/media/media_stream_manager.h"
27 #include "content/public/browser/browser_task_traits.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/media_device_id.h"
30 #include "content/public/browser/render_frame_host.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/web_contents_media_capture_id.h"
33 #include "content/public/common/media_stream_request.h"
34 #include "media/audio/audio_device_description.h"
35 #include "media/base/audio_parameters.h"
36 #include "mojo/public/cpp/bindings/binding.h"
37 #include "services/audio/public/mojom/audio_processing.mojom.h"
38 #include "url/origin.h"
44 AudioStreamBroker::LoopbackSource* GetLoopbackSourceOnUIThread(
45 int render_process_id,
46 int render_frame_id) {
47 DCHECK_CURRENTLY_ON(BrowserThread::UI);
48 auto* source = ForwardingAudioStreamFactory::CoreForFrame(
49 (RenderFrameHost::FromID(render_process_id, render_frame_id)));
51 // The source of the capture has already been destroyed, so fail early.
54 // Note: this pointer is sent over to the IO thread. This is safe since the
55 // destruction of |source| is posted to the IO thread and it hasn't been
60 void EnumerateOutputDevices(MediaStreamManager* media_stream_manager,
61 MediaDevicesManager::EnumerationCallback cb) {
62 DCHECK_CURRENTLY_ON(BrowserThread::IO);
63 MediaDevicesManager::BoolDeviceTypes device_types;
64 device_types[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true;
65 media_stream_manager->media_devices_manager()->EnumerateDevices(
66 device_types, std::move(cb));
69 void TranslateDeviceId(const std::string& device_id,
70 const MediaDeviceSaltAndOrigin& salt_and_origin,
71 base::RepeatingCallback<void(const std::string&)> cb,
72 const MediaDeviceEnumeration& device_array) {
73 DCHECK_CURRENTLY_ON(BrowserThread::IO);
74 for (const auto& device_info : device_array[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT]) {
75 if (MediaStreamManager::DoesMediaDeviceIDMatchHMAC(
76 salt_and_origin.device_id_salt, salt_and_origin.origin, device_id,
77 device_info.device_id)) {
78 cb.Run(device_info.device_id);
82 // If we're unable to translate the device id, |cb| will not be run.
85 void GetSaltOriginAndPermissionsOnUIThread(
88 base::OnceCallback<void(MediaDeviceSaltAndOrigin salt_and_origin,
89 bool has_access)> cb) {
90 auto salt_and_origin = GetMediaDeviceSaltAndOrigin(process_id, frame_id);
91 bool access = MediaDevicesPermissionChecker().CheckPermissionOnUIThread(
92 MEDIA_DEVICE_TYPE_AUDIO_OUTPUT, process_id, frame_id);
93 base::PostTaskWithTraits(
94 FROM_HERE, {BrowserThread::IO},
95 base::BindOnce(std::move(cb), std::move(salt_and_origin), access));
100 class RenderFrameAudioInputStreamFactory::Core final
101 : public mojom::RendererAudioInputStreamFactory {
103 Core(mojom::RendererAudioInputStreamFactoryRequest request,
104 MediaStreamManager* media_stream_manager,
105 RenderFrameHost* render_frame_host);
109 void Init(mojom::RendererAudioInputStreamFactoryRequest request);
111 // mojom::RendererAudioInputStreamFactory implementation.
113 mojom::RendererAudioInputStreamFactoryClientPtr client,
115 const media::AudioParameters& audio_params,
116 bool automatic_gain_control,
117 uint32_t shared_memory_count,
118 audio::mojom::AudioProcessingConfigPtr processing_config) final;
120 void AssociateInputAndOutputForAec(
121 const base::UnguessableToken& input_stream_id,
122 const std::string& output_device_id) final;
124 void CreateLoopbackStream(
125 mojom::RendererAudioInputStreamFactoryClientPtr client,
126 const media::AudioParameters& audio_params,
127 uint32_t shared_memory_count,
128 bool disable_local_echo,
129 AudioStreamBroker::LoopbackSource* loopback_source);
131 void AssociateInputAndOutputForAecAfterCheckingAccess(
132 const base::UnguessableToken& input_stream_id,
133 const std::string& output_device_id,
134 MediaDeviceSaltAndOrigin salt_and_origin,
135 bool access_granted);
137 void AssociateTranslatedOutputDeviceForAec(
138 const base::UnguessableToken& input_stream_id,
139 const std::string& raw_output_device_id);
141 MediaStreamManager* const media_stream_manager_;
142 const int process_id_;
144 const url::Origin origin_;
146 mojo::Binding<RendererAudioInputStreamFactory> binding_;
147 ForwardingAudioStreamFactory::Core* forwarding_factory_;
149 base::WeakPtrFactory<Core> weak_ptr_factory_;
151 DISALLOW_COPY_AND_ASSIGN(Core);
154 RenderFrameAudioInputStreamFactory::RenderFrameAudioInputStreamFactory(
155 mojom::RendererAudioInputStreamFactoryRequest request,
156 MediaStreamManager* media_stream_manager,
157 RenderFrameHost* render_frame_host)
158 : core_(new Core(std::move(request),
159 media_stream_manager,
160 render_frame_host)) {
161 DCHECK_CURRENTLY_ON(BrowserThread::UI);
164 RenderFrameAudioInputStreamFactory::~RenderFrameAudioInputStreamFactory() {
165 DCHECK_CURRENTLY_ON(BrowserThread::UI);
166 // Ensure |core_| is deleted on the right thread. DeleteOnIOThread isn't used
167 // as it doesn't post in case it is already executed on the right thread. That
168 // causes issues in unit tests where the UI thread and the IO thread are the
170 base::PostTaskWithTraits(
171 FROM_HERE, {BrowserThread::IO},
172 base::BindOnce([](std::unique_ptr<Core>) {}, std::move(core_)));
175 RenderFrameAudioInputStreamFactory::Core::Core(
176 mojom::RendererAudioInputStreamFactoryRequest request,
177 MediaStreamManager* media_stream_manager,
178 RenderFrameHost* render_frame_host)
179 : media_stream_manager_(media_stream_manager),
180 process_id_(render_frame_host->GetProcess()->GetID()),
181 frame_id_(render_frame_host->GetRoutingID()),
182 origin_(render_frame_host->GetLastCommittedOrigin()),
185 ForwardingAudioStreamFactory::CoreForFrame(render_frame_host)),
186 weak_ptr_factory_(this) {
187 DCHECK_CURRENTLY_ON(BrowserThread::UI);
189 if (!forwarding_factory_) {
190 // The only case when we not have a forwarding factory is when the
191 // frame belongs to an interstitial. Interstitials don't need audio, so it's
192 // fine to drop the request.
196 // Unretained is safe since the destruction of |this| is posted to the IO
198 base::PostTaskWithTraits(
199 FROM_HERE, {BrowserThread::IO},
200 base::BindOnce(&Core::Init, base::Unretained(this), std::move(request)));
203 RenderFrameAudioInputStreamFactory::Core::~Core() {
204 DCHECK_CURRENTLY_ON(BrowserThread::IO);
207 void RenderFrameAudioInputStreamFactory::Core::Init(
208 mojom::RendererAudioInputStreamFactoryRequest request) {
209 DCHECK_CURRENTLY_ON(BrowserThread::IO);
210 binding_.Bind(std::move(request));
213 void RenderFrameAudioInputStreamFactory::Core::CreateStream(
214 mojom::RendererAudioInputStreamFactoryClientPtr client,
216 const media::AudioParameters& audio_params,
217 bool automatic_gain_control,
218 uint32_t shared_memory_count,
219 audio::mojom::AudioProcessingConfigPtr processing_config) {
220 DCHECK_CURRENTLY_ON(BrowserThread::IO);
221 DCHECK(forwarding_factory_);
222 TRACE_EVENT1("audio", "RenderFrameAudioInputStreamFactory::CreateStream",
223 "session id", session_id);
225 const MediaStreamDevice* device =
226 media_stream_manager_->audio_input_device_manager()->GetOpenedDeviceById(
230 TRACE_EVENT_INSTANT0("audio", "device not found", TRACE_EVENT_SCOPE_THREAD);
234 WebContentsMediaCaptureId capture_id;
235 if (WebContentsMediaCaptureId::Parse(device->id, &capture_id)) {
236 // For MEDIA_GUM_DESKTOP_AUDIO_CAPTURE, the source is selected from
237 // picker window, we do not mute the source audio. For
238 // MEDIA_GUM_TAB_AUDIO_CAPTURE, the probable use case is Cast, we mute
240 // TODO(qiangchen): Analyze audio constraints to make a duplicating or
241 // diverting decision. It would give web developer more flexibility.
243 base::PostTaskWithTraitsAndReplyWithResult(
244 FROM_HERE, {BrowserThread::UI},
245 base::BindOnce(&GetLoopbackSourceOnUIThread,
246 capture_id.render_process_id,
247 capture_id.main_render_frame_id),
249 &RenderFrameAudioInputStreamFactory::Core::CreateLoopbackStream,
250 weak_ptr_factory_.GetWeakPtr(), std::move(client), audio_params,
251 shared_memory_count, capture_id.disable_local_echo));
253 if (device->type == MEDIA_GUM_DESKTOP_AUDIO_CAPTURE)
254 IncrementDesktopCaptureCounter(SYSTEM_LOOPBACK_AUDIO_CAPTURER_CREATED);
257 forwarding_factory_->CreateInputStream(
258 process_id_, frame_id_, device->id, audio_params, shared_memory_count,
259 automatic_gain_control, std::move(processing_config),
262 // Only count for captures from desktop media picker dialog and system loop
264 if (device->type == MEDIA_GUM_DESKTOP_AUDIO_CAPTURE &&
265 (media::AudioDeviceDescription::IsLoopbackDevice(device->id))) {
266 IncrementDesktopCaptureCounter(SYSTEM_LOOPBACK_AUDIO_CAPTURER_CREATED);
271 void RenderFrameAudioInputStreamFactory::Core::CreateLoopbackStream(
272 mojom::RendererAudioInputStreamFactoryClientPtr client,
273 const media::AudioParameters& audio_params,
274 uint32_t shared_memory_count,
275 bool disable_local_echo,
276 AudioStreamBroker::LoopbackSource* loopback_source) {
277 if (!loopback_source)
280 forwarding_factory_->CreateLoopbackStream(
281 process_id_, frame_id_, loopback_source, audio_params,
282 shared_memory_count, disable_local_echo, std::move(client));
285 void RenderFrameAudioInputStreamFactory::Core::AssociateInputAndOutputForAec(
286 const base::UnguessableToken& input_stream_id,
287 const std::string& output_device_id) {
288 DCHECK_CURRENTLY_ON(BrowserThread::IO);
289 DCHECK(forwarding_factory_);
290 if (!IsValidDeviceId(output_device_id))
293 base::PostTaskWithTraits(
294 FROM_HERE, {BrowserThread::UI},
296 &GetSaltOriginAndPermissionsOnUIThread, process_id_, frame_id_,
298 &Core::AssociateInputAndOutputForAecAfterCheckingAccess,
299 weak_ptr_factory_.GetWeakPtr(), input_stream_id,
303 void RenderFrameAudioInputStreamFactory::Core::
304 AssociateInputAndOutputForAecAfterCheckingAccess(
305 const base::UnguessableToken& input_stream_id,
306 const std::string& output_device_id,
307 MediaDeviceSaltAndOrigin salt_and_origin,
308 bool access_granted) {
309 DCHECK_CURRENTLY_ON(BrowserThread::IO);
310 DCHECK(forwarding_factory_);
315 if (media::AudioDeviceDescription::IsDefaultDevice(output_device_id) ||
316 media::AudioDeviceDescription::IsCommunicationsDevice(output_device_id)) {
317 forwarding_factory_->AssociateInputAndOutputForAec(input_stream_id,
320 EnumerateOutputDevices(
321 media_stream_manager_,
323 &TranslateDeviceId, output_device_id, salt_and_origin,
324 base::BindRepeating(&RenderFrameAudioInputStreamFactory::Core::
325 AssociateTranslatedOutputDeviceForAec,
326 weak_ptr_factory_.GetWeakPtr(),
331 void RenderFrameAudioInputStreamFactory::Core::
332 AssociateTranslatedOutputDeviceForAec(
333 const base::UnguessableToken& input_stream_id,
334 const std::string& raw_output_device_id) {
335 DCHECK_CURRENTLY_ON(BrowserThread::IO);
336 DCHECK(forwarding_factory_);
337 forwarding_factory_->AssociateInputAndOutputForAec(input_stream_id,
338 raw_output_device_id);
341 } // namespace content