2021-12-21 14:34:52 +03:00
|
|
|
/* 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 "midirMIDIPlatformService.h"
|
|
|
|
#include "mozilla/StaticMutex.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/dom/MIDIPort.h"
|
|
|
|
#include "mozilla/dom/MIDITypes.h"
|
|
|
|
#include "mozilla/dom/MIDIPortInterface.h"
|
|
|
|
#include "mozilla/dom/MIDIPortParent.h"
|
|
|
|
#include "mozilla/dom/MIDIPlatformRunnables.h"
|
|
|
|
#include "mozilla/dom/MIDIUtils.h"
|
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "nsIThread.h"
|
2022-01-18 12:54:42 +03:00
|
|
|
#include "mozilla/Logging.h"
|
|
|
|
#include "MIDILog.h"
|
2021-12-21 14:34:52 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
static_assert(sizeof(TimeStamp) == sizeof(GeckoTimeStamp));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runnable used for to send messages asynchronously on the I/O thread.
|
|
|
|
*/
|
|
|
|
class SendRunnable : public MIDIBackgroundRunnable {
|
|
|
|
public:
|
2022-02-10 11:36:02 +03:00
|
|
|
explicit SendRunnable(const nsAString& aPortID, const MIDIMessage& aMessage)
|
|
|
|
: MIDIBackgroundRunnable("SendRunnable"),
|
|
|
|
mPortID(aPortID),
|
|
|
|
mMessage(aMessage) {}
|
2021-12-21 14:34:52 +03:00
|
|
|
~SendRunnable() = default;
|
|
|
|
virtual void RunInternal() {
|
|
|
|
AssertIsOnBackgroundThread();
|
2022-02-15 11:39:30 +03:00
|
|
|
if (!MIDIPlatformService::IsRunning()) {
|
|
|
|
// Some send operations might outlive the service, bail out and do nothing
|
|
|
|
return;
|
|
|
|
}
|
2021-12-21 14:34:52 +03:00
|
|
|
midirMIDIPlatformService* srv =
|
|
|
|
static_cast<midirMIDIPlatformService*>(MIDIPlatformService::Get());
|
2022-02-10 11:36:02 +03:00
|
|
|
srv->SendMessage(mPortID, mMessage);
|
2021-12-21 14:34:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mPortID;
|
2022-02-10 11:36:02 +03:00
|
|
|
MIDIMessage mMessage;
|
2021-12-21 14:34:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// static
|
|
|
|
StaticMutex midirMIDIPlatformService::gBackgroundThreadMutex;
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsCOMPtr<nsIThread> midirMIDIPlatformService::gBackgroundThread;
|
|
|
|
|
|
|
|
midirMIDIPlatformService::midirMIDIPlatformService()
|
2022-01-19 09:58:16 +03:00
|
|
|
: mImplementation(nullptr) {
|
2021-12-21 14:34:52 +03:00
|
|
|
StaticMutexAutoLock lock(gBackgroundThreadMutex);
|
|
|
|
gBackgroundThread = NS_GetCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
midirMIDIPlatformService::~midirMIDIPlatformService() {
|
2022-01-18 12:54:42 +03:00
|
|
|
LOG("midir_impl_shutdown");
|
2022-02-15 11:39:30 +03:00
|
|
|
if (mImplementation) {
|
|
|
|
midir_impl_shutdown(mImplementation);
|
|
|
|
}
|
2021-12-21 14:34:52 +03:00
|
|
|
StaticMutexAutoLock lock(gBackgroundThreadMutex);
|
|
|
|
gBackgroundThread = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void midirMIDIPlatformService::AddPort(const nsString* aId,
|
|
|
|
const nsString* aName, bool aInput) {
|
|
|
|
MIDIPortType type = aInput ? MIDIPortType::Input : MIDIPortType::Output;
|
|
|
|
MIDIPortInfo port(*aId, *aName, u""_ns, u""_ns, static_cast<uint32_t>(type));
|
|
|
|
MIDIPlatformService::Get()->AddPortInfo(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void midirMIDIPlatformService::Init() {
|
2022-01-19 09:58:16 +03:00
|
|
|
if (mImplementation) {
|
2021-12-21 14:34:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-19 09:58:16 +03:00
|
|
|
mImplementation = midir_impl_init(AddPort);
|
2021-12-21 14:34:52 +03:00
|
|
|
|
|
|
|
if (mImplementation) {
|
|
|
|
MIDIPlatformService::Get()->SendPortList();
|
2022-01-18 12:54:42 +03:00
|
|
|
} else {
|
|
|
|
LOG("midir_impl_init failure");
|
2021-12-21 14:34:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void midirMIDIPlatformService::CheckAndReceive(const nsString* aId,
|
|
|
|
const uint8_t* aData,
|
|
|
|
size_t aLength,
|
|
|
|
const GeckoTimeStamp* aTimeStamp,
|
|
|
|
uint64_t aMicros) {
|
|
|
|
nsTArray<uint8_t> data;
|
|
|
|
for (size_t i = 0; i < aLength; i++) {
|
|
|
|
data.AppendElement(aData[i]);
|
|
|
|
}
|
|
|
|
const TimeStamp* openTime = reinterpret_cast<const TimeStamp*>(aTimeStamp);
|
|
|
|
TimeStamp timestamp =
|
|
|
|
*openTime + TimeDuration::FromMicroseconds(static_cast<double>(aMicros));
|
|
|
|
MIDIMessage message(data, timestamp);
|
2022-02-10 11:36:02 +03:00
|
|
|
LogMIDIMessage(message, *aId, MIDIPortType::Input);
|
2021-12-21 14:34:52 +03:00
|
|
|
nsTArray<MIDIMessage> messages;
|
|
|
|
messages.AppendElement(message);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r(new ReceiveRunnable(*aId, messages));
|
|
|
|
StaticMutexAutoLock lock(gBackgroundThreadMutex);
|
|
|
|
if (gBackgroundThread) {
|
|
|
|
gBackgroundThread->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void midirMIDIPlatformService::Open(MIDIPortParent* aPort) {
|
|
|
|
MOZ_ASSERT(aPort);
|
|
|
|
nsString id = aPort->MIDIPortInterface::Id();
|
|
|
|
TimeStamp openTimeStamp = TimeStamp::Now();
|
|
|
|
if (midir_impl_open_port(mImplementation, &id,
|
|
|
|
reinterpret_cast<GeckoTimeStamp*>(&openTimeStamp),
|
|
|
|
CheckAndReceive)) {
|
2022-01-18 12:54:42 +03:00
|
|
|
LOG("MIDI port open: %s at t=%lf", NS_ConvertUTF16toUTF8(id).get(),
|
|
|
|
(openTimeStamp - TimeStamp::ProcessCreation()).ToSeconds());
|
2021-12-21 14:34:52 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r(new SetStatusRunnable(
|
2022-01-19 09:58:17 +03:00
|
|
|
aPort, aPort->DeviceState(), MIDIPortConnectionState::Open));
|
2021-12-21 14:34:52 +03:00
|
|
|
NS_DispatchToCurrentThread(r);
|
2022-01-18 12:54:42 +03:00
|
|
|
} else {
|
2022-01-19 09:58:16 +03:00
|
|
|
LOG("MIDI port open failed: %s", NS_ConvertUTF16toUTF8(id).get());
|
2021-12-21 14:34:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void midirMIDIPlatformService::Stop() {
|
|
|
|
// Nothing to do here AFAIK
|
|
|
|
}
|
|
|
|
|
|
|
|
void midirMIDIPlatformService::ScheduleSend(const nsAString& aPortId) {
|
2022-02-10 11:36:02 +03:00
|
|
|
LOG("MIDI port schedule send %s", NS_ConvertUTF16toUTF8(aPortId).get());
|
|
|
|
nsTArray<MIDIMessage> messages;
|
|
|
|
GetMessages(aPortId, messages);
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
for (const auto& message : messages) {
|
|
|
|
if (message.timestamp().IsNull()) {
|
|
|
|
SendMessage(aPortId, message);
|
|
|
|
} else {
|
|
|
|
double delay = (message.timestamp() - now).ToMilliseconds();
|
|
|
|
if (delay < 1.0) {
|
|
|
|
SendMessage(aPortId, message);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> r(new SendRunnable(aPortId, message));
|
|
|
|
NS_DelayedDispatchToCurrentThread(r.forget(),
|
|
|
|
static_cast<uint32_t>(delay));
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 14:34:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void midirMIDIPlatformService::ScheduleClose(MIDIPortParent* aPort) {
|
|
|
|
MOZ_ASSERT(aPort);
|
|
|
|
nsString id = aPort->MIDIPortInterface::Id();
|
2022-01-18 12:54:42 +03:00
|
|
|
LOG("MIDI port schedule close %s", NS_ConvertUTF16toUTF8(id).get());
|
2021-12-21 14:34:52 +03:00
|
|
|
if (aPort->ConnectionState() == MIDIPortConnectionState::Open) {
|
|
|
|
midir_impl_close_port(mImplementation, &id);
|
|
|
|
nsCOMPtr<nsIRunnable> r(new SetStatusRunnable(
|
2022-01-19 09:58:17 +03:00
|
|
|
aPort, aPort->DeviceState(), MIDIPortConnectionState::Closed));
|
2021-12-21 14:34:52 +03:00
|
|
|
NS_DispatchToCurrentThread(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:36:02 +03:00
|
|
|
void midirMIDIPlatformService::SendMessage(const nsAString& aPortId,
|
|
|
|
const MIDIMessage& aMessage) {
|
|
|
|
LOG("MIDI send message on %s", NS_ConvertUTF16toUTF8(aPortId).get());
|
|
|
|
LogMIDIMessage(aMessage, aPortId, MIDIPortType::Output);
|
|
|
|
midir_impl_send(mImplementation, &aPortId, &aMessage.data());
|
2021-12-21 14:34:52 +03:00
|
|
|
}
|