2019-03-06 23:12:25 +03: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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_GraphRunner_h
|
|
|
|
#define mozilla_GraphRunner_h
|
|
|
|
|
2019-12-19 01:51:05 +03:00
|
|
|
#include "GraphDriver.h"
|
2019-03-06 23:12:25 +03:00
|
|
|
#include "MediaSegment.h"
|
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
struct PRThread;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2019-12-19 01:50:00 +03:00
|
|
|
class AudioMixer;
|
2019-10-02 13:23:02 +03:00
|
|
|
class MediaTrackGraphImpl;
|
2019-03-06 23:12:25 +03:00
|
|
|
|
2019-11-26 00:55:36 +03:00
|
|
|
class GraphRunner final : public Runnable {
|
2019-12-19 01:51:05 +03:00
|
|
|
using IterationResult = GraphDriver::IterationResult;
|
|
|
|
|
2019-03-06 23:12:25 +03:00
|
|
|
public:
|
2019-11-26 00:55:36 +03:00
|
|
|
static already_AddRefed<GraphRunner> Create(MediaTrackGraphImpl* aGraph);
|
2019-03-06 23:12:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks us as shut down and signals mThread, so that it runs until the end.
|
|
|
|
*/
|
2019-11-26 00:55:36 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT void Shutdown();
|
2019-03-06 23:12:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signals one iteration of mGraph. Hands aStateEnd over to mThread and runs
|
|
|
|
* the iteration there.
|
|
|
|
*/
|
2019-12-19 01:51:05 +03:00
|
|
|
IterationResult OneIteration(GraphTime aStateEnd, AudioMixer* aMixer);
|
2019-03-06 23:12:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs mGraph until it shuts down.
|
|
|
|
*/
|
2019-11-26 00:55:36 +03:00
|
|
|
NS_IMETHOD Run();
|
2019-03-06 23:12:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if called on mThread.
|
|
|
|
*/
|
|
|
|
bool OnThread();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/**
|
|
|
|
* Returns true if called on mThread, and aDriver was the driver that called
|
|
|
|
* OneIteration() last.
|
|
|
|
*/
|
2019-12-19 01:51:45 +03:00
|
|
|
bool InDriverIteration(GraphDriver* aDriver);
|
2019-03-06 23:12:25 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2019-11-26 00:55:36 +03:00
|
|
|
explicit GraphRunner(MediaTrackGraphImpl* aGraph,
|
|
|
|
already_AddRefed<nsIThread> aThread);
|
|
|
|
~GraphRunner();
|
|
|
|
|
2019-12-19 01:50:00 +03:00
|
|
|
class IterationState {
|
|
|
|
GraphTime mStateEnd;
|
|
|
|
AudioMixer* MOZ_NON_OWNING_REF mMixer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
IterationState(GraphTime aStateEnd, AudioMixer* aMixer)
|
|
|
|
: mStateEnd(aStateEnd), mMixer(aMixer) {}
|
|
|
|
IterationState& operator=(const IterationState& aOther) {
|
|
|
|
mStateEnd = aOther.mStateEnd;
|
|
|
|
mMixer = aOther.mMixer;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
GraphTime StateEnd() const { return mStateEnd; }
|
|
|
|
AudioMixer* Mixer() const { return mMixer; }
|
|
|
|
};
|
|
|
|
|
2019-03-06 23:12:25 +03:00
|
|
|
// Monitor used for yielding mThread through Wait(), and scheduling mThread
|
|
|
|
// through Signal() from a GraphDriver.
|
|
|
|
Monitor mMonitor;
|
2019-10-02 13:23:02 +03:00
|
|
|
// The MediaTrackGraph we're running. Weakptr beecause this graph owns us and
|
2019-03-06 23:12:25 +03:00
|
|
|
// guarantees that our lifetime will not go beyond that of itself.
|
2019-10-02 13:23:02 +03:00
|
|
|
MediaTrackGraphImpl* const mGraph;
|
2019-12-19 01:50:00 +03:00
|
|
|
// State being handed over to the graph through OneIteration. Protected by
|
2019-03-06 23:12:25 +03:00
|
|
|
// mMonitor.
|
2019-12-19 01:50:00 +03:00
|
|
|
Maybe<IterationState> mIterationState;
|
2019-12-19 01:51:05 +03:00
|
|
|
// Result from mGraph's OneIteration. Protected by mMonitor.
|
|
|
|
IterationResult mIterationResult;
|
2019-03-26 01:49:02 +03:00
|
|
|
|
|
|
|
enum class ThreadState {
|
|
|
|
Wait, // Waiting for a message. This is the initial state.
|
|
|
|
// A transition from Run back to Wait occurs on the runner
|
2019-12-19 01:50:00 +03:00
|
|
|
// thread after it processes as far as mIterationState->mStateEnd
|
2019-12-19 01:51:05 +03:00
|
|
|
// and sets mIterationResult.
|
2019-12-19 01:50:00 +03:00
|
|
|
Run, // Set on driver thread after each mIterationState update.
|
2019-03-26 01:49:02 +03:00
|
|
|
Shutdown, // Set when Shutdown() is called on main thread.
|
|
|
|
};
|
|
|
|
// Protected by mMonitor until set to Shutdown, after which this is not
|
|
|
|
// modified.
|
|
|
|
ThreadState mThreadState;
|
|
|
|
|
2019-03-26 03:13:55 +03:00
|
|
|
// The thread running mGraph. Set on construction, after other members are
|
|
|
|
// initialized. Cleared at the end of Shutdown().
|
2019-11-26 00:55:36 +03:00
|
|
|
const nsCOMPtr<nsIThread> mThread;
|
2019-03-06 23:12:25 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Set to mGraph's audio callback driver's thread id, if run by an
|
|
|
|
// AudioCallbackDriver, while OneIteration() is running.
|
|
|
|
std::thread::id mAudioDriverThreadId = std::thread::id();
|
|
|
|
// Set to mGraph's system clock driver's thread, if run by a
|
|
|
|
// SystemClockDriver, while OneIteration() is running.
|
|
|
|
nsIThread* mClockDriverThread = nullptr;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|