| 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/win32devicemanager.h" | |
| 12 | |
| 13 #include <atlbase.h> | |
| 14 #include <dbt.h> | |
| 15 #include <strmif.h> // must come before ks.h | |
| 16 #include <ks.h> | |
| 17 #include <ksmedia.h> | |
| 18 #include <mmdeviceapi.h> | |
| 19 #include <mmsystem.h> | |
| 20 #include <functiondiscoverykeys_devpkey.h> | |
| 21 #include <uuids.h> | |
| 22 | |
| 23 // PKEY_AudioEndpoint_GUID isn't included in uuid.lib and we don't want | |
| 24 // to define INITGUID in order to define all the uuids in this object file | |
| 25 // as it will conflict with uuid.lib (multiply defined symbols). | |
| 26 // So our workaround is to define this one missing symbol here manually. | |
| 27 // See: https://code.google.com/p/webrtc/issues/detail?id=3996 | |
| 28 EXTERN_C const PROPERTYKEY PKEY_AudioEndpoint_GUID = { { | |
| 29 0x1da5d803, 0xd492, 0x4edd, { | |
| 30 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e | |
| 31 } }, 4 | |
| 32 }; | |
| 33 | |
| 34 #include "webrtc/base/arraysize.h" | |
| 35 #include "webrtc/base/logging.h" | |
| 36 #include "webrtc/base/stringutils.h" | |
| 37 #include "webrtc/base/thread.h" | |
| 38 #include "webrtc/base/win32.h" // ToUtf8 | |
| 39 #include "webrtc/base/win32window.h" | |
| 40 #include "webrtc/media/base/mediacommon.h" | |
| 41 #ifdef HAVE_LOGITECH_HEADERS | |
| 42 #include "third_party/logitech/files/logitechquickcam.h" | |
| 43 #endif | |
| 44 | |
| 45 namespace cricket { | |
| 46 | |
| 47 DeviceManagerInterface* DeviceManagerFactory::Create() { | |
| 48 return new Win32DeviceManager(); | |
| 49 } | |
| 50 | |
| 51 class Win32DeviceWatcher | |
| 52 : public DeviceWatcher, | |
| 53 public rtc::Win32Window { | |
| 54 public: | |
| 55 explicit Win32DeviceWatcher(Win32DeviceManager* dm); | |
| 56 virtual ~Win32DeviceWatcher(); | |
| 57 virtual bool Start(); | |
| 58 virtual void Stop(); | |
| 59 | |
| 60 private: | |
| 61 HDEVNOTIFY Register(REFGUID guid); | |
| 62 void Unregister(HDEVNOTIFY notify); | |
| 63 virtual bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result); | |
| 64 | |
| 65 Win32DeviceManager* manager_; | |
| 66 HDEVNOTIFY audio_notify_; | |
| 67 HDEVNOTIFY video_notify_; | |
| 68 }; | |
| 69 | |
| 70 static const char* kFilteredAudioDevicesName[] = { | |
| 71 NULL, | |
| 72 }; | |
| 73 static const char* const kFilteredVideoDevicesName[] = { | |
| 74 "Asus virtual Camera", // Bad Asus desktop virtual cam | |
| 75 "Bluetooth Video", // Bad Sony viao bluetooth sharing driver | |
| 76 NULL, | |
| 77 }; | |
| 78 static const wchar_t kFriendlyName[] = L"FriendlyName"; | |
| 79 static const wchar_t kDevicePath[] = L"DevicePath"; | |
| 80 static const char kUsbDevicePathPrefix[] = "\\\\?\\usb"; | |
| 81 static bool GetDevices(const CLSID& catid, std::vector<Device>* out); | |
| 82 static bool GetCoreAudioDevices(bool input, std::vector<Device>* devs); | |
| 83 static bool GetWaveDevices(bool input, std::vector<Device>* devs); | |
| 84 | |
| 85 Win32DeviceManager::Win32DeviceManager() | |
| 86 : need_couninitialize_(false) { | |
| 87 set_watcher(new Win32DeviceWatcher(this)); | |
| 88 } | |
| 89 | |
| 90 Win32DeviceManager::~Win32DeviceManager() { | |
| 91 if (initialized()) { | |
| 92 Terminate(); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 bool Win32DeviceManager::Init() { | |
| 97 if (!initialized()) { | |
| 98 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); | |
| 99 need_couninitialize_ = SUCCEEDED(hr); | |
| 100 if (FAILED(hr)) { | |
| 101 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; | |
| 102 if (hr != RPC_E_CHANGED_MODE) { | |
| 103 return false; | |
| 104 } | |
| 105 } | |
| 106 if (!watcher()->Start()) { | |
| 107 return false; | |
| 108 } | |
| 109 set_initialized(true); | |
| 110 } | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 void Win32DeviceManager::Terminate() { | |
| 115 if (initialized()) { | |
| 116 watcher()->Stop(); | |
| 117 if (need_couninitialize_) { | |
| 118 CoUninitialize(); | |
| 119 need_couninitialize_ = false; | |
| 120 } | |
| 121 set_initialized(false); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 bool Win32DeviceManager::GetDefaultVideoCaptureDevice(Device* device) { | |
| 126 bool ret = false; | |
| 127 // If there are multiple capture devices, we want the first USB one. | |
| 128 // This avoids issues with defaulting to virtual cameras or grabber cards. | |
| 129 std::vector<Device> devices; | |
| 130 ret = (GetVideoCaptureDevices(&devices) && !devices.empty()); | |
| 131 if (ret) { | |
| 132 *device = devices[0]; | |
| 133 for (size_t i = 0; i < devices.size(); ++i) { | |
| 134 if (strnicmp(devices[i].id.c_str(), kUsbDevicePathPrefix, | |
| 135 arraysize(kUsbDevicePathPrefix) - 1) == 0) { | |
| 136 *device = devices[i]; | |
| 137 break; | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 return ret; | |
| 142 } | |
| 143 | |
| 144 bool Win32DeviceManager::GetAudioDevices(bool input, | |
| 145 std::vector<Device>* devs) { | |
| 146 devs->clear(); | |
| 147 | |
| 148 if (rtc::IsWindowsVistaOrLater()) { | |
| 149 if (!GetCoreAudioDevices(input, devs)) | |
| 150 return false; | |
| 151 } else { | |
| 152 if (!GetWaveDevices(input, devs)) | |
| 153 return false; | |
| 154 } | |
| 155 return FilterDevices(devs, kFilteredAudioDevicesName); | |
| 156 } | |
| 157 | |
| 158 bool Win32DeviceManager::GetVideoCaptureDevices(std::vector<Device>* devices) { | |
| 159 devices->clear(); | |
| 160 if (!GetDevices(CLSID_VideoInputDeviceCategory, devices)) { | |
| 161 return false; | |
| 162 } | |
| 163 return FilterDevices(devices, kFilteredVideoDevicesName); | |
| 164 } | |
| 165 | |
| 166 bool GetDevices(const CLSID& catid, std::vector<Device>* devices) { | |
| 167 HRESULT hr; | |
| 168 | |
| 169 // CComPtr is a scoped pointer that will be auto released when going | |
| 170 // out of scope. CoUninitialize must not be called before the | |
| 171 // release. | |
| 172 CComPtr<ICreateDevEnum> sys_dev_enum; | |
| 173 CComPtr<IEnumMoniker> cam_enum; | |
| 174 if (FAILED(hr = sys_dev_enum.CoCreateInstance(CLSID_SystemDeviceEnum)) || | |
| 175 FAILED(hr = sys_dev_enum->CreateClassEnumerator(catid, &cam_enum, 0))) { | |
| 176 LOG(LS_ERROR) << "Failed to create device enumerator, hr=" << hr; | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 // Only enum devices if CreateClassEnumerator returns S_OK. If there are no | |
| 181 // devices available, S_FALSE will be returned, but enumMk will be NULL. | |
| 182 if (hr == S_OK) { | |
| 183 CComPtr<IMoniker> mk; | |
| 184 while (cam_enum->Next(1, &mk, NULL) == S_OK) { | |
| 185 #ifdef HAVE_LOGITECH_HEADERS | |
| 186 // Initialize Logitech device if applicable | |
| 187 MaybeLogitechDeviceReset(mk); | |
| 188 #endif | |
| 189 CComPtr<IPropertyBag> bag; | |
| 190 if (SUCCEEDED(mk->BindToStorage(NULL, NULL, | |
| 191 __uuidof(bag), reinterpret_cast<void**>(&bag)))) { | |
| 192 CComVariant name, path; | |
| 193 std::string name_str, path_str; | |
| 194 if (SUCCEEDED(bag->Read(kFriendlyName, &name, 0)) && | |
| 195 name.vt == VT_BSTR) { | |
| 196 name_str = rtc::ToUtf8(name.bstrVal); | |
| 197 // Get the device id if one exists. | |
| 198 if (SUCCEEDED(bag->Read(kDevicePath, &path, 0)) && | |
| 199 path.vt == VT_BSTR) { | |
| 200 path_str = rtc::ToUtf8(path.bstrVal); | |
| 201 } | |
| 202 | |
| 203 devices->push_back(Device(name_str, path_str)); | |
| 204 } | |
| 205 } | |
| 206 mk = NULL; | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 return true; | |
| 211 } | |
| 212 | |
| 213 HRESULT GetStringProp(IPropertyStore* bag, PROPERTYKEY key, std::string* out) { | |
| 214 out->clear(); | |
| 215 PROPVARIANT var; | |
| 216 PropVariantInit(&var); | |
| 217 | |
| 218 HRESULT hr = bag->GetValue(key, &var); | |
| 219 if (SUCCEEDED(hr)) { | |
| 220 if (var.pwszVal) | |
| 221 *out = rtc::ToUtf8(var.pwszVal); | |
| 222 else | |
| 223 hr = E_FAIL; | |
| 224 } | |
| 225 | |
| 226 PropVariantClear(&var); | |
| 227 return hr; | |
| 228 } | |
| 229 | |
| 230 // Adapted from http://msdn.microsoft.com/en-us/library/dd370812(v=VS.85).aspx | |
| 231 HRESULT CricketDeviceFromImmDevice(IMMDevice* device, Device* out) { | |
| 232 CComPtr<IPropertyStore> props; | |
| 233 | |
| 234 HRESULT hr = device->OpenPropertyStore(STGM_READ, &props); | |
| 235 if (FAILED(hr)) { | |
| 236 return hr; | |
| 237 } | |
| 238 | |
| 239 // Get the endpoint's name and id. | |
| 240 std::string name, guid; | |
| 241 hr = GetStringProp(props, PKEY_Device_FriendlyName, &name); | |
| 242 if (SUCCEEDED(hr)) { | |
| 243 hr = GetStringProp(props, PKEY_AudioEndpoint_GUID, &guid); | |
| 244 | |
| 245 if (SUCCEEDED(hr)) { | |
| 246 out->name = name; | |
| 247 out->id = guid; | |
| 248 } | |
| 249 } | |
| 250 return hr; | |
| 251 } | |
| 252 | |
| 253 bool GetCoreAudioDevices( | |
| 254 bool input, std::vector<Device>* devs) { | |
| 255 HRESULT hr = S_OK; | |
| 256 CComPtr<IMMDeviceEnumerator> enumerator; | |
| 257 | |
| 258 hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, | |
| 259 __uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&enumerator)); | |
| 260 if (SUCCEEDED(hr)) { | |
| 261 CComPtr<IMMDeviceCollection> devices; | |
| 262 hr = enumerator->EnumAudioEndpoints((input ? eCapture : eRender), | |
| 263 DEVICE_STATE_ACTIVE, &devices); | |
| 264 if (SUCCEEDED(hr)) { | |
| 265 unsigned int count; | |
| 266 hr = devices->GetCount(&count); | |
| 267 | |
| 268 if (SUCCEEDED(hr)) { | |
| 269 for (unsigned int i = 0; i < count; i++) { | |
| 270 CComPtr<IMMDevice> device; | |
| 271 | |
| 272 // Get pointer to endpoint number i. | |
| 273 hr = devices->Item(i, &device); | |
| 274 if (FAILED(hr)) { | |
| 275 break; | |
| 276 } | |
| 277 | |
| 278 Device dev; | |
| 279 hr = CricketDeviceFromImmDevice(device, &dev); | |
| 280 if (SUCCEEDED(hr)) { | |
| 281 devs->push_back(dev); | |
| 282 } else { | |
| 283 LOG(LS_WARNING) << "Unable to query IMM Device, skipping. HR=" | |
| 284 << hr; | |
| 285 hr = S_FALSE; | |
| 286 } | |
| 287 } | |
| 288 } | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 if (FAILED(hr)) { | |
| 293 LOG(LS_WARNING) << "GetCoreAudioDevices failed with hr " << hr; | |
| 294 return false; | |
| 295 } | |
| 296 return true; | |
| 297 } | |
| 298 | |
| 299 bool GetWaveDevices(bool input, std::vector<Device>* devs) { | |
| 300 // Note, we don't use the System Device Enumerator interface here since it | |
| 301 // adds lots of pseudo-devices to the list, such as DirectSound and Wave | |
| 302 // variants of the same device. | |
| 303 if (input) { | |
| 304 int num_devs = waveInGetNumDevs(); | |
| 305 for (int i = 0; i < num_devs; ++i) { | |
| 306 WAVEINCAPS caps; | |
| 307 if (waveInGetDevCaps(i, &caps, sizeof(caps)) == MMSYSERR_NOERROR && | |
| 308 caps.wChannels > 0) { | |
| 309 devs->push_back(Device(rtc::ToUtf8(caps.szPname), | |
| 310 rtc::ToString(i))); | |
| 311 } | |
| 312 } | |
| 313 } else { | |
| 314 int num_devs = waveOutGetNumDevs(); | |
| 315 for (int i = 0; i < num_devs; ++i) { | |
| 316 WAVEOUTCAPS caps; | |
| 317 if (waveOutGetDevCaps(i, &caps, sizeof(caps)) == MMSYSERR_NOERROR && | |
| 318 caps.wChannels > 0) { | |
| 319 devs->push_back(Device(rtc::ToUtf8(caps.szPname), i)); | |
| 320 } | |
| 321 } | |
| 322 } | |
| 323 return true; | |
| 324 } | |
| 325 | |
| 326 Win32DeviceWatcher::Win32DeviceWatcher(Win32DeviceManager* manager) | |
| 327 : DeviceWatcher(manager), | |
| 328 manager_(manager), | |
| 329 audio_notify_(NULL), | |
| 330 video_notify_(NULL) { | |
| 331 } | |
| 332 | |
| 333 Win32DeviceWatcher::~Win32DeviceWatcher() { | |
| 334 } | |
| 335 | |
| 336 bool Win32DeviceWatcher::Start() { | |
| 337 if (!Create(NULL, _T("libjingle Win32DeviceWatcher Window"), | |
| 338 0, 0, 0, 0, 0, 0)) { | |
| 339 return false; | |
| 340 } | |
| 341 | |
| 342 audio_notify_ = Register(KSCATEGORY_AUDIO); | |
| 343 if (!audio_notify_) { | |
| 344 Stop(); | |
| 345 return false; | |
| 346 } | |
| 347 | |
| 348 video_notify_ = Register(KSCATEGORY_VIDEO); | |
| 349 if (!video_notify_) { | |
| 350 Stop(); | |
| 351 return false; | |
| 352 } | |
| 353 | |
| 354 return true; | |
| 355 } | |
| 356 | |
| 357 void Win32DeviceWatcher::Stop() { | |
| 358 UnregisterDeviceNotification(video_notify_); | |
| 359 video_notify_ = NULL; | |
| 360 UnregisterDeviceNotification(audio_notify_); | |
| 361 audio_notify_ = NULL; | |
| 362 Destroy(); | |
| 363 } | |
| 364 | |
| 365 HDEVNOTIFY Win32DeviceWatcher::Register(REFGUID guid) { | |
| 366 DEV_BROADCAST_DEVICEINTERFACE dbdi; | |
| 367 dbdi.dbcc_size = sizeof(dbdi); | |
| 368 dbdi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; | |
| 369 dbdi.dbcc_classguid = guid; | |
| 370 dbdi.dbcc_name[0] = '\0'; | |
| 371 return RegisterDeviceNotification(handle(), &dbdi, | |
| 372 DEVICE_NOTIFY_WINDOW_HANDLE); | |
| 373 } | |
| 374 | |
| 375 void Win32DeviceWatcher::Unregister(HDEVNOTIFY handle) { | |
| 376 UnregisterDeviceNotification(handle); | |
| 377 } | |
| 378 | |
| 379 bool Win32DeviceWatcher::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, | |
| 380 LRESULT& result) { | |
| 381 if (uMsg == WM_DEVICECHANGE) { | |
| 382 if (wParam == DBT_DEVICEARRIVAL || | |
| 383 wParam == DBT_DEVICEREMOVECOMPLETE) { | |
| 384 DEV_BROADCAST_DEVICEINTERFACE* dbdi = | |
| 385 reinterpret_cast<DEV_BROADCAST_DEVICEINTERFACE*>(lParam); | |
| 386 if (dbdi->dbcc_classguid == KSCATEGORY_AUDIO || | |
| 387 dbdi->dbcc_classguid == KSCATEGORY_VIDEO) { | |
| 388 manager_->SignalDevicesChange(); | |
| 389 } | |
| 390 } | |
| 391 result = 0; | |
| 392 return true; | |
| 393 } | |
| 394 | |
| 395 return false; | |
| 396 } | |
| 397 | |
| 398 }; // namespace cricket | |
| OLD | NEW |