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