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"
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
;
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
, ¤t
, &data_to_send
))
31 } else if ((first_byte
& kSysMessageBitMask
) == kSysMessageBitPattern
) {
32 if (first_byte
& 0x08) {
33 // System Real-Time messages
34 PushSysRTMessage(data
, ¤t
, &data_to_send
);
36 // System Common messages
37 if (!PushSysCommonMessage(data
, ¤t
, &data_to_send
))
40 } else if (first_byte
& 0x80) {
41 if (!PushChannelMessage(data
, ¤t
, &data_to_send
))
45 DVLOG(1) << "Unknown byte: " << static_cast<unsigned int>(first_byte
);
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
,
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
,
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.
85 data_to_send
->push_back((jack_
.cable_number
<< 4) | 0x4);
86 data_to_send
->insert(data_to_send
->end(),
88 message
+ arraysize(message
));
89 is_sending_sysex_
= 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
);
99 message
[message_size
] = byte
;
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(),
107 message
+ arraysize(message
));
108 *current
= index
+ 1;
109 is_sending_sysex_
= false;
117 bool UsbMidiOutputStream::PushSysCommonMessage(
118 const std::vector
<uint8
>& data
,
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.
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
;
147 void UsbMidiOutputStream::PushSysRTMessage(const std::vector
<uint8
>& data
,
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);
162 bool UsbMidiOutputStream::PushChannelMessage(const std::vector
<uint8
>& data
,
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
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.
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
;