Chromium Code Reviews| 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 NotifyPortAdded( | |
|
yhirano
2017/02/20 08:02:06
These two functions need not to be virtual.
Takashi Toyoshima
2017/02/20 16:51:59
Ah, right.
I did so for calling them from Port cla
| |
| 141 DynamicallyInitializedMidiManagerWin* manager) = 0; | |
| 142 | |
| 143 virtual void NotifyPortStateSet( | |
| 144 DynamicallyInitializedMidiManagerWin* manager) = 0; | |
| 145 | |
| 146 virtual void Open() { info_.state = mojom::PortState::OPENED; } | |
| 147 | |
| 148 protected: | |
| 149 size_t index_; | |
| 150 std::string type_; | |
| 151 uint32_t device_id_; | |
| 152 const uint16_t manufacturer_id_; | |
| 153 const uint16_t product_id_; | |
| 154 const uint32_t driver_version_; | |
| 155 const std::string product_name_; | |
| 156 MidiPortInfo info_; | |
| 157 }; // class Port | |
| 158 | |
| 159 } // namespace | |
| 160 | |
| 161 // TODO(toyoshim): Following patches will implement actual functions. | |
| 162 class DynamicallyInitializedMidiManagerWin::InPort final : public Port { | |
| 163 public: | |
| 164 InPort(UINT device_id, const MIDIINCAPS2W& caps) | |
| 165 : Port("input", | |
| 166 device_id, | |
| 167 caps.wMid, | |
| 168 caps.wPid, | |
| 169 caps.vDriverVersion, | |
| 170 base::WideToUTF8( | |
| 171 base::string16(caps.szPname, wcslen(caps.szPname)))) {} | |
| 172 | |
| 173 static std::vector<std::unique_ptr<InPort>> EnumerateActivePorts() { | |
| 174 std::vector<std::unique_ptr<InPort>> ports; | |
| 175 const UINT num_devices = midiInGetNumDevs(); | |
| 176 for (UINT device_id = 0; device_id < num_devices; ++device_id) { | |
| 177 MIDIINCAPS2W caps; | |
| 178 MMRESULT result = midiInGetDevCaps( | |
| 179 device_id, reinterpret_cast<LPMIDIINCAPSW>(&caps), sizeof(caps)); | |
| 180 if (result != MMSYSERR_NOERROR) { | |
| 181 LOG(ERROR) << "midiInGetDevCaps fails on device " << device_id; | |
| 182 continue; | |
| 183 } | |
| 184 ports.push_back(base::MakeUnique<InPort>(device_id, caps)); | |
| 185 } | |
| 186 return ports; | |
| 187 } | |
| 188 | |
| 189 // Port Overrides: | |
| 190 void NotifyPortStateSet( | |
| 191 DynamicallyInitializedMidiManagerWin* manager) override { | |
| 192 manager->PostReplyTask( | |
| 193 base::Bind(&DynamicallyInitializedMidiManagerWin::SetInputPortState, | |
| 194 base::Unretained(manager), index_, info_.state)); | |
| 195 } | |
| 196 | |
| 197 void NotifyPortAdded(DynamicallyInitializedMidiManagerWin* manager) override { | |
| 198 manager->PostReplyTask( | |
| 199 base::Bind(&DynamicallyInitializedMidiManagerWin::AddInputPort, | |
| 200 base::Unretained(manager), info_)); | |
| 201 } | |
| 202 }; | |
| 203 | |
| 204 // TODO(toyoshim): Following patches will implement actual functions. | |
| 205 class DynamicallyInitializedMidiManagerWin::OutPort final : public Port { | |
| 206 public: | |
| 207 OutPort(UINT device_id, const MIDIOUTCAPS2W& caps) | |
| 208 : Port("output", | |
| 209 device_id, | |
| 210 caps.wMid, | |
| 211 caps.wPid, | |
| 212 caps.vDriverVersion, | |
| 213 base::WideToUTF8( | |
| 214 base::string16(caps.szPname, wcslen(caps.szPname)))), | |
| 215 software_(caps.wTechnology == MOD_SWSYNTH) {} | |
| 216 | |
| 217 static std::vector<std::unique_ptr<OutPort>> EnumerateActivePorts() { | |
| 218 std::vector<std::unique_ptr<OutPort>> ports; | |
| 219 const UINT num_devices = midiOutGetNumDevs(); | |
| 220 for (UINT device_id = 0; device_id < num_devices; ++device_id) { | |
| 221 MIDIOUTCAPS2W caps; | |
| 222 MMRESULT result = midiOutGetDevCaps( | |
| 223 device_id, reinterpret_cast<LPMIDIOUTCAPSW>(&caps), sizeof(caps)); | |
| 224 if (result != MMSYSERR_NOERROR) { | |
| 225 LOG(ERROR) << "midiOutGetDevCaps fails on device " << device_id; | |
| 226 continue; | |
| 227 } | |
| 228 ports.push_back(base::MakeUnique<OutPort>(device_id, caps)); | |
| 229 } | |
| 230 return ports; | |
| 231 } | |
| 232 | |
| 233 // Port overrides: | |
| 234 bool Connect() override { | |
| 235 // Until |software| option is supported, disable Microsoft GS Wavetable | |
| 236 // Synth that has a known security issue. | |
| 237 if (software_ && manufacturer_id_ == MM_MICROSOFT && | |
| 238 (product_id_ == MM_MSFT_WDMAUDIO_MIDIOUT || | |
| 239 product_id_ == MM_MSFT_GENERIC_MIDISYNTH)) { | |
| 240 return false; | |
| 241 } | |
| 242 return Port::Connect(); | |
| 243 } | |
| 244 | |
| 245 // Port Overrides: | |
| 246 void NotifyPortStateSet( | |
| 247 DynamicallyInitializedMidiManagerWin* manager) override { | |
| 248 manager->PostReplyTask( | |
| 249 base::Bind(&DynamicallyInitializedMidiManagerWin::SetOutputPortState, | |
| 250 base::Unretained(manager), index_, info_.state)); | |
| 251 } | |
| 252 | |
| 253 void NotifyPortAdded(DynamicallyInitializedMidiManagerWin* manager) override { | |
| 254 manager->PostReplyTask( | |
| 255 base::Bind(&DynamicallyInitializedMidiManagerWin::AddOutputPort, | |
| 256 base::Unretained(manager), info_)); | |
| 257 } | |
| 258 | |
| 259 const bool software_; | |
| 260 }; | |
| 261 | |
| 262 DynamicallyInitializedMidiManagerWin::DynamicallyInitializedMidiManagerWin( | |
| 263 MidiService* service) | |
| 264 : MidiManager(service), instance_id_(IssueNextInstanceId()) { | |
| 265 base::AutoLock lock(*GetInstanceIdLock()); | |
| 266 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); | |
| 267 | |
| 268 // Obtains the task runner for the current thread that hosts this instnace. | |
| 269 thread_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 270 } | |
| 271 | |
| 272 DynamicallyInitializedMidiManagerWin::~DynamicallyInitializedMidiManagerWin() { | |
| 273 base::AutoLock lock(*GetInstanceIdLock()); | |
| 274 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); | |
| 275 CHECK(thread_runner_->BelongsToCurrentThread()); | |
| 276 } | |
| 277 | |
| 278 void DynamicallyInitializedMidiManagerWin::PostReplyTask( | |
| 279 const base::Closure& task) { | |
| 280 thread_runner_->PostTask(FROM_HERE, base::Bind(&RunTask, instance_id_, task)); | |
| 281 } | |
| 282 | |
| 283 void DynamicallyInitializedMidiManagerWin::StartInitialization() { | |
| 284 { | |
| 285 base::AutoLock lock(*GetInstanceIdLock()); | |
| 286 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); | |
| 287 g_active_instance_id = instance_id_; | |
| 288 CHECK_EQ(nullptr, g_manager_instance); | |
| 289 g_manager_instance = this; | |
| 290 } | |
| 291 // Registers on the I/O thread to be notified on the I/O thread. | |
| 292 CHECK(thread_runner_->BelongsToCurrentThread()); | |
| 293 base::SystemMonitor::Get()->AddDevicesChangedObserver(this); | |
| 294 | |
| 295 // Starts asynchronous initialization on TaskRunner. | |
| 296 PostTask( | |
| 297 base::Bind(&DynamicallyInitializedMidiManagerWin::InitializeOnTaskRunner, | |
| 298 base::Unretained(this))); | |
| 299 } | |
| 300 | |
| 301 void DynamicallyInitializedMidiManagerWin::Finalize() { | |
| 302 // Unregisters on the I/O thread. OnDevicesChanged() won't be called any more. | |
| 303 CHECK(thread_runner_->BelongsToCurrentThread()); | |
| 304 base::SystemMonitor::Get()->RemoveDevicesChangedObserver(this); | |
| 305 { | |
| 306 base::AutoLock lock(*GetInstanceIdLock()); | |
| 307 CHECK_EQ(instance_id_, g_active_instance_id); | |
| 308 g_active_instance_id = kInvalidInstanceId; | |
| 309 CHECK_EQ(this, g_manager_instance); | |
| 310 g_manager_instance = nullptr; | |
| 311 } | |
| 312 | |
| 313 // Ensures that no task runs on TaskRunner so to destruct the instance safely. | |
| 314 // Tasks that did not started yet will do nothing after invalidate the | |
| 315 // instance ID above. | |
| 316 base::AutoLock lock(*GetTaskLock()); | |
| 317 } | |
| 318 | |
| 319 void DynamicallyInitializedMidiManagerWin::DispatchSendMidiData( | |
| 320 MidiManagerClient* client, | |
| 321 uint32_t port_index, | |
| 322 const std::vector<uint8_t>& data, | |
| 323 double timestamp) { | |
| 324 // TODO(toyoshim): Following patches will implement. | |
| 325 } | |
| 326 | |
| 327 void DynamicallyInitializedMidiManagerWin::OnDevicesChanged( | |
| 328 base::SystemMonitor::DeviceType device_type) { | |
| 329 // Notified on the I/O thread. | |
| 330 CHECK(thread_runner_->BelongsToCurrentThread()); | |
| 331 | |
| 332 switch (device_type) { | |
| 333 case base::SystemMonitor::DEVTYPE_AUDIO: | |
| 334 case base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE: | |
| 335 // Add case of other unrelated device types here. | |
| 336 return; | |
| 337 case base::SystemMonitor::DEVTYPE_UNKNOWN: { | |
| 338 PostTask(base::Bind( | |
| 339 &DynamicallyInitializedMidiManagerWin::UpdateDeviceListOnTaskRunner, | |
| 340 base::Unretained(this))); | |
| 341 break; | |
| 342 } | |
| 343 } | |
| 344 } | |
| 345 | |
| 346 void DynamicallyInitializedMidiManagerWin::PostTask(const base::Closure& task) { | |
| 347 service() | |
| 348 ->GetTaskRunner(kTaskRunner) | |
| 349 ->PostTask(FROM_HERE, base::Bind(&RunTask, instance_id_, task)); | |
| 350 } | |
| 351 | |
| 352 void DynamicallyInitializedMidiManagerWin::InitializeOnTaskRunner() { | |
| 353 UpdateDeviceListOnTaskRunner(); | |
| 354 PostReplyTask( | |
| 355 base::Bind(&DynamicallyInitializedMidiManagerWin::CompleteInitialization, | |
| 356 base::Unretained(this), mojom::Result::OK)); | |
| 357 } | |
| 358 | |
| 359 void DynamicallyInitializedMidiManagerWin::UpdateDeviceListOnTaskRunner() { | |
| 360 std::vector<std::unique_ptr<InPort>> active_input_ports = | |
| 361 InPort::EnumerateActivePorts(); | |
| 362 ReflectActiveDeviceList(this, &input_ports_, &active_input_ports); | |
| 363 | |
| 364 std::vector<std::unique_ptr<OutPort>> active_output_ports = | |
| 365 OutPort::EnumerateActivePorts(); | |
| 366 ReflectActiveDeviceList(this, &output_ports_, &active_output_ports); | |
| 367 | |
| 368 // TODO(toyoshim): This method may run before internal MIDI device lists that | |
| 369 // Windows manages were updated. This may be because MIDI driver may be loaded | |
| 370 // after the raw device list was updated. To avoid this problem, we may want | |
| 371 // to retry device check later if no changes are detected here. | |
| 372 } | |
| 373 | |
| 374 template <typename T> | |
| 375 void DynamicallyInitializedMidiManagerWin::ReflectActiveDeviceList( | |
| 376 DynamicallyInitializedMidiManagerWin* manager, | |
| 377 T* known_ports, | |
| 378 T* active_ports) { | |
| 379 // Update existing port states. | |
| 380 for (const auto& port : *known_ports) { | |
| 381 const auto& it = std::find_if( | |
| 382 active_ports->begin(), active_ports->end(), | |
| 383 [&port](const auto& candidate) { return *candidate == *port; }); | |
| 384 if (it == active_ports->end()) { | |
| 385 if (port->Disconnect()) | |
| 386 port->NotifyPortStateSet(this); | |
| 387 } else { | |
| 388 port->set_device_id((*it)->device_id()); | |
| 389 if (port->Connect()) | |
| 390 port->NotifyPortStateSet(this); | |
| 391 } | |
| 392 } | |
| 393 | |
|
Takashi Toyoshima
2017/02/17 17:42:29
NotifyPortStateSet() or NotifyPortAdded() are call
| |
| 394 // Find new ports from active ports and append them to known ports. | |
| 395 for (auto& port : *active_ports) { | |
| 396 if (std::find_if(known_ports->begin(), known_ports->end(), | |
| 397 [&port](const auto& candidate) { | |
| 398 return *candidate == *port; | |
| 399 }) == known_ports->end()) { | |
| 400 size_t index = known_ports->size(); | |
| 401 port->set_index(index); | |
| 402 known_ports->push_back(std::move(port)); | |
| 403 (*known_ports)[index]->Connect(); | |
| 404 (*known_ports)[index]->NotifyPortAdded(this); | |
| 405 } | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 } // namespace midi | |
| OLD | NEW |