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