yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 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" |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame^] | 13 | #include "base/time/time.h" |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 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: |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 26 | class MEDIA_EXPORT Delegate { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 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, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame^] | 34 | base::TimeTicks time) = 0; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 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 | |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 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, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame^] | 60 | base::TimeTicks time); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 61 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 62 | std::vector<JackUniqueKey> RegisteredJackKeysForTesting() const; |
| 63 | |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 64 | private: |
| 65 | static const size_t kPacketSize = 4; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 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, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame^] | 71 | base::TimeTicks time); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 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_ |