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/. */
7 #include "MIDIMessageQueue.h"
8 #include "mozilla/dom/MIDITypes.h"
10 namespace mozilla::dom
{
12 MIDIMessageQueue::MIDIMessageQueue() : mMutex("MIDIMessageQueue::mMutex") {}
14 class MIDIMessageTimestampComparator
{
16 bool Equals(const MIDIMessage
& a
, const MIDIMessage
& b
) const {
17 return a
.timestamp() == b
.timestamp();
19 bool LessThan(const MIDIMessage
& a
, const MIDIMessage
& b
) const {
20 return a
.timestamp() < b
.timestamp();
24 void MIDIMessageQueue::Add(nsTArray
<MIDIMessage
>& aMsg
) {
25 MutexAutoLock
lock(mMutex
);
26 for (auto msg
: aMsg
) {
27 mMessageQueue
.InsertElementSorted(msg
, MIDIMessageTimestampComparator());
31 void MIDIMessageQueue::GetMessagesBefore(TimeStamp aTimestamp
,
32 nsTArray
<MIDIMessage
>& aMsgQueue
) {
33 MutexAutoLock
lock(mMutex
);
35 for (auto msg
: mMessageQueue
) {
36 if (aTimestamp
< msg
.timestamp()) {
39 aMsgQueue
.AppendElement(msg
);
43 mMessageQueue
.RemoveElementsAt(0, i
);
47 void MIDIMessageQueue::GetMessages(nsTArray
<MIDIMessage
>& aMsgQueue
) {
48 MutexAutoLock
lock(mMutex
);
49 aMsgQueue
.AppendElements(mMessageQueue
);
50 mMessageQueue
.Clear();
53 void MIDIMessageQueue::Clear() {
54 MutexAutoLock
lock(mMutex
);
55 mMessageQueue
.Clear();
58 void MIDIMessageQueue::ClearAfterNow() {
59 MutexAutoLock
lock(mMutex
);
60 TimeStamp now
= TimeStamp::Now();
62 for (auto msg
: mMessageQueue
) {
63 if (now
< msg
.timestamp()) {
69 mMessageQueue
.RemoveElementsAt(0, i
);
73 } // namespace mozilla::dom