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_output_stream.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "media/midi/midi_message_util.h" |
| 9 | #include "media/midi/usb_midi_device.h" |
| 10 | |
| 11 | namespace media { |
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 | UsbMidiOutputStream::UsbMidiOutputStream(const UsbMidiJack& jack) |
| 15 | : jack_(jack), pending_size_(0), is_sending_sysex_(false) {} |
| 16 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 17 | void UsbMidiOutputStream::Send(const std::vector<uint8_t>& data) { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 18 | // To prevent link errors caused by DCHECK_*. |
| 19 | const size_t kPacketContentSize = UsbMidiOutputStream::kPacketContentSize; |
| 20 | DCHECK_LT(jack_.cable_number, 16u); |
| 21 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 22 | std::vector<uint8_t> data_to_send; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 23 | size_t current = 0; |
| 24 | size_t size = GetSize(data); |
| 25 | while (current < size) { |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 26 | uint8_t first_byte = Get(data, current); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 27 | if (first_byte == kSysExByte || is_sending_sysex_) { |
| 28 | // System Exclusive messages |
| 29 | if (!PushSysExMessage(data, ¤t, &data_to_send)) |
| 30 | break; |
| 31 | } else if ((first_byte & kSysMessageBitMask) == kSysMessageBitPattern) { |
| 32 | if (first_byte & 0x08) { |
| 33 | // System Real-Time messages |
| 34 | PushSysRTMessage(data, ¤t, &data_to_send); |
| 35 | } else { |
| 36 | // System Common messages |
| 37 | if (!PushSysCommonMessage(data, ¤t, &data_to_send)) |
| 38 | break; |
| 39 | } |
| 40 | } else if (first_byte & 0x80) { |
| 41 | if (!PushChannelMessage(data, ¤t, &data_to_send)) |
| 42 | break; |
| 43 | } else { |
| 44 | // Unknown messages |
| 45 | DVLOG(1) << "Unknown byte: " << static_cast<unsigned int>(first_byte); |
| 46 | ++current; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (data_to_send.size() > 0) |
| 51 | jack_.device->Send(jack_.endpoint_number(), data_to_send); |
| 52 | |
| 53 | DCHECK_LE(current, size); |
| 54 | DCHECK_LE(size - current, kPacketContentSize); |
| 55 | // Note that this can be a self-copying and the iteration order is important. |
| 56 | for (size_t i = current; i < size; ++i) |
| 57 | pending_data_[i - current] = Get(data, i); |
| 58 | pending_size_ = size - current; |
| 59 | } |
| 60 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 61 | size_t UsbMidiOutputStream::GetSize(const std::vector<uint8_t>& data) const { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 62 | return data.size() + pending_size_; |
| 63 | } |
| 64 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 65 | uint8_t UsbMidiOutputStream::Get(const std::vector<uint8_t>& data, |
| 66 | size_t index) const { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 67 | DCHECK_LT(index, GetSize(data)); |
| 68 | if (index < pending_size_) |
| 69 | return pending_data_[index]; |
| 70 | return data[index - pending_size_]; |
| 71 | } |
| 72 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 73 | bool UsbMidiOutputStream::PushSysExMessage(const std::vector<uint8_t>& data, |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 74 | size_t* current, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 75 | std::vector<uint8_t>* data_to_send) { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 76 | size_t index = *current; |
| 77 | size_t message_size = 0; |
| 78 | const size_t kMessageSizeMax = 3; |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 79 | uint8_t message[kMessageSizeMax] = {}; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 80 | |
| 81 | while (index < GetSize(data)) { |
| 82 | if (message_size == kMessageSizeMax) { |
| 83 | // We can't find the end-of-message mark in the three bytes. |
| 84 | *current = index; |
| 85 | data_to_send->push_back((jack_.cable_number << 4) | 0x4); |
| 86 | data_to_send->insert(data_to_send->end(), |
| 87 | message, |
| 88 | message + arraysize(message)); |
| 89 | is_sending_sysex_ = true; |
| 90 | return true; |
| 91 | } |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 92 | uint8_t byte = Get(data, index); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 93 | if ((byte & kSysRTMessageBitMask) == kSysRTMessageBitPattern) { |
| 94 | // System Real-Time messages interleaved in a SysEx message |
| 95 | PushSysRTMessage(data, &index, data_to_send); |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | message[message_size] = byte; |
| 100 | ++message_size; |
| 101 | if (byte == kEndOfSysExByte) { |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 102 | uint8_t code_index = static_cast<uint8_t>(message_size) + 0x4; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 103 | DCHECK(code_index == 0x5 || code_index == 0x6 || code_index == 0x7); |
| 104 | data_to_send->push_back((jack_.cable_number << 4) | code_index); |
| 105 | data_to_send->insert(data_to_send->end(), |
| 106 | message, |
| 107 | message + arraysize(message)); |
| 108 | *current = index + 1; |
| 109 | is_sending_sysex_ = false; |
| 110 | return true; |
| 111 | } |
| 112 | ++index; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | bool UsbMidiOutputStream::PushSysCommonMessage( |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 118 | const std::vector<uint8_t>& data, |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 119 | size_t* current, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 120 | std::vector<uint8_t>* data_to_send) { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 121 | size_t index = *current; |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 122 | uint8_t first_byte = Get(data, index); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 123 | DCHECK_LE(0xf1, first_byte); |
| 124 | DCHECK_LE(first_byte, 0xf7); |
yhirano | 86ce905 | 2014-11-03 18:58:10 -0800 | [diff] [blame] | 125 | DCHECK_EQ(0xf0, first_byte & 0xf8); |
| 126 | // There are only 6 message types (0xf1 - 0xf7), so the table size is 8. |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 127 | const size_t message_size_table[8] = { |
| 128 | 0, 2, 3, 2, 1, 1, 1, 0, |
| 129 | }; |
yhirano | 86ce905 | 2014-11-03 18:58:10 -0800 | [diff] [blame] | 130 | size_t message_size = message_size_table[first_byte & 0x07]; |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 131 | DCHECK_NE(0u, message_size); |
| 132 | DCHECK_LE(message_size, 3u); |
| 133 | |
| 134 | if (GetSize(data) < index + message_size) { |
| 135 | // The message is incomplete. |
| 136 | return false; |
| 137 | } |
| 138 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 139 | uint8_t code_index = |
| 140 | message_size == 1 ? 0x5 : static_cast<uint8_t>(message_size); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 141 | data_to_send->push_back((jack_.cable_number << 4) | code_index); |
| 142 | for (size_t i = index; i < index + 3; ++i) |
| 143 | data_to_send->push_back(i < index + message_size ? Get(data, i) : 0); |
| 144 | *current += message_size; |
| 145 | return true; |
| 146 | } |
| 147 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 148 | void UsbMidiOutputStream::PushSysRTMessage(const std::vector<uint8_t>& data, |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 149 | size_t* current, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 150 | std::vector<uint8_t>* data_to_send) { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 151 | size_t index = *current; |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 152 | uint8_t first_byte = Get(data, index); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 153 | DCHECK_LE(0xf8, first_byte); |
| 154 | DCHECK_LE(first_byte, 0xff); |
| 155 | |
| 156 | data_to_send->push_back((jack_.cable_number << 4) | 0x5); |
| 157 | data_to_send->push_back(first_byte); |
| 158 | data_to_send->push_back(0); |
| 159 | data_to_send->push_back(0); |
| 160 | *current += 1; |
| 161 | } |
| 162 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 163 | bool UsbMidiOutputStream::PushChannelMessage( |
| 164 | const std::vector<uint8_t>& data, |
| 165 | size_t* current, |
| 166 | std::vector<uint8_t>* data_to_send) { |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 167 | size_t index = *current; |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 168 | uint8_t first_byte = Get(data, index); |
yhirano | 86ce905 | 2014-11-03 18:58:10 -0800 | [diff] [blame] | 169 | |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 170 | DCHECK_LE(0x80, (first_byte & 0xf0)); |
| 171 | DCHECK_LE((first_byte & 0xf0), 0xe0); |
yhirano | 86ce905 | 2014-11-03 18:58:10 -0800 | [diff] [blame] | 172 | // There are only 7 message types (0x8-0xe in the higher four bits), so the |
| 173 | // table size is 8. |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 174 | const size_t message_size_table[8] = { |
| 175 | 3, 3, 3, 3, 2, 3, 3, 0, |
| 176 | }; |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 177 | uint8_t code_index = first_byte >> 4; |
yhirano | 86ce905 | 2014-11-03 18:58:10 -0800 | [diff] [blame] | 178 | DCHECK_LE(0x08, code_index); |
| 179 | DCHECK_LE(code_index, 0x0e); |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 180 | size_t message_size = message_size_table[code_index & 0x7]; |
| 181 | DCHECK_NE(0u, message_size); |
| 182 | DCHECK_LE(message_size, 3u); |
| 183 | |
| 184 | if (GetSize(data) < index + message_size) { |
| 185 | // The message is incomplete. |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | data_to_send->push_back((jack_.cable_number << 4) | code_index); |
| 190 | for (size_t i = index; i < index + 3; ++i) |
| 191 | data_to_send->push_back(i < index + message_size ? Get(data, i) : 0); |
| 192 | *current += message_size; |
| 193 | return true; |
| 194 | } |
| 195 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 196 | } // namespace midi |
yhirano@chromium.org | 0e3c3ea | 2014-01-22 10:39:41 +0000 | [diff] [blame] | 197 | } // namespace media |