blob: 1beb682c1cb6a5622f46c8d0b0cf3505d17e6081 [file] [log] [blame]
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +00001// 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/midi_message_queue.h"
6
7#include <algorithm>
8
Hans Wennborg5f6a4612020-04-24 20:55:49 +00009#include "base/check_op.h"
10#include "base/notreached.h"
toyoshimbb2750c2016-10-20 05:13:24 -070011#include "media/midi/message_util.h"
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000012
toyoshime147c5e2015-05-07 21:58:31 -070013namespace midi {
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000014
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000015MidiMessageQueue::MidiMessageQueue(bool allow_running_status)
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000016 : allow_running_status_(allow_running_status) {}
17
Chris Watkinsc6cbcf62017-12-01 03:08:01 +000018MidiMessageQueue::~MidiMessageQueue() = default;
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000019
Avi Drissman3528fd02015-12-18 20:11:31 -050020void MidiMessageQueue::Add(const std::vector<uint8_t>& data) {
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000021 queue_.insert(queue_.end(), data.begin(), data.end());
22}
23
Avi Drissman3528fd02015-12-18 20:11:31 -050024void MidiMessageQueue::Add(const uint8_t* data, size_t length) {
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000025 queue_.insert(queue_.end(), data, data + length);
26}
27
Avi Drissman3528fd02015-12-18 20:11:31 -050028void MidiMessageQueue::Get(std::vector<uint8_t>* message) {
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000029 message->clear();
30
31 while (true) {
yukawaa0920f12015-10-08 00:09:59 -070032 // Check if |next_message_| is already a complete MIDI message or not.
33 if (!next_message_.empty()) {
Avi Drissman3528fd02015-12-18 20:11:31 -050034 const uint8_t status_byte = next_message_.front();
toyoshimbb2750c2016-10-20 05:13:24 -070035 const size_t target_len = GetMessageLength(status_byte);
yukawaa0920f12015-10-08 00:09:59 -070036 if (target_len == 0) {
toyoshimbb2750c2016-10-20 05:13:24 -070037 DCHECK_EQ(kSysExByte, status_byte);
38 if (next_message_.back() == kEndOfSysExByte) {
39 // OK, this is a complete SysEx message.
40 std::swap(*message, next_message_);
41 DCHECK(next_message_.empty());
yukawaa0920f12015-10-08 00:09:59 -070042 return;
43 }
44 } else if (next_message_.size() == target_len) {
45 // OK, this is a complete non-SysEx message.
46 std::swap(*message, next_message_);
47 DCHECK(next_message_.empty());
48 if (allow_running_status_ && !IsSystemMessage(status_byte)) {
49 // Speculatively keep the status byte in case of running status.
50 // If this assumption is not true, |next_message_| will be cleared
51 // anyway. Note that system common messages should reset the
52 // running status.
53 next_message_.push_back(status_byte);
54 }
55 return;
56 } else if (next_message_.size() > target_len) {
57 NOTREACHED();
58 }
59 }
60
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000061 if (queue_.empty())
62 return;
63
yukawaa0920f12015-10-08 00:09:59 -070064 // "System Real Time Messages" is a special MIDI message, which can appear
65 // at an arbitrary byte position of MIDI stream. Here we reorder
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000066 // "System Real Time Messages" prior to |next_message_| so that each message
67 // can be clearly separated as a complete MIDI message.
Avi Drissman3528fd02015-12-18 20:11:31 -050068 const uint8_t next = queue_.front();
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000069 if (IsSystemRealTimeMessage(next)) {
70 message->push_back(next);
yukawaa0920f12015-10-08 00:09:59 -070071 queue_.pop_front();
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000072 return;
73 }
74
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000075 if (next_message_.empty()) {
toyoshimbb2750c2016-10-20 05:13:24 -070076 const size_t target_len = GetMessageLength(next);
yukawaa0920f12015-10-08 00:09:59 -070077 // If |target_len| is zero, it means either |next| is not a valid status
78 // byte or |next| is a valid status byte but the message length is
79 // unpredictable. For the latter case, only SysEx can be accepted.
toyoshimbb2750c2016-10-20 05:13:24 -070080 if (target_len > 0 || next == kSysExByte) {
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000081 next_message_.push_back(next);
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000082 }
yukawaa0920f12015-10-08 00:09:59 -070083 // Consume |next| always, since if |next| isn't status byte, which means
84 // that |next| is just corrupted data, or a data byte followed by
85 // reserved message, which we are unable to understand and deal with
86 // anyway.
87 queue_.pop_front();
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000088 continue;
89 }
90
Avi Drissman3528fd02015-12-18 20:11:31 -050091 const uint8_t status_byte = next_message_.front();
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000092
yukawaa0920f12015-10-08 00:09:59 -070093 // If we receive a new non-data byte before completing the pending message,
94 // drop the pending message and respin the loop to re-evaluate |next|.
95 // This also clears the running status byte speculatively added above, as
96 // well as any broken incomplete messages.
toyoshimbb2750c2016-10-20 05:13:24 -070097 if (!IsDataByte(next) &&
98 !(status_byte == kSysExByte && next == kEndOfSysExByte)) {
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +000099 next_message_.clear();
yukawaa0920f12015-10-08 00:09:59 -0700100 continue;
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +0000101 }
102
yukawaa0920f12015-10-08 00:09:59 -0700103 // OK to consume this byte.
104 next_message_.push_back(next);
105 queue_.pop_front();
yukawa@chromium.org09ef89a2013-11-29 06:27:41 +0000106 }
107}
108
toyoshime147c5e2015-05-07 21:58:31 -0700109} // namespace midi