1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_MIDIMessageQueue_h
8 #define mozilla_dom_MIDIMessageQueue_h
9 
10 #include "nsTArray.h"
11 
12 namespace mozilla {
13 namespace dom {
14 
15 class MIDIMessage;
16 
17 /**
18  * Since some MIDI Messages can be scheduled to be sent in the future, the
19  * MIDIMessageQueue is responsible for making sure all MIDI messages are
20  * scheduled and sent in order.
21  */
22 class MIDIMessageQueue {
23  public:
24   MIDIMessageQueue();
25   ~MIDIMessageQueue() = default;
26   // Adds an array of possibly out-of-order messages to our queue.
27   void Add(nsTArray<MIDIMessage>& aMsg);
28   // Retrieve all pending messages before the time specified.
29   void GetMessagesBefore(TimeStamp aTimestamp,
30                          nsTArray<MIDIMessage>& aMsgArray);
31   // Get all pending messages.
32   void GetMessages(nsTArray<MIDIMessage>& aMsgArray);
33   // Erase all pending messages.
34   void Clear();
35   // Erase all pending messages that would be sent in the future. Useful for
36   // when ports are closed, as we must send all messages with timestamps in the
37   // past.
38   void ClearAfterNow();
39 
40  private:
41   // Array of messages to be sent.
42   nsTArray<MIDIMessage> mMessageQueue;
43   // Mutex for coordinating cross thread array access.
44   Mutex mMutex;
45 };
46 
47 }  // namespace dom
48 }  // namespace mozilla
49 
50 #endif  // mozilla_dom_MIDIMessageQueue_h
51