Delete unused downloads page asset.
[chromium-blink-merge.git] / media / midi / midi_manager_usb.cc
blobf493b151c884e89cb4f09ab22bbf9781d0a63ae0
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(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(
33 this,
34 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone,
35 base::Unretained(this)));
38 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client,
39 uint32_t port_index,
40 const std::vector<uint8>& data,
41 double timestamp) {
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.
45 return;
47 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted.
48 // The task posted to the MidiScheduler will be disposed safely on deleting
49 // the scheduler.
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,
57 int endpoint_number,
58 const uint8* data,
59 size_t size,
60 base::TimeTicks time) {
61 if (!input_stream_)
62 return;
63 input_stream_->OnReceivedData(device,
64 endpoint_number,
65 data,
66 size,
67 time);
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()) {
78 return;
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,
95 const uint8* data,
96 size_t size,
97 base::TimeTicks time) {
98 ReceiveMidiData(static_cast<uint32>(jack_index), data, size, time);
102 void MidiManagerUsb::OnEnumerateDevicesDone(bool result,
103 UsbMidiDevice::Devices* devices) {
104 if (!result) {
105 initialize_callback_.Run(Result::INITIALIZATION_ERROR);
106 return;
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);
113 return;
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,
125 data,
126 descriptor.size(),
127 &jacks);
128 if (!parse_result)
129 return false;
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.
139 std::string 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,
144 MIDI_PORT_OPENED));
145 } else {
146 DCHECK_EQ(jacks[j].direction(), UsbMidiJack::DIRECTION_IN);
147 input_stream_->Add(jacks[j]);
148 AddInputPort(MidiPortInfo(id, manufacturer, product_name, version,
149 MIDI_PORT_OPENED));
152 return true;
155 } // namespace midi
156 } // namespace media