summaryrefslogtreecommitdiffstats
path: root/dom/media/GraphRunner.cpp
blob: eef05c6fe3893c5638ee5d3807e74fe95942c341 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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 "GraphRunner.h"

#include "GraphDriver.h"
#include "MediaTrackGraph.h"
#include "MediaTrackGraphImpl.h"
#include "nsISupportsImpl.h"
#include "nsISupportsPriority.h"
#include "prthread.h"
#include "Tracing.h"
#include "audio_thread_priority.h"
#ifdef MOZ_WIDGET_ANDROID
#  include "AndroidProcess.h"
#endif  // MOZ_WIDGET_ANDROID

namespace mozilla {

GraphRunner::GraphRunner(MediaTrackGraphImpl* aGraph,
                         already_AddRefed<nsIThread> aThread)
    : Runnable("GraphRunner"),
      mMonitor("GraphRunner::mMonitor"),
      mGraph(aGraph),
      mThreadState(ThreadState::Wait),
      mThread(aThread) {
  mThread->Dispatch(do_AddRef(this));
}

GraphRunner::~GraphRunner() {
  MOZ_ASSERT(mThreadState == ThreadState::Shutdown);
}

/* static */
already_AddRefed<GraphRunner> GraphRunner::Create(MediaTrackGraphImpl* aGraph) {
  nsCOMPtr<nsIThread> thread;
  if (NS_WARN_IF(NS_FAILED(
          NS_NewNamedThread("GraphRunner", getter_AddRefs(thread))))) {
    return nullptr;
  }
  nsCOMPtr<nsISupportsPriority> supportsPriority = do_QueryInterface(thread);
  MOZ_ASSERT(supportsPriority);
  MOZ_ALWAYS_SUCCEEDS(
      supportsPriority->SetPriority(nsISupportsPriority::PRIORITY_HIGHEST));

  return do_AddRef(new GraphRunner(aGraph, thread.forget()));
}

void GraphRunner::Shutdown() {
  {
    MonitorAutoLock lock(mMonitor);
    MOZ_ASSERT(mThreadState == ThreadState::Wait);
    mThreadState = ThreadState::Shutdown;
    mMonitor.Notify();
  }
  mThread->Shutdown();
}

auto GraphRunner::OneIteration(GraphTime aStateTime, GraphTime aIterationEnd,
                               MixerCallbackReceiver* aMixerReceiver)
    -> IterationResult {
  TRACE("GraphRunner::OneIteration");

  MonitorAutoLock lock(mMonitor);
  MOZ_ASSERT(mThreadState == ThreadState::Wait);
  mIterationState =
      Some(IterationState(aStateTime, aIterationEnd, aMixerReceiver));

#ifdef DEBUG
  if (const auto* audioDriver =
          mGraph->CurrentDriver()->AsAudioCallbackDriver()) {
    mAudioDriverThreadId = audioDriver->ThreadId();
  } else if (const auto* clockDriver =
                 mGraph->CurrentDriver()->AsSystemClockDriver()) {
    mClockDriverThread = clockDriver->Thread();
  } else {
    MOZ_CRASH("Unknown GraphDriver");
  }
#endif
  // Signal that mIterationState was updated
  mThreadState = ThreadState::Run;
  mMonitor.Notify();
  // Wait for mIterationResult to update
  do {
    mMonitor.Wait();
  } while (mThreadState == ThreadState::Run);

#ifdef DEBUG
  mAudioDriverThreadId = std::thread::id();
  mClockDriverThread = nullptr;
#endif

  mIterationState = Nothing();

  IterationResult result = std::move(mIterationResult);
  mIterationResult = IterationResult();
  return result;
}

#ifdef MOZ_WIDGET_ANDROID
namespace {
void PromoteRenderingThreadAndroid() {
  MOZ_LOG(gMediaTrackGraphLog, LogLevel::Debug,
          ("GraphRunner default thread priority: %d",
           java::sdk::Process::GetThreadPriority(java::sdk::Process::MyTid())));
  java::sdk::Process::SetThreadPriority(
      java::sdk::Process::THREAD_PRIORITY_URGENT_AUDIO);
  MOZ_LOG(gMediaTrackGraphLog, LogLevel::Debug,
          ("GraphRunner promoted thread priority: %d",
           java::sdk::Process::GetThreadPriority(java::sdk::Process::MyTid())));
}
};      // namespace
#endif  // MOZ_WIDGET_ANDROID

NS_IMETHODIMP GraphRunner::Run() {
#ifndef XP_LINUX
  atp_handle* handle =
      atp_promote_current_thread_to_real_time(0, mGraph->GraphRate());
#endif

#ifdef MOZ_WIDGET_ANDROID
  PromoteRenderingThreadAndroid();
#endif  // MOZ_WIDGET_ANDROID

  nsCOMPtr<nsIThreadInternal> threadInternal = do_QueryInterface(mThread);
  threadInternal->SetObserver(mGraph);

  MonitorAutoLock lock(mMonitor);
  while (true) {
    while (mThreadState == ThreadState::Wait) {
      mMonitor.Wait();  // Wait for mIterationState to update or for shutdown
    }
    if (mThreadState == ThreadState::Shutdown) {
      break;
    }
    MOZ_DIAGNOSTIC_ASSERT(mIterationState.isSome());
    TRACE("GraphRunner::Run");
    mIterationResult = mGraph->OneIterationImpl(
        mIterationState->StateTime(), mIterationState->IterationEnd(),
        mIterationState->MixerReceiver());
    // Signal that mIterationResult was updated
    mThreadState = ThreadState::Wait;
    mMonitor.Notify();
  }

#ifndef XP_LINUX
  if (handle) {
    atp_demote_current_thread_from_real_time(handle);
  }
#endif

  return NS_OK;
}

bool GraphRunner::OnThread() const { return mThread->IsOnCurrentThread(); }

#ifdef DEBUG
bool GraphRunner::InDriverIteration(const GraphDriver* aDriver) const {
  if (!OnThread()) {
    return false;
  }

  if (const auto* audioDriver = aDriver->AsAudioCallbackDriver()) {
    return audioDriver->ThreadId() == mAudioDriverThreadId;
  }

  if (const auto* clockDriver = aDriver->AsSystemClockDriver()) {
    return clockDriver->Thread() == mClockDriverThread;
  }

  MOZ_CRASH("Unknown driver");
}
#endif

}  // namespace mozilla