Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1008)

Unified Diff: media/midi/midi_message_queue.h

Issue 68353002: Use MIDIMessageQueue/IsValidWebMIDIData for MIDI byte stream validation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use MIDIMessageQueue in MIDIHost::OnSendData as well as MIDIHost::ReceiveMIDIData Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/midi/midi_message_queue.h
diff --git a/media/midi/midi_message_queue.h b/media/midi/midi_message_queue.h
new file mode 100644
index 0000000000000000000000000000000000000000..14865d167915eb5ba9cc792d67bdabe5f386abb9
--- /dev/null
+++ b/media/midi/midi_message_queue.h
@@ -0,0 +1,74 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
Takashi Toyoshima 2013/11/19 01:08:37 Any reason to keep it having in not content/browse
yukawa 2013/11/19 09:41:41 Because it was originally written for media/midi/m
Takashi Toyoshima 2013/11/19 12:49:59 If you don't have a plan to use this file in media
yukawa 2013/11/19 14:49:31 Yeah, I'm planning to use it from media/midi/midi_
Takashi Toyoshima 2013/11/20 10:04:58 sgtm
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MEDIA_MIDI_MIDI_MESSAGE_QUEUE_H_
+#define MEDIA_MIDI_MIDI_MESSAGE_QUEUE_H_
+
+#include <deque>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "media/base/media_export.h"
+
+namespace media {
+
+// A simple message splitter for possibly unsafe MIDI data stream. This class
+// allows you to
+// - maintain fragmented MIDI message.
+// - skip any invalid data sequence.
+// - reorder MIDI messages so that "System Real Time Message", which can be
+// inserted at any point of the byte stream, is placed at the boundary of
+// complete MIDI messages.
+// - (Optional) reconstruct complete MIDI messages from data stream that is
+// compressed with "running status".
+//
+// Example (pseudo message loop):
+// MIDIMessageQueue queue(true); // true to support "running status"
+// while (true) {
+// if (is_incomming_midi_data_available()) {
+// std::vector<uint8> incomming_data;
+// read_incomming_midi_data(&incomming_data)
+// queue.Add(incomming_data);
+// }
+// while (true) {
+// std::vector<uint8> next_message;
+// queue.Get(&next_message);
+// if (!next_message.empty())
+// dispatch(next_message);
+// }
+// }
+class MEDIA_EXPORT MIDIMessageQueue {
+ public:
+ // Initializes the queue. Set true to |allow_running_status| to enable
+ // "MIDI running status" reconstruction.
+ explicit MIDIMessageQueue(bool allow_running_status);
+ ~MIDIMessageQueue();
+
+ // Enqueues |data| to the internal buffer.
+ void Add(const std::vector<uint8>& data);
+ // Enqueues |data| to the internal buffer. |data| must be valid until
+ // this method returns.
+ void Add(const uint8* data, size_t length);
+
+ // Fills the next complete MIDI message into |message|. If |message| is
+ // not empty, the data sequence falls into one of the following types of
+ // MIDI message.
+ // - Single "Channel Voice Message" (w/o "System Real Time Messages")
+ // - Single "Channel Mode Message" (w/o "System Real Time Messages")
+ // - Single "System Exclusive Message" (w/o "System Real Time Messages")
+ // - Single "System Common Message" (w/o "System Real Time Messages")
+ // - Single "System Real Time message"
+ // |message| is empty if there is no complete MIDI message any more.
+ void Get(std::vector<uint8>* message);
+
+ private:
+ std::deque<uint8> queue_;
+ std::vector<uint8> next_message_;
+ const bool allow_running_status_;
+ DISALLOW_COPY_AND_ASSIGN(MIDIMessageQueue);
+};
+
+} // namespace media
+
+#endif // MEDIA_MIDI_MIDI_MESSAGE_QUEUE_H_

Powered by Google App Engine
This is Rietveld 408576698