| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/media/devices/devicemanager.h" | |
| 12 | |
| 13 #include "webrtc/base/fileutils.h" | |
| 14 #include "webrtc/base/logging.h" | |
| 15 #include "webrtc/base/pathutils.h" | |
| 16 #include "webrtc/base/stringutils.h" | |
| 17 #include "webrtc/base/thread.h" | |
| 18 #include "webrtc/base/windowpicker.h" | |
| 19 #include "webrtc/base/windowpickerfactory.h" | |
| 20 #include "webrtc/media/base/mediacommon.h" | |
| 21 #include "webrtc/media/base/videocapturer.h" | |
| 22 #include "webrtc/media/base/videocapturerfactory.h" | |
| 23 #include "webrtc/media/devices/deviceinfo.h" | |
| 24 | |
| 25 #ifdef HAVE_WEBRTC_VIDEO | |
| 26 #include "webrtc/media/engine/webrtcvideocapturerfactory.h" | |
| 27 #endif // HAVE_WEBRTC_VIDEO | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 bool StringMatchWithWildcard( | |
| 32 const std::pair<const std::basic_string<char>, cricket::VideoFormat> key, | |
| 33 const std::string& val) { | |
| 34 return rtc::string_match(val.c_str(), key.first.c_str()); | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 namespace cricket { | |
| 40 | |
| 41 // Initialize to empty string. | |
| 42 const char DeviceManagerInterface::kDefaultDeviceName[] = ""; | |
| 43 | |
| 44 DeviceManager::DeviceManager() | |
| 45 : initialized_(false), | |
| 46 window_picker_(rtc::WindowPickerFactory::CreateWindowPicker()) { | |
| 47 #ifdef HAVE_WEBRTC_VIDEO | |
| 48 SetVideoDeviceCapturerFactory(new WebRtcVideoDeviceCapturerFactory()); | |
| 49 #endif // HAVE_WEBRTC_VIDEO | |
| 50 } | |
| 51 | |
| 52 DeviceManager::~DeviceManager() { | |
| 53 if (initialized()) { | |
| 54 Terminate(); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 bool DeviceManager::Init() { | |
| 59 if (!initialized()) { | |
| 60 if (!watcher()->Start()) { | |
| 61 return false; | |
| 62 } | |
| 63 set_initialized(true); | |
| 64 } | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 void DeviceManager::Terminate() { | |
| 69 if (initialized()) { | |
| 70 watcher()->Stop(); | |
| 71 set_initialized(false); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 int DeviceManager::GetCapabilities() { | |
| 76 std::vector<Device> devices; | |
| 77 int caps = VIDEO_RECV; | |
| 78 if (GetAudioInputDevices(&devices) && !devices.empty()) { | |
| 79 caps |= AUDIO_SEND; | |
| 80 } | |
| 81 if (GetAudioOutputDevices(&devices) && !devices.empty()) { | |
| 82 caps |= AUDIO_RECV; | |
| 83 } | |
| 84 if (GetVideoCaptureDevices(&devices) && !devices.empty()) { | |
| 85 caps |= VIDEO_SEND; | |
| 86 } | |
| 87 return caps; | |
| 88 } | |
| 89 | |
| 90 bool DeviceManager::GetAudioInputDevices(std::vector<Device>* devices) { | |
| 91 return GetAudioDevices(true, devices); | |
| 92 } | |
| 93 | |
| 94 bool DeviceManager::GetAudioOutputDevices(std::vector<Device>* devices) { | |
| 95 return GetAudioDevices(false, devices); | |
| 96 } | |
| 97 | |
| 98 bool DeviceManager::GetAudioInputDevice(const std::string& name, Device* out) { | |
| 99 return GetAudioDevice(true, name, out); | |
| 100 } | |
| 101 | |
| 102 bool DeviceManager::GetAudioOutputDevice(const std::string& name, Device* out) { | |
| 103 return GetAudioDevice(false, name, out); | |
| 104 } | |
| 105 | |
| 106 bool DeviceManager::GetVideoCaptureDevices(std::vector<Device>* devices) { | |
| 107 devices->clear(); | |
| 108 #if defined(ANDROID) || defined(WEBRTC_IOS) | |
| 109 // On Android and iOS, we treat the camera(s) as a single device. Even if | |
| 110 // there are multiple cameras, that's abstracted away at a higher level. | |
| 111 Device dev("camera", "1"); // name and ID | |
| 112 devices->push_back(dev); | |
| 113 return true; | |
| 114 #else | |
| 115 return false; | |
| 116 #endif | |
| 117 } | |
| 118 | |
| 119 bool DeviceManager::GetVideoCaptureDevice(const std::string& name, | |
| 120 Device* out) { | |
| 121 // If the name is empty, return the default device. | |
| 122 if (name.empty() || name == kDefaultDeviceName) { | |
| 123 return GetDefaultVideoCaptureDevice(out); | |
| 124 } | |
| 125 | |
| 126 std::vector<Device> devices; | |
| 127 if (!GetVideoCaptureDevices(&devices)) { | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 for (std::vector<Device>::const_iterator it = devices.begin(); | |
| 132 it != devices.end(); ++it) { | |
| 133 if (name == it->name) { | |
| 134 *out = *it; | |
| 135 return true; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 return false; | |
| 140 } | |
| 141 | |
| 142 void DeviceManager::SetVideoCaptureDeviceMaxFormat( | |
| 143 const std::string& usb_id, | |
| 144 const VideoFormat& max_format) { | |
| 145 max_formats_[usb_id] = max_format; | |
| 146 } | |
| 147 | |
| 148 void DeviceManager::ClearVideoCaptureDeviceMaxFormat( | |
| 149 const std::string& usb_id) { | |
| 150 max_formats_.erase(usb_id); | |
| 151 } | |
| 152 | |
| 153 VideoCapturer* DeviceManager::CreateVideoCapturer(const Device& device) const { | |
| 154 if (!video_device_capturer_factory_) { | |
| 155 LOG(LS_ERROR) << "No video capturer factory for devices."; | |
| 156 return NULL; | |
| 157 } | |
| 158 cricket::VideoCapturer* capturer = | |
| 159 video_device_capturer_factory_->Create(device); | |
| 160 if (!capturer) { | |
| 161 return NULL; | |
| 162 } | |
| 163 LOG(LS_INFO) << "Created VideoCapturer for " << device.name; | |
| 164 VideoFormat video_format; | |
| 165 bool has_max = GetMaxFormat(device, &video_format); | |
| 166 capturer->set_enable_camera_list(has_max); | |
| 167 if (has_max) { | |
| 168 capturer->ConstrainSupportedFormats(video_format); | |
| 169 } | |
| 170 return capturer; | |
| 171 } | |
| 172 | |
| 173 bool DeviceManager::GetWindows( | |
| 174 std::vector<rtc::WindowDescription>* descriptions) { | |
| 175 if (!window_picker_) { | |
| 176 return false; | |
| 177 } | |
| 178 return window_picker_->GetWindowList(descriptions); | |
| 179 } | |
| 180 | |
| 181 bool DeviceManager::GetDesktops( | |
| 182 std::vector<rtc::DesktopDescription>* descriptions) { | |
| 183 if (!window_picker_) { | |
| 184 return false; | |
| 185 } | |
| 186 return window_picker_->GetDesktopList(descriptions); | |
| 187 } | |
| 188 | |
| 189 VideoCapturer* DeviceManager::CreateScreenCapturer( | |
| 190 const ScreencastId& screenid) const { | |
| 191 if (!screen_capturer_factory_) { | |
| 192 LOG(LS_ERROR) << "No video capturer factory for screens."; | |
| 193 return NULL; | |
| 194 } | |
| 195 return screen_capturer_factory_->Create(screenid); | |
| 196 } | |
| 197 | |
| 198 bool DeviceManager::GetAudioDevices(bool input, | |
| 199 std::vector<Device>* devs) { | |
| 200 devs->clear(); | |
| 201 #if defined(ANDROID) | |
| 202 // Under Android, 0 is always required for the playout device and 0 is the | |
| 203 // default for the recording device. | |
| 204 devs->push_back(Device("default-device", 0)); | |
| 205 return true; | |
| 206 #else | |
| 207 // Other platforms either have their own derived class implementation | |
| 208 // (desktop) or don't use device manager for audio devices (iOS). | |
| 209 return false; | |
| 210 #endif | |
| 211 } | |
| 212 | |
| 213 bool DeviceManager::GetAudioDevice(bool is_input, const std::string& name, | |
| 214 Device* out) { | |
| 215 // If the name is empty, return the default device id. | |
| 216 if (name.empty() || name == kDefaultDeviceName) { | |
| 217 *out = Device(name, -1); | |
| 218 return true; | |
| 219 } | |
| 220 | |
| 221 std::vector<Device> devices; | |
| 222 bool ret = is_input ? GetAudioInputDevices(&devices) : | |
| 223 GetAudioOutputDevices(&devices); | |
| 224 if (ret) { | |
| 225 ret = false; | |
| 226 for (size_t i = 0; i < devices.size(); ++i) { | |
| 227 if (devices[i].name == name) { | |
| 228 *out = devices[i]; | |
| 229 ret = true; | |
| 230 break; | |
| 231 } | |
| 232 } | |
| 233 } | |
| 234 return ret; | |
| 235 } | |
| 236 | |
| 237 bool DeviceManager::GetDefaultVideoCaptureDevice(Device* device) { | |
| 238 bool ret = false; | |
| 239 // We just return the first device. | |
| 240 std::vector<Device> devices; | |
| 241 ret = (GetVideoCaptureDevices(&devices) && !devices.empty()); | |
| 242 if (ret) { | |
| 243 *device = devices[0]; | |
| 244 } | |
| 245 return ret; | |
| 246 } | |
| 247 | |
| 248 bool DeviceManager::IsInWhitelist(const std::string& key, | |
| 249 VideoFormat* video_format) const { | |
| 250 std::map<std::string, VideoFormat>::const_iterator found = | |
| 251 std::search_n(max_formats_.begin(), max_formats_.end(), 1, key, | |
| 252 StringMatchWithWildcard); | |
| 253 if (found == max_formats_.end()) { | |
| 254 return false; | |
| 255 } | |
| 256 *video_format = found->second; | |
| 257 return true; | |
| 258 } | |
| 259 | |
| 260 bool DeviceManager::GetMaxFormat(const Device& device, | |
| 261 VideoFormat* video_format) const { | |
| 262 // Match USB ID if available. Failing that, match device name. | |
| 263 std::string usb_id; | |
| 264 if (GetUsbId(device, &usb_id) && IsInWhitelist(usb_id, video_format)) { | |
| 265 return true; | |
| 266 } | |
| 267 return IsInWhitelist(device.name, video_format); | |
| 268 } | |
| 269 | |
| 270 bool DeviceManager::ShouldDeviceBeIgnored(const std::string& device_name, | |
| 271 const char* const exclusion_list[]) { | |
| 272 // If exclusion_list is empty return directly. | |
| 273 if (!exclusion_list) | |
| 274 return false; | |
| 275 | |
| 276 int i = 0; | |
| 277 while (exclusion_list[i]) { | |
| 278 if (strnicmp(device_name.c_str(), exclusion_list[i], | |
| 279 strlen(exclusion_list[i])) == 0) { | |
| 280 LOG(LS_INFO) << "Ignoring device " << device_name; | |
| 281 return true; | |
| 282 } | |
| 283 ++i; | |
| 284 } | |
| 285 return false; | |
| 286 } | |
| 287 | |
| 288 bool DeviceManager::FilterDevices(std::vector<Device>* devices, | |
| 289 const char* const exclusion_list[]) { | |
| 290 if (!devices) { | |
| 291 return false; | |
| 292 } | |
| 293 | |
| 294 for (std::vector<Device>::iterator it = devices->begin(); | |
| 295 it != devices->end(); ) { | |
| 296 if (ShouldDeviceBeIgnored(it->name, exclusion_list)) { | |
| 297 it = devices->erase(it); | |
| 298 } else { | |
| 299 ++it; | |
| 300 } | |
| 301 } | |
| 302 return true; | |
| 303 } | |
| 304 | |
| 305 } // namespace cricket | |
| OLD | NEW |