2019-03-06 20:12:25 +00:00
|
|
|
/* -*- 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"
|
2019-10-02 10:23:02 +00:00
|
|
|
#include "MediaTrackGraph.h"
|
|
|
|
#include "MediaTrackGraphImpl.h"
|
2019-03-06 20:12:25 +00:00
|
|
|
#include "nsISupportsImpl.h"
|
2020-05-15 08:24:06 +00:00
|
|
|
#include "nsISupportsPriority.h"
|
2019-03-06 20:12:25 +00:00
|
|
|
#include "prthread.h"
|
|
|
|
#include "Tracing.h"
|
2024-09-17 14:09:05 +00:00
|
|
|
#include "mozilla/dom/WorkletThread.h"
|
2019-06-21 19:51:22 +00:00
|
|
|
#include "audio_thread_priority.h"
|
2021-08-31 11:38:48 +00:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2021-08-24 15:13:31 +00:00
|
|
|
# include "AndroidProcess.h"
|
2021-11-16 08:07:30 +00:00
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
2019-03-06 20:12:25 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2019-11-25 21:55:36 +00:00
|
|
|
GraphRunner::GraphRunner(MediaTrackGraphImpl* aGraph,
|
|
|
|
already_AddRefed<nsIThread> aThread)
|
|
|
|
: Runnable("GraphRunner"),
|
|
|
|
mMonitor("GraphRunner::mMonitor"),
|
2019-03-06 20:12:25 +00:00
|
|
|
mGraph(aGraph),
|
2019-03-25 22:49:02 +00:00
|
|
|
mThreadState(ThreadState::Wait),
|
2019-11-25 21:55:36 +00:00
|
|
|
mThread(aThread) {
|
|
|
|
mThread->Dispatch(do_AddRef(this));
|
2019-03-06 20:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphRunner::~GraphRunner() {
|
2019-03-25 22:49:02 +00:00
|
|
|
MOZ_ASSERT(mThreadState == ThreadState::Shutdown);
|
2019-03-06 20:12:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 21:55:36 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<GraphRunner> GraphRunner::Create(MediaTrackGraphImpl* aGraph) {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
2024-09-17 14:09:05 +00:00
|
|
|
nsIThreadManager::ThreadCreationOptions options = {
|
|
|
|
.stackSize = mozilla::dom::WorkletThread::StackSize()};
|
|
|
|
if (NS_WARN_IF(NS_FAILED(NS_NewNamedThread(
|
|
|
|
"GraphRunner", getter_AddRefs(thread), nullptr, options)))) {
|
2019-11-25 21:55:36 +00:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:12:25 +00:00
|
|
|
void GraphRunner::Shutdown() {
|
2019-03-26 00:13:55 +00:00
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
MOZ_ASSERT(mThreadState == ThreadState::Wait);
|
|
|
|
mThreadState = ThreadState::Shutdown;
|
|
|
|
mMonitor.Notify();
|
|
|
|
}
|
2019-11-25 21:55:36 +00:00
|
|
|
mThread->Shutdown();
|
2019-03-06 20:12:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 14:16:31 +00:00
|
|
|
auto GraphRunner::OneIteration(GraphTime aStateTime, GraphTime aIterationEnd,
|
2024-01-12 01:32:50 +00:00
|
|
|
MixerCallbackReceiver* aMixerReceiver)
|
|
|
|
-> IterationResult {
|
2021-08-26 09:15:40 +00:00
|
|
|
TRACE("GraphRunner::OneIteration");
|
2019-03-06 20:12:25 +00:00
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
2019-03-25 22:49:02 +00:00
|
|
|
MOZ_ASSERT(mThreadState == ThreadState::Wait);
|
2024-01-12 01:32:50 +00:00
|
|
|
mIterationState =
|
|
|
|
Some(IterationState(aStateTime, aIterationEnd, aMixerReceiver));
|
2019-03-06 20:12:25 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2021-08-24 14:07:17 +00:00
|
|
|
if (const auto* audioDriver =
|
|
|
|
mGraph->CurrentDriver()->AsAudioCallbackDriver()) {
|
2019-03-06 20:12:25 +00:00
|
|
|
mAudioDriverThreadId = audioDriver->ThreadId();
|
2021-08-24 14:07:17 +00:00
|
|
|
} else if (const auto* clockDriver =
|
2019-03-06 20:12:25 +00:00
|
|
|
mGraph->CurrentDriver()->AsSystemClockDriver()) {
|
|
|
|
mClockDriverThread = clockDriver->Thread();
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unknown GraphDriver");
|
|
|
|
}
|
|
|
|
#endif
|
2019-12-18 22:50:00 +00:00
|
|
|
// Signal that mIterationState was updated
|
2019-03-25 22:49:02 +00:00
|
|
|
mThreadState = ThreadState::Run;
|
|
|
|
mMonitor.Notify();
|
2019-12-18 22:51:05 +00:00
|
|
|
// Wait for mIterationResult to update
|
2019-03-25 22:49:02 +00:00
|
|
|
do {
|
|
|
|
mMonitor.Wait();
|
|
|
|
} while (mThreadState == ThreadState::Run);
|
2019-03-06 20:12:25 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mAudioDriverThreadId = std::thread::id();
|
|
|
|
mClockDriverThread = nullptr;
|
|
|
|
#endif
|
|
|
|
|
2019-12-18 22:50:00 +00:00
|
|
|
mIterationState = Nothing();
|
|
|
|
|
2019-12-18 22:51:05 +00:00
|
|
|
IterationResult result = std::move(mIterationResult);
|
|
|
|
mIterationResult = IterationResult();
|
|
|
|
return result;
|
2019-03-06 20:12:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:38:48 +00:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2021-08-24 15:13:31 +00:00
|
|
|
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())));
|
|
|
|
}
|
Bug 1519636 - Reformat recent changes to the Google coding style r=emilio,necko-reviewers,geckoview-reviewers,application-update-reviewers,media-playback-reviewers,devtools-reviewers,anti-tracking-reviewers,profiler-reviewers,win-reviewers,migration-reviewers,padenot,mconley,nchevobbe,kershaw,gstoll,mstange,bytesized,m_kato
This new version of clang 17 also slightly changed the formatting.
# ignore-this-changeset
Differential Revision: https://phabricator.services.mozilla.com/D215914
2024-07-17 11:15:31 +00:00
|
|
|
}; // namespace
|
2021-11-16 08:07:30 +00:00
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
2021-08-24 15:13:31 +00:00
|
|
|
|
2019-11-25 21:55:36 +00:00
|
|
|
NS_IMETHODIMP GraphRunner::Run() {
|
2020-07-27 14:47:51 +00:00
|
|
|
#ifndef XP_LINUX
|
2019-06-21 19:51:22 +00:00
|
|
|
atp_handle* handle =
|
|
|
|
atp_promote_current_thread_to_real_time(0, mGraph->GraphRate());
|
2020-07-27 14:47:51 +00:00
|
|
|
#endif
|
2019-06-21 19:51:22 +00:00
|
|
|
|
2021-08-31 11:38:48 +00:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2021-08-24 15:13:31 +00:00
|
|
|
PromoteRenderingThreadAndroid();
|
2021-11-16 08:07:30 +00:00
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
2021-08-24 15:13:31 +00:00
|
|
|
|
2020-03-03 18:16:54 +00:00
|
|
|
nsCOMPtr<nsIThreadInternal> threadInternal = do_QueryInterface(mThread);
|
|
|
|
threadInternal->SetObserver(mGraph);
|
|
|
|
|
2019-03-06 20:12:25 +00:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
while (true) {
|
2019-03-25 22:49:02 +00:00
|
|
|
while (mThreadState == ThreadState::Wait) {
|
2019-12-18 22:50:00 +00:00
|
|
|
mMonitor.Wait(); // Wait for mIterationState to update or for shutdown
|
2019-03-25 22:49:02 +00:00
|
|
|
}
|
|
|
|
if (mThreadState == ThreadState::Shutdown) {
|
2019-03-06 20:12:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-12-18 22:50:00 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mIterationState.isSome());
|
2021-08-26 09:15:40 +00:00
|
|
|
TRACE("GraphRunner::Run");
|
2024-01-12 01:32:50 +00:00
|
|
|
mIterationResult = mGraph->OneIterationImpl(
|
|
|
|
mIterationState->StateTime(), mIterationState->IterationEnd(),
|
|
|
|
mIterationState->MixerReceiver());
|
2019-12-18 22:51:05 +00:00
|
|
|
// Signal that mIterationResult was updated
|
2019-03-25 22:49:02 +00:00
|
|
|
mThreadState = ThreadState::Wait;
|
|
|
|
mMonitor.Notify();
|
2019-03-06 20:12:25 +00:00
|
|
|
}
|
2019-03-14 13:40:07 +00:00
|
|
|
|
2020-07-27 14:47:51 +00:00
|
|
|
#ifndef XP_LINUX
|
2019-06-21 19:51:22 +00:00
|
|
|
if (handle) {
|
|
|
|
atp_demote_current_thread_from_real_time(handle);
|
|
|
|
}
|
2020-07-27 14:47:51 +00:00
|
|
|
#endif
|
2019-06-21 19:51:22 +00:00
|
|
|
|
2019-11-25 21:55:36 +00:00
|
|
|
return NS_OK;
|
2019-03-06 20:12:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 23:14:10 +00:00
|
|
|
bool GraphRunner::OnThread() const { return mThread->IsOnCurrentThread(); }
|
2019-03-06 20:12:25 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2020-11-30 14:16:57 +00:00
|
|
|
bool GraphRunner::InDriverIteration(const GraphDriver* aDriver) const {
|
2019-03-06 20:12:25 +00:00
|
|
|
if (!OnThread()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:07:17 +00:00
|
|
|
if (const auto* audioDriver = aDriver->AsAudioCallbackDriver()) {
|
2019-03-06 20:12:25 +00:00
|
|
|
return audioDriver->ThreadId() == mAudioDriverThreadId;
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:07:17 +00:00
|
|
|
if (const auto* clockDriver = aDriver->AsSystemClockDriver()) {
|
2019-03-06 20:12:25 +00:00
|
|
|
return clockDriver->Thread() == mClockDriverThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unknown driver");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace mozilla
|