3
* Copyright 2004 Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#include "talk/session/phone/win32devicemanager.h"
32
#include <strmif.h> // must come before ks.h
35
#define INITGUID // For PKEY_AudioEndpoint_GUID
36
#include <mmdeviceapi.h>
38
#include <functiondiscoverykeys_devpkey.h>
41
#include "talk/base/win32.h" // ToUtf8
42
#include "talk/base/win32window.h"
43
#include "talk/base/logging.h"
44
#include "talk/base/stringutils.h"
45
#include "talk/base/thread.h"
46
#include "talk/session/phone/mediacommon.h"
47
#ifdef HAVE_LOGITECH_HEADERS
48
#include "third_party/logitech/files/logitechquickcam.h"
53
DeviceManagerInterface* DeviceManagerFactory::Create() {
54
return new Win32DeviceManager();
57
class Win32DeviceWatcher
58
: public DeviceWatcher,
59
public talk_base::Win32Window {
61
explicit Win32DeviceWatcher(Win32DeviceManager* dm);
62
virtual ~Win32DeviceWatcher();
67
HDEVNOTIFY Register(REFGUID guid);
68
void Unregister(HDEVNOTIFY notify);
69
virtual bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result);
71
Win32DeviceManager* manager_;
72
HDEVNOTIFY audio_notify_;
73
HDEVNOTIFY video_notify_;
76
static const char* kFilteredAudioDevicesName[] = {
79
static const char* const kFilteredVideoDevicesName[] = {
80
"Google Camera Adapter", // Our own magiccams
81
"Asus virtual Camera", // Bad Asus desktop virtual cam
82
"Bluetooth Video", // Bad Sony viao bluetooth sharing driver
85
static const wchar_t kFriendlyName[] = L"FriendlyName";
86
static const wchar_t kDevicePath[] = L"DevicePath";
87
static const char kUsbDevicePathPrefix[] = "\\\\?\\usb";
88
static bool GetDevices(const CLSID& catid, std::vector<Device>* out);
89
static bool GetCoreAudioDevices(bool input, std::vector<Device>* devs);
90
static bool GetWaveDevices(bool input, std::vector<Device>* devs);
92
Win32DeviceManager::Win32DeviceManager()
93
: need_couninitialize_(false) {
94
set_watcher(new Win32DeviceWatcher(this));
97
Win32DeviceManager::~Win32DeviceManager() {
103
bool Win32DeviceManager::Init() {
104
if (!initialized()) {
105
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
106
need_couninitialize_ = SUCCEEDED(hr);
108
LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr;
109
if (hr != RPC_E_CHANGED_MODE) {
113
if (!watcher()->Start()) {
116
set_initialized(true);
121
void Win32DeviceManager::Terminate() {
124
if (need_couninitialize_) {
126
need_couninitialize_ = false;
128
set_initialized(false);
132
bool Win32DeviceManager::GetDefaultVideoCaptureDevice(Device* device) {
134
// If there are multiple capture devices, we want the first USB one.
135
// This avoids issues with defaulting to virtual cameras or grabber cards.
136
std::vector<Device> devices;
137
ret = (GetVideoCaptureDevices(&devices) && !devices.empty());
139
*device = devices[0];
140
for (size_t i = 0; i < devices.size(); ++i) {
141
if (strnicmp(devices[i].id.c_str(), kUsbDevicePathPrefix,
142
ARRAY_SIZE(kUsbDevicePathPrefix) - 1) == 0) {
143
*device = devices[i];
151
bool Win32DeviceManager::GetAudioDevices(bool input,
152
std::vector<Device>* devs) {
155
if (talk_base::IsWindowsVistaOrLater()) {
156
if (!GetCoreAudioDevices(input, devs))
159
if (!GetWaveDevices(input, devs))
162
return FilterDevices(devs, kFilteredAudioDevicesName);
165
bool Win32DeviceManager::GetVideoCaptureDevices(std::vector<Device>* devices) {
167
if (!GetDevices(CLSID_VideoInputDeviceCategory, devices)) {
170
return FilterDevices(devices, kFilteredVideoDevicesName);
173
bool GetDevices(const CLSID& catid, std::vector<Device>* devices) {
176
// CComPtr is a scoped pointer that will be auto released when going
177
// out of scope. CoUninitialize must not be called before the
179
CComPtr<ICreateDevEnum> sys_dev_enum;
180
CComPtr<IEnumMoniker> cam_enum;
181
if (FAILED(hr = sys_dev_enum.CoCreateInstance(CLSID_SystemDeviceEnum)) ||
182
FAILED(hr = sys_dev_enum->CreateClassEnumerator(catid, &cam_enum, 0))) {
183
LOG(LS_ERROR) << "Failed to create device enumerator, hr=" << hr;
187
// Only enum devices if CreateClassEnumerator returns S_OK. If there are no
188
// devices available, S_FALSE will be returned, but enumMk will be NULL.
190
CComPtr<IMoniker> mk;
191
while (cam_enum->Next(1, &mk, NULL) == S_OK) {
192
#ifdef HAVE_LOGITECH_HEADERS
193
// Initialize Logitech device if applicable
194
MaybeLogitechDeviceReset(mk);
196
CComPtr<IPropertyBag> bag;
197
if (SUCCEEDED(mk->BindToStorage(NULL, NULL,
198
__uuidof(bag), reinterpret_cast<void**>(&bag)))) {
199
CComVariant name, path;
200
std::string name_str, path_str;
201
if (SUCCEEDED(bag->Read(kFriendlyName, &name, 0)) &&
202
name.vt == VT_BSTR) {
203
name_str = talk_base::ToUtf8(name.bstrVal);
204
// Get the device id if one exists.
205
if (SUCCEEDED(bag->Read(kDevicePath, &path, 0)) &&
206
path.vt == VT_BSTR) {
207
path_str = talk_base::ToUtf8(path.bstrVal);
210
devices->push_back(Device(name_str, path_str));
220
HRESULT GetStringProp(IPropertyStore* bag, PROPERTYKEY key, std::string* out) {
223
PropVariantInit(&var);
225
HRESULT hr = bag->GetValue(key, &var);
228
*out = talk_base::ToUtf8(var.pwszVal);
233
PropVariantClear(&var);
237
// Adapted from http://msdn.microsoft.com/en-us/library/dd370812(v=VS.85).aspx
238
HRESULT CricketDeviceFromImmDevice(IMMDevice* device, Device* out) {
239
CComPtr<IPropertyStore> props;
241
HRESULT hr = device->OpenPropertyStore(STGM_READ, &props);
246
// Get the endpoint's name and id.
247
std::string name, guid;
248
hr = GetStringProp(props, PKEY_Device_FriendlyName, &name);
250
hr = GetStringProp(props, PKEY_AudioEndpoint_GUID, &guid);
260
bool GetCoreAudioDevices(
261
bool input, std::vector<Device>* devs) {
263
CComPtr<IMMDeviceEnumerator> enumerator;
265
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
266
__uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&enumerator));
268
CComPtr<IMMDeviceCollection> devices;
269
hr = enumerator->EnumAudioEndpoints((input ? eCapture : eRender),
270
DEVICE_STATE_ACTIVE, &devices);
273
hr = devices->GetCount(&count);
276
for (unsigned int i = 0; i < count; i++) {
277
CComPtr<IMMDevice> device;
279
// Get pointer to endpoint number i.
280
hr = devices->Item(i, &device);
286
hr = CricketDeviceFromImmDevice(device, &dev);
288
devs->push_back(dev);
290
LOG(LS_WARNING) << "Unable to query IMM Device, skipping. HR="
300
LOG(LS_WARNING) << "GetCoreAudioDevices failed with hr " << hr;
306
bool GetWaveDevices(bool input, std::vector<Device>* devs) {
307
// Note, we don't use the System Device Enumerator interface here since it
308
// adds lots of pseudo-devices to the list, such as DirectSound and Wave
309
// variants of the same device.
311
int num_devs = waveInGetNumDevs();
312
for (int i = 0; i < num_devs; ++i) {
314
if (waveInGetDevCaps(i, &caps, sizeof(caps)) == MMSYSERR_NOERROR &&
315
caps.wChannels > 0) {
316
devs->push_back(Device(talk_base::ToUtf8(caps.szPname),
317
talk_base::ToString(i)));
321
int num_devs = waveOutGetNumDevs();
322
for (int i = 0; i < num_devs; ++i) {
324
if (waveOutGetDevCaps(i, &caps, sizeof(caps)) == MMSYSERR_NOERROR &&
325
caps.wChannels > 0) {
326
devs->push_back(Device(talk_base::ToUtf8(caps.szPname), i));
333
Win32DeviceWatcher::Win32DeviceWatcher(Win32DeviceManager* manager)
334
: DeviceWatcher(manager),
337
video_notify_(NULL) {
340
Win32DeviceWatcher::~Win32DeviceWatcher() {
343
bool Win32DeviceWatcher::Start() {
344
if (!Create(NULL, _T("libjingle Win32DeviceWatcher Window"),
349
audio_notify_ = Register(KSCATEGORY_AUDIO);
350
if (!audio_notify_) {
355
video_notify_ = Register(KSCATEGORY_VIDEO);
356
if (!video_notify_) {
364
void Win32DeviceWatcher::Stop() {
365
UnregisterDeviceNotification(video_notify_);
366
video_notify_ = NULL;
367
UnregisterDeviceNotification(audio_notify_);
368
audio_notify_ = NULL;
372
HDEVNOTIFY Win32DeviceWatcher::Register(REFGUID guid) {
373
DEV_BROADCAST_DEVICEINTERFACE dbdi;
374
dbdi.dbcc_size = sizeof(dbdi);
375
dbdi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
376
dbdi.dbcc_classguid = guid;
377
dbdi.dbcc_name[0] = '\0';
378
return RegisterDeviceNotification(handle(), &dbdi,
379
DEVICE_NOTIFY_WINDOW_HANDLE);
382
void Win32DeviceWatcher::Unregister(HDEVNOTIFY handle) {
383
UnregisterDeviceNotification(handle);
386
bool Win32DeviceWatcher::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam,
388
if (uMsg == WM_DEVICECHANGE) {
389
if (wParam == DBT_DEVICEARRIVAL ||
390
wParam == DBT_DEVICEREMOVECOMPLETE) {
391
DEV_BROADCAST_DEVICEINTERFACE* dbdi =
392
reinterpret_cast<DEV_BROADCAST_DEVICEINTERFACE*>(lParam);
393
if (dbdi->dbcc_classguid == KSCATEGORY_AUDIO ||
394
dbdi->dbcc_classguid == KSCATEGORY_VIDEO) {
395
manager_->SignalDevicesChange();
405
}; // namespace cricket