OLD | NEW |
---|---|
(Empty) | |
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 { | |
12 | |
13 void UsbMidiOutputStream::Send(const std::vector<uint8>& data) { | |
14 // To prevent link errors caused by DCHECK_*. | |
15 const size_t kPacketContentSize = UsbMidiOutputStream::kPacketContentSize; | |
16 DCHECK_LT(jack_.cable_number, 16u); | |
17 DCHECK_EQ(3u, kPacketContentSize); | |
scherkus (not reviewing)
2014/01/16 21:55:52
what is this DCHECK() protecting against?
UsbMidi
yhirano
2014/01/20 09:12:19
I added it for readability, but OK, I deleted.
| |
18 | |
19 std::vector<uint8> data_to_send; | |
20 size_t current = 0; | |
21 size_t size = GetSize(data); | |
22 while (current < size) { | |
23 uint8 first_byte = Get(data, current); | |
24 if (first_byte == kSysExByte || is_sending_sysex_) { | |
25 // System Exclusive messages | |
26 if (!PushSysExMessage(data, ¤t, &data_to_send)) | |
27 break; | |
28 } else if ((first_byte & kSysMessageBitMask) == kSysMessageBitPattern) { | |
29 if (first_byte & 0x08) { | |
30 // System Real-Time messages | |
31 PushSysRTMessage(data, ¤t, &data_to_send); | |
32 } else { | |
33 // System Common messages | |
34 if (!PushSysCommonMessage(data, ¤t, &data_to_send)) | |
35 break; | |
36 } | |
37 } else if (first_byte & 0x80) { | |
38 if (!PushChannelMessage(data, ¤t, &data_to_send)) | |
39 break; | |
40 } else { | |
41 // Unknown messages | |
42 DVLOG(1) << "Unknown byte: " << static_cast<unsigned int>(first_byte); | |
43 ++current; | |
44 } | |
45 } | |
46 | |
47 if (data_to_send.size() > 0) | |
48 jack_.device->Send(jack_.endpoint_number(), data_to_send); | |
49 | |
50 DCHECK_LE(current, size); | |
51 DCHECK_LE(size - current, kPacketContentSize); | |
52 // Note that this can be a self-copying and the iteration order is important. | |
53 for (size_t i = current; i < size; ++i) | |
54 pending_data_[i - current] = Get(data, i); | |
55 pending_size_ = size - current; | |
56 } | |
57 | |
58 size_t UsbMidiOutputStream::GetSize(const std::vector<uint8>& data) const { | |
59 return data.size() + pending_size_; | |
60 } | |
61 | |
62 uint8_t UsbMidiOutputStream::Get(const std::vector<uint8>& data, | |
63 size_t index) const { | |
64 DCHECK_LT(index, GetSize(data)); | |
65 if (index < pending_size_) | |
66 return pending_data_[index]; | |
67 return data[index - pending_size_]; | |
68 } | |
69 | |
70 bool UsbMidiOutputStream::PushSysExMessage(const std::vector<uint8>& data, | |
71 size_t* current, | |
72 std::vector<uint8>* data_to_send) { | |
73 size_t index = *current; | |
74 size_t message_size = 0; | |
75 const size_t kMessageSizeMax = 3; | |
76 uint8 message[kMessageSizeMax] = {}; | |
77 | |
78 while (index < GetSize(data)) { | |
79 if (message_size == kMessageSizeMax) { | |
80 // We can't find the end-of-message mark in the three bytes. | |
81 *current = index; | |
82 data_to_send->push_back((jack_.cable_number << 4) | 0x4); | |
83 data_to_send->insert(data_to_send->end(), | |
84 message, | |
85 message + arraysize(message)); | |
86 is_sending_sysex_ = true; | |
87 return true; | |
88 } | |
89 uint8 byte = Get(data, index); | |
90 if ((byte & kSysRTMessageBitMask) == kSysRTMessageBitPattern) { | |
91 // System Real-Time messages interleaved in a SysEx message | |
92 PushSysRTMessage(data, &index, data_to_send); | |
93 continue; | |
94 } | |
95 | |
96 message[message_size] = byte; | |
97 ++message_size; | |
98 if (byte == kEndOfSysExByte) { | |
99 uint8 code_index = message_size + 0x4; | |
100 DCHECK(code_index == 0x5 || code_index == 0x6 || code_index == 0x7); | |
101 data_to_send->push_back((jack_.cable_number << 4) | code_index); | |
102 data_to_send->insert(data_to_send->end(), | |
103 message, | |
104 message + arraysize(message)); | |
105 *current = index + 1; | |
106 is_sending_sysex_ = false; | |
107 return true; | |
108 } | |
109 ++index; | |
110 } | |
111 return false; | |
112 } | |
113 | |
114 bool UsbMidiOutputStream::PushSysCommonMessage( | |
115 const std::vector<uint8>& data, | |
116 size_t* current, | |
117 std::vector<uint8>* data_to_send) { | |
118 size_t index = *current; | |
119 uint8 first_byte = Get(data, index); | |
120 DCHECK_LE(0xf1, first_byte); | |
121 DCHECK_LE(first_byte, 0xf7); | |
122 const size_t message_size_table[8] = { | |
123 0, 2, 3, 2, 1, 1, 1, 0, | |
124 }; | |
125 size_t message_size = message_size_table[first_byte & 0x0f]; | |
126 DCHECK_NE(0u, message_size); | |
127 DCHECK_LE(message_size, 3u); | |
128 | |
129 if (GetSize(data) < index + message_size) { | |
130 // The message is incomplete. | |
131 return false; | |
132 } | |
133 | |
134 uint8 code_index = message_size == 1 ? 0x5 : static_cast<uint8>(message_size); | |
135 data_to_send->push_back((jack_.cable_number << 4) | code_index); | |
136 for (size_t i = index; i < index + 3; ++i) | |
137 data_to_send->push_back(i < index + message_size ? Get(data, i) : 0); | |
138 *current += message_size; | |
139 return true; | |
140 } | |
141 | |
142 void UsbMidiOutputStream::PushSysRTMessage(const std::vector<uint8>& data, | |
143 size_t* current, | |
144 std::vector<uint8>* data_to_send) { | |
145 size_t index = *current; | |
146 uint8 first_byte = Get(data, index); | |
147 DCHECK_LE(0xf8, first_byte); | |
148 DCHECK_LE(first_byte, 0xff); | |
149 | |
150 data_to_send->push_back((jack_.cable_number << 4) | 0x5); | |
151 data_to_send->push_back(first_byte); | |
152 data_to_send->push_back(0); | |
153 data_to_send->push_back(0); | |
154 *current += 1; | |
155 } | |
156 | |
157 bool UsbMidiOutputStream::PushChannelMessage(const std::vector<uint8>& data, | |
158 size_t* current, | |
159 std::vector<uint8>* data_to_send) { | |
160 size_t index = *current; | |
161 uint8 first_byte = Get(data, index); | |
162 DCHECK_LE(0x80, (first_byte & 0xf0)); | |
163 DCHECK_LE((first_byte & 0xf0), 0xe0); | |
164 | |
165 const size_t message_size_table[8] = { | |
166 3, 3, 3, 3, 2, 3, 3, 0, | |
167 }; | |
168 uint8 code_index = first_byte >> 4; | |
169 size_t message_size = message_size_table[code_index & 0x7]; | |
170 DCHECK_NE(0u, message_size); | |
171 DCHECK_LE(message_size, 3u); | |
172 | |
173 if (GetSize(data) < index + message_size) { | |
174 // The message is incomplete. | |
175 return false; | |
176 } | |
177 | |
178 data_to_send->push_back((jack_.cable_number << 4) | code_index); | |
179 for (size_t i = index; i < index + 3; ++i) | |
180 data_to_send->push_back(i < index + message_size ? Get(data, i) : 0); | |
181 *current += message_size; | |
182 return true; | |
183 } | |
184 | |
185 } // namespace media | |
OLD | NEW |