| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/capture/system_message_window_win.h" | |
| 6 | |
| 7 #include <dbt.h> | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/system_monitor/system_monitor.h" | |
| 13 #include "base/win/wrapped_window_proc.h" | |
| 14 #include "media/audio/win/core_audio_util_win.h" | |
| 15 | |
| 16 namespace media { | |
| 17 | |
| 18 namespace { | |
| 19 const wchar_t kWindowClassName[] = L"Chrome_SystemMessageWindow"; | |
| 20 | |
| 21 // A static map from a device category guid to base::SystemMonitor::DeviceType. | |
| 22 struct { | |
| 23 const GUID device_category; | |
| 24 const base::SystemMonitor::DeviceType device_type; | |
| 25 } const kDeviceCategoryMap[] = { | |
| 26 {KSCATEGORY_AUDIO, base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE}, | |
| 27 {KSCATEGORY_VIDEO, base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE}, | |
| 28 }; | |
| 29 } // namespace | |
| 30 | |
| 31 // Manages the device notification handles for SystemMessageWindowWin. | |
| 32 class SystemMessageWindowWin::DeviceNotifications { | |
| 33 public: | |
| 34 explicit DeviceNotifications(HWND hwnd) : notifications_() { Register(hwnd); } | |
| 35 | |
| 36 ~DeviceNotifications() { Unregister(); } | |
| 37 | |
| 38 void Register(HWND hwnd) { | |
| 39 // Request to receive device notifications. All applications receive basic | |
| 40 // notifications via WM_DEVICECHANGE but in order to receive detailed device | |
| 41 // arrival and removal messages, we need to register. | |
| 42 DEV_BROADCAST_DEVICEINTERFACE filter = {0}; | |
| 43 filter.dbcc_size = sizeof(filter); | |
| 44 filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; | |
| 45 bool core_audio_support = media::CoreAudioUtil::IsSupported(); | |
| 46 for (size_t i = 0; i < arraysize(kDeviceCategoryMap); ++i) { | |
| 47 // If CoreAudio is supported, AudioDeviceListenerWin will | |
| 48 // take care of monitoring audio devices. | |
| 49 if (core_audio_support && | |
| 50 KSCATEGORY_AUDIO == kDeviceCategoryMap[i].device_category) { | |
| 51 continue; | |
| 52 } | |
| 53 | |
| 54 filter.dbcc_classguid = kDeviceCategoryMap[i].device_category; | |
| 55 DCHECK_EQ(notifications_[i], static_cast<HDEVNOTIFY>(NULL)); | |
| 56 notifications_[i] = RegisterDeviceNotification( | |
| 57 hwnd, &filter, DEVICE_NOTIFY_WINDOW_HANDLE); | |
| 58 DPLOG_IF(ERROR, !notifications_[i]) | |
| 59 << "RegisterDeviceNotification failed"; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void Unregister() { | |
| 64 for (size_t i = 0; i < arraysize(notifications_); ++i) { | |
| 65 if (notifications_[i]) { | |
| 66 UnregisterDeviceNotification(notifications_[i]); | |
| 67 notifications_[i] = NULL; | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 HDEVNOTIFY notifications_[arraysize(kDeviceCategoryMap)]; | |
| 74 | |
| 75 DISALLOW_IMPLICIT_CONSTRUCTORS(DeviceNotifications); | |
| 76 }; | |
| 77 | |
| 78 SystemMessageWindowWin::SystemMessageWindowWin() { | |
| 79 WNDCLASSEX window_class; | |
| 80 base::win::InitializeWindowClass( | |
| 81 kWindowClassName, | |
| 82 &base::win::WrappedWindowProc<SystemMessageWindowWin::WndProcThunk>, 0, 0, | |
| 83 0, NULL, NULL, NULL, NULL, NULL, &window_class); | |
| 84 instance_ = window_class.hInstance; | |
| 85 ATOM clazz = RegisterClassEx(&window_class); | |
| 86 DCHECK(clazz); | |
| 87 | |
| 88 window_ = | |
| 89 CreateWindow(kWindowClassName, 0, 0, 0, 0, 0, 0, 0, 0, instance_, 0); | |
| 90 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | |
| 91 device_notifications_.reset(new DeviceNotifications(window_)); | |
| 92 } | |
| 93 | |
| 94 SystemMessageWindowWin::~SystemMessageWindowWin() { | |
| 95 if (window_) { | |
| 96 DestroyWindow(window_); | |
| 97 UnregisterClass(kWindowClassName, instance_); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 LRESULT SystemMessageWindowWin::OnDeviceChange(UINT event_type, LPARAM data) { | |
| 102 base::SystemMonitor* monitor = base::SystemMonitor::Get(); | |
| 103 base::SystemMonitor::DeviceType device_type = | |
| 104 base::SystemMonitor::DEVTYPE_UNKNOWN; | |
| 105 switch (event_type) { | |
| 106 case DBT_DEVNODES_CHANGED: | |
| 107 // For this notification, we're happy with the default DEVTYPE_UNKNOWN. | |
| 108 break; | |
| 109 | |
| 110 case DBT_DEVICEREMOVECOMPLETE: | |
| 111 case DBT_DEVICEARRIVAL: { | |
| 112 // This notification has more details about the specific device that | |
| 113 // was added or removed. See if this is a category we're interested | |
| 114 // in monitoring and if so report the specific device type. If we don't | |
| 115 // find the category in our map, ignore the notification and do not | |
| 116 // notify the system monitor. | |
| 117 DEV_BROADCAST_DEVICEINTERFACE* device_interface = | |
| 118 reinterpret_cast<DEV_BROADCAST_DEVICEINTERFACE*>(data); | |
| 119 if (device_interface->dbcc_devicetype != DBT_DEVTYP_DEVICEINTERFACE) | |
| 120 return TRUE; | |
| 121 for (const auto& map_entry : kDeviceCategoryMap) { | |
| 122 if (map_entry.device_category == device_interface->dbcc_classguid) { | |
| 123 device_type = map_entry.device_type; | |
| 124 break; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 // Devices that we do not have a DEVTYPE_ for, get detected via | |
| 129 // DBT_DEVNODES_CHANGED, so we avoid sending additional notifications | |
| 130 // for those here. | |
| 131 if (device_type == base::SystemMonitor::DEVTYPE_UNKNOWN) | |
| 132 return TRUE; | |
| 133 break; | |
| 134 } | |
| 135 | |
| 136 default: | |
| 137 return TRUE; | |
| 138 } | |
| 139 | |
| 140 monitor->ProcessDevicesChanged(device_type); | |
| 141 | |
| 142 return TRUE; | |
| 143 } | |
| 144 | |
| 145 LRESULT CALLBACK SystemMessageWindowWin::WndProc(HWND hwnd, | |
| 146 UINT message, | |
| 147 WPARAM wparam, | |
| 148 LPARAM lparam) { | |
| 149 switch (message) { | |
| 150 case WM_DEVICECHANGE: | |
| 151 return OnDeviceChange(static_cast<UINT>(wparam), lparam); | |
| 152 default: | |
| 153 break; | |
| 154 } | |
| 155 | |
| 156 return ::DefWindowProc(hwnd, message, wparam, lparam); | |
| 157 } | |
| 158 | |
| 159 } // namespace media | |
| OLD | NEW |