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 "mozilla/dom/MIDIOutput.h"
8 #include "mozilla/dom/MIDIPortChild.h"
9 #include "mozilla/dom/MIDITypes.h"
10 #include "mozilla/dom/MIDIOutputBinding.h"
11 #include "mozilla/dom/MIDIUtils.h"
12 #include "nsDOMNavigationTiming.h"
13 #include "mozilla/ErrorResult.h"
14 #include "mozilla/TimeStamp.h"
15 #include "mozilla/dom/Document.h"
16 #include "mozilla/dom/Performance.h"
18 using namespace mozilla
;
19 using namespace mozilla::dom
;
21 MIDIOutput::MIDIOutput(nsPIDOMWindowInner
* aWindow
,
22 MIDIAccess
* aMIDIAccessParent
)
23 : MIDIPort(aWindow
, aMIDIAccessParent
) {}
26 MIDIOutput
* MIDIOutput::Create(nsPIDOMWindowInner
* aWindow
,
27 MIDIAccess
* aMIDIAccessParent
,
28 const MIDIPortInfo
& aPortInfo
,
29 const bool aSysexEnabled
) {
30 MOZ_ASSERT(static_cast<MIDIPortType
>(aPortInfo
.type()) ==
31 MIDIPortType::Output
);
32 auto* port
= new MIDIOutput(aWindow
, aMIDIAccessParent
);
33 if (NS_WARN_IF(!port
->Initialize(aPortInfo
, aSysexEnabled
))) {
39 JSObject
* MIDIOutput::WrapObject(JSContext
* aCx
,
40 JS::Handle
<JSObject
*> aGivenProto
) {
41 return MIDIOutput_Binding::Wrap(aCx
, this, aGivenProto
);
44 void MIDIOutput::Send(const Sequence
<uint8_t>& aData
,
45 const Optional
<double>& aTimestamp
, ErrorResult
& aRv
) {
46 if (mPort
->DeviceState() == MIDIPortDeviceState::Disconnected
) {
47 aRv
.Throw(NS_ERROR_DOM_INVALID_STATE_ERR
);
50 // The timestamp passed to us is a DOMHighResTimestamp, which is in relation
51 // to the start of navigation timing. This needs to be turned into a
52 // TimeStamp before it hits the platform specific MIDI service.
54 // If timestamp is either not set or zero, set timestamp to now and send the
57 if (aTimestamp
.WasPassed() && aTimestamp
.Value() != 0) {
58 nsCOMPtr
<Document
> doc
= GetOwner()->GetDoc();
60 aRv
.Throw(NS_ERROR_DOM_INVALID_STATE_ERR
);
63 TimeDuration ts_diff
= TimeDuration::FromMilliseconds(aTimestamp
.Value());
64 timestamp
= GetOwner()
67 ->GetNavigationStartTimeStamp() +
70 timestamp
= TimeStamp::Now();
73 nsTArray
<MIDIMessage
> msgArray
;
74 bool ret
= MIDIUtils::ParseMessages(aData
, timestamp
, msgArray
);
76 aRv
.ThrowTypeError("Invalid MIDI message");
80 if (msgArray
.IsEmpty()) {
81 aRv
.ThrowTypeError("Empty message array");
85 // TODO Move this check back to parse message so we don't have to iterate
87 if (!SysexEnabled()) {
88 for (auto& msg
: msgArray
) {
89 if (MIDIUtils::IsSysexMessage(msg
)) {
90 aRv
.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR
);
95 mPort
->SendSend(msgArray
);
98 void MIDIOutput::Clear() {
99 if (mPort
->ConnectionState() == MIDIPortConnectionState::Closed
) {