blob: 2207c5d81510f9c40d4fa834e53bc17a95ef82f7 [file] [log] [blame]
yhirano@chromium.org0e3c3ea2014-01-22 10:39:41 +00001// 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 "media/base/media_export.h"
14#include "media/midi/usb_midi_jack.h"
15
16namespace media {
17
18class UsbMidiDevice;
19
20// UsbMidiInputStream converts USB-MIDI data to MIDI data.
21// See "USB Device Class Definition for MIDI Devices" Release 1.0,
22// Section 4 "USB-MIDI Event Packets" for details.
23class MEDIA_EXPORT UsbMidiInputStream {
24 public:
yhirano@chromium.org3cecb692014-01-29 09:27:41 +000025 class MEDIA_EXPORT Delegate {
yhirano@chromium.org0e3c3ea2014-01-22 10:39:41 +000026 public:
27 virtual ~Delegate() {}
28 // This function is called when some data arrives to a USB-MIDI jack.
29 // An input USB-MIDI jack corresponds to an input MIDIPortInfo.
30 virtual void OnReceivedData(size_t jack_index,
31 const uint8* data,
32 size_t size,
33 double timestamp) = 0;
34 };
35
yhirano@chromium.org3cecb692014-01-29 09:27:41 +000036 // This is public for testing.
37 struct JackUniqueKey {
38 JackUniqueKey(UsbMidiDevice* device, int endpoint_number, int cable_number);
39 bool operator==(const JackUniqueKey& that) const;
40 bool operator<(const JackUniqueKey& that) const;
41
42 UsbMidiDevice* device;
43 int endpoint_number;
44 int cable_number;
45 };
46
yhirano@chromium.org0e3c3ea2014-01-22 10:39:41 +000047 UsbMidiInputStream(const std::vector<UsbMidiJack>& jacks,
48 Delegate* delegate);
49 ~UsbMidiInputStream();
50
51 // This function should be called when some data arrives to a USB-MIDI
52 // endpoint. This function converts the data to MIDI data and call
53 // |delegate->OnReceivedData| with it.
54 // |size| must be a multiple of |kPacketSize|.
55 void OnReceivedData(UsbMidiDevice* device,
56 int endpoint_number,
57 const uint8* data,
58 size_t size,
59 double timestamp);
60
yhirano@chromium.org3cecb692014-01-29 09:27:41 +000061 std::vector<JackUniqueKey> RegisteredJackKeysForTesting() const;
62
yhirano@chromium.org0e3c3ea2014-01-22 10:39:41 +000063 private:
64 static const size_t kPacketSize = 4;
yhirano@chromium.org0e3c3ea2014-01-22 10:39:41 +000065 // Processes a USB-MIDI Event Packet.
66 // The first |kPacketSize| bytes of |packet| must be accessible.
67 void ProcessOnePacket(UsbMidiDevice* device,
68 int endpoint_number,
69 const uint8* packet,
70 double timestamp);
71
72 // A map from UsbMidiJack to its index in |jacks_|.
73 std::map<JackUniqueKey, size_t> jack_dictionary_;
74
75 // Not owned
76 Delegate* delegate_;
77
78 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStream);
79};
80
81} // namespace media
82
83#endif // MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_