OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/midi/midi_message_queue.h" | 5 #include "media/midi/midi_message_queue.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "media/midi/midi_message_util.h" | 10 #include "media/midi/midi_message_util.h" |
11 | 11 |
12 namespace media { | 12 namespace media { |
13 namespace midi { | 13 namespace midi { |
14 namespace { | 14 namespace { |
15 | 15 |
16 const uint8 kSysEx = 0xf0; | 16 const uint8_t kSysEx = 0xf0; |
17 const uint8 kEndOfSysEx = 0xf7; | 17 const uint8_t kEndOfSysEx = 0xf7; |
18 | 18 |
19 bool IsDataByte(uint8 data) { | 19 bool IsDataByte(uint8_t data) { |
20 return (data & 0x80) == 0; | 20 return (data & 0x80) == 0; |
21 } | 21 } |
22 | 22 |
23 bool IsSystemRealTimeMessage(uint8 data) { | 23 bool IsSystemRealTimeMessage(uint8_t data) { |
24 return 0xf8 <= data; | 24 return 0xf8 <= data; |
25 } | 25 } |
26 | 26 |
27 bool IsSystemMessage(uint8 data) { | 27 bool IsSystemMessage(uint8_t data) { |
28 return 0xf0 <= data; | 28 return 0xf0 <= data; |
29 } | 29 } |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 MidiMessageQueue::MidiMessageQueue(bool allow_running_status) | 33 MidiMessageQueue::MidiMessageQueue(bool allow_running_status) |
34 : allow_running_status_(allow_running_status) {} | 34 : allow_running_status_(allow_running_status) {} |
35 | 35 |
36 MidiMessageQueue::~MidiMessageQueue() {} | 36 MidiMessageQueue::~MidiMessageQueue() {} |
37 | 37 |
38 void MidiMessageQueue::Add(const std::vector<uint8>& data) { | 38 void MidiMessageQueue::Add(const std::vector<uint8_t>& data) { |
39 queue_.insert(queue_.end(), data.begin(), data.end()); | 39 queue_.insert(queue_.end(), data.begin(), data.end()); |
40 } | 40 } |
41 | 41 |
42 void MidiMessageQueue::Add(const uint8* data, size_t length) { | 42 void MidiMessageQueue::Add(const uint8_t* data, size_t length) { |
43 queue_.insert(queue_.end(), data, data + length); | 43 queue_.insert(queue_.end(), data, data + length); |
44 } | 44 } |
45 | 45 |
46 void MidiMessageQueue::Get(std::vector<uint8>* message) { | 46 void MidiMessageQueue::Get(std::vector<uint8_t>* message) { |
47 message->clear(); | 47 message->clear(); |
48 | 48 |
49 while (true) { | 49 while (true) { |
50 // Check if |next_message_| is already a complete MIDI message or not. | 50 // Check if |next_message_| is already a complete MIDI message or not. |
51 if (!next_message_.empty()) { | 51 if (!next_message_.empty()) { |
52 const uint8 status_byte = next_message_.front(); | 52 const uint8_t status_byte = next_message_.front(); |
53 const size_t target_len = GetMidiMessageLength(status_byte); | 53 const size_t target_len = GetMidiMessageLength(status_byte); |
54 if (target_len == 0) { | 54 if (target_len == 0) { |
55 DCHECK_EQ(kSysEx, status_byte); | 55 DCHECK_EQ(kSysEx, status_byte); |
56 if (next_message_.back() == kEndOfSysEx) { | 56 if (next_message_.back() == kEndOfSysEx) { |
57 // OK, this is a complete SysEx message. | 57 // OK, this is a complete SysEx message. |
58 std::swap(*message, next_message_); | 58 std::swap(*message, next_message_); |
59 DCHECK(next_message_.empty()); | 59 DCHECK(next_message_.empty()); |
60 return; | 60 return; |
61 } | 61 } |
62 } else if (next_message_.size() == target_len) { | 62 } else if (next_message_.size() == target_len) { |
(...skipping 13 matching lines...) Expand all Loading... |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 if (queue_.empty()) | 79 if (queue_.empty()) |
80 return; | 80 return; |
81 | 81 |
82 // "System Real Time Messages" is a special MIDI message, which can appear | 82 // "System Real Time Messages" is a special MIDI message, which can appear |
83 // at an arbitrary byte position of MIDI stream. Here we reorder | 83 // at an arbitrary byte position of MIDI stream. Here we reorder |
84 // "System Real Time Messages" prior to |next_message_| so that each message | 84 // "System Real Time Messages" prior to |next_message_| so that each message |
85 // can be clearly separated as a complete MIDI message. | 85 // can be clearly separated as a complete MIDI message. |
86 const uint8 next = queue_.front(); | 86 const uint8_t next = queue_.front(); |
87 if (IsSystemRealTimeMessage(next)) { | 87 if (IsSystemRealTimeMessage(next)) { |
88 message->push_back(next); | 88 message->push_back(next); |
89 queue_.pop_front(); | 89 queue_.pop_front(); |
90 return; | 90 return; |
91 } | 91 } |
92 | 92 |
93 if (next_message_.empty()) { | 93 if (next_message_.empty()) { |
94 const size_t target_len = GetMidiMessageLength(next); | 94 const size_t target_len = GetMidiMessageLength(next); |
95 // If |target_len| is zero, it means either |next| is not a valid status | 95 // If |target_len| is zero, it means either |next| is not a valid status |
96 // byte or |next| is a valid status byte but the message length is | 96 // byte or |next| is a valid status byte but the message length is |
97 // unpredictable. For the latter case, only SysEx can be accepted. | 97 // unpredictable. For the latter case, only SysEx can be accepted. |
98 if (target_len > 0 || next == kSysEx) { | 98 if (target_len > 0 || next == kSysEx) { |
99 next_message_.push_back(next); | 99 next_message_.push_back(next); |
100 } | 100 } |
101 // Consume |next| always, since if |next| isn't status byte, which means | 101 // Consume |next| always, since if |next| isn't status byte, which means |
102 // that |next| is just corrupted data, or a data byte followed by | 102 // that |next| is just corrupted data, or a data byte followed by |
103 // reserved message, which we are unable to understand and deal with | 103 // reserved message, which we are unable to understand and deal with |
104 // anyway. | 104 // anyway. |
105 queue_.pop_front(); | 105 queue_.pop_front(); |
106 continue; | 106 continue; |
107 } | 107 } |
108 | 108 |
109 const uint8 status_byte = next_message_.front(); | 109 const uint8_t status_byte = next_message_.front(); |
110 | 110 |
111 // If we receive a new non-data byte before completing the pending message, | 111 // If we receive a new non-data byte before completing the pending message, |
112 // drop the pending message and respin the loop to re-evaluate |next|. | 112 // drop the pending message and respin the loop to re-evaluate |next|. |
113 // This also clears the running status byte speculatively added above, as | 113 // This also clears the running status byte speculatively added above, as |
114 // well as any broken incomplete messages. | 114 // well as any broken incomplete messages. |
115 if (!IsDataByte(next) && !(status_byte == kSysEx && next == kEndOfSysEx)) { | 115 if (!IsDataByte(next) && !(status_byte == kSysEx && next == kEndOfSysEx)) { |
116 next_message_.clear(); | 116 next_message_.clear(); |
117 continue; | 117 continue; |
118 } | 118 } |
119 | 119 |
120 // OK to consume this byte. | 120 // OK to consume this byte. |
121 next_message_.push_back(next); | 121 next_message_.push_back(next); |
122 queue_.pop_front(); | 122 queue_.pop_front(); |
123 } | 123 } |
124 } | 124 } |
125 | 125 |
126 } // namespace midi | 126 } // namespace midi |
127 } // namespace media | 127 } // namespace media |
OLD | NEW |