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_USB_MIDI_INPUT_STREAM_H_
6 #define MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/time/time.h"
14 #include "media/base/media_export.h"
15 #include "media/midi/usb_midi_jack.h"
21 // UsbMidiInputStream converts USB-MIDI data to MIDI data.
22 // See "USB Device Class Definition for MIDI Devices" Release 1.0,
23 // Section 4 "USB-MIDI Event Packets" for details.
24 class MEDIA_EXPORT UsbMidiInputStream
{
26 class MEDIA_EXPORT Delegate
{
28 virtual ~Delegate() {}
29 // This function is called when some data arrives to a USB-MIDI jack.
30 // An input USB-MIDI jack corresponds to an input MIDIPortInfo.
31 virtual void OnReceivedData(size_t jack_index
,
34 base::TimeTicks time
) = 0;
37 // This is public for testing.
38 struct JackUniqueKey
{
39 JackUniqueKey(UsbMidiDevice
* device
, int endpoint_number
, int cable_number
);
40 bool operator==(const JackUniqueKey
& that
) const;
41 bool operator<(const JackUniqueKey
& that
) const;
43 UsbMidiDevice
* device
;
48 explicit UsbMidiInputStream(Delegate
* delegate
);
49 ~UsbMidiInputStream();
51 void Add(const UsbMidiJack
& jack
);
53 // This function should be called when some data arrives to a USB-MIDI
54 // endpoint. This function converts the data to MIDI data and call
55 // |delegate->OnReceivedData| with it.
56 // |size| must be a multiple of |kPacketSize|.
57 void OnReceivedData(UsbMidiDevice
* device
,
61 base::TimeTicks time
);
63 const std::vector
<UsbMidiJack
>& jacks() const { return jacks_
; }
66 static const size_t kPacketSize
= 4;
67 // Processes a USB-MIDI Event Packet.
68 // The first |kPacketSize| bytes of |packet| must be accessible.
69 void ProcessOnePacket(UsbMidiDevice
* device
,
72 base::TimeTicks time
);
74 std::vector
<UsbMidiJack
> jacks_
;
75 // A map from UsbMidiJack to its index in |jacks_|.
76 std::map
<JackUniqueKey
, size_t> jack_dictionary_
;
81 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStream
);
86 #endif // MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_