shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 1 | // Copyright 2016 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/midi/midi_manager_winrt.h" |
| 6 | |
qiankun.miao | 53f2d66 | 2016-09-02 17:44:08 -0700 | [diff] [blame] | 7 | #pragma warning(disable : 4467) |
shaochuan | 80f1fba | 2016-09-01 20:44:51 -0700 | [diff] [blame] | 8 | |
Donna Wu | d5a689e | 2019-05-13 02:56:18 +0000 | [diff] [blame] | 9 | #define INITGUID |
| 10 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 11 | #include <windows.h> |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 12 | |
| 13 | #include <cfgmgr32.h> |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 14 | #include <comdef.h> |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 15 | #include <devpkey.h> |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 16 | #include <initguid.h> |
robliao | 048b157 | 2017-04-21 12:46:39 -0700 | [diff] [blame] | 17 | #include <objbase.h> |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 18 | #include <robuffer.h> |
| 19 | #include <windows.devices.enumeration.h> |
| 20 | #include <windows.devices.midi.h> |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 21 | #include <wrl/client.h> |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 22 | #include <wrl/event.h> |
| 23 | |
| 24 | #include <iomanip> |
Peter Boström | 1684b97 | 2021-05-01 01:31:25 +0000 | [diff] [blame^] | 25 | #include <memory> |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
| 27 | #include <unordered_set> |
| 28 | |
| 29 | #include "base/bind.h" |
shaochuan | 9ff63b8 | 2016-09-01 01:58:44 -0700 | [diff] [blame] | 30 | #include "base/scoped_generic.h" |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 31 | #include "base/strings/string_util.h" |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 32 | #include "base/strings/utf_string_conversions.h" |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 33 | #include "base/timer/timer.h" |
Finnur Thorarinsson | ee8428f | 2017-09-30 23:36:49 +0000 | [diff] [blame] | 34 | #include "base/win/core_winrt_util.h" |
Finnur Thorarinsson | ee8428f | 2017-09-30 23:36:49 +0000 | [diff] [blame] | 35 | #include "base/win/scoped_hstring.h" |
junweifu | f51c5a0 | 2017-11-03 06:37:09 +0000 | [diff] [blame] | 36 | #include "base/win/winrt_storage_util.h" |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 37 | #include "media/midi/midi_service.h" |
| 38 | #include "media/midi/task_service.h" |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 39 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 40 | namespace midi { |
| 41 | namespace { |
| 42 | |
| 43 | namespace WRL = Microsoft::WRL; |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 44 | namespace Win = ABI::Windows; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 45 | |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 46 | using base::win::GetActivationFactory; |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 47 | using base::win::ScopedHString; |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 48 | using mojom::PortState; |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 49 | using mojom::Result; |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 50 | using Win::Devices::Enumeration::DeviceInformationUpdate; |
| 51 | using Win::Devices::Enumeration::DeviceWatcher; |
| 52 | using Win::Devices::Enumeration::IDeviceInformation; |
| 53 | using Win::Devices::Enumeration::IDeviceInformationUpdate; |
| 54 | using Win::Devices::Enumeration::IDeviceWatcher; |
| 55 | using Win::Foundation::IAsyncOperation; |
| 56 | using Win::Foundation::ITypedEventHandler; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 57 | |
Xiaohan Wang | fd3e0aa | 2020-02-22 04:08:43 +0000 | [diff] [blame] | 58 | // Alias for printing HRESULT. |
| 59 | const auto PrintHr = logging::SystemErrorCodeToString; |
| 60 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 61 | enum { |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 62 | kDefaultTaskRunner = TaskService::kDefaultRunnerId, |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 63 | kComTaskRunner |
| 64 | }; |
| 65 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 66 | template <typename T> |
| 67 | std::string GetIdString(T* obj) { |
shaochuan | 80f1fba | 2016-09-01 20:44:51 -0700 | [diff] [blame] | 68 | HSTRING result; |
| 69 | HRESULT hr = obj->get_Id(&result); |
| 70 | if (FAILED(hr)) { |
| 71 | VLOG(1) << "get_Id failed: " << PrintHr(hr); |
| 72 | return std::string(); |
| 73 | } |
Finnur Thorarinsson | ee8428f | 2017-09-30 23:36:49 +0000 | [diff] [blame] | 74 | return ScopedHString(result).GetAsUTF8(); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | template <typename T> |
| 78 | std::string GetDeviceIdString(T* obj) { |
shaochuan | 80f1fba | 2016-09-01 20:44:51 -0700 | [diff] [blame] | 79 | HSTRING result; |
| 80 | HRESULT hr = obj->get_DeviceId(&result); |
| 81 | if (FAILED(hr)) { |
| 82 | VLOG(1) << "get_DeviceId failed: " << PrintHr(hr); |
| 83 | return std::string(); |
| 84 | } |
Finnur Thorarinsson | ee8428f | 2017-09-30 23:36:49 +0000 | [diff] [blame] | 85 | return ScopedHString(result).GetAsUTF8(); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | std::string GetNameString(IDeviceInformation* info) { |
shaochuan | 80f1fba | 2016-09-01 20:44:51 -0700 | [diff] [blame] | 89 | HSTRING result; |
| 90 | HRESULT hr = info->get_Name(&result); |
| 91 | if (FAILED(hr)) { |
| 92 | VLOG(1) << "get_Name failed: " << PrintHr(hr); |
| 93 | return std::string(); |
| 94 | } |
Finnur Thorarinsson | ee8428f | 2017-09-30 23:36:49 +0000 | [diff] [blame] | 95 | return ScopedHString(result).GetAsUTF8(); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 96 | } |
| 97 | |
shaochuan | 110262b | 2016-08-31 02:15:16 -0700 | [diff] [blame] | 98 | // Checks if given DeviceInformation represent a Microsoft GS Wavetable Synth |
| 99 | // instance. |
| 100 | bool IsMicrosoftSynthesizer(IDeviceInformation* info) { |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 101 | WRL::ComPtr<Win::Devices::Midi::IMidiSynthesizerStatics> |
| 102 | midi_synthesizer_statics; |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 103 | HRESULT hr = |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 104 | GetActivationFactory<Win::Devices::Midi::IMidiSynthesizerStatics, |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 105 | RuntimeClass_Windows_Devices_Midi_MidiSynthesizer>( |
| 106 | &midi_synthesizer_statics); |
| 107 | if (FAILED(hr)) { |
| 108 | VLOG(1) << "IMidiSynthesizerStatics factory failed: " << PrintHr(hr); |
| 109 | return false; |
| 110 | } |
shaochuan | 110262b | 2016-08-31 02:15:16 -0700 | [diff] [blame] | 111 | boolean result = FALSE; |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 112 | hr = midi_synthesizer_statics->IsSynthesizer(info, &result); |
shaochuan | 110262b | 2016-08-31 02:15:16 -0700 | [diff] [blame] | 113 | VLOG_IF(1, FAILED(hr)) << "IsSynthesizer failed: " << PrintHr(hr); |
| 114 | return result != FALSE; |
| 115 | } |
| 116 | |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 117 | void GetDevPropString(DEVINST handle, |
| 118 | const DEVPROPKEY* devprop_key, |
| 119 | std::string* out) { |
| 120 | DEVPROPTYPE devprop_type; |
| 121 | unsigned long buffer_size = 0; |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 122 | |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 123 | // Retrieve |buffer_size| and allocate buffer later for receiving data. |
| 124 | CONFIGRET cr = CM_Get_DevNode_Property(handle, devprop_key, &devprop_type, |
| 125 | nullptr, &buffer_size, 0); |
| 126 | if (cr != CR_BUFFER_SMALL) { |
| 127 | // Here we print error codes in hex instead of using PrintHr() with |
| 128 | // HRESULT_FROM_WIN32() and CM_MapCrToWin32Err(), since only a minor set of |
| 129 | // CONFIGRET values are mapped to Win32 errors. Same for following VLOG()s. |
| 130 | VLOG(1) << "CM_Get_DevNode_Property failed: CONFIGRET 0x" << std::hex << cr; |
| 131 | return; |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 132 | } |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 133 | if (devprop_type != DEVPROP_TYPE_STRING) { |
| 134 | VLOG(1) << "CM_Get_DevNode_Property returns wrong data type, " |
| 135 | << "expected DEVPROP_TYPE_STRING"; |
| 136 | return; |
| 137 | } |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 138 | |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 139 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
| 140 | |
| 141 | // Receive property data. |
| 142 | cr = CM_Get_DevNode_Property(handle, devprop_key, &devprop_type, buffer.get(), |
| 143 | &buffer_size, 0); |
| 144 | if (cr != CR_SUCCESS) |
| 145 | VLOG(1) << "CM_Get_DevNode_Property failed: CONFIGRET 0x" << std::hex << cr; |
| 146 | else |
Peter Kasting | 426665c | 2021-02-02 13:01:34 +0000 | [diff] [blame] | 147 | *out = base::WideToUTF8(reinterpret_cast<wchar_t*>(buffer.get())); |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 148 | } |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 149 | |
| 150 | // Retrieves manufacturer (provider) and version information of underlying |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 151 | // device driver through PnP Configuration Manager, given device (interface) ID |
| 152 | // provided by WinRT. |out_manufacturer| and |out_driver_version| won't be |
| 153 | // modified if retrieval fails. |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 154 | // |
| 155 | // Device instance ID is extracted from device (interface) ID provided by WinRT |
| 156 | // APIs, for example from the following interface ID: |
| 157 | // \\?\SWD#MMDEVAPI#MIDII_60F39FCA.P_0002#{504be32c-ccf6-4d2c-b73f-6f8b3747e22b} |
| 158 | // we extract the device instance ID: SWD\MMDEVAPI\MIDII_60F39FCA.P_0002 |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 159 | // |
| 160 | // However the extracted device instance ID represent a "software device" |
| 161 | // provided by Microsoft, which is an interface on top of the hardware for each |
| 162 | // input/output port. Therefore we further locate its parent device, which is |
| 163 | // the actual hardware device, for driver information. |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 164 | void GetDriverInfoFromDeviceId(const std::string& dev_id, |
| 165 | std::string* out_manufacturer, |
| 166 | std::string* out_driver_version) { |
Peter Kasting | 426665c | 2021-02-02 13:01:34 +0000 | [diff] [blame] | 167 | std::wstring dev_instance_id = |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 168 | base::UTF8ToWide(dev_id.substr(4, dev_id.size() - 43)); |
| 169 | base::ReplaceChars(dev_instance_id, L"#", L"\\", &dev_instance_id); |
| 170 | |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 171 | DEVINST dev_instance_handle; |
| 172 | CONFIGRET cr = CM_Locate_DevNode(&dev_instance_handle, &dev_instance_id[0], |
| 173 | CM_LOCATE_DEVNODE_NORMAL); |
| 174 | if (cr != CR_SUCCESS) { |
| 175 | VLOG(1) << "CM_Locate_DevNode failed: CONFIGRET 0x" << std::hex << cr; |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 179 | DEVINST parent_handle; |
| 180 | cr = CM_Get_Parent(&parent_handle, dev_instance_handle, 0); |
| 181 | if (cr != CR_SUCCESS) { |
| 182 | VLOG(1) << "CM_Get_Parent failed: CONFIGRET 0x" << std::hex << cr; |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
shaochuan | 4eff30e | 2016-09-09 01:24:14 -0700 | [diff] [blame] | 186 | GetDevPropString(parent_handle, &DEVPKEY_Device_DriverProvider, |
| 187 | out_manufacturer); |
| 188 | GetDevPropString(parent_handle, &DEVPKEY_Device_DriverVersion, |
| 189 | out_driver_version); |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 190 | } |
| 191 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 192 | // Tokens with value = 0 are considered invalid (as in <wrl/event.h>). |
| 193 | const int64_t kInvalidTokenValue = 0; |
| 194 | |
| 195 | template <typename InterfaceType> |
| 196 | struct MidiPort { |
| 197 | MidiPort() = default; |
| 198 | |
| 199 | uint32_t index; |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 200 | WRL::ComPtr<InterfaceType> handle; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 201 | EventRegistrationToken token_MessageReceived; |
| 202 | |
| 203 | private: |
| 204 | DISALLOW_COPY_AND_ASSIGN(MidiPort); |
| 205 | }; |
| 206 | |
| 207 | } // namespace |
| 208 | |
| 209 | template <typename InterfaceType, |
| 210 | typename RuntimeType, |
| 211 | typename StaticsInterfaceType, |
Peter Kasting | 426665c | 2021-02-02 13:01:34 +0000 | [diff] [blame] | 212 | wchar_t const* runtime_class_id> |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 213 | class MidiManagerWinrt::MidiPortManager { |
| 214 | public: |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 215 | // MidiPortManager instances should be constructed on the kComTaskRunner. |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 216 | MidiPortManager(MidiManagerWinrt* midi_manager) |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 217 | : midi_service_(midi_manager->service()), midi_manager_(midi_manager) {} |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 218 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 219 | virtual ~MidiPortManager() { |
| 220 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
| 221 | } |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 222 | |
| 223 | bool StartWatcher() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 224 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 225 | |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 226 | HRESULT hr = GetActivationFactory<StaticsInterfaceType, runtime_class_id>( |
| 227 | &midi_port_statics_); |
| 228 | if (FAILED(hr)) { |
| 229 | VLOG(1) << "StaticsInterfaceType factory failed: " << PrintHr(hr); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 230 | return false; |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 231 | } |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 232 | |
| 233 | HSTRING device_selector = nullptr; |
| 234 | hr = midi_port_statics_->GetDeviceSelector(&device_selector); |
| 235 | if (FAILED(hr)) { |
| 236 | VLOG(1) << "GetDeviceSelector failed: " << PrintHr(hr); |
| 237 | return false; |
| 238 | } |
| 239 | |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 240 | WRL::ComPtr<Win::Devices::Enumeration::IDeviceInformationStatics> |
| 241 | dev_info_statics; |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 242 | hr = GetActivationFactory< |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 243 | Win::Devices::Enumeration::IDeviceInformationStatics, |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 244 | RuntimeClass_Windows_Devices_Enumeration_DeviceInformation>( |
| 245 | &dev_info_statics); |
| 246 | if (FAILED(hr)) { |
| 247 | VLOG(1) << "IDeviceInformationStatics failed: " << PrintHr(hr); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 248 | return false; |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 249 | } |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 250 | |
Xiaohan Wang | f283291 | 2020-02-21 18:38:02 +0000 | [diff] [blame] | 251 | hr = dev_info_statics->CreateWatcherAqsFilter(device_selector, &watcher_); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 252 | if (FAILED(hr)) { |
| 253 | VLOG(1) << "CreateWatcherAqsFilter failed: " << PrintHr(hr); |
| 254 | return false; |
| 255 | } |
| 256 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 257 | // Register callbacks to WinRT that post state-modifying tasks back to |
| 258 | // kComTaskRunner. All posted tasks run only during the MidiPortManager |
| 259 | // instance is alive. This is ensured by MidiManagerWinrt by calling |
| 260 | // UnbindInstance() before destructing any MidiPortManager instance. Thus |
| 261 | // we can handle raw pointers safely in the following blocks. |
| 262 | MidiPortManager* port_manager = this; |
| 263 | TaskService* task_service = midi_service_->task_service(); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 264 | |
| 265 | hr = watcher_->add_Added( |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 266 | WRL::Callback<ITypedEventHandler< |
| 267 | DeviceWatcher*, Win::Devices::Enumeration::DeviceInformation*>>( |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 268 | [port_manager, task_service](IDeviceWatcher* watcher, |
| 269 | IDeviceInformation* info) { |
shaochuan | c289452 | 2016-09-20 01:10:50 -0700 | [diff] [blame] | 270 | if (!info) { |
| 271 | VLOG(1) << "DeviceWatcher.Added callback provides null " |
| 272 | "pointer, ignoring"; |
| 273 | return S_OK; |
| 274 | } |
| 275 | |
shaochuan | 110262b | 2016-08-31 02:15:16 -0700 | [diff] [blame] | 276 | // Disable Microsoft GS Wavetable Synth due to security reasons. |
| 277 | // http://crbug.com/499279 |
| 278 | if (IsMicrosoftSynthesizer(info)) |
| 279 | return S_OK; |
| 280 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 281 | std::string dev_id = GetIdString(info), |
| 282 | dev_name = GetNameString(info); |
| 283 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 284 | task_service->PostBoundTask( |
| 285 | kComTaskRunner, base::BindOnce(&MidiPortManager::OnAdded, |
| 286 | base::Unretained(port_manager), |
| 287 | dev_id, dev_name)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 288 | |
| 289 | return S_OK; |
| 290 | }) |
| 291 | .Get(), |
| 292 | &token_Added_); |
| 293 | if (FAILED(hr)) { |
| 294 | VLOG(1) << "add_Added failed: " << PrintHr(hr); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | hr = watcher_->add_EnumerationCompleted( |
| 299 | WRL::Callback<ITypedEventHandler<DeviceWatcher*, IInspectable*>>( |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 300 | [port_manager, task_service](IDeviceWatcher* watcher, |
| 301 | IInspectable* insp) { |
| 302 | task_service->PostBoundTask( |
| 303 | kComTaskRunner, |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 304 | base::BindOnce(&MidiPortManager::OnEnumerationCompleted, |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 305 | base::Unretained(port_manager))); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 306 | |
| 307 | return S_OK; |
| 308 | }) |
| 309 | .Get(), |
| 310 | &token_EnumerationCompleted_); |
| 311 | if (FAILED(hr)) { |
| 312 | VLOG(1) << "add_EnumerationCompleted failed: " << PrintHr(hr); |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | hr = watcher_->add_Removed( |
| 317 | WRL::Callback< |
| 318 | ITypedEventHandler<DeviceWatcher*, DeviceInformationUpdate*>>( |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 319 | [port_manager, task_service](IDeviceWatcher* watcher, |
| 320 | IDeviceInformationUpdate* update) { |
shaochuan | c289452 | 2016-09-20 01:10:50 -0700 | [diff] [blame] | 321 | if (!update) { |
| 322 | VLOG(1) << "DeviceWatcher.Removed callback provides null " |
| 323 | "pointer, ignoring"; |
| 324 | return S_OK; |
| 325 | } |
| 326 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 327 | std::string dev_id = GetIdString(update); |
| 328 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 329 | task_service->PostBoundTask( |
| 330 | kComTaskRunner, |
| 331 | base::BindOnce(&MidiPortManager::OnRemoved, |
| 332 | base::Unretained(port_manager), dev_id)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 333 | |
| 334 | return S_OK; |
| 335 | }) |
| 336 | .Get(), |
| 337 | &token_Removed_); |
| 338 | if (FAILED(hr)) { |
| 339 | VLOG(1) << "add_Removed failed: " << PrintHr(hr); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | hr = watcher_->add_Stopped( |
| 344 | WRL::Callback<ITypedEventHandler<DeviceWatcher*, IInspectable*>>( |
| 345 | [](IDeviceWatcher* watcher, IInspectable* insp) { |
| 346 | // Placeholder, does nothing for now. |
| 347 | return S_OK; |
| 348 | }) |
| 349 | .Get(), |
| 350 | &token_Stopped_); |
| 351 | if (FAILED(hr)) { |
| 352 | VLOG(1) << "add_Stopped failed: " << PrintHr(hr); |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | hr = watcher_->add_Updated( |
| 357 | WRL::Callback< |
| 358 | ITypedEventHandler<DeviceWatcher*, DeviceInformationUpdate*>>( |
| 359 | [](IDeviceWatcher* watcher, IDeviceInformationUpdate* update) { |
| 360 | // TODO(shaochuan): Check for fields to be updated here. |
| 361 | return S_OK; |
| 362 | }) |
| 363 | .Get(), |
| 364 | &token_Updated_); |
| 365 | if (FAILED(hr)) { |
| 366 | VLOG(1) << "add_Updated failed: " << PrintHr(hr); |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | hr = watcher_->Start(); |
| 371 | if (FAILED(hr)) { |
| 372 | VLOG(1) << "Start failed: " << PrintHr(hr); |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | is_initialized_ = true; |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | void StopWatcher() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 381 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 382 | |
| 383 | HRESULT hr; |
| 384 | |
| 385 | for (const auto& entry : ports_) |
| 386 | RemovePortEventHandlers(entry.second.get()); |
| 387 | |
| 388 | if (token_Added_.value != kInvalidTokenValue) { |
| 389 | hr = watcher_->remove_Added(token_Added_); |
| 390 | VLOG_IF(1, FAILED(hr)) << "remove_Added failed: " << PrintHr(hr); |
| 391 | token_Added_.value = kInvalidTokenValue; |
| 392 | } |
| 393 | if (token_EnumerationCompleted_.value != kInvalidTokenValue) { |
| 394 | hr = watcher_->remove_EnumerationCompleted(token_EnumerationCompleted_); |
| 395 | VLOG_IF(1, FAILED(hr)) << "remove_EnumerationCompleted failed: " |
| 396 | << PrintHr(hr); |
| 397 | token_EnumerationCompleted_.value = kInvalidTokenValue; |
| 398 | } |
| 399 | if (token_Removed_.value != kInvalidTokenValue) { |
| 400 | hr = watcher_->remove_Removed(token_Removed_); |
| 401 | VLOG_IF(1, FAILED(hr)) << "remove_Removed failed: " << PrintHr(hr); |
| 402 | token_Removed_.value = kInvalidTokenValue; |
| 403 | } |
| 404 | if (token_Stopped_.value != kInvalidTokenValue) { |
| 405 | hr = watcher_->remove_Stopped(token_Stopped_); |
| 406 | VLOG_IF(1, FAILED(hr)) << "remove_Stopped failed: " << PrintHr(hr); |
| 407 | token_Stopped_.value = kInvalidTokenValue; |
| 408 | } |
| 409 | if (token_Updated_.value != kInvalidTokenValue) { |
| 410 | hr = watcher_->remove_Updated(token_Updated_); |
| 411 | VLOG_IF(1, FAILED(hr)) << "remove_Updated failed: " << PrintHr(hr); |
| 412 | token_Updated_.value = kInvalidTokenValue; |
| 413 | } |
| 414 | |
| 415 | if (is_initialized_) { |
| 416 | hr = watcher_->Stop(); |
| 417 | VLOG_IF(1, FAILED(hr)) << "Stop failed: " << PrintHr(hr); |
| 418 | is_initialized_ = false; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | MidiPort<InterfaceType>* GetPortByDeviceId(std::string dev_id) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 423 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 424 | CHECK(is_initialized_); |
| 425 | |
| 426 | auto it = ports_.find(dev_id); |
| 427 | if (it == ports_.end()) |
| 428 | return nullptr; |
| 429 | return it->second.get(); |
| 430 | } |
| 431 | |
| 432 | MidiPort<InterfaceType>* GetPortByIndex(uint32_t port_index) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 433 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 434 | CHECK(is_initialized_); |
| 435 | |
| 436 | return GetPortByDeviceId(port_ids_[port_index]); |
| 437 | } |
| 438 | |
| 439 | protected: |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 440 | // Points to the MidiService instance, which is expected to outlive the |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 441 | // MidiPortManager instance. |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 442 | MidiService* midi_service_; |
| 443 | |
| 444 | // Points to the MidiManagerWinrt instance, which is safe to be accessed |
| 445 | // from tasks that are invoked by TaskService. |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 446 | MidiManagerWinrt* midi_manager_; |
| 447 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 448 | private: |
| 449 | // DeviceWatcher callbacks: |
| 450 | void OnAdded(std::string dev_id, std::string dev_name) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 451 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 452 | CHECK(is_initialized_); |
| 453 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 454 | port_names_[dev_id] = dev_name; |
| 455 | |
Finnur Thorarinsson | ee8428f | 2017-09-30 23:36:49 +0000 | [diff] [blame] | 456 | ScopedHString dev_id_hstring = ScopedHString::Create(dev_id); |
shaochuan | 9ff63b8 | 2016-09-01 01:58:44 -0700 | [diff] [blame] | 457 | if (!dev_id_hstring.is_valid()) |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 458 | return; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 459 | |
| 460 | IAsyncOperation<RuntimeType*>* async_op; |
| 461 | |
shaochuan | 9ff63b8 | 2016-09-01 01:58:44 -0700 | [diff] [blame] | 462 | HRESULT hr = |
| 463 | midi_port_statics_->FromIdAsync(dev_id_hstring.get(), &async_op); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 464 | if (FAILED(hr)) { |
| 465 | VLOG(1) << "FromIdAsync failed: " << PrintHr(hr); |
| 466 | return; |
| 467 | } |
| 468 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 469 | MidiPortManager* port_manager = this; |
| 470 | TaskService* task_service = midi_service_->task_service(); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 471 | |
| 472 | hr = async_op->put_Completed( |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 473 | WRL::Callback< |
| 474 | Win::Foundation::IAsyncOperationCompletedHandler<RuntimeType*>>( |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 475 | [port_manager, task_service]( |
| 476 | IAsyncOperation<RuntimeType*>* async_op, AsyncStatus status) { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 477 | // A reference to |async_op| is kept in |async_ops_|, safe to pass |
| 478 | // outside. |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 479 | task_service->PostBoundTask( |
| 480 | kComTaskRunner, |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 481 | base::BindOnce( |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 482 | &MidiPortManager::OnCompletedGetPortFromIdAsync, |
tzik | 9f09f55 | 2018-02-21 12:56:03 +0000 | [diff] [blame] | 483 | base::Unretained(port_manager), |
| 484 | base::Unretained(async_op))); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 485 | |
| 486 | return S_OK; |
| 487 | }) |
| 488 | .Get()); |
| 489 | if (FAILED(hr)) { |
| 490 | VLOG(1) << "put_Completed failed: " << PrintHr(hr); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | // Keep a reference to incompleted |async_op| for releasing later. |
| 495 | async_ops_.insert(async_op); |
| 496 | } |
| 497 | |
| 498 | void OnEnumerationCompleted() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 499 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 500 | CHECK(is_initialized_); |
| 501 | |
| 502 | if (async_ops_.empty()) |
| 503 | midi_manager_->OnPortManagerReady(); |
| 504 | else |
| 505 | enumeration_completed_not_ready_ = true; |
| 506 | } |
| 507 | |
| 508 | void OnRemoved(std::string dev_id) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 509 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 510 | CHECK(is_initialized_); |
| 511 | |
shaochuan | 110262b | 2016-08-31 02:15:16 -0700 | [diff] [blame] | 512 | // Note: in case Microsoft GS Wavetable Synth triggers this event for some |
| 513 | // reason, it will be ignored here with log emitted. |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 514 | MidiPort<InterfaceType>* port = GetPortByDeviceId(dev_id); |
| 515 | if (!port) { |
| 516 | VLOG(1) << "Removing non-existent port " << dev_id; |
| 517 | return; |
| 518 | } |
| 519 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 520 | SetPortState(port->index, PortState::DISCONNECTED); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 521 | |
| 522 | RemovePortEventHandlers(port); |
| 523 | port->handle = nullptr; |
| 524 | } |
| 525 | |
shaochuan | c289452 | 2016-09-20 01:10:50 -0700 | [diff] [blame] | 526 | void OnCompletedGetPortFromIdAsync(IAsyncOperation<RuntimeType*>* async_op) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 527 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 528 | CHECK(is_initialized_); |
| 529 | |
shaochuan | c289452 | 2016-09-20 01:10:50 -0700 | [diff] [blame] | 530 | InterfaceType* handle = nullptr; |
| 531 | HRESULT hr = async_op->GetResults(&handle); |
| 532 | if (FAILED(hr)) { |
| 533 | VLOG(1) << "GetResults failed: " << PrintHr(hr); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | // Manually release COM interface to completed |async_op|. |
| 538 | auto it = async_ops_.find(async_op); |
| 539 | CHECK(it != async_ops_.end()); |
| 540 | (*it)->Release(); |
| 541 | async_ops_.erase(it); |
| 542 | |
| 543 | if (!handle) { |
| 544 | VLOG(1) << "Midi{In,Out}Port.FromIdAsync callback provides null pointer, " |
| 545 | "ignoring"; |
| 546 | return; |
| 547 | } |
| 548 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 549 | EventRegistrationToken token = {kInvalidTokenValue}; |
| 550 | if (!RegisterOnMessageReceived(handle, &token)) |
| 551 | return; |
| 552 | |
| 553 | std::string dev_id = GetDeviceIdString(handle); |
| 554 | |
| 555 | MidiPort<InterfaceType>* port = GetPortByDeviceId(dev_id); |
| 556 | |
| 557 | if (port == nullptr) { |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 558 | std::string manufacturer = "Unknown", driver_version = "Unknown"; |
| 559 | GetDriverInfoFromDeviceId(dev_id, &manufacturer, &driver_version); |
| 560 | |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 561 | AddPort(mojom::PortInfo(dev_id, manufacturer, port_names_[dev_id], |
| 562 | driver_version, PortState::OPENED)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 563 | |
| 564 | port = new MidiPort<InterfaceType>; |
| 565 | port->index = static_cast<uint32_t>(port_ids_.size()); |
| 566 | |
| 567 | ports_[dev_id].reset(port); |
| 568 | port_ids_.push_back(dev_id); |
| 569 | } else { |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 570 | SetPortState(port->index, PortState::CONNECTED); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | port->handle = handle; |
| 574 | port->token_MessageReceived = token; |
| 575 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 576 | if (enumeration_completed_not_ready_ && async_ops_.empty()) { |
| 577 | midi_manager_->OnPortManagerReady(); |
| 578 | enumeration_completed_not_ready_ = false; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Overrided by MidiInPortManager to listen to input ports. |
| 583 | virtual bool RegisterOnMessageReceived(InterfaceType* handle, |
| 584 | EventRegistrationToken* p_token) { |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | // Overrided by MidiInPortManager to remove MessageReceived event handler. |
| 589 | virtual void RemovePortEventHandlers(MidiPort<InterfaceType>* port) {} |
| 590 | |
| 591 | // Calls midi_manager_->Add{Input,Output}Port. |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 592 | virtual void AddPort(mojom::PortInfo info) = 0; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 593 | |
| 594 | // Calls midi_manager_->Set{Input,Output}PortState. |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 595 | virtual void SetPortState(uint32_t port_index, PortState state) = 0; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 596 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 597 | // Midi{In,Out}PortStatics instance. |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 598 | WRL::ComPtr<StaticsInterfaceType> midi_port_statics_; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 599 | |
| 600 | // DeviceWatcher instance and event registration tokens for unsubscribing |
| 601 | // events in destructor. |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 602 | WRL::ComPtr<IDeviceWatcher> watcher_; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 603 | EventRegistrationToken token_Added_ = {kInvalidTokenValue}, |
| 604 | token_EnumerationCompleted_ = {kInvalidTokenValue}, |
| 605 | token_Removed_ = {kInvalidTokenValue}, |
| 606 | token_Stopped_ = {kInvalidTokenValue}, |
| 607 | token_Updated_ = {kInvalidTokenValue}; |
| 608 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 609 | // All manipulations to these fields should be done on kComTaskRunner. |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 610 | std::unordered_map<std::string, std::unique_ptr<MidiPort<InterfaceType>>> |
| 611 | ports_; |
| 612 | std::vector<std::string> port_ids_; |
| 613 | std::unordered_map<std::string, std::string> port_names_; |
| 614 | |
| 615 | // Keeps AsyncOperation references before the operation completes. Note that |
| 616 | // raw pointers are used here and the COM interfaces should be released |
| 617 | // manually. |
| 618 | std::unordered_set<IAsyncOperation<RuntimeType*>*> async_ops_; |
| 619 | |
| 620 | // Set when device enumeration is completed but OnPortManagerReady() is not |
| 621 | // called since some ports are not yet ready (i.e. |async_ops_| is not empty). |
| 622 | // In such cases, OnPortManagerReady() will be called in |
| 623 | // OnCompletedGetPortFromIdAsync() when the last pending port is ready. |
| 624 | bool enumeration_completed_not_ready_ = false; |
| 625 | |
| 626 | // Set if the instance is initialized without error. Should be checked in all |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 627 | // methods on kComTaskRunner except StartWatcher(). |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 628 | bool is_initialized_ = false; |
| 629 | }; |
| 630 | |
| 631 | class MidiManagerWinrt::MidiInPortManager final |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 632 | : public MidiPortManager<Win::Devices::Midi::IMidiInPort, |
| 633 | Win::Devices::Midi::MidiInPort, |
| 634 | Win::Devices::Midi::IMidiInPortStatics, |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 635 | RuntimeClass_Windows_Devices_Midi_MidiInPort> { |
| 636 | public: |
| 637 | MidiInPortManager(MidiManagerWinrt* midi_manager) |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 638 | : MidiPortManager(midi_manager) {} |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 639 | |
| 640 | private: |
| 641 | // MidiPortManager overrides: |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 642 | bool RegisterOnMessageReceived(Win::Devices::Midi::IMidiInPort* handle, |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 643 | EventRegistrationToken* p_token) override { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 644 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 645 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 646 | MidiInPortManager* port_manager = this; |
| 647 | TaskService* task_service = midi_service_->task_service(); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 648 | |
| 649 | HRESULT hr = handle->add_MessageReceived( |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 650 | WRL::Callback<ITypedEventHandler< |
| 651 | Win::Devices::Midi::MidiInPort*, |
| 652 | Win::Devices::Midi::MidiMessageReceivedEventArgs*>>( |
| 653 | [port_manager, task_service]( |
| 654 | Win::Devices::Midi::IMidiInPort* handle, |
| 655 | Win::Devices::Midi::IMidiMessageReceivedEventArgs* args) { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 656 | const base::TimeTicks now = base::TimeTicks::Now(); |
| 657 | |
| 658 | std::string dev_id = GetDeviceIdString(handle); |
| 659 | |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 660 | WRL::ComPtr<Win::Devices::Midi::IMidiMessage> message; |
Xiaohan Wang | f283291 | 2020-02-21 18:38:02 +0000 | [diff] [blame] | 661 | HRESULT hr = args->get_Message(&message); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 662 | if (FAILED(hr)) { |
| 663 | VLOG(1) << "get_Message failed: " << PrintHr(hr); |
| 664 | return hr; |
| 665 | } |
| 666 | |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 667 | WRL::ComPtr<Win::Storage::Streams::IBuffer> buffer; |
Xiaohan Wang | f283291 | 2020-02-21 18:38:02 +0000 | [diff] [blame] | 668 | hr = message->get_RawData(&buffer); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 669 | if (FAILED(hr)) { |
| 670 | VLOG(1) << "get_RawData failed: " << PrintHr(hr); |
| 671 | return hr; |
| 672 | } |
| 673 | |
| 674 | uint8_t* p_buffer_data = nullptr; |
junweifu | f51c5a0 | 2017-11-03 06:37:09 +0000 | [diff] [blame] | 675 | uint32_t data_length = 0; |
| 676 | hr = base::win::GetPointerToBufferData( |
| 677 | buffer.Get(), &p_buffer_data, &data_length); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 678 | if (FAILED(hr)) |
| 679 | return hr; |
| 680 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 681 | std::vector<uint8_t> data(p_buffer_data, |
| 682 | p_buffer_data + data_length); |
| 683 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 684 | task_service->PostBoundTask( |
| 685 | kComTaskRunner, |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 686 | base::BindOnce(&MidiInPortManager::OnMessageReceived, |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 687 | base::Unretained(port_manager), dev_id, data, |
| 688 | now)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 689 | |
| 690 | return S_OK; |
| 691 | }) |
| 692 | .Get(), |
| 693 | p_token); |
| 694 | if (FAILED(hr)) { |
| 695 | VLOG(1) << "add_MessageReceived failed: " << PrintHr(hr); |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | return true; |
| 700 | } |
| 701 | |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 702 | void RemovePortEventHandlers( |
| 703 | MidiPort<Win::Devices::Midi::IMidiInPort>* port) override { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 704 | if (!(port->handle && |
| 705 | port->token_MessageReceived.value != kInvalidTokenValue)) |
| 706 | return; |
| 707 | |
| 708 | HRESULT hr = |
| 709 | port->handle->remove_MessageReceived(port->token_MessageReceived); |
| 710 | VLOG_IF(1, FAILED(hr)) << "remove_MessageReceived failed: " << PrintHr(hr); |
| 711 | port->token_MessageReceived.value = kInvalidTokenValue; |
| 712 | } |
| 713 | |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 714 | void AddPort(mojom::PortInfo info) final { |
| 715 | midi_manager_->AddInputPort(info); |
| 716 | } |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 717 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 718 | void SetPortState(uint32_t port_index, PortState state) final { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 719 | midi_manager_->SetInputPortState(port_index, state); |
| 720 | } |
| 721 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 722 | // Callback on receiving MIDI input message. |
| 723 | void OnMessageReceived(std::string dev_id, |
| 724 | std::vector<uint8_t> data, |
| 725 | base::TimeTicks time) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 726 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 727 | |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 728 | MidiPort<Win::Devices::Midi::IMidiInPort>* port = GetPortByDeviceId(dev_id); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 729 | CHECK(port); |
| 730 | |
| 731 | midi_manager_->ReceiveMidiData(port->index, &data[0], data.size(), time); |
| 732 | } |
| 733 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 734 | DISALLOW_COPY_AND_ASSIGN(MidiInPortManager); |
| 735 | }; |
| 736 | |
| 737 | class MidiManagerWinrt::MidiOutPortManager final |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 738 | : public MidiPortManager<Win::Devices::Midi::IMidiOutPort, |
| 739 | Win::Devices::Midi::IMidiOutPort, |
| 740 | Win::Devices::Midi::IMidiOutPortStatics, |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 741 | RuntimeClass_Windows_Devices_Midi_MidiOutPort> { |
| 742 | public: |
| 743 | MidiOutPortManager(MidiManagerWinrt* midi_manager) |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 744 | : MidiPortManager(midi_manager) {} |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 745 | |
| 746 | private: |
| 747 | // MidiPortManager overrides: |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 748 | void AddPort(mojom::PortInfo info) final { |
| 749 | midi_manager_->AddOutputPort(info); |
| 750 | } |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 751 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 752 | void SetPortState(uint32_t port_index, PortState state) final { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 753 | midi_manager_->SetOutputPortState(port_index, state); |
| 754 | } |
| 755 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 756 | DISALLOW_COPY_AND_ASSIGN(MidiOutPortManager); |
| 757 | }; |
| 758 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 759 | namespace { |
| 760 | |
| 761 | // FinalizeOnComRunner() run on kComTaskRunner even after the MidiManager |
| 762 | // instance destruction. |
| 763 | void FinalizeOnComRunner( |
| 764 | std::unique_ptr<MidiManagerWinrt::MidiInPortManager> port_manager_in, |
| 765 | std::unique_ptr<MidiManagerWinrt::MidiOutPortManager> port_manager_out) { |
| 766 | if (port_manager_in) |
| 767 | port_manager_in->StopWatcher(); |
| 768 | |
| 769 | if (port_manager_out) |
| 770 | port_manager_out->StopWatcher(); |
| 771 | } |
| 772 | |
| 773 | } // namespace |
| 774 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 775 | MidiManagerWinrt::MidiManagerWinrt(MidiService* service) |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 776 | : MidiManager(service) {} |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 777 | |
| 778 | MidiManagerWinrt::~MidiManagerWinrt() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 779 | // Unbind and take a lock to ensure that InitializeOnComRunner should not run |
| 780 | // after here. |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 781 | if (!service()->task_service()->UnbindInstance()) |
| 782 | return; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 783 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 784 | base::AutoLock auto_lock(lazy_init_member_lock_); |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 785 | service()->task_service()->PostStaticTask( |
| 786 | kComTaskRunner, |
| 787 | base::BindOnce(&FinalizeOnComRunner, std::move(port_manager_in_), |
| 788 | std::move(port_manager_out_))); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Takashi Toyoshima | e8240ab | 2018-10-03 09:30:11 +0000 | [diff] [blame] | 791 | void MidiManagerWinrt::StartInitialization() { |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 792 | if (!service()->task_service()->BindInstance()) |
| 793 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
Takashi Toyoshima | e8240ab | 2018-10-03 09:30:11 +0000 | [diff] [blame] | 794 | |
| 795 | service()->task_service()->PostBoundTask( |
| 796 | kComTaskRunner, base::BindOnce(&MidiManagerWinrt::InitializeOnComRunner, |
| 797 | base::Unretained(this))); |
| 798 | } |
| 799 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 800 | void MidiManagerWinrt::DispatchSendMidiData(MidiManagerClient* client, |
| 801 | uint32_t port_index, |
| 802 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 803 | base::TimeTicks timestamp) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 804 | base::TimeDelta delay = MidiService::TimestampToTimeDeltaDelay(timestamp); |
| 805 | service()->task_service()->PostBoundDelayedTask( |
| 806 | kComTaskRunner, |
| 807 | base::BindOnce(&MidiManagerWinrt::SendOnComRunner, base::Unretained(this), |
| 808 | port_index, data), |
| 809 | delay); |
| 810 | service()->task_service()->PostBoundDelayedTask( |
| 811 | kComTaskRunner, |
| 812 | base::BindOnce(&MidiManagerWinrt::AccumulateMidiBytesSent, |
| 813 | base::Unretained(this), client, data.size()), |
| 814 | delay); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 817 | void MidiManagerWinrt::InitializeOnComRunner() { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 818 | base::AutoLock auto_lock(lazy_init_member_lock_); |
| 819 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 820 | DCHECK(service()->task_service()->IsOnTaskRunner(kComTaskRunner)); |
| 821 | |
Finnur Thorarinsson | ee8428f | 2017-09-30 23:36:49 +0000 | [diff] [blame] | 822 | bool preload_success = base::win::ResolveCoreWinRTDelayload() && |
| 823 | ScopedHString::ResolveCoreWinRTStringDelayload(); |
| 824 | if (!preload_success) { |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 825 | service()->task_service()->PostBoundTask( |
| 826 | kDefaultTaskRunner, |
| 827 | base::BindOnce(&MidiManagerWinrt::CompleteInitialization, |
| 828 | base::Unretained(this), Result::INITIALIZATION_ERROR)); |
shaochuan | 9ff63b8 | 2016-09-01 01:58:44 -0700 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
Peter Boström | 1684b97 | 2021-05-01 01:31:25 +0000 | [diff] [blame^] | 832 | port_manager_in_ = std::make_unique<MidiInPortManager>(this); |
| 833 | port_manager_out_ = std::make_unique<MidiOutPortManager>(this); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 834 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 835 | if (!(port_manager_in_->StartWatcher() && |
| 836 | port_manager_out_->StartWatcher())) { |
| 837 | port_manager_in_->StopWatcher(); |
| 838 | port_manager_out_->StopWatcher(); |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 839 | service()->task_service()->PostBoundTask( |
| 840 | kDefaultTaskRunner, |
| 841 | base::BindOnce(&MidiManagerWinrt::CompleteInitialization, |
| 842 | base::Unretained(this), Result::INITIALIZATION_ERROR)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 846 | void MidiManagerWinrt::SendOnComRunner(uint32_t port_index, |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 847 | const std::vector<uint8_t>& data) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 848 | DCHECK(service()->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 849 | |
Takashi Toyoshima | d4f37cd | 2018-10-01 07:43:39 +0000 | [diff] [blame] | 850 | base::AutoLock auto_lock(lazy_init_member_lock_); |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 851 | MidiPort<Win::Devices::Midi::IMidiOutPort>* port = |
| 852 | port_manager_out_->GetPortByIndex(port_index); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 853 | if (!(port && port->handle)) { |
| 854 | VLOG(1) << "Port not available: " << port_index; |
| 855 | return; |
| 856 | } |
| 857 | |
Peter Kasting | 6e8c847 | 2019-09-20 17:55:55 +0000 | [diff] [blame] | 858 | WRL::ComPtr<Win::Storage::Streams::IBuffer> buffer; |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 859 | HRESULT hr = base::win::CreateIBufferFromData( |
junweifu | f51c5a0 | 2017-11-03 06:37:09 +0000 | [diff] [blame] | 860 | data.data(), static_cast<UINT32>(data.size()), &buffer); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 861 | if (FAILED(hr)) { |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 862 | VLOG(1) << "CreateIBufferFromData failed: " << PrintHr(hr); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 863 | return; |
| 864 | } |
| 865 | |
robliao | 3566d1a | 2017-04-18 17:28:09 -0700 | [diff] [blame] | 866 | hr = port->handle->SendBuffer(buffer.Get()); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 867 | if (FAILED(hr)) { |
| 868 | VLOG(1) << "SendBuffer failed: " << PrintHr(hr); |
| 869 | return; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | void MidiManagerWinrt::OnPortManagerReady() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 874 | DCHECK(service()->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 875 | DCHECK(port_manager_ready_count_ < 2); |
| 876 | |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 877 | if (++port_manager_ready_count_ == 2) { |
| 878 | service()->task_service()->PostBoundTask( |
| 879 | kDefaultTaskRunner, |
| 880 | base::BindOnce(&MidiManagerWinrt::CompleteInitialization, |
| 881 | base::Unretained(this), Result::OK)); |
| 882 | } |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 883 | } |
| 884 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 885 | } // namespace midi |