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