Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / midi / usb_midi_input_stream.h
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_USB_MIDI_INPUT_STREAM_H_
6 #define MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_
7
8 #include <map>
9 #include <vector>
10
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"
16
17 namespace media {
18
19 class UsbMidiDevice;
20
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 {
25  public:
26   class MEDIA_EXPORT Delegate {
27    public:
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,
32                                 const uint8* data,
33                                 size_t size,
34                                 base::TimeTicks time) = 0;
35   };
36
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;
42
43     UsbMidiDevice* device;
44     int endpoint_number;
45     int cable_number;
46   };
47
48   UsbMidiInputStream(const std::vector<UsbMidiJack>& jacks,
49                      Delegate* delegate);
50   ~UsbMidiInputStream();
51
52   // This function should be called when some data arrives to a USB-MIDI
53   // endpoint. This function converts the data to MIDI data and call
54   // |delegate->OnReceivedData| with it.
55   // |size| must be a multiple of |kPacketSize|.
56   void OnReceivedData(UsbMidiDevice* device,
57                       int endpoint_number,
58                       const uint8* data,
59                       size_t size,
60                       base::TimeTicks time);
61
62   std::vector<JackUniqueKey> RegisteredJackKeysForTesting() const;
63
64  private:
65   static const size_t kPacketSize = 4;
66   // Processes a USB-MIDI Event Packet.
67   // The first |kPacketSize| bytes of |packet| must be accessible.
68   void ProcessOnePacket(UsbMidiDevice* device,
69                         int endpoint_number,
70                         const uint8* packet,
71                         base::TimeTicks time);
72
73   // A map from UsbMidiJack to its index in |jacks_|.
74   std::map<JackUniqueKey, size_t> jack_dictionary_;
75
76   // Not owned
77   Delegate* delegate_;
78
79   DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStream);
80 };
81
82 }  // namespace media
83
84 #endif  // MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_