henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2008 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 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 28 | #ifndef WEBRTC_MEDIA_DEVICES_FAKEDEVICEMANAGER_H_ |
| 29 | #define WEBRTC_MEDIA_DEVICES_FAKEDEVICEMANAGER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 34 | #include "webrtc/base/scoped_ptr.h" |
| 35 | #include "webrtc/base/window.h" |
| 36 | #include "webrtc/base/windowpicker.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 37 | #include "webrtc/media/base/fakevideocapturer.h" |
| 38 | #include "webrtc/media/base/mediacommon.h" |
| 39 | #include "webrtc/media/devices/devicemanager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | |
| 41 | namespace cricket { |
| 42 | |
| 43 | class FakeDeviceManager : public DeviceManagerInterface { |
| 44 | public: |
| 45 | FakeDeviceManager() {} |
| 46 | virtual bool Init() { |
| 47 | return true; |
| 48 | } |
| 49 | virtual void Terminate() { |
| 50 | } |
| 51 | virtual int GetCapabilities() { |
| 52 | std::vector<Device> devices; |
| 53 | int caps = VIDEO_RECV; |
| 54 | if (!input_devices_.empty()) { |
| 55 | caps |= AUDIO_SEND; |
| 56 | } |
| 57 | if (!output_devices_.empty()) { |
| 58 | caps |= AUDIO_RECV; |
| 59 | } |
| 60 | if (!vidcap_devices_.empty()) { |
| 61 | caps |= VIDEO_SEND; |
| 62 | } |
| 63 | return caps; |
| 64 | } |
| 65 | virtual bool GetAudioInputDevices(std::vector<Device>* devs) { |
| 66 | *devs = input_devices_; |
| 67 | return true; |
| 68 | } |
| 69 | virtual bool GetAudioOutputDevices(std::vector<Device>* devs) { |
| 70 | *devs = output_devices_; |
| 71 | return true; |
| 72 | } |
| 73 | virtual bool GetAudioInputDevice(const std::string& name, Device* out) { |
| 74 | return GetAudioDevice(true, name, out); |
| 75 | } |
| 76 | virtual bool GetAudioOutputDevice(const std::string& name, Device* out) { |
| 77 | return GetAudioDevice(false, name, out); |
| 78 | } |
| 79 | virtual bool GetVideoCaptureDevices(std::vector<Device>* devs) { |
| 80 | *devs = vidcap_devices_; |
| 81 | return true; |
| 82 | } |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 83 | virtual void SetVideoDeviceCapturerFactory( |
| 84 | VideoDeviceCapturerFactory* video_device_capturer_factory) { |
| 85 | } |
| 86 | virtual void SetScreenCapturerFactory( |
| 87 | ScreenCapturerFactory* screen_capturer_factory) { |
| 88 | screen_capturer_factory_.reset(screen_capturer_factory); |
| 89 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | virtual void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id, |
| 91 | const VideoFormat& max_format) { |
| 92 | max_formats_[usb_id] = max_format; |
| 93 | } |
| 94 | bool IsMaxFormatForDevice(const std::string& usb_id, |
| 95 | const VideoFormat& max_format) const { |
| 96 | std::map<std::string, VideoFormat>::const_iterator found = |
| 97 | max_formats_.find(usb_id); |
| 98 | return (found != max_formats_.end()) ? |
| 99 | max_format == found->second : |
| 100 | false; |
| 101 | } |
| 102 | virtual void ClearVideoCaptureDeviceMaxFormat(const std::string& usb_id) { |
| 103 | max_formats_.erase(usb_id); |
| 104 | } |
| 105 | virtual VideoCapturer* CreateVideoCapturer(const Device& device) const { |
| 106 | return new FakeVideoCapturer(); |
| 107 | } |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 108 | virtual VideoCapturer* CreateScreenCapturer( |
| 109 | const ScreencastId& screenid) const { |
| 110 | if (!screen_capturer_factory_) { |
| 111 | return new FakeVideoCapturer(); |
| 112 | } |
| 113 | return screen_capturer_factory_->Create(screenid); |
| 114 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | virtual bool GetWindows( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 116 | std::vector<rtc::WindowDescription>* descriptions) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | descriptions->clear(); |
| 118 | const uint32_t id = 1u; // Note that 0 is not a valid ID. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 119 | const rtc::WindowId window_id = |
| 120 | rtc::WindowId::Cast(id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | std::string title = "FakeWindow"; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 122 | rtc::WindowDescription window_description(window_id, title); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | descriptions->push_back(window_description); |
| 124 | return true; |
| 125 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 126 | virtual VideoCapturer* CreateWindowCapturer(rtc::WindowId window) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | if (!window.IsValid()) { |
| 128 | return NULL; |
| 129 | } |
| 130 | return new FakeVideoCapturer; |
| 131 | } |
| 132 | virtual bool GetDesktops( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 133 | std::vector<rtc::DesktopDescription>* descriptions) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | descriptions->clear(); |
| 135 | const int id = 0; |
| 136 | const int valid_index = 0; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 137 | const rtc::DesktopId desktop_id = |
| 138 | rtc::DesktopId::Cast(id, valid_index); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | std::string title = "FakeDesktop"; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 140 | rtc::DesktopDescription desktop_description(desktop_id, title); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | descriptions->push_back(desktop_description); |
| 142 | return true; |
| 143 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 144 | virtual VideoCapturer* CreateDesktopCapturer(rtc::DesktopId desktop) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | if (!desktop.IsValid()) { |
| 146 | return NULL; |
| 147 | } |
| 148 | return new FakeVideoCapturer; |
| 149 | } |
| 150 | |
| 151 | virtual bool GetDefaultVideoCaptureDevice(Device* device) { |
| 152 | if (vidcap_devices_.empty()) { |
| 153 | return false; |
| 154 | } |
| 155 | *device = vidcap_devices_[0]; |
| 156 | return true; |
| 157 | } |
| 158 | |
kjellander | fcfc804 | 2016-01-14 11:01:09 -0800 | [diff] [blame] | 159 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | bool QtKitToSgDevice(const std::string& qtkit_name, Device* out) { |
| 161 | out->name = qtkit_name; |
| 162 | out->id = "sg:" + qtkit_name; |
| 163 | return true; |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | void SetAudioInputDevices(const std::vector<std::string>& devices) { |
| 168 | input_devices_.clear(); |
| 169 | for (size_t i = 0; i < devices.size(); ++i) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 170 | input_devices_.push_back(Device(devices[i], |
| 171 | static_cast<int>(i))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | } |
| 173 | SignalDevicesChange(); |
| 174 | } |
| 175 | void SetAudioOutputDevices(const std::vector<std::string>& devices) { |
| 176 | output_devices_.clear(); |
| 177 | for (size_t i = 0; i < devices.size(); ++i) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 178 | output_devices_.push_back(Device(devices[i], |
| 179 | static_cast<int>(i))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | } |
| 181 | SignalDevicesChange(); |
| 182 | } |
| 183 | void SetVideoCaptureDevices(const std::vector<std::string>& devices) { |
| 184 | vidcap_devices_.clear(); |
| 185 | for (size_t i = 0; i < devices.size(); ++i) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 186 | vidcap_devices_.push_back(Device(devices[i], |
| 187 | static_cast<int>(i))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 188 | } |
| 189 | SignalDevicesChange(); |
| 190 | } |
| 191 | virtual bool GetVideoCaptureDevice(const std::string& name, |
| 192 | Device* out) { |
| 193 | if (vidcap_devices_.empty()) |
| 194 | return false; |
| 195 | |
| 196 | // If the name is empty, return the default device. |
| 197 | if (name.empty() || name == kDefaultDeviceName) { |
| 198 | *out = vidcap_devices_[0]; |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | return FindDeviceByName(vidcap_devices_, name, out); |
| 203 | } |
| 204 | bool GetAudioDevice(bool is_input, const std::string& name, |
| 205 | Device* out) { |
| 206 | // If the name is empty, return the default device. |
| 207 | if (name.empty() || name == kDefaultDeviceName) { |
| 208 | *out = Device(name, -1); |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | return FindDeviceByName((is_input ? input_devices_ : output_devices_), |
| 213 | name, out); |
| 214 | } |
| 215 | static bool FindDeviceByName(const std::vector<Device>& devices, |
| 216 | const std::string& name, |
| 217 | Device* out) { |
| 218 | for (std::vector<Device>::const_iterator it = devices.begin(); |
| 219 | it != devices.end(); ++it) { |
| 220 | if (name == it->name) { |
| 221 | *out = *it; |
| 222 | return true; |
| 223 | } |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | private: |
| 229 | std::vector<Device> input_devices_; |
| 230 | std::vector<Device> output_devices_; |
| 231 | std::vector<Device> vidcap_devices_; |
| 232 | std::map<std::string, VideoFormat> max_formats_; |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 233 | rtc::scoped_ptr< |
| 234 | ScreenCapturerFactory> screen_capturer_factory_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | } // namespace cricket |
| 238 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 239 | #endif // WEBRTC_MEDIA_DEVICES_FAKEDEVICEMANAGER_H_ |