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