Roll leveldb 3f7758:803d69 (v1.17 -> v1.18)
[chromium-blink-merge.git] / media / midi / usb_midi_output_stream.cc
blob2f3289754f082edcae351fa7e5fab3ac0f95ffbe
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 {
13 UsbMidiOutputStream::UsbMidiOutputStream(const UsbMidiJack& jack)
14 : jack_(jack), pending_size_(0), is_sending_sysex_(false) {}
16 void UsbMidiOutputStream::Send(const std::vector<uint8>& data) {
17 // To prevent link errors caused by DCHECK_*.
18 const size_t kPacketContentSize = UsbMidiOutputStream::kPacketContentSize;
19 DCHECK_LT(jack_.cable_number, 16u);
21 std::vector<uint8> data_to_send;
22 size_t current = 0;
23 size_t size = GetSize(data);
24 while (current < size) {
25 uint8 first_byte = Get(data, current);
26 if (first_byte == kSysExByte || is_sending_sysex_) {
27 // System Exclusive messages
28 if (!PushSysExMessage(data, &current, &data_to_send))
29 break;
30 } else if ((first_byte & kSysMessageBitMask) == kSysMessageBitPattern) {
31 if (first_byte & 0x08) {
32 // System Real-Time messages
33 PushSysRTMessage(data, &current, &data_to_send);
34 } else {
35 // System Common messages
36 if (!PushSysCommonMessage(data, &current, &data_to_send))
37 break;
39 } else if (first_byte & 0x80) {
40 if (!PushChannelMessage(data, &current, &data_to_send))
41 break;
42 } else {
43 // Unknown messages
44 DVLOG(1) << "Unknown byte: " << static_cast<unsigned int>(first_byte);
45 ++current;
49 if (data_to_send.size() > 0)
50 jack_.device->Send(jack_.endpoint_number(), data_to_send);
52 DCHECK_LE(current, size);
53 DCHECK_LE(size - current, kPacketContentSize);
54 // Note that this can be a self-copying and the iteration order is important.
55 for (size_t i = current; i < size; ++i)
56 pending_data_[i - current] = Get(data, i);
57 pending_size_ = size - current;
60 size_t UsbMidiOutputStream::GetSize(const std::vector<uint8>& data) const {
61 return data.size() + pending_size_;
64 uint8_t UsbMidiOutputStream::Get(const std::vector<uint8>& data,
65 size_t index) const {
66 DCHECK_LT(index, GetSize(data));
67 if (index < pending_size_)
68 return pending_data_[index];
69 return data[index - pending_size_];
72 bool UsbMidiOutputStream::PushSysExMessage(const std::vector<uint8>& data,
73 size_t* current,
74 std::vector<uint8>* data_to_send) {
75 size_t index = *current;
76 size_t message_size = 0;
77 const size_t kMessageSizeMax = 3;
78 uint8 message[kMessageSizeMax] = {};
80 while (index < GetSize(data)) {
81 if (message_size == kMessageSizeMax) {
82 // We can't find the end-of-message mark in the three bytes.
83 *current = index;
84 data_to_send->push_back((jack_.cable_number << 4) | 0x4);
85 data_to_send->insert(data_to_send->end(),
86 message,
87 message + arraysize(message));
88 is_sending_sysex_ = true;
89 return true;
91 uint8 byte = Get(data, index);
92 if ((byte & kSysRTMessageBitMask) == kSysRTMessageBitPattern) {
93 // System Real-Time messages interleaved in a SysEx message
94 PushSysRTMessage(data, &index, data_to_send);
95 continue;
98 message[message_size] = byte;
99 ++message_size;
100 if (byte == kEndOfSysExByte) {
101 uint8 code_index = message_size + 0x4;
102 DCHECK(code_index == 0x5 || code_index == 0x6 || code_index == 0x7);
103 data_to_send->push_back((jack_.cable_number << 4) | code_index);
104 data_to_send->insert(data_to_send->end(),
105 message,
106 message + arraysize(message));
107 *current = index + 1;
108 is_sending_sysex_ = false;
109 return true;
111 ++index;
113 return false;
116 bool UsbMidiOutputStream::PushSysCommonMessage(
117 const std::vector<uint8>& data,
118 size_t* current,
119 std::vector<uint8>* data_to_send) {
120 size_t index = *current;
121 uint8 first_byte = Get(data, index);
122 DCHECK_LE(0xf1, first_byte);
123 DCHECK_LE(first_byte, 0xf7);
124 DCHECK_EQ(0xf0, first_byte & 0xf8);
125 // There are only 6 message types (0xf1 - 0xf7), so the table size is 8.
126 const size_t message_size_table[8] = {
127 0, 2, 3, 2, 1, 1, 1, 0,
129 size_t message_size = message_size_table[first_byte & 0x07];
130 DCHECK_NE(0u, message_size);
131 DCHECK_LE(message_size, 3u);
133 if (GetSize(data) < index + message_size) {
134 // The message is incomplete.
135 return false;
138 uint8 code_index = message_size == 1 ? 0x5 : static_cast<uint8>(message_size);
139 data_to_send->push_back((jack_.cable_number << 4) | code_index);
140 for (size_t i = index; i < index + 3; ++i)
141 data_to_send->push_back(i < index + message_size ? Get(data, i) : 0);
142 *current += message_size;
143 return true;
146 void UsbMidiOutputStream::PushSysRTMessage(const std::vector<uint8>& data,
147 size_t* current,
148 std::vector<uint8>* data_to_send) {
149 size_t index = *current;
150 uint8 first_byte = Get(data, index);
151 DCHECK_LE(0xf8, first_byte);
152 DCHECK_LE(first_byte, 0xff);
154 data_to_send->push_back((jack_.cable_number << 4) | 0x5);
155 data_to_send->push_back(first_byte);
156 data_to_send->push_back(0);
157 data_to_send->push_back(0);
158 *current += 1;
161 bool UsbMidiOutputStream::PushChannelMessage(const std::vector<uint8>& data,
162 size_t* current,
163 std::vector<uint8>* data_to_send) {
164 size_t index = *current;
165 uint8 first_byte = Get(data, index);
167 DCHECK_LE(0x80, (first_byte & 0xf0));
168 DCHECK_LE((first_byte & 0xf0), 0xe0);
169 // There are only 7 message types (0x8-0xe in the higher four bits), so the
170 // table size is 8.
171 const size_t message_size_table[8] = {
172 3, 3, 3, 3, 2, 3, 3, 0,
174 uint8 code_index = first_byte >> 4;
175 DCHECK_LE(0x08, code_index);
176 DCHECK_LE(code_index, 0x0e);
177 size_t message_size = message_size_table[code_index & 0x7];
178 DCHECK_NE(0u, message_size);
179 DCHECK_LE(message_size, 3u);
181 if (GetSize(data) < index + message_size) {
182 // The message is incomplete.
183 return false;
186 data_to_send->push_back((jack_.cable_number << 4) | code_index);
187 for (size_t i = index; i < index + 3; ++i)
188 data_to_send->push_back(i < index + message_size ? Get(data, i) : 0);
189 *current += message_size;
190 return true;
193 } // namespace media