RTCRtpScriptTransform.cpp (3600B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=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 https://mozilla.org/MPL/2.0/. */ 6 7 #include "RTCRtpScriptTransform.h" 8 9 #include "ErrorList.h" 10 #include "js/RootingAPI.h" 11 #include "jsapi/RTCTransformEventRunnable.h" 12 #include "libwebrtcglue/FrameTransformerProxy.h" 13 #include "mozilla/AlreadyAddRefed.h" 14 #include "mozilla/Logging.h" 15 #include "mozilla/RefPtr.h" 16 #include "mozilla/dom/MessagePortBinding.h" 17 #include "mozilla/dom/Promise.h" 18 #include "mozilla/dom/RTCRtpScriptTransformBinding.h" 19 #include "mozilla/dom/Worker.h" 20 #include "nsCOMPtr.h" 21 #include "nsContentUtils.h" 22 #include "nsCycleCollectionParticipant.h" 23 #include "nsDebug.h" 24 #include "nsISupports.h" 25 #include "nsPIDOMWindow.h" 26 #include "nsWrapperCache.h" 27 28 namespace mozilla::dom { 29 30 LazyLogModule gScriptTransformLog("RTCRtpScriptTransform"); 31 32 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(RTCRtpScriptTransform, mWindow) 33 NS_IMPL_CYCLE_COLLECTING_ADDREF(RTCRtpScriptTransform) 34 NS_IMPL_CYCLE_COLLECTING_RELEASE(RTCRtpScriptTransform) 35 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RTCRtpScriptTransform) 36 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 37 NS_INTERFACE_MAP_ENTRY(nsISupports) 38 NS_INTERFACE_MAP_END 39 40 already_AddRefed<RTCRtpScriptTransform> RTCRtpScriptTransform::Constructor( 41 const GlobalObject& aGlobal, Worker& aWorker, 42 JS::Handle<JS::Value> aOptions, 43 const Optional<Sequence<JSObject*>>& aTransfer, ErrorResult& aRv) { 44 nsCOMPtr<nsPIDOMWindowInner> ownerWindow = 45 do_QueryInterface(aGlobal.GetAsSupports()); 46 if (NS_WARN_IF(!ownerWindow)) { 47 aRv.Throw(NS_ERROR_FAILURE); 48 return nullptr; 49 } 50 51 // The spec currently fails to describe what to do when the worker is closing 52 // or closed; the following placeholder text can be found in the spec at: 53 // https://w3c.github.io/webrtc-encoded-transform/#dom-rtcrtpscripttransform-rtcrtpscripttransform 54 // 55 // > FIXME: Describe error handling (worker closing flag true at 56 // > RTCRtpScriptTransform creation time. And worker being terminated while 57 // > transform is processing data). 58 // 59 // Because our worker runnables do not like to be pointed at a nonexistant 60 // worker, we throw in this case. 61 if (!aWorker.IsEligibleForMessaging()) { 62 aRv.Throw(NS_ERROR_FAILURE); 63 return nullptr; 64 } 65 66 auto newTransform = MakeRefPtr<RTCRtpScriptTransform>(ownerWindow); 67 RefPtr<RTCTransformEventRunnable> runnable = 68 new RTCTransformEventRunnable(aWorker, &newTransform->GetProxy()); 69 70 if (aTransfer.WasPassed()) { 71 aWorker.PostEventWithOptions(aGlobal.Context(), aOptions, aTransfer.Value(), 72 runnable, aRv); 73 } else { 74 StructuredSerializeOptions transferOptions; 75 aWorker.PostEventWithOptions(aGlobal.Context(), aOptions, 76 transferOptions.mTransfer, runnable, aRv); 77 } 78 79 if (NS_WARN_IF(aRv.Failed())) { 80 return nullptr; 81 } 82 83 return newTransform.forget(); 84 } 85 86 RTCRtpScriptTransform::RTCRtpScriptTransform(nsPIDOMWindowInner* aWindow) 87 : mWindow(aWindow), mProxy(new FrameTransformerProxy) {} 88 89 RTCRtpScriptTransform::~RTCRtpScriptTransform() { 90 mProxy->ReleaseScriptTransformer(); 91 } 92 93 JSObject* RTCRtpScriptTransform::WrapObject(JSContext* aCx, 94 JS::Handle<JSObject*> aGivenProto) { 95 return RTCRtpScriptTransform_Binding::Wrap(aCx, this, aGivenProto); 96 } 97 98 } // namespace mozilla::dom 99 100 #undef LOGTAG