Chromium Code Reviews| Index: media/midi/usb_midi_output_stream.cc |
| diff --git a/media/midi/usb_midi_output_stream.cc b/media/midi/usb_midi_output_stream.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0485c3fe98f65f03f632835db81221298f8a196d |
| --- /dev/null |
| +++ b/media/midi/usb_midi_output_stream.cc |
| @@ -0,0 +1,171 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/midi/usb_midi_output_stream.h" |
| + |
| +#include "base/logging.h" |
| +#include "media/midi/midi_message_util.h" |
| +#include "media/midi/usb_midi_device.h" |
| + |
| +namespace media { |
| + |
| +void UsbMidiOutputStream::Send(const std::vector<uint8>& data) { |
| + // To prevent link errors caused by DCHECK_*. |
| + const size_t kPacketContentSize = UsbMidiOutputStream::kPacketContentSize; |
| + DCHECK_LT(jack_.cable_number, 16u); |
| + DCHECK_EQ(3u, kPacketContentSize); |
| + |
| + std::vector<uint8> data_to_send; |
| + size_t current = 0; |
| + size_t size = GetSize(data); |
| + while (current < size) { |
| + 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
|
| + if (first_byte == kSystemExclusiveByte || is_sending_sysex_) { |
| + // System Exclusive messages |
| + if (!PushSysExMessage(data, ¤t, &data_to_send)) |
| + break; |
| + } 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.
|
| + if (first_byte & 0x08) { |
| + // System Real-Time messages |
| + if (!PushSysRTMessage(data, ¤t, &data_to_send)) |
| + break; |
| + } else { |
| + // System Common messages |
| + if (!PushSysCommonMessage(data, ¤t, &data_to_send)) |
| + break; |
| + } |
| + } else if (first_byte & 0x80) { |
| + if (!PushChannelMessage(data, ¤t, &data_to_send)) |
| + break; |
| + } else { |
| + // Unknown messages |
| + 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
|
| + ++current; |
| + } |
| + } |
| + |
| + if (data_to_send.size() > 0) |
| + jack_.device->Send(jack_.endpoint_number(), data_to_send); |
| + |
| + DCHECK_LE(current, size); |
| + DCHECK_LE(size - current, kPacketContentSize); |
| + // Note that this can be a self-copying and the iteration order is important. |
| + for (size_t i = current; i < size; ++i) |
| + pending_data_[i - current] = Get(data, i); |
| + pending_size_ = size - current; |
| +} |
| + |
| +size_t UsbMidiOutputStream::GetSize(const std::vector<uint8>& data) const { |
| + return data.size() + pending_size_; |
| +} |
| + |
| +uint8_t UsbMidiOutputStream::Get(const std::vector<uint8>& data, |
| + size_t index) const { |
| + DCHECK_LT(index, GetSize(data)); |
| + if (index < pending_size_) |
| + return pending_data_[index]; |
| + return data[index - pending_size_]; |
| +} |
| + |
| +bool UsbMidiOutputStream::PushSysExMessage(const std::vector<uint8>& data, |
| + size_t* current, |
| + std::vector<uint8>* data_to_send) { |
| + size_t index = *current; |
| + for (size_t j = index; j < GetSize(data); ++j) { |
| + if (j == index + 3) { |
| + // We can't find the end-of-message mark in the three bytes. |
| + *current = j; |
| + data_to_send->push_back((jack_.cable_number << 4) | 0x4); |
| + data_to_send->push_back(Get(data, index)); |
| + data_to_send->push_back(Get(data, index + 1)); |
| + data_to_send->push_back(Get(data, index + 2)); |
| + is_sending_sysex_ = true; |
| + return true; |
| + } |
| + if (Get(data, j) == kSystemExclusiveEndByte) { |
| + uint8 code_index = (j - index) + 0x5; |
| + DCHECK(code_index == 0x5 || code_index == 0x6 || code_index == 0x7); |
| + data_to_send->push_back((jack_.cable_number << 4) | code_index); |
| + for (size_t k = index; k < index + 3; ++k) |
| + data_to_send->push_back(k <= j ? Get(data, k) : 0); |
| + *current = j + 1; |
| + is_sending_sysex_ = false; |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +bool UsbMidiOutputStream::PushSysCommonMessage(const std::vector<uint8>& data, |
| + size_t* current, |
| + std::vector<uint8>* data_to_send) { |
| + size_t index = *current; |
| + uint8 first_byte = Get(data, index); |
| + DCHECK_LE(0xf1, first_byte); |
| + DCHECK_LE(first_byte, 0xf7); |
| + const size_t message_size_table[8] = { |
| + 0, 2, 3, 2, 1, 1, 1, 0, |
| + }; |
| + size_t message_size = message_size_table[first_byte & 0x0f]; |
| + DCHECK_NE(0u, message_size); |
| + DCHECK_LE(message_size, 3u); |
| + |
| + if (GetSize(data) < index + message_size) { |
| + // The message is incomplete. |
| + return false; |
| + } |
| + |
| + uint8 code_index = message_size == 1 ? 0x5 : static_cast<uint8>(message_size); |
| + data_to_send->push_back((jack_.cable_number << 4) | code_index); |
| + for (size_t i = index; i < index + 3; ++i) |
| + data_to_send->push_back(i < index + message_size ? Get(data, i) : 0); |
| + *current += message_size; |
| + return true; |
| +} |
| + |
| +bool UsbMidiOutputStream::PushSysRTMessage(const std::vector<uint8>& data, |
| + size_t* current, |
| + std::vector<uint8>* data_to_send) { |
| + size_t index = *current; |
| + uint8 first_byte = Get(data, index); |
| + DCHECK_LE(0xf8, first_byte); |
| + DCHECK_LE(first_byte, 0xff); |
| + |
| + data_to_send->push_back((jack_.cable_number << 4) | 0x5); |
| + data_to_send->push_back(first_byte); |
| + data_to_send->push_back(0); |
| + data_to_send->push_back(0); |
| + *current += 1; |
| + return true; |
| +} |
| + |
| +bool UsbMidiOutputStream::PushChannelMessage(const std::vector<uint8>& data, |
| + size_t* current, |
| + std::vector<uint8>* data_to_send) { |
| + size_t index = *current; |
| + uint8 first_byte = Get(data, index); |
| + DCHECK_LE(0x80, (first_byte & 0xf0)); |
| + DCHECK_LE((first_byte & 0xf0), 0xe0); |
| + |
| + const size_t message_size_table[8] = { |
| + 3, 3, 3, 3, 2, 3, 3, 0, |
| + }; |
| + uint8 code_index = first_byte >> 4; |
| + size_t message_size = message_size_table[code_index & 0x7]; |
| + DCHECK_NE(0u, message_size); |
| + DCHECK_LE(message_size, 3u); |
| + |
| + if (GetSize(data) < index + message_size) { |
| + // The message is incomplete. |
| + return false; |
| + } |
| + |
| + data_to_send->push_back((jack_.cable_number << 4) | code_index); |
| + for (size_t i = index; i < index + 3; ++i) |
| + data_to_send->push_back(i < index + message_size ? Get(data, i) : 0); |
| + *current += message_size; |
| + return true; |
| +} |
| + |
| +} // namespace media |