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