1 // Copyright 2014 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_usb.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "media/midi/midi_scheduler.h"
13 #include "media/midi/usb_midi_descriptor_parser.h"
14 #include "media/midi/usb_midi_device.h"
15 #include "media/midi/usb_midi_input_stream.h"
16 #include "media/midi/usb_midi_jack.h"
17 #include "media/midi/usb_midi_output_stream.h"
21 MidiManagerUsb::MidiManagerUsb(scoped_ptr
<UsbMidiDevice::Factory
> factory
)
22 : device_factory_(factory
.Pass()) {
25 MidiManagerUsb::~MidiManagerUsb() {
28 void MidiManagerUsb::StartInitialization() {
30 base::Bind(&MidiManager::CompleteInitialization
, base::Unretained(this)));
33 void MidiManagerUsb::Initialize(
34 base::Callback
<void(MidiResult result
)> callback
) {
35 initialize_callback_
= callback
;
36 scheduler_
.reset(new MidiScheduler(this));
37 // This is safe because EnumerateDevices cancels the operation on destruction.
38 device_factory_
->EnumerateDevices(
40 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone
,
41 base::Unretained(this)));
44 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient
* client
,
46 const std::vector
<uint8
>& data
,
48 if (port_index
>= output_streams_
.size()) {
49 // |port_index| is provided by a renderer so we can't believe that it is
50 // in the valid range.
53 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted.
54 // The task posted to the MidiScheduler will be disposed safely on deleting
56 scheduler_
->PostSendDataTask(
57 client
, data
.size(), timestamp
,
58 base::Bind(&UsbMidiOutputStream::Send
,
59 base::Unretained(output_streams_
[port_index
]), data
));
62 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice
* device
,
66 base::TimeTicks time
) {
69 input_stream_
->OnReceivedData(device
,
76 void MidiManagerUsb::OnDeviceAttached(scoped_ptr
<UsbMidiDevice
> device
) {
77 int device_id
= devices_
.size();
78 devices_
.push_back(device
.release());
79 AddPorts(devices_
.back(), device_id
);
82 void MidiManagerUsb::OnDeviceDetached(size_t index
) {
83 if (index
>= devices_
.size()) {
86 UsbMidiDevice
* device
= devices_
[index
];
87 for (size_t i
= 0; i
< output_streams_
.size(); ++i
) {
88 if (output_streams_
[i
]->jack().device
== device
) {
89 SetOutputPortState(i
, MIDI_PORT_DISCONNECTED
);
92 const std::vector
<UsbMidiJack
>& input_jacks
= input_stream_
->jacks();
93 for (size_t i
= 0; i
< input_jacks
.size(); ++i
) {
94 if (input_jacks
[i
].device
== device
) {
95 SetInputPortState(i
, MIDI_PORT_DISCONNECTED
);
100 void MidiManagerUsb::OnReceivedData(size_t jack_index
,
103 base::TimeTicks time
) {
104 ReceiveMidiData(jack_index
, data
, size
, time
);
108 void MidiManagerUsb::OnEnumerateDevicesDone(bool result
,
109 UsbMidiDevice::Devices
* devices
) {
111 initialize_callback_
.Run(MIDI_INITIALIZATION_ERROR
);
114 input_stream_
.reset(new UsbMidiInputStream(this));
115 devices
->swap(devices_
);
116 for (size_t i
= 0; i
< devices_
.size(); ++i
) {
117 if (!AddPorts(devices_
[i
], i
)) {
118 initialize_callback_
.Run(MIDI_INITIALIZATION_ERROR
);
122 initialize_callback_
.Run(MIDI_OK
);
125 bool MidiManagerUsb::AddPorts(UsbMidiDevice
* device
, int device_id
) {
126 UsbMidiDescriptorParser parser
;
127 std::vector
<uint8
> descriptor
= device
->GetDescriptor();
128 const uint8
* data
= descriptor
.size() > 0 ? &descriptor
[0] : NULL
;
129 std::vector
<UsbMidiJack
> jacks
;
130 bool parse_result
= parser
.Parse(device
,
137 for (size_t j
= 0; j
< jacks
.size(); ++j
) {
138 if (jacks
[j
].direction() == UsbMidiJack::DIRECTION_OUT
) {
139 output_streams_
.push_back(new UsbMidiOutputStream(jacks
[j
]));
140 // TODO(yhirano): Set appropriate properties.
141 // TODO(yhiran): Port ID should contain product ID / vendor ID.
142 // Port ID must be unique in a MIDI manager. This (and the below) ID
143 // setting is sufficiently unique although there is no user-friendly
146 port
.state
= MIDI_PORT_OPENED
;
147 port
.id
= base::StringPrintf("port-%d-%ld",
149 static_cast<long>(j
));
152 DCHECK_EQ(jacks
[j
].direction(), UsbMidiJack::DIRECTION_IN
);
153 input_stream_
->Add(jacks
[j
]);
154 // TODO(yhirano): Set appropriate properties.
156 port
.state
= MIDI_PORT_OPENED
;
157 port
.id
= base::StringPrintf("port-%d-%ld",
159 static_cast<long>(j
));