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/midi/usb_midi_export.h"
15 #include "media/midi/usb_midi_jack.h"
22 // UsbMidiInputStream converts USB-MIDI data to MIDI data.
23 // See "USB Device Class Definition for MIDI Devices" Release 1.0,
24 // Section 4 "USB-MIDI Event Packets" for details.
25 class USB_MIDI_EXPORT UsbMidiInputStream
{
27 class USB_MIDI_EXPORT Delegate
{
29 virtual ~Delegate() {}
30 // This function is called when some data arrives to a USB-MIDI jack.
31 // An input USB-MIDI jack corresponds to an input MIDIPortInfo.
32 virtual void OnReceivedData(size_t jack_index
,
35 base::TimeTicks time
) = 0;
38 // This is public for testing.
39 struct JackUniqueKey
{
40 JackUniqueKey(UsbMidiDevice
* device
, int endpoint_number
, int cable_number
);
41 bool operator==(const JackUniqueKey
& that
) const;
42 bool operator<(const JackUniqueKey
& that
) const;
44 UsbMidiDevice
* device
;
49 explicit UsbMidiInputStream(Delegate
* delegate
);
50 ~UsbMidiInputStream();
52 void Add(const UsbMidiJack
& jack
);
54 // This function should be called when some data arrives to a USB-MIDI
55 // endpoint. This function converts the data to MIDI data and call
56 // |delegate->OnReceivedData| with it.
57 // |size| must be a multiple of |kPacketSize|.
58 void OnReceivedData(UsbMidiDevice
* device
,
62 base::TimeTicks time
);
64 const std::vector
<UsbMidiJack
>& jacks() const { return jacks_
; }
67 static const size_t kPacketSize
= 4;
68 // Processes a USB-MIDI Event Packet.
69 // The first |kPacketSize| bytes of |packet| must be accessible.
70 void ProcessOnePacket(UsbMidiDevice
* device
,
73 base::TimeTicks time
);
75 std::vector
<UsbMidiJack
> jacks_
;
76 // A map from UsbMidiJack to its index in |jacks_|.
77 std::map
<JackUniqueKey
, size_t> jack_dictionary_
;
82 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStream
);
88 #endif // MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_