1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_GraphRunner_h
8 #define mozilla_GraphRunner_h
10 #include "GraphDriver.h"
11 #include "MediaSegment.h"
12 #include "mozilla/Monitor.h"
21 class MediaTrackGraphImpl
;
23 class GraphRunner final
: public Runnable
{
24 using IterationResult
= GraphInterface::IterationResult
;
27 static already_AddRefed
<GraphRunner
> Create(MediaTrackGraphImpl
* aGraph
);
30 * Marks us as shut down and signals mThread, so that it runs until the end.
32 MOZ_CAN_RUN_SCRIPT
void Shutdown();
35 * Signals one iteration of mGraph. Hands state over to mThread and runs
36 * the iteration there.
38 IterationResult
OneIteration(GraphTime aStateTime
, GraphTime aIterationEnd
,
39 MixerCallbackReceiver
* aMixerReceiver
);
42 * Runs mGraph until it shuts down.
44 NS_IMETHOD
Run() override
;
47 * Returns true if called on mThread.
49 bool OnThread() const;
53 * Returns true if called on mThread, and aDriver was the driver that called
54 * OneIteration() last.
56 bool InDriverIteration(const GraphDriver
* aDriver
) const;
60 explicit GraphRunner(MediaTrackGraphImpl
* aGraph
,
61 already_AddRefed
<nsIThread
> aThread
);
64 class IterationState
{
66 GraphTime mIterationEnd
;
67 MixerCallbackReceiver
* MOZ_NON_OWNING_REF mMixerReceiver
;
70 IterationState(GraphTime aStateTime
, GraphTime aIterationEnd
,
71 MixerCallbackReceiver
* aMixerReceiver
)
72 : mStateTime(aStateTime
),
73 mIterationEnd(aIterationEnd
),
74 mMixerReceiver(aMixerReceiver
) {}
75 IterationState
& operator=(const IterationState
& aOther
) = default;
76 GraphTime
StateTime() const { return mStateTime
; }
77 GraphTime
IterationEnd() const { return mIterationEnd
; }
78 MixerCallbackReceiver
* MixerReceiver() const { return mMixerReceiver
; }
81 // Monitor used for yielding mThread through Wait(), and scheduling mThread
82 // through Signal() from a GraphDriver.
84 // The MediaTrackGraph we're running. Weakptr beecause this graph owns us and
85 // guarantees that our lifetime will not go beyond that of itself.
86 MediaTrackGraphImpl
* const mGraph
;
87 // State being handed over to the graph through OneIteration. Protected by
89 Maybe
<IterationState
> mIterationState
MOZ_GUARDED_BY(mMonitor
);
90 // Result from mGraph's OneIteration. Protected by mMonitor.
91 IterationResult mIterationResult
MOZ_GUARDED_BY(mMonitor
);
93 enum class ThreadState
{
94 Wait
, // Waiting for a message. This is the initial state.
95 // A transition from Run back to Wait occurs on the runner thread
96 // after it processes as far as mIterationState->mStateTime
97 // and sets mIterationResult.
98 Run
, // Set on driver thread after each mIterationState update.
99 Shutdown
, // Set when Shutdown() is called on main thread.
101 // Protected by mMonitor until set to Shutdown, after which this is not
103 ThreadState mThreadState
MOZ_GUARDED_BY(mMonitor
);
105 // The thread running mGraph. Set on construction, after other members are
106 // initialized. Cleared at the end of Shutdown().
107 const nsCOMPtr
<nsIThread
> mThread
;
110 // Set to mGraph's audio callback driver's thread id, if run by an
111 // AudioCallbackDriver, while OneIteration() is running.
112 std::thread::id mAudioDriverThreadId
= std::thread::id();
113 // Set to mGraph's system clock driver's thread, if run by a
114 // SystemClockDriver, while OneIteration() is running.
115 nsIThread
* mClockDriverThread
= nullptr;
119 } // namespace mozilla