summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/jsapi/RTCTransformEventRunnable.cpp
blob: 6f41baf80ff5a652849b5fb888ed7c4eafdd6967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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 https://mozilla.org/MPL/2.0/. */

#include "RTCTransformEventRunnable.h"

#include "nsIGlobalObject.h"
#include "ErrorList.h"
#include "nsError.h"
#include "nsDebug.h"
#include "nsLiteralString.h"
#include "mozilla/RefPtr.h"
#include "mozilla/AlreadyAddRefed.h"
// This needs to come before RTCTransformEvent.h, since webidl codegen doesn't
// include-what-you-use or forward declare.
#include "mozilla/dom/RTCRtpScriptTransformer.h"
#include "mozilla/dom/RTCTransformEvent.h"
#include "mozilla/dom/RTCTransformEventBinding.h"
#include "mozilla/dom/EventWithOptionsRunnable.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/EventTarget.h"
#include "mozilla/dom/RootedDictionary.h"
#include "js/RootingAPI.h"
#include "js/Value.h"
#include "libwebrtcglue/FrameTransformerProxy.h"

namespace mozilla::dom {

RTCTransformEventRunnable::RTCTransformEventRunnable(
    Worker& aWorker, FrameTransformerProxy* aProxy)
    : EventWithOptionsRunnable(aWorker, "RTCTransformEventRunnable"),
      mProxy(aProxy) {}

RTCTransformEventRunnable::~RTCTransformEventRunnable() = default;

already_AddRefed<Event> RTCTransformEventRunnable::BuildEvent(
    JSContext* aCx, nsIGlobalObject* aGlobal, EventTarget* aTarget,
    JS::Handle<JS::Value> aTransformerOptions) {
  // Let transformerOptions be the result of
  // StructuredDeserialize(serializedOptions, the current Realm).

  // NOTE: We do not do this streams stuff. Spec will likely change here.
  // The gist here is that we hook [[readable]] and [[writable]] up to the frame
  // source/sink, which in out case is FrameTransformerProxy.
  // Let readable be the result of StructuredDeserialize(serializedReadable, the
  // current Realm). Let writable be the result of
  // StructuredDeserialize(serializedWritable, the current Realm).

  // Let transformer be a new RTCRtpScriptTransformer.

  // Set transformer.[[options]] to transformerOptions.

  // Set transformer.[[readable]] to readable.

  // Set transformer.[[writable]] to writable.
  RefPtr<RTCRtpScriptTransformer> transformer =
      new RTCRtpScriptTransformer(aGlobal);
  nsresult nrv =
      transformer->Init(aCx, aTransformerOptions, mWorkerPrivate, mProxy);
  if (NS_WARN_IF(NS_FAILED(nrv))) {
    // TODO: Error handling. Currently unspecified.
    return nullptr;
  }

  // Fire an event named rtctransform using RTCTransformEvent with transformer
  // set to transformer on worker’s global scope.
  RootedDictionary<RTCTransformEventInit> init(aCx);
  init.mBubbles = false;
  init.mCancelable = false;
  init.mTransformer = transformer;

  RefPtr<RTCTransformEvent> event =
      RTCTransformEvent::Constructor(aTarget, u"rtctransform"_ns, init);
  event->SetTrusted(true);
  return event.forget();
}

}  // namespace mozilla::dom