Skip to content

Commit e85a493

Browse files
committed
Bug 1993981 - In MediaTrackGraphImpl natively support AudioWorklet's message passing. r=karlt
This patch does two things to support AudioWorklet: - Supports target shutdown tasks. - Exempts the IPC I/O Child thread from the tail dispatch requirement. The IPC I/O Child thread does not support tail dispatch, and there are no ordering requirements between those messages and any from other threads. Prior to D277176, these messages went through GraphRunner::mThread. It was given to the MessageChannel opened in the AudioWorklet through GetCurrentSerialEventTarget(). Differential Revision: https://phabricator.services.mozilla.com/D277177
1 parent 4cdd700 commit e85a493

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎dom/media/MediaTrackGraph.cpp‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "mozilla/dom/BaseAudioContextBinding.h"
4545
#include "mozilla/dom/Document.h"
4646
#include "mozilla/dom/WorkletThread.h"
47+
#include "mozilla/ipc/IOThread.h"
4748
#include "mozilla/media/MediaUtils.h"
4849
#include "transport/runnable_utils.h"
4950
#include "webaudio/blink/DenormalDisabler.h"
@@ -1916,14 +1917,19 @@ class MediaTrackGraphShutDownRunnable : public Runnable {
19161917
track->RemoveAllResourcesAndListenersImpl();
19171918
}
19181919

1919-
#ifdef DEBUG
1920+
TargetShutdownTaskSet::TasksArray shutdownTasks;
19201921
{
19211922
MonitorAutoLock lock(mGraph->mMonitor);
19221923
MOZ_ASSERT(mGraph->mUpdateRunnables.IsEmpty());
1924+
shutdownTasks = mGraph->mShutdownTasks.Extract();
19231925
}
1924-
#endif
1926+
19251927
mGraph->mPendingUpdateRunnables.Clear();
19261928

1929+
for (auto& shutdownTask : shutdownTasks) {
1930+
shutdownTask->TargetShutdown();
1931+
}
1932+
19271933
mGraph->RemoveShutdownBlocker();
19281934

19291935
// We can't block past the final LIFECYCLE_WAITING_FOR_TRACK_DESTRUCTION
@@ -4462,10 +4468,13 @@ nsresult MediaTrackGraphImpl::Dispatch(
44624468
event = MakeAndAddRef<DefaultShutdownRunnableWrapper>(event.forget());
44634469
}
44644470

4465-
MOZ_ASSERT(RequiresTailDispatchFromCurrentThread(),
4466-
"Tail dispatch will be used for the Dispatch below");
4471+
// Enforce tail-dispatch. The IOThread is exempt as it dispatches messages to
4472+
// AudioWorklet from JS.
4473+
MOZ_ASSERT_IF(
4474+
ipc::IOThread::Get()->GetEventTarget() != GetCurrentSerialEventTarget(),
4475+
RequiresTailDispatchFromCurrentThread());
44674476

4468-
if (aReason == TailDispatch) {
4477+
if (aReason == TailDispatch || !RequiresTailDispatchFromCurrentThread()) {
44694478
return TailDispatchMessage(event.forget());
44704479
}
44714480
return QueueMessageForTailDispatch(event.forget());
@@ -4481,16 +4490,18 @@ TaskDispatcher& MediaTrackGraphImpl::TailDispatcher() {
44814490

44824491
NS_IMETHODIMP MediaTrackGraphImpl::RegisterShutdownTask(
44834492
nsITargetShutdownTask* aTask) {
4484-
return NS_ERROR_NOT_IMPLEMENTED;
4493+
MonitorAutoLock lock(mMonitor);
4494+
return mShutdownTasks.AddTask(aTask);
44854495
}
44864496

44874497
NS_IMETHODIMP MediaTrackGraphImpl::UnregisterShutdownTask(
44884498
nsITargetShutdownTask* aTask) {
4489-
return NS_ERROR_NOT_IMPLEMENTED;
4499+
MonitorAutoLock lock(mMonitor);
4500+
return mShutdownTasks.RemoveTask(aTask);
44904501
}
44914502

44924503
nsIEventTarget::FeatureFlags MediaTrackGraphImpl::GetFeatures() {
4493-
return SUPPORTS_BASE;
4504+
return SUPPORTS_SHUTDOWN_TASKS;
44944505
}
44954506

44964507
nsresult MediaTrackGraphImpl::TailDispatchMessage(

‎dom/media/MediaTrackGraphImpl.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mozilla/AbstractThread.h"
1616
#include "mozilla/Atomics.h"
1717
#include "mozilla/Monitor.h"
18+
#include "mozilla/TargetShutdownTaskSet.h"
1819
#include "mozilla/TimeStamp.h"
1920
#include "mozilla/UniquePtr.h"
2021
#include "nsIDirectTaskDispatcher.h"
@@ -913,6 +914,10 @@ class MediaTrackGraphImpl : public MediaTrackGraph,
913914
mMonitor.AssertCurrentThreadOwns();
914915
return !mBackMessageQueue.IsEmpty();
915916
}
917+
918+
/* Tasks to run at shutdown. */
919+
TargetShutdownTaskSet mShutdownTasks MOZ_GUARDED_BY(mMonitor);
920+
916921
/**
917922
* This enum specifies where this graph is in its lifecycle. This is used
918923
* to control shutdown.

0 commit comments

Comments
 (0)