Delete unused downloads page asset.
[chromium-blink-merge.git] / media / midi / usb_midi_output_stream.cc
blobded37e401ef1449a5bc4cc562d09b43b3f2af6b5
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.
5 #include "media/midi/usb_midi_output_stream.h"
7 #include "base/logging.h"
8 #include "media/midi/midi_message_util.h"
9 #include "media/midi/usb_midi_device.h"
11 namespace media {
12 namespace midi {
14 UsbMidiOutputStream::UsbMidiOutputStream(const UsbMidiJack& jack)
15 : jack_(jack), pending_size_(0), is_sending_sysex_(false) {}
17 void UsbMidiOutputStream::Send(const std::vector<uint8>& data) {
18 // To prevent link errors caused by DCHECK_*.
19 const size_t kPacketContentSize = UsbMidiOutputStream::kPacketContentSize;
20 DCHECK_LT(jack_.cable_number, 16u);
22 std::vector<uint8> data_to_send;
23 size_t current = 0;
24 size_t size = GetSize(data);
25 while (current < size) {
26 uint8 first_byte = Get(data, current);
27 if (first_byte == kSysExByte || is_sending_sysex_) {
28 // System Exclusive messages
29 if (!PushSysExMessage(data, &current, &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, &current, &data_to_send);
35 } else {
36 // System Common messages
37 if (!PushSysCommonMessage(data, &current, &data_to_send))
38 break;
40 } else if (first_byte & 0x80) {
41 if (!PushChannelMessage(data, &current, &data_to_send))
42 break;
43 } else {
44 // Unknown messages
45 DVLOG(1) << "Unknown byte: " << static_cast<unsigned int>(first_byte);
46 ++current;
50 if (data_to_send.size() > 0)
51 jack_.device->Send(jack_.endpoint_number(), data_to_send);
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;
61 size_t UsbMidiOutputStream::GetSize(const std::vector<uint8>& data) const {
62 return data.size() + pending_size_;
65 uint8_t UsbMidiOutputStream::Get(const std::vector<uint8>& data,
66 size_t index) const {
67 DCHECK_LT(index, GetSize(data));
68 if (index < pending_size_)
69 return pending_data_[index];
70 return data[index - pending_size_];
73 bool UsbMidiOutputStream::PushSysExMessage(const std::vector<uint8>& data,
74 size_t* current,
75 std::vector<uint8>* data_to_send) {
76 size_t index = *current;
77 size_t message_size = 0;
78 const size_t kMessageSizeMax = 3;
79 uint8 message[kMessageSizeMax] = {};
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;
92 uint8 byte = Get(data, index);
93 if ((byte & kSysRTMessageBitMask) == kSysRTMessageBitPattern) {
94 // System Real-Time messages interleaved in a SysEx message
95 PushSysRTMessage(data, &index, data_to_send);
96 continue;
99 message[message_size] = byte;
100 ++message_size;
101 if (byte == kEndOfSysExByte) {
102 uint8 code_index = static_cast<uint8>(message_size) + 0x4;
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;
112 ++index;
114 return false;
117 bool UsbMidiOutputStream::PushSysCommonMessage(
118 const std::vector<uint8>& data,
119 size_t* current,
120 std::vector<uint8>* data_to_send) {
121 size_t index = *current;
122 uint8 first_byte = Get(data, index);
123 DCHECK_LE(0xf1, first_byte);
124 DCHECK_LE(first_byte, 0xf7);
125 DCHECK_EQ(0xf0, first_byte & 0xf8);
126 // There are only 6 message types (0xf1 - 0xf7), so the table size is 8.
127 const size_t message_size_table[8] = {
128 0, 2, 3, 2, 1, 1, 1, 0,
130 size_t message_size = message_size_table[first_byte & 0x07];
131 DCHECK_NE(0u, message_size);
132 DCHECK_LE(message_size, 3u);
134 if (GetSize(data) < index + message_size) {
135 // The message is incomplete.
136 return false;
139 uint8 code_index = message_size == 1 ? 0x5 : static_cast<uint8>(message_size);
140 data_to_send->push_back((jack_.cable_number << 4) | code_index);
141 for (size_t i = index; i < index + 3; ++i)
142 data_to_send->push_back(i < index + message_size ? Get(data, i) : 0);
143 *current += message_size;
144 return true;
147 void UsbMidiOutputStream::PushSysRTMessage(const std::vector<uint8>& data,
148 size_t* current,
149 std::vector<uint8>* data_to_send) {
150 size_t index = *current;
151 uint8 first_byte = Get(data, index);
152 DCHECK_LE(0xf8, first_byte);
153 DCHECK_LE(first_byte, 0xff);
155 data_to_send->push_back((jack_.cable_number << 4) | 0x5);
156 data_to_send->push_back(first_byte);
157 data_to_send->push_back(0);
158 data_to_send->push_back(0);
159 *current += 1;
162 bool UsbMidiOutputStream::PushChannelMessage(const std::vector<uint8>& data,
163 size_t* current,
164 std::vector<uint8>* data_to_send) {
165 size_t index = *current;
166 uint8 first_byte = Get(data, index);
168 DCHECK_LE(0x80, (first_byte & 0xf0));
169 DCHECK_LE((first_byte & 0xf0), 0xe0);
170 // There are only 7 message types (0x8-0xe in the higher four bits), so the
171 // table size is 8.
172 const size_t message_size_table[8] = {
173 3, 3, 3, 3, 2, 3, 3, 0,
175 uint8 code_index = first_byte >> 4;
176 DCHECK_LE(0x08, code_index);
177 DCHECK_LE(code_index, 0x0e);
178 size_t message_size = message_size_table[code_index & 0x7];
179 DCHECK_NE(0u, message_size);
180 DCHECK_LE(message_size, 3u);
182 if (GetSize(data) < index + message_size) {
183 // The message is incomplete.
184 return false;
187 data_to_send->push_back((jack_.cable_number << 4) | code_index);
188 for (size_t i = index; i < index + 3; ++i)
189 data_to_send->push_back(i < index + message_size ? Get(data, i) : 0);
190 *current += message_size;
191 return true;
194 } // namespace midi
195 } // namespace media