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