| 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/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "media/midi/midi_scheduler.h" |
| 12 #include "media/midi/usb_midi_descriptor_parser.h" | 13 #include "media/midi/usb_midi_descriptor_parser.h" |
| 13 #include "media/midi/usb_midi_device.h" | 14 #include "media/midi/usb_midi_device.h" |
| 14 #include "media/midi/usb_midi_input_stream.h" | 15 #include "media/midi/usb_midi_input_stream.h" |
| 15 #include "media/midi/usb_midi_jack.h" | 16 #include "media/midi/usb_midi_jack.h" |
| 16 #include "media/midi/usb_midi_output_stream.h" | 17 #include "media/midi/usb_midi_output_stream.h" |
| 17 | 18 |
| 18 namespace media { | 19 namespace media { |
| 19 | 20 |
| 20 MidiManagerUsb::MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> factory) | 21 MidiManagerUsb::MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> factory) |
| 21 : device_factory_(factory.Pass()) { | 22 : device_factory_(factory.Pass()) { |
| 22 } | 23 } |
| 23 | 24 |
| 24 MidiManagerUsb::~MidiManagerUsb() { | 25 MidiManagerUsb::~MidiManagerUsb() { |
| 25 } | 26 } |
| 26 | 27 |
| 27 void MidiManagerUsb::StartInitialization() { | 28 void MidiManagerUsb::StartInitialization() { |
| 28 Initialize( | 29 Initialize( |
| 29 base::Bind(&MidiManager::CompleteInitialization, base::Unretained(this))); | 30 base::Bind(&MidiManager::CompleteInitialization, base::Unretained(this))); |
| 30 } | 31 } |
| 31 | 32 |
| 32 void MidiManagerUsb::Initialize( | 33 void MidiManagerUsb::Initialize( |
| 33 base::Callback<void(MidiResult result)> callback) { | 34 base::Callback<void(MidiResult result)> callback) { |
| 34 initialize_callback_ = callback; | 35 initialize_callback_ = callback; |
| 36 scheduler_.reset(new MidiScheduler); |
| 35 // This is safe because EnumerateDevices cancels the operation on destruction. | 37 // This is safe because EnumerateDevices cancels the operation on destruction. |
| 36 device_factory_->EnumerateDevices( | 38 device_factory_->EnumerateDevices( |
| 37 this, | 39 this, |
| 38 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone, | 40 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone, |
| 39 base::Unretained(this))); | 41 base::Unretained(this))); |
| 40 } | 42 } |
| 41 | 43 |
| 42 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client, | 44 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client, |
| 43 uint32_t port_index, | 45 uint32_t port_index, |
| 44 const std::vector<uint8>& data, | 46 const std::vector<uint8>& data, |
| 45 double timestamp) { | 47 double timestamp) { |
| 46 if (port_index >= output_streams_.size()) { | 48 if (port_index >= output_streams_.size()) { |
| 47 // |port_index| is provided by a renderer so we can't believe that it is | 49 // |port_index| is provided by a renderer so we can't believe that it is |
| 48 // in the valid range. | 50 // in the valid range. |
| 49 return; | 51 return; |
| 50 } | 52 } |
| 51 output_streams_[port_index]->Send(data); | 53 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted. |
| 52 client->AccumulateMidiBytesSent(data.size()); | 54 // The task posted to the MidiScheduler will be disposed safely on deleting |
| 55 // the scheduler. |
| 56 scheduler_->PostSendDataTask( |
| 57 client, data.size(), timestamp, |
| 58 base::Bind(&UsbMidiOutputStream::Send, |
| 59 base::Unretained(output_streams_[port_index]), data)); |
| 53 } | 60 } |
| 54 | 61 |
| 55 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device, | 62 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device, |
| 56 int endpoint_number, | 63 int endpoint_number, |
| 57 const uint8* data, | 64 const uint8* data, |
| 58 size_t size, | 65 size_t size, |
| 59 base::TimeTicks time) { | 66 base::TimeTicks time) { |
| 60 if (!input_stream_) | 67 if (!input_stream_) |
| 61 return; | 68 return; |
| 62 input_stream_->OnReceivedData(device, | 69 input_stream_->OnReceivedData(device, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 port.id = base::StringPrintf("port-%d-%ld", | 157 port.id = base::StringPrintf("port-%d-%ld", |
| 151 device_id, | 158 device_id, |
| 152 static_cast<long>(j)); | 159 static_cast<long>(j)); |
| 153 AddInputPort(port); | 160 AddInputPort(port); |
| 154 } | 161 } |
| 155 } | 162 } |
| 156 return true; | 163 return true; |
| 157 } | 164 } |
| 158 | 165 |
| 159 } // namespace media | 166 } // namespace media |
| OLD | NEW |