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 | #include "media/midi/usb_midi_input_stream.h" |
| 6 | |
| 7 | #include <string.h> |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "media/midi/usb_midi_device.h" |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 11 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 12 | namespace midi { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 13 | |
| 14 | UsbMidiInputStream::JackUniqueKey::JackUniqueKey(UsbMidiDevice* device, |
| 15 | int endpoint_number, |
| 16 | int cable_number) |
| 17 | : device(device), |
| 18 | endpoint_number(endpoint_number), |
| 19 | cable_number(cable_number) {} |
| 20 | |
| 21 | bool UsbMidiInputStream::JackUniqueKey::operator==( |
| 22 | const JackUniqueKey& that) const { |
| 23 | return device == that.device && |
| 24 | endpoint_number == that.endpoint_number && |
| 25 | cable_number == that.cable_number; |
| 26 | } |
| 27 | |
| 28 | bool UsbMidiInputStream::JackUniqueKey::operator<( |
| 29 | const JackUniqueKey& that) const { |
| 30 | if (device != that.device) |
| 31 | return device < that.device; |
| 32 | if (endpoint_number != that.endpoint_number) |
| 33 | return endpoint_number < that.endpoint_number; |
| 34 | return cable_number < that.cable_number; |
| 35 | } |
| 36 | |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 37 | UsbMidiInputStream::UsbMidiInputStream(Delegate* delegate) |
| 38 | : delegate_(delegate) {} |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 39 | |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 40 | UsbMidiInputStream::~UsbMidiInputStream() = default; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 41 | |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 42 | void UsbMidiInputStream::Add(const UsbMidiJack& jack) { |
| 43 | JackUniqueKey key(jack.device, |
| 44 | jack.endpoint_number(), |
| 45 | jack.cable_number); |
| 46 | |
yhirano | 807f97f | 2015-02-26 18:44:10 -0800 | [diff] [blame] | 47 | jacks_.push_back(jack); |
yhirano | 33315c6 | 2015-02-26 17:01:15 -0800 | [diff] [blame] | 48 | DCHECK(jack_dictionary_.end() == jack_dictionary_.find(key)); |
| 49 | jack_dictionary_.insert(std::make_pair(key, jack_dictionary_.size())); |
| 50 | } |
| 51 | |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 52 | void UsbMidiInputStream::OnReceivedData(UsbMidiDevice* device, |
| 53 | int endpoint_number, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 54 | const uint8_t* data, |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 55 | size_t size, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 56 | base::TimeTicks time) { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 57 | DCHECK_EQ(0u, size % kPacketSize); |
| 58 | size_t current = 0; |
| 59 | while (current + kPacketSize <= size) { |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 60 | ProcessOnePacket(device, endpoint_number, &data[current], time); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 61 | current += kPacketSize; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void UsbMidiInputStream::ProcessOnePacket(UsbMidiDevice* device, |
| 66 | int endpoint_number, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 67 | const uint8_t* packet, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 68 | base::TimeTicks time) { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 69 | // The first 4 bytes of the packet is accessible here. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 70 | uint8_t code_index = packet[0] & 0x0f; |
| 71 | uint8_t cable_number = packet[0] >> 4; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 72 | const size_t packet_size_table[16] = { |
| 73 | 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1, |
| 74 | }; |
| 75 | size_t packet_size = packet_size_table[code_index]; |
| 76 | if (packet_size == 0) { |
| 77 | // These CINs are reserved. Ignore them. |
| 78 | DVLOG(1) << "code index number (" << code_index << ") arrives " |
| 79 | << "but it is reserved."; |
| 80 | return; |
| 81 | } |
| 82 | std::map<JackUniqueKey, size_t>::const_iterator it = |
| 83 | jack_dictionary_.find(JackUniqueKey(device, |
| 84 | endpoint_number, |
| 85 | cable_number)); |
| 86 | if (it != jack_dictionary_.end()) |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 87 | delegate_->OnReceivedData(it->second, &packet[1], packet_size, time); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 88 | } |
| 89 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 90 | } // namespace midi |