1 /* 2 * libjingle 3 * Copyright 2004 Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TALK_MEDIA_DEVICES_DEVICEMANAGER_H_ 29 #define TALK_MEDIA_DEVICES_DEVICEMANAGER_H_ 30 31 #include <map> 32 #include <string> 33 #include <vector> 34 35 #include "talk/base/scoped_ptr.h" 36 #include "talk/base/sigslot.h" 37 #include "talk/base/stringencode.h" 38 #include "talk/base/window.h" 39 #include "talk/media/base/videocommon.h" 40 41 namespace talk_base { 42 43 class DesktopDescription; 44 class WindowDescription; 45 class WindowPicker; 46 47 } 48 namespace cricket { 49 50 class VideoCapturer; 51 52 // Used to represent an audio or video capture or render device. 53 struct Device { DeviceDevice54 Device() {} DeviceDevice55 Device(const std::string& first, int second) 56 : name(first), 57 id(talk_base::ToString(second)) { 58 } DeviceDevice59 Device(const std::string& first, const std::string& second) 60 : name(first), id(second) {} 61 62 std::string name; 63 std::string id; 64 }; 65 66 class VideoCapturerFactory { 67 public: VideoCapturerFactory()68 VideoCapturerFactory() {} ~VideoCapturerFactory()69 virtual ~VideoCapturerFactory() {} 70 71 virtual VideoCapturer* Create(const Device& device) = 0; 72 }; 73 74 // DeviceManagerInterface - interface to manage the audio and 75 // video devices on the system. 76 class DeviceManagerInterface { 77 public: ~DeviceManagerInterface()78 virtual ~DeviceManagerInterface() { } 79 80 // Initialization 81 virtual bool Init() = 0; 82 virtual void Terminate() = 0; 83 84 // Capabilities 85 virtual int GetCapabilities() = 0; 86 87 // Device enumeration 88 virtual bool GetAudioInputDevices(std::vector<Device>* devices) = 0; 89 virtual bool GetAudioOutputDevices(std::vector<Device>* devices) = 0; 90 91 virtual bool GetAudioInputDevice(const std::string& name, Device* out) = 0; 92 virtual bool GetAudioOutputDevice(const std::string& name, Device* out) = 0; 93 94 virtual bool GetVideoCaptureDevices(std::vector<Device>* devs) = 0; 95 virtual bool GetVideoCaptureDevice(const std::string& name, Device* out) = 0; 96 97 // Caps the capture format according to max format for capturers created 98 // by CreateVideoCapturer(). See ConstrainSupportedFormats() in 99 // videocapturer.h for more detail. 100 // Note that once a VideoCapturer has been created, calling this API will 101 // not affect it. 102 virtual void SetVideoCaptureDeviceMaxFormat( 103 const std::string& usb_id, 104 const VideoFormat& max_format) = 0; 105 virtual void ClearVideoCaptureDeviceMaxFormat(const std::string& usb_id) = 0; 106 107 // Device creation 108 virtual VideoCapturer* CreateVideoCapturer(const Device& device) const = 0; 109 110 virtual bool GetWindows( 111 std::vector<talk_base::WindowDescription>* descriptions) = 0; 112 virtual VideoCapturer* CreateWindowCapturer(talk_base::WindowId window) = 0; 113 114 virtual bool GetDesktops( 115 std::vector<talk_base::DesktopDescription>* descriptions) = 0; 116 virtual VideoCapturer* CreateDesktopCapturer( 117 talk_base::DesktopId desktop) = 0; 118 119 sigslot::signal0<> SignalDevicesChange; 120 121 static const char kDefaultDeviceName[]; 122 }; 123 124 class DeviceWatcher { 125 public: DeviceWatcher(DeviceManagerInterface * dm)126 explicit DeviceWatcher(DeviceManagerInterface* dm) {} ~DeviceWatcher()127 virtual ~DeviceWatcher() {} Start()128 virtual bool Start() { return true; } Stop()129 virtual void Stop() {} 130 }; 131 132 class DeviceManagerFactory { 133 public: 134 static DeviceManagerInterface* Create(); 135 136 private: DeviceManagerFactory()137 DeviceManagerFactory() {} 138 }; 139 140 class DeviceManager : public DeviceManagerInterface { 141 public: 142 DeviceManager(); 143 virtual ~DeviceManager(); 144 set_device_video_capturer_factory(VideoCapturerFactory * device_video_capturer_factory)145 void set_device_video_capturer_factory( 146 VideoCapturerFactory* device_video_capturer_factory) { 147 device_video_capturer_factory_.reset(device_video_capturer_factory); 148 } 149 150 // Initialization 151 virtual bool Init(); 152 virtual void Terminate(); 153 154 // Capabilities 155 virtual int GetCapabilities(); 156 157 // Device enumeration 158 virtual bool GetAudioInputDevices(std::vector<Device>* devices); 159 virtual bool GetAudioOutputDevices(std::vector<Device>* devices); 160 161 virtual bool GetAudioInputDevice(const std::string& name, Device* out); 162 virtual bool GetAudioOutputDevice(const std::string& name, Device* out); 163 164 virtual bool GetVideoCaptureDevices(std::vector<Device>* devs); 165 virtual bool GetVideoCaptureDevice(const std::string& name, Device* out); 166 167 virtual void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id, 168 const VideoFormat& max_format); 169 virtual void ClearVideoCaptureDeviceMaxFormat(const std::string& usb_id); 170 171 virtual VideoCapturer* CreateVideoCapturer(const Device& device) const; 172 173 virtual bool GetWindows( 174 std::vector<talk_base::WindowDescription>* descriptions); 175 virtual VideoCapturer* CreateWindowCapturer(talk_base::WindowId window); 176 177 virtual bool GetDesktops( 178 std::vector<talk_base::DesktopDescription>* descriptions); 179 virtual VideoCapturer* CreateDesktopCapturer(talk_base::DesktopId desktop); 180 181 // The exclusion_list MUST be a NULL terminated list. 182 static bool FilterDevices(std::vector<Device>* devices, 183 const char* const exclusion_list[]); initialized()184 bool initialized() const { return initialized_; } 185 186 protected: 187 virtual bool GetAudioDevices(bool input, std::vector<Device>* devs); 188 virtual bool GetAudioDevice(bool is_input, const std::string& name, 189 Device* out); 190 virtual bool GetDefaultVideoCaptureDevice(Device* device); 191 bool IsInWhitelist(const std::string& key, VideoFormat* video_format) const; 192 virtual bool GetMaxFormat(const Device& device, 193 VideoFormat* video_format) const; 194 set_initialized(bool initialized)195 void set_initialized(bool initialized) { initialized_ = initialized; } 196 set_watcher(DeviceWatcher * watcher)197 void set_watcher(DeviceWatcher* watcher) { watcher_.reset(watcher); } watcher()198 DeviceWatcher* watcher() { return watcher_.get(); } 199 200 private: 201 // The exclusion_list MUST be a NULL terminated list. 202 static bool ShouldDeviceBeIgnored(const std::string& device_name, 203 const char* const exclusion_list[]); 204 205 bool initialized_; 206 talk_base::scoped_ptr<VideoCapturerFactory> device_video_capturer_factory_; 207 std::map<std::string, VideoFormat> max_formats_; 208 talk_base::scoped_ptr<DeviceWatcher> watcher_; 209 talk_base::scoped_ptr<talk_base::WindowPicker> window_picker_; 210 }; 211 212 } // namespace cricket 213 214 #endif // TALK_MEDIA_DEVICES_DEVICEMANAGER_H_ 215