Skip to content

Commit 73bbfc2

Browse files
committed
Bug 1993981 - Implement nsIDirectTaskDispatcher for MediaTrackGraphImpl. r=karlt
Differential Revision: https://phabricator.services.mozilla.com/D277174
1 parent 0018897 commit 73bbfc2

2 files changed

Lines changed: 53 additions & 15 deletions

File tree

‎dom/media/MediaTrackGraph.cpp‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ MediaTrackGraphImpl::~MediaTrackGraphImpl() {
183183
MOZ_ASSERT(mTracks.IsEmpty() && mSuspendedTracks.IsEmpty(),
184184
"All tracks should have been destroyed by messages from the main "
185185
"thread");
186+
MOZ_ASSERT(
187+
mDirectMessages.IsEmpty(),
188+
"All direct tasks should have been drained in the final iteration");
186189
LOG(LogLevel::Debug, ("MediaTrackGraph {} destroyed", fmt::ptr(this)));
187190
LOG(LogLevel::Debug, ("MediaTrackGraphImpl::~MediaTrackGraphImpl"));
188191
}
@@ -1251,14 +1254,7 @@ void MediaTrackGraphImpl::ProduceDataForTracksBlockByBlock(
12511254
void MediaTrackGraphImpl::RunMessageAfterProcessing(
12521255
already_AddRefed<nsIRunnable> aMessage) {
12531256
MOZ_ASSERT(OnGraphThread());
1254-
1255-
if (mFrontMessageQueue.IsEmpty()) {
1256-
mFrontMessageQueue.AppendElement();
1257-
}
1258-
1259-
// Only one block is used for messages from the graph thread.
1260-
MOZ_ASSERT(mFrontMessageQueue.Length() == 1);
1261-
mFrontMessageQueue[0].mMessages.AppendElement(std::move(aMessage));
1257+
DispatchDirectTask(std::move(aMessage));
12621258
}
12631259

12641260
void MediaTrackGraphImpl::RunMessagesInQueue() {
@@ -1632,9 +1628,7 @@ auto MediaTrackGraphImpl::OneIterationImpl(
16321628

16331629
ProcessChunkMetadata(oldProcessedTime);
16341630

1635-
// Process graph messages queued from RunMessageAfterProcessing() on this
1636-
// thread during the iteration.
1637-
RunMessagesInQueue();
1631+
DrainDirectTasks();
16381632

16391633
if (!UpdateMainThreadState()) {
16401634
if (Switching()) {
@@ -3597,8 +3591,9 @@ void MediaTrackGraph::ForceShutDown() {
35973591
graph->ForceShutDown();
35983592
}
35993593

3600-
NS_IMPL_ISUPPORTS(MediaTrackGraphImpl, nsIMemoryReporter, nsIObserver,
3601-
nsIThreadObserver, nsITimerCallback, nsINamed)
3594+
NS_IMPL_ISUPPORTS(MediaTrackGraphImpl, nsIDirectTaskDispatcher,
3595+
nsIMemoryReporter, nsIObserver, nsIThreadObserver,
3596+
nsITimerCallback, nsINamed)
36023597

36033598
NS_IMETHODIMP
36043599
MediaTrackGraphImpl::CollectReports(nsIHandleReportCallback* aHandleReport,
@@ -4377,6 +4372,42 @@ NS_IMETHODIMP
43774372
MediaTrackGraphImpl::AfterProcessNextEvent(nsIThreadInternal*, bool) {
43784373
return NS_OK;
43794374
}
4375+
4376+
// nsIDirectTaskDispatcher methods
4377+
4378+
NS_IMETHODIMP
4379+
MediaTrackGraphImpl::DispatchDirectTask(already_AddRefed<nsIRunnable> aTask) {
4380+
MOZ_ASSERT(OnGraphThread());
4381+
nsCOMPtr task = aTask;
4382+
PROFILER_MARKER("MediaTrackGraphImpl::DispatchDirectTask", OTHER,
4383+
{MarkerStack::Capture()}, FlowMarker,
4384+
Flow::FromPointer(task.get()));
4385+
mDirectMessages.AppendElement(task.forget());
4386+
return NS_OK;
4387+
}
4388+
4389+
NS_IMETHODIMP
4390+
MediaTrackGraphImpl::DrainDirectTasks() {
4391+
MOZ_ASSERT(OnGraphThread());
4392+
// Run all tasks in mDirectMessages. This continues even if one direct task
4393+
// adds another.
4394+
for (size_t i = 0; i < mDirectMessages.Length(); ++i) {
4395+
nsCOMPtr<nsIRunnable> message = std::move(mDirectMessages[i]);
4396+
AUTO_PROFILE_FOLLOWING_RUNNABLE(message);
4397+
message->Run();
4398+
}
4399+
mDirectMessages.ClearAndRetainStorage();
4400+
return NS_OK;
4401+
}
4402+
4403+
NS_IMETHODIMP
4404+
MediaTrackGraphImpl::HaveDirectTasks(bool* aResult) {
4405+
if (!OnGraphThread()) {
4406+
return NS_ERROR_FAILURE;
4407+
}
4408+
*aResult = !mDirectMessages.IsEmpty();
4409+
return NS_OK;
4410+
}
43804411
} // namespace mozilla
43814412

43824413
#undef LOG

‎dom/media/MediaTrackGraphImpl.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <atomic>
99

10-
#include "AsyncLogger.h"
1110
#include "AudioMixer.h"
1211
#include "DeviceInputTrack.h"
1312
#include "GraphDriver.h"
@@ -17,7 +16,7 @@
1716
#include "mozilla/Monitor.h"
1817
#include "mozilla/TimeStamp.h"
1918
#include "mozilla/UniquePtr.h"
20-
#include "nsClassHashtable.h"
19+
#include "nsIDirectTaskDispatcher.h"
2120
#include "nsIMemoryReporter.h"
2221
#include "nsINamed.h"
2322
#include "nsIRunnable.h"
@@ -106,6 +105,7 @@ class MessageBlock {
106105
*/
107106
class MediaTrackGraphImpl : public MediaTrackGraph,
108107
public GraphInterface,
108+
public nsIDirectTaskDispatcher,
109109
public nsIMemoryReporter,
110110
public nsIObserver,
111111
public nsIThreadObserver,
@@ -116,6 +116,7 @@ class MediaTrackGraphImpl : public MediaTrackGraph,
116116
using ControlMessageWrapper = MediaTrack::ControlMessageWrapper;
117117

118118
NS_DECL_THREADSAFE_ISUPPORTS
119+
NS_DECL_NSIDIRECTTASKDISPATCHER
119120
NS_DECL_NSIMEMORYREPORTER
120121
NS_DECL_NSIOBSERVER
121122
NS_DECL_NSITHREADOBSERVER
@@ -869,6 +870,12 @@ class MediaTrackGraphImpl : public MediaTrackGraph,
869870
* A list of batches of messages to process. Each batch is processed
870871
* as an atomic unit.
871872
*/
873+
/*
874+
* Queue of direct messages added by the currently processed MessageBlock.
875+
* Processed by the MTG thread at the end of an iteration.
876+
* Accessed on graph thread only.
877+
*/
878+
nsTArray<nsCOMPtr<nsIRunnable>> mDirectMessages;
872879
/*
873880
* Message queue processed by the MTG thread during an iteration.
874881
* Accessed on graph thread only.

0 commit comments

Comments
 (0)