blob: e543077db38e7ad7bbdddd181eb056115b908346 [file] [log] [blame]
toyoshimd28b59c2017-02-20 11:07:37 -08001// 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
toyoshim63e32a52017-04-25 07:20:10 -07005#include "media/midi/midi_manager_win.h"
toyoshimd28b59c2017-02-20 11:07:37 -08006
7#include <windows.h>
8
toyoshim6ebf1182017-03-01 00:40:31 -08009#include <ks.h>
10#include <ksmedia.h>
toyoshimd28b59c2017-02-20 11:07:37 -080011#include <mmreg.h>
12#include <mmsystem.h>
13
14#include <algorithm>
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000015#include <limits>
toyoshimd28b59c2017-02-20 11:07:37 -080016#include <string>
17
Sebastien Marchand2912d9f2019-01-25 16:49:37 +000018#include "base/bind.h"
toyoshim8a5ad422017-02-28 21:16:18 -080019#include "base/bind_helpers.h"
toyoshimd28b59c2017-02-20 11:07:37 -080020#include "base/callback.h"
21#include "base/logging.h"
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000022#include "base/optional.h"
Gabriel Charette78f94a02017-05-16 14:03:45 -040023#include "base/single_thread_task_runner.h"
Avi Drissman2c637192018-12-25 20:26:39 +000024#include "base/stl_util.h"
toyoshimd28b59c2017-02-20 11:07:37 -080025#include "base/strings/string16.h"
26#include "base/strings/stringprintf.h"
27#include "base/strings/utf_string_conversions.h"
28#include "base/synchronization/lock.h"
toyoshim15385b32017-04-24 23:52:01 -070029#include "base/win/windows_version.h"
toyoshim6ebf1182017-03-01 00:40:31 -080030#include "device/usb/usb_ids.h"
toyoshim8a5ad422017-02-28 21:16:18 -080031#include "media/midi/message_util.h"
toyoshim15385b32017-04-24 23:52:01 -070032#include "media/midi/midi_manager_winrt.h"
toyoshimd28b59c2017-02-20 11:07:37 -080033#include "media/midi/midi_service.h"
Adithya Srinivasan33252732018-10-17 15:59:40 +000034#include "media/midi/midi_service.mojom.h"
toyoshim15385b32017-04-24 23:52:01 -070035#include "media/midi/midi_switches.h"
toyoshimd28b59c2017-02-20 11:07:37 -080036
37namespace midi {
38
toyoshim8a5ad422017-02-28 21:16:18 -080039// Forward declaration of PortManager for anonymous functions and internal
40// classes to use it.
toyoshim63e32a52017-04-25 07:20:10 -070041class MidiManagerWin::PortManager {
toyoshim8a5ad422017-02-28 21:16:18 -080042 public:
43 // Calculates event time from elapsed time that system provides.
44 base::TimeTicks CalculateInEventTime(size_t index, uint32_t elapsed_ms) const;
45
46 // Registers HMIDIIN handle to resolve port index.
47 void RegisterInHandle(HMIDIIN handle, size_t index);
48
49 // Unregisters HMIDIIN handle.
50 void UnregisterInHandle(HMIDIIN handle);
51
52 // Finds HMIDIIN handle and fullfil |out_index| with the port index.
toyoshim6d87aaa2017-02-28 22:36:44 -080053 bool FindInHandle(HMIDIIN hmi, size_t* out_index);
toyoshim8a5ad422017-02-28 21:16:18 -080054
55 // Restores used input buffer for the next data receive.
56 void RestoreInBuffer(size_t index);
57
58 // Ports accessors.
59 std::vector<std::unique_ptr<InPort>>* inputs() { return &input_ports_; }
60 std::vector<std::unique_ptr<OutPort>>* outputs() { return &output_ports_; }
61
62 // Handles MIDI input port callbacks that runs on a system provided thread.
63 static void CALLBACK HandleMidiInCallback(HMIDIIN hmi,
64 UINT msg,
65 DWORD_PTR instance,
66 DWORD_PTR param1,
67 DWORD_PTR param2);
68
toyoshim6d87aaa2017-02-28 22:36:44 -080069 // Handles MIDI output port callbacks that runs on a system provided thread.
70 static void CALLBACK HandleMidiOutCallback(HMIDIOUT hmo,
71 UINT msg,
72 DWORD_PTR instance,
73 DWORD_PTR param1,
74 DWORD_PTR param2);
75
toyoshim8a5ad422017-02-28 21:16:18 -080076 private:
77 // Holds all MIDI input or output ports connected once.
78 std::vector<std::unique_ptr<InPort>> input_ports_;
79 std::vector<std::unique_ptr<OutPort>> output_ports_;
80
81 // Map to resolve MIDI input port index from HMIDIIN.
82 std::map<HMIDIIN, size_t> hmidiin_to_index_map_;
83};
84
toyoshimd28b59c2017-02-20 11:07:37 -080085namespace {
86
toyoshimc32dd892017-02-24 02:13:14 -080087// Assumes that nullptr represents an invalid MIDI handle.
88constexpr HMIDIIN kInvalidInHandle = nullptr;
89constexpr HMIDIOUT kInvalidOutHandle = nullptr;
90
toyoshim6d87aaa2017-02-28 22:36:44 -080091// Defines SysEx message size limit.
92// TODO(crbug.com/383578): This restriction should be removed once Web MIDI
93// defines a standardized way to handle large sysex messages.
94// Note for built-in USB-MIDI driver:
95// From an observation on Windows 7/8.1 with a USB-MIDI keyboard,
96// midiOutLongMsg() will be always blocked. Sending 64 bytes or less data takes
97// roughly 300 usecs. Sending 2048 bytes or more data takes roughly
98// |message.size() / (75 * 1024)| secs in practice. Here we put 256 KB size
99// limit on SysEx message, with hoping that midiOutLongMsg will be blocked at
100// most 4 sec or so with a typical USB-MIDI device.
101// TODO(toyoshim): Consider to use linked small buffers so that midiOutReset()
102// can abort sending unhandled following buffers.
103constexpr size_t kSysExSizeLimit = 256 * 1024;
104
toyoshim8a5ad422017-02-28 21:16:18 -0800105// Defines input buffer size.
106constexpr size_t kBufferLength = 32 * 1024;
107
toyoshimd28b59c2017-02-20 11:07:37 -0800108// Global variables to identify MidiManager instance.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000109constexpr int64_t kInvalidInstanceId = -1;
110int64_t g_active_instance_id = kInvalidInstanceId;
toyoshim63e32a52017-04-25 07:20:10 -0700111MidiManagerWin* g_manager_instance = nullptr;
toyoshimd28b59c2017-02-20 11:07:37 -0800112
113// Obtains base::Lock instance pointer to lock instance_id.
114base::Lock* GetInstanceIdLock() {
115 static base::Lock* lock = new base::Lock;
116 return lock;
117}
118
119// Issues unique MidiManager instance ID.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000120int64_t IssueNextInstanceId(base::Optional<int64_t> override_id) {
121 static int64_t id = kInvalidInstanceId;
122 if (override_id) {
123 int64_t result = ++id;
124 id = *override_id;
125 return result;
126 }
127 if (id == std::numeric_limits<int64_t>::max())
128 return kInvalidInstanceId;
toyoshimd28b59c2017-02-20 11:07:37 -0800129 return ++id;
130}
131
132// Use single TaskRunner for all tasks running outside the I/O thread.
133constexpr int kTaskRunner = 0;
134
135// Obtains base::Lock instance pointer to ensure tasks run safely on TaskRunner.
toyoshimc32dd892017-02-24 02:13:14 -0800136// Since all tasks on TaskRunner run behind a lock of *GetTaskLock(), we can
137// access all members even on the I/O thread if a lock of *GetTaskLock() is
138// obtained.
toyoshimd28b59c2017-02-20 11:07:37 -0800139base::Lock* GetTaskLock() {
140 static base::Lock* lock = new base::Lock;
141 return lock;
142}
143
144// Helper function to run a posted task on TaskRunner safely.
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000145void RunTask(int instance_id, base::OnceClosure task) {
toyoshimd28b59c2017-02-20 11:07:37 -0800146 // Obtains task lock to ensure that the instance should not complete
147 // Finalize() while running the |task|.
148 base::AutoLock task_lock(*GetTaskLock());
149 {
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000150 // If destructor finished before the lock avobe, do nothing.
toyoshimd28b59c2017-02-20 11:07:37 -0800151 base::AutoLock lock(*GetInstanceIdLock());
152 if (instance_id != g_active_instance_id)
153 return;
154 }
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000155 std::move(task).Run();
toyoshimd28b59c2017-02-20 11:07:37 -0800156}
157
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000158// TODO(toyoshim): Use midi::TaskService and deprecate its prototype
159// implementation above that is still used in this MidiManagerWin class.
toyoshimd28b59c2017-02-20 11:07:37 -0800160
Takashi Toyoshimaa09cbc72017-06-01 18:49:34 +0900161// Obtains base::Lock instance pointer to protect
162// |g_midi_in_get_num_devs_thread_id|.
163base::Lock* GetMidiInGetNumDevsThreadIdLock() {
164 static base::Lock* lock = new base::Lock;
165 return lock;
166}
167
168// Holds a thread id that calls midiInGetNumDevs() now. We use a platform
169// primitive to identify the thread because the following functions can be
170// called on a thread that Windows allocates internally, and Chrome or //base
171// library does not know.
172base::PlatformThreadId g_midi_in_get_num_devs_thread_id;
173
174// Prepares to call midiInGetNumDevs().
175void EnterMidiInGetNumDevs() {
176 base::AutoLock lock(*GetMidiInGetNumDevsThreadIdLock());
177 g_midi_in_get_num_devs_thread_id = base::PlatformThread::CurrentId();
178}
179
180// Finalizes to call midiInGetNumDevs().
181void LeaveMidiInGetNumDevs() {
182 base::AutoLock lock(*GetMidiInGetNumDevsThreadIdLock());
183 g_midi_in_get_num_devs_thread_id = base::PlatformThreadId();
184}
185
186// Checks if the current thread is running midiInGetNumDevs(), that means
187// current code is invoked inside midiInGetNumDevs().
188bool IsRunningInsideMidiInGetNumDevs() {
189 base::AutoLock lock(*GetMidiInGetNumDevsThreadIdLock());
190 return base::PlatformThread::CurrentId() == g_midi_in_get_num_devs_thread_id;
191}
192
toyoshim8a5ad422017-02-28 21:16:18 -0800193// Utility class to handle MIDIHDR struct safely.
194class MIDIHDRDeleter {
195 public:
196 void operator()(LPMIDIHDR header) {
197 if (!header)
198 return;
199 delete[] static_cast<char*>(header->lpData);
200 delete header;
201 }
202};
203
204using ScopedMIDIHDR = std::unique_ptr<MIDIHDR, MIDIHDRDeleter>;
205
206ScopedMIDIHDR CreateMIDIHDR(size_t size) {
207 ScopedMIDIHDR hdr(new MIDIHDR);
208 ZeroMemory(hdr.get(), sizeof(*hdr));
209 hdr->lpData = new char[size];
210 hdr->dwBufferLength = static_cast<DWORD>(size);
211 return hdr;
212}
213
toyoshim6d87aaa2017-02-28 22:36:44 -0800214ScopedMIDIHDR CreateMIDIHDR(const std::vector<uint8_t>& data) {
215 ScopedMIDIHDR hdr(CreateMIDIHDR(data.size()));
216 std::copy(data.begin(), data.end(), hdr->lpData);
217 return hdr;
218}
219
toyoshimc32dd892017-02-24 02:13:14 -0800220// Helper functions to close MIDI device handles on TaskRunner asynchronously.
toyoshim8a5ad422017-02-28 21:16:18 -0800221void FinalizeInPort(HMIDIIN handle, ScopedMIDIHDR hdr) {
222 // Resets the device. This stops receiving messages, and allows to release
223 // registered buffer headers. Otherwise, midiInUnprepareHeader() and
224 // midiInClose() will fail with MIDIERR_STILLPLAYING.
225 midiInReset(handle);
226
227 if (hdr)
228 midiInUnprepareHeader(handle, hdr.get(), sizeof(*hdr));
toyoshimc32dd892017-02-24 02:13:14 -0800229 midiInClose(handle);
230}
231
232void FinalizeOutPort(HMIDIOUT handle) {
toyoshim6d87aaa2017-02-28 22:36:44 -0800233 // Resets inflight buffers. This will cancel sending data that system
234 // holds and were not sent yet.
235 midiOutReset(handle);
toyoshimc32dd892017-02-24 02:13:14 -0800236 midiOutClose(handle);
237}
238
toyoshim6ebf1182017-03-01 00:40:31 -0800239// Gets manufacturer name in string from identifiers.
240std::string GetManufacturerName(uint16_t id, const GUID& guid) {
241 if (IS_COMPATIBLE_USBAUDIO_MID(&guid)) {
242 const char* name =
243 device::UsbIds::GetVendorName(EXTRACT_USBAUDIO_MID(&guid));
244 if (name)
245 return std::string(name);
246 }
247 if (id == MM_MICROSOFT)
248 return "Microsoft Corporation";
249
250 // TODO(crbug.com/472341): Support other manufacture IDs.
251 return "";
252}
253
toyoshimc32dd892017-02-24 02:13:14 -0800254// All instances of Port subclasses are always accessed behind a lock of
255// *GetTaskLock(). Port and subclasses implementation do not need to
256// consider thread safety.
toyoshimd28b59c2017-02-20 11:07:37 -0800257class Port {
258 public:
259 Port(const std::string& type,
260 uint32_t device_id,
261 uint16_t manufacturer_id,
262 uint16_t product_id,
263 uint32_t driver_version,
toyoshim6ebf1182017-03-01 00:40:31 -0800264 const std::string& product_name,
265 const GUID& manufacturer_guid)
toyoshimd28b59c2017-02-20 11:07:37 -0800266 : index_(0u),
267 type_(type),
268 device_id_(device_id),
269 manufacturer_id_(manufacturer_id),
270 product_id_(product_id),
271 driver_version_(driver_version),
272 product_name_(product_name) {
toyoshim6ebf1182017-03-01 00:40:31 -0800273 info_.manufacturer =
274 GetManufacturerName(manufacturer_id, manufacturer_guid);
toyoshimd28b59c2017-02-20 11:07:37 -0800275 info_.name = product_name_;
276 info_.version = base::StringPrintf("%d.%d", HIBYTE(driver_version_),
277 LOBYTE(driver_version_));
278 info_.state = mojom::PortState::DISCONNECTED;
279 }
280
281 virtual ~Port() {}
282
283 bool operator==(const Port& other) const {
284 // Should not use |device_id| for comparison because it can be changed on
285 // each enumeration.
286 // Since the GUID will be changed on each enumeration for Microsoft GS
287 // Wavetable synth and might be done for others, do not use it for device
288 // comparison.
289 return manufacturer_id_ == other.manufacturer_id_ &&
290 product_id_ == other.product_id_ &&
291 driver_version_ == other.driver_version_ &&
292 product_name_ == other.product_name_;
293 }
294
295 bool IsConnected() const {
296 return info_.state != mojom::PortState::DISCONNECTED;
297 }
298
299 void set_index(size_t index) {
300 index_ = index;
301 // TODO(toyoshim): Use hashed ID.
Bruce Dawson66ae7db2017-08-04 17:57:45 +0000302 info_.id = base::StringPrintf("%s-%zd", type_.c_str(), index_);
toyoshimd28b59c2017-02-20 11:07:37 -0800303 }
304 size_t index() { return index_; }
305 void set_device_id(uint32_t device_id) { device_id_ = device_id; }
306 uint32_t device_id() { return device_id_; }
Adithya Srinivasan33252732018-10-17 15:59:40 +0000307 const mojom::PortInfo& info() { return info_; }
toyoshimd28b59c2017-02-20 11:07:37 -0800308
309 virtual bool Connect() {
310 if (info_.state != mojom::PortState::DISCONNECTED)
311 return false;
312
313 info_.state = mojom::PortState::CONNECTED;
314 // TODO(toyoshim) Until open() / close() are supported, open each device on
315 // connected.
316 Open();
317 return true;
318 }
319
320 virtual bool Disconnect() {
321 if (info_.state == mojom::PortState::DISCONNECTED)
322 return false;
323 info_.state = mojom::PortState::DISCONNECTED;
324 return true;
325 }
326
327 virtual void Open() { info_.state = mojom::PortState::OPENED; }
328
329 protected:
330 size_t index_;
331 std::string type_;
332 uint32_t device_id_;
333 const uint16_t manufacturer_id_;
334 const uint16_t product_id_;
335 const uint32_t driver_version_;
336 const std::string product_name_;
Adithya Srinivasan33252732018-10-17 15:59:40 +0000337 mojom::PortInfo info_;
toyoshimd28b59c2017-02-20 11:07:37 -0800338}; // class Port
339
340} // namespace
341
toyoshim63e32a52017-04-25 07:20:10 -0700342class MidiManagerWin::InPort final : public Port {
toyoshimd28b59c2017-02-20 11:07:37 -0800343 public:
toyoshim63e32a52017-04-25 07:20:10 -0700344 InPort(MidiManagerWin* manager,
toyoshim8a5ad422017-02-28 21:16:18 -0800345 int instance_id,
346 UINT device_id,
347 const MIDIINCAPS2W& caps)
toyoshimd28b59c2017-02-20 11:07:37 -0800348 : Port("input",
349 device_id,
350 caps.wMid,
351 caps.wPid,
352 caps.vDriverVersion,
353 base::WideToUTF8(
toyoshim6ebf1182017-03-01 00:40:31 -0800354 base::string16(caps.szPname, wcslen(caps.szPname))),
355 caps.ManufacturerGuid),
toyoshim8a5ad422017-02-28 21:16:18 -0800356 manager_(manager),
357 in_handle_(kInvalidInHandle),
358 instance_id_(instance_id) {}
toyoshimd28b59c2017-02-20 11:07:37 -0800359
toyoshim8a5ad422017-02-28 21:16:18 -0800360 static std::vector<std::unique_ptr<InPort>> EnumerateActivePorts(
toyoshim63e32a52017-04-25 07:20:10 -0700361 MidiManagerWin* manager,
toyoshim8a5ad422017-02-28 21:16:18 -0800362 int instance_id) {
toyoshimd28b59c2017-02-20 11:07:37 -0800363 std::vector<std::unique_ptr<InPort>> ports;
Takashi Toyoshimaa09cbc72017-06-01 18:49:34 +0900364
365 // Allow callback invocations indie midiInGetNumDevs().
366 EnterMidiInGetNumDevs();
toyoshimd28b59c2017-02-20 11:07:37 -0800367 const UINT num_devices = midiInGetNumDevs();
Takashi Toyoshimaa09cbc72017-06-01 18:49:34 +0900368 LeaveMidiInGetNumDevs();
369
toyoshimd28b59c2017-02-20 11:07:37 -0800370 for (UINT device_id = 0; device_id < num_devices; ++device_id) {
371 MIDIINCAPS2W caps;
372 MMRESULT result = midiInGetDevCaps(
373 device_id, reinterpret_cast<LPMIDIINCAPSW>(&caps), sizeof(caps));
374 if (result != MMSYSERR_NOERROR) {
375 LOG(ERROR) << "midiInGetDevCaps fails on device " << device_id;
376 continue;
377 }
toyoshim8a5ad422017-02-28 21:16:18 -0800378 ports.push_back(
Gyuyoung Kim34e191a2018-01-10 09:48:42 +0000379 std::make_unique<InPort>(manager, instance_id, device_id, caps));
toyoshimd28b59c2017-02-20 11:07:37 -0800380 }
381 return ports;
382 }
383
toyoshimc32dd892017-02-24 02:13:14 -0800384 void Finalize(scoped_refptr<base::SingleThreadTaskRunner> runner) {
385 if (in_handle_ != kInvalidInHandle) {
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000386 runner->PostTask(FROM_HERE, base::BindOnce(&FinalizeInPort, in_handle_,
387 base::Passed(&hdr_)));
toyoshim8a5ad422017-02-28 21:16:18 -0800388 manager_->port_manager()->UnregisterInHandle(in_handle_);
toyoshimc32dd892017-02-24 02:13:14 -0800389 in_handle_ = kInvalidInHandle;
390 }
391 }
392
toyoshim8a5ad422017-02-28 21:16:18 -0800393 base::TimeTicks CalculateInEventTime(uint32_t elapsed_ms) const {
394 return start_time_ + base::TimeDelta::FromMilliseconds(elapsed_ms);
395 }
396
397 void RestoreBuffer() {
398 if (in_handle_ == kInvalidInHandle || !hdr_)
399 return;
400 midiInAddBuffer(in_handle_, hdr_.get(), sizeof(*hdr_));
401 }
402
toyoshim63e32a52017-04-25 07:20:10 -0700403 void NotifyPortStateSet(MidiManagerWin* manager) {
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000404 manager->PostReplyTask(base::BindOnce(
Bruce Dawson7fd26de2017-08-01 19:33:12 +0000405 &MidiManagerWin::SetInputPortState, base::Unretained(manager),
406 static_cast<uint32_t>(index_), info_.state));
toyoshimd28b59c2017-02-20 11:07:37 -0800407 }
408
toyoshim63e32a52017-04-25 07:20:10 -0700409 void NotifyPortAdded(MidiManagerWin* manager) {
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000410 manager->PostReplyTask(base::BindOnce(&MidiManagerWin::AddInputPort,
411 base::Unretained(manager), info_));
toyoshimd28b59c2017-02-20 11:07:37 -0800412 }
toyoshimc32dd892017-02-24 02:13:14 -0800413
414 // Port overrides:
415 bool Disconnect() override {
416 if (in_handle_ != kInvalidInHandle) {
417 // Following API call may fail because device was already disconnected.
418 // But just in case.
419 midiInClose(in_handle_);
toyoshim8a5ad422017-02-28 21:16:18 -0800420 manager_->port_manager()->UnregisterInHandle(in_handle_);
toyoshimc32dd892017-02-24 02:13:14 -0800421 in_handle_ = kInvalidInHandle;
422 }
423 return Port::Disconnect();
424 }
425
426 void Open() override {
toyoshim8a5ad422017-02-28 21:16:18 -0800427 MMRESULT result = midiInOpen(
428 &in_handle_, device_id_,
429 reinterpret_cast<DWORD_PTR>(&PortManager::HandleMidiInCallback),
430 instance_id_, CALLBACK_FUNCTION);
toyoshimc32dd892017-02-24 02:13:14 -0800431 if (result == MMSYSERR_NOERROR) {
toyoshim8a5ad422017-02-28 21:16:18 -0800432 hdr_ = CreateMIDIHDR(kBufferLength);
433 result = midiInPrepareHeader(in_handle_, hdr_.get(), sizeof(*hdr_));
434 }
435 if (result != MMSYSERR_NOERROR)
436 in_handle_ = kInvalidInHandle;
437 if (result == MMSYSERR_NOERROR)
438 result = midiInAddBuffer(in_handle_, hdr_.get(), sizeof(*hdr_));
439 if (result == MMSYSERR_NOERROR)
440 result = midiInStart(in_handle_);
441 if (result == MMSYSERR_NOERROR) {
442 start_time_ = base::TimeTicks::Now();
443 manager_->port_manager()->RegisterInHandle(in_handle_, index_);
toyoshimc32dd892017-02-24 02:13:14 -0800444 Port::Open();
445 } else {
toyoshim8a5ad422017-02-28 21:16:18 -0800446 if (in_handle_ != kInvalidInHandle) {
447 midiInUnprepareHeader(in_handle_, hdr_.get(), sizeof(*hdr_));
448 hdr_.reset();
449 midiInClose(in_handle_);
450 in_handle_ = kInvalidInHandle;
451 }
toyoshimc32dd892017-02-24 02:13:14 -0800452 Disconnect();
453 }
454 }
455
456 private:
toyoshim63e32a52017-04-25 07:20:10 -0700457 MidiManagerWin* manager_;
toyoshimc32dd892017-02-24 02:13:14 -0800458 HMIDIIN in_handle_;
toyoshim8a5ad422017-02-28 21:16:18 -0800459 ScopedMIDIHDR hdr_;
460 base::TimeTicks start_time_;
461 const int instance_id_;
toyoshimd28b59c2017-02-20 11:07:37 -0800462};
463
toyoshim63e32a52017-04-25 07:20:10 -0700464class MidiManagerWin::OutPort final : public Port {
toyoshimd28b59c2017-02-20 11:07:37 -0800465 public:
466 OutPort(UINT device_id, const MIDIOUTCAPS2W& caps)
467 : Port("output",
468 device_id,
469 caps.wMid,
470 caps.wPid,
471 caps.vDriverVersion,
472 base::WideToUTF8(
toyoshim6ebf1182017-03-01 00:40:31 -0800473 base::string16(caps.szPname, wcslen(caps.szPname))),
474 caps.ManufacturerGuid),
toyoshimc32dd892017-02-24 02:13:14 -0800475 software_(caps.wTechnology == MOD_SWSYNTH),
476 out_handle_(kInvalidOutHandle) {}
toyoshimd28b59c2017-02-20 11:07:37 -0800477
478 static std::vector<std::unique_ptr<OutPort>> EnumerateActivePorts() {
479 std::vector<std::unique_ptr<OutPort>> ports;
480 const UINT num_devices = midiOutGetNumDevs();
481 for (UINT device_id = 0; device_id < num_devices; ++device_id) {
482 MIDIOUTCAPS2W caps;
483 MMRESULT result = midiOutGetDevCaps(
484 device_id, reinterpret_cast<LPMIDIOUTCAPSW>(&caps), sizeof(caps));
485 if (result != MMSYSERR_NOERROR) {
486 LOG(ERROR) << "midiOutGetDevCaps fails on device " << device_id;
487 continue;
488 }
Gyuyoung Kim34e191a2018-01-10 09:48:42 +0000489 ports.push_back(std::make_unique<OutPort>(device_id, caps));
toyoshimd28b59c2017-02-20 11:07:37 -0800490 }
491 return ports;
492 }
493
toyoshimc32dd892017-02-24 02:13:14 -0800494 void Finalize(scoped_refptr<base::SingleThreadTaskRunner> runner) {
495 if (out_handle_ != kInvalidOutHandle) {
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000496 runner->PostTask(FROM_HERE,
497 base::BindOnce(&FinalizeOutPort, out_handle_));
toyoshimc32dd892017-02-24 02:13:14 -0800498 out_handle_ = kInvalidOutHandle;
toyoshimd28b59c2017-02-20 11:07:37 -0800499 }
toyoshimd28b59c2017-02-20 11:07:37 -0800500 }
501
toyoshim63e32a52017-04-25 07:20:10 -0700502 void NotifyPortStateSet(MidiManagerWin* manager) {
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000503 manager->PostReplyTask(base::BindOnce(
Bruce Dawson7fd26de2017-08-01 19:33:12 +0000504 &MidiManagerWin::SetOutputPortState, base::Unretained(manager),
505 static_cast<uint32_t>(index_), info_.state));
toyoshimd28b59c2017-02-20 11:07:37 -0800506 }
507
toyoshim63e32a52017-04-25 07:20:10 -0700508 void NotifyPortAdded(MidiManagerWin* manager) {
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000509 manager->PostReplyTask(base::BindOnce(&MidiManagerWin::AddOutputPort,
510 base::Unretained(manager), info_));
toyoshimd28b59c2017-02-20 11:07:37 -0800511 }
512
toyoshim6d87aaa2017-02-28 22:36:44 -0800513 void Send(const std::vector<uint8_t>& data) {
514 if (out_handle_ == kInvalidOutHandle)
515 return;
516
517 if (data.size() <= 3) {
518 uint32_t message = 0;
519 for (size_t i = 0; i < data.size(); ++i)
520 message |= (static_cast<uint32_t>(data[i]) << (i * 8));
521 midiOutShortMsg(out_handle_, message);
522 } else {
523 if (data.size() > kSysExSizeLimit) {
524 LOG(ERROR) << "Ignoring SysEx message due to the size limit"
525 << ", size = " << data.size();
526 // TODO(toyoshim): Consider to report metrics here.
527 return;
528 }
529 ScopedMIDIHDR hdr(CreateMIDIHDR(data));
530 MMRESULT result =
531 midiOutPrepareHeader(out_handle_, hdr.get(), sizeof(*hdr));
532 if (result != MMSYSERR_NOERROR)
533 return;
534 result = midiOutLongMsg(out_handle_, hdr.get(), sizeof(*hdr));
535 if (result != MMSYSERR_NOERROR) {
536 midiOutUnprepareHeader(out_handle_, hdr.get(), sizeof(*hdr));
537 } else {
538 // MIDIHDR will be released on MOM_DONE.
539 ignore_result(hdr.release());
540 }
541 }
542 }
543
toyoshimc32dd892017-02-24 02:13:14 -0800544 // Port overrides:
545 bool Connect() override {
546 // Until |software| option is supported, disable Microsoft GS Wavetable
547 // Synth that has a known security issue.
548 if (software_ && manufacturer_id_ == MM_MICROSOFT &&
549 (product_id_ == MM_MSFT_WDMAUDIO_MIDIOUT ||
550 product_id_ == MM_MSFT_GENERIC_MIDISYNTH)) {
551 return false;
552 }
553 return Port::Connect();
554 }
555
556 bool Disconnect() override {
557 if (out_handle_ != kInvalidOutHandle) {
558 // Following API call may fail because device was already disconnected.
559 // But just in case.
560 midiOutClose(out_handle_);
561 out_handle_ = kInvalidOutHandle;
562 }
563 return Port::Disconnect();
564 }
565
566 void Open() override {
toyoshim6d87aaa2017-02-28 22:36:44 -0800567 MMRESULT result = midiOutOpen(
568 &out_handle_, device_id_,
569 reinterpret_cast<DWORD_PTR>(&PortManager::HandleMidiOutCallback), 0,
570 CALLBACK_FUNCTION);
toyoshimc32dd892017-02-24 02:13:14 -0800571 if (result == MMSYSERR_NOERROR) {
572 Port::Open();
573 } else {
574 out_handle_ = kInvalidOutHandle;
575 Disconnect();
576 }
577 }
578
toyoshimd28b59c2017-02-20 11:07:37 -0800579 const bool software_;
toyoshimc32dd892017-02-24 02:13:14 -0800580 HMIDIOUT out_handle_;
toyoshimd28b59c2017-02-20 11:07:37 -0800581};
582
toyoshim63e32a52017-04-25 07:20:10 -0700583base::TimeTicks MidiManagerWin::PortManager::CalculateInEventTime(
toyoshim8a5ad422017-02-28 21:16:18 -0800584 size_t index,
585 uint32_t elapsed_ms) const {
586 GetTaskLock()->AssertAcquired();
587 CHECK_GT(input_ports_.size(), index);
588 return input_ports_[index]->CalculateInEventTime(elapsed_ms);
589}
590
toyoshim63e32a52017-04-25 07:20:10 -0700591void MidiManagerWin::PortManager::RegisterInHandle(HMIDIIN handle,
592 size_t index) {
toyoshim8a5ad422017-02-28 21:16:18 -0800593 GetTaskLock()->AssertAcquired();
594 hmidiin_to_index_map_[handle] = index;
595}
596
toyoshim63e32a52017-04-25 07:20:10 -0700597void MidiManagerWin::PortManager::UnregisterInHandle(HMIDIIN handle) {
toyoshim8a5ad422017-02-28 21:16:18 -0800598 GetTaskLock()->AssertAcquired();
599 hmidiin_to_index_map_.erase(handle);
600}
601
toyoshim63e32a52017-04-25 07:20:10 -0700602bool MidiManagerWin::PortManager::FindInHandle(HMIDIIN hmi, size_t* out_index) {
toyoshim8a5ad422017-02-28 21:16:18 -0800603 GetTaskLock()->AssertAcquired();
604 auto found = hmidiin_to_index_map_.find(hmi);
605 if (found == hmidiin_to_index_map_.end())
606 return false;
607 *out_index = found->second;
608 return true;
609}
610
toyoshim63e32a52017-04-25 07:20:10 -0700611void MidiManagerWin::PortManager::RestoreInBuffer(size_t index) {
toyoshim8a5ad422017-02-28 21:16:18 -0800612 GetTaskLock()->AssertAcquired();
613 CHECK_GT(input_ports_.size(), index);
614 input_ports_[index]->RestoreBuffer();
615}
616
617void CALLBACK
toyoshim63e32a52017-04-25 07:20:10 -0700618MidiManagerWin::PortManager::HandleMidiInCallback(HMIDIIN hmi,
619 UINT msg,
620 DWORD_PTR instance,
621 DWORD_PTR param1,
622 DWORD_PTR param2) {
toyoshim8a5ad422017-02-28 21:16:18 -0800623 if (msg != MIM_DATA && msg != MIM_LONGDATA)
624 return;
625 int instance_id = static_cast<int>(instance);
toyoshim63e32a52017-04-25 07:20:10 -0700626 MidiManagerWin* manager = nullptr;
toyoshim8a5ad422017-02-28 21:16:18 -0800627
628 // Use |g_task_lock| so to ensure the instance can keep alive while running,
629 // and to access member variables that are used on TaskRunner.
Takashi Toyoshimaa09cbc72017-06-01 18:49:34 +0900630 // Exceptionally, we do not take the lock when this callback is invoked inside
631 // midiInGetNumDevs() on the caller thread because the lock is already
632 // obtained by the current caller thread.
633 std::unique_ptr<base::AutoLock> task_lock;
634 if (IsRunningInsideMidiInGetNumDevs())
635 GetTaskLock()->AssertAcquired();
636 else
637 task_lock.reset(new base::AutoLock(*GetTaskLock()));
toyoshim8a5ad422017-02-28 21:16:18 -0800638 {
639 base::AutoLock lock(*GetInstanceIdLock());
640 if (instance_id != g_active_instance_id)
641 return;
642 manager = g_manager_instance;
643 }
644
645 size_t index;
toyoshim6d87aaa2017-02-28 22:36:44 -0800646 if (!manager->port_manager()->FindInHandle(hmi, &index))
toyoshim8a5ad422017-02-28 21:16:18 -0800647 return;
648
649 DCHECK(msg == MIM_DATA || msg == MIM_LONGDATA);
650 if (msg == MIM_DATA) {
651 const uint8_t status_byte = static_cast<uint8_t>(param1 & 0xff);
652 const uint8_t first_data_byte = static_cast<uint8_t>((param1 >> 8) & 0xff);
653 const uint8_t second_data_byte =
654 static_cast<uint8_t>((param1 >> 16) & 0xff);
655 const uint8_t kData[] = {status_byte, first_data_byte, second_data_byte};
656 const size_t len = GetMessageLength(status_byte);
Avi Drissman2c637192018-12-25 20:26:39 +0000657 DCHECK_LE(len, base::size(kData));
toyoshim8a5ad422017-02-28 21:16:18 -0800658 std::vector<uint8_t> data;
659 data.assign(kData, kData + len);
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000660 manager->PostReplyTask(base::BindOnce(
Yutaka Hirano138dd962017-08-01 08:14:15 +0000661 &MidiManagerWin::ReceiveMidiData, base::Unretained(manager),
662 static_cast<uint32_t>(index), data,
663 manager->port_manager()->CalculateInEventTime(index, param2)));
toyoshim8a5ad422017-02-28 21:16:18 -0800664 } else {
665 DCHECK_EQ(static_cast<UINT>(MIM_LONGDATA), msg);
666 LPMIDIHDR hdr = reinterpret_cast<LPMIDIHDR>(param1);
667 if (hdr->dwBytesRecorded > 0) {
668 const uint8_t* src = reinterpret_cast<const uint8_t*>(hdr->lpData);
669 std::vector<uint8_t> data;
670 data.assign(src, src + hdr->dwBytesRecorded);
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000671 manager->PostReplyTask(base::BindOnce(
Yutaka Hirano138dd962017-08-01 08:14:15 +0000672 &MidiManagerWin::ReceiveMidiData, base::Unretained(manager),
673 static_cast<uint32_t>(index), data,
674 manager->port_manager()->CalculateInEventTime(index, param2)));
toyoshim8a5ad422017-02-28 21:16:18 -0800675 }
toyoshim824deed2017-06-07 16:45:43 -0700676 manager->port_manager()->RestoreInBuffer(index);
toyoshim8a5ad422017-02-28 21:16:18 -0800677 }
678}
679
toyoshim6d87aaa2017-02-28 22:36:44 -0800680void CALLBACK
toyoshim63e32a52017-04-25 07:20:10 -0700681MidiManagerWin::PortManager::HandleMidiOutCallback(HMIDIOUT hmo,
682 UINT msg,
683 DWORD_PTR instance,
684 DWORD_PTR param1,
685 DWORD_PTR param2) {
toyoshim6d87aaa2017-02-28 22:36:44 -0800686 if (msg == MOM_DONE) {
687 ScopedMIDIHDR hdr(reinterpret_cast<LPMIDIHDR>(param1));
688 if (!hdr)
689 return;
690 // TODO(toyoshim): Call midiOutUnprepareHeader outside the callback.
691 // Since this callback may be invoked after the manager is destructed,
692 // and can not send a task to the TaskRunner in such case, we need to
693 // consider to track MIDIHDR per port, and clean it in port finalization
694 // steps, too.
695 midiOutUnprepareHeader(hmo, hdr.get(), sizeof(*hdr));
696 }
697}
698
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000699// static
700void MidiManagerWin::OverflowInstanceIdForTesting() {
701 IssueNextInstanceId(std::numeric_limits<int64_t>::max());
702}
703
toyoshim63e32a52017-04-25 07:20:10 -0700704MidiManagerWin::MidiManagerWin(MidiService* service)
toyoshim8a5ad422017-02-28 21:16:18 -0800705 : MidiManager(service),
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000706 instance_id_(IssueNextInstanceId(base::nullopt)),
Gyuyoung Kim34e191a2018-01-10 09:48:42 +0000707 port_manager_(std::make_unique<PortManager>()) {
toyoshimd28b59c2017-02-20 11:07:37 -0800708 base::AutoLock lock(*GetInstanceIdLock());
709 CHECK_EQ(kInvalidInstanceId, g_active_instance_id);
710
711 // Obtains the task runner for the current thread that hosts this instnace.
712 thread_runner_ = base::ThreadTaskRunnerHandle::Get();
713}
714
toyoshim63e32a52017-04-25 07:20:10 -0700715MidiManagerWin::~MidiManagerWin() {
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000716 // Initialization failed. Exit without running actual finalization that should
717 // not be needed.
718 if (instance_id_ == kInvalidInstanceId)
719 return;
720
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000721 // Unregisters on the I/O thread. OnDevicesChanged() won't be called any more.
toyoshimd28b59c2017-02-20 11:07:37 -0800722 CHECK(thread_runner_->BelongsToCurrentThread());
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000723 base::SystemMonitor::Get()->RemoveDevicesChangedObserver(this);
724
725 // Posts tasks that finalize each device port without MidiManager instance
726 // on TaskRunner. If another MidiManager instance is created, its
727 // initialization runs on the same task runner after all tasks posted here
728 // finish.
729 for (const auto& port : *port_manager_->inputs())
730 port->Finalize(service()->GetTaskRunner(kTaskRunner));
731 for (const auto& port : *port_manager_->outputs())
732 port->Finalize(service()->GetTaskRunner(kTaskRunner));
733
734 // Invalidate instance bound tasks.
735 {
736 base::AutoLock lock(*GetInstanceIdLock());
737 CHECK_EQ(instance_id_, g_active_instance_id);
738 g_active_instance_id = kInvalidInstanceId;
739 CHECK_EQ(this, g_manager_instance);
740 g_manager_instance = nullptr;
741 }
742
743 // Ensures that no bound task runs on TaskRunner so to destruct the instance
744 // safely.
745 // Tasks that did not started yet will do nothing after invalidate the
746 // instance ID above.
747 // Behind the lock below, we can safely access all members for finalization
748 // even on the I/O thread.
749 base::AutoLock lock(*GetTaskLock());
toyoshimd28b59c2017-02-20 11:07:37 -0800750}
751
toyoshim63e32a52017-04-25 07:20:10 -0700752void MidiManagerWin::StartInitialization() {
toyoshimd28b59c2017-02-20 11:07:37 -0800753 {
754 base::AutoLock lock(*GetInstanceIdLock());
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000755 if (instance_id_ == kInvalidInstanceId)
756 return CompleteInitialization(mojom::Result::INITIALIZATION_ERROR);
757
toyoshimd28b59c2017-02-20 11:07:37 -0800758 CHECK_EQ(kInvalidInstanceId, g_active_instance_id);
759 g_active_instance_id = instance_id_;
760 CHECK_EQ(nullptr, g_manager_instance);
761 g_manager_instance = this;
762 }
763 // Registers on the I/O thread to be notified on the I/O thread.
764 CHECK(thread_runner_->BelongsToCurrentThread());
765 base::SystemMonitor::Get()->AddDevicesChangedObserver(this);
766
767 // Starts asynchronous initialization on TaskRunner.
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000768 PostTask(base::BindOnce(&MidiManagerWin::InitializeOnTaskRunner,
769 base::Unretained(this)));
toyoshimd28b59c2017-02-20 11:07:37 -0800770}
771
toyoshim63e32a52017-04-25 07:20:10 -0700772void MidiManagerWin::DispatchSendMidiData(MidiManagerClient* client,
773 uint32_t port_index,
774 const std::vector<uint8_t>& data,
tzik925e2c62018-02-02 07:39:45 +0000775 base::TimeTicks timestamp) {
Takashi Toyoshimaafb27d52017-09-13 11:50:41 +0000776 PostDelayedTask(
777 base::BindOnce(&MidiManagerWin::SendOnTaskRunner, base::Unretained(this),
778 client, port_index, data),
779 MidiService::TimestampToTimeDeltaDelay(timestamp));
toyoshimd28b59c2017-02-20 11:07:37 -0800780}
781
toyoshim63e32a52017-04-25 07:20:10 -0700782void MidiManagerWin::OnDevicesChanged(
toyoshimd28b59c2017-02-20 11:07:37 -0800783 base::SystemMonitor::DeviceType device_type) {
784 // Notified on the I/O thread.
785 CHECK(thread_runner_->BelongsToCurrentThread());
786
787 switch (device_type) {
788 case base::SystemMonitor::DEVTYPE_AUDIO:
789 case base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE:
790 // Add case of other unrelated device types here.
791 return;
792 case base::SystemMonitor::DEVTYPE_UNKNOWN: {
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000793 PostTask(base::BindOnce(&MidiManagerWin::UpdateDeviceListOnTaskRunner,
794 base::Unretained(this)));
toyoshimd28b59c2017-02-20 11:07:37 -0800795 break;
796 }
797 }
798}
799
toyoshim63e32a52017-04-25 07:20:10 -0700800void MidiManagerWin::ReceiveMidiData(uint32_t index,
801 const std::vector<uint8_t>& data,
802 base::TimeTicks time) {
toyoshim8a5ad422017-02-28 21:16:18 -0800803 MidiManager::ReceiveMidiData(index, data.data(), data.size(), time);
804}
805
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000806void MidiManagerWin::PostTask(base::OnceClosure task) {
toyoshimd28b59c2017-02-20 11:07:37 -0800807 service()
808 ->GetTaskRunner(kTaskRunner)
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000809 ->PostTask(FROM_HERE,
810 base::BindOnce(&RunTask, instance_id_, std::move(task)));
toyoshimd28b59c2017-02-20 11:07:37 -0800811}
812
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000813void MidiManagerWin::PostDelayedTask(base::OnceClosure task,
toyoshim63e32a52017-04-25 07:20:10 -0700814 base::TimeDelta delay) {
toyoshim6d87aaa2017-02-28 22:36:44 -0800815 service()
816 ->GetTaskRunner(kTaskRunner)
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000817 ->PostDelayedTask(FROM_HERE,
818 base::BindOnce(&RunTask, instance_id_, std::move(task)),
toyoshim6d87aaa2017-02-28 22:36:44 -0800819 delay);
820}
821
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000822void MidiManagerWin::PostReplyTask(base::OnceClosure task) {
823 thread_runner_->PostTask(
824 FROM_HERE, base::BindOnce(&RunTask, instance_id_, std::move(task)));
toyoshim8a5ad422017-02-28 21:16:18 -0800825}
826
toyoshim63e32a52017-04-25 07:20:10 -0700827void MidiManagerWin::InitializeOnTaskRunner() {
toyoshimd28b59c2017-02-20 11:07:37 -0800828 UpdateDeviceListOnTaskRunner();
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000829 PostReplyTask(base::BindOnce(&MidiManagerWin::CompleteInitialization,
830 base::Unretained(this), mojom::Result::OK));
toyoshimd28b59c2017-02-20 11:07:37 -0800831}
832
toyoshim63e32a52017-04-25 07:20:10 -0700833void MidiManagerWin::UpdateDeviceListOnTaskRunner() {
toyoshimd28b59c2017-02-20 11:07:37 -0800834 std::vector<std::unique_ptr<InPort>> active_input_ports =
toyoshim8a5ad422017-02-28 21:16:18 -0800835 InPort::EnumerateActivePorts(this, instance_id_);
836 ReflectActiveDeviceList(this, port_manager_->inputs(), &active_input_ports);
toyoshimd28b59c2017-02-20 11:07:37 -0800837
838 std::vector<std::unique_ptr<OutPort>> active_output_ports =
839 OutPort::EnumerateActivePorts();
toyoshim8a5ad422017-02-28 21:16:18 -0800840 ReflectActiveDeviceList(this, port_manager_->outputs(), &active_output_ports);
toyoshimd28b59c2017-02-20 11:07:37 -0800841
842 // TODO(toyoshim): This method may run before internal MIDI device lists that
843 // Windows manages were updated. This may be because MIDI driver may be loaded
844 // after the raw device list was updated. To avoid this problem, we may want
845 // to retry device check later if no changes are detected here.
846}
847
848template <typename T>
toyoshim63e32a52017-04-25 07:20:10 -0700849void MidiManagerWin::ReflectActiveDeviceList(MidiManagerWin* manager,
850 std::vector<T>* known_ports,
851 std::vector<T>* active_ports) {
toyoshimd28b59c2017-02-20 11:07:37 -0800852 // Update existing port states.
853 for (const auto& port : *known_ports) {
854 const auto& it = std::find_if(
855 active_ports->begin(), active_ports->end(),
856 [&port](const auto& candidate) { return *candidate == *port; });
857 if (it == active_ports->end()) {
858 if (port->Disconnect())
859 port->NotifyPortStateSet(this);
860 } else {
861 port->set_device_id((*it)->device_id());
862 if (port->Connect())
863 port->NotifyPortStateSet(this);
864 }
865 }
866
867 // Find new ports from active ports and append them to known ports.
868 for (auto& port : *active_ports) {
869 if (std::find_if(known_ports->begin(), known_ports->end(),
870 [&port](const auto& candidate) {
871 return *candidate == *port;
872 }) == known_ports->end()) {
873 size_t index = known_ports->size();
874 port->set_index(index);
875 known_ports->push_back(std::move(port));
876 (*known_ports)[index]->Connect();
877 (*known_ports)[index]->NotifyPortAdded(this);
878 }
879 }
880}
881
toyoshim63e32a52017-04-25 07:20:10 -0700882void MidiManagerWin::SendOnTaskRunner(MidiManagerClient* client,
883 uint32_t port_index,
884 const std::vector<uint8_t>& data) {
toyoshim6d87aaa2017-02-28 22:36:44 -0800885 CHECK_GT(port_manager_->outputs()->size(), port_index);
886 (*port_manager_->outputs())[port_index]->Send(data);
887 // |client| will be checked inside MidiManager::AccumulateMidiBytesSent.
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000888 PostReplyTask(base::BindOnce(&MidiManagerWin::AccumulateMidiBytesSent,
889 base::Unretained(this), client, data.size()));
toyoshim6d87aaa2017-02-28 22:36:44 -0800890}
891
toyoshim15385b32017-04-24 23:52:01 -0700892MidiManager* MidiManager::Create(MidiService* service) {
893 if (base::FeatureList::IsEnabled(features::kMidiManagerWinrt) &&
toyoshim63e32a52017-04-25 07:20:10 -0700894 base::win::GetVersion() >= base::win::VERSION_WIN10) {
toyoshim15385b32017-04-24 23:52:01 -0700895 return new MidiManagerWinrt(service);
toyoshim63e32a52017-04-25 07:20:10 -0700896 }
897 return new MidiManagerWin(service);
toyoshim15385b32017-04-24 23:52:01 -0700898}
899
toyoshimd28b59c2017-02-20 11:07:37 -0800900} // namespace midi