| 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..4ca69adf9c7ad1b657691f52b8c791e156efbd05
|
| --- /dev/null
|
| +++ b/media/midi/usb_midi_output_stream.cc
|
| @@ -0,0 +1,178 @@
|
| +// Copyright (c) 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/usb_midi_device.h"
|
| +
|
| +namespace media {
|
| +
|
| +namespace {
|
| +
|
| +const uint8 kSysExByte = 0xf0;
|
| +const uint8 kSysExEndByte = 0xf7;
|
| +const uint8 kSystemMessagePattern = 0xf0;
|
| +
|
| +} // namespace
|
| +
|
| +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);
|
| + if (first_byte == kSysExByte || is_sending_sysex_) {
|
| + // System Exclusive messages
|
| + if (!PushSysExMessage(data, ¤t, &data_to_send))
|
| + break;
|
| + } else if ((first_byte & 0xf0) == kSystemMessagePattern) {
|
| + 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);
|
| + ++current;
|
| + }
|
| + }
|
| +
|
| + if (data_to_send.size() > 0)
|
| + jack_.device->Send(jack_.GetEndpointNumber(), 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) == kSysExEndByte) {
|
| + uint8 cin = (j - index) + 0x5;
|
| + DCHECK(cin == 0x5 || cin == 0x6 || cin == 0x7);
|
| + data_to_send->push_back((jack_.cable_number << 4) | cin);
|
| + 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 cin = message_size == 1 ? 0x5 : static_cast<uint8>(message_size);
|
| + data_to_send->push_back((jack_.cable_number << 4) | cin);
|
| + 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 cin = first_byte >> 4;
|
| + size_t message_size = message_size_table[cin & 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) | cin);
|
| + 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
|
|
|