yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 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 | #ifndef MEDIA_MIDI_MIDI_MANAGER_USB_H_ |
| 6 | #define MEDIA_MIDI_MIDI_MANAGER_USB_H_ |
| 7 | |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame^] | 11 | #include <memory> |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 15 | #include "base/bind.h" |
| 16 | #include "base/callback.h" |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 18 | #include "base/containers/hash_tables.h" |
avi | eea95e8 | 2015-12-18 20:27:08 -0800 | [diff] [blame] | 19 | #include "base/macros.h" |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 20 | #include "base/time/time.h" |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 21 | #include "media/midi/midi_manager.h" |
| 22 | #include "media/midi/usb_midi_device.h" |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 23 | #include "media/midi/usb_midi_export.h" |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 24 | #include "media/midi/usb_midi_input_stream.h" |
| 25 | #include "media/midi/usb_midi_jack.h" |
| 26 | #include "media/midi/usb_midi_output_stream.h" |
| 27 | |
| 28 | namespace media { |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 29 | namespace midi { |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 30 | |
toyoshim | ec5518e | 2015-04-03 00:53:39 -0700 | [diff] [blame] | 31 | class MidiScheduler; |
| 32 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 33 | // MidiManager for USB-MIDI. |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 34 | class USB_MIDI_EXPORT MidiManagerUsb |
| 35 | : public MidiManager, |
| 36 | public UsbMidiDeviceDelegate, |
| 37 | NON_EXPORTED_BASE(public UsbMidiInputStream::Delegate) { |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 38 | public: |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame^] | 39 | explicit MidiManagerUsb( |
| 40 | std::unique_ptr<UsbMidiDevice::Factory> device_factory); |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 41 | ~MidiManagerUsb() override; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 42 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 43 | // MidiManager implementation. |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 44 | void StartInitialization() override; |
| 45 | void DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 46 | uint32_t port_index, |
| 47 | const std::vector<uint8_t>& data, |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 48 | double timestamp) override; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 49 | |
| 50 | // UsbMidiDeviceDelegate implementation. |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 51 | void ReceiveUsbMidiData(UsbMidiDevice* device, |
| 52 | int endpoint_number, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 53 | const uint8_t* data, |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 54 | size_t size, |
| 55 | base::TimeTicks time) override; |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame^] | 56 | void OnDeviceAttached(std::unique_ptr<UsbMidiDevice> device) override; |
yhirano | 807f97f | 2015-02-26 18:44:10 -0800 | [diff] [blame] | 57 | void OnDeviceDetached(size_t index) override; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 58 | |
| 59 | // UsbMidiInputStream::Delegate implementation. |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 60 | void OnReceivedData(size_t jack_index, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 61 | const uint8_t* data, |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 62 | size_t size, |
| 63 | base::TimeTicks time) override; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 64 | |
| 65 | const ScopedVector<UsbMidiOutputStream>& output_streams() const { |
| 66 | return output_streams_; |
| 67 | } |
| 68 | const UsbMidiInputStream* input_stream() const { return input_stream_.get(); } |
| 69 | |
| 70 | // Initializes this object. |
| 71 | // When the initialization finishes, |callback| will be called with the |
| 72 | // result. |
| 73 | // When this factory is destroyed during the operation, the operation |
| 74 | // will be canceled silently (i.e. |callback| will not be called). |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 75 | // The function is public just for unit tests. Do not call this function |
| 76 | // outside code for testing. |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 77 | void Initialize(base::Callback<void(Result result)> callback); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | void OnEnumerateDevicesDone(bool result, UsbMidiDevice::Devices* devices); |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 81 | bool AddPorts(UsbMidiDevice* device, int device_id); |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 82 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame^] | 83 | std::unique_ptr<UsbMidiDevice::Factory> device_factory_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 84 | ScopedVector<UsbMidiDevice> devices_; |
| 85 | ScopedVector<UsbMidiOutputStream> output_streams_; |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame^] | 86 | std::unique_ptr<UsbMidiInputStream> input_stream_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 87 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 88 | base::Callback<void(Result result)> initialize_callback_; |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 89 | |
| 90 | // A map from <endpoint_number, cable_number> to the index of input jacks. |
| 91 | base::hash_map<std::pair<int, int>, size_t> input_jack_dictionary_; |
| 92 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame^] | 93 | std::unique_ptr<MidiScheduler> scheduler_; |
toyoshim | ec5518e | 2015-04-03 00:53:39 -0700 | [diff] [blame] | 94 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 95 | DISALLOW_COPY_AND_ASSIGN(MidiManagerUsb); |
| 96 | }; |
| 97 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 98 | } // namespace midi |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 99 | } // namespace media |
| 100 | |
| 101 | #endif // MEDIA_MIDI_MIDI_MANAGER_USB_H_ |