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.
4
5 #include "media/midi/midi_manager_usb.h"
6
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 "media/midi/usb_midi_descriptor_parser.h"
12 #include "media/midi/usb_midi_device.h"
13 #include "media/midi/usb_midi_input_stream.h"
14 #include "media/midi/usb_midi_jack.h"
15 #include "media/midi/usb_midi_output_stream.h"
16
17 namespace media {
18
MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> factory)19 MidiManagerUsb::MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> factory)
20 : device_factory_(factory.Pass()) {
21 }
22
~MidiManagerUsb()23 MidiManagerUsb::~MidiManagerUsb() {
24 }
25
StartInitialization()26 void MidiManagerUsb::StartInitialization() {
27 Initialize(
28 base::Bind(&MidiManager::CompleteInitialization, base::Unretained(this)));
29 }
30
Initialize(base::Callback<void (MidiResult result)> callback)31 void MidiManagerUsb::Initialize(
32 base::Callback<void(MidiResult result)> callback) {
33 initialize_callback_ = callback;
34 // This is safe because EnumerateDevices cancels the operation on destruction.
35 device_factory_->EnumerateDevices(
36 this,
37 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone,
38 base::Unretained(this)));
39 }
40
DispatchSendMidiData(MidiManagerClient * client,uint32_t port_index,const std::vector<uint8> & data,double timestamp)41 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client,
42 uint32_t port_index,
43 const std::vector<uint8>& data,
44 double timestamp) {
45 DCHECK_LT(port_index, output_streams_.size());
46 output_streams_[port_index]->Send(data);
47 client->AccumulateMidiBytesSent(data.size());
48 }
49
ReceiveUsbMidiData(UsbMidiDevice * device,int endpoint_number,const uint8 * data,size_t size,base::TimeTicks time)50 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device,
51 int endpoint_number,
52 const uint8* data,
53 size_t size,
54 base::TimeTicks time) {
55 if (!input_stream_)
56 return;
57 input_stream_->OnReceivedData(device,
58 endpoint_number,
59 data,
60 size,
61 time);
62 }
63
OnReceivedData(size_t jack_index,const uint8 * data,size_t size,base::TimeTicks time)64 void MidiManagerUsb::OnReceivedData(size_t jack_index,
65 const uint8* data,
66 size_t size,
67 base::TimeTicks time) {
68 ReceiveMidiData(jack_index, data, size, time);
69 }
70
71
OnEnumerateDevicesDone(bool result,UsbMidiDevice::Devices * devices)72 void MidiManagerUsb::OnEnumerateDevicesDone(bool result,
73 UsbMidiDevice::Devices* devices) {
74 if (!result) {
75 initialize_callback_.Run(MIDI_INITIALIZATION_ERROR);
76 return;
77 }
78 devices->swap(devices_);
79 for (size_t i = 0; i < devices_.size(); ++i) {
80 UsbMidiDescriptorParser parser;
81 std::vector<uint8> descriptor = devices_[i]->GetDescriptor();
82 const uint8* data = descriptor.size() > 0 ? &descriptor[0] : NULL;
83 std::vector<UsbMidiJack> jacks;
84 bool parse_result = parser.Parse(devices_[i],
85 data,
86 descriptor.size(),
87 &jacks);
88 if (!parse_result) {
89 initialize_callback_.Run(MIDI_INITIALIZATION_ERROR);
90 return;
91 }
92 std::vector<UsbMidiJack> input_jacks;
93 for (size_t j = 0; j < jacks.size(); ++j) {
94 if (jacks[j].direction() == UsbMidiJack::DIRECTION_OUT) {
95 output_streams_.push_back(new UsbMidiOutputStream(jacks[j]));
96 // TODO(yhirano): Set appropriate properties.
97 AddOutputPort(MidiPortInfo());
98 } else {
99 DCHECK_EQ(jacks[j].direction(), UsbMidiJack::DIRECTION_IN);
100 input_jacks.push_back(jacks[j]);
101 // TODO(yhirano): Set appropriate properties.
102 AddInputPort(MidiPortInfo());
103 }
104 }
105 input_stream_.reset(new UsbMidiInputStream(input_jacks, this));
106 }
107 initialize_callback_.Run(MIDI_OK);
108 }
109
110 } // namespace media
111