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, |
| 485 | base::Unretained(port_manager), async_op)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 486 | |
| 487 | return S_OK; |
| 488 | }) |
| 489 | .Get()); |
| 490 | if (FAILED(hr)) { |
| 491 | VLOG(1) << "put_Completed failed: " << PrintHr(hr); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | // Keep a reference to incompleted |async_op| for releasing later. |
| 496 | async_ops_.insert(async_op); |
| 497 | } |
| 498 | |
| 499 | void OnEnumerationCompleted() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 500 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 501 | CHECK(is_initialized_); |
| 502 | |
| 503 | if (async_ops_.empty()) |
| 504 | midi_manager_->OnPortManagerReady(); |
| 505 | else |
| 506 | enumeration_completed_not_ready_ = true; |
| 507 | } |
| 508 | |
| 509 | void OnRemoved(std::string dev_id) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 510 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 511 | CHECK(is_initialized_); |
| 512 | |
shaochuan | 110262b | 2016-08-31 02:15:16 -0700 | [diff] [blame] | 513 | // Note: in case Microsoft GS Wavetable Synth triggers this event for some |
| 514 | // reason, it will be ignored here with log emitted. |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 515 | MidiPort<InterfaceType>* port = GetPortByDeviceId(dev_id); |
| 516 | if (!port) { |
| 517 | VLOG(1) << "Removing non-existent port " << dev_id; |
| 518 | return; |
| 519 | } |
| 520 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 521 | SetPortState(port->index, PortState::DISCONNECTED); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 522 | |
| 523 | RemovePortEventHandlers(port); |
| 524 | port->handle = nullptr; |
| 525 | } |
| 526 | |
shaochuan | c289452 | 2016-09-20 01:10:50 -0700 | [diff] [blame] | 527 | void OnCompletedGetPortFromIdAsync(IAsyncOperation<RuntimeType*>* async_op) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 528 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 529 | CHECK(is_initialized_); |
| 530 | |
shaochuan | c289452 | 2016-09-20 01:10:50 -0700 | [diff] [blame] | 531 | InterfaceType* handle = nullptr; |
| 532 | HRESULT hr = async_op->GetResults(&handle); |
| 533 | if (FAILED(hr)) { |
| 534 | VLOG(1) << "GetResults failed: " << PrintHr(hr); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | // Manually release COM interface to completed |async_op|. |
| 539 | auto it = async_ops_.find(async_op); |
| 540 | CHECK(it != async_ops_.end()); |
| 541 | (*it)->Release(); |
| 542 | async_ops_.erase(it); |
| 543 | |
| 544 | if (!handle) { |
| 545 | VLOG(1) << "Midi{In,Out}Port.FromIdAsync callback provides null pointer, " |
| 546 | "ignoring"; |
| 547 | return; |
| 548 | } |
| 549 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 550 | EventRegistrationToken token = {kInvalidTokenValue}; |
| 551 | if (!RegisterOnMessageReceived(handle, &token)) |
| 552 | return; |
| 553 | |
| 554 | std::string dev_id = GetDeviceIdString(handle); |
| 555 | |
| 556 | MidiPort<InterfaceType>* port = GetPortByDeviceId(dev_id); |
| 557 | |
| 558 | if (port == nullptr) { |
shaochuan | 17bc4a0 | 2016-09-06 01:42:12 -0700 | [diff] [blame] | 559 | std::string manufacturer = "Unknown", driver_version = "Unknown"; |
| 560 | GetDriverInfoFromDeviceId(dev_id, &manufacturer, &driver_version); |
| 561 | |
| 562 | AddPort(MidiPortInfo(dev_id, manufacturer, port_names_[dev_id], |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 563 | driver_version, PortState::OPENED)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 564 | |
| 565 | port = new MidiPort<InterfaceType>; |
| 566 | port->index = static_cast<uint32_t>(port_ids_.size()); |
| 567 | |
| 568 | ports_[dev_id].reset(port); |
| 569 | port_ids_.push_back(dev_id); |
| 570 | } else { |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 571 | SetPortState(port->index, PortState::CONNECTED); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | port->handle = handle; |
| 575 | port->token_MessageReceived = token; |
| 576 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 577 | if (enumeration_completed_not_ready_ && async_ops_.empty()) { |
| 578 | midi_manager_->OnPortManagerReady(); |
| 579 | enumeration_completed_not_ready_ = false; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | // Overrided by MidiInPortManager to listen to input ports. |
| 584 | virtual bool RegisterOnMessageReceived(InterfaceType* handle, |
| 585 | EventRegistrationToken* p_token) { |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | // Overrided by MidiInPortManager to remove MessageReceived event handler. |
| 590 | virtual void RemovePortEventHandlers(MidiPort<InterfaceType>* port) {} |
| 591 | |
| 592 | // Calls midi_manager_->Add{Input,Output}Port. |
| 593 | virtual void AddPort(MidiPortInfo info) = 0; |
| 594 | |
| 595 | // Calls midi_manager_->Set{Input,Output}PortState. |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 596 | virtual void SetPortState(uint32_t port_index, PortState state) = 0; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 597 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 598 | // Midi{In,Out}PortStatics instance. |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 599 | WRL::ComPtr<StaticsInterfaceType> midi_port_statics_; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 600 | |
| 601 | // DeviceWatcher instance and event registration tokens for unsubscribing |
| 602 | // events in destructor. |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 603 | WRL::ComPtr<IDeviceWatcher> watcher_; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 604 | EventRegistrationToken token_Added_ = {kInvalidTokenValue}, |
| 605 | token_EnumerationCompleted_ = {kInvalidTokenValue}, |
| 606 | token_Removed_ = {kInvalidTokenValue}, |
| 607 | token_Stopped_ = {kInvalidTokenValue}, |
| 608 | token_Updated_ = {kInvalidTokenValue}; |
| 609 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 610 | // All manipulations to these fields should be done on kComTaskRunner. |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 611 | std::unordered_map<std::string, std::unique_ptr<MidiPort<InterfaceType>>> |
| 612 | ports_; |
| 613 | std::vector<std::string> port_ids_; |
| 614 | std::unordered_map<std::string, std::string> port_names_; |
| 615 | |
| 616 | // Keeps AsyncOperation references before the operation completes. Note that |
| 617 | // raw pointers are used here and the COM interfaces should be released |
| 618 | // manually. |
| 619 | std::unordered_set<IAsyncOperation<RuntimeType*>*> async_ops_; |
| 620 | |
| 621 | // Set when device enumeration is completed but OnPortManagerReady() is not |
| 622 | // called since some ports are not yet ready (i.e. |async_ops_| is not empty). |
| 623 | // In such cases, OnPortManagerReady() will be called in |
| 624 | // OnCompletedGetPortFromIdAsync() when the last pending port is ready. |
| 625 | bool enumeration_completed_not_ready_ = false; |
| 626 | |
| 627 | // Set if the instance is initialized without error. Should be checked in all |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 628 | // methods on kComTaskRunner except StartWatcher(). |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 629 | bool is_initialized_ = false; |
| 630 | }; |
| 631 | |
| 632 | class MidiManagerWinrt::MidiInPortManager final |
| 633 | : public MidiPortManager<IMidiInPort, |
| 634 | MidiInPort, |
| 635 | IMidiInPortStatics, |
| 636 | RuntimeClass_Windows_Devices_Midi_MidiInPort> { |
| 637 | public: |
| 638 | MidiInPortManager(MidiManagerWinrt* midi_manager) |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 639 | : MidiPortManager(midi_manager) {} |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 640 | |
| 641 | private: |
| 642 | // MidiPortManager overrides: |
| 643 | bool RegisterOnMessageReceived(IMidiInPort* handle, |
| 644 | EventRegistrationToken* p_token) override { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 645 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 646 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 647 | MidiInPortManager* port_manager = this; |
| 648 | TaskService* task_service = midi_service_->task_service(); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 649 | |
| 650 | HRESULT hr = handle->add_MessageReceived( |
| 651 | WRL::Callback< |
| 652 | ITypedEventHandler<MidiInPort*, MidiMessageReceivedEventArgs*>>( |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 653 | [port_manager, task_service](IMidiInPort* handle, |
| 654 | IMidiMessageReceivedEventArgs* args) { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 655 | const base::TimeTicks now = base::TimeTicks::Now(); |
| 656 | |
| 657 | std::string dev_id = GetDeviceIdString(handle); |
| 658 | |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 659 | WRL::ComPtr<IMidiMessage> message; |
robliao | 8d08e69 | 2017-05-11 10:14:00 -0700 | [diff] [blame] | 660 | HRESULT hr = args->get_Message(message.GetAddressOf()); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 661 | if (FAILED(hr)) { |
| 662 | VLOG(1) << "get_Message failed: " << PrintHr(hr); |
| 663 | return hr; |
| 664 | } |
| 665 | |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 666 | WRL::ComPtr<IBuffer> buffer; |
robliao | 8d08e69 | 2017-05-11 10:14:00 -0700 | [diff] [blame] | 667 | hr = message->get_RawData(buffer.GetAddressOf()); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 668 | if (FAILED(hr)) { |
| 669 | VLOG(1) << "get_RawData failed: " << PrintHr(hr); |
| 670 | return hr; |
| 671 | } |
| 672 | |
| 673 | uint8_t* p_buffer_data = nullptr; |
junweifu | f51c5a0 | 2017-11-03 06:37:09 +0000 | [diff] [blame] | 674 | uint32_t data_length = 0; |
| 675 | hr = base::win::GetPointerToBufferData( |
| 676 | buffer.Get(), &p_buffer_data, &data_length); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 677 | if (FAILED(hr)) |
| 678 | return hr; |
| 679 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 680 | std::vector<uint8_t> data(p_buffer_data, |
| 681 | p_buffer_data + data_length); |
| 682 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 683 | task_service->PostBoundTask( |
| 684 | kComTaskRunner, |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 685 | base::BindOnce(&MidiInPortManager::OnMessageReceived, |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 686 | base::Unretained(port_manager), dev_id, data, |
| 687 | now)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 688 | |
| 689 | return S_OK; |
| 690 | }) |
| 691 | .Get(), |
| 692 | p_token); |
| 693 | if (FAILED(hr)) { |
| 694 | VLOG(1) << "add_MessageReceived failed: " << PrintHr(hr); |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | return true; |
| 699 | } |
| 700 | |
| 701 | void RemovePortEventHandlers(MidiPort<IMidiInPort>* port) override { |
| 702 | if (!(port->handle && |
| 703 | port->token_MessageReceived.value != kInvalidTokenValue)) |
| 704 | return; |
| 705 | |
| 706 | HRESULT hr = |
| 707 | port->handle->remove_MessageReceived(port->token_MessageReceived); |
| 708 | VLOG_IF(1, FAILED(hr)) << "remove_MessageReceived failed: " << PrintHr(hr); |
| 709 | port->token_MessageReceived.value = kInvalidTokenValue; |
| 710 | } |
| 711 | |
| 712 | void AddPort(MidiPortInfo info) final { midi_manager_->AddInputPort(info); } |
| 713 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 714 | void SetPortState(uint32_t port_index, PortState state) final { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 715 | midi_manager_->SetInputPortState(port_index, state); |
| 716 | } |
| 717 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 718 | // Callback on receiving MIDI input message. |
| 719 | void OnMessageReceived(std::string dev_id, |
| 720 | std::vector<uint8_t> data, |
| 721 | base::TimeTicks time) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 722 | DCHECK(midi_service_->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 723 | |
| 724 | MidiPort<IMidiInPort>* port = GetPortByDeviceId(dev_id); |
| 725 | CHECK(port); |
| 726 | |
| 727 | midi_manager_->ReceiveMidiData(port->index, &data[0], data.size(), time); |
| 728 | } |
| 729 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 730 | DISALLOW_COPY_AND_ASSIGN(MidiInPortManager); |
| 731 | }; |
| 732 | |
| 733 | class MidiManagerWinrt::MidiOutPortManager final |
| 734 | : public MidiPortManager<IMidiOutPort, |
| 735 | IMidiOutPort, |
| 736 | IMidiOutPortStatics, |
| 737 | RuntimeClass_Windows_Devices_Midi_MidiOutPort> { |
| 738 | public: |
| 739 | MidiOutPortManager(MidiManagerWinrt* midi_manager) |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 740 | : MidiPortManager(midi_manager) {} |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 741 | |
| 742 | private: |
| 743 | // MidiPortManager overrides: |
| 744 | void AddPort(MidiPortInfo info) final { midi_manager_->AddOutputPort(info); } |
| 745 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 746 | void SetPortState(uint32_t port_index, PortState state) final { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 747 | midi_manager_->SetOutputPortState(port_index, state); |
| 748 | } |
| 749 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 750 | DISALLOW_COPY_AND_ASSIGN(MidiOutPortManager); |
| 751 | }; |
| 752 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 753 | namespace { |
| 754 | |
| 755 | // FinalizeOnComRunner() run on kComTaskRunner even after the MidiManager |
| 756 | // instance destruction. |
| 757 | void FinalizeOnComRunner( |
| 758 | std::unique_ptr<MidiManagerWinrt::MidiInPortManager> port_manager_in, |
| 759 | std::unique_ptr<MidiManagerWinrt::MidiOutPortManager> port_manager_out) { |
| 760 | if (port_manager_in) |
| 761 | port_manager_in->StopWatcher(); |
| 762 | |
| 763 | if (port_manager_out) |
| 764 | port_manager_out->StopWatcher(); |
| 765 | } |
| 766 | |
| 767 | } // namespace |
| 768 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 769 | MidiManagerWinrt::MidiManagerWinrt(MidiService* service) |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 770 | : MidiManager(service) {} |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 771 | |
| 772 | MidiManagerWinrt::~MidiManagerWinrt() { |
| 773 | base::AutoLock auto_lock(lazy_init_member_lock_); |
| 774 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 775 | CHECK(!port_manager_in_); |
| 776 | CHECK(!port_manager_out_); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void MidiManagerWinrt::StartInitialization() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 780 | bool result = service()->task_service()->BindInstance(); |
| 781 | DCHECK(result); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 782 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 783 | service()->task_service()->PostBoundTask( |
| 784 | kComTaskRunner, base::BindOnce(&MidiManagerWinrt::InitializeOnComRunner, |
| 785 | base::Unretained(this))); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | void MidiManagerWinrt::Finalize() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 789 | // Unbind and take a lock to ensure that InitializeOnComRunner should not run |
| 790 | // after here. |
| 791 | bool result = service()->task_service()->UnbindInstance(); |
| 792 | DCHECK(result); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 793 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 794 | base::AutoLock auto_lock(lazy_init_member_lock_); |
| 795 | |
| 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 | |
| 802 | void MidiManagerWinrt::DispatchSendMidiData(MidiManagerClient* client, |
| 803 | uint32_t port_index, |
| 804 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame^] | 805 | base::TimeTicks timestamp) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 806 | base::TimeDelta delay = MidiService::TimestampToTimeDeltaDelay(timestamp); |
| 807 | service()->task_service()->PostBoundDelayedTask( |
| 808 | kComTaskRunner, |
| 809 | base::BindOnce(&MidiManagerWinrt::SendOnComRunner, base::Unretained(this), |
| 810 | port_index, data), |
| 811 | delay); |
| 812 | service()->task_service()->PostBoundDelayedTask( |
| 813 | kComTaskRunner, |
| 814 | base::BindOnce(&MidiManagerWinrt::AccumulateMidiBytesSent, |
| 815 | base::Unretained(this), client, data.size()), |
| 816 | delay); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 819 | void MidiManagerWinrt::InitializeOnComRunner() { |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 820 | base::AutoLock auto_lock(lazy_init_member_lock_); |
| 821 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 822 | DCHECK(service()->task_service()->IsOnTaskRunner(kComTaskRunner)); |
| 823 | |
Finnur Thorarinsson | ee8428f | 2017-09-30 23:36:49 +0000 | [diff] [blame] | 824 | bool preload_success = base::win::ResolveCoreWinRTDelayload() && |
| 825 | ScopedHString::ResolveCoreWinRTStringDelayload(); |
| 826 | if (!preload_success) { |
shaochuan | 9ff63b8 | 2016-09-01 01:58:44 -0700 | [diff] [blame] | 827 | CompleteInitialization(Result::INITIALIZATION_ERROR); |
| 828 | return; |
| 829 | } |
| 830 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 831 | port_manager_in_.reset(new MidiInPortManager(this)); |
| 832 | port_manager_out_.reset(new MidiOutPortManager(this)); |
| 833 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 834 | if (!(port_manager_in_->StartWatcher() && |
| 835 | port_manager_out_->StartWatcher())) { |
| 836 | port_manager_in_->StopWatcher(); |
| 837 | port_manager_out_->StopWatcher(); |
| 838 | CompleteInitialization(Result::INITIALIZATION_ERROR); |
| 839 | } |
| 840 | } |
| 841 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 842 | void MidiManagerWinrt::SendOnComRunner(uint32_t port_index, |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 843 | const std::vector<uint8_t>& data) { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 844 | DCHECK(service()->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 845 | |
| 846 | MidiPort<IMidiOutPort>* port = port_manager_out_->GetPortByIndex(port_index); |
| 847 | if (!(port && port->handle)) { |
| 848 | VLOG(1) << "Port not available: " << port_index; |
| 849 | return; |
| 850 | } |
| 851 | |
Robert Liao | 4a680c3 | 2017-10-18 19:10:01 +0000 | [diff] [blame] | 852 | WRL::ComPtr<IBuffer> buffer; |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 853 | HRESULT hr = base::win::CreateIBufferFromData( |
junweifu | f51c5a0 | 2017-11-03 06:37:09 +0000 | [diff] [blame] | 854 | data.data(), static_cast<UINT32>(data.size()), &buffer); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 855 | if (FAILED(hr)) { |
junweifu | a8cea85 | 2017-10-17 06:21:16 +0000 | [diff] [blame] | 856 | VLOG(1) << "CreateIBufferFromData failed: " << PrintHr(hr); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 857 | return; |
| 858 | } |
| 859 | |
robliao | 3566d1a | 2017-04-18 17:28:09 -0700 | [diff] [blame] | 860 | hr = port->handle->SendBuffer(buffer.Get()); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 861 | if (FAILED(hr)) { |
| 862 | VLOG(1) << "SendBuffer failed: " << PrintHr(hr); |
| 863 | return; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | void MidiManagerWinrt::OnPortManagerReady() { |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 868 | DCHECK(service()->task_service()->IsOnTaskRunner(kComTaskRunner)); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 869 | DCHECK(port_manager_ready_count_ < 2); |
| 870 | |
| 871 | if (++port_manager_ready_count_ == 2) |
| 872 | CompleteInitialization(Result::OK); |
| 873 | } |
| 874 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 875 | } // namespace midi |