Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace media { | |
| 12 namespace { | |
| 13 | |
| 14 const uint8 kSysEx = 0xf0; | |
| 15 const uint8 kEndOfSysEx = 0xf7; | |
| 16 | |
| 17 bool IsDataByte(uint8 data) { | |
| 18 return data < 128; | |
|
Takashi Toyoshima
2013/11/19 01:45:31
I'm slightly prefer (data & 0x80) == 0 or somethin
yukawa
2013/11/19 09:41:41
Done.
| |
| 19 } | |
| 20 | |
| 21 bool IsFirstStatusByte(uint8 data) { | |
| 22 return !IsDataByte(data) && data != kEndOfSysEx; | |
| 23 } | |
| 24 | |
| 25 bool IsSystemRealTimeMessage(uint8 data) { | |
| 26 return 0xf8 <= data && data <= 0xff; | |
| 27 } | |
| 28 | |
| 29 size_t GetMessageLength(uint8 data) { | |
|
Takashi Toyoshima
2013/11/19 01:45:31
We may want to report some UMA for undefined messa
yukawa
2013/11/19 09:41:41
Hmm, I cannot imagine how we can utilize such UMA.
Takashi Toyoshima
2013/11/19 12:49:59
We can catch up unknown usage, or spec update for
| |
| 30 if (IsDataByte(data)) | |
| 31 return 0; | |
| 32 else if (0x80 <= data && data <= 0xbf) | |
| 33 return 3; | |
| 34 else if (0xc0 <= data && data <= 0xdf) | |
| 35 return 2; | |
| 36 else if (0xe0 <= data && data <= 0xf0) | |
|
Takashi Toyoshima
2013/11/19 01:45:31
latter condition should be data <= 0xef ?
yukawa
2013/11/19 09:41:41
Nice catch. Fixed.
| |
| 37 return 3; | |
| 38 else if (data == 0xf0) | |
| 39 return 0; | |
| 40 else if (data == 0xf1) | |
| 41 return 2; | |
| 42 else if (data == 0xf2) | |
| 43 return 3; | |
| 44 else if (data == 0xf3) | |
| 45 return 2; | |
| 46 else if (0xf4 <= data && data <= 0xf6) | |
| 47 return 1; | |
| 48 else if (data == 0xf7) | |
| 49 return 0; | |
| 50 else // 0xf8 <= data && data <= 0xff | |
| 51 return 1; | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 MIDIMessageQueue::MIDIMessageQueue(bool allow_running_status) | |
| 57 : allow_running_status_(allow_running_status) {} | |
| 58 | |
| 59 MIDIMessageQueue::~MIDIMessageQueue() {} | |
| 60 | |
| 61 void MIDIMessageQueue::Add(const std::vector<uint8>& data) { | |
| 62 queue_.insert(queue_.end(), data.begin(), data.end()); | |
| 63 } | |
| 64 | |
| 65 void MIDIMessageQueue::Add(const uint8* data, size_t length) { | |
| 66 queue_.insert(queue_.end(), data, data + length); | |
| 67 } | |
| 68 | |
| 69 void MIDIMessageQueue::Get(std::vector<uint8>* message) { | |
| 70 message->clear(); | |
| 71 | |
| 72 while (true) { | |
| 73 if (queue_.empty()) | |
| 74 return; | |
| 75 | |
| 76 const uint8 next = queue_.front(); | |
| 77 queue_.pop_front(); | |
| 78 | |
| 79 // "System Real Time Messages" is a special kind of MIDI messages, which can | |
| 80 // appear at arbitrary byte position of MIDI stream. Here we reorder | |
| 81 // "System Real Time Messages" prior to |next_message_| so that each message | |
| 82 // can be clearly separated as a complete MIDI message. | |
| 83 if (IsSystemRealTimeMessage(next)) { | |
| 84 message->push_back(next); | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 if (!next_message_.empty() && | |
| 89 ((next_message_[0] == kSysEx && IsFirstStatusByte(next)) || | |
| 90 (next_message_[0] != kSysEx && !IsDataByte(next)))) { | |
| 91 // Status byte appears. Clear the existing incomplete data. | |
|
Takashi Toyoshima
2013/11/19 12:49:59
Ah, I see. This comment "Status byte appears" is a
| |
| 92 next_message_.clear(); | |
| 93 } | |
| 94 | |
| 95 if (next_message_.empty()) { | |
| 96 if (IsFirstStatusByte(next)) { | |
| 97 next_message_.push_back(next); | |
| 98 } else { | |
| 99 // Invalid data. Ignore |next|. | |
|
Takashi Toyoshima
2013/11/19 01:45:31
Can you add some comment here, like "MIDI protocol
yukawa
2013/11/19 09:41:41
Done.
| |
| 100 } | |
| 101 continue; | |
| 102 } | |
| 103 | |
| 104 next_message_.push_back(next); | |
|
Takashi Toyoshima
2013/11/19 01:45:31
What happens if the status byte was set by allow_r
yukawa
2013/11/19 09:41:41
Yeah, this is a trick part. In such case, |next_me
Takashi Toyoshima
2013/11/19 12:49:59
I see. But this code has a state, and a little har
| |
| 105 | |
| 106 DCHECK(!next_message_.empty()); | |
|
Takashi Toyoshima
2013/11/19 01:45:31
this DCHECK looks too trivial.... but it's ok to k
yukawa
2013/11/19 09:41:41
Oops. Agreed.
| |
| 107 const uint8 status_byte = next_message_[0]; | |
| 108 if (status_byte == kSysEx) { | |
| 109 if (next == kEndOfSysEx) { | |
| 110 std::swap(*message, next_message_); | |
| 111 next_message_.clear(); | |
| 112 return; | |
| 113 } | |
| 114 continue; | |
| 115 } | |
| 116 | |
| 117 DCHECK(IsDataByte(next)); | |
|
Takashi Toyoshima
2013/11/19 01:45:31
This should not be DCHECK because as a commented a
yukawa
2013/11/19 09:41:41
See the trick condition at line 88-90.
| |
| 118 DCHECK_NE(kSysEx, status_byte); | |
| 119 const size_t target_len = GetMessageLength(status_byte); | |
| 120 if (next_message_.size() < target_len) | |
| 121 continue; | |
| 122 if (next_message_.size() == target_len) { | |
| 123 std::swap(*message, next_message_); | |
| 124 next_message_.clear(); | |
| 125 if (allow_running_status_) | |
| 126 next_message_.push_back(status_byte); | |
|
Takashi Toyoshima
2013/11/19 01:45:31
I think this code doesn't work.
yukawa
2013/11/19 09:41:41
See the trick condition at line 88-90.
| |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 NOTREACHED(); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 } // namespace media | |
| OLD | NEW |