1 // Copyright 2013 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.
5 #include "media/midi/midi_manager_win.h"
11 // Prevent unnecessary functions from being included from <mmsystem.h>
28 #include "base/bind.h"
29 #include "base/containers/hash_tables.h"
30 #include "base/message_loop/message_loop.h"
31 #include "base/strings/string16.h"
32 #include "base/strings/string_number_conversions.h"
33 #include "base/strings/string_piece.h"
34 #include "base/strings/stringprintf.h"
35 #include "base/strings/utf_string_conversions.h"
36 #include "base/system_monitor/system_monitor.h"
37 #include "base/threading/thread_checker.h"
38 #include "base/timer/timer.h"
39 #include "base/win/message_window.h"
40 #include "device/usb/usb_ids.h"
41 #include "media/midi/midi_message_queue.h"
42 #include "media/midi/midi_message_util.h"
43 #include "media/midi/midi_port_info.h"
49 static const size_t kBufferLength
= 32 * 1024;
51 // We assume that nullpter represents an invalid MIDI handle.
52 const HMIDIIN kInvalidMidiInHandle
= nullptr;
53 const HMIDIOUT kInvalidMidiOutHandle
= nullptr;
55 std::string
GetInErrorMessage(MMRESULT result
) {
56 wchar_t text
[MAXERRORLENGTH
];
57 MMRESULT get_result
= midiInGetErrorText(result
, text
, arraysize(text
));
58 if (get_result
!= MMSYSERR_NOERROR
) {
59 DLOG(ERROR
) << "Failed to get error message."
60 << " original error: " << result
61 << " midiInGetErrorText error: " << get_result
;
64 return base::WideToUTF8(text
);
67 std::string
GetOutErrorMessage(MMRESULT result
) {
68 wchar_t text
[MAXERRORLENGTH
];
69 MMRESULT get_result
= midiOutGetErrorText(result
, text
, arraysize(text
));
70 if (get_result
!= MMSYSERR_NOERROR
) {
71 DLOG(ERROR
) << "Failed to get error message."
72 << " original error: " << result
73 << " midiOutGetErrorText error: " << get_result
;
76 return base::WideToUTF8(text
);
79 std::string
MmversionToString(MMVERSION version
) {
80 return base::StringPrintf("%d.%d", HIBYTE(version
), LOBYTE(version
));
83 class MIDIHDRDeleter
{
85 void operator()(MIDIHDR
* header
) {
88 delete[] static_cast<char*>(header
->lpData
);
89 header
->lpData
= NULL
;
90 header
->dwBufferLength
= 0;
95 typedef scoped_ptr
<MIDIHDR
, MIDIHDRDeleter
> ScopedMIDIHDR
;
97 ScopedMIDIHDR
CreateMIDIHDR(size_t size
) {
98 ScopedMIDIHDR
header(new MIDIHDR
);
99 ZeroMemory(header
.get(), sizeof(*header
));
100 header
->lpData
= new char[size
];
101 header
->dwBufferLength
= static_cast<DWORD
>(size
);
102 return header
.Pass();
105 void SendShortMidiMessageInternal(HMIDIOUT midi_out_handle
,
106 const std::vector
<uint8
>& message
) {
107 DCHECK_LE(message
.size(), static_cast<size_t>(3))
108 << "A short MIDI message should be up to 3 bytes.";
110 DWORD packed_message
= 0;
111 for (size_t i
= 0; i
< message
.size(); ++i
)
112 packed_message
|= (static_cast<uint32
>(message
[i
]) << (i
* 8));
113 MMRESULT result
= midiOutShortMsg(midi_out_handle
, packed_message
);
114 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
115 << "Failed to output short message: " << GetOutErrorMessage(result
);
118 void SendLongMidiMessageInternal(HMIDIOUT midi_out_handle
,
119 const std::vector
<uint8
>& message
) {
120 // Implementation note:
121 // Sending a long MIDI message can be performed synchronously or
122 // asynchronously depending on the driver. There are 2 options to support both
124 // 1) Call midiOutLongMsg() API and wait for its completion within this
125 // function. In this approach, we can avoid memory copy by directly pointing
126 // |message| as the data buffer to be sent.
127 // 2) Allocate a buffer and copy |message| to it, then call midiOutLongMsg()
128 // API. The buffer will be freed in the MOM_DONE event hander, which tells
129 // us that the task of midiOutLongMsg() API is completed.
130 // Here we choose option 2) in favor of asynchronous design.
132 // Note for built-in USB-MIDI driver:
133 // From an observation on Windows 7/8.1 with a USB-MIDI keyboard,
134 // midiOutLongMsg() will be always blocked. Sending 64 bytes or less data
135 // takes roughly 300 usecs. Sending 2048 bytes or more data takes roughly
136 // |message.size() / (75 * 1024)| secs in practice. Here we put 60 KB size
137 // limit on SysEx message, with hoping that midiOutLongMsg will be blocked at
138 // most 1 sec or so with a typical USB-MIDI device.
139 const size_t kSysExSizeLimit
= 60 * 1024;
140 if (message
.size() >= kSysExSizeLimit
) {
141 DVLOG(1) << "Ingnoreing SysEx message due to the size limit"
142 << ", size = " << message
.size();
146 ScopedMIDIHDR
midi_header(CreateMIDIHDR(message
.size()));
147 std::copy(message
.begin(), message
.end(), midi_header
->lpData
);
149 MMRESULT result
= midiOutPrepareHeader(midi_out_handle
, midi_header
.get(),
150 sizeof(*midi_header
));
151 if (result
!= MMSYSERR_NOERROR
) {
152 DLOG(ERROR
) << "Failed to prepare output buffer: "
153 << GetOutErrorMessage(result
);
158 midiOutLongMsg(midi_out_handle
, midi_header
.get(), sizeof(*midi_header
));
159 if (result
!= MMSYSERR_NOERROR
) {
160 DLOG(ERROR
) << "Failed to output long message: "
161 << GetOutErrorMessage(result
);
162 result
= midiOutUnprepareHeader(midi_out_handle
, midi_header
.get(),
163 sizeof(*midi_header
));
164 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
165 << "Failed to uninitialize output buffer: "
166 << GetOutErrorMessage(result
);
170 // The ownership of |midi_header| is moved to MOM_DONE event handler.
171 midi_header
.release();
174 template <size_t array_size
>
175 base::string16
AsString16(const wchar_t(&buffer
)[array_size
]) {
177 for (len
= 0; len
< array_size
; ++len
) {
178 if (buffer
[len
] == L
'\0')
181 return base::string16(buffer
, len
);
184 struct MidiDeviceInfo final
{
185 explicit MidiDeviceInfo(const MIDIINCAPS2W
& caps
)
186 : manufacturer_id(caps
.wMid
),
187 product_id(caps
.wPid
),
188 driver_version(caps
.vDriverVersion
),
189 product_name(AsString16(caps
.szPname
)),
190 usb_vendor_id(ExtractUsbVendorIdIfExists(caps
)),
191 usb_product_id(ExtractUsbProductIdIfExists(caps
)),
192 is_usb_device(IsUsbDevice(caps
)) {}
193 explicit MidiDeviceInfo(const MIDIOUTCAPS2W
& caps
)
194 : manufacturer_id(caps
.wMid
),
195 product_id(caps
.wPid
),
196 driver_version(caps
.vDriverVersion
),
197 product_name(AsString16(caps
.szPname
)),
198 usb_vendor_id(ExtractUsbVendorIdIfExists(caps
)),
199 usb_product_id(ExtractUsbProductIdIfExists(caps
)),
200 is_usb_device(IsUsbDevice(caps
)) {}
201 explicit MidiDeviceInfo(const MidiDeviceInfo
& info
)
202 : manufacturer_id(info
.manufacturer_id
),
203 product_id(info
.product_id
),
204 driver_version(info
.driver_version
),
205 product_name(info
.product_name
),
206 usb_vendor_id(info
.usb_vendor_id
),
207 usb_product_id(info
.usb_product_id
),
208 is_usb_device(info
.is_usb_device
) {}
209 // Currently only following entities are considered when testing the equality
210 // of two MIDI devices.
211 // TODO(toyoshim): Consider to calculate MIDIPort.id here and use it as the
212 // key. See crbug.com/467448. Then optimize the data for |MidiPortInfo|.
213 const uint16 manufacturer_id
;
214 const uint16 product_id
;
215 const uint32 driver_version
;
216 const base::string16 product_name
;
217 const uint16 usb_vendor_id
;
218 const uint16 usb_product_id
;
219 const bool is_usb_device
;
221 // Required to be used as the key of base::hash_map.
222 bool operator==(const MidiDeviceInfo
& that
) const {
223 return manufacturer_id
== that
.manufacturer_id
&&
224 product_id
== that
.product_id
&&
225 driver_version
== that
.driver_version
&&
226 product_name
== that
.product_name
&&
227 is_usb_device
== that
.is_usb_device
&&
228 (is_usb_device
&& usb_vendor_id
== that
.usb_vendor_id
&&
229 usb_product_id
== that
.usb_product_id
);
232 // Hash function to be used in base::hash_map.
234 size_t operator()(const MidiDeviceInfo
& info
) const {
235 size_t hash
= info
.manufacturer_id
;
237 hash
+= info
.product_id
;
239 hash
+= info
.driver_version
;
241 hash
+= info
.product_name
.size();
243 if (!info
.product_name
.empty()) {
244 hash
+= info
.product_name
[0];
247 hash
+= info
.usb_vendor_id
;
249 hash
+= info
.usb_product_id
;
255 static bool IsUsbDevice(const MIDIINCAPS2W
& caps
) {
256 return IS_COMPATIBLE_USBAUDIO_MID(&caps
.ManufacturerGuid
) &&
257 IS_COMPATIBLE_USBAUDIO_PID(&caps
.ProductGuid
);
259 static bool IsUsbDevice(const MIDIOUTCAPS2W
& caps
) {
260 return IS_COMPATIBLE_USBAUDIO_MID(&caps
.ManufacturerGuid
) &&
261 IS_COMPATIBLE_USBAUDIO_PID(&caps
.ProductGuid
);
263 static uint16
ExtractUsbVendorIdIfExists(const MIDIINCAPS2W
& caps
) {
264 if (!IS_COMPATIBLE_USBAUDIO_MID(&caps
.ManufacturerGuid
))
266 return EXTRACT_USBAUDIO_MID(&caps
.ManufacturerGuid
);
268 static uint16
ExtractUsbVendorIdIfExists(const MIDIOUTCAPS2W
& caps
) {
269 if (!IS_COMPATIBLE_USBAUDIO_MID(&caps
.ManufacturerGuid
))
271 return EXTRACT_USBAUDIO_MID(&caps
.ManufacturerGuid
);
273 static uint16
ExtractUsbProductIdIfExists(const MIDIINCAPS2W
& caps
) {
274 if (!IS_COMPATIBLE_USBAUDIO_PID(&caps
.ProductGuid
))
276 return EXTRACT_USBAUDIO_PID(&caps
.ProductGuid
);
278 static uint16
ExtractUsbProductIdIfExists(const MIDIOUTCAPS2W
& caps
) {
279 if (!IS_COMPATIBLE_USBAUDIO_PID(&caps
.ProductGuid
))
281 return EXTRACT_USBAUDIO_PID(&caps
.ProductGuid
);
285 std::string
GetManufacturerName(const MidiDeviceInfo
& info
) {
286 if (info
.is_usb_device
) {
287 const char* name
= device::UsbIds::GetVendorName(info
.usb_vendor_id
);
288 return std::string(name
? name
: "");
291 switch (info
.manufacturer_id
) {
293 return "Microsoft Corporation";
295 // TODO(toyoshim): Support other manufacture IDs. crbug.com/472341.
300 using PortNumberCache
= base::hash_map
<
302 std::priority_queue
<uint32
, std::vector
<uint32
>, std::greater
<uint32
>>,
303 MidiDeviceInfo::Hasher
>;
305 struct MidiInputDeviceState final
: base::RefCounted
<MidiInputDeviceState
> {
306 explicit MidiInputDeviceState(const MidiDeviceInfo
& device_info
)
307 : device_info(device_info
),
308 midi_handle(kInvalidMidiInHandle
),
311 start_time_initialized(false) {}
313 const MidiDeviceInfo device_info
;
315 ScopedMIDIHDR midi_header
;
316 // Since Win32 multimedia system uses a relative time offset from when
317 // |midiInStart| API is called, we need to record when it is called.
318 base::TimeTicks start_time
;
319 // 0-based port index. We will try to reuse the previous port index when the
320 // MIDI device is closed then reopened.
322 // A sequence number which represents how many times |port_index| is reused.
323 // We can remove this field if we decide not to clear unsent events
324 // when the device is disconnected.
325 // See https://github.com/WebAudio/web-midi-api/issues/133
327 // True if |start_time| is initialized. This field is not used so far, but
328 // kept for the debugging purpose.
329 bool start_time_initialized
;
332 friend class base::RefCounted
<MidiInputDeviceState
>;
333 ~MidiInputDeviceState() {}
336 struct MidiOutputDeviceState final
: base::RefCounted
<MidiOutputDeviceState
> {
337 explicit MidiOutputDeviceState(const MidiDeviceInfo
& device_info
)
338 : device_info(device_info
),
339 midi_handle(kInvalidMidiOutHandle
),
344 const MidiDeviceInfo device_info
;
345 HMIDIOUT midi_handle
;
346 // 0-based port index. We will try to reuse the previous port index when the
347 // MIDI device is closed then reopened.
349 // A sequence number which represents how many times |port_index| is reused.
350 // We can remove this field if we decide not to clear unsent events
351 // when the device is disconnected.
352 // See https://github.com/WebAudio/web-midi-api/issues/133
354 // True if the device is already closed and |midi_handle| is considered to be
356 // TODO(toyoshim): Use std::atomic<bool> when it is allowed in Chromium
358 volatile bool closed
;
361 friend class base::RefCounted
<MidiOutputDeviceState
>;
362 ~MidiOutputDeviceState() {}
365 // The core logic of MIDI device handling for Windows. Basically this class is
366 // shared among following 4 threads:
367 // 1. Chrome IO Thread
368 // 2. OS Multimedia Thread
373 // MidiManager runs on Chrome IO thread. Device change notification is
374 // delivered to the thread through the SystemMonitor service.
375 // OnDevicesChanged() callback is invoked to update the MIDI device list.
376 // Note that in the current implementation we will try to open all the
377 // existing devices in practice. This is OK because trying to reopen a MIDI
378 // device that is already opened would simply fail, and there is no unwilling
381 // OS Multimedia Thread:
382 // This thread is maintained by the OS as a part of MIDI runtime, and
383 // responsible for receiving all the system initiated events such as device
384 // close, and receiving data. For performance reasons, most of potentially
385 // blocking operations will be dispatched into Task Thread.
388 // This thread will be used to call back following methods of MidiManager.
389 // - MidiManager::CompleteInitialization
390 // - MidiManager::AddInputPort
391 // - MidiManager::AddOutputPort
392 // - MidiManager::SetInputPortState
393 // - MidiManager::SetOutputPortState
394 // - MidiManager::ReceiveMidiData
397 // This thread will be used to call Win32 APIs to send MIDI message at the
398 // specified time. We don't want to call MIDI send APIs on Task Thread
399 // because those APIs could be performed synchronously, hence they could block
400 // the caller thread for a while. See the comment in
401 // SendLongMidiMessageInternal for details. Currently we expect that the
402 // blocking time would be less than 1 second.
403 class MidiServiceWinImpl
: public MidiServiceWin
,
404 public base::SystemMonitor::DevicesChangedObserver
{
407 : delegate_(nullptr),
408 sender_thread_("Windows MIDI sender thread"),
409 task_thread_("Windows MIDI task thread"),
410 destructor_started(false) {}
412 ~MidiServiceWinImpl() final
{
413 // Start() and Stop() of the threads, and AddDevicesChangeObserver() and
414 // RemoveDevicesChangeObserver() should be called on the same thread.
415 CHECK(thread_checker_
.CalledOnValidThread());
417 destructor_started
= true;
418 base::SystemMonitor::Get()->RemoveDevicesChangedObserver(this);
420 std::vector
<HMIDIIN
> input_devices
;
422 base::AutoLock
auto_lock(input_ports_lock_
);
423 for (auto it
: input_device_map_
)
424 input_devices
.push_back(it
.first
);
427 for (const auto handle
: input_devices
) {
428 MMRESULT result
= midiInClose(handle
);
429 if (result
== MIDIERR_STILLPLAYING
) {
430 result
= midiInReset(handle
);
431 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
432 << "midiInReset failed: " << GetInErrorMessage(result
);
433 result
= midiInClose(handle
);
435 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
436 << "midiInClose failed: " << GetInErrorMessage(result
);
441 std::vector
<HMIDIOUT
> output_devices
;
443 base::AutoLock
auto_lock(output_ports_lock_
);
444 for (auto it
: output_device_map_
)
445 output_devices
.push_back(it
.first
);
448 for (const auto handle
: output_devices
) {
449 MMRESULT result
= midiOutClose(handle
);
450 if (result
== MIDIERR_STILLPLAYING
) {
451 result
= midiOutReset(handle
);
452 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
453 << "midiOutReset failed: " << GetOutErrorMessage(result
);
454 result
= midiOutClose(handle
);
456 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
457 << "midiOutClose failed: " << GetOutErrorMessage(result
);
461 sender_thread_
.Stop();
465 // MidiServiceWin overrides:
466 void InitializeAsync(MidiServiceWinDelegate
* delegate
) final
{
467 // Start() and Stop() of the threads, and AddDevicesChangeObserver() and
468 // RemoveDevicesChangeObserver() should be called on the same thread.
469 CHECK(thread_checker_
.CalledOnValidThread());
471 delegate_
= delegate
;
473 sender_thread_
.Start();
474 task_thread_
.Start();
476 // Start monitoring device changes. This should start before the
477 // following UpdateDeviceList() call not to miss the event happening
478 // between the call and the observer registration.
479 base::SystemMonitor::Get()->AddDevicesChangedObserver(this);
483 task_thread_
.message_loop()->PostTask(
485 base::Bind(&MidiServiceWinImpl::CompleteInitializationOnTaskThread
,
486 base::Unretained(this), MIDI_OK
));
489 void SendMidiDataAsync(uint32 port_number
,
490 const std::vector
<uint8
>& data
,
491 base::TimeTicks time
) final
{
492 if (destructor_started
) {
493 LOG(ERROR
) << "ThreadSafeSendData failed because MidiServiceWinImpl is "
494 "being destructed. port: " << port_number
;
497 auto state
= GetOutputDeviceFromPort(port_number
);
499 LOG(ERROR
) << "ThreadSafeSendData failed due to an invalid port number. "
500 << "port: " << port_number
;
505 << "ThreadSafeSendData failed because target port is already closed."
506 << "port: " << port_number
;
509 const auto now
= base::TimeTicks::Now();
511 sender_thread_
.message_loop()->PostDelayedTask(
512 FROM_HERE
, base::Bind(&MidiServiceWinImpl::SendOnSenderThread
,
513 base::Unretained(this), port_number
,
514 state
->port_age
, data
, time
),
517 sender_thread_
.message_loop()->PostTask(
518 FROM_HERE
, base::Bind(&MidiServiceWinImpl::SendOnSenderThread
,
519 base::Unretained(this), port_number
,
520 state
->port_age
, data
, time
));
524 // base::SystemMonitor::DevicesChangedObserver overrides:
525 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type
) final
{
526 CHECK(thread_checker_
.CalledOnValidThread());
527 if (destructor_started
)
530 switch (device_type
) {
531 case base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE
:
532 case base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE
:
533 // Add case of other unrelated device types here.
535 case base::SystemMonitor::DEVTYPE_UNKNOWN
:
536 // Interested in MIDI devices. Try updating the device list.
539 // No default here to capture new DeviceType by compile time.
544 scoped_refptr
<MidiInputDeviceState
> GetInputDeviceFromHandle(
545 HMIDIIN midi_handle
) {
546 base::AutoLock
auto_lock(input_ports_lock_
);
547 const auto it
= input_device_map_
.find(midi_handle
);
548 return (it
!= input_device_map_
.end() ? it
->second
: nullptr);
551 scoped_refptr
<MidiOutputDeviceState
> GetOutputDeviceFromHandle(
552 HMIDIOUT midi_handle
) {
553 base::AutoLock
auto_lock(output_ports_lock_
);
554 const auto it
= output_device_map_
.find(midi_handle
);
555 return (it
!= output_device_map_
.end() ? it
->second
: nullptr);
558 scoped_refptr
<MidiOutputDeviceState
> GetOutputDeviceFromPort(
559 uint32 port_number
) {
560 base::AutoLock
auto_lock(output_ports_lock_
);
561 if (output_ports_
.size() <= port_number
)
563 return output_ports_
[port_number
];
566 void UpdateDeviceList() {
567 task_thread_
.message_loop()->PostTask(
568 FROM_HERE
, base::Bind(&MidiServiceWinImpl::UpdateDeviceListOnTaskThread
,
569 base::Unretained(this)));
572 /////////////////////////////////////////////////////////////////////////////
573 // Callbacks on the OS multimedia thread.
574 /////////////////////////////////////////////////////////////////////////////
577 OnMidiInEventOnMainlyMultimediaThread(HMIDIIN midi_in_handle
,
582 MidiServiceWinImpl
* self
= reinterpret_cast<MidiServiceWinImpl
*>(instance
);
587 self
->OnMidiInOpen(midi_in_handle
);
590 self
->OnMidiInDataOnMultimediaThread(midi_in_handle
, param1
, param2
);
593 self
->OnMidiInLongDataOnMultimediaThread(midi_in_handle
, param1
,
597 self
->OnMidiInCloseOnMultimediaThread(midi_in_handle
);
602 void OnMidiInOpen(HMIDIIN midi_in_handle
) {
604 MMRESULT result
= midiInGetID(midi_in_handle
, &device_id
);
605 if (result
!= MMSYSERR_NOERROR
) {
606 DLOG(ERROR
) << "midiInGetID failed: " << GetInErrorMessage(result
);
609 MIDIINCAPS2W caps
= {};
610 result
= midiInGetDevCaps(device_id
, reinterpret_cast<LPMIDIINCAPSW
>(&caps
),
612 if (result
!= MMSYSERR_NOERROR
) {
613 DLOG(ERROR
) << "midiInGetDevCaps failed: " << GetInErrorMessage(result
);
617 make_scoped_refptr(new MidiInputDeviceState(MidiDeviceInfo(caps
)));
618 state
->midi_handle
= midi_in_handle
;
619 state
->midi_header
= CreateMIDIHDR(kBufferLength
);
620 const auto& state_device_info
= state
->device_info
;
621 bool add_new_port
= false;
622 uint32 port_number
= 0;
624 base::AutoLock
auto_lock(input_ports_lock_
);
625 const auto it
= unused_input_ports_
.find(state_device_info
);
626 if (it
== unused_input_ports_
.end()) {
627 port_number
= static_cast<uint32
>(input_ports_
.size());
629 input_ports_
.push_back(nullptr);
630 input_ports_ages_
.push_back(0);
632 port_number
= it
->second
.top();
634 if (it
->second
.empty()) {
635 unused_input_ports_
.erase(it
);
638 input_ports_
[port_number
] = state
;
640 input_ports_ages_
[port_number
] += 1;
641 input_device_map_
[input_ports_
[port_number
]->midi_handle
] =
642 input_ports_
[port_number
];
643 input_ports_
[port_number
]->port_index
= port_number
;
644 input_ports_
[port_number
]->port_age
= input_ports_ages_
[port_number
];
646 // Several initial startup tasks cannot be done in MIM_OPEN handler.
647 task_thread_
.message_loop()->PostTask(
648 FROM_HERE
, base::Bind(&MidiServiceWinImpl::StartInputDeviceOnTaskThread
,
649 base::Unretained(this), midi_in_handle
));
651 const MidiPortInfo
port_info(
652 // TODO(toyoshim): Use a hash ID insted crbug.com/467448
653 base::IntToString(static_cast<int>(port_number
)),
654 GetManufacturerName(state_device_info
),
655 base::WideToUTF8(state_device_info
.product_name
),
656 MmversionToString(state_device_info
.driver_version
),
658 task_thread_
.message_loop()->PostTask(
659 FROM_HERE
, base::Bind(&MidiServiceWinImpl::AddInputPortOnTaskThread
,
660 base::Unretained(this), port_info
));
662 task_thread_
.message_loop()->PostTask(
664 base::Bind(&MidiServiceWinImpl::SetInputPortStateOnTaskThread
,
665 base::Unretained(this), port_number
,
666 MidiPortState::MIDI_PORT_CONNECTED
));
670 void OnMidiInDataOnMultimediaThread(HMIDIIN midi_in_handle
,
673 auto state
= GetInputDeviceFromHandle(midi_in_handle
);
676 const uint8 status_byte
= static_cast<uint8
>(param1
& 0xff);
677 const uint8 first_data_byte
= static_cast<uint8
>((param1
>> 8) & 0xff);
678 const uint8 second_data_byte
= static_cast<uint8
>((param1
>> 16) & 0xff);
679 const DWORD elapsed_ms
= param2
;
680 const size_t len
= GetMidiMessageLength(status_byte
);
681 const uint8 kData
[] = {status_byte
, first_data_byte
, second_data_byte
};
682 std::vector
<uint8
> data
;
683 data
.assign(kData
, kData
+ len
);
684 DCHECK_LE(len
, arraysize(kData
));
685 // MIM_DATA/MIM_LONGDATA message treats the time when midiInStart() is
686 // called as the origin of |elapsed_ms|.
687 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd757284.aspx
688 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd757286.aspx
689 const base::TimeTicks event_time
=
690 state
->start_time
+ base::TimeDelta::FromMilliseconds(elapsed_ms
);
691 task_thread_
.message_loop()->PostTask(
692 FROM_HERE
, base::Bind(&MidiServiceWinImpl::ReceiveMidiDataOnTaskThread
,
693 base::Unretained(this), state
->port_index
, data
,
697 void OnMidiInLongDataOnMultimediaThread(HMIDIIN midi_in_handle
,
700 auto state
= GetInputDeviceFromHandle(midi_in_handle
);
703 MIDIHDR
* header
= reinterpret_cast<MIDIHDR
*>(param1
);
704 const DWORD elapsed_ms
= param2
;
705 MMRESULT result
= MMSYSERR_NOERROR
;
706 if (destructor_started
) {
707 if (state
->midi_header
&&
708 (state
->midi_header
->dwFlags
& MHDR_PREPARED
) == MHDR_PREPARED
) {
710 midiInUnprepareHeader(state
->midi_handle
, state
->midi_header
.get(),
711 sizeof(*state
->midi_header
));
712 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
713 << "Failed to uninitialize input buffer: "
714 << GetInErrorMessage(result
);
718 if (header
->dwBytesRecorded
> 0) {
719 const uint8
* src
= reinterpret_cast<const uint8
*>(header
->lpData
);
720 std::vector
<uint8
> data
;
721 data
.assign(src
, src
+ header
->dwBytesRecorded
);
722 // MIM_DATA/MIM_LONGDATA message treats the time when midiInStart() is
723 // called as the origin of |elapsed_ms|.
724 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd757284.aspx
725 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd757286.aspx
726 const base::TimeTicks event_time
=
727 state
->start_time
+ base::TimeDelta::FromMilliseconds(elapsed_ms
);
728 task_thread_
.message_loop()->PostTask(
730 base::Bind(&MidiServiceWinImpl::ReceiveMidiDataOnTaskThread
,
731 base::Unretained(this), state
->port_index
, data
,
734 result
= midiInAddBuffer(state
->midi_handle
, header
, sizeof(*header
));
735 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
736 << "Failed to attach input buffer: " << GetInErrorMessage(result
)
737 << "port number:" << state
->port_index
;
740 void OnMidiInCloseOnMultimediaThread(HMIDIIN midi_in_handle
) {
741 auto state
= GetInputDeviceFromHandle(midi_in_handle
);
744 const uint32 port_number
= state
->port_index
;
745 const auto device_info(state
->device_info
);
747 base::AutoLock
auto_lock(input_ports_lock_
);
748 input_device_map_
.erase(state
->midi_handle
);
749 input_ports_
[port_number
] = nullptr;
750 input_ports_ages_
[port_number
] += 1;
751 unused_input_ports_
[device_info
].push(port_number
);
753 task_thread_
.message_loop()->PostTask(
755 base::Bind(&MidiServiceWinImpl::SetInputPortStateOnTaskThread
,
756 base::Unretained(this), port_number
,
757 MIDI_PORT_DISCONNECTED
));
761 OnMidiOutEventOnMainlyMultimediaThread(HMIDIOUT midi_out_handle
,
766 MidiServiceWinImpl
* self
= reinterpret_cast<MidiServiceWinImpl
*>(instance
);
771 self
->OnMidiOutOpen(midi_out_handle
, param1
, param2
);
774 self
->OnMidiOutDoneOnMultimediaThread(midi_out_handle
, param1
);
777 self
->OnMidiOutCloseOnMultimediaThread(midi_out_handle
);
782 void OnMidiOutOpen(HMIDIOUT midi_out_handle
,
786 MMRESULT result
= midiOutGetID(midi_out_handle
, &device_id
);
787 if (result
!= MMSYSERR_NOERROR
) {
788 DLOG(ERROR
) << "midiOutGetID failed: " << GetOutErrorMessage(result
);
791 MIDIOUTCAPS2W caps
= {};
792 result
= midiOutGetDevCaps(
793 device_id
, reinterpret_cast<LPMIDIOUTCAPSW
>(&caps
), sizeof(caps
));
794 if (result
!= MMSYSERR_NOERROR
) {
795 DLOG(ERROR
) << "midiInGetDevCaps failed: " << GetOutErrorMessage(result
);
799 make_scoped_refptr(new MidiOutputDeviceState(MidiDeviceInfo(caps
)));
800 state
->midi_handle
= midi_out_handle
;
801 const auto& state_device_info
= state
->device_info
;
802 bool add_new_port
= false;
803 uint32 port_number
= 0;
805 base::AutoLock
auto_lock(output_ports_lock_
);
806 const auto it
= unused_output_ports_
.find(state_device_info
);
807 if (it
== unused_output_ports_
.end()) {
808 port_number
= static_cast<uint32
>(output_ports_
.size());
810 output_ports_
.push_back(nullptr);
811 output_ports_ages_
.push_back(0);
813 port_number
= it
->second
.top();
815 if (it
->second
.empty())
816 unused_output_ports_
.erase(it
);
818 output_ports_
[port_number
] = state
;
819 output_ports_ages_
[port_number
] += 1;
820 output_device_map_
[output_ports_
[port_number
]->midi_handle
] =
821 output_ports_
[port_number
];
822 output_ports_
[port_number
]->port_index
= port_number
;
823 output_ports_
[port_number
]->port_age
= output_ports_ages_
[port_number
];
826 const MidiPortInfo
port_info(
827 // TODO(toyoshim): Use a hash ID insted. crbug.com/467448
828 base::IntToString(static_cast<int>(port_number
)),
829 GetManufacturerName(state_device_info
),
830 base::WideToUTF8(state_device_info
.product_name
),
831 MmversionToString(state_device_info
.driver_version
),
833 task_thread_
.message_loop()->PostTask(
834 FROM_HERE
, base::Bind(&MidiServiceWinImpl::AddOutputPortOnTaskThread
,
835 base::Unretained(this), port_info
));
837 task_thread_
.message_loop()->PostTask(
839 base::Bind(&MidiServiceWinImpl::SetOutputPortStateOnTaskThread
,
840 base::Unretained(this), port_number
, MIDI_PORT_CONNECTED
));
844 void OnMidiOutDoneOnMultimediaThread(HMIDIOUT midi_out_handle
,
846 auto state
= GetOutputDeviceFromHandle(midi_out_handle
);
849 // Take ownership of the MIDIHDR object.
850 ScopedMIDIHDR
header(reinterpret_cast<MIDIHDR
*>(param1
));
853 MMRESULT result
= midiOutUnprepareHeader(state
->midi_handle
, header
.get(),
855 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
)
856 << "Failed to uninitialize output buffer: "
857 << GetOutErrorMessage(result
);
860 void OnMidiOutCloseOnMultimediaThread(HMIDIOUT midi_out_handle
) {
861 auto state
= GetOutputDeviceFromHandle(midi_out_handle
);
864 const uint32 port_number
= state
->port_index
;
865 const auto device_info(state
->device_info
);
867 base::AutoLock
auto_lock(output_ports_lock_
);
868 output_device_map_
.erase(state
->midi_handle
);
869 output_ports_
[port_number
] = nullptr;
870 output_ports_ages_
[port_number
] += 1;
871 unused_output_ports_
[device_info
].push(port_number
);
872 state
->closed
= true;
874 task_thread_
.message_loop()->PostTask(
876 base::Bind(&MidiServiceWinImpl::SetOutputPortStateOnTaskThread
,
877 base::Unretained(this), port_number
,
878 MIDI_PORT_DISCONNECTED
));
881 /////////////////////////////////////////////////////////////////////////////
882 // Callbacks on the sender thread.
883 /////////////////////////////////////////////////////////////////////////////
885 void AssertOnSenderThread() {
886 DCHECK_EQ(sender_thread_
.thread_id(), base::PlatformThread::CurrentId());
889 void SendOnSenderThread(uint32 port_number
,
891 const std::vector
<uint8
>& data
,
892 base::TimeTicks time
) {
893 AssertOnSenderThread();
894 if (destructor_started
) {
895 LOG(ERROR
) << "ThreadSafeSendData failed because MidiServiceWinImpl is "
896 "being destructed. port: " << port_number
;
898 auto state
= GetOutputDeviceFromPort(port_number
);
900 LOG(ERROR
) << "ThreadSafeSendData failed due to an invalid port number. "
901 << "port: " << port_number
;
906 << "ThreadSafeSendData failed because target port is already closed."
907 << "port: " << port_number
;
910 if (state
->port_age
!= port_age
) {
912 << "ThreadSafeSendData failed because target port is being closed."
913 << "port: " << port_number
<< "expected port age: " << port_age
914 << "actual port age: " << state
->port_age
;
917 // MIDI Running status must be filtered out.
918 MidiMessageQueue
message_queue(false);
919 message_queue
.Add(data
);
920 std::vector
<uint8
> message
;
922 if (destructor_started
)
926 message_queue
.Get(&message
);
929 // SendShortMidiMessageInternal can send a MIDI message up to 3 bytes.
930 if (message
.size() <= 3)
931 SendShortMidiMessageInternal(state
->midi_handle
, message
);
933 SendLongMidiMessageInternal(state
->midi_handle
, message
);
937 /////////////////////////////////////////////////////////////////////////////
938 // Callbacks on the task thread.
939 /////////////////////////////////////////////////////////////////////////////
941 void AssertOnTaskThread() {
942 DCHECK_EQ(task_thread_
.thread_id(), base::PlatformThread::CurrentId());
945 void UpdateDeviceListOnTaskThread() {
946 AssertOnTaskThread();
947 const UINT num_in_devices
= midiInGetNumDevs();
948 for (UINT device_id
= 0; device_id
< num_in_devices
; ++device_id
) {
949 // Here we use |CALLBACK_FUNCTION| to subscribe MIM_DATA, MIM_LONGDATA,
950 // MIM_OPEN, and MIM_CLOSE events.
951 // - MIM_DATA: This is the only way to get a short MIDI message with
952 // timestamp information.
953 // - MIM_LONGDATA: This is the only way to get a long MIDI message with
954 // timestamp information.
955 // - MIM_OPEN: This event is sent the input device is opened. Note that
956 // this message is called on the caller thread.
957 // - MIM_CLOSE: This event is sent when 1) midiInClose() is called, or 2)
958 // the MIDI device becomes unavailable for some reasons, e.g., the
959 // cable is disconnected. As for the former case, HMIDIOUT will be
960 // invalidated soon after the callback is finished. As for the later
961 // case, however, HMIDIOUT continues to be valid until midiInClose()
963 HMIDIIN midi_handle
= kInvalidMidiInHandle
;
964 const MMRESULT result
= midiInOpen(
965 &midi_handle
, device_id
,
966 reinterpret_cast<DWORD_PTR
>(&OnMidiInEventOnMainlyMultimediaThread
),
967 reinterpret_cast<DWORD_PTR
>(this), CALLBACK_FUNCTION
);
968 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
&& result
!= MMSYSERR_ALLOCATED
)
969 << "Failed to open output device. "
970 << " id: " << device_id
<< " message: " << GetInErrorMessage(result
);
973 const UINT num_out_devices
= midiOutGetNumDevs();
974 for (UINT device_id
= 0; device_id
< num_out_devices
; ++device_id
) {
975 // Here we use |CALLBACK_FUNCTION| to subscribe MOM_DONE, MOM_OPEN, and
977 // - MOM_DONE: SendLongMidiMessageInternal() relies on this event to clean
978 // up the backing store where a long MIDI message is stored.
979 // - MOM_OPEN: This event is sent the output device is opened. Note that
980 // this message is called on the caller thread.
981 // - MOM_CLOSE: This event is sent when 1) midiOutClose() is called, or 2)
982 // the MIDI device becomes unavailable for some reasons, e.g., the
983 // cable is disconnected. As for the former case, HMIDIOUT will be
984 // invalidated soon after the callback is finished. As for the later
985 // case, however, HMIDIOUT continues to be valid until midiOutClose()
987 HMIDIOUT midi_handle
= kInvalidMidiOutHandle
;
988 const MMRESULT result
= midiOutOpen(
989 &midi_handle
, device_id
,
990 reinterpret_cast<DWORD_PTR
>(&OnMidiOutEventOnMainlyMultimediaThread
),
991 reinterpret_cast<DWORD_PTR
>(this), CALLBACK_FUNCTION
);
992 DLOG_IF(ERROR
, result
!= MMSYSERR_NOERROR
&& result
!= MMSYSERR_ALLOCATED
)
993 << "Failed to open output device. "
994 << " id: " << device_id
<< " message: " << GetOutErrorMessage(result
);
998 void StartInputDeviceOnTaskThread(HMIDIIN midi_in_handle
) {
999 AssertOnTaskThread();
1000 auto state
= GetInputDeviceFromHandle(midi_in_handle
);
1004 midiInPrepareHeader(state
->midi_handle
, state
->midi_header
.get(),
1005 sizeof(*state
->midi_header
));
1006 if (result
!= MMSYSERR_NOERROR
) {
1007 DLOG(ERROR
) << "Failed to initialize input buffer: "
1008 << GetInErrorMessage(result
);
1011 result
= midiInAddBuffer(state
->midi_handle
, state
->midi_header
.get(),
1012 sizeof(*state
->midi_header
));
1013 if (result
!= MMSYSERR_NOERROR
) {
1014 DLOG(ERROR
) << "Failed to attach input buffer: "
1015 << GetInErrorMessage(result
);
1018 result
= midiInStart(state
->midi_handle
);
1019 if (result
!= MMSYSERR_NOERROR
) {
1020 DLOG(ERROR
) << "Failed to start input port: "
1021 << GetInErrorMessage(result
);
1024 state
->start_time
= base::TimeTicks::Now();
1025 state
->start_time_initialized
= true;
1028 void CompleteInitializationOnTaskThread(MidiResult result
) {
1029 AssertOnTaskThread();
1030 delegate_
->OnCompleteInitialization(result
);
1033 void ReceiveMidiDataOnTaskThread(uint32 port_index
,
1034 std::vector
<uint8
> data
,
1035 base::TimeTicks time
) {
1036 AssertOnTaskThread();
1037 delegate_
->OnReceiveMidiData(port_index
, data
, time
);
1040 void AddInputPortOnTaskThread(MidiPortInfo info
) {
1041 AssertOnTaskThread();
1042 delegate_
->OnAddInputPort(info
);
1045 void AddOutputPortOnTaskThread(MidiPortInfo info
) {
1046 AssertOnTaskThread();
1047 delegate_
->OnAddOutputPort(info
);
1050 void SetInputPortStateOnTaskThread(uint32 port_index
, MidiPortState state
) {
1051 AssertOnTaskThread();
1052 delegate_
->OnSetInputPortState(port_index
, state
);
1055 void SetOutputPortStateOnTaskThread(uint32 port_index
, MidiPortState state
) {
1056 AssertOnTaskThread();
1057 delegate_
->OnSetOutputPortState(port_index
, state
);
1060 /////////////////////////////////////////////////////////////////////////////
1062 /////////////////////////////////////////////////////////////////////////////
1064 // Does not take ownership.
1065 MidiServiceWinDelegate
* delegate_
;
1067 base::ThreadChecker thread_checker_
;
1069 base::Thread sender_thread_
;
1070 base::Thread task_thread_
;
1072 base::Lock input_ports_lock_
;
1073 base::hash_map
<HMIDIIN
, scoped_refptr
<MidiInputDeviceState
>>
1074 input_device_map_
; // GUARDED_BY(input_ports_lock_)
1075 PortNumberCache unused_input_ports_
; // GUARDED_BY(input_ports_lock_)
1076 std::vector
<scoped_refptr
<MidiInputDeviceState
>>
1077 input_ports_
; // GUARDED_BY(input_ports_lock_)
1078 std::vector
<uint64
> input_ports_ages_
; // GUARDED_BY(input_ports_lock_)
1080 base::Lock output_ports_lock_
;
1081 base::hash_map
<HMIDIOUT
, scoped_refptr
<MidiOutputDeviceState
>>
1082 output_device_map_
; // GUARDED_BY(output_ports_lock_)
1083 PortNumberCache unused_output_ports_
; // GUARDED_BY(output_ports_lock_)
1084 std::vector
<scoped_refptr
<MidiOutputDeviceState
>>
1085 output_ports_
; // GUARDED_BY(output_ports_lock_)
1086 std::vector
<uint64
> output_ports_ages_
; // GUARDED_BY(output_ports_lock_)
1088 // True if one thread reached MidiServiceWinImpl::~MidiServiceWinImpl(). Note
1089 // that MidiServiceWinImpl::~MidiServiceWinImpl() is blocked until
1090 // |sender_thread_|, and |task_thread_| are stopped.
1091 // This flag can be used as the signal that when background tasks must be
1093 // TODO(toyoshim): Use std::atomic<bool> when it is allowed.
1094 volatile bool destructor_started
;
1096 DISALLOW_COPY_AND_ASSIGN(MidiServiceWinImpl
);
1101 MidiManagerWin::MidiManagerWin() {
1104 MidiManagerWin::~MidiManagerWin() {
1105 midi_service_
.reset();
1108 void MidiManagerWin::StartInitialization() {
1109 midi_service_
.reset(new MidiServiceWinImpl
);
1110 // Note that |CompleteInitialization()| will be called from the callback.
1111 midi_service_
->InitializeAsync(this);
1114 void MidiManagerWin::DispatchSendMidiData(MidiManagerClient
* client
,
1116 const std::vector
<uint8
>& data
,
1121 base::TimeTicks time_to_send
= base::TimeTicks::Now();
1122 if (timestamp
!= 0.0) {
1124 base::TimeTicks() + base::TimeDelta::FromMicroseconds(
1125 timestamp
* base::Time::kMicrosecondsPerSecond
);
1127 midi_service_
->SendMidiDataAsync(port_index
, data
, time_to_send
);
1129 // TOOD(toyoshim): This calculation should be done when the date is actually
1131 client
->AccumulateMidiBytesSent(data
.size());
1134 void MidiManagerWin::OnCompleteInitialization(MidiResult result
) {
1135 CompleteInitialization(result
);
1138 void MidiManagerWin::OnAddInputPort(MidiPortInfo info
) {
1142 void MidiManagerWin::OnAddOutputPort(MidiPortInfo info
) {
1143 AddOutputPort(info
);
1146 void MidiManagerWin::OnSetInputPortState(uint32 port_index
,
1147 MidiPortState state
) {
1148 SetInputPortState(port_index
, state
);
1151 void MidiManagerWin::OnSetOutputPortState(uint32 port_index
,
1152 MidiPortState state
) {
1153 SetOutputPortState(port_index
, state
);
1156 void MidiManagerWin::OnReceiveMidiData(uint32 port_index
,
1157 const std::vector
<uint8
>& data
,
1158 base::TimeTicks time
) {
1159 ReceiveMidiData(port_index
, &data
[0], data
.size(), time
);
1162 MidiManager
* MidiManager::Create() {
1163 return new MidiManagerWin();
1167 } // namespace media