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" 16 #include "base/time/time.h" 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 26 // MidiManager for USB-MIDI. 27 class MEDIA_EXPORT MidiManagerUsb : public MidiManager, 28 public UsbMidiDeviceDelegate, 29 public UsbMidiInputStream::Delegate { 30 public: 31 explicit MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> device_factory); 32 virtual ~MidiManagerUsb(); 33 34 // MidiManager implementation. 35 virtual void StartInitialization() OVERRIDE; 36 virtual void DispatchSendMidiData(MidiManagerClient* client, 37 uint32 port_index, 38 const std::vector<uint8>& data, 39 double timestamp) OVERRIDE; 40 41 // UsbMidiDeviceDelegate implementation. 42 virtual void ReceiveUsbMidiData(UsbMidiDevice* device, 43 int endpoint_number, 44 const uint8* data, 45 size_t size, 46 base::TimeTicks time) OVERRIDE; 47 48 // UsbMidiInputStream::Delegate implementation. 49 virtual void OnReceivedData(size_t jack_index, 50 const uint8* data, 51 size_t size, 52 base::TimeTicks time) OVERRIDE; 53 output_streams()54 const ScopedVector<UsbMidiOutputStream>& output_streams() const { 55 return output_streams_; 56 } input_stream()57 const UsbMidiInputStream* input_stream() const { return input_stream_.get(); } 58 59 // Initializes this object. 60 // When the initialization finishes, |callback| will be called with the 61 // result. 62 // When this factory is destroyed during the operation, the operation 63 // will be canceled silently (i.e. |callback| will not be called). 64 // The function is public just for unit tests. Do not call this function 65 // outside code for testing. 66 void Initialize(base::Callback<void(MidiResult result)> callback); 67 68 private: 69 void OnEnumerateDevicesDone(bool result, UsbMidiDevice::Devices* devices); 70 71 scoped_ptr<UsbMidiDevice::Factory> device_factory_; 72 ScopedVector<UsbMidiDevice> devices_; 73 ScopedVector<UsbMidiOutputStream> output_streams_; 74 scoped_ptr<UsbMidiInputStream> input_stream_; 75 76 base::Callback<void(MidiResult result)> initialize_callback_; 77 78 // A map from <endpoint_number, cable_number> to the index of input jacks. 79 base::hash_map<std::pair<int, int>, size_t> input_jack_dictionary_; 80 81 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsb); 82 }; 83 84 } // namespace media 85 86 #endif // MEDIA_MIDI_MIDI_MANAGER_USB_H_ 87