OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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); | |
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); | |
Takashi Toyoshima
2014/01/15 12:08:40
Real-Time messages can be inserted in the midst of
yhirano
2014/01/15 13:25:42
Thank you, I didn't know that.
Done in PushSysExMe
Takashi Toyoshima
2014/01/16 03:43:36
Why don't you handle it here, but in PushSysExMess
yhirano
2014/01/16 04:15:24
Is it possible without inlining PushSysExMessage h
Takashi Toyoshima
2014/01/16 04:31:22
I thought one USB event packet contains only one n
| |
24 if (first_byte == kSystemExclusiveByte || is_sending_sysex_) { | |
25 // System Exclusive messages | |
26 if (!PushSysExMessage(data, ¤t, &data_to_send)) | |
27 break; | |
28 } else if ((first_byte & 0xf0) == kSystemMessagePattern) { | |
Takashi Toyoshima
2014/01/15 12:08:40
(first_byte & kMessageTypeMask) == kSystemMessageT
yhirano
2014/01/15 13:25:42
Done.
| |
29 if (first_byte & 0x08) { | |
30 // System Real-Time messages | |
31 if (!PushSysRTMessage(data, ¤t, &data_to_send)) | |
32 break; | |
33 } else { | |
34 // System Common messages | |
35 if (!PushSysCommonMessage(data, ¤t, &data_to_send)) | |
36 break; | |
37 } | |
38 } else if (first_byte & 0x80) { | |
39 if (!PushChannelMessage(data, ¤t, &data_to_send)) | |
40 break; | |
41 } else { | |
42 // Unknown messages | |
43 DVLOG(1) << "Unknown byte: " << static_cast<unsigned int>(first_byte); | |
Takashi Toyoshima
2014/01/15 12:08:40
You may want to add similar DVLOG() for other retu
yhirano
2014/01/15 13:25:42
Push* function may return false, but it is not an
| |
44 ++current; | |
45 } | |
46 } | |
47 | |
48 if (data_to_send.size() > 0) | |
49 jack_.device->Send(jack_.endpoint_number(), data_to_send); | |
50 | |
51 DCHECK_LE(current, size); | |
52 DCHECK_LE(size - current, kPacketContentSize); | |
53 // Note that this can be a self-copying and the iteration order is important. | |
54 for (size_t i = current; i < size; ++i) | |
55 pending_data_[i - current] = Get(data, i); | |
56 pending_size_ = size - current; | |
57 } | |
58 | |
59 size_t UsbMidiOutputStream::GetSize(const std::vector<uint8>& data) const { | |
60 return data.size() + pending_size_; | |
61 } | |
62 | |
63 uint8_t UsbMidiOutputStream::Get(const std::vector<uint8>& data, | |
64 size_t index) const { | |
65 DCHECK_LT(index, GetSize(data)); | |
66 if (index < pending_size_) | |
67 return pending_data_[index]; | |
68 return data[index - pending_size_]; | |
69 } | |
70 | |
71 bool UsbMidiOutputStream::PushSysExMessage(const std::vector<uint8>& data, | |
72 size_t* current, | |
73 std::vector<uint8>* data_to_send) { | |
74 size_t index = *current; | |
75 for (size_t j = index; j < GetSize(data); ++j) { | |
76 if (j == index + 3) { | |
77 // We can't find the end-of-message mark in the three bytes. | |
78 *current = j; | |
79 data_to_send->push_back((jack_.cable_number << 4) | 0x4); | |
80 data_to_send->push_back(Get(data, index)); | |
81 data_to_send->push_back(Get(data, index + 1)); | |
82 data_to_send->push_back(Get(data, index + 2)); | |
83 is_sending_sysex_ = true; | |
84 return true; | |
85 } | |
86 if (Get(data, j) == kSystemExclusiveEndByte) { | |
87 uint8 code_index = (j - index) + 0x5; | |
88 DCHECK(code_index == 0x5 || code_index == 0x6 || code_index == 0x7); | |
89 data_to_send->push_back((jack_.cable_number << 4) | code_index); | |
90 for (size_t k = index; k < index + 3; ++k) | |
91 data_to_send->push_back(k <= j ? Get(data, k) : 0); | |
92 *current = j + 1; | |
93 is_sending_sysex_ = false; | |
94 return true; | |
95 } | |
96 } | |
97 return false; | |
98 } | |
99 | |
100 bool UsbMidiOutputStream::PushSysCommonMessage(const std::vector<uint8>& data, | |
101 size_t* current, | |
102 std::vector<uint8>* data_to_send) { | |
103 size_t index = *current; | |
104 uint8 first_byte = Get(data, index); | |
105 DCHECK_LE(0xf1, first_byte); | |
106 DCHECK_LE(first_byte, 0xf7); | |
107 const size_t message_size_table[8] = { | |
108 0, 2, 3, 2, 1, 1, 1, 0, | |
109 }; | |
110 size_t message_size = message_size_table[first_byte & 0x0f]; | |
111 DCHECK_NE(0u, message_size); | |
112 DCHECK_LE(message_size, 3u); | |
113 | |
114 if (GetSize(data) < index + message_size) { | |
115 // The message is incomplete. | |
116 return false; | |
117 } | |
118 | |
119 uint8 code_index = message_size == 1 ? 0x5 : static_cast<uint8>(message_size); | |
120 data_to_send->push_back((jack_.cable_number << 4) | code_index); | |
121 for (size_t i = index; i < index + 3; ++i) | |
122 data_to_send->push_back(i < index + message_size ? Get(data, i) : 0); | |
123 *current += message_size; | |
124 return true; | |
125 } | |
126 | |
127 bool UsbMidiOutputStream::PushSysRTMessage(const std::vector<uint8>& data, | |
128 size_t* current, | |
129 std::vector<uint8>* data_to_send) { | |
130 size_t index = *current; | |
131 uint8 first_byte = Get(data, index); | |
132 DCHECK_LE(0xf8, first_byte); | |
133 DCHECK_LE(first_byte, 0xff); | |
134 | |
135 data_to_send->push_back((jack_.cable_number << 4) | 0x5); | |
136 data_to_send->push_back(first_byte); | |
137 data_to_send->push_back(0); | |
138 data_to_send->push_back(0); | |
139 *current += 1; | |
140 return true; | |
141 } | |
142 | |
143 bool UsbMidiOutputStream::PushChannelMessage(const std::vector<uint8>& data, | |
144 size_t* current, | |
145 std::vector<uint8>* data_to_send) { | |
146 size_t index = *current; | |
147 uint8 first_byte = Get(data, index); | |
148 DCHECK_LE(0x80, (first_byte & 0xf0)); | |
149 DCHECK_LE((first_byte & 0xf0), 0xe0); | |
150 | |
151 const size_t message_size_table[8] = { | |
152 3, 3, 3, 3, 2, 3, 3, 0, | |
153 }; | |
154 uint8 code_index = first_byte >> 4; | |
155 size_t message_size = message_size_table[code_index & 0x7]; | |
156 DCHECK_NE(0u, message_size); | |
157 DCHECK_LE(message_size, 3u); | |
158 | |
159 if (GetSize(data) < index + message_size) { | |
160 // The message is incomplete. | |
161 return false; | |
162 } | |
163 | |
164 data_to_send->push_back((jack_.cable_number << 4) | code_index); | |
165 for (size_t i = index; i < index + 3; ++i) | |
166 data_to_send->push_back(i < index + message_size ? Get(data, i) : 0); | |
167 *current += message_size; | |
168 return true; | |
169 } | |
170 | |
171 } // namespace media | |
OLD | NEW |