Bug 1572460 - Refactor `selection` out of the `InspectorFront`. r=yulia
[gecko.git] / dom / media / GraphRunner.h
blob2f6a98173f7059bad51f20a404a645387a84e397
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 "MediaSegment.h"
11 #include "mozilla/Monitor.h"
13 #include <thread>
15 struct PRThread;
17 namespace mozilla {
19 class GraphDriver;
20 class MediaStreamGraphImpl;
22 class GraphRunner {
23 public:
24 explicit GraphRunner(MediaStreamGraphImpl* aGraph);
25 ~GraphRunner();
27 /**
28 * Marks us as shut down and signals mThread, so that it runs until the end.
30 void Shutdown();
32 /**
33 * Signals one iteration of mGraph. Hands aStateEnd over to mThread and runs
34 * the iteration there.
36 bool OneIteration(GraphTime aStateEnd);
38 /**
39 * Runs mGraph until it shuts down.
41 void Run();
43 /**
44 * Returns true if called on mThread.
46 bool OnThread();
48 #ifdef DEBUG
49 /**
50 * Returns true if called on mThread, and aDriver was the driver that called
51 * OneIteration() last.
53 bool RunByGraphDriver(GraphDriver* aDriver);
54 #endif
56 private:
57 // Monitor used for yielding mThread through Wait(), and scheduling mThread
58 // through Signal() from a GraphDriver.
59 Monitor mMonitor;
60 // The MediaStreamGraph we're running. Weakptr beecause this graph owns us and
61 // guarantees that our lifetime will not go beyond that of itself.
62 MediaStreamGraphImpl* const mGraph;
63 // GraphTime being handed over to the graph through OneIteration. Protected by
64 // mMonitor.
65 GraphTime mStateEnd;
66 // Reply from mGraph's OneIteration. Protected by mMonitor.
67 bool mStillProcessing;
69 enum class ThreadState {
70 Wait, // Waiting for a message. This is the initial state.
71 // A transition from Run back to Wait occurs on the runner
72 // thread after it processes as far as mStateEnd and sets
73 // mStillProcessing.
74 Run, // Set on driver thread after each mStateEnd update.
75 Shutdown, // Set when Shutdown() is called on main thread.
77 // Protected by mMonitor until set to Shutdown, after which this is not
78 // modified.
79 ThreadState mThreadState;
81 // The thread running mGraph. Set on construction, after other members are
82 // initialized. Cleared at the end of Shutdown().
83 PRThread* mThread;
85 #ifdef DEBUG
86 // Set to mGraph's audio callback driver's thread id, if run by an
87 // AudioCallbackDriver, while OneIteration() is running.
88 std::thread::id mAudioDriverThreadId = std::thread::id();
89 // Set to mGraph's system clock driver's thread, if run by a
90 // SystemClockDriver, while OneIteration() is running.
91 nsIThread* mClockDriverThread = nullptr;
92 #endif
95 } // namespace mozilla
97 #endif