1 // Copyright (c) 2012 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 "libcef/browser/media_capture_devices_dispatcher.h" 6 7 #include "chrome/common/pref_names.h" 8 #include "components/prefs/pref_registry_simple.h" 9 #include "components/prefs/pref_service.h" 10 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/media_capture_devices.h" 12 13 using blink::MediaStreamDevices; 14 using content::BrowserThread; 15 16 namespace { 17 FindDefaultDeviceWithId(const blink::MediaStreamDevices & devices,const std::string & device_id)18 const blink::MediaStreamDevice* FindDefaultDeviceWithId( 19 const blink::MediaStreamDevices& devices, 20 const std::string& device_id) { 21 if (devices.empty()) 22 return nullptr; 23 24 blink::MediaStreamDevices::const_iterator iter = devices.begin(); 25 for (; iter != devices.end(); ++iter) { 26 if (iter->id == device_id) { 27 return &(*iter); 28 } 29 } 30 31 return &(*devices.begin()); 32 } 33 34 } // namespace 35 36 CefMediaCaptureDevicesDispatcher* GetInstance()37 CefMediaCaptureDevicesDispatcher::GetInstance() { 38 return base::Singleton<CefMediaCaptureDevicesDispatcher>::get(); 39 } 40 CefMediaCaptureDevicesDispatcher()41 CefMediaCaptureDevicesDispatcher::CefMediaCaptureDevicesDispatcher() {} 42 ~CefMediaCaptureDevicesDispatcher()43 CefMediaCaptureDevicesDispatcher::~CefMediaCaptureDevicesDispatcher() {} 44 RegisterPrefs(PrefRegistrySimple * registry)45 void CefMediaCaptureDevicesDispatcher::RegisterPrefs( 46 PrefRegistrySimple* registry) { 47 registry->RegisterStringPref(prefs::kDefaultAudioCaptureDevice, 48 std::string()); 49 registry->RegisterStringPref(prefs::kDefaultVideoCaptureDevice, 50 std::string()); 51 } 52 GetDefaultDevices(PrefService * prefs,bool audio,bool video,blink::MediaStreamDevices * devices)53 void CefMediaCaptureDevicesDispatcher::GetDefaultDevices( 54 PrefService* prefs, 55 bool audio, 56 bool video, 57 blink::MediaStreamDevices* devices) { 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 59 DCHECK(audio || video); 60 61 std::string default_device; 62 if (audio) { 63 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); 64 GetRequestedDevice(default_device, true, false, devices); 65 } 66 67 if (video) { 68 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); 69 GetRequestedDevice(default_device, false, true, devices); 70 } 71 } 72 GetRequestedDevice(const std::string & requested_device_id,bool audio,bool video,blink::MediaStreamDevices * devices)73 void CefMediaCaptureDevicesDispatcher::GetRequestedDevice( 74 const std::string& requested_device_id, 75 bool audio, 76 bool video, 77 blink::MediaStreamDevices* devices) { 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 79 DCHECK(audio || video); 80 81 if (audio) { 82 const blink::MediaStreamDevices& audio_devices = GetAudioCaptureDevices(); 83 const blink::MediaStreamDevice* const device = 84 FindDefaultDeviceWithId(audio_devices, requested_device_id); 85 if (device) 86 devices->push_back(*device); 87 } 88 if (video) { 89 const blink::MediaStreamDevices& video_devices = GetVideoCaptureDevices(); 90 const blink::MediaStreamDevice* const device = 91 FindDefaultDeviceWithId(video_devices, requested_device_id); 92 if (device) 93 devices->push_back(*device); 94 } 95 } 96 OnAudioCaptureDevicesChanged()97 void CefMediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() {} 98 OnVideoCaptureDevicesChanged()99 void CefMediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() {} 100 OnMediaRequestStateChanged(int render_process_id,int render_frame_id,int page_request_id,const GURL & security_origin,blink::mojom::MediaStreamType stream_type,content::MediaRequestState state)101 void CefMediaCaptureDevicesDispatcher::OnMediaRequestStateChanged( 102 int render_process_id, 103 int render_frame_id, 104 int page_request_id, 105 const GURL& security_origin, 106 blink::mojom::MediaStreamType stream_type, 107 content::MediaRequestState state) {} 108 OnCreatingAudioStream(int render_process_id,int render_view_id)109 void CefMediaCaptureDevicesDispatcher::OnCreatingAudioStream( 110 int render_process_id, 111 int render_view_id) {} 112 OnSetCapturingLinkSecured(int render_process_id,int render_frame_id,int page_request_id,blink::mojom::MediaStreamType stream_type,bool is_secure)113 void CefMediaCaptureDevicesDispatcher::OnSetCapturingLinkSecured( 114 int render_process_id, 115 int render_frame_id, 116 int page_request_id, 117 blink::mojom::MediaStreamType stream_type, 118 bool is_secure) {} 119 120 const MediaStreamDevices& GetAudioCaptureDevices()121 CefMediaCaptureDevicesDispatcher::GetAudioCaptureDevices() { 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 123 return content::MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(); 124 } 125 126 const MediaStreamDevices& GetVideoCaptureDevices()127 CefMediaCaptureDevicesDispatcher::GetVideoCaptureDevices() { 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 129 return content::MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(); 130 } 131