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