| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/midi/midi_manager_usb.h" | 5 #include "media/midi/midi_manager_usb.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "media/midi/midi_scheduler.h" | 10 #include "media/midi/midi_scheduler.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 scheduler_.reset(new MidiScheduler(this)); | 30 scheduler_.reset(new MidiScheduler(this)); |
| 31 // This is safe because EnumerateDevices cancels the operation on destruction. | 31 // This is safe because EnumerateDevices cancels the operation on destruction. |
| 32 device_factory_->EnumerateDevices( | 32 device_factory_->EnumerateDevices( |
| 33 this, | 33 this, |
| 34 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone, | 34 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone, |
| 35 base::Unretained(this))); | 35 base::Unretained(this))); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client, | 38 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client, |
| 39 uint32_t port_index, | 39 uint32_t port_index, |
| 40 const std::vector<uint8>& data, | 40 const std::vector<uint8_t>& data, |
| 41 double timestamp) { | 41 double timestamp) { |
| 42 if (port_index >= output_streams_.size()) { | 42 if (port_index >= output_streams_.size()) { |
| 43 // |port_index| is provided by a renderer so we can't believe that it is | 43 // |port_index| is provided by a renderer so we can't believe that it is |
| 44 // in the valid range. | 44 // in the valid range. |
| 45 return; | 45 return; |
| 46 } | 46 } |
| 47 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted. | 47 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted. |
| 48 // The task posted to the MidiScheduler will be disposed safely on deleting | 48 // The task posted to the MidiScheduler will be disposed safely on deleting |
| 49 // the scheduler. | 49 // the scheduler. |
| 50 scheduler_->PostSendDataTask( | 50 scheduler_->PostSendDataTask( |
| 51 client, data.size(), timestamp, | 51 client, data.size(), timestamp, |
| 52 base::Bind(&UsbMidiOutputStream::Send, | 52 base::Bind(&UsbMidiOutputStream::Send, |
| 53 base::Unretained(output_streams_[port_index]), data)); | 53 base::Unretained(output_streams_[port_index]), data)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device, | 56 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device, |
| 57 int endpoint_number, | 57 int endpoint_number, |
| 58 const uint8* data, | 58 const uint8_t* data, |
| 59 size_t size, | 59 size_t size, |
| 60 base::TimeTicks time) { | 60 base::TimeTicks time) { |
| 61 if (!input_stream_) | 61 if (!input_stream_) |
| 62 return; | 62 return; |
| 63 input_stream_->OnReceivedData(device, | 63 input_stream_->OnReceivedData(device, |
| 64 endpoint_number, | 64 endpoint_number, |
| 65 data, | 65 data, |
| 66 size, | 66 size, |
| 67 time); | 67 time); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void MidiManagerUsb::OnDeviceAttached(scoped_ptr<UsbMidiDevice> device) { | 70 void MidiManagerUsb::OnDeviceAttached(scoped_ptr<UsbMidiDevice> device) { |
| 71 int device_id = static_cast<int>(devices_.size()); | 71 int device_id = static_cast<int>(devices_.size()); |
| 72 devices_.push_back(device.Pass()); | 72 devices_.push_back(device.Pass()); |
| 73 AddPorts(devices_.back(), device_id); | 73 AddPorts(devices_.back(), device_id); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void MidiManagerUsb::OnDeviceDetached(size_t index) { | 76 void MidiManagerUsb::OnDeviceDetached(size_t index) { |
| 77 if (index >= devices_.size()) { | 77 if (index >= devices_.size()) { |
| 78 return; | 78 return; |
| 79 } | 79 } |
| 80 UsbMidiDevice* device = devices_[index]; | 80 UsbMidiDevice* device = devices_[index]; |
| 81 for (size_t i = 0; i < output_streams_.size(); ++i) { | 81 for (size_t i = 0; i < output_streams_.size(); ++i) { |
| 82 if (output_streams_[i]->jack().device == device) { | 82 if (output_streams_[i]->jack().device == device) { |
| 83 SetOutputPortState(static_cast<uint32>(i), MIDI_PORT_DISCONNECTED); | 83 SetOutputPortState(static_cast<uint32_t>(i), MIDI_PORT_DISCONNECTED); |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 const std::vector<UsbMidiJack>& input_jacks = input_stream_->jacks(); | 86 const std::vector<UsbMidiJack>& input_jacks = input_stream_->jacks(); |
| 87 for (size_t i = 0; i < input_jacks.size(); ++i) { | 87 for (size_t i = 0; i < input_jacks.size(); ++i) { |
| 88 if (input_jacks[i].device == device) { | 88 if (input_jacks[i].device == device) { |
| 89 SetInputPortState(static_cast<uint32>(i), MIDI_PORT_DISCONNECTED); | 89 SetInputPortState(static_cast<uint32_t>(i), MIDI_PORT_DISCONNECTED); |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 void MidiManagerUsb::OnReceivedData(size_t jack_index, | 94 void MidiManagerUsb::OnReceivedData(size_t jack_index, |
| 95 const uint8* data, | 95 const uint8_t* data, |
| 96 size_t size, | 96 size_t size, |
| 97 base::TimeTicks time) { | 97 base::TimeTicks time) { |
| 98 ReceiveMidiData(static_cast<uint32>(jack_index), data, size, time); | 98 ReceiveMidiData(static_cast<uint32_t>(jack_index), data, size, time); |
| 99 } | 99 } |
| 100 | 100 |
| 101 | 101 |
| 102 void MidiManagerUsb::OnEnumerateDevicesDone(bool result, | 102 void MidiManagerUsb::OnEnumerateDevicesDone(bool result, |
| 103 UsbMidiDevice::Devices* devices) { | 103 UsbMidiDevice::Devices* devices) { |
| 104 if (!result) { | 104 if (!result) { |
| 105 initialize_callback_.Run(Result::INITIALIZATION_ERROR); | 105 initialize_callback_.Run(Result::INITIALIZATION_ERROR); |
| 106 return; | 106 return; |
| 107 } | 107 } |
| 108 input_stream_.reset(new UsbMidiInputStream(this)); | 108 input_stream_.reset(new UsbMidiInputStream(this)); |
| 109 devices->swap(devices_); | 109 devices->swap(devices_); |
| 110 for (size_t i = 0; i < devices_.size(); ++i) { | 110 for (size_t i = 0; i < devices_.size(); ++i) { |
| 111 if (!AddPorts(devices_[i], static_cast<int>(i))) { | 111 if (!AddPorts(devices_[i], static_cast<int>(i))) { |
| 112 initialize_callback_.Run(Result::INITIALIZATION_ERROR); | 112 initialize_callback_.Run(Result::INITIALIZATION_ERROR); |
| 113 return; | 113 return; |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 initialize_callback_.Run(Result::OK); | 116 initialize_callback_.Run(Result::OK); |
| 117 } | 117 } |
| 118 | 118 |
| 119 bool MidiManagerUsb::AddPorts(UsbMidiDevice* device, int device_id) { | 119 bool MidiManagerUsb::AddPorts(UsbMidiDevice* device, int device_id) { |
| 120 UsbMidiDescriptorParser parser; | 120 UsbMidiDescriptorParser parser; |
| 121 std::vector<uint8> descriptor = device->GetDescriptors(); | 121 std::vector<uint8_t> descriptor = device->GetDescriptors(); |
| 122 const uint8* data = descriptor.size() > 0 ? &descriptor[0] : NULL; | 122 const uint8_t* data = descriptor.size() > 0 ? &descriptor[0] : NULL; |
| 123 std::vector<UsbMidiJack> jacks; | 123 std::vector<UsbMidiJack> jacks; |
| 124 bool parse_result = parser.Parse(device, | 124 bool parse_result = parser.Parse(device, |
| 125 data, | 125 data, |
| 126 descriptor.size(), | 126 descriptor.size(), |
| 127 &jacks); | 127 &jacks); |
| 128 if (!parse_result) | 128 if (!parse_result) |
| 129 return false; | 129 return false; |
| 130 | 130 |
| 131 std::string manufacturer(device->GetManufacturer()); | 131 std::string manufacturer(device->GetManufacturer()); |
| 132 std::string product_name(device->GetProductName()); | 132 std::string product_name(device->GetProductName()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 147 input_stream_->Add(jacks[j]); | 147 input_stream_->Add(jacks[j]); |
| 148 AddInputPort(MidiPortInfo(id, manufacturer, product_name, version, | 148 AddInputPort(MidiPortInfo(id, manufacturer, product_name, version, |
| 149 MIDI_PORT_OPENED)); | 149 MIDI_PORT_OPENED)); |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 return true; | 152 return true; |
| 153 } | 153 } |
| 154 | 154 |
| 155 } // namespace midi | 155 } // namespace midi |
| 156 } // namespace media | 156 } // namespace media |
| OLD | NEW |