OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/dynamically_initialized_midi_manager_win.h" |
| 6 |
| 7 #include <windows.h> |
| 8 |
| 9 #include <mmreg.h> |
| 10 #include <mmsystem.h> |
| 11 |
| 12 #include <algorithm> |
| 13 #include <string> |
| 14 |
| 15 #include "base/callback.h" |
| 16 #include "base/logging.h" |
| 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/strings/string16.h" |
| 19 #include "base/strings/stringprintf.h" |
| 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/synchronization/lock.h" |
| 22 #include "media/midi/midi_port_info.h" |
| 23 #include "media/midi/midi_service.h" |
| 24 |
| 25 namespace midi { |
| 26 |
| 27 namespace { |
| 28 |
| 29 // Global variables to identify MidiManager instance. |
| 30 constexpr int kInvalidInstanceId = -1; |
| 31 int g_active_instance_id = kInvalidInstanceId; |
| 32 DynamicallyInitializedMidiManagerWin* g_manager_instance = nullptr; |
| 33 |
| 34 // Obtains base::Lock instance pointer to lock instance_id. |
| 35 base::Lock* GetInstanceIdLock() { |
| 36 static base::Lock* lock = new base::Lock; |
| 37 return lock; |
| 38 } |
| 39 |
| 40 // Issues unique MidiManager instance ID. |
| 41 int IssueNextInstanceId() { |
| 42 static int id = kInvalidInstanceId; |
| 43 return ++id; |
| 44 } |
| 45 |
| 46 // Use single TaskRunner for all tasks running outside the I/O thread. |
| 47 constexpr int kTaskRunner = 0; |
| 48 |
| 49 // Obtains base::Lock instance pointer to ensure tasks run safely on TaskRunner. |
| 50 base::Lock* GetTaskLock() { |
| 51 static base::Lock* lock = new base::Lock; |
| 52 return lock; |
| 53 } |
| 54 |
| 55 // Helper function to run a posted task on TaskRunner safely. |
| 56 void RunTask(int instance_id, const base::Closure& task) { |
| 57 // Obtains task lock to ensure that the instance should not complete |
| 58 // Finalize() while running the |task|. |
| 59 base::AutoLock task_lock(*GetTaskLock()); |
| 60 { |
| 61 // If Finalize() finished before the lock avobe, do nothing. |
| 62 base::AutoLock lock(*GetInstanceIdLock()); |
| 63 if (instance_id != g_active_instance_id) |
| 64 return; |
| 65 } |
| 66 task.Run(); |
| 67 } |
| 68 |
| 69 // TODO(toyoshim): Factor out TaskRunner related functionaliries above, and |
| 70 // deprecate MidiScheduler. It should be available via MidiManager::scheduler(). |
| 71 |
| 72 class Port { |
| 73 public: |
| 74 Port(const std::string& type, |
| 75 uint32_t device_id, |
| 76 uint16_t manufacturer_id, |
| 77 uint16_t product_id, |
| 78 uint32_t driver_version, |
| 79 const std::string& product_name) |
| 80 : index_(0u), |
| 81 type_(type), |
| 82 device_id_(device_id), |
| 83 manufacturer_id_(manufacturer_id), |
| 84 product_id_(product_id), |
| 85 driver_version_(driver_version), |
| 86 product_name_(product_name) { |
| 87 info_.manufacturer = "unknown"; // TODO(toyoshim): Use USB information. |
| 88 info_.name = product_name_; |
| 89 info_.version = base::StringPrintf("%d.%d", HIBYTE(driver_version_), |
| 90 LOBYTE(driver_version_)); |
| 91 info_.state = mojom::PortState::DISCONNECTED; |
| 92 } |
| 93 |
| 94 virtual ~Port() {} |
| 95 |
| 96 bool operator==(const Port& other) const { |
| 97 // Should not use |device_id| for comparison because it can be changed on |
| 98 // each enumeration. |
| 99 // Since the GUID will be changed on each enumeration for Microsoft GS |
| 100 // Wavetable synth and might be done for others, do not use it for device |
| 101 // comparison. |
| 102 return manufacturer_id_ == other.manufacturer_id_ && |
| 103 product_id_ == other.product_id_ && |
| 104 driver_version_ == other.driver_version_ && |
| 105 product_name_ == other.product_name_; |
| 106 } |
| 107 |
| 108 bool IsConnected() const { |
| 109 return info_.state != mojom::PortState::DISCONNECTED; |
| 110 } |
| 111 |
| 112 void set_index(size_t index) { |
| 113 index_ = index; |
| 114 // TODO(toyoshim): Use hashed ID. |
| 115 info_.id = base::StringPrintf("%s-%d", type_.c_str(), index_); |
| 116 } |
| 117 size_t index() { return index_; } |
| 118 void set_device_id(uint32_t device_id) { device_id_ = device_id; } |
| 119 uint32_t device_id() { return device_id_; } |
| 120 const MidiPortInfo& info() { return info_; } |
| 121 |
| 122 virtual bool Connect() { |
| 123 if (info_.state != mojom::PortState::DISCONNECTED) |
| 124 return false; |
| 125 |
| 126 info_.state = mojom::PortState::CONNECTED; |
| 127 // TODO(toyoshim) Until open() / close() are supported, open each device on |
| 128 // connected. |
| 129 Open(); |
| 130 return true; |
| 131 } |
| 132 |
| 133 virtual bool Disconnect() { |
| 134 if (info_.state == mojom::PortState::DISCONNECTED) |
| 135 return false; |
| 136 info_.state = mojom::PortState::DISCONNECTED; |
| 137 return true; |
| 138 } |
| 139 |
| 140 virtual void Open() { info_.state = mojom::PortState::OPENED; } |
| 141 |
| 142 protected: |
| 143 size_t index_; |
| 144 std::string type_; |
| 145 uint32_t device_id_; |
| 146 const uint16_t manufacturer_id_; |
| 147 const uint16_t product_id_; |
| 148 const uint32_t driver_version_; |
| 149 const std::string product_name_; |
| 150 MidiPortInfo info_; |
| 151 }; // class Port |
| 152 |
| 153 } // namespace |
| 154 |
| 155 // TODO(toyoshim): Following patches will implement actual functions. |
| 156 class DynamicallyInitializedMidiManagerWin::InPort final : public Port { |
| 157 public: |
| 158 InPort(UINT device_id, const MIDIINCAPS2W& caps) |
| 159 : Port("input", |
| 160 device_id, |
| 161 caps.wMid, |
| 162 caps.wPid, |
| 163 caps.vDriverVersion, |
| 164 base::WideToUTF8( |
| 165 base::string16(caps.szPname, wcslen(caps.szPname)))) {} |
| 166 |
| 167 static std::vector<std::unique_ptr<InPort>> EnumerateActivePorts() { |
| 168 std::vector<std::unique_ptr<InPort>> ports; |
| 169 const UINT num_devices = midiInGetNumDevs(); |
| 170 for (UINT device_id = 0; device_id < num_devices; ++device_id) { |
| 171 MIDIINCAPS2W caps; |
| 172 MMRESULT result = midiInGetDevCaps( |
| 173 device_id, reinterpret_cast<LPMIDIINCAPSW>(&caps), sizeof(caps)); |
| 174 if (result != MMSYSERR_NOERROR) { |
| 175 LOG(ERROR) << "midiInGetDevCaps fails on device " << device_id; |
| 176 continue; |
| 177 } |
| 178 ports.push_back(base::MakeUnique<InPort>(device_id, caps)); |
| 179 } |
| 180 return ports; |
| 181 } |
| 182 |
| 183 void NotifyPortStateSet(DynamicallyInitializedMidiManagerWin* manager) { |
| 184 manager->PostReplyTask( |
| 185 base::Bind(&DynamicallyInitializedMidiManagerWin::SetInputPortState, |
| 186 base::Unretained(manager), index_, info_.state)); |
| 187 } |
| 188 |
| 189 void NotifyPortAdded(DynamicallyInitializedMidiManagerWin* manager) { |
| 190 manager->PostReplyTask( |
| 191 base::Bind(&DynamicallyInitializedMidiManagerWin::AddInputPort, |
| 192 base::Unretained(manager), info_)); |
| 193 } |
| 194 }; |
| 195 |
| 196 // TODO(toyoshim): Following patches will implement actual functions. |
| 197 class DynamicallyInitializedMidiManagerWin::OutPort final : public Port { |
| 198 public: |
| 199 OutPort(UINT device_id, const MIDIOUTCAPS2W& caps) |
| 200 : Port("output", |
| 201 device_id, |
| 202 caps.wMid, |
| 203 caps.wPid, |
| 204 caps.vDriverVersion, |
| 205 base::WideToUTF8( |
| 206 base::string16(caps.szPname, wcslen(caps.szPname)))), |
| 207 software_(caps.wTechnology == MOD_SWSYNTH) {} |
| 208 |
| 209 static std::vector<std::unique_ptr<OutPort>> EnumerateActivePorts() { |
| 210 std::vector<std::unique_ptr<OutPort>> ports; |
| 211 const UINT num_devices = midiOutGetNumDevs(); |
| 212 for (UINT device_id = 0; device_id < num_devices; ++device_id) { |
| 213 MIDIOUTCAPS2W caps; |
| 214 MMRESULT result = midiOutGetDevCaps( |
| 215 device_id, reinterpret_cast<LPMIDIOUTCAPSW>(&caps), sizeof(caps)); |
| 216 if (result != MMSYSERR_NOERROR) { |
| 217 LOG(ERROR) << "midiOutGetDevCaps fails on device " << device_id; |
| 218 continue; |
| 219 } |
| 220 ports.push_back(base::MakeUnique<OutPort>(device_id, caps)); |
| 221 } |
| 222 return ports; |
| 223 } |
| 224 |
| 225 // Port overrides: |
| 226 bool Connect() override { |
| 227 // Until |software| option is supported, disable Microsoft GS Wavetable |
| 228 // Synth that has a known security issue. |
| 229 if (software_ && manufacturer_id_ == MM_MICROSOFT && |
| 230 (product_id_ == MM_MSFT_WDMAUDIO_MIDIOUT || |
| 231 product_id_ == MM_MSFT_GENERIC_MIDISYNTH)) { |
| 232 return false; |
| 233 } |
| 234 return Port::Connect(); |
| 235 } |
| 236 |
| 237 // Port Overrides: |
| 238 void NotifyPortStateSet(DynamicallyInitializedMidiManagerWin* manager) { |
| 239 manager->PostReplyTask( |
| 240 base::Bind(&DynamicallyInitializedMidiManagerWin::SetOutputPortState, |
| 241 base::Unretained(manager), index_, info_.state)); |
| 242 } |
| 243 |
| 244 void NotifyPortAdded(DynamicallyInitializedMidiManagerWin* manager) { |
| 245 manager->PostReplyTask( |
| 246 base::Bind(&DynamicallyInitializedMidiManagerWin::AddOutputPort, |
| 247 base::Unretained(manager), info_)); |
| 248 } |
| 249 |
| 250 const bool software_; |
| 251 }; |
| 252 |
| 253 DynamicallyInitializedMidiManagerWin::DynamicallyInitializedMidiManagerWin( |
| 254 MidiService* service) |
| 255 : MidiManager(service), instance_id_(IssueNextInstanceId()) { |
| 256 base::AutoLock lock(*GetInstanceIdLock()); |
| 257 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); |
| 258 |
| 259 // Obtains the task runner for the current thread that hosts this instnace. |
| 260 thread_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 261 } |
| 262 |
| 263 DynamicallyInitializedMidiManagerWin::~DynamicallyInitializedMidiManagerWin() { |
| 264 base::AutoLock lock(*GetInstanceIdLock()); |
| 265 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); |
| 266 CHECK(thread_runner_->BelongsToCurrentThread()); |
| 267 } |
| 268 |
| 269 void DynamicallyInitializedMidiManagerWin::PostReplyTask( |
| 270 const base::Closure& task) { |
| 271 thread_runner_->PostTask(FROM_HERE, base::Bind(&RunTask, instance_id_, task)); |
| 272 } |
| 273 |
| 274 void DynamicallyInitializedMidiManagerWin::StartInitialization() { |
| 275 { |
| 276 base::AutoLock lock(*GetInstanceIdLock()); |
| 277 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); |
| 278 g_active_instance_id = instance_id_; |
| 279 CHECK_EQ(nullptr, g_manager_instance); |
| 280 g_manager_instance = this; |
| 281 } |
| 282 // Registers on the I/O thread to be notified on the I/O thread. |
| 283 CHECK(thread_runner_->BelongsToCurrentThread()); |
| 284 base::SystemMonitor::Get()->AddDevicesChangedObserver(this); |
| 285 |
| 286 // Starts asynchronous initialization on TaskRunner. |
| 287 PostTask( |
| 288 base::Bind(&DynamicallyInitializedMidiManagerWin::InitializeOnTaskRunner, |
| 289 base::Unretained(this))); |
| 290 } |
| 291 |
| 292 void DynamicallyInitializedMidiManagerWin::Finalize() { |
| 293 // Unregisters on the I/O thread. OnDevicesChanged() won't be called any more. |
| 294 CHECK(thread_runner_->BelongsToCurrentThread()); |
| 295 base::SystemMonitor::Get()->RemoveDevicesChangedObserver(this); |
| 296 { |
| 297 base::AutoLock lock(*GetInstanceIdLock()); |
| 298 CHECK_EQ(instance_id_, g_active_instance_id); |
| 299 g_active_instance_id = kInvalidInstanceId; |
| 300 CHECK_EQ(this, g_manager_instance); |
| 301 g_manager_instance = nullptr; |
| 302 } |
| 303 |
| 304 // Ensures that no task runs on TaskRunner so to destruct the instance safely. |
| 305 // Tasks that did not started yet will do nothing after invalidate the |
| 306 // instance ID above. |
| 307 base::AutoLock lock(*GetTaskLock()); |
| 308 } |
| 309 |
| 310 void DynamicallyInitializedMidiManagerWin::DispatchSendMidiData( |
| 311 MidiManagerClient* client, |
| 312 uint32_t port_index, |
| 313 const std::vector<uint8_t>& data, |
| 314 double timestamp) { |
| 315 // TODO(toyoshim): Following patches will implement. |
| 316 } |
| 317 |
| 318 void DynamicallyInitializedMidiManagerWin::OnDevicesChanged( |
| 319 base::SystemMonitor::DeviceType device_type) { |
| 320 // Notified on the I/O thread. |
| 321 CHECK(thread_runner_->BelongsToCurrentThread()); |
| 322 |
| 323 switch (device_type) { |
| 324 case base::SystemMonitor::DEVTYPE_AUDIO: |
| 325 case base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE: |
| 326 // Add case of other unrelated device types here. |
| 327 return; |
| 328 case base::SystemMonitor::DEVTYPE_UNKNOWN: { |
| 329 PostTask(base::Bind( |
| 330 &DynamicallyInitializedMidiManagerWin::UpdateDeviceListOnTaskRunner, |
| 331 base::Unretained(this))); |
| 332 break; |
| 333 } |
| 334 } |
| 335 } |
| 336 |
| 337 void DynamicallyInitializedMidiManagerWin::PostTask(const base::Closure& task) { |
| 338 service() |
| 339 ->GetTaskRunner(kTaskRunner) |
| 340 ->PostTask(FROM_HERE, base::Bind(&RunTask, instance_id_, task)); |
| 341 } |
| 342 |
| 343 void DynamicallyInitializedMidiManagerWin::InitializeOnTaskRunner() { |
| 344 UpdateDeviceListOnTaskRunner(); |
| 345 PostReplyTask( |
| 346 base::Bind(&DynamicallyInitializedMidiManagerWin::CompleteInitialization, |
| 347 base::Unretained(this), mojom::Result::OK)); |
| 348 } |
| 349 |
| 350 void DynamicallyInitializedMidiManagerWin::UpdateDeviceListOnTaskRunner() { |
| 351 std::vector<std::unique_ptr<InPort>> active_input_ports = |
| 352 InPort::EnumerateActivePorts(); |
| 353 ReflectActiveDeviceList(this, &input_ports_, &active_input_ports); |
| 354 |
| 355 std::vector<std::unique_ptr<OutPort>> active_output_ports = |
| 356 OutPort::EnumerateActivePorts(); |
| 357 ReflectActiveDeviceList(this, &output_ports_, &active_output_ports); |
| 358 |
| 359 // TODO(toyoshim): This method may run before internal MIDI device lists that |
| 360 // Windows manages were updated. This may be because MIDI driver may be loaded |
| 361 // after the raw device list was updated. To avoid this problem, we may want |
| 362 // to retry device check later if no changes are detected here. |
| 363 } |
| 364 |
| 365 template <typename T> |
| 366 void DynamicallyInitializedMidiManagerWin::ReflectActiveDeviceList( |
| 367 DynamicallyInitializedMidiManagerWin* manager, |
| 368 std::vector<T>* known_ports, |
| 369 std::vector<T>* active_ports) { |
| 370 // Update existing port states. |
| 371 for (const auto& port : *known_ports) { |
| 372 const auto& it = std::find_if( |
| 373 active_ports->begin(), active_ports->end(), |
| 374 [&port](const auto& candidate) { return *candidate == *port; }); |
| 375 if (it == active_ports->end()) { |
| 376 if (port->Disconnect()) |
| 377 port->NotifyPortStateSet(this); |
| 378 } else { |
| 379 port->set_device_id((*it)->device_id()); |
| 380 if (port->Connect()) |
| 381 port->NotifyPortStateSet(this); |
| 382 } |
| 383 } |
| 384 |
| 385 // Find new ports from active ports and append them to known ports. |
| 386 for (auto& port : *active_ports) { |
| 387 if (std::find_if(known_ports->begin(), known_ports->end(), |
| 388 [&port](const auto& candidate) { |
| 389 return *candidate == *port; |
| 390 }) == known_ports->end()) { |
| 391 size_t index = known_ports->size(); |
| 392 port->set_index(index); |
| 393 known_ports->push_back(std::move(port)); |
| 394 (*known_ports)[index]->Connect(); |
| 395 (*known_ports)[index]->NotifyPortAdded(this); |
| 396 } |
| 397 } |
| 398 } |
| 399 |
| 400 } // namespace midi |
OLD | NEW |