2014-04-25 18:09:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-04-12 18:51:35 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-04-25 18:09:30 +04:00
|
|
|
/* 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 http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <MediaStreamGraphImpl.h>
|
2015-11-27 07:40:30 +03:00
|
|
|
#include "mozilla/dom/AudioContext.h"
|
2016-01-21 19:51:36 +03:00
|
|
|
#include "mozilla/SharedThreadPool.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2014-08-26 19:01:33 +04:00
|
|
|
#include "CubebUtils.h"
|
2018-04-12 18:51:35 +03:00
|
|
|
#include "Tracing.h"
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2016-03-15 18:11:30 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
2016-03-08 20:11:09 +03:00
|
|
|
#include "webrtc/MediaEngineWebRTC.h"
|
2016-03-15 18:11:30 +03:00
|
|
|
#endif
|
2016-03-08 20:11:09 +03:00
|
|
|
|
2014-08-26 19:02:31 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
extern mozilla::LazyLogModule gMediaStreamGraphLog;
|
2017-02-06 18:22:36 +03:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif // LOG
|
|
|
|
#define LOG(type, msg) MOZ_LOG(gMediaStreamGraphLog, type, msg)
|
2014-08-31 16:19:48 +04:00
|
|
|
|
2014-04-25 18:09:30 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
GraphDriver::GraphDriver(MediaStreamGraphImpl* aGraphImpl)
|
2014-08-26 19:01:33 +04:00
|
|
|
: mIterationStart(0),
|
|
|
|
mIterationEnd(0),
|
2014-08-25 17:26:21 +04:00
|
|
|
mGraphImpl(aGraphImpl),
|
2014-08-26 19:01:33 +04:00
|
|
|
mCurrentTimeStamp(TimeStamp::Now()),
|
|
|
|
mPreviousDriver(nullptr),
|
2017-11-04 09:00:46 +03:00
|
|
|
mNextDriver(nullptr)
|
2014-08-25 17:26:21 +04:00
|
|
|
{ }
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
void GraphDriver::SetGraphTime(GraphDriver* aPreviousDriver,
|
|
|
|
GraphTime aLastSwitchNextIterationStart,
|
2015-08-13 07:23:17 +03:00
|
|
|
GraphTime aLastSwitchNextIterationEnd)
|
2014-08-26 19:01:33 +04:00
|
|
|
{
|
2015-12-01 13:47:59 +03:00
|
|
|
GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
|
2014-08-26 19:01:33 +04:00
|
|
|
// We set mIterationEnd here, because the first thing a driver do when it
|
|
|
|
// does an iteration is to update graph times, so we are in fact setting
|
|
|
|
// mIterationStart of the next iteration by setting the end of the previous
|
|
|
|
// iteration.
|
|
|
|
mIterationStart = aLastSwitchNextIterationStart;
|
|
|
|
mIterationEnd = aLastSwitchNextIterationEnd;
|
|
|
|
|
2015-12-01 13:47:31 +03:00
|
|
|
MOZ_ASSERT(!PreviousDriver());
|
2016-01-21 19:51:36 +03:00
|
|
|
MOZ_ASSERT(aPreviousDriver);
|
2017-12-21 10:08:21 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(GraphImpl()->CurrentDriver() == aPreviousDriver);
|
2016-01-21 19:51:36 +03:00
|
|
|
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Setting previous driver: %p (%s)",
|
|
|
|
aPreviousDriver,
|
|
|
|
aPreviousDriver->AsAudioCallbackDriver() ? "AudioCallbackDriver"
|
|
|
|
: "SystemClockDriver"));
|
|
|
|
|
2015-12-01 13:47:31 +03:00
|
|
|
SetPreviousDriver(aPreviousDriver);
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphDriver::SwitchAtNextIteration(GraphDriver* aNextDriver)
|
|
|
|
{
|
2015-12-01 13:47:59 +03:00
|
|
|
GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Switching to new driver: %p (%s)",
|
|
|
|
aNextDriver,
|
|
|
|
aNextDriver->AsAudioCallbackDriver() ? "AudioCallbackDriver"
|
|
|
|
: "SystemClockDriver"));
|
2016-01-06 11:20:20 +03:00
|
|
|
if (mNextDriver &&
|
|
|
|
mNextDriver != GraphImpl()->CurrentDriver()) {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Discarding previous next driver: %p (%s)",
|
|
|
|
mNextDriver.get(),
|
|
|
|
mNextDriver->AsAudioCallbackDriver() ? "AudioCallbackDriver"
|
|
|
|
: "SystemClockDriver"));
|
2016-01-06 11:20:20 +03:00
|
|
|
}
|
2015-12-01 13:47:31 +03:00
|
|
|
SetNextDriver(aNextDriver);
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2015-08-13 07:23:17 +03:00
|
|
|
GraphTime
|
|
|
|
GraphDriver::StateComputedTime() const
|
2014-08-26 19:01:33 +04:00
|
|
|
{
|
2015-08-13 07:23:17 +03:00
|
|
|
return mGraphImpl->mStateComputedTime;
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphDriver::EnsureNextIteration()
|
|
|
|
{
|
2014-09-28 20:07:25 +04:00
|
|
|
mGraphImpl->EnsureNextIteration();
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 13:47:31 +03:00
|
|
|
bool GraphDriver::Switching()
|
|
|
|
{
|
|
|
|
GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
return mNextDriver || mPreviousDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphDriver* GraphDriver::NextDriver()
|
|
|
|
{
|
|
|
|
GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
return mNextDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphDriver* GraphDriver::PreviousDriver()
|
|
|
|
{
|
|
|
|
GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
return mPreviousDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphDriver::SetNextDriver(GraphDriver* aNextDriver)
|
|
|
|
{
|
|
|
|
GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
mNextDriver = aNextDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphDriver::SetPreviousDriver(GraphDriver* aPreviousDriver)
|
|
|
|
{
|
|
|
|
GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
mPreviousDriver = aPreviousDriver;
|
|
|
|
}
|
|
|
|
|
2014-09-03 17:52:43 +04:00
|
|
|
ThreadedDriver::ThreadedDriver(MediaStreamGraphImpl* aGraphImpl)
|
|
|
|
: GraphDriver(aGraphImpl)
|
|
|
|
{ }
|
|
|
|
|
2016-04-26 22:33:14 +03:00
|
|
|
class MediaStreamGraphShutdownThreadRunnable : public Runnable {
|
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
explicit MediaStreamGraphShutdownThreadRunnable(
|
|
|
|
already_AddRefed<nsIThread> aThread)
|
|
|
|
: Runnable("MediaStreamGraphShutdownThreadRunnable")
|
|
|
|
, mThread(aThread)
|
2016-04-26 22:33:14 +03:00
|
|
|
{
|
|
|
|
}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2016-04-26 22:33:14 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mThread);
|
|
|
|
|
|
|
|
mThread->Shutdown();
|
|
|
|
mThread = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2014-09-03 17:52:43 +04:00
|
|
|
ThreadedDriver::~ThreadedDriver()
|
|
|
|
{
|
|
|
|
if (mThread) {
|
2017-07-04 10:21:23 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new MediaStreamGraphShutdownThreadRunnable(mThread.forget());
|
2017-07-18 12:26:58 +03:00
|
|
|
GraphImpl()->Dispatch(event.forget());
|
2014-09-03 17:52:43 +04:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 12:26:58 +03:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class MediaStreamGraphInitThreadRunnable : public Runnable {
|
2014-04-25 20:03:04 +04:00
|
|
|
public:
|
2014-04-25 20:04:53 +04:00
|
|
|
explicit MediaStreamGraphInitThreadRunnable(ThreadedDriver* aDriver)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("MediaStreamGraphInitThreadRunnable")
|
|
|
|
, mDriver(aDriver)
|
2014-04-25 20:03:04 +04:00
|
|
|
{
|
|
|
|
}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-04-25 20:03:04 +04:00
|
|
|
{
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Starting a new system driver for graph %p", mDriver->mGraphImpl));
|
2015-12-01 13:47:31 +03:00
|
|
|
|
2017-09-08 17:41:36 +03:00
|
|
|
RefPtr<GraphDriver> previousDriver;
|
2015-12-01 13:47:31 +03:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mDriver->mGraphImpl->GetMonitor());
|
|
|
|
previousDriver = mDriver->PreviousDriver();
|
|
|
|
}
|
|
|
|
if (previousDriver) {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("%p releasing an AudioCallbackDriver(%p), for graph %p",
|
|
|
|
mDriver.get(),
|
2017-09-08 17:41:36 +03:00
|
|
|
previousDriver.get(),
|
2017-02-06 18:22:36 +03:00
|
|
|
mDriver->GraphImpl()));
|
2014-08-26 19:02:07 +04:00
|
|
|
MOZ_ASSERT(!mDriver->AsAudioCallbackDriver());
|
2016-04-22 17:24:19 +03:00
|
|
|
RefPtr<AsyncCubebTask> releaseEvent =
|
|
|
|
new AsyncCubebTask(previousDriver->AsAudioCallbackDriver(), AsyncCubebOperation::SHUTDOWN);
|
|
|
|
releaseEvent->Dispatch();
|
|
|
|
|
|
|
|
MonitorAutoLock mon(mDriver->mGraphImpl->GetMonitor());
|
|
|
|
mDriver->SetPreviousDriver(nullptr);
|
2014-08-26 19:02:09 +04:00
|
|
|
} else {
|
|
|
|
MonitorAutoLock mon(mDriver->mGraphImpl->GetMonitor());
|
2016-03-15 18:46:01 +03:00
|
|
|
MOZ_ASSERT(mDriver->mGraphImpl->MessagesQueued() ||
|
|
|
|
mDriver->mGraphImpl->mForceShutDown, "Don't start a graph without messages queued.");
|
2014-08-26 19:02:09 +04:00
|
|
|
mDriver->mGraphImpl->SwapMessageQueues();
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
2016-04-22 17:24:19 +03:00
|
|
|
|
2014-04-25 20:03:04 +04:00
|
|
|
mDriver->RunThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2016-06-07 23:26:20 +03:00
|
|
|
RefPtr<ThreadedDriver> mDriver;
|
2014-04-25 20:03:04 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2014-04-25 20:04:53 +04:00
|
|
|
ThreadedDriver::Start()
|
2014-04-25 20:03:04 +04:00
|
|
|
{
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Starting thread for a SystemClockDriver %p", mGraphImpl));
|
2016-03-29 04:00:21 +03:00
|
|
|
Unused << NS_WARN_IF(mThread);
|
2016-03-22 13:30:06 +03:00
|
|
|
if (!mThread) { // Ensure we haven't already started it
|
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphInitThreadRunnable(this);
|
|
|
|
// Note: mThread may be null during event->Run() if we pass to NewNamedThread! See AudioInitTask
|
|
|
|
nsresult rv = NS_NewNamedThread("MediaStreamGrph", getter_AddRefs(mThread));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-11-04 09:00:46 +03:00
|
|
|
mThread->EventTarget()->Dispatch(event.forget(), NS_DISPATCH_NORMAL);
|
2016-03-22 13:30:06 +03:00
|
|
|
}
|
2014-09-28 20:07:24 +04:00
|
|
|
}
|
2014-04-25 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 18:09:30 +04:00
|
|
|
void
|
2014-08-26 19:01:33 +04:00
|
|
|
ThreadedDriver::Revive()
|
2014-04-25 18:09:30 +04:00
|
|
|
{
|
2014-09-28 20:07:24 +04:00
|
|
|
// Note: only called on MainThread, without monitor
|
|
|
|
// We know were weren't in a running state
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("AudioCallbackDriver reviving."));
|
2014-08-26 19:01:33 +04:00
|
|
|
// If we were switching, switch now. Otherwise, tell thread to run the main
|
|
|
|
// loop again.
|
2014-09-28 20:07:24 +04:00
|
|
|
MonitorAutoLock mon(mGraphImpl->GetMonitor());
|
2015-12-01 13:47:31 +03:00
|
|
|
if (NextDriver()) {
|
|
|
|
NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
|
|
|
|
mGraphImpl->SetCurrentDriver(NextDriver());
|
|
|
|
NextDriver()->Start();
|
2014-08-26 19:01:33 +04:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphInitThreadRunnable(this);
|
2017-06-01 23:41:18 +03:00
|
|
|
mThread->EventTarget()->Dispatch(event.forget(), NS_DISPATCH_NORMAL);
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
2014-04-25 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-28 05:05:36 +03:00
|
|
|
ThreadedDriver::Shutdown()
|
2014-04-25 20:03:04 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be called on main thread");
|
|
|
|
// mGraph's thread is not running so it's OK to do whatever here
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("Stopping threads for MediaStreamGraph %p", this));
|
2014-04-25 20:03:04 +04:00
|
|
|
|
|
|
|
if (mThread) {
|
|
|
|
mThread->Shutdown();
|
2014-09-28 20:07:24 +04:00
|
|
|
mThread = nullptr;
|
2014-04-25 20:03:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 20:04:53 +04:00
|
|
|
SystemClockDriver::SystemClockDriver(MediaStreamGraphImpl* aGraphImpl)
|
|
|
|
: ThreadedDriver(aGraphImpl),
|
|
|
|
mInitialTimeStamp(TimeStamp::Now()),
|
2016-07-27 16:18:17 +03:00
|
|
|
mLastTimeStamp(TimeStamp::Now()),
|
2017-11-04 01:02:52 +03:00
|
|
|
mWaitState(WAITSTATE_RUNNING),
|
2016-07-27 16:18:17 +03:00
|
|
|
mIsFallback(false)
|
2014-04-25 20:04:53 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
SystemClockDriver::~SystemClockDriver()
|
|
|
|
{ }
|
|
|
|
|
2016-07-27 16:18:17 +03:00
|
|
|
void
|
|
|
|
SystemClockDriver::MarkAsFallback()
|
|
|
|
{
|
|
|
|
mIsFallback = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SystemClockDriver::IsFallback()
|
|
|
|
{
|
|
|
|
return mIsFallback;
|
|
|
|
}
|
|
|
|
|
2014-04-25 20:03:04 +04:00
|
|
|
void
|
2014-04-25 20:04:53 +04:00
|
|
|
ThreadedDriver::RunThread()
|
2014-04-25 20:03:04 +04:00
|
|
|
{
|
2017-10-17 08:14:43 +03:00
|
|
|
while (true) {
|
2015-07-23 08:15:49 +03:00
|
|
|
mIterationStart = IterationEnd();
|
|
|
|
mIterationEnd += GetIntervalForIteration();
|
|
|
|
|
2015-08-13 07:23:17 +03:00
|
|
|
GraphTime stateComputedTime = StateComputedTime();
|
|
|
|
if (stateComputedTime < mIterationEnd) {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Warning, ("Media graph global underrun detected"));
|
2015-08-13 07:23:17 +03:00
|
|
|
mIterationEnd = stateComputedTime;
|
2015-07-23 08:15:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mIterationStart >= mIterationEnd) {
|
|
|
|
NS_ASSERTION(mIterationStart == mIterationEnd ,
|
|
|
|
"Time can't go backwards!");
|
|
|
|
// This could happen due to low clock resolution, maybe?
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("Time did not advance"));
|
2015-07-23 08:15:49 +03:00
|
|
|
}
|
2014-04-25 20:04:23 +04:00
|
|
|
|
2015-07-29 08:13:23 +03:00
|
|
|
GraphTime nextStateComputedTime =
|
2018-03-01 00:37:02 +03:00
|
|
|
mGraphImpl->RoundUpToEndOfAudioBlock(
|
2015-07-23 08:15:49 +03:00
|
|
|
mIterationEnd + mGraphImpl->MillisecondsToMediaTime(AUDIO_TARGET_MS));
|
2015-08-04 10:54:54 +03:00
|
|
|
if (nextStateComputedTime < stateComputedTime) {
|
|
|
|
// A previous driver may have been processing further ahead of
|
|
|
|
// iterationEnd.
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Warning,
|
|
|
|
("Prevent state from going backwards. interval[%ld; %ld] state[%ld; "
|
|
|
|
"%ld]",
|
|
|
|
(long)mIterationStart,
|
|
|
|
(long)mIterationEnd,
|
|
|
|
(long)stateComputedTime,
|
|
|
|
(long)nextStateComputedTime));
|
2015-08-04 10:54:54 +03:00
|
|
|
nextStateComputedTime = stateComputedTime;
|
|
|
|
}
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Verbose,
|
|
|
|
("interval[%ld; %ld] state[%ld; %ld]",
|
|
|
|
(long)mIterationStart,
|
|
|
|
(long)mIterationEnd,
|
|
|
|
(long)stateComputedTime,
|
|
|
|
(long)nextStateComputedTime));
|
2014-04-25 20:04:23 +04:00
|
|
|
|
2017-10-17 08:14:43 +03:00
|
|
|
bool stillProcessing = mGraphImpl->OneIteration(nextStateComputedTime);
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2017-10-17 08:14:43 +03:00
|
|
|
if (!stillProcessing) {
|
|
|
|
// Enter shutdown mode. The stable-state handler will detect this
|
|
|
|
// and complete shutdown if the graph does not get restarted.
|
|
|
|
mGraphImpl->SignalMainThreadCleanup();
|
|
|
|
return;
|
|
|
|
}
|
2015-12-01 13:47:31 +03:00
|
|
|
MonitorAutoLock lock(GraphImpl()->GetMonitor());
|
2017-10-17 08:14:43 +03:00
|
|
|
if (NextDriver()) {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("Switching to AudioCallbackDriver"));
|
2015-12-01 13:47:31 +03:00
|
|
|
NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
|
|
|
|
mGraphImpl->SetCurrentDriver(NextDriver());
|
|
|
|
NextDriver()->Start();
|
2014-08-26 19:01:33 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-04-25 20:04:23 +04:00
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
2015-07-23 08:15:49 +03:00
|
|
|
MediaTime
|
|
|
|
SystemClockDriver::GetIntervalForIteration()
|
2014-04-25 18:09:30 +04:00
|
|
|
{
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
2015-07-23 08:15:49 +03:00
|
|
|
MediaTime interval =
|
|
|
|
mGraphImpl->SecondsToMediaTime((now - mCurrentTimeStamp).ToSeconds());
|
2014-04-25 18:09:30 +04:00
|
|
|
mCurrentTimeStamp = now;
|
|
|
|
|
2015-07-23 08:15:49 +03:00
|
|
|
MOZ_LOG(gMediaStreamGraphLog, LogLevel::Verbose,
|
2015-08-13 07:23:17 +03:00
|
|
|
("Updating current time to %f (real %f, StateComputedTime() %f)",
|
2015-07-23 08:15:49 +03:00
|
|
|
mGraphImpl->MediaTimeToSeconds(IterationEnd() + interval),
|
|
|
|
(now - mInitialTimeStamp).ToSeconds(),
|
|
|
|
mGraphImpl->MediaTimeToSeconds(StateComputedTime())));
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2015-07-23 08:15:49 +03:00
|
|
|
return interval;
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:03:04 +04:00
|
|
|
TimeStamp
|
2014-08-26 19:01:33 +04:00
|
|
|
OfflineClockDriver::GetCurrentTimeStamp()
|
2014-04-25 20:03:04 +04:00
|
|
|
{
|
2014-08-26 19:01:33 +04:00
|
|
|
MOZ_CRASH("This driver does not support getting the current timestamp.");
|
|
|
|
return TimeStamp();
|
2014-04-25 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 18:09:30 +04:00
|
|
|
void
|
|
|
|
SystemClockDriver::WaitForNextIteration()
|
|
|
|
{
|
2014-08-26 19:02:30 +04:00
|
|
|
mGraphImpl->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
|
2018-02-09 23:17:26 +03:00
|
|
|
TimeDuration timeout = TimeDuration::Forever();
|
2014-04-25 18:09:30 +04:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2016-08-29 17:41:01 +03:00
|
|
|
|
|
|
|
// This lets us avoid hitting the Atomic twice when we know we won't sleep
|
|
|
|
bool another = mGraphImpl->mNeedAnotherIteration; // atomic
|
|
|
|
if (!another) {
|
|
|
|
mGraphImpl->mGraphDriverAsleep = true; // atomic
|
|
|
|
mWaitState = WAITSTATE_WAITING_INDEFINITELY;
|
|
|
|
}
|
|
|
|
// NOTE: mNeedAnotherIteration while also atomic may have changed before
|
|
|
|
// we could set mGraphDriverAsleep, so we must re-test it.
|
|
|
|
// (EnsureNextIteration sets mNeedAnotherIteration, then tests
|
|
|
|
// mGraphDriverAsleep
|
|
|
|
if (another || mGraphImpl->mNeedAnotherIteration) { // atomic
|
2014-04-25 18:09:30 +04:00
|
|
|
int64_t timeoutMS = MEDIA_GRAPH_TARGET_PERIOD_MS -
|
|
|
|
int64_t((now - mCurrentTimeStamp).ToMilliseconds());
|
|
|
|
// Make sure timeoutMS doesn't overflow 32 bits by waking up at
|
|
|
|
// least once a minute, if we need to wake up at all
|
|
|
|
timeoutMS = std::max<int64_t>(0, std::min<int64_t>(timeoutMS, 60*1000));
|
2018-02-09 23:17:26 +03:00
|
|
|
timeout = TimeDuration::FromMilliseconds(timeoutMS);
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Verbose,
|
|
|
|
("Waiting for next iteration; at %f, timeout=%f",
|
|
|
|
(now - mInitialTimeStamp).ToSeconds(),
|
|
|
|
timeoutMS / 1000.0));
|
2014-09-28 20:07:25 +04:00
|
|
|
if (mWaitState == WAITSTATE_WAITING_INDEFINITELY) {
|
|
|
|
mGraphImpl->mGraphDriverAsleep = false; // atomic
|
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
mWaitState = WAITSTATE_WAITING_FOR_NEXT_ITERATION;
|
|
|
|
}
|
2018-02-09 23:17:26 +03:00
|
|
|
if (!timeout.IsZero()) {
|
2014-08-26 19:01:33 +04:00
|
|
|
mGraphImpl->GetMonitor().Wait(timeout);
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Verbose,
|
|
|
|
("Resuming after timeout; at %f, elapsed=%f",
|
|
|
|
(TimeStamp::Now() - mInitialTimeStamp).ToSeconds(),
|
|
|
|
(TimeStamp::Now() - now).ToSeconds()));
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
2014-09-28 20:07:25 +04:00
|
|
|
if (mWaitState == WAITSTATE_WAITING_INDEFINITELY) {
|
|
|
|
mGraphImpl->mGraphDriverAsleep = false; // atomic
|
|
|
|
}
|
2016-08-29 17:41:01 +03:00
|
|
|
// Note: this can race against the EnsureNextIteration setting
|
|
|
|
// WAITSTATE_RUNNING and setting mGraphDriverAsleep to false, so you can
|
|
|
|
// have an iteration with WAITSTATE_WAKING_UP instead of RUNNING.
|
2014-04-25 18:09:30 +04:00
|
|
|
mWaitState = WAITSTATE_RUNNING;
|
2016-08-29 17:41:01 +03:00
|
|
|
mGraphImpl->mNeedAnotherIteration = false; // atomic
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 13:47:31 +03:00
|
|
|
void SystemClockDriver::WakeUp()
|
2014-04-25 18:09:30 +04:00
|
|
|
{
|
2014-08-26 19:02:31 +04:00
|
|
|
mGraphImpl->GetMonitor().AssertCurrentThreadOwns();
|
2016-08-29 17:41:01 +03:00
|
|
|
// Note: this can race against the thread setting WAITSTATE_RUNNING and
|
|
|
|
// setting mGraphDriverAsleep to false, so you can have an iteration
|
|
|
|
// with WAITSTATE_WAKING_UP instead of RUNNING.
|
2014-04-25 18:09:30 +04:00
|
|
|
mWaitState = WAITSTATE_WAKING_UP;
|
2014-09-28 20:07:25 +04:00
|
|
|
mGraphImpl->mGraphDriverAsleep = false; // atomic
|
2014-08-26 19:01:33 +04:00
|
|
|
mGraphImpl->GetMonitor().Notify();
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
OfflineClockDriver::OfflineClockDriver(MediaStreamGraphImpl* aGraphImpl, GraphTime aSlice)
|
2014-04-25 20:04:53 +04:00
|
|
|
: ThreadedDriver(aGraphImpl),
|
2014-04-25 18:09:30 +04:00
|
|
|
mSlice(aSlice)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
OfflineClockDriver::~OfflineClockDriver()
|
2014-08-26 19:01:33 +04:00
|
|
|
{
|
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2015-07-23 08:15:49 +03:00
|
|
|
MediaTime
|
|
|
|
OfflineClockDriver::GetIntervalForIteration()
|
2014-04-25 18:09:30 +04:00
|
|
|
{
|
2015-07-23 08:15:49 +03:00
|
|
|
return mGraphImpl->MillisecondsToMediaTime(mSlice);
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OfflineClockDriver::WaitForNextIteration()
|
|
|
|
{
|
|
|
|
// No op: we want to go as fast as possible when we are offline
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OfflineClockDriver::WakeUp()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(false, "An offline graph should not have to wake up.");
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
AsyncCubebTask::AsyncCubebTask(AudioCallbackDriver* aDriver,
|
|
|
|
AsyncCubebOperation aOperation)
|
|
|
|
: Runnable("AsyncCubebTask")
|
|
|
|
, mDriver(aDriver)
|
|
|
|
, mOperation(aOperation)
|
|
|
|
, mShutdownGrip(aDriver->GraphImpl())
|
2014-09-03 17:52:43 +04:00
|
|
|
{
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(mDriver->mAudioStream || aOperation == INIT,
|
|
|
|
"No audio stream!");
|
2014-09-03 17:52:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AsyncCubebTask::~AsyncCubebTask()
|
|
|
|
{
|
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2017-09-26 07:28:17 +03:00
|
|
|
SharedThreadPool*
|
|
|
|
AudioCallbackDriver::GetInitShutdownThread()
|
2016-01-21 19:51:36 +03:00
|
|
|
{
|
2017-09-26 07:28:17 +03:00
|
|
|
if (!mInitShutdownThread) {
|
|
|
|
mInitShutdownThread =
|
2016-01-21 19:51:36 +03:00
|
|
|
SharedThreadPool::Get(NS_LITERAL_CSTRING("CubebOperation"), 1);
|
|
|
|
|
|
|
|
const uint32_t kIdleThreadTimeoutMs = 2000;
|
|
|
|
|
2017-09-26 07:28:17 +03:00
|
|
|
mInitShutdownThread->
|
|
|
|
SetIdleThreadTimeout(PR_MillisecondsToInterval(kIdleThreadTimeoutMs));
|
2016-01-22 04:28:17 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 07:28:17 +03:00
|
|
|
return mInitShutdownThread;
|
2016-01-21 19:51:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncCubebTask::Run()
|
|
|
|
{
|
2014-08-26 19:02:07 +04:00
|
|
|
MOZ_ASSERT(mDriver);
|
|
|
|
|
|
|
|
switch(mOperation) {
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
case AsyncCubebOperation::INIT: {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("AsyncCubebOperation::INIT driver=%p", mDriver.get()));
|
2017-01-11 22:51:23 +03:00
|
|
|
if (!mDriver->Init()) {
|
2018-03-16 22:09:55 +03:00
|
|
|
LOG(LogLevel::Warning,
|
|
|
|
("AsyncCubebOperation::INIT failed for driver=%p", mDriver.get()));
|
2017-01-11 22:51:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
mDriver->CompleteAudioContextOperations(mOperation);
|
2014-08-26 19:02:07 +04:00
|
|
|
break;
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
case AsyncCubebOperation::SHUTDOWN: {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("AsyncCubebOperation::SHUTDOWN driver=%p", mDriver.get()));
|
2014-08-26 19:02:07 +04:00
|
|
|
mDriver->Stop();
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
|
|
|
|
mDriver->CompleteAudioContextOperations(mOperation);
|
|
|
|
|
2014-08-26 19:02:07 +04:00
|
|
|
mDriver = nullptr;
|
2014-09-03 17:52:43 +04:00
|
|
|
mShutdownGrip = nullptr;
|
2014-08-26 19:02:07 +04:00
|
|
|
break;
|
2014-08-26 19:02:08 +04:00
|
|
|
}
|
2014-08-26 19:02:07 +04:00
|
|
|
default:
|
|
|
|
MOZ_CRASH("Operation not implemented.");
|
|
|
|
}
|
|
|
|
|
2016-01-21 19:51:36 +03:00
|
|
|
// The thread will kill itself after a bit
|
2014-08-26 19:02:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
StreamAndPromiseForOperation::StreamAndPromiseForOperation(MediaStream* aStream,
|
|
|
|
void* aPromise,
|
|
|
|
dom::AudioContextOperation aOperation)
|
|
|
|
: mStream(aStream)
|
|
|
|
, mPromise(aPromise)
|
|
|
|
, mOperation(aOperation)
|
|
|
|
{
|
|
|
|
// MOZ_ASSERT(aPromise);
|
|
|
|
}
|
|
|
|
|
2015-10-23 06:43:15 +03:00
|
|
|
AudioCallbackDriver::AudioCallbackDriver(MediaStreamGraphImpl* aGraphImpl)
|
2014-08-26 19:02:08 +04:00
|
|
|
: GraphDriver(aGraphImpl)
|
2017-09-28 17:57:46 +03:00
|
|
|
, mOutputChannels(0)
|
2015-12-07 04:17:00 +03:00
|
|
|
, mSampleRate(0)
|
2016-02-17 21:19:02 +03:00
|
|
|
, mInputChannels(1)
|
2014-09-09 20:16:01 +04:00
|
|
|
, mIterationDurationMS(MEDIA_GRAPH_TARGET_PERIOD_MS)
|
2014-08-26 19:02:08 +04:00
|
|
|
, mStarted(false)
|
2016-01-21 19:51:36 +03:00
|
|
|
, mAudioInput(nullptr)
|
2016-01-21 19:51:36 +03:00
|
|
|
, mAddedMixer(false)
|
2014-08-26 19:02:30 +04:00
|
|
|
, mInCallback(false)
|
2016-04-13 21:31:35 +03:00
|
|
|
, mMicrophoneActive(false)
|
2017-11-04 09:00:46 +03:00
|
|
|
, mShouldFallbackIfError(false)
|
2016-07-27 16:18:17 +03:00
|
|
|
, mFromFallback(false)
|
2014-08-26 19:01:33 +04:00
|
|
|
{
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("AudioCallbackDriver ctor for graph %p", aGraphImpl));
|
2017-08-29 12:45:44 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
audio::AudioNotificationReceiver::Register(this);
|
|
|
|
}
|
|
|
|
#endif
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioCallbackDriver::~AudioCallbackDriver()
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPromisesForOperation.IsEmpty());
|
2017-08-29 12:45:44 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
audio::AudioNotificationReceiver::Unregister(this);
|
|
|
|
}
|
|
|
|
#endif
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2016-10-14 19:35:35 +03:00
|
|
|
bool IsMacbookOrMacbookAir()
|
|
|
|
{
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
size_t len = 0;
|
|
|
|
sysctlbyname("hw.model", NULL, &len, NULL, 0);
|
|
|
|
if (len) {
|
2016-10-14 22:13:30 +03:00
|
|
|
UniquePtr<char[]> model(new char[len]);
|
2016-10-14 19:35:35 +03:00
|
|
|
// This string can be
|
|
|
|
// MacBook%d,%d for a normal MacBook
|
|
|
|
// MacBookPro%d,%d for a MacBook Pro
|
|
|
|
// MacBookAir%d,%d for a Macbook Air
|
2016-10-14 22:13:30 +03:00
|
|
|
sysctlbyname("hw.model", model.get(), &len, NULL, 0);
|
|
|
|
char* substring = strstr(model.get(), "MacBook");
|
2016-10-14 19:35:35 +03:00
|
|
|
if (substring) {
|
|
|
|
const size_t offset = strlen("MacBook");
|
2018-01-16 18:44:34 +03:00
|
|
|
if (!strncmp(model.get() + offset, "Air", 3) ||
|
2016-10-14 19:35:35 +03:00
|
|
|
isdigit(model[offset + 1])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-14 22:13:30 +03:00
|
|
|
return false;
|
2016-10-14 19:35:35 +03:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:51:23 +03:00
|
|
|
bool
|
2014-08-26 19:02:07 +04:00
|
|
|
AudioCallbackDriver::Init()
|
2014-08-26 19:01:33 +04:00
|
|
|
{
|
2016-08-31 03:20:10 +03:00
|
|
|
cubeb* cubebContext = CubebUtils::GetCubebContext();
|
|
|
|
if (!cubebContext) {
|
|
|
|
NS_WARNING("Could not get cubeb context.");
|
2018-03-19 21:46:36 +03:00
|
|
|
LOG(LogLevel::Warning, ("%s: Could not get cubeb context", __func__));
|
2016-09-16 03:54:24 +03:00
|
|
|
if (!mFromFallback) {
|
|
|
|
CubebUtils::ReportCubebStreamInitFailure(true);
|
|
|
|
}
|
2018-03-19 21:46:36 +03:00
|
|
|
MonitorAutoLock lock(GraphImpl()->GetMonitor());
|
|
|
|
FallbackToSystemClockDriver();
|
|
|
|
return true;
|
2016-08-31 03:20:10 +03:00
|
|
|
}
|
|
|
|
|
2016-01-21 19:51:36 +03:00
|
|
|
cubeb_stream_params output;
|
|
|
|
cubeb_stream_params input;
|
2016-06-23 18:50:52 +03:00
|
|
|
bool firstStream = CubebUtils::GetFirstStream();
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2014-08-26 19:02:07 +04:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread(),
|
|
|
|
"This is blocking and should never run on the main thread.");
|
|
|
|
|
2018-04-03 20:02:15 +03:00
|
|
|
mSampleRate = output.rate = mGraphImpl->GraphRate();
|
2014-08-26 19:01:33 +04:00
|
|
|
|
|
|
|
if (AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16) {
|
2016-01-21 19:51:36 +03:00
|
|
|
output.format = CUBEB_SAMPLE_S16NE;
|
2014-08-26 19:01:33 +04:00
|
|
|
} else {
|
2016-01-21 19:51:36 +03:00
|
|
|
output.format = CUBEB_SAMPLE_FLOAT32NE;
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2017-09-28 17:57:46 +03:00
|
|
|
// Query and set the number of channels this AudioCallbackDriver will use.
|
2017-11-28 13:57:02 +03:00
|
|
|
mOutputChannels = mGraphImpl->AudioChannelCount();
|
|
|
|
if (!mOutputChannels) {
|
|
|
|
LOG(LogLevel::Warning, ("Output number of channels is 0."));
|
|
|
|
MonitorAutoLock lock(GraphImpl()->GetMonitor());
|
|
|
|
FallbackToSystemClockDriver();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-28 17:57:46 +03:00
|
|
|
mBuffer = AudioCallbackBufferWrapper<AudioDataValue>(mOutputChannels);
|
|
|
|
mScratchBuffer = SpillBuffer<AudioDataValue, WEBAUDIO_BLOCK_SIZE * 2>(mOutputChannels);
|
|
|
|
|
2017-09-26 13:05:01 +03:00
|
|
|
output.channels = mOutputChannels;
|
2018-01-31 21:08:51 +03:00
|
|
|
output.layout = CUBEB_LAYOUT_UNDEFINED;
|
2018-04-30 10:59:32 +03:00
|
|
|
output.prefs = CubebUtils::GetDefaultStreamPrefs();
|
2017-01-20 17:54:00 +03:00
|
|
|
|
2018-02-20 16:37:07 +03:00
|
|
|
uint32_t latency_frames = CubebUtils::GetCubebMSGLatencyInFrames(&output);
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2016-10-14 19:35:35 +03:00
|
|
|
// Macbook and MacBook air don't have enough CPU to run very low latency
|
|
|
|
// MediaStreamGraphs, cap the minimal latency to 512 frames int this case.
|
|
|
|
if (IsMacbookOrMacbookAir()) {
|
2016-10-14 22:13:30 +03:00
|
|
|
latency_frames = std::max((uint32_t) 512, latency_frames);
|
2016-10-14 19:35:35 +03:00
|
|
|
}
|
|
|
|
|
2017-06-01 19:26:17 +03:00
|
|
|
input = output;
|
2017-06-02 09:11:56 +03:00
|
|
|
input.channels = mInputChannels;
|
|
|
|
input.layout = CUBEB_LAYOUT_UNDEFINED;
|
|
|
|
|
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
if (mGraphImpl->mInputWanted) {
|
|
|
|
StaticMutexAutoLock lock(AudioInputCubeb::Mutex());
|
2017-06-30 07:01:17 +03:00
|
|
|
uint32_t userChannels = 0;
|
|
|
|
AudioInputCubeb::GetUserChannelCount(mGraphImpl->mInputDeviceID, userChannels);
|
2017-09-04 17:26:03 +03:00
|
|
|
input.channels = mInputChannels = std::min<uint32_t>(8, userChannels);
|
2017-06-02 09:11:56 +03:00
|
|
|
}
|
|
|
|
#endif
|
2016-01-21 19:51:36 +03:00
|
|
|
|
2016-03-08 20:11:09 +03:00
|
|
|
cubeb_stream* stream = nullptr;
|
|
|
|
CubebUtils::AudioDeviceID input_id = nullptr, output_id = nullptr;
|
|
|
|
// We have to translate the deviceID values to cubeb devid's since those can be
|
|
|
|
// freed whenever enumerate is called.
|
2016-04-01 07:18:13 +03:00
|
|
|
{
|
2016-04-02 19:04:00 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
2016-04-01 07:18:13 +03:00
|
|
|
StaticMutexAutoLock lock(AudioInputCubeb::Mutex());
|
2016-04-02 19:04:00 +03:00
|
|
|
#endif
|
2016-04-01 07:18:13 +03:00
|
|
|
if ((!mGraphImpl->mInputWanted
|
2016-03-15 18:11:30 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
2016-04-01 07:18:13 +03:00
|
|
|
|| AudioInputCubeb::GetDeviceID(mGraphImpl->mInputDeviceID, input_id)
|
2016-03-15 18:11:30 +03:00
|
|
|
#endif
|
2016-04-01 07:18:13 +03:00
|
|
|
) &&
|
|
|
|
(mGraphImpl->mOutputDeviceID == -1 // pass nullptr for ID for default output
|
2016-03-15 18:11:30 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
2016-04-01 07:18:13 +03:00
|
|
|
// XXX we should figure out how we would use a deviceID for output without webrtc.
|
|
|
|
// Currently we don't set this though, so it's ok
|
|
|
|
|| AudioInputCubeb::GetDeviceID(mGraphImpl->mOutputDeviceID, output_id)
|
2016-03-15 18:11:30 +03:00
|
|
|
#endif
|
2016-04-01 07:18:13 +03:00
|
|
|
) &&
|
|
|
|
// XXX Only pass input input if we have an input listener. Always
|
|
|
|
// set up output because it's easier, and it will just get silence.
|
|
|
|
// XXX Add support for adding/removing an input listener later.
|
2016-08-31 03:20:10 +03:00
|
|
|
cubeb_stream_init(cubebContext, &stream,
|
2016-04-01 07:18:13 +03:00
|
|
|
"AudioCallbackDriver",
|
|
|
|
input_id,
|
|
|
|
mGraphImpl->mInputWanted ? &input : nullptr,
|
|
|
|
output_id,
|
2016-07-20 16:02:23 +03:00
|
|
|
mGraphImpl->mOutputWanted ? &output : nullptr, latency_frames,
|
2016-04-01 07:18:13 +03:00
|
|
|
DataCallback_s, StateCallback_s, this) == CUBEB_OK) {
|
|
|
|
mAudioStream.own(stream);
|
2016-06-03 16:32:52 +03:00
|
|
|
DebugOnly<int> rv = cubeb_stream_set_volume(mAudioStream, CubebUtils::GetVolumeScale());
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
rv == CUBEB_OK,
|
|
|
|
"Could not set the audio stream volume in GraphDriver.cpp");
|
2016-06-23 18:50:52 +03:00
|
|
|
CubebUtils::ReportCubebBackendUsed();
|
2016-04-01 07:18:13 +03:00
|
|
|
} else {
|
2016-04-02 19:04:00 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
2016-04-01 07:18:13 +03:00
|
|
|
StaticMutexAutoUnlock unlock(AudioInputCubeb::Mutex());
|
2016-04-02 19:04:00 +03:00
|
|
|
#endif
|
2016-04-01 07:18:13 +03:00
|
|
|
NS_WARNING("Could not create a cubeb stream for MediaStreamGraph, falling back to a SystemClockDriver");
|
2016-07-27 16:18:17 +03:00
|
|
|
// Only report failures when we're not coming from a driver that was
|
|
|
|
// created itself as a fallback driver because of a previous audio driver
|
|
|
|
// failure.
|
|
|
|
if (!mFromFallback) {
|
|
|
|
CubebUtils::ReportCubebStreamInitFailure(firstStream);
|
|
|
|
}
|
2016-04-01 07:18:13 +03:00
|
|
|
MonitorAutoLock lock(GraphImpl()->GetMonitor());
|
2017-11-28 13:57:02 +03:00
|
|
|
FallbackToSystemClockDriver();
|
2017-01-11 22:51:23 +03:00
|
|
|
return true;
|
2016-04-01 07:18:13 +03:00
|
|
|
}
|
2014-08-26 19:02:07 +04:00
|
|
|
}
|
2017-06-06 12:26:39 +03:00
|
|
|
SetMicrophoneActive(mGraphImpl->mInputWanted);
|
2016-04-13 21:31:35 +03:00
|
|
|
|
2014-08-26 19:02:31 +04:00
|
|
|
cubeb_stream_register_device_changed_callback(mAudioStream,
|
|
|
|
AudioCallbackDriver::DeviceChangedCallback_s);
|
|
|
|
|
2017-01-11 22:51:23 +03:00
|
|
|
if (!StartStream()) {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Warning, ("AudioCallbackDriver couldn't start stream."));
|
2017-01-11 22:51:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-26 19:02:07 +04:00
|
|
|
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("AudioCallbackDriver started."));
|
2017-01-11 22:51:23 +03:00
|
|
|
return true;
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 19:02:07 +04:00
|
|
|
void
|
|
|
|
AudioCallbackDriver::Start()
|
|
|
|
{
|
2016-01-21 19:51:36 +03:00
|
|
|
if (mPreviousDriver) {
|
|
|
|
if (mPreviousDriver->AsAudioCallbackDriver()) {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("Releasing audio driver off main thread."));
|
2016-01-21 19:51:36 +03:00
|
|
|
RefPtr<AsyncCubebTask> releaseEvent =
|
|
|
|
new AsyncCubebTask(mPreviousDriver->AsAudioCallbackDriver(),
|
|
|
|
AsyncCubebOperation::SHUTDOWN);
|
|
|
|
releaseEvent->Dispatch();
|
|
|
|
mPreviousDriver = nullptr;
|
|
|
|
} else {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Dropping driver reference for SystemClockDriver."));
|
2016-07-27 16:18:17 +03:00
|
|
|
MOZ_ASSERT(mPreviousDriver->AsSystemClockDriver());
|
|
|
|
mFromFallback = mPreviousDriver->AsSystemClockDriver()->IsFallback();
|
2016-01-21 19:51:36 +03:00
|
|
|
mPreviousDriver = nullptr;
|
2016-01-22 04:28:23 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-21 19:51:36 +03:00
|
|
|
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Starting new audio driver off main thread, "
|
|
|
|
"to ensure it runs after previous shutdown."));
|
2016-01-21 19:51:36 +03:00
|
|
|
RefPtr<AsyncCubebTask> initEvent =
|
|
|
|
new AsyncCubebTask(AsAudioCallbackDriver(), AsyncCubebOperation::INIT);
|
2017-11-04 09:00:46 +03:00
|
|
|
initEvent->Dispatch();
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:51:23 +03:00
|
|
|
bool
|
2014-08-26 19:02:30 +04:00
|
|
|
AudioCallbackDriver::StartStream()
|
|
|
|
{
|
2017-11-04 09:00:46 +03:00
|
|
|
mShouldFallbackIfError = true;
|
2014-08-26 19:02:30 +04:00
|
|
|
if (cubeb_stream_start(mAudioStream) != CUBEB_OK) {
|
2017-01-11 22:51:23 +03:00
|
|
|
NS_WARNING("Could not start cubeb stream for MSG.");
|
|
|
|
return false;
|
2014-08-26 19:02:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mGraphImpl->GetMonitor());
|
|
|
|
mStarted = true;
|
|
|
|
}
|
2017-01-11 22:51:23 +03:00
|
|
|
return true;
|
2014-08-26 19:02:30 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
void
|
|
|
|
AudioCallbackDriver::Stop()
|
|
|
|
{
|
|
|
|
if (cubeb_stream_stop(mAudioStream) != CUBEB_OK) {
|
|
|
|
NS_WARNING("Could not stop cubeb stream for MSG.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioCallbackDriver::Revive()
|
|
|
|
{
|
2014-09-28 20:07:24 +04:00
|
|
|
// Note: only called on MainThread, without monitor
|
|
|
|
// We know were weren't in a running state
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("AudioCallbackDriver reviving."));
|
2014-08-26 19:01:33 +04:00
|
|
|
// If we were switching, switch now. Otherwise, start the audio thread again.
|
2014-09-28 20:07:24 +04:00
|
|
|
MonitorAutoLock mon(mGraphImpl->GetMonitor());
|
2015-12-01 13:47:31 +03:00
|
|
|
if (NextDriver()) {
|
2016-01-21 19:51:36 +03:00
|
|
|
RemoveCallback();
|
2015-12-01 13:47:31 +03:00
|
|
|
NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
|
|
|
|
mGraphImpl->SetCurrentDriver(NextDriver());
|
|
|
|
NextDriver()->Start();
|
2014-08-26 19:01:33 +04:00
|
|
|
} else {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Starting audio threads for MediaStreamGraph %p from a new thread.",
|
|
|
|
mGraphImpl));
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncCubebTask> initEvent =
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
new AsyncCubebTask(this, AsyncCubebOperation::INIT);
|
2014-09-28 20:07:24 +04:00
|
|
|
initEvent->Dispatch();
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 19:51:36 +03:00
|
|
|
void
|
|
|
|
AudioCallbackDriver::RemoveCallback()
|
|
|
|
{
|
|
|
|
if (mAddedMixer) {
|
|
|
|
mGraphImpl->mMixer.RemoveCallback(this);
|
|
|
|
mAddedMixer = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioCallbackDriver::WaitForNextIteration()
|
2014-08-26 19:02:08 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
void
|
|
|
|
AudioCallbackDriver::WakeUp()
|
|
|
|
{
|
2014-08-26 19:02:30 +04:00
|
|
|
mGraphImpl->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
mGraphImpl->GetMonitor().Notify();
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2017-09-28 05:05:36 +03:00
|
|
|
void
|
|
|
|
AudioCallbackDriver::Shutdown()
|
|
|
|
{
|
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Releasing audio driver off main thread (GraphDriver::Shutdown)."));
|
|
|
|
RefPtr<AsyncCubebTask> releaseEvent =
|
|
|
|
new AsyncCubebTask(this, AsyncCubebOperation::SHUTDOWN);
|
|
|
|
releaseEvent->Dispatch(NS_DISPATCH_SYNC);
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:45:44 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
void
|
|
|
|
AudioCallbackDriver::ResetDefaultDevice()
|
|
|
|
{
|
|
|
|
if (cubeb_stream_reset_default_device(mAudioStream) != CUBEB_OK) {
|
|
|
|
NS_WARNING("Could not reset cubeb stream to default output device.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
/* static */ long
|
|
|
|
AudioCallbackDriver::DataCallback_s(cubeb_stream* aStream,
|
2016-01-21 19:51:36 +03:00
|
|
|
void* aUser,
|
|
|
|
const void* aInputBuffer,
|
|
|
|
void* aOutputBuffer,
|
2014-08-26 19:01:33 +04:00
|
|
|
long aFrames)
|
|
|
|
{
|
|
|
|
AudioCallbackDriver* driver = reinterpret_cast<AudioCallbackDriver*>(aUser);
|
2016-01-21 19:51:36 +03:00
|
|
|
return driver->DataCallback(static_cast<const AudioDataValue*>(aInputBuffer),
|
2016-01-21 19:51:36 +03:00
|
|
|
static_cast<AudioDataValue*>(aOutputBuffer), aFrames);
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
AudioCallbackDriver::StateCallback_s(cubeb_stream* aStream, void * aUser,
|
|
|
|
cubeb_state aState)
|
|
|
|
{
|
|
|
|
AudioCallbackDriver* driver = reinterpret_cast<AudioCallbackDriver*>(aUser);
|
|
|
|
driver->StateCallback(aState);
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:02:31 +04:00
|
|
|
/* static */ void
|
|
|
|
AudioCallbackDriver::DeviceChangedCallback_s(void* aUser)
|
|
|
|
{
|
|
|
|
AudioCallbackDriver* driver = reinterpret_cast<AudioCallbackDriver*>(aUser);
|
|
|
|
driver->DeviceChangedCallback();
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:01:35 +04:00
|
|
|
bool AudioCallbackDriver::InCallback() {
|
|
|
|
return mInCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioCallbackDriver::AutoInCallback::AutoInCallback(AudioCallbackDriver* aDriver)
|
|
|
|
: mDriver(aDriver)
|
|
|
|
{
|
|
|
|
mDriver->mInCallback = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioCallbackDriver::AutoInCallback::~AutoInCallback() {
|
|
|
|
mDriver->mInCallback = false;
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
long
|
2016-01-21 19:51:36 +03:00
|
|
|
AudioCallbackDriver::DataCallback(const AudioDataValue* aInputBuffer,
|
2016-01-21 19:51:36 +03:00
|
|
|
AudioDataValue* aOutputBuffer, long aFrames)
|
2014-08-26 19:01:33 +04:00
|
|
|
{
|
2018-04-12 18:51:35 +03:00
|
|
|
TRACE_AUDIO_CALLBACK_BUDGET(aFrames, mSampleRate);
|
|
|
|
TRACE_AUDIO_CALLBACK();
|
|
|
|
|
2016-01-21 19:51:36 +03:00
|
|
|
// Don't add the callback until we're inited and ready
|
|
|
|
if (!mAddedMixer) {
|
|
|
|
mGraphImpl->mMixer.AddCallback(this);
|
|
|
|
mAddedMixer = true;
|
|
|
|
}
|
|
|
|
|
2014-09-28 20:07:24 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
// DebugOnly<> doesn't work here... it forces an initialization that will cause
|
|
|
|
// mInCallback to be set back to false before we exit the statement. Do it by
|
|
|
|
// hand instead.
|
|
|
|
AutoInCallback aic(this);
|
|
|
|
#endif
|
2014-08-26 19:01:35 +04:00
|
|
|
|
2015-08-13 07:23:17 +03:00
|
|
|
GraphTime stateComputedTime = StateComputedTime();
|
|
|
|
if (stateComputedTime == 0) {
|
2014-08-26 19:01:33 +04:00
|
|
|
MonitorAutoLock mon(mGraphImpl->GetMonitor());
|
|
|
|
// Because this function is called during cubeb_stream_init (to prefill the
|
|
|
|
// audio buffers), it can be that we don't have a message here (because this
|
|
|
|
// driver is the first one for this graph), and the graph would exit. Simply
|
|
|
|
// return here until we have messages.
|
|
|
|
if (!mGraphImpl->MessagesQueued()) {
|
2017-09-26 13:05:01 +03:00
|
|
|
PodZero(aOutputBuffer, aFrames * mOutputChannels);
|
2014-08-26 19:01:33 +04:00
|
|
|
return aFrames;
|
|
|
|
}
|
|
|
|
mGraphImpl->SwapMessageQueues();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t durationMS = aFrames * 1000 / mSampleRate;
|
|
|
|
|
|
|
|
// For now, simply average the duration with the previous
|
|
|
|
// duration so there is some damping against sudden changes.
|
|
|
|
if (!mIterationDurationMS) {
|
|
|
|
mIterationDurationMS = durationMS;
|
|
|
|
} else {
|
2014-09-30 18:35:17 +04:00
|
|
|
mIterationDurationMS = (mIterationDurationMS*3) + durationMS;
|
|
|
|
mIterationDurationMS /= 4;
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2017-11-28 15:32:21 +03:00
|
|
|
mBuffer.SetBuffer(aOutputBuffer, aFrames);
|
|
|
|
// fill part or all with leftover data from last iteration (since we
|
|
|
|
// align to Audio blocks)
|
|
|
|
mScratchBuffer.Empty(mBuffer);
|
|
|
|
|
|
|
|
// State computed time is decided by the audio callback's buffer length. We
|
|
|
|
// compute the iteration start and end from there, trying to keep the amount
|
|
|
|
// of buffering in the graph constant.
|
|
|
|
GraphTime nextStateComputedTime =
|
2018-03-01 00:37:02 +03:00
|
|
|
mGraphImpl->RoundUpToEndOfAudioBlock(
|
|
|
|
stateComputedTime + mBuffer.Available());
|
2017-11-28 15:32:21 +03:00
|
|
|
|
|
|
|
mIterationStart = mIterationEnd;
|
|
|
|
// inGraph is the number of audio frames there is between the state time and
|
|
|
|
// the current time, i.e. the maximum theoretical length of the interval we
|
|
|
|
// could use as [mIterationStart; mIterationEnd].
|
|
|
|
GraphTime inGraph = stateComputedTime - mIterationStart;
|
|
|
|
// We want the interval [mIterationStart; mIterationEnd] to be before the
|
|
|
|
// interval [stateComputedTime; nextStateComputedTime]. We also want
|
|
|
|
// the distance between these intervals to be roughly equivalent each time, to
|
|
|
|
// ensure there is no clock drift between current time and state time. Since
|
|
|
|
// we can't act on the state time because we have to fill the audio buffer, we
|
|
|
|
// reclock the current time against the state time, here.
|
|
|
|
mIterationEnd = mIterationStart + 0.8 * inGraph;
|
|
|
|
|
|
|
|
LOG(LogLevel::Verbose,
|
|
|
|
("interval[%ld; %ld] state[%ld; %ld] (frames: %ld) (durationMS: %u) "
|
|
|
|
"(duration ticks: %ld)",
|
|
|
|
(long)mIterationStart,
|
|
|
|
(long)mIterationEnd,
|
|
|
|
(long)stateComputedTime,
|
|
|
|
(long)nextStateComputedTime,
|
|
|
|
(long)aFrames,
|
|
|
|
(uint32_t)durationMS,
|
|
|
|
(long)(nextStateComputedTime - stateComputedTime)));
|
|
|
|
|
|
|
|
mCurrentTimeStamp = TimeStamp::Now();
|
|
|
|
|
|
|
|
if (stateComputedTime < mIterationEnd) {
|
2017-12-01 17:37:54 +03:00
|
|
|
LOG(LogLevel::Error, ("Media graph global underrun detected"));
|
|
|
|
MOZ_ASSERT_UNREACHABLE("We should not underrun in full duplex");
|
2017-11-28 15:32:21 +03:00
|
|
|
mIterationEnd = stateComputedTime;
|
|
|
|
}
|
|
|
|
|
2016-04-29 16:16:46 +03:00
|
|
|
// Process mic data if any/needed
|
|
|
|
if (aInputBuffer) {
|
|
|
|
if (mAudioInput) { // for this specific input-only or full-duplex stream
|
|
|
|
mAudioInput->NotifyInputData(mGraphImpl, aInputBuffer,
|
|
|
|
static_cast<size_t>(aFrames),
|
|
|
|
mSampleRate, mInputChannels);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 15:32:21 +03:00
|
|
|
bool stillProcessing;
|
2014-09-30 18:35:17 +04:00
|
|
|
if (mBuffer.Available()) {
|
2017-11-28 15:32:21 +03:00
|
|
|
// We totally filled the buffer (and mScratchBuffer isn't empty).
|
|
|
|
// We don't need to run an iteration and if we do so we may overflow.
|
2015-08-04 10:42:10 +03:00
|
|
|
stillProcessing = mGraphImpl->OneIteration(nextStateComputedTime);
|
2014-09-30 18:35:17 +04:00
|
|
|
} else {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Verbose,
|
|
|
|
("DataCallback buffer filled entirely from scratch "
|
|
|
|
"buffer, skipping iteration."));
|
2014-09-30 18:35:17 +04:00
|
|
|
stillProcessing = true;
|
|
|
|
}
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2014-08-26 19:04:38 +04:00
|
|
|
mBuffer.BufferFilled();
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2016-01-21 19:51:35 +03:00
|
|
|
// Callback any observers for the AEC speaker data. Note that one
|
|
|
|
// (maybe) of these will be full-duplex, the others will get their input
|
|
|
|
// data off separate cubeb callbacks. Take care with how stuff is
|
|
|
|
// removed/added to this list and TSAN issues, but input and output will
|
|
|
|
// use separate callback methods.
|
2016-01-21 19:51:36 +03:00
|
|
|
mGraphImpl->NotifyOutputData(aOutputBuffer, static_cast<size_t>(aFrames),
|
2017-09-26 13:05:01 +03:00
|
|
|
mSampleRate, mOutputChannels);
|
2016-01-21 19:51:35 +03:00
|
|
|
|
2017-10-17 08:14:43 +03:00
|
|
|
if (!stillProcessing) {
|
2017-11-04 09:00:46 +03:00
|
|
|
// About to hand over control of the graph. Do not start a new driver if
|
|
|
|
// StateCallback() receives an error for this stream while the main thread
|
|
|
|
// or another driver has control of the graph.
|
|
|
|
mShouldFallbackIfError = false;
|
2017-10-17 08:14:43 +03:00
|
|
|
// Enter shutdown mode. The stable-state handler will detect this
|
|
|
|
// and complete shutdown if the graph does not get restarted.
|
|
|
|
mGraphImpl->SignalMainThreadCleanup();
|
|
|
|
return aFrames - 1;
|
|
|
|
}
|
|
|
|
|
2015-12-01 13:47:31 +03:00
|
|
|
bool switching = false;
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mGraphImpl->GetMonitor());
|
|
|
|
switching = !!NextDriver();
|
|
|
|
}
|
|
|
|
|
2017-10-17 08:14:43 +03:00
|
|
|
if (switching) {
|
2017-11-04 09:00:46 +03:00
|
|
|
mShouldFallbackIfError = false;
|
2015-12-01 13:47:31 +03:00
|
|
|
// If the audio stream has not been started by the previous driver or
|
|
|
|
// the graph itself, keep it alive.
|
|
|
|
MonitorAutoLock mon(mGraphImpl->GetMonitor());
|
|
|
|
if (!IsStarted()) {
|
|
|
|
return aFrames;
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("Switching to system driver."));
|
2016-01-21 19:51:36 +03:00
|
|
|
RemoveCallback();
|
2015-12-01 13:47:31 +03:00
|
|
|
NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
|
|
|
|
mGraphImpl->SetCurrentDriver(NextDriver());
|
|
|
|
NextDriver()->Start();
|
2014-08-26 19:01:33 +04:00
|
|
|
// Returning less than aFrames starts the draining and eventually stops the
|
|
|
|
// audio thread. This function will never get called again.
|
|
|
|
return aFrames - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aFrames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioCallbackDriver::StateCallback(cubeb_state aState)
|
|
|
|
{
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug, ("AudioCallbackDriver State: %d", aState));
|
2017-09-08 17:41:36 +03:00
|
|
|
|
2017-11-04 09:00:46 +03:00
|
|
|
if (aState == CUBEB_STATE_ERROR && mShouldFallbackIfError) {
|
2018-02-07 10:16:01 +03:00
|
|
|
mShouldFallbackIfError = false;
|
2017-09-08 17:41:36 +03:00
|
|
|
MonitorAutoLock lock(GraphImpl()->GetMonitor());
|
2017-02-24 18:42:20 +03:00
|
|
|
RemoveCallback();
|
2017-11-28 13:57:02 +03:00
|
|
|
FallbackToSystemClockDriver();
|
2017-02-24 18:42:20 +03:00
|
|
|
}
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioCallbackDriver::MixerCallback(AudioDataValue* aMixedBuffer,
|
|
|
|
AudioSampleFormat aFormat,
|
|
|
|
uint32_t aChannels,
|
|
|
|
uint32_t aFrames,
|
|
|
|
uint32_t aSampleRate)
|
|
|
|
{
|
|
|
|
uint32_t toWrite = mBuffer.Available();
|
|
|
|
|
|
|
|
if (!mBuffer.Available()) {
|
2014-09-30 18:35:17 +04:00
|
|
|
NS_WARNING("DataCallback buffer full, expect frame drops.");
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mBuffer.Available() <= aFrames);
|
|
|
|
|
|
|
|
mBuffer.WriteFrames(aMixedBuffer, mBuffer.Available());
|
|
|
|
MOZ_ASSERT(mBuffer.Available() == 0, "Missing frames to fill audio callback's buffer.");
|
|
|
|
|
|
|
|
DebugOnly<uint32_t> written = mScratchBuffer.Fill(aMixedBuffer + toWrite * aChannels, aFrames - toWrite);
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(written == aFrames - toWrite, "Dropping frames.");
|
2014-08-26 19:01:33 +04:00
|
|
|
};
|
|
|
|
|
2014-08-26 19:02:31 +04:00
|
|
|
void AudioCallbackDriver::PanOutputIfNeeded(bool aMicrophoneActive)
|
|
|
|
{
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
cubeb_device* out;
|
|
|
|
int rv;
|
|
|
|
char name[128];
|
|
|
|
size_t length = sizeof(name);
|
|
|
|
|
|
|
|
rv = sysctlbyname("hw.model", name, &length, NULL, 0);
|
|
|
|
if (rv) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(name, "MacBookPro", 10)) {
|
|
|
|
if (cubeb_stream_get_current_device(mAudioStream, &out) == CUBEB_OK) {
|
|
|
|
// Check if we are currently outputing sound on external speakers.
|
|
|
|
if (!strcmp(out->output_name, "ispk")) {
|
|
|
|
// Pan everything to the right speaker.
|
|
|
|
if (aMicrophoneActive) {
|
|
|
|
if (cubeb_stream_set_panning(mAudioStream, 1.0) != CUBEB_OK) {
|
|
|
|
NS_WARNING("Could not pan audio output to the right.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (cubeb_stream_set_panning(mAudioStream, 0.0) != CUBEB_OK) {
|
|
|
|
NS_WARNING("Could not pan audio output to the center.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (cubeb_stream_set_panning(mAudioStream, 0.0) != CUBEB_OK) {
|
|
|
|
NS_WARNING("Could not pan audio output to the center.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cubeb_stream_device_destroy(mAudioStream, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioCallbackDriver::DeviceChangedCallback() {
|
2016-04-22 17:24:17 +03:00
|
|
|
// Tell the audio engine the device has changed, it might want to reset some
|
|
|
|
// state.
|
2014-08-26 19:02:31 +04:00
|
|
|
MonitorAutoLock mon(mGraphImpl->GetMonitor());
|
2016-04-22 17:24:17 +03:00
|
|
|
if (mAudioInput) {
|
|
|
|
mAudioInput->DeviceChanged();
|
Bug 1085356 - Better handling of OSX audio output devices switching when SourceMediaStream are present in the MSG. r=jesup
On OSX, when the audio output device changes, the OS will call the audio
callbacks in weird patterns, if at all, during a period of ~1s. If
real-time SourceMediaStreams are present in the MediaStreamGraph, this means
buffering will occur, and the overall latency between the MediaStreamGraph
insertion time, and the actual output time will grow.
To fix this, we detect when the output device changes, and we switch temporarily
to a SystemClockDriver, that will pull from the SourceMediaStream, and simply
discard all input data. Then, when we get audio callbacks called reliably
(basically, when OSX is done switching to the other output), we switch back to
the previous AudioCallbackDriver.
We keep the previous AudioCallbackDriver alive using a self-reference. If an
AudioCallbackDriver has a self-reference, that means it's in a state when a
device is switching, so it's not linked to an MSG per se.
2014-10-22 18:12:29 +04:00
|
|
|
}
|
2016-04-22 17:24:17 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
PanOutputIfNeeded(mMicrophoneActive);
|
Bug 1085356 - Better handling of OSX audio output devices switching when SourceMediaStream are present in the MSG. r=jesup
On OSX, when the audio output device changes, the OS will call the audio
callbacks in weird patterns, if at all, during a period of ~1s. If
real-time SourceMediaStreams are present in the MediaStreamGraph, this means
buffering will occur, and the overall latency between the MediaStreamGraph
insertion time, and the actual output time will grow.
To fix this, we detect when the output device changes, and we switch temporarily
to a SystemClockDriver, that will pull from the SourceMediaStream, and simply
discard all input data. Then, when we get audio callbacks called reliably
(basically, when OSX is done switching to the other output), we switch back to
the previous AudioCallbackDriver.
We keep the previous AudioCallbackDriver alive using a self-reference. If an
AudioCallbackDriver has a self-reference, that means it's in a state when a
device is switching, so it's not linked to an MSG per se.
2014-10-22 18:12:29 +04:00
|
|
|
#endif
|
2014-08-26 19:02:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioCallbackDriver::SetMicrophoneActive(bool aActive)
|
|
|
|
{
|
|
|
|
mMicrophoneActive = aActive;
|
|
|
|
|
2016-04-22 17:24:19 +03:00
|
|
|
#ifdef XP_MACOSX
|
2014-08-26 19:02:31 +04:00
|
|
|
PanOutputIfNeeded(mMicrophoneActive);
|
2016-04-13 21:31:35 +03:00
|
|
|
#endif
|
2014-08-26 19:02:31 +04:00
|
|
|
}
|
2014-08-26 19:01:33 +04:00
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AudioCallbackDriver::IterationDuration()
|
|
|
|
{
|
|
|
|
// The real fix would be to have an API in cubeb to give us the number. Short
|
|
|
|
// of that, we approximate it here. bug 1019507
|
|
|
|
return mIterationDurationMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioCallbackDriver::IsStarted() {
|
|
|
|
mGraphImpl->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
return mStarted;
|
|
|
|
}
|
|
|
|
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
void
|
|
|
|
AudioCallbackDriver::EnqueueStreamAndPromiseForOperation(MediaStream* aStream,
|
|
|
|
void* aPromise,
|
|
|
|
dom::AudioContextOperation aOperation)
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mGraphImpl->GetMonitor());
|
|
|
|
mPromisesForOperation.AppendElement(StreamAndPromiseForOperation(aStream,
|
|
|
|
aPromise,
|
|
|
|
aOperation));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioCallbackDriver::CompleteAudioContextOperations(AsyncCubebOperation aOperation)
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<StreamAndPromiseForOperation, 1> array;
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
|
|
|
|
// We can't lock for the whole function because AudioContextOperationCompleted
|
|
|
|
// will grab the monitor
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(GraphImpl()->GetMonitor());
|
|
|
|
array.SwapElements(mPromisesForOperation);
|
|
|
|
}
|
|
|
|
|
2015-04-29 12:02:57 +03:00
|
|
|
for (uint32_t i = 0; i < array.Length(); i++) {
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
StreamAndPromiseForOperation& s = array[i];
|
|
|
|
if ((aOperation == AsyncCubebOperation::INIT &&
|
2015-05-10 06:38:15 +03:00
|
|
|
s.mOperation == dom::AudioContextOperation::Resume) ||
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
(aOperation == AsyncCubebOperation::SHUTDOWN &&
|
2015-05-10 06:38:15 +03:00
|
|
|
s.mOperation != dom::AudioContextOperation::Resume)) {
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
|
|
|
|
GraphImpl()->AudioContextOperationCompleted(s.mStream,
|
|
|
|
s.mPromise,
|
|
|
|
s.mOperation);
|
|
|
|
array.RemoveElementAt(i);
|
2015-04-29 12:02:57 +03:00
|
|
|
i--;
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array.IsEmpty()) {
|
|
|
|
MonitorAutoLock mon(GraphImpl()->GetMonitor());
|
|
|
|
mPromisesForOperation.AppendElements(array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 13:57:02 +03:00
|
|
|
void AudioCallbackDriver::FallbackToSystemClockDriver()
|
|
|
|
{
|
|
|
|
GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
|
|
|
|
SystemClockDriver* nextDriver = new SystemClockDriver(GraphImpl());
|
|
|
|
SetNextDriver(nextDriver);
|
|
|
|
nextDriver->MarkAsFallback();
|
|
|
|
nextDriver->SetGraphTime(this, mIterationStart, mIterationEnd);
|
|
|
|
// We're not using SwitchAtNextIteration here, because there
|
|
|
|
// won't be a next iteration if we don't restart things manually:
|
|
|
|
// the audio stream just signaled that it's in error state.
|
|
|
|
mGraphImpl->SetCurrentDriver(nextDriver);
|
|
|
|
nextDriver->Start();
|
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2017-03-24 06:17:17 +03:00
|
|
|
|
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef LOG
|