Chromium Code Reviews| Index: media/midi/midi_message_util.cc |
| diff --git a/media/midi/midi_message_util.cc b/media/midi/midi_message_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c7b05ef22bef32c75a49efe487901711f45d39b |
| --- /dev/null |
| +++ b/media/midi/midi_message_util.cc |
| @@ -0,0 +1,175 @@ |
| +// 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/midi_message_util.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/logging.h" |
| + |
| +namespace media { |
| +namespace { |
| + |
| +const uint8 kSysEx = 0xf0; |
| +const uint8 kEndOfSysEx = 0xf7; |
| + |
| +bool IsDataByte(uint8 data) { |
| + return (data & 0x80) == 0; |
| +} |
| + |
| +bool IsFirstStatusByte(uint8 data) { |
| + return !IsDataByte(data) && data != kEndOfSysEx; |
| +} |
| + |
| +bool IsSystemRealTimeMessage(uint8 data) { |
| + return 0xf8 <= data && data <= 0xff; |
| +} |
| + |
| +size_t GetMessageLength(uint8 data) { |
| + if (IsDataByte(data)) |
| + return 0; |
| + else if (0x80 <= data && data <= 0xbf) |
|
scherkus (not reviewing)
2013/11/20 20:16:00
nit: since you return every time I'd drop all the
yukawa
2013/11/21 01:19:40
Done.
|
| + return 3; |
| + else if (0xc0 <= data && data <= 0xdf) |
| + return 2; |
| + else if (0xe0 <= data && data <= 0xef) |
| + return 3; |
| + else if (data == 0xf0) |
| + return 0; |
| + else if (data == 0xf1) |
| + return 2; |
| + else if (data == 0xf2) |
| + return 3; |
| + else if (data == 0xf3) |
| + return 2; |
| + else if (0xf4 <= data && data <= 0xf6) |
| + return 1; |
| + else if (data == 0xf7) |
| + return 0; |
| + else // 0xf8 <= data && data <= 0xff |
| + return 1; |
| +} |
| + |
| +} // namespace |
| + |
| +MIDIMessageQueue::MIDIMessageQueue(bool allow_running_status) |
| + : allow_running_status_(allow_running_status) {} |
| + |
| +MIDIMessageQueue::~MIDIMessageQueue() {} |
| + |
| +void MIDIMessageQueue::Add(const std::vector<uint8>& data) { |
| + queue_.insert(queue_.end(), data.begin(), data.end()); |
| +} |
| + |
| +void MIDIMessageQueue::Add(const uint8* data, size_t length) { |
| + queue_.insert(queue_.end(), data, data + length); |
| +} |
| + |
| +void MIDIMessageQueue::Get(std::vector<uint8>* message) { |
| + message->clear(); |
| + |
| + while (true) { |
| + if (queue_.empty()) |
| + return; |
| + |
| + const uint8 next = queue_.front(); |
| + queue_.pop_front(); |
| + |
| + // "System Real Time Messages" is a special kind of MIDI messages, which can |
| + // appear at arbitrary byte position of MIDI stream. Here we reorder |
| + // "System Real Time Messages" prior to |next_message_| so that each message |
| + // can be clearly separated as a complete MIDI message. |
| + if (IsSystemRealTimeMessage(next)) { |
| + message->push_back(next); |
| + return; |
| + } |
| + |
| + // Here |next_message_[0]| may contain the previous status byte when |
| + // |allow_running_status_| is true. Following condition fixes up |
| + // |next_message_| if running status condition is not fulfilled. |
| + if (!next_message_.empty() && |
| + ((next_message_[0] == kSysEx && IsFirstStatusByte(next)) || |
| + (next_message_[0] != kSysEx && !IsDataByte(next)))) { |
| + // An invalid data sequence is found or running status condition is not |
| + // fulfilled. |
| + next_message_.clear(); |
| + } |
| + |
| + if (next_message_.empty()) { |
| + if (IsFirstStatusByte(next)) { |
| + next_message_.push_back(next); |
| + } else { |
| + // MIDI protocol doesn't provide any error correction mechanism in |
| + // physical layers, and incoming messages can be corrupted, and should |
| + // be corrected here. |
| + } |
| + continue; |
| + } |
| + |
| + // Here we can assume |next_message_| starts with a valid status byte. |
| + const uint8 status_byte = next_message_[0]; |
| + next_message_.push_back(next); |
| + |
| + if (status_byte == kSysEx) { |
| + if (next == kEndOfSysEx) { |
| + std::swap(*message, next_message_); |
| + next_message_.clear(); |
| + return; |
| + } |
| + continue; |
| + } |
| + |
| + DCHECK(IsDataByte(next)); |
| + DCHECK_NE(kSysEx, status_byte); |
| + const size_t target_len = GetMessageLength(status_byte); |
| + if (next_message_.size() < target_len) |
| + continue; |
| + if (next_message_.size() == target_len) { |
| + std::swap(*message, next_message_); |
| + next_message_.clear(); |
| + if (allow_running_status_) { |
| + // Speculatively keep the status byte in case of running status. If this |
| + // assumption is not true, |next_message_| will be cleared anyway. |
| + next_message_.push_back(status_byte); |
| + } |
| + return; |
| + } |
| + |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +bool IsValidWebMIDIMessage(const std::vector<uint8>& data) { |
| + bool in_sysex = false; |
| + size_t waiting_data_length = 0; |
| + for (size_t i = 0; i < data.size(); ++i) { |
| + const uint8 current = data[i]; |
| + if (IsSystemRealTimeMessage(current)) |
| + continue; // Real time message can be placed at any point. |
| + if (waiting_data_length > 0) { |
| + if (!IsDataByte(current)) |
| + return false; // Error: |current| should have been data byte. |
| + --waiting_data_length; |
| + continue; // Found data byte as expected. |
| + } |
| + if (in_sysex) { |
| + if (data[i] == kEndOfSysEx) |
| + in_sysex = false; |
| + else if (!IsDataByte(current)) |
| + return false; // Error: |current| should have been data byte. |
| + continue; // Found data byte as expected. |
| + } |
| + if (current == kSysEx) { |
| + in_sysex = true; |
| + continue; // Found SysEX |
| + } |
| + waiting_data_length = GetMessageLength(current); |
| + if (waiting_data_length == 0) |
| + return false; // Error: |current| should have been a valid status byte. |
| + --waiting_data_length; // Found status byte |
| + } |
| + return waiting_data_length == 0 && !in_sysex; |
| +} |
| + |
| +} // namespace media |