summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/libwebrtcglue/CallWorkerThread.h
blob: 12d21fbee4fdb448a7ce4ddee9a50b5fe3b9cbb5 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */

#ifndef DOM_MEDIA_WEBRTC_LIBWEBRTCGLUE_CALLWORKERTHREAD_H_
#define DOM_MEDIA_WEBRTC_LIBWEBRTCGLUE_CALLWORKERTHREAD_H_

#include "mozilla/AbstractThread.h"
#include "nsIDirectTaskDispatcher.h"
#include "TaskQueueWrapper.h"

namespace mozilla {

// Implements AbstractThread for running things on the webrtc TaskQueue.
// Webrtc TaskQueues are not refcounted so cannot implement AbstractThread
// directly.
class CallWorkerThread final : public AbstractThread,
                               public nsIDirectTaskDispatcher {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSIDIRECTTASKDISPATCHER

  explicit CallWorkerThread(
      UniquePtr<TaskQueueWrapper<DeletionPolicy::NonBlocking>> aWebrtcTaskQueue)
      : AbstractThread(aWebrtcTaskQueue->mTaskQueue->SupportsTailDispatch()),
        mWebrtcTaskQueue(std::move(aWebrtcTaskQueue)) {}

  // AbstractThread overrides
  nsresult Dispatch(already_AddRefed<nsIRunnable> aRunnable,
                    DispatchReason aReason) override;
  bool IsCurrentThreadIn() const override;
  TaskDispatcher& TailDispatcher() override;
  nsIEventTarget* AsEventTarget() override;
  NS_IMETHOD
  DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
                  uint32_t aDelayMs) override;

  NS_IMETHOD RegisterShutdownTask(nsITargetShutdownTask* aTask) override;
  NS_IMETHOD UnregisterShutdownTask(nsITargetShutdownTask* aTask) override;

  const UniquePtr<TaskQueueWrapper<DeletionPolicy::NonBlocking>>
      mWebrtcTaskQueue;

 protected:
  ~CallWorkerThread() = default;
};

NS_IMPL_ISUPPORTS(CallWorkerThread, nsIDirectTaskDispatcher,
                  nsISerialEventTarget, nsIEventTarget);

//-----------------------------------------------------------------------------
// AbstractThread
//-----------------------------------------------------------------------------

nsresult CallWorkerThread::Dispatch(already_AddRefed<nsIRunnable> aRunnable,
                                    DispatchReason aReason) {
  RefPtr<nsIRunnable> runnable = aRunnable;
  return mWebrtcTaskQueue->mTaskQueue->Dispatch(
      mWebrtcTaskQueue->CreateTaskRunner(std::move(runnable)), aReason);
}

bool CallWorkerThread::IsCurrentThreadIn() const {
  return mWebrtcTaskQueue->mTaskQueue->IsOnCurrentThreadInfallible() &&
         mWebrtcTaskQueue->IsCurrent();
}

TaskDispatcher& CallWorkerThread::TailDispatcher() {
  return mWebrtcTaskQueue->mTaskQueue->TailDispatcher();
}

nsIEventTarget* CallWorkerThread::AsEventTarget() {
  return mWebrtcTaskQueue->mTaskQueue->AsEventTarget();
}

NS_IMETHODIMP
CallWorkerThread::DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
                                  uint32_t aDelayMs) {
  RefPtr<nsIRunnable> event = aEvent;
  return mWebrtcTaskQueue->mTaskQueue->DelayedDispatch(
      mWebrtcTaskQueue->CreateTaskRunner(std::move(event)), aDelayMs);
}

NS_IMETHODIMP CallWorkerThread::RegisterShutdownTask(
    nsITargetShutdownTask* aTask) {
  return mWebrtcTaskQueue->mTaskQueue->RegisterShutdownTask(aTask);
}

NS_IMETHODIMP CallWorkerThread::UnregisterShutdownTask(
    nsITargetShutdownTask* aTask) {
  return mWebrtcTaskQueue->mTaskQueue->UnregisterShutdownTask(aTask);
}

//-----------------------------------------------------------------------------
// nsIDirectTaskDispatcher
//-----------------------------------------------------------------------------

NS_IMETHODIMP
CallWorkerThread::DispatchDirectTask(already_AddRefed<nsIRunnable> aEvent) {
  nsCOMPtr<nsIRunnable> event = aEvent;
  return mWebrtcTaskQueue->mTaskQueue->DispatchDirectTask(
      mWebrtcTaskQueue->CreateTaskRunner(std::move(event)));
}

NS_IMETHODIMP CallWorkerThread::DrainDirectTasks() {
  return mWebrtcTaskQueue->mTaskQueue->DrainDirectTasks();
}

NS_IMETHODIMP CallWorkerThread::HaveDirectTasks(bool* aValue) {
  return mWebrtcTaskQueue->mTaskQueue->HaveDirectTasks(aValue);
}

}  // namespace mozilla

#endif