Bug 1201590 - WebMIDI Utility classes; r=baku,padenot

MozReview-Commit-ID: 8TAO1dcdK5E

--HG--
extra : rebase_source : 4e28bd94c5aec3e4cf9642a597b624a7fb14bd09
This commit is contained in:
Kyle Machulis 2017-11-15 11:16:34 -08:00
parent a95162b2c3
commit 0a96d6d23e
5 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1,36 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/MIDIPortInterface.h"
#include "mozilla/dom/MIDIPlatformService.h"
#include "mozilla/dom/MIDITypes.h"
MIDIPortInterface::MIDIPortInterface(const MIDIPortInfo& aPortInfo, bool aSysexEnabled) :
mId(aPortInfo.id()),
mName(aPortInfo.name()),
mManufacturer(aPortInfo.manufacturer()),
mVersion(aPortInfo.version()),
mSysexEnabled(aSysexEnabled),
mType((MIDIPortType)aPortInfo.type()),
// We'll never initialize a port object that's not connected
mDeviceState(MIDIPortDeviceState::Connected),
// Open everything on connection
mConnectionState(MIDIPortConnectionState::Open),
mShuttingDown(false)
{
}
MIDIPortInterface::~MIDIPortInterface()
{
Shutdown();
}
void
MIDIPortInterface::Shutdown()
{
mShuttingDown = true;
}

View File

@ -0,0 +1,51 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_MIDIPortInterface_h
#define mozilla_dom_MIDIPortInterface_h
#include "nsIObserver.h"
#include "mozilla/dom/MIDIPortBinding.h"
namespace mozilla {
namespace dom {
class MIDIPortInfo;
/**
* Base class for MIDIPort Parent/Child Actors. Makes sure both sides of the
* MIDIPort IPC connection need to a synchronized set of info/state.
*
*/
class MIDIPortInterface
{
public:
MIDIPortInterface(const MIDIPortInfo& aPortInfo, bool aSysexEnabled);
const nsString& Id() const { return mId; }
const nsString& Name() const { return mName; }
const nsString& Manufacturer() const { return mManufacturer; }
const nsString& Version() const { return mVersion; }
bool SysexEnabled() const { return mSysexEnabled; }
MIDIPortType Type() const { return mType; }
MIDIPortDeviceState DeviceState() const { return mDeviceState; }
MIDIPortConnectionState ConnectionState() const { return mConnectionState; }
bool IsShutdown() const { return mShuttingDown; }
virtual void Shutdown();
protected:
virtual ~MIDIPortInterface();
nsString mId;
nsString mName;
nsString mManufacturer;
nsString mVersion;
bool mSysexEnabled;
MIDIPortType mType;
MIDIPortDeviceState mDeviceState;
MIDIPortConnectionState mConnectionState;
bool mShuttingDown;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_MIDIPortInterface_h

131
dom/midi/MIDIUtils.cpp Normal file
View File

@ -0,0 +1,131 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/MIDITypes.h"
#include "mozilla/dom/MIDIUtils.h"
// Taken from MIDI IMPLEMENTATION CHART INSTRUCTIONS, MIDI Spec v1.0, Pg. 97
static const uint8_t kCommandByte = 0x80;
static const uint8_t kSysexMessageStart = 0xF0;
static const uint8_t kSystemMessage = 0xF0;
static const uint8_t kSysexMessageEnd = 0xF7;
static const uint8_t kSystemRealtimeMessage = 0xF8;
// Represents the length of all possible command messages.
// Taken from MIDI Spec, Pg. 101v 1.0, Table 2
static const uint8_t kCommandLengths[] = {3, 3, 3, 3, 2, 2, 3};
// Represents the length of all possible system messages. The length of sysex
// messages is variable, so we just put zero since it won't be checked anyways.
// Taken from MIDI Spec v1.0, Pg. 105, Table 5
static const uint8_t kSystemLengths[] = {0, 2, 3, 2, 1, 1, 1, 1};
namespace mozilla {
namespace dom {
namespace MIDIUtils {
// Checks validity of MIDIMessage passed to it. Throws debug warnings and
// returns false if message is not valid.
bool
IsValidMessage(const MIDIMessage* aMsg)
{
if (NS_WARN_IF(!aMsg)) {
return false;
}
// Assert on parser problems
MOZ_ASSERT(aMsg->data().Length() > 0,
"Created a MIDI Message of Length 0. This should never happen!");
uint8_t cmd = aMsg->data()[0];
// If first byte isn't a command, something is definitely wrong.
MOZ_ASSERT((cmd & kCommandByte) == kCommandByte,
"Constructed a MIDI packet where first byte is not command!");
if (cmd == kSysexMessageStart) {
// All we can do with sysex is make sure it starts and ends with the correct
// command bytes.
if (aMsg->data()[aMsg->data().Length() - 1] != kSysexMessageEnd) {
NS_WARNING("Last byte of Sysex Message not 0xF7!");
return false;
}
return true;
}
// For system realtime messages, the length should always be 1.
if ((cmd & kSystemRealtimeMessage) == kSystemRealtimeMessage) {
return aMsg->data().Length() == 1;
}
// Otherwise, just use the correct array for testing lengths. We can't tell
// much about message validity other than that.
if ((cmd & kSystemMessage) == kSystemMessage) {
if (cmd - kSystemMessage >= static_cast<uint8_t>(ArrayLength(kSystemLengths))) {
NS_WARNING("System Message Command byte not valid!");
return false;
}
return aMsg->data().Length() == kSystemLengths[cmd - kSystemMessage];
}
// For non system commands, we only care about differences in the high nibble
// of the first byte. Shift this down to give the index of the expected packet
// length.
uint8_t cmdIndex = (cmd - kCommandByte) >> 4;
if (cmdIndex >= ArrayLength(kCommandLengths)) {
// If our index is bigger than our array length, command byte is unknown;
NS_WARNING("Unknown MIDI command!");
return false;
}
return aMsg->data().Length() == kCommandLengths[cmdIndex];
}
uint32_t
ParseMessages(const nsTArray<uint8_t>& aByteBuffer,
const TimeStamp& aTimestamp,
nsTArray<MIDIMessage>& aMsgArray)
{
uint32_t bytesRead = 0;
bool inSysexMessage = false;
nsAutoPtr<MIDIMessage> currentMsg;
for (auto& byte : aByteBuffer) {
bytesRead++;
if ((byte & kSystemRealtimeMessage) == kSystemRealtimeMessage) {
MIDIMessage rt_msg;
rt_msg.data().AppendElement(byte);
rt_msg.timestamp() = aTimestamp;
aMsgArray.AppendElement(rt_msg);
continue;
}
if (byte == kSysexMessageEnd) {
if (!inSysexMessage) {
MOZ_ASSERT(inSysexMessage);
NS_WARNING("Got sysex message end with no sysex message being processed!");
}
inSysexMessage = false;
} else if (byte & kCommandByte) {
if (currentMsg && IsValidMessage(currentMsg)) {
aMsgArray.AppendElement(*currentMsg);
}
currentMsg = new MIDIMessage();
currentMsg->timestamp() = aTimestamp;
}
currentMsg->data().AppendElement(byte);
if (byte == kSysexMessageStart) {
inSysexMessage = true;
}
}
if (currentMsg && IsValidMessage(currentMsg)) {
aMsgArray.AppendElement(*currentMsg);
}
return bytesRead;
}
bool
IsSysexMessage(const MIDIMessage& aMsg)
{
if (aMsg.data().Length() == 0) {
return false;
}
if (aMsg.data()[0] == kSysexMessageStart) {
return true;
}
return false;
}
}
} // namespace dom
} // namespace mozilla

29
dom/midi/MIDIUtils.h Normal file
View File

@ -0,0 +1,29 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsTArray.h"
#include "mozilla/TimeStamp.h"
namespace mozilla {
namespace dom {
class MIDIMessage;
/**
* Set of utility functions for dealing with MIDI Messages.
*
*/
namespace MIDIUtils {
// Takes a nsTArray of bytes and parses it into zero or more MIDI messages.
// Returns number of bytes parsed.
uint32_t ParseMessages(const nsTArray<uint8_t>& aByteBuffer,
const TimeStamp& aTimestamp,
nsTArray<MIDIMessage>& aMsgArray);
// Returns true if a message is a sysex message.
bool IsSysexMessage(const MIDIMessage& a);
}
} // namespace dom
} // namespace mozilla

View File

@ -225,6 +225,9 @@ pref("dom.keyboardevent.dispatch_during_composition", false);
// cause inputting printable character.
pref("dom.keyboardevent.keypress.dispatch_non_printable_keys_only_system_group_in_content", false);
// Whether the WebMIDI API is enabled
pref("dom.webmidi.enabled", false);
// Whether to run add-on code in different compartments from browser code. This
// causes a separate compartment for each (addon, global) combination, which may
// significantly increase the number of compartments in the system.