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/usb_midi_descriptor_parser.h"
13 #include "media/midi/usb_midi_device.h"
14 #include "media/midi/usb_midi_input_stream.h"
15 #include "media/midi/usb_midi_jack.h"
16 #include "media/midi/usb_midi_output_stream.h"
20 MidiManagerUsb::MidiManagerUsb(scoped_ptr
<UsbMidiDevice::Factory
> factory
)
21 : device_factory_(factory
.Pass()) {
24 MidiManagerUsb::~MidiManagerUsb() {
27 void MidiManagerUsb::StartInitialization() {
29 base::Bind(&MidiManager::CompleteInitialization
, base::Unretained(this)));
32 void MidiManagerUsb::Initialize(
33 base::Callback
<void(MidiResult result
)> callback
) {
34 initialize_callback_
= callback
;
35 // This is safe because EnumerateDevices cancels the operation on destruction.
36 device_factory_
->EnumerateDevices(
38 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone
,
39 base::Unretained(this)));
42 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient
* client
,
44 const std::vector
<uint8
>& data
,
46 if (port_index
>= output_streams_
.size()) {
47 // |port_index| is provided by a renderer so we can't believe that it is
48 // in the valid range.
51 output_streams_
[port_index
]->Send(data
);
52 client
->AccumulateMidiBytesSent(data
.size());
55 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice
* device
,
59 base::TimeTicks time
) {
62 input_stream_
->OnReceivedData(device
,
69 void MidiManagerUsb::OnDeviceAttached(scoped_ptr
<UsbMidiDevice
> device
) {
70 int device_id
= devices_
.size();
71 devices_
.push_back(device
.release());
72 AddPorts(devices_
.back(), device_id
);
75 void MidiManagerUsb::OnDeviceDetached(size_t index
) {
76 if (index
>= devices_
.size()) {
79 UsbMidiDevice
* device
= devices_
[index
];
80 for (size_t i
= 0; i
< output_streams_
.size(); ++i
) {
81 if (output_streams_
[i
]->jack().device
== device
) {
82 SetOutputPortState(i
, MIDI_PORT_DISCONNECTED
);
85 const std::vector
<UsbMidiJack
>& input_jacks
= input_stream_
->jacks();
86 for (size_t i
= 0; i
< input_jacks
.size(); ++i
) {
87 if (input_jacks
[i
].device
== device
) {
88 SetInputPortState(i
, MIDI_PORT_DISCONNECTED
);
93 void MidiManagerUsb::OnReceivedData(size_t jack_index
,
96 base::TimeTicks time
) {
97 ReceiveMidiData(jack_index
, data
, size
, time
);
101 void MidiManagerUsb::OnEnumerateDevicesDone(bool result
,
102 UsbMidiDevice::Devices
* devices
) {
104 initialize_callback_
.Run(MIDI_INITIALIZATION_ERROR
);
107 input_stream_
.reset(new UsbMidiInputStream(this));
108 devices
->swap(devices_
);
109 for (size_t i
= 0; i
< devices_
.size(); ++i
) {
110 if (!AddPorts(devices_
[i
], i
)) {
111 initialize_callback_
.Run(MIDI_INITIALIZATION_ERROR
);
115 initialize_callback_
.Run(MIDI_OK
);
118 bool MidiManagerUsb::AddPorts(UsbMidiDevice
* device
, int device_id
) {
119 UsbMidiDescriptorParser parser
;
120 std::vector
<uint8
> descriptor
= device
->GetDescriptor();
121 const uint8
* data
= descriptor
.size() > 0 ? &descriptor
[0] : NULL
;
122 std::vector
<UsbMidiJack
> jacks
;
123 bool parse_result
= parser
.Parse(device
,
130 for (size_t j
= 0; j
< jacks
.size(); ++j
) {
131 if (jacks
[j
].direction() == UsbMidiJack::DIRECTION_OUT
) {
132 output_streams_
.push_back(new UsbMidiOutputStream(jacks
[j
]));
133 // TODO(yhirano): Set appropriate properties.
134 // TODO(yhiran): Port ID should contain product ID / vendor ID.
135 // Port ID must be unique in a MIDI manager. This (and the below) ID
136 // setting is sufficiently unique although there is no user-friendly
139 port
.state
= MIDI_PORT_OPENED
;
140 port
.id
= base::StringPrintf("port-%d-%ld",
142 static_cast<long>(j
));
145 DCHECK_EQ(jacks
[j
].direction(), UsbMidiJack::DIRECTION_IN
);
146 input_stream_
->Add(jacks
[j
]);
147 // TODO(yhirano): Set appropriate properties.
149 port
.state
= MIDI_PORT_OPENED
;
150 port
.id
= base::StringPrintf("port-%d-%ld",
152 static_cast<long>(j
));