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