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(base::Callback
<void(Result result
)> callback
) {
29 initialize_callback_
= callback
;
30 scheduler_
.reset(new MidiScheduler(this));
31 // This is safe because EnumerateDevices cancels the operation on destruction.
32 device_factory_
->EnumerateDevices(
34 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone
,
35 base::Unretained(this)));
38 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient
* client
,
40 const std::vector
<uint8
>& data
,
42 if (port_index
>= output_streams_
.size()) {
43 // |port_index| is provided by a renderer so we can't believe that it is
44 // in the valid range.
47 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted.
48 // The task posted to the MidiScheduler will be disposed safely on deleting
50 scheduler_
->PostSendDataTask(
51 client
, data
.size(), timestamp
,
52 base::Bind(&UsbMidiOutputStream::Send
,
53 base::Unretained(output_streams_
[port_index
]), data
));
56 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice
* device
,
60 base::TimeTicks time
) {
63 input_stream_
->OnReceivedData(device
,
70 void MidiManagerUsb::OnDeviceAttached(scoped_ptr
<UsbMidiDevice
> device
) {
71 int device_id
= static_cast<int>(devices_
.size());
72 devices_
.push_back(device
.Pass());
73 AddPorts(devices_
.back(), device_id
);
76 void MidiManagerUsb::OnDeviceDetached(size_t index
) {
77 if (index
>= devices_
.size()) {
80 UsbMidiDevice
* device
= devices_
[index
];
81 for (size_t i
= 0; i
< output_streams_
.size(); ++i
) {
82 if (output_streams_
[i
]->jack().device
== device
) {
83 SetOutputPortState(static_cast<uint32
>(i
), MIDI_PORT_DISCONNECTED
);
86 const std::vector
<UsbMidiJack
>& input_jacks
= input_stream_
->jacks();
87 for (size_t i
= 0; i
< input_jacks
.size(); ++i
) {
88 if (input_jacks
[i
].device
== device
) {
89 SetInputPortState(static_cast<uint32
>(i
), MIDI_PORT_DISCONNECTED
);
94 void MidiManagerUsb::OnReceivedData(size_t jack_index
,
97 base::TimeTicks time
) {
98 ReceiveMidiData(static_cast<uint32
>(jack_index
), data
, size
, time
);
102 void MidiManagerUsb::OnEnumerateDevicesDone(bool result
,
103 UsbMidiDevice::Devices
* devices
) {
105 initialize_callback_
.Run(Result::INITIALIZATION_ERROR
);
108 input_stream_
.reset(new UsbMidiInputStream(this));
109 devices
->swap(devices_
);
110 for (size_t i
= 0; i
< devices_
.size(); ++i
) {
111 if (!AddPorts(devices_
[i
], static_cast<int>(i
))) {
112 initialize_callback_
.Run(Result::INITIALIZATION_ERROR
);
116 initialize_callback_
.Run(Result::OK
);
119 bool MidiManagerUsb::AddPorts(UsbMidiDevice
* device
, int device_id
) {
120 UsbMidiDescriptorParser parser
;
121 std::vector
<uint8
> descriptor
= device
->GetDescriptors();
122 const uint8
* data
= descriptor
.size() > 0 ? &descriptor
[0] : NULL
;
123 std::vector
<UsbMidiJack
> jacks
;
124 bool parse_result
= parser
.Parse(device
,
131 std::string
manufacturer(device
->GetManufacturer());
132 std::string
product_name(device
->GetProductName());
133 std::string
version(device
->GetDeviceVersion());
135 for (size_t j
= 0; j
< jacks
.size(); ++j
) {
136 // Port ID must be unique in a MIDI manager. This ID setting is
137 // sufficiently unique although there is no user-friendly meaning.
138 // TODO(yhirano): Use a hashed string as ID.
140 base::StringPrintf("port-%d-%ld", device_id
, static_cast<long>(j
)));
141 if (jacks
[j
].direction() == UsbMidiJack::DIRECTION_OUT
) {
142 output_streams_
.push_back(new UsbMidiOutputStream(jacks
[j
]));
143 AddOutputPort(MidiPortInfo(id
, manufacturer
, product_name
, version
,
146 DCHECK_EQ(jacks
[j
].direction(), UsbMidiJack::DIRECTION_IN
);
147 input_stream_
->Add(jacks
[j
]);
148 AddInputPort(MidiPortInfo(id
, manufacturer
, product_name
, version
,