Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 19 namespace { | |
| 20 | |
| 21 // Noop callback for (sync) Initialize. | |
| 22 // TODO(yhirano): This function should go away when | |
| 23 // MIDIManager::Initialize() becomes asynchronous. | |
| 24 void Noop(bool result) { | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 MIDIManagerUsb::MIDIManagerUsb(scoped_ptr<UsbMidiDevice::Factory> factory) | |
| 30 : device_factory_(factory.Pass()) { | |
| 31 } | |
| 32 | |
| 33 MIDIManagerUsb::~MIDIManagerUsb() { | |
| 34 } | |
| 35 | |
| 36 bool MIDIManagerUsb::Initialize() { | |
| 37 Initialize(base::Bind(Noop)); | |
| 38 return true; | |
| 39 } | |
| 40 | |
| 41 void MIDIManagerUsb::Initialize(base::Callback<void(bool result)> callback) { | |
| 42 initialize_callback_ = callback; | |
| 43 // This is safe because EnumerateDevices cancels the operation on destruction. | |
| 44 device_factory_->EnumerateDevices( | |
| 45 this, | |
| 46 base::Bind(&MIDIManagerUsb::OnEnumerateDevicesDone, | |
| 47 base::Unretained(this))); | |
| 48 } | |
| 49 | |
| 50 void MIDIManagerUsb::DispatchSendMIDIData(MIDIManagerClient* client, | |
| 51 uint32_t port_index, | |
| 52 const std::vector<uint8>& data, | |
| 53 double timestamp) { | |
| 54 DCHECK_LT(port_index, output_streams_.size()); | |
| 55 output_streams_[port_index]->Send(data); | |
| 56 client->AccumulateMIDIBytesSent(data.size()); | |
| 57 } | |
| 58 | |
| 59 | |
|
scherkus (not reviewing)
2014/01/27 19:23:49
nit: remove extra blank line
yhirano
2014/01/28 04:31:05
Done.
| |
| 60 void MIDIManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device, | |
| 61 int endpoint_number, | |
| 62 const uint8* data, | |
| 63 size_t size, | |
| 64 double timestamp) { | |
| 65 if (!input_stream_) | |
| 66 return; | |
| 67 input_stream_->OnReceivedData(device, | |
| 68 endpoint_number, | |
| 69 data, | |
| 70 size, | |
| 71 timestamp); | |
| 72 } | |
| 73 | |
| 74 void MIDIManagerUsb::OnReceivedData(size_t jack_index, | |
| 75 const uint8* data, | |
| 76 size_t size, | |
| 77 double timestamp) { | |
| 78 ReceiveMIDIData(jack_index, data, size, timestamp); | |
| 79 } | |
| 80 | |
| 81 | |
| 82 void MIDIManagerUsb::OnEnumerateDevicesDone(bool result, | |
| 83 UsbMidiDevice::Devices* devices) { | |
| 84 if (!result) { | |
| 85 initialize_callback_.Run(false); | |
| 86 return; | |
| 87 } | |
| 88 devices->swap(devices_); | |
| 89 for (size_t i = 0; i < devices_.size(); ++i) { | |
| 90 UsbMidiDescriptorParser parser; | |
| 91 std::vector<uint8> descriptor = devices_[i]->GetDescriptor(); | |
| 92 const uint8* data = descriptor.size() > 0 ? &descriptor[0] : NULL; | |
| 93 std::vector<UsbMidiJack> jacks; | |
| 94 bool parse_result = parser.Parse(devices_[i], | |
| 95 data, | |
| 96 descriptor.size(), | |
| 97 &jacks); | |
| 98 if (!parse_result) { | |
| 99 initialize_callback_.Run(false); | |
| 100 return; | |
| 101 } | |
| 102 std::vector<UsbMidiJack> input_jacks; | |
| 103 for (size_t j = 0; j < jacks.size(); ++j) { | |
| 104 if (jacks[j].direction() == UsbMidiJack::OUT) { | |
| 105 output_streams_.push_back(new UsbMidiOutputStream(jacks[j])); | |
| 106 // TODO(yhirano): Set appropriate properties. | |
| 107 AddOutputPort(MIDIPortInfo()); | |
| 108 } else { | |
| 109 DCHECK_EQ(jacks[j].direction(), UsbMidiJack::IN); | |
| 110 input_jacks.push_back(jacks[j]); | |
| 111 // TODO(yhirano): Set appropriate properties. | |
| 112 AddInputPort(MIDIPortInfo()); | |
| 113 } | |
| 114 } | |
| 115 input_stream_.reset(new UsbMidiInputStream(input_jacks, this)); | |
| 116 } | |
| 117 initialize_callback_.Run(true); | |
| 118 } | |
| 119 | |
| 120 } // namespace media | |
| OLD | NEW |