[MediaRouter] Update MR-2-Extension's PostMessage to return boolean.
[chromium-blink-merge.git] / media / midi / midi_manager_usb.cc
blob3e096d233c7c3b55d3fad89117e92c8c60cb3377
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"
13 namespace media {
14 namespace midi {
16 MidiManagerUsb::MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> factory)
17 : device_factory_(factory.Pass()) {
20 MidiManagerUsb::~MidiManagerUsb() {
23 void MidiManagerUsb::StartInitialization() {
24 Initialize(
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(
34 this,
35 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone,
36 base::Unretained(this)));
39 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client,
40 uint32_t port_index,
41 const std::vector<uint8>& data,
42 double timestamp) {
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.
46 return;
48 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted.
49 // The task posted to the MidiScheduler will be disposed safely on deleting
50 // the scheduler.
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,
58 int endpoint_number,
59 const uint8* data,
60 size_t size,
61 base::TimeTicks time) {
62 if (!input_stream_)
63 return;
64 input_stream_->OnReceivedData(device,
65 endpoint_number,
66 data,
67 size,
68 time);
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()) {
79 return;
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,
96 const uint8* data,
97 size_t size,
98 base::TimeTicks time) {
99 ReceiveMidiData(static_cast<uint32>(jack_index), data, size, time);
103 void MidiManagerUsb::OnEnumerateDevicesDone(bool result,
104 UsbMidiDevice::Devices* devices) {
105 if (!result) {
106 initialize_callback_.Run(MIDI_INITIALIZATION_ERROR);
107 return;
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);
114 return;
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,
126 data,
127 descriptor.size(),
128 &jacks);
129 if (!parse_result)
130 return false;
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.
140 std::string 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,
145 MIDI_PORT_OPENED));
146 } else {
147 DCHECK_EQ(jacks[j].direction(), UsbMidiJack::DIRECTION_IN);
148 input_stream_->Add(jacks[j]);
149 AddInputPort(MidiPortInfo(id, manufacturer, product_name, version,
150 MIDI_PORT_OPENED));
153 return true;
156 } // namespace midi
157 } // namespace media