OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_ | 5 #ifndef MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_ |
6 #define MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_ | 6 #define MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | |
12 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
14 #include "media/midi/usb_midi_export.h" | 13 #include "media/midi/usb_midi_export.h" |
15 #include "media/midi/usb_midi_jack.h" | 14 #include "media/midi/usb_midi_jack.h" |
16 | 15 |
17 namespace media { | 16 namespace media { |
18 namespace midi { | 17 namespace midi { |
19 | 18 |
20 class UsbMidiDevice; | 19 class UsbMidiDevice; |
21 | 20 |
22 // UsbMidiInputStream converts USB-MIDI data to MIDI data. | 21 // UsbMidiInputStream converts USB-MIDI data to MIDI data. |
23 // See "USB Device Class Definition for MIDI Devices" Release 1.0, | 22 // See "USB Device Class Definition for MIDI Devices" Release 1.0, |
24 // Section 4 "USB-MIDI Event Packets" for details. | 23 // Section 4 "USB-MIDI Event Packets" for details. |
25 class USB_MIDI_EXPORT UsbMidiInputStream { | 24 class USB_MIDI_EXPORT UsbMidiInputStream { |
26 public: | 25 public: |
27 class USB_MIDI_EXPORT Delegate { | 26 class USB_MIDI_EXPORT Delegate { |
28 public: | 27 public: |
29 virtual ~Delegate() {} | 28 virtual ~Delegate() {} |
30 // This function is called when some data arrives to a USB-MIDI jack. | 29 // This function is called when some data arrives to a USB-MIDI jack. |
31 // An input USB-MIDI jack corresponds to an input MIDIPortInfo. | 30 // An input USB-MIDI jack corresponds to an input MIDIPortInfo. |
32 virtual void OnReceivedData(size_t jack_index, | 31 virtual void OnReceivedData(size_t jack_index, |
33 const uint8* data, | 32 const uint8_t* data, |
34 size_t size, | 33 size_t size, |
35 base::TimeTicks time) = 0; | 34 base::TimeTicks time) = 0; |
36 }; | 35 }; |
37 | 36 |
38 // This is public for testing. | 37 // This is public for testing. |
39 struct JackUniqueKey { | 38 struct JackUniqueKey { |
40 JackUniqueKey(UsbMidiDevice* device, int endpoint_number, int cable_number); | 39 JackUniqueKey(UsbMidiDevice* device, int endpoint_number, int cable_number); |
41 bool operator==(const JackUniqueKey& that) const; | 40 bool operator==(const JackUniqueKey& that) const; |
42 bool operator<(const JackUniqueKey& that) const; | 41 bool operator<(const JackUniqueKey& that) const; |
43 | 42 |
44 UsbMidiDevice* device; | 43 UsbMidiDevice* device; |
45 int endpoint_number; | 44 int endpoint_number; |
46 int cable_number; | 45 int cable_number; |
47 }; | 46 }; |
48 | 47 |
49 explicit UsbMidiInputStream(Delegate* delegate); | 48 explicit UsbMidiInputStream(Delegate* delegate); |
50 ~UsbMidiInputStream(); | 49 ~UsbMidiInputStream(); |
51 | 50 |
52 void Add(const UsbMidiJack& jack); | 51 void Add(const UsbMidiJack& jack); |
53 | 52 |
54 // This function should be called when some data arrives to a USB-MIDI | 53 // 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 | 54 // endpoint. This function converts the data to MIDI data and call |
56 // |delegate->OnReceivedData| with it. | 55 // |delegate->OnReceivedData| with it. |
57 // |size| must be a multiple of |kPacketSize|. | 56 // |size| must be a multiple of |kPacketSize|. |
58 void OnReceivedData(UsbMidiDevice* device, | 57 void OnReceivedData(UsbMidiDevice* device, |
59 int endpoint_number, | 58 int endpoint_number, |
60 const uint8* data, | 59 const uint8_t* data, |
61 size_t size, | 60 size_t size, |
62 base::TimeTicks time); | 61 base::TimeTicks time); |
63 | 62 |
64 const std::vector<UsbMidiJack>& jacks() const { return jacks_; } | 63 const std::vector<UsbMidiJack>& jacks() const { return jacks_; } |
65 | 64 |
66 private: | 65 private: |
67 static const size_t kPacketSize = 4; | 66 static const size_t kPacketSize = 4; |
68 // Processes a USB-MIDI Event Packet. | 67 // Processes a USB-MIDI Event Packet. |
69 // The first |kPacketSize| bytes of |packet| must be accessible. | 68 // The first |kPacketSize| bytes of |packet| must be accessible. |
70 void ProcessOnePacket(UsbMidiDevice* device, | 69 void ProcessOnePacket(UsbMidiDevice* device, |
71 int endpoint_number, | 70 int endpoint_number, |
72 const uint8* packet, | 71 const uint8_t* packet, |
73 base::TimeTicks time); | 72 base::TimeTicks time); |
74 | 73 |
75 std::vector<UsbMidiJack> jacks_; | 74 std::vector<UsbMidiJack> jacks_; |
76 // A map from UsbMidiJack to its index in |jacks_|. | 75 // A map from UsbMidiJack to its index in |jacks_|. |
77 std::map<JackUniqueKey, size_t> jack_dictionary_; | 76 std::map<JackUniqueKey, size_t> jack_dictionary_; |
78 | 77 |
79 // Not owned | 78 // Not owned |
80 Delegate* delegate_; | 79 Delegate* delegate_; |
81 | 80 |
82 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStream); | 81 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStream); |
83 }; | 82 }; |
84 | 83 |
85 } // namespace midi | 84 } // namespace midi |
86 } // namespace media | 85 } // namespace media |
87 | 86 |
88 #endif // MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_ | 87 #endif // MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_ |
OLD | NEW |