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