AudioWorkletGlobalScope.h (3243B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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_AudioWorkletGlobalScope_h 8 #define mozilla_dom_AudioWorkletGlobalScope_h 9 10 #include "js/ForOfIterator.h" 11 #include "mozilla/dom/AudioParamDescriptorMap.h" 12 #include "mozilla/dom/FunctionBinding.h" 13 #include "mozilla/dom/WorkletGlobalScope.h" 14 #include "nsRefPtrHashtable.h" 15 16 namespace mozilla { 17 18 class AudioWorkletImpl; 19 20 namespace dom { 21 22 class AudioWorkletProcessorConstructor; 23 class MessagePort; 24 class StructuredCloneHolder; 25 class UniqueMessagePortId; 26 27 class AudioWorkletGlobalScope final : public WorkletGlobalScope { 28 public: 29 NS_DECL_ISUPPORTS_INHERITED 30 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioWorkletGlobalScope, 31 WorkletGlobalScope); 32 33 explicit AudioWorkletGlobalScope(AudioWorkletImpl* aImpl); 34 35 bool WrapGlobalObject(JSContext* aCx, 36 JS::MutableHandle<JSObject*> aReflector) override; 37 38 void RegisterProcessor(JSContext* aCx, const nsAString& aName, 39 AudioWorkletProcessorConstructor& aProcessorCtor, 40 ErrorResult& aRv); 41 42 AudioWorkletImpl* Impl() const; 43 44 uint64_t CurrentFrame() const; 45 46 double CurrentTime() const; 47 48 float SampleRate() const; 49 50 MessagePort* Port() const { return mPort; }; 51 52 void SetPort(MessagePort* aPort) { mPort = aPort; } 53 54 // If successful, returns true and sets aRetProcessor, which will be in the 55 // compartment for the realm of this global. Returns false on failure. 56 MOZ_CAN_RUN_SCRIPT 57 bool ConstructProcessor(JSContext* aCx, const nsAString& aName, 58 NotNull<StructuredCloneHolder*> aSerializedOptions, 59 UniqueMessagePortId& aPortIdentifier, 60 JS::MutableHandle<JSObject*> aRetProcessor); 61 62 // Returns null if not called during ConstructProcessor() or if the port has 63 // already been taken. 64 RefPtr<MessagePort> TakePortForProcessorCtor(); 65 66 private: 67 ~AudioWorkletGlobalScope() = default; 68 69 // Returns an AudioParamDescriptorMap filled with AudioParamDescriptor 70 // objects, extracted from JS. Returns an empty map in case of error and set 71 // aRv accordingly. 72 AudioParamDescriptorMap DescriptorsFromJS(JSContext* aCx, 73 JS::ForOfIterator* aIter, 74 ErrorResult& aRv); 75 76 typedef nsRefPtrHashtable<nsStringHashKey, AudioWorkletProcessorConstructor> 77 NodeNameToProcessorDefinitionMap; 78 NodeNameToProcessorDefinitionMap mNameToProcessorMap; 79 // https://webaudio.github.io/web-audio-api/#pending-processor-construction-data-transferred-port 80 // This does not need to be traversed during cycle-collection because it is 81 // only set while this AudioWorkletGlobalScope is on the stack. 82 RefPtr<MessagePort> mPortForProcessor; 83 84 RefPtr<MessagePort> mPort; 85 }; 86 87 } // namespace dom 88 } // namespace mozilla 89 90 #endif // mozilla_dom_AudioWorkletGlobalScope_h