midirMIDIPlatformService.h (2418B)
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_midirMIDIPlatformService_h 8 #define mozilla_dom_midirMIDIPlatformService_h 9 10 #include "mozilla/StaticMutex.h" 11 #include "mozilla/dom/MIDIPlatformService.h" 12 #include "mozilla/dom/MIDITypes.h" 13 #include "mozilla/dom/midi/midir_impl_ffi_generated.h" 14 15 class nsIThread; 16 struct MidirWrapper; 17 18 namespace mozilla::dom { 19 20 class MIDIPortInterface; 21 22 /** 23 * Platform service implementation using the midir crate. 24 */ 25 class midirMIDIPlatformService : public MIDIPlatformService { 26 public: 27 midirMIDIPlatformService(); 28 virtual void Init() override; 29 virtual void Refresh() override; 30 virtual void Open(MIDIPortParent* aPort) override; 31 virtual void Stop() override; 32 virtual void ScheduleSend(const nsAString& aPort) override; 33 virtual void ScheduleClose(MIDIPortParent* aPort) override; 34 35 void SendMessage(const nsAString& aPort, const MIDIMessage& aMessage); 36 37 private: 38 virtual ~midirMIDIPlatformService(); 39 40 static void AddPort(const nsString* aId, const nsString* aName, bool aInput); 41 static void RemovePort(const nsString* aId, const nsString* aName, 42 bool aInput); 43 static void CheckAndReceive(const nsString* aId, const uint8_t* aData, 44 size_t aLength, const GeckoTimeStamp* aTimeStamp, 45 uint64_t aMicros); 46 47 // Wrapper around the midir Rust implementation. 48 MidirWrapper* mImplementation; 49 50 // The midir backends can invoke CheckAndReceive on arbitrary background 51 // threads, and so we dispatch events from there to the owner task queue. 52 // It's a little ambiguous whether midir can ever invoke CheckAndReceive 53 // on one of its platform-specific background threads after we've dropped 54 // the main instance. Just in case, we use a static mutex to avoid a potential 55 // race with dropping the primary reference to the task queue via 56 // ClearOnShutdown. 57 static StaticMutex gOwnerThreadMutex; 58 static nsCOMPtr<nsISerialEventTarget> gOwnerThread 59 MOZ_GUARDED_BY(gOwnerThreadMutex); 60 }; 61 62 } // namespace mozilla::dom 63 64 #endif // mozilla_dom_midirMIDIPlatformService_h