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 | |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame^] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 11 | #include <map> |
| 12 | #include <vector> |
| 13 | |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 14 | #include "base/containers/hash_tables.h" |
avi | eea95e8 | 2015-12-18 20:27:08 -0800 | [diff] [blame] | 15 | #include "base/macros.h" |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 16 | #include "base/time/time.h" |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 17 | #include "media/midi/usb_midi_export.h" |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 18 | #include "media/midi/usb_midi_jack.h" |
| 19 | |
| 20 | namespace media { |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 21 | namespace midi { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 22 | |
| 23 | class UsbMidiDevice; |
| 24 | |
| 25 | // UsbMidiInputStream converts USB-MIDI data to MIDI data. |
| 26 | // See "USB Device Class Definition for MIDI Devices" Release 1.0, |
| 27 | // Section 4 "USB-MIDI Event Packets" for details. |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 28 | class USB_MIDI_EXPORT UsbMidiInputStream { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 29 | public: |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 30 | class USB_MIDI_EXPORT Delegate { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 31 | public: |
| 32 | virtual ~Delegate() {} |
| 33 | // This function is called when some data arrives to a USB-MIDI jack. |
| 34 | // An input USB-MIDI jack corresponds to an input MIDIPortInfo. |
| 35 | virtual void OnReceivedData(size_t jack_index, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 36 | const uint8_t* data, |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 37 | size_t size, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 38 | base::TimeTicks time) = 0; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 41 | // This is public for testing. |
| 42 | struct JackUniqueKey { |
| 43 | JackUniqueKey(UsbMidiDevice* device, int endpoint_number, int cable_number); |
| 44 | bool operator==(const JackUniqueKey& that) const; |
| 45 | bool operator<(const JackUniqueKey& that) const; |
| 46 | |
| 47 | UsbMidiDevice* device; |
| 48 | int endpoint_number; |
| 49 | int cable_number; |
| 50 | }; |
| 51 | |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 52 | explicit UsbMidiInputStream(Delegate* delegate); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 53 | ~UsbMidiInputStream(); |
| 54 | |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 55 | void Add(const UsbMidiJack& jack); |
| 56 | |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 57 | // This function should be called when some data arrives to a USB-MIDI |
| 58 | // endpoint. This function converts the data to MIDI data and call |
| 59 | // |delegate->OnReceivedData| with it. |
| 60 | // |size| must be a multiple of |kPacketSize|. |
| 61 | void OnReceivedData(UsbMidiDevice* device, |
| 62 | int endpoint_number, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 63 | const uint8_t* data, |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 64 | size_t size, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 65 | base::TimeTicks time); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 66 | |
yhirano | 807f97f | 2015-02-26 18:44:10 -0800 | [diff] [blame] | 67 | const std::vector<UsbMidiJack>& jacks() const { return jacks_; } |
yhirano@chromium.org | 3cecb69 | 2014-01-29 09:27:41 +0000 | [diff] [blame] | 68 | |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 69 | private: |
| 70 | static const size_t kPacketSize = 4; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 71 | // Processes a USB-MIDI Event Packet. |
| 72 | // The first |kPacketSize| bytes of |packet| must be accessible. |
| 73 | void ProcessOnePacket(UsbMidiDevice* device, |
| 74 | int endpoint_number, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 75 | const uint8_t* packet, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 76 | base::TimeTicks time); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 77 | |
yhirano | 807f97f | 2015-02-26 18:44:10 -0800 | [diff] [blame] | 78 | std::vector<UsbMidiJack> jacks_; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 79 | // A map from UsbMidiJack to its index in |jacks_|. |
| 80 | std::map<JackUniqueKey, size_t> jack_dictionary_; |
| 81 | |
| 82 | // Not owned |
| 83 | Delegate* delegate_; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStream); |
| 86 | }; |
| 87 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 88 | } // namespace midi |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 89 | } // namespace media |
| 90 | |
| 91 | #endif // MEDIA_MIDI_USB_MIDI_INPUT_STREAM_H_ |