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/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "media/midi/midi_scheduler.h"
11 #include "media/midi/usb_midi_descriptor_parser.h"
16 MidiManagerUsb::MidiManagerUsb(scoped_ptr
<UsbMidiDevice::Factory
> factory
)
17 : device_factory_(factory
.Pass()) {
20 MidiManagerUsb::~MidiManagerUsb() {
23 void MidiManagerUsb::StartInitialization() {
25 base::Bind(&MidiManager::CompleteInitialization
, base::Unretained(this)));
28 void MidiManagerUsb::Initialize(
29 base::Callback
<void(MidiResult result
)> callback
) {
30 initialize_callback_
= callback
;
31 scheduler_
.reset(new MidiScheduler(this));
32 // This is safe because EnumerateDevices cancels the operation on destruction.
33 device_factory_
->EnumerateDevices(
35 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone
,
36 base::Unretained(this)));
39 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient
* client
,
41 const std::vector
<uint8
>& data
,
43 if (port_index
>= output_streams_
.size()) {
44 // |port_index| is provided by a renderer so we can't believe that it is
45 // in the valid range.
48 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted.
49 // The task posted to the MidiScheduler will be disposed safely on deleting
51 scheduler_
->PostSendDataTask(
52 client
, data
.size(), timestamp
,
53 base::Bind(&UsbMidiOutputStream::Send
,
54 base::Unretained(output_streams_
[port_index
]), data
));
57 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice
* device
,
61 base::TimeTicks time
) {
64 input_stream_
->OnReceivedData(device
,
71 void MidiManagerUsb::OnDeviceAttached(scoped_ptr
<UsbMidiDevice
> device
) {
72 int device_id
= static_cast<int>(devices_
.size());
73 devices_
.push_back(device
.Pass());
74 AddPorts(devices_
.back(), device_id
);
77 void MidiManagerUsb::OnDeviceDetached(size_t index
) {
78 if (index
>= devices_
.size()) {
81 UsbMidiDevice
* device
= devices_
[index
];
82 for (size_t i
= 0; i
< output_streams_
.size(); ++i
) {
83 if (output_streams_
[i
]->jack().device
== device
) {
84 SetOutputPortState(static_cast<uint32
>(i
), MIDI_PORT_DISCONNECTED
);
87 const std::vector
<UsbMidiJack
>& input_jacks
= input_stream_
->jacks();
88 for (size_t i
= 0; i
< input_jacks
.size(); ++i
) {
89 if (input_jacks
[i
].device
== device
) {
90 SetInputPortState(static_cast<uint32
>(i
), MIDI_PORT_DISCONNECTED
);
95 void MidiManagerUsb::OnReceivedData(size_t jack_index
,
98 base::TimeTicks time
) {
99 ReceiveMidiData(static_cast<uint32
>(jack_index
), data
, size
, time
);
103 void MidiManagerUsb::OnEnumerateDevicesDone(bool result
,
104 UsbMidiDevice::Devices
* devices
) {
106 initialize_callback_
.Run(MIDI_INITIALIZATION_ERROR
);
109 input_stream_
.reset(new UsbMidiInputStream(this));
110 devices
->swap(devices_
);
111 for (size_t i
= 0; i
< devices_
.size(); ++i
) {
112 if (!AddPorts(devices_
[i
], static_cast<int>(i
))) {
113 initialize_callback_
.Run(MIDI_INITIALIZATION_ERROR
);
117 initialize_callback_
.Run(MIDI_OK
);
120 bool MidiManagerUsb::AddPorts(UsbMidiDevice
* device
, int device_id
) {
121 UsbMidiDescriptorParser parser
;
122 std::vector
<uint8
> descriptor
= device
->GetDescriptors();
123 const uint8
* data
= descriptor
.size() > 0 ? &descriptor
[0] : NULL
;
124 std::vector
<UsbMidiJack
> jacks
;
125 bool parse_result
= parser
.Parse(device
,
132 std::string
manufacturer(device
->GetManufacturer());
133 std::string
product_name(device
->GetProductName());
134 std::string
version(device
->GetDeviceVersion());
136 for (size_t j
= 0; j
< jacks
.size(); ++j
) {
137 // Port ID must be unique in a MIDI manager. This ID setting is
138 // sufficiently unique although there is no user-friendly meaning.
139 // TODO(yhirano): Use a hashed string as ID.
141 base::StringPrintf("port-%d-%ld", device_id
, static_cast<long>(j
)));
142 if (jacks
[j
].direction() == UsbMidiJack::DIRECTION_OUT
) {
143 output_streams_
.push_back(new UsbMidiOutputStream(jacks
[j
]));
144 AddOutputPort(MidiPortInfo(id
, manufacturer
, product_name
, version
,
147 DCHECK_EQ(jacks
[j
].direction(), UsbMidiJack::DIRECTION_IN
);
148 input_stream_
->Add(jacks
[j
]);
149 AddInputPort(MidiPortInfo(id
, manufacturer
, product_name
, version
,