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.
5 #ifndef MEDIA_MIDI_MIDI_MANAGER_USB_H_
6 #define MEDIA_MIDI_MIDI_MANAGER_USB_H_
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"
28 // MidiManager for USB-MIDI.
29 class MEDIA_EXPORT MidiManagerUsb
: public MidiManager
,
30 public UsbMidiDeviceDelegate
,
31 public UsbMidiInputStream::Delegate
{
33 explicit MidiManagerUsb(scoped_ptr
<UsbMidiDevice::Factory
> device_factory
);
34 ~MidiManagerUsb() override
;
36 // MidiManager implementation.
37 void StartInitialization() override
;
38 void DispatchSendMidiData(MidiManagerClient
* client
,
40 const std::vector
<uint8
>& data
,
41 double timestamp
) override
;
43 // UsbMidiDeviceDelegate implementation.
44 void ReceiveUsbMidiData(UsbMidiDevice
* device
,
48 base::TimeTicks time
) override
;
49 void OnDeviceAttached(scoped_ptr
<UsbMidiDevice
> device
) override
;
50 void OnDeviceDetached(size_t index
) override
;
52 // UsbMidiInputStream::Delegate implementation.
53 void OnReceivedData(size_t jack_index
,
56 base::TimeTicks time
) override
;
58 const ScopedVector
<UsbMidiOutputStream
>& output_streams() const {
59 return output_streams_
;
61 const UsbMidiInputStream
* input_stream() const { return input_stream_
.get(); }
63 // Initializes this object.
64 // When the initialization finishes, |callback| will be called with the
66 // When this factory is destroyed during the operation, the operation
67 // will be canceled silently (i.e. |callback| will not be called).
68 // The function is public just for unit tests. Do not call this function
69 // outside code for testing.
70 void Initialize(base::Callback
<void(MidiResult result
)> callback
);
73 void OnEnumerateDevicesDone(bool result
, UsbMidiDevice::Devices
* devices
);
74 bool AddPorts(UsbMidiDevice
* device
, int device_id
);
76 scoped_ptr
<UsbMidiDevice::Factory
> device_factory_
;
77 ScopedVector
<UsbMidiDevice
> devices_
;
78 ScopedVector
<UsbMidiOutputStream
> output_streams_
;
79 scoped_ptr
<UsbMidiInputStream
> input_stream_
;
81 base::Callback
<void(MidiResult result
)> initialize_callback_
;
83 // A map from <endpoint_number, cable_number> to the index of input jacks.
84 base::hash_map
<std::pair
<int, int>, size_t> input_jack_dictionary_
;
86 scoped_ptr
<MidiScheduler
> scheduler_
;
88 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsb
);
93 #endif // MEDIA_MIDI_MIDI_MANAGER_USB_H_