1 // Copyright 2017 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 "chrome/browser/media/media_access_handler.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
11 #include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
12 #include "content/public/browser/web_contents.h"
13 #include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
14 
IsInsecureCapturingInProgress(int render_process_id,int render_frame_id)15 bool MediaAccessHandler::IsInsecureCapturingInProgress(int render_process_id,
16                                                        int render_frame_id) {
17   return false;
18 }
19 
20 // static
CheckDevicesAndRunCallback(content::WebContents * web_contents,const content::MediaStreamRequest & request,content::MediaResponseCallback callback,bool audio_allowed,bool video_allowed)21 void MediaAccessHandler::CheckDevicesAndRunCallback(
22     content::WebContents* web_contents,
23     const content::MediaStreamRequest& request,
24     content::MediaResponseCallback callback,
25     bool audio_allowed,
26     bool video_allowed) {
27   // TODO(vrk): This code is largely duplicated in
28   // MediaStreamDevicesController::GetDevices(). Move this code into a shared
29   // method between the two classes.
30   bool get_default_audio_device = audio_allowed;
31   bool get_default_video_device = video_allowed;
32 
33   blink::MediaStreamDevices devices;
34 
35   // Set an initial error result. If neither audio or video is allowed, we'll
36   // never try to get any device below but will just create |ui| and return an
37   // empty list with "invalid state" result. If at least one is allowed, we'll
38   // try to get device(s), and if failure, we want to return "no hardware"
39   // result.
40   // TODO(grunell): The invalid state result should be changed to a new denied
41   // result + a dcheck to ensure at least one of audio or video types is
42   // capture.
43   blink::mojom::MediaStreamRequestResult result =
44       (audio_allowed || video_allowed)
45           ? blink::mojom::MediaStreamRequestResult::NO_HARDWARE
46           : blink::mojom::MediaStreamRequestResult::INVALID_STATE;
47 
48   // Get the exact audio or video device if an id is specified.
49   // We only set any error result here and before running the callback change
50   // it to OK if we have any device.
51   if (audio_allowed && !request.requested_audio_device_id.empty()) {
52     const blink::MediaStreamDevice* audio_device =
53         MediaCaptureDevicesDispatcher::GetInstance()->GetRequestedAudioDevice(
54             request.requested_audio_device_id);
55     if (audio_device) {
56       devices.push_back(*audio_device);
57       get_default_audio_device = false;
58     }
59   }
60   if (video_allowed && !request.requested_video_device_id.empty()) {
61     const blink::MediaStreamDevice* video_device =
62         MediaCaptureDevicesDispatcher::GetInstance()->GetRequestedVideoDevice(
63             request.requested_video_device_id);
64     if (video_device) {
65       devices.push_back(*video_device);
66       get_default_video_device = false;
67     }
68   }
69 
70   // If either or both audio and video devices were requested but not
71   // specified by id, get the default devices.
72   if (get_default_audio_device || get_default_video_device) {
73     MediaCaptureDevicesDispatcher::GetInstance()
74         ->GetDefaultDevicesForBrowserContext(
75             web_contents->GetBrowserContext(), get_default_audio_device,
76             get_default_video_device, &devices);
77   }
78 
79   std::unique_ptr<content::MediaStreamUI> ui;
80   if (!devices.empty()) {
81     result = blink::mojom::MediaStreamRequestResult::OK;
82     ui = MediaCaptureDevicesDispatcher::GetInstance()
83              ->GetMediaStreamCaptureIndicator()
84              ->RegisterMediaStream(web_contents, devices);
85   }
86 
87   std::move(callback).Run(devices, result, std::move(ui));
88 }
89