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/. */
|
|
|
|
|
2020-06-01 18:53:18 +03:00
|
|
|
#include "GraphDriver.h"
|
|
|
|
|
|
|
|
#include "AudioNodeEngine.h"
|
2015-11-27 07:40:30 +03:00
|
|
|
#include "mozilla/dom/AudioContext.h"
|
2018-08-27 16:11:41 +03:00
|
|
|
#include "mozilla/dom/AudioDeviceInfo.h"
|
2019-12-19 01:50:52 +03:00
|
|
|
#include "mozilla/dom/BaseAudioContextBinding.h"
|
2020-04-07 18:16:33 +03:00
|
|
|
#include "mozilla/SchedulerGroup.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"
|
2019-07-29 15:50:39 +03:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2019-03-19 13:40:12 +03:00
|
|
|
#include "CubebDeviceEnumerator.h"
|
2020-06-01 18:53:18 +03:00
|
|
|
#include "MediaTrackGraphImpl.h"
|
2020-06-23 19:43:40 +03:00
|
|
|
#include "AudioThreadRegistry.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
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
extern mozilla::LazyLogModule gMediaTrackGraphLog;
|
2017-02-06 18:22:36 +03:00
|
|
|
#ifdef LOG
|
|
|
|
# undef LOG
|
|
|
|
#endif // LOG
|
2019-10-02 13:23:02 +03:00
|
|
|
#define LOG(type, msg) MOZ_LOG(gMediaTrackGraphLog, type, msg)
|
2014-08-31 16:19:48 +04:00
|
|
|
|
2014-04-25 18:09:30 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2019-12-19 01:51:57 +03:00
|
|
|
GraphDriver::GraphDriver(GraphInterface* aGraphInterface,
|
2019-12-19 01:51:25 +03:00
|
|
|
GraphDriver* aPreviousDriver, uint32_t aSampleRate)
|
2019-12-19 01:51:57 +03:00
|
|
|
: mGraphInterface(aGraphInterface),
|
2019-12-19 01:51:25 +03:00
|
|
|
mSampleRate(aSampleRate),
|
|
|
|
mPreviousDriver(aPreviousDriver) {}
|
2019-12-19 01:49:30 +03:00
|
|
|
|
2019-12-19 01:51:25 +03:00
|
|
|
void GraphDriver::SetState(GraphTime aIterationStart, GraphTime aIterationEnd,
|
2019-12-19 01:49:30 +03:00
|
|
|
GraphTime aStateComputedTime) {
|
2019-12-19 01:51:45 +03:00
|
|
|
MOZ_ASSERT(InIteration() || !ThreadRunning());
|
2019-12-19 01:49:30 +03:00
|
|
|
|
|
|
|
mIterationStart = aIterationStart;
|
|
|
|
mIterationEnd = aIterationEnd;
|
|
|
|
mStateComputedTime = aStateComputedTime;
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2019-03-06 23:12:25 +03:00
|
|
|
#ifdef DEBUG
|
2020-11-30 17:16:57 +03:00
|
|
|
bool GraphDriver::InIteration() const {
|
2019-12-19 01:52:33 +03:00
|
|
|
return OnThread() || Graph()->InDriverIteration(this);
|
|
|
|
}
|
2019-03-06 23:12:25 +03:00
|
|
|
#endif
|
|
|
|
|
2015-12-01 13:47:31 +03:00
|
|
|
GraphDriver* GraphDriver::PreviousDriver() {
|
2019-12-19 01:51:45 +03:00
|
|
|
MOZ_ASSERT(InIteration() || !ThreadRunning());
|
2015-12-01 13:47:31 +03:00
|
|
|
return mPreviousDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphDriver::SetPreviousDriver(GraphDriver* aPreviousDriver) {
|
2019-12-19 01:51:45 +03:00
|
|
|
MOZ_ASSERT(InIteration() || !ThreadRunning());
|
2015-12-01 13:47:31 +03:00
|
|
|
mPreviousDriver = aPreviousDriver;
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:51:57 +03:00
|
|
|
ThreadedDriver::ThreadedDriver(GraphInterface* aGraphInterface,
|
2019-12-19 01:51:25 +03:00
|
|
|
GraphDriver* aPreviousDriver,
|
2019-12-19 01:50:12 +03:00
|
|
|
uint32_t aSampleRate)
|
2019-12-19 01:51:57 +03:00
|
|
|
: GraphDriver(aGraphInterface, aPreviousDriver, aSampleRate),
|
2019-12-19 01:51:25 +03:00
|
|
|
mThreadRunning(false) {}
|
2014-09-03 17:52:43 +04:00
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
class MediaTrackGraphShutdownThreadRunnable : public Runnable {
|
2016-04-26 22:33:14 +03:00
|
|
|
public:
|
2019-10-02 13:23:02 +03:00
|
|
|
explicit MediaTrackGraphShutdownThreadRunnable(
|
2017-06-12 22:34:10 +03:00
|
|
|
already_AddRefed<nsIThread> aThread)
|
2019-10-02 13:23:02 +03:00
|
|
|
: Runnable("MediaTrackGraphShutdownThreadRunnable"), mThread(aThread) {}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2021-08-26 12:15:40 +03:00
|
|
|
TRACE("MediaTrackGraphShutdownThreadRunnable");
|
2016-04-26 22:33:14 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mThread);
|
|
|
|
|
|
|
|
mThread->Shutdown();
|
|
|
|
mThread = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2016-04-26 22:33:14 +03:00
|
|
|
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 =
|
2019-10-02 13:23:02 +03:00
|
|
|
new MediaTrackGraphShutdownThreadRunnable(mThread.forget());
|
2020-04-07 18:16:33 +03:00
|
|
|
SchedulerGroup::Dispatch(TaskCategory::Other, event.forget());
|
2014-09-03 17:52:43 +04:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 12:26:58 +03:00
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
class MediaTrackGraphInitThreadRunnable : public Runnable {
|
2014-04-25 20:03:04 +04:00
|
|
|
public:
|
2019-10-02 13:23:02 +03:00
|
|
|
explicit MediaTrackGraphInitThreadRunnable(ThreadedDriver* aDriver)
|
|
|
|
: Runnable("MediaTrackGraphInitThreadRunnable"), mDriver(aDriver) {}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2021-08-26 12:15:40 +03:00
|
|
|
TRACE("MediaTrackGraphInitThreadRunnable");
|
2018-05-25 11:58:45 +03:00
|
|
|
MOZ_ASSERT(!mDriver->ThreadRunning());
|
2018-07-03 08:23:09 +03:00
|
|
|
LOG(LogLevel::Debug, ("Starting a new system driver for graph %p",
|
2019-12-19 01:51:57 +03:00
|
|
|
mDriver->mGraphInterface.get()));
|
2015-12-01 13:47:31 +03:00
|
|
|
|
2019-12-19 01:51:25 +03:00
|
|
|
if (GraphDriver* previousDriver = mDriver->PreviousDriver()) {
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("%p releasing an AudioCallbackDriver(%p), for graph %p",
|
2019-12-19 01:51:57 +03:00
|
|
|
mDriver.get(), previousDriver, mDriver->Graph()));
|
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();
|
|
|
|
mDriver->SetPreviousDriver(nullptr);
|
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;
|
|
|
|
}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2014-04-25 20:03:04 +04:00
|
|
|
private:
|
2016-06-07 23:26:20 +03:00
|
|
|
RefPtr<ThreadedDriver> mDriver;
|
2014-04-25 20:03:04 +04:00
|
|
|
};
|
|
|
|
|
2014-04-25 20:04:53 +04:00
|
|
|
void ThreadedDriver::Start() {
|
2018-05-25 11:58:58 +03:00
|
|
|
MOZ_ASSERT(!ThreadRunning());
|
2018-07-03 08:23:09 +03:00
|
|
|
LOG(LogLevel::Debug,
|
2019-12-19 01:51:57 +03:00
|
|
|
("Starting thread for a SystemClockDriver %p", mGraphInterface.get()));
|
2016-03-29 04:00:21 +03:00
|
|
|
Unused << NS_WARN_IF(mThread);
|
2018-05-25 11:58:58 +03:00
|
|
|
MOZ_ASSERT(!mThread); // Ensure we haven't already started it
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaTrackGraphInitThreadRunnable(this);
|
2018-05-25 11:58:58 +03:00
|
|
|
// Note: mThread may be null during event->Run() if we pass to NewNamedThread!
|
|
|
|
// See AudioInitTask
|
2019-10-02 13:23:02 +03:00
|
|
|
nsresult rv = NS_NewNamedThread("MediaTrackGrph", getter_AddRefs(mThread));
|
2018-05-25 11:58:58 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mThread->EventTarget()->Dispatch(event.forget(), NS_DISPATCH_NORMAL);
|
2014-09-28 20:07:24 +04:00
|
|
|
}
|
2014-04-25 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
2017-09-28 05:05:36 +03:00
|
|
|
void 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
|
2019-10-02 13:23:02 +03:00
|
|
|
LOG(LogLevel::Debug, ("Stopping threads for MediaTrackGraph %p", this));
|
2014-04-25 20:03:04 +04:00
|
|
|
|
|
|
|
if (mThread) {
|
2018-04-17 18:11:13 +03:00
|
|
|
LOG(LogLevel::Debug,
|
2019-12-19 01:51:57 +03:00
|
|
|
("%p: Stopping ThreadedDriver's %p thread", Graph(), this));
|
2014-04-25 20:03:04 +04:00
|
|
|
mThread->Shutdown();
|
2014-09-28 20:07:24 +04:00
|
|
|
mThread = nullptr;
|
2014-04-25 20:03:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:51:57 +03:00
|
|
|
SystemClockDriver::SystemClockDriver(GraphInterface* aGraphInterface,
|
2019-12-19 01:51:25 +03:00
|
|
|
GraphDriver* aPreviousDriver,
|
2019-12-19 01:52:33 +03:00
|
|
|
uint32_t aSampleRate)
|
2019-12-19 01:51:57 +03:00
|
|
|
: ThreadedDriver(aGraphInterface, aPreviousDriver, aSampleRate),
|
2014-04-25 20:04:53 +04:00
|
|
|
mInitialTimeStamp(TimeStamp::Now()),
|
2019-03-22 14:41:46 +03:00
|
|
|
mCurrentTimeStamp(TimeStamp::Now()),
|
2019-12-19 01:52:33 +03:00
|
|
|
mLastTimeStamp(TimeStamp::Now()) {}
|
2016-07-27 16:18:17 +03:00
|
|
|
|
2020-03-04 18:39:20 +03:00
|
|
|
SystemClockDriver::~SystemClockDriver() = default;
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2014-04-25 20:04:53 +04:00
|
|
|
void ThreadedDriver::RunThread() {
|
2018-05-22 19:51:42 +03:00
|
|
|
mThreadRunning = true;
|
2017-10-17 08:14:43 +03:00
|
|
|
while (true) {
|
2019-12-19 01:52:24 +03:00
|
|
|
mIterationStart = mIterationEnd;
|
2015-07-23 08:15:49 +03:00
|
|
|
mIterationEnd += GetIntervalForIteration();
|
|
|
|
|
2019-12-19 01:49:30 +03:00
|
|
|
if (mStateComputedTime < mIterationEnd) {
|
2019-12-19 01:51:57 +03:00
|
|
|
LOG(LogLevel::Warning, ("%p: Global underrun detected", Graph()));
|
2019-12-19 01:49:30 +03:00
|
|
|
mIterationEnd = mStateComputedTime;
|
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?
|
2019-12-19 01:51:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("%p: Time did not advance", Graph()));
|
2015-07-23 08:15:49 +03:00
|
|
|
}
|
2014-04-25 20:04:23 +04:00
|
|
|
|
2019-12-19 01:50:45 +03:00
|
|
|
GraphTime nextStateComputedTime =
|
|
|
|
MediaTrackGraphImpl::RoundUpToEndOfAudioBlock(
|
|
|
|
mIterationEnd + MillisecondsToMediaTime(AUDIO_TARGET_MS));
|
2019-12-19 01:49:30 +03:00
|
|
|
if (nextStateComputedTime < mStateComputedTime) {
|
2015-08-04 10:54:54 +03:00
|
|
|
// A previous driver may have been processing further ahead of
|
|
|
|
// iterationEnd.
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Warning,
|
2018-04-17 18:11:13 +03:00
|
|
|
("%p: Prevent state from going backwards. interval[%ld; %ld] "
|
|
|
|
"state[%ld; "
|
2017-02-06 18:22:36 +03:00
|
|
|
"%ld]",
|
2019-12-19 01:51:57 +03:00
|
|
|
Graph(), (long)mIterationStart, (long)mIterationEnd,
|
2019-12-19 01:49:30 +03:00
|
|
|
(long)mStateComputedTime, (long)nextStateComputedTime));
|
|
|
|
nextStateComputedTime = mStateComputedTime;
|
2015-08-04 10:54:54 +03:00
|
|
|
}
|
2017-02-06 18:22:36 +03:00
|
|
|
LOG(LogLevel::Verbose,
|
2019-12-19 01:51:57 +03:00
|
|
|
("%p: interval[%ld; %ld] state[%ld; %ld]", Graph(),
|
2019-12-19 01:49:30 +03:00
|
|
|
(long)mIterationStart, (long)mIterationEnd, (long)mStateComputedTime,
|
2017-02-06 18:22:36 +03:00
|
|
|
(long)nextStateComputedTime));
|
2014-04-25 20:04:23 +04:00
|
|
|
|
2019-12-19 01:51:25 +03:00
|
|
|
mStateComputedTime = nextStateComputedTime;
|
2019-12-19 01:52:24 +03:00
|
|
|
IterationResult result =
|
|
|
|
Graph()->OneIteration(mStateComputedTime, mIterationEnd, nullptr);
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2019-12-19 01:51:05 +03:00
|
|
|
if (result.IsStop()) {
|
2019-12-19 01:51:12 +03:00
|
|
|
// Signal that we're done stopping.
|
|
|
|
result.Stopped();
|
2018-05-22 19:51:42 +03:00
|
|
|
break;
|
2017-10-17 08:14:43 +03:00
|
|
|
}
|
2019-12-19 01:49:03 +03:00
|
|
|
WaitForNextIteration();
|
2019-12-19 01:51:25 +03:00
|
|
|
if (GraphDriver* nextDriver = result.NextDriver()) {
|
2019-12-19 01:51:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("%p: Switching to AudioCallbackDriver", Graph()));
|
2019-12-19 01:51:25 +03:00
|
|
|
result.Switched();
|
|
|
|
nextDriver->SetState(mIterationStart, mIterationEnd, mStateComputedTime);
|
|
|
|
nextDriver->Start();
|
2018-05-22 19:51:42 +03:00
|
|
|
break;
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
2019-12-19 01:51:25 +03:00
|
|
|
MOZ_ASSERT(result.IsStillProcessing());
|
2014-04-25 20:04:23 +04:00
|
|
|
}
|
2018-05-22 19:51:42 +03:00
|
|
|
mThreadRunning = false;
|
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 =
|
2019-12-19 01:50:45 +03:00
|
|
|
SecondsToMediaTime((now - mCurrentTimeStamp).ToSeconds());
|
2014-04-25 18:09:30 +04:00
|
|
|
mCurrentTimeStamp = now;
|
|
|
|
|
2019-12-19 01:50:45 +03:00
|
|
|
MOZ_LOG(gMediaTrackGraphLog, LogLevel::Verbose,
|
|
|
|
("%p: Updating current time to %f (real %f, StateComputedTime() %f)",
|
2019-12-19 01:52:24 +03:00
|
|
|
Graph(), MediaTimeToSeconds(mIterationEnd + interval),
|
2019-12-19 01:50:45 +03:00
|
|
|
(now - mInitialTimeStamp).ToSeconds(),
|
|
|
|
MediaTimeToSeconds(mStateComputedTime)));
|
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
|
|
|
}
|
|
|
|
|
2019-12-19 01:49:52 +03:00
|
|
|
void ThreadedDriver::EnsureNextIteration() {
|
|
|
|
mWaitHelper.EnsureNextIteration();
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
2019-12-19 01:49:52 +03:00
|
|
|
void ThreadedDriver::WaitForNextIteration() {
|
|
|
|
MOZ_ASSERT(mThread);
|
|
|
|
MOZ_ASSERT(OnThread());
|
|
|
|
mWaitHelper.WaitForNextIterationAtLeast(WaitInterval());
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
2018-10-30 12:48:08 +03:00
|
|
|
TimeDuration SystemClockDriver::WaitInterval() {
|
2019-12-19 01:49:52 +03:00
|
|
|
MOZ_ASSERT(mThread);
|
|
|
|
MOZ_ASSERT(OnThread());
|
2018-10-30 12:48:08 +03:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
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));
|
|
|
|
LOG(LogLevel::Verbose,
|
2019-12-19 01:51:57 +03:00
|
|
|
("%p: Waiting for next iteration; at %f, timeout=%f", Graph(),
|
2018-10-30 12:48:08 +03:00
|
|
|
(now - mInitialTimeStamp).ToSeconds(), timeoutMS / 1000.0));
|
|
|
|
|
|
|
|
return TimeDuration::FromMilliseconds(timeoutMS);
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:51:57 +03:00
|
|
|
OfflineClockDriver::OfflineClockDriver(GraphInterface* aGraphInterface,
|
2019-12-19 01:50:12 +03:00
|
|
|
uint32_t aSampleRate, GraphTime aSlice)
|
2019-12-19 01:51:57 +03:00
|
|
|
: ThreadedDriver(aGraphInterface, nullptr, aSampleRate), mSlice(aSlice) {}
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2020-03-04 18:39:20 +03:00
|
|
|
OfflineClockDriver::~OfflineClockDriver() = default;
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2020-03-03 21:16:54 +03:00
|
|
|
void OfflineClockDriver::RunThread() {
|
|
|
|
nsCOMPtr<nsIThreadInternal> threadInternal = do_QueryInterface(mThread);
|
|
|
|
nsCOMPtr<nsIThreadObserver> observer = do_QueryInterface(Graph());
|
|
|
|
threadInternal->SetObserver(observer);
|
|
|
|
|
|
|
|
ThreadedDriver::RunThread();
|
|
|
|
}
|
|
|
|
|
2015-07-23 08:15:49 +03:00
|
|
|
MediaTime OfflineClockDriver::GetIntervalForIteration() {
|
2019-12-19 01:50:45 +03:00
|
|
|
return MillisecondsToMediaTime(mSlice);
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
AsyncCubebTask::AsyncCubebTask(AudioCallbackDriver* aDriver,
|
|
|
|
AsyncCubebOperation aOperation)
|
|
|
|
: Runnable("AsyncCubebTask"),
|
|
|
|
mDriver(aDriver),
|
|
|
|
mOperation(aOperation),
|
2019-12-19 01:51:57 +03:00
|
|
|
mShutdownGrip(aDriver->Graph()) {
|
2019-04-19 11:34:22 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
mDriver->mAudioStream || aOperation == AsyncCubebOperation::INIT,
|
|
|
|
"No audio stream!");
|
2014-09-03 17:52:43 +04:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:39:20 +03:00
|
|
|
AsyncCubebTask::~AsyncCubebTask() = default;
|
2014-04-25 18:09:30 +04:00
|
|
|
|
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: {
|
2018-04-17 18:11:13 +03:00
|
|
|
LOG(LogLevel::Debug, ("%p: AsyncCubebOperation::INIT driver=%p",
|
2019-12-19 01:51:57 +03:00
|
|
|
mDriver->Graph(), mDriver.get()));
|
2019-12-19 16:17:32 +03:00
|
|
|
mDriver->Init();
|
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: {
|
2018-04-17 18:11:13 +03:00
|
|
|
LOG(LogLevel::Debug, ("%p: AsyncCubebOperation::SHUTDOWN driver=%p",
|
2019-12-19 01:51:57 +03:00
|
|
|
mDriver->Graph(), mDriver.get()));
|
2014-08-26 19:02:07 +04:00
|
|
|
mDriver->Stop();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
TrackAndPromiseForOperation::TrackAndPromiseForOperation(
|
2019-12-19 01:50:52 +03:00
|
|
|
MediaTrack* aTrack, dom::AudioContextOperation aOperation,
|
|
|
|
AbstractThread* aMainThread,
|
|
|
|
MozPromiseHolder<MediaTrackGraph::AudioContextOperationPromise>&& aHolder)
|
2019-10-02 13:23:02 +03:00
|
|
|
: mTrack(aTrack),
|
2019-04-02 14:10:02 +03:00
|
|
|
mOperation(aOperation),
|
2019-12-19 01:50:52 +03:00
|
|
|
mMainThread(aMainThread),
|
|
|
|
mHolder(std::move(aHolder)) {}
|
|
|
|
|
|
|
|
TrackAndPromiseForOperation::TrackAndPromiseForOperation(
|
|
|
|
TrackAndPromiseForOperation&& aOther) noexcept
|
|
|
|
: mTrack(std::move(aOther.mTrack)),
|
|
|
|
mOperation(aOther.mOperation),
|
|
|
|
mMainThread(std::move(aOther.mMainThread)),
|
|
|
|
mHolder(std::move(aOther.mHolder)) {}
|
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
|
|
|
|
2019-12-19 01:52:33 +03:00
|
|
|
/* Helper to proxy the GraphInterface methods used by a running
|
|
|
|
* mFallbackDriver. */
|
|
|
|
class AudioCallbackDriver::FallbackWrapper : public GraphInterface {
|
|
|
|
public:
|
|
|
|
FallbackWrapper(RefPtr<GraphInterface> aGraph,
|
|
|
|
RefPtr<AudioCallbackDriver> aOwner, uint32_t aSampleRate,
|
|
|
|
GraphTime aIterationStart, GraphTime aIterationEnd,
|
|
|
|
GraphTime aStateComputedTime)
|
|
|
|
: mGraph(std::move(aGraph)),
|
|
|
|
mOwner(std::move(aOwner)),
|
|
|
|
mFallbackDriver(
|
|
|
|
MakeRefPtr<SystemClockDriver>(this, nullptr, aSampleRate)),
|
|
|
|
mIterationStart(aIterationStart),
|
|
|
|
mIterationEnd(aIterationEnd),
|
|
|
|
mStateComputedTime(aStateComputedTime) {
|
|
|
|
mFallbackDriver->SetState(mIterationStart, mIterationEnd,
|
|
|
|
mStateComputedTime);
|
|
|
|
}
|
|
|
|
|
2020-03-03 21:16:21 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2019-12-19 01:52:33 +03:00
|
|
|
|
|
|
|
/* Proxied SystemClockDriver methods */
|
|
|
|
void SetState(GraphTime aIterationStart, GraphTime aIterationEnd,
|
|
|
|
GraphTime aStateComputedTime) {
|
|
|
|
mIterationStart = aIterationStart;
|
|
|
|
mIterationEnd = aIterationEnd;
|
|
|
|
mStateComputedTime = aStateComputedTime;
|
|
|
|
mFallbackDriver->SetState(aIterationStart, aIterationEnd,
|
|
|
|
aStateComputedTime);
|
|
|
|
}
|
|
|
|
void Start() { mFallbackDriver->Start(); }
|
|
|
|
MOZ_CAN_RUN_SCRIPT void Shutdown() {
|
|
|
|
RefPtr<SystemClockDriver> driver = mFallbackDriver;
|
|
|
|
driver->Shutdown();
|
|
|
|
}
|
|
|
|
void EnsureNextIteration() { mFallbackDriver->EnsureNextIteration(); }
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool InIteration() { return mFallbackDriver->InIteration(); }
|
|
|
|
#endif
|
|
|
|
bool OnThread() { return mFallbackDriver->OnThread(); }
|
|
|
|
|
|
|
|
/* GraphInterface methods */
|
|
|
|
void NotifyOutputData(AudioDataValue* aBuffer, size_t aFrames,
|
|
|
|
TrackRate aRate, uint32_t aChannels) override {
|
|
|
|
MOZ_CRASH("Unexpected NotifyOutputData from fallback SystemClockDriver");
|
|
|
|
}
|
2020-11-05 19:43:24 +03:00
|
|
|
void NotifyInputStopped() override {
|
|
|
|
MOZ_CRASH("Unexpected NotifyInputStopped from fallback SystemClockDriver");
|
2019-12-19 01:53:37 +03:00
|
|
|
}
|
2019-12-19 01:52:33 +03:00
|
|
|
void NotifyInputData(const AudioDataValue* aBuffer, size_t aFrames,
|
2020-11-05 19:44:00 +03:00
|
|
|
TrackRate aRate, uint32_t aChannels,
|
|
|
|
uint32_t aAlreadyBuffered) override {
|
2019-12-19 01:52:33 +03:00
|
|
|
MOZ_CRASH("Unexpected NotifyInputData from fallback SystemClockDriver");
|
|
|
|
}
|
|
|
|
void DeviceChanged() override {
|
|
|
|
MOZ_CRASH("Unexpected DeviceChanged from fallback SystemClockDriver");
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2020-11-30 17:16:57 +03:00
|
|
|
bool InDriverIteration(const GraphDriver* aDriver) const override {
|
2019-12-19 01:52:33 +03:00
|
|
|
return !mOwner->ThreadRunning() && mOwner->InIteration();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
IterationResult OneIteration(GraphTime aStateComputedEnd,
|
|
|
|
GraphTime aIterationEnd,
|
|
|
|
AudioMixer* aMixer) override {
|
|
|
|
MOZ_ASSERT(!aMixer);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
AutoInCallback aic(mOwner);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mIterationStart = mIterationEnd;
|
|
|
|
mIterationEnd = aIterationEnd;
|
|
|
|
mStateComputedTime = aStateComputedEnd;
|
|
|
|
IterationResult result =
|
|
|
|
mGraph->OneIteration(aStateComputedEnd, aIterationEnd, aMixer);
|
|
|
|
|
2019-12-19 01:53:28 +03:00
|
|
|
AudioStreamState audioState = mOwner->mAudioStreamState;
|
|
|
|
|
|
|
|
MOZ_ASSERT(audioState != AudioStreamState::Stopping,
|
|
|
|
"The audio driver can only enter stopping if it iterated the "
|
|
|
|
"graph, which it can only do if there's no fallback driver");
|
|
|
|
if (audioState != AudioStreamState::Running && result.IsStillProcessing()) {
|
2020-10-28 03:58:15 +03:00
|
|
|
mOwner->MaybeStartAudioStream();
|
2019-12-19 01:52:33 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:53:28 +03:00
|
|
|
MOZ_ASSERT(result.IsStillProcessing() || result.IsStop() ||
|
|
|
|
result.IsSwitchDriver());
|
2019-12-19 01:52:33 +03:00
|
|
|
|
|
|
|
// Proxy the release of the fallback driver to a background thread, so it
|
|
|
|
// doesn't perform unexpected suicide.
|
|
|
|
IterationResult stopFallback =
|
|
|
|
IterationResult::CreateStop(NS_NewRunnableFunction(
|
|
|
|
"AudioCallbackDriver::FallbackDriverStopped",
|
|
|
|
[self = RefPtr<FallbackWrapper>(this), this,
|
|
|
|
result = std::move(result)]() mutable {
|
2019-12-19 01:53:28 +03:00
|
|
|
FallbackDriverState fallbackState =
|
|
|
|
result.IsStillProcessing() ? FallbackDriverState::None
|
|
|
|
: FallbackDriverState::Stopped;
|
2019-12-19 01:52:33 +03:00
|
|
|
mOwner->FallbackDriverStopped(mIterationStart, mIterationEnd,
|
2019-12-19 01:53:28 +03:00
|
|
|
mStateComputedTime, fallbackState);
|
|
|
|
|
|
|
|
if (fallbackState == FallbackDriverState::Stopped) {
|
2019-12-19 01:52:33 +03:00
|
|
|
#ifdef DEBUG
|
2019-12-19 01:53:28 +03:00
|
|
|
// The AudioCallbackDriver may not iterate the graph, but we'll
|
|
|
|
// call into it so we need to be regarded as "in iteration".
|
|
|
|
AutoInCallback aic(mOwner);
|
2019-12-19 01:52:33 +03:00
|
|
|
#endif
|
2019-12-19 01:53:28 +03:00
|
|
|
if (GraphDriver* nextDriver = result.NextDriver()) {
|
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("%p: Switching from fallback to other driver.",
|
|
|
|
mGraph.get()));
|
|
|
|
result.Switched();
|
|
|
|
nextDriver->SetState(mIterationStart, mIterationEnd,
|
|
|
|
mStateComputedTime);
|
|
|
|
nextDriver->Start();
|
|
|
|
} else if (result.IsStop()) {
|
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("%p: Stopping fallback driver.", mGraph.get()));
|
|
|
|
result.Stopped();
|
|
|
|
}
|
2019-12-19 01:52:33 +03:00
|
|
|
}
|
|
|
|
mOwner = nullptr;
|
|
|
|
NS_DispatchBackgroundTask(NS_NewRunnableFunction(
|
|
|
|
"AudioCallbackDriver::FallbackDriverStopped::Release",
|
|
|
|
[fallback = std::move(self->mFallbackDriver)] {}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
return stopFallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~FallbackWrapper() = default;
|
|
|
|
|
|
|
|
const RefPtr<GraphInterface> mGraph;
|
|
|
|
// Valid until mFallbackDriver has finished its last iteration.
|
|
|
|
RefPtr<AudioCallbackDriver> mOwner;
|
|
|
|
RefPtr<SystemClockDriver> mFallbackDriver;
|
|
|
|
|
|
|
|
GraphTime mIterationStart;
|
|
|
|
GraphTime mIterationEnd;
|
|
|
|
GraphTime mStateComputedTime;
|
|
|
|
};
|
|
|
|
|
2020-03-03 21:16:21 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(AudioCallbackDriver::FallbackWrapper)
|
|
|
|
|
2019-12-19 01:50:25 +03:00
|
|
|
AudioCallbackDriver::AudioCallbackDriver(
|
2019-12-19 01:51:57 +03:00
|
|
|
GraphInterface* aGraphInterface, GraphDriver* aPreviousDriver,
|
2019-12-19 01:51:25 +03:00
|
|
|
uint32_t aSampleRate, uint32_t aOutputChannelCount,
|
|
|
|
uint32_t aInputChannelCount, CubebUtils::AudioDeviceID aOutputDeviceID,
|
2019-12-19 01:50:25 +03:00
|
|
|
CubebUtils::AudioDeviceID aInputDeviceID, AudioInputType aAudioInputType)
|
2019-12-19 01:51:57 +03:00
|
|
|
: GraphDriver(aGraphInterface, aPreviousDriver, aSampleRate),
|
2020-04-07 17:33:57 +03:00
|
|
|
mOutputChannelCount(aOutputChannelCount),
|
2018-04-30 17:01:56 +03:00
|
|
|
mInputChannelCount(aInputChannelCount),
|
2019-12-19 01:50:25 +03:00
|
|
|
mOutputDeviceID(aOutputDeviceID),
|
|
|
|
mInputDeviceID(aInputDeviceID),
|
2014-09-09 20:16:01 +04:00
|
|
|
mIterationDurationMS(MEDIA_GRAPH_TARGET_PERIOD_MS),
|
2014-08-26 19:02:08 +04:00
|
|
|
mStarted(false),
|
2022-04-18 21:45:35 +03:00
|
|
|
mInitShutdownThread(CUBEB_TASK_THREAD),
|
2021-07-21 07:16:59 +03:00
|
|
|
mAudioThreadId(ProfilerThreadId{}),
|
2020-06-12 16:32:49 +03:00
|
|
|
mAudioThreadIdInCb(std::thread::id()),
|
2019-12-19 01:53:28 +03:00
|
|
|
mAudioStreamState(AudioStreamState::None),
|
2020-06-12 16:33:04 +03:00
|
|
|
mFallback("AudioCallbackDriver::mFallback"),
|
|
|
|
mSandboxed(CubebUtils::SandboxEnabled()) {
|
2021-12-18 18:09:45 +03:00
|
|
|
LOG(LogLevel::Debug, ("%p: AudioCallbackDriver %p ctor - input: device %p, "
|
|
|
|
"channel %d, output: device %p, channel %d",
|
|
|
|
Graph(), this, mInputDeviceID, mInputChannelCount,
|
|
|
|
mOutputDeviceID, mOutputChannelCount));
|
2018-05-25 11:58:45 +03:00
|
|
|
|
2020-04-07 17:33:57 +03:00
|
|
|
NS_WARNING_ASSERTION(mOutputChannelCount != 0,
|
|
|
|
"Invalid output channel count");
|
|
|
|
MOZ_ASSERT(mOutputChannelCount <= 8);
|
2019-12-19 01:52:33 +03:00
|
|
|
|
2018-05-25 11:58:45 +03:00
|
|
|
const uint32_t kIdleThreadTimeoutMs = 2000;
|
|
|
|
mInitShutdownThread->SetIdleThreadTimeout(
|
|
|
|
PR_MillisecondsToInterval(kIdleThreadTimeoutMs));
|
2018-04-17 18:11:13 +03:00
|
|
|
|
2019-04-16 18:42:42 +03:00
|
|
|
if (aAudioInputType == AudioInputType::Voice) {
|
|
|
|
LOG(LogLevel::Debug, ("VOICE."));
|
|
|
|
mInputDevicePreference = CUBEB_DEVICE_PREF_VOICE;
|
|
|
|
CubebUtils::SetInCommunication(true);
|
|
|
|
} else {
|
|
|
|
mInputDevicePreference = CUBEB_DEVICE_PREF_ALL;
|
|
|
|
}
|
2019-12-19 01:50:00 +03:00
|
|
|
|
|
|
|
mMixer.AddCallback(WrapNotNull(this));
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioCallbackDriver::~AudioCallbackDriver() {
|
2019-04-16 18:42:42 +03:00
|
|
|
if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE) {
|
|
|
|
CubebUtils::SetInCommunication(false);
|
|
|
|
}
|
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
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-14 22:13:30 +03:00
|
|
|
return false;
|
2016-10-14 19:35:35 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:17:32 +03:00
|
|
|
void AudioCallbackDriver::Init() {
|
2021-08-26 12:15:40 +03:00
|
|
|
TRACE("AudioCallbackDriver::Init");
|
2019-12-19 01:52:33 +03:00
|
|
|
MOZ_ASSERT(OnCubebOperationThread());
|
2019-12-19 01:53:28 +03:00
|
|
|
MOZ_ASSERT(mAudioStreamState == AudioStreamState::Pending);
|
|
|
|
FallbackDriverState fallbackState = mFallbackDriverState;
|
|
|
|
if (fallbackState == FallbackDriverState::Stopped) {
|
2019-12-19 16:17:32 +03:00
|
|
|
// The graph has already stopped us.
|
|
|
|
return;
|
2019-12-19 01:52:33 +03:00
|
|
|
}
|
2019-12-19 01:53:28 +03:00
|
|
|
bool fromFallback = fallbackState == FallbackDriverState::Running;
|
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__));
|
2019-12-19 01:53:28 +03:00
|
|
|
mAudioStreamState = AudioStreamState::None;
|
2019-12-19 01:52:33 +03:00
|
|
|
if (!fromFallback) {
|
2016-09-16 03:54:24 +03:00
|
|
|
CubebUtils::ReportCubebStreamInitFailure(true);
|
2019-12-19 01:52:33 +03:00
|
|
|
FallbackToSystemClockDriver();
|
2016-09-16 03:54:24 +03:00
|
|
|
}
|
2019-12-19 16:17:32 +03:00
|
|
|
return;
|
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.");
|
|
|
|
|
2019-12-19 01:50:12 +03:00
|
|
|
output.rate = mSampleRate;
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2021-09-29 06:46:41 +03:00
|
|
|
#ifdef MOZ_SAMPLE_TYPE_S16
|
|
|
|
MOZ_ASSERT(AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16);
|
|
|
|
output.format = CUBEB_SAMPLE_S16NE;
|
|
|
|
#else
|
|
|
|
MOZ_ASSERT(AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_FLOAT32);
|
|
|
|
output.format = CUBEB_SAMPLE_FLOAT32NE;
|
|
|
|
#endif
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2020-04-07 17:33:57 +03:00
|
|
|
if (!mOutputChannelCount) {
|
2017-11-28 13:57:02 +03:00
|
|
|
LOG(LogLevel::Warning, ("Output number of channels is 0."));
|
2019-12-19 01:53:28 +03:00
|
|
|
mAudioStreamState = AudioStreamState::None;
|
2019-12-19 01:52:33 +03:00
|
|
|
if (!fromFallback) {
|
|
|
|
CubebUtils::ReportCubebStreamInitFailure(firstStream);
|
|
|
|
FallbackToSystemClockDriver();
|
|
|
|
}
|
2019-12-19 16:17:32 +03:00
|
|
|
return;
|
2017-11-28 13:57:02 +03:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:16:30 +03:00
|
|
|
CubebUtils::AudioDeviceID forcedOutputDeviceId = nullptr;
|
|
|
|
|
|
|
|
char* forcedOutputDeviceName = CubebUtils::GetForcedOutputDevice();
|
|
|
|
if (forcedOutputDeviceName) {
|
2019-03-19 13:40:12 +03:00
|
|
|
RefPtr<CubebDeviceEnumerator> enumerator = Enumerator::GetInstance();
|
|
|
|
RefPtr<AudioDeviceInfo> device = enumerator->DeviceInfoFromName(
|
|
|
|
NS_ConvertUTF8toUTF16(forcedOutputDeviceName), EnumeratorSide::OUTPUT);
|
2019-03-25 15:18:25 +03:00
|
|
|
if (device && device->DeviceID()) {
|
2019-03-19 13:40:12 +03:00
|
|
|
forcedOutputDeviceId = device->DeviceID();
|
2018-08-01 15:16:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:33:57 +03:00
|
|
|
mBuffer = AudioCallbackBufferWrapper<AudioDataValue>(mOutputChannelCount);
|
2017-09-28 17:57:46 +03:00
|
|
|
mScratchBuffer =
|
2020-04-07 17:33:57 +03:00
|
|
|
SpillBuffer<AudioDataValue, WEBAUDIO_BLOCK_SIZE * 2>(mOutputChannelCount);
|
2017-09-28 17:57:46 +03:00
|
|
|
|
2020-04-07 17:33:57 +03:00
|
|
|
output.channels = mOutputChannelCount;
|
2019-11-19 21:23:21 +03:00
|
|
|
AudioConfig::ChannelLayout::ChannelMap channelMap =
|
2020-04-07 17:33:57 +03:00
|
|
|
AudioConfig::ChannelLayout(mOutputChannelCount).Map();
|
2019-11-19 21:23:21 +03:00
|
|
|
|
|
|
|
output.layout = static_cast<uint32_t>(channelMap);
|
2020-10-20 23:03:26 +03:00
|
|
|
output.prefs = CubebUtils::GetDefaultStreamPrefs(CUBEB_DEVICE_TYPE_OUTPUT);
|
2020-05-12 11:31:58 +03:00
|
|
|
if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE &&
|
|
|
|
CubebUtils::RouteOutputAsVoice()) {
|
2019-04-16 18:42:42 +03:00
|
|
|
output.prefs |= static_cast<cubeb_stream_prefs>(CUBEB_STREAM_PREF_VOICE);
|
|
|
|
}
|
2017-01-20 17:54:00 +03:00
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
uint32_t latencyFrames = CubebUtils::GetCubebMTGLatencyInFrames(&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
|
2019-10-02 13:23:02 +03:00
|
|
|
// MediaTrackGraphs, cap the minimal latency to 512 frames int this case.
|
2016-10-14 19:35:35 +03:00
|
|
|
if (IsMacbookOrMacbookAir()) {
|
2019-07-29 15:50:44 +03:00
|
|
|
latencyFrames = std::max((uint32_t)512, latencyFrames);
|
2016-10-14 19:35:35 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 15:50:39 +03:00
|
|
|
// On OSX, having a latency that is lower than 10ms is very common. It's
|
|
|
|
// not very useful when doing voice, because all the WebRTC code deal in 10ms
|
|
|
|
// chunks of audio. Take the first power of two above 10ms at the current
|
|
|
|
// rate in this case. It's probably 512, for common rates.
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE) {
|
2019-07-29 15:50:44 +03:00
|
|
|
if (latencyFrames < mSampleRate / 100) {
|
|
|
|
latencyFrames = mozilla::RoundUpPow2(mSampleRate / 100);
|
2019-07-29 15:50:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-29 15:50:44 +03:00
|
|
|
LOG(LogLevel::Debug, ("Effective latency in frames: %d", latencyFrames));
|
2019-07-29 15:50:39 +03:00
|
|
|
|
2017-06-01 19:26:17 +03:00
|
|
|
input = output;
|
2018-04-30 17:01:56 +03:00
|
|
|
input.channels = mInputChannelCount;
|
2017-06-02 09:11:56 +03:00
|
|
|
input.layout = CUBEB_LAYOUT_UNDEFINED;
|
2020-10-20 23:03:26 +03:00
|
|
|
input.prefs = CubebUtils::GetDefaultStreamPrefs(CUBEB_DEVICE_TYPE_INPUT);
|
2020-05-12 11:31:58 +03:00
|
|
|
if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE) {
|
|
|
|
input.prefs |= static_cast<cubeb_stream_prefs>(CUBEB_STREAM_PREF_VOICE);
|
|
|
|
}
|
2017-06-02 09:11:56 +03:00
|
|
|
|
2016-03-08 20:11:09 +03:00
|
|
|
cubeb_stream* stream = nullptr;
|
2018-04-30 17:01:56 +03:00
|
|
|
bool inputWanted = mInputChannelCount > 0;
|
2019-12-19 01:50:25 +03:00
|
|
|
CubebUtils::AudioDeviceID outputId = mOutputDeviceID;
|
|
|
|
CubebUtils::AudioDeviceID inputId = mInputDeviceID;
|
2018-04-30 17:01:56 +03:00
|
|
|
|
2022-05-24 14:41:07 +03:00
|
|
|
if (CubebUtils::CubebStreamInit(
|
|
|
|
cubebContext, &stream, "AudioCallbackDriver", inputId,
|
|
|
|
inputWanted ? &input : nullptr,
|
|
|
|
forcedOutputDeviceId ? forcedOutputDeviceId : outputId, &output,
|
|
|
|
latencyFrames, DataCallback_s, StateCallback_s, this) == CUBEB_OK) {
|
2018-04-30 17:01:56 +03:00
|
|
|
mAudioStream.own(stream);
|
|
|
|
DebugOnly<int> rv =
|
|
|
|
cubeb_stream_set_volume(mAudioStream, CubebUtils::GetVolumeScale());
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
rv == CUBEB_OK,
|
|
|
|
"Could not set the audio stream volume in GraphDriver.cpp");
|
|
|
|
CubebUtils::ReportCubebBackendUsed();
|
|
|
|
} else {
|
|
|
|
NS_WARNING(
|
2019-10-02 13:23:02 +03:00
|
|
|
"Could not create a cubeb stream for MediaTrackGraph, falling "
|
2018-04-30 17:01:56 +03:00
|
|
|
"back to a SystemClockDriver");
|
2019-12-19 01:53:28 +03:00
|
|
|
mAudioStreamState = AudioStreamState::None;
|
2018-04-30 17:01:56 +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.
|
2019-12-19 01:52:33 +03:00
|
|
|
if (!fromFallback) {
|
2018-04-30 17:01:56 +03:00
|
|
|
CubebUtils::ReportCubebStreamInitFailure(firstStream);
|
2019-12-19 01:52:33 +03:00
|
|
|
FallbackToSystemClockDriver();
|
2016-04-01 07:18:13 +03:00
|
|
|
}
|
2019-12-19 16:17:32 +03:00
|
|
|
return;
|
2014-08-26 19:02:07 +04:00
|
|
|
}
|
2016-04-13 21:31:35 +03:00
|
|
|
|
2018-04-30 17:01:56 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
PanOutputIfNeeded(inputWanted);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cubeb_stream_register_device_changed_callback(
|
|
|
|
mAudioStream, AudioCallbackDriver::DeviceChangedCallback_s);
|
2014-08-26 19:02:31 +04:00
|
|
|
|
2020-04-07 17:33:57 +03:00
|
|
|
// No-op if MOZ_DUMP_AUDIO is not defined as an environment variable. This
|
|
|
|
// is intended for diagnosing issues, and only works if the content sandbox is
|
|
|
|
// disabled.
|
|
|
|
mInputStreamFile.Open("GraphDriverInput", input.channels, input.rate);
|
|
|
|
mOutputStreamFile.Open("GraphDriverOutput", output.channels, output.rate);
|
|
|
|
|
2019-12-19 16:17:32 +03:00
|
|
|
if (NS_WARN_IF(!StartStream())) {
|
2018-04-30 17:01:56 +03:00
|
|
|
LOG(LogLevel::Warning,
|
2019-12-19 01:51:57 +03:00
|
|
|
("%p: AudioCallbackDriver couldn't start a cubeb stream.", Graph()));
|
2019-12-19 16:17:32 +03:00
|
|
|
return;
|
2017-01-11 22:51:23 +03:00
|
|
|
}
|
2014-08-26 19:02:07 +04:00
|
|
|
|
2019-12-19 01:51:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("%p: AudioCallbackDriver started.", Graph()));
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 19:02:07 +04:00
|
|
|
void AudioCallbackDriver::Start() {
|
2018-05-25 11:58:45 +03:00
|
|
|
MOZ_ASSERT(!IsStarted());
|
2019-12-19 01:53:28 +03:00
|
|
|
MOZ_ASSERT(mAudioStreamState == AudioStreamState::None);
|
|
|
|
MOZ_ASSERT_IF(PreviousDriver(), PreviousDriver()->InIteration());
|
|
|
|
mAudioStreamState = AudioStreamState::Pending;
|
2019-12-19 01:53:33 +03:00
|
|
|
|
|
|
|
if (mFallbackDriverState == FallbackDriverState::None) {
|
|
|
|
// Starting an audio driver could take a while. We start a system driver in
|
|
|
|
// the meantime so that the graph is kept running.
|
|
|
|
FallbackToSystemClockDriver();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
} 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());
|
2016-01-22 04:28:23 +03:00
|
|
|
}
|
2019-12-19 01:51:25 +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
|
|
|
}
|
|
|
|
|
2014-08-26 19:02:30 +04:00
|
|
|
bool AudioCallbackDriver::StartStream() {
|
2021-08-26 12:15:40 +03:00
|
|
|
TRACE("AudioCallbackDriver::StartStream");
|
2018-05-25 11:58:45 +03:00
|
|
|
MOZ_ASSERT(!IsStarted() && OnCubebOperationThread());
|
2019-12-19 01:49:43 +03:00
|
|
|
// Set mStarted before cubeb_stream_start, since starting the cubeb stream can
|
|
|
|
// result in a callback (that may read mStarted) before mStarted would
|
|
|
|
// otherwise be set to true.
|
|
|
|
mStarted = true;
|
2014-08-26 19:02:30 +04:00
|
|
|
if (cubeb_stream_start(mAudioStream) != CUBEB_OK) {
|
2019-10-02 13:23:02 +03:00
|
|
|
NS_WARNING("Could not start cubeb stream for MTG.");
|
2019-12-19 01:49:43 +03:00
|
|
|
mStarted = false;
|
2017-01-11 22:51:23 +03:00
|
|
|
return false;
|
2014-08-26 19:02:30 +04:00
|
|
|
}
|
|
|
|
|
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() {
|
2021-08-26 12:15:40 +03:00
|
|
|
TRACE("AudioCallbackDriver::Stop");
|
2018-05-25 11:58:45 +03:00
|
|
|
MOZ_ASSERT(OnCubebOperationThread());
|
2021-07-06 17:12:15 +03:00
|
|
|
cubeb_stream_register_device_changed_callback(mAudioStream, nullptr);
|
2014-08-26 19:01:33 +04:00
|
|
|
if (cubeb_stream_stop(mAudioStream) != CUBEB_OK) {
|
2019-10-02 13:23:02 +03:00
|
|
|
NS_WARNING("Could not stop cubeb stream for MTG.");
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 05:05:36 +03:00
|
|
|
void AudioCallbackDriver::Shutdown() {
|
2018-05-25 11:58:45 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-12-19 01:52:33 +03:00
|
|
|
RefPtr<FallbackWrapper> fallback;
|
|
|
|
{
|
|
|
|
auto fallbackLock = mFallback.Lock();
|
|
|
|
fallback = fallbackLock.ref();
|
|
|
|
fallbackLock.ref() = nullptr;
|
|
|
|
}
|
|
|
|
if (fallback) {
|
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("%p: Releasing fallback driver %p.", Graph(), fallback.get()));
|
|
|
|
fallback->Shutdown();
|
|
|
|
}
|
|
|
|
|
2017-09-28 05:05:36 +03:00
|
|
|
LOG(LogLevel::Debug,
|
2018-04-17 18:11:13 +03:00
|
|
|
("%p: Releasing audio driver off main thread (GraphDriver::Shutdown).",
|
2019-12-19 01:51:57 +03:00
|
|
|
Graph()));
|
2021-03-24 13:25:45 +03:00
|
|
|
|
2017-09-28 05:05:36 +03:00
|
|
|
RefPtr<AsyncCubebTask> releaseEvent =
|
|
|
|
new AsyncCubebTask(this, AsyncCubebOperation::SHUTDOWN);
|
|
|
|
releaseEvent->Dispatch(NS_DISPATCH_SYNC);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
long AudioCallbackDriver::DataCallback_s(cubeb_stream* aStream, void* aUser,
|
|
|
|
const void* aInputBuffer,
|
|
|
|
void* aOutputBuffer, long aFrames) {
|
2014-08-26 19:01:33 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void AudioCallbackDriver::StateCallback_s(cubeb_stream* aStream, void* aUser,
|
|
|
|
cubeb_state aState) {
|
2014-08-26 19:01:33 +04:00
|
|
|
AudioCallbackDriver* driver = reinterpret_cast<AudioCallbackDriver*>(aUser);
|
|
|
|
driver->StateCallback(aState);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void AudioCallbackDriver::DeviceChangedCallback_s(void* aUser) {
|
2014-08-26 19:02:31 +04:00
|
|
|
AudioCallbackDriver* driver = reinterpret_cast<AudioCallbackDriver*>(aUser);
|
|
|
|
driver->DeviceChangedCallback();
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:01:35 +04:00
|
|
|
AudioCallbackDriver::AutoInCallback::AutoInCallback(
|
|
|
|
AudioCallbackDriver* aDriver)
|
|
|
|
: mDriver(aDriver) {
|
2020-06-12 16:32:49 +03:00
|
|
|
MOZ_ASSERT(mDriver->mAudioThreadIdInCb == std::thread::id());
|
|
|
|
mDriver->mAudioThreadIdInCb = std::this_thread::get_id();
|
2014-08-26 19:01:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioCallbackDriver::AutoInCallback::~AutoInCallback() {
|
2020-06-12 16:32:49 +03:00
|
|
|
MOZ_ASSERT(mDriver->mAudioThreadIdInCb == std::this_thread::get_id());
|
|
|
|
mDriver->mAudioThreadIdInCb = std::thread::id();
|
|
|
|
}
|
|
|
|
|
2020-06-12 16:33:04 +03:00
|
|
|
bool AudioCallbackDriver::CheckThreadIdChanged() {
|
2021-07-21 07:16:59 +03:00
|
|
|
ProfilerThreadId id = profiler_current_thread_id();
|
2020-06-12 16:32:49 +03:00
|
|
|
if (id != mAudioThreadId) {
|
2020-06-23 19:42:57 +03:00
|
|
|
mAudioThreadId = id;
|
|
|
|
return true;
|
2020-06-12 16:32:49 +03:00
|
|
|
}
|
2020-06-12 16:33:04 +03:00
|
|
|
return false;
|
2014-08-26 19:01:35 +04:00
|
|
|
}
|
|
|
|
|
2016-01-21 19:51:36 +03:00
|
|
|
long AudioCallbackDriver::DataCallback(const AudioDataValue* aInputBuffer,
|
2016-01-21 19:51:36 +03:00
|
|
|
AudioDataValue* aOutputBuffer,
|
|
|
|
long aFrames) {
|
2020-06-12 16:33:04 +03:00
|
|
|
if (!mSandboxed && CheckThreadIdChanged()) {
|
2020-06-23 19:43:40 +03:00
|
|
|
CubebUtils::GetAudioThreadRegistry()->Register(mAudioThreadId);
|
2020-06-12 16:33:04 +03:00
|
|
|
}
|
2020-11-30 17:17:04 +03:00
|
|
|
|
|
|
|
if (mAudioStreamState.compareExchange(AudioStreamState::Pending,
|
|
|
|
AudioStreamState::Running)) {
|
|
|
|
LOG(LogLevel::Verbose, ("%p: AudioCallbackDriver %p First audio callback "
|
2020-11-30 17:16:59 +03:00
|
|
|
"close the Fallback driver",
|
2020-11-30 17:17:04 +03:00
|
|
|
Graph(), this));
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:53:28 +03:00
|
|
|
FallbackDriverState fallbackState = mFallbackDriverState;
|
|
|
|
if (MOZ_UNLIKELY(fallbackState == FallbackDriverState::Running)) {
|
|
|
|
// Wait for the fallback driver to stop. Wake it up so it can stop if it's
|
|
|
|
// sleeping.
|
2020-11-30 17:17:04 +03:00
|
|
|
LOG(LogLevel::Verbose,
|
2020-11-30 17:16:59 +03:00
|
|
|
("%p: AudioCallbackDriver %p Waiting for the Fallback driver to stop",
|
|
|
|
Graph(), this));
|
2019-12-19 01:53:28 +03:00
|
|
|
EnsureNextIteration();
|
2020-04-07 17:33:57 +03:00
|
|
|
PodZero(aOutputBuffer, aFrames * mOutputChannelCount);
|
2019-12-19 01:53:28 +03:00
|
|
|
return aFrames;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(fallbackState == FallbackDriverState::Stopped)) {
|
|
|
|
// We're supposed to stop.
|
2020-04-07 17:33:57 +03:00
|
|
|
PodZero(aOutputBuffer, aFrames * mOutputChannelCount);
|
2020-06-23 19:42:57 +03:00
|
|
|
if (!mSandboxed) {
|
2020-06-23 19:43:40 +03:00
|
|
|
CubebUtils::GetAudioThreadRegistry()->Unregister(mAudioThreadId);
|
2020-06-23 19:42:57 +03:00
|
|
|
}
|
2019-12-19 01:53:28 +03:00
|
|
|
return aFrames - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(ThreadRunning());
|
2018-04-12 18:51:35 +03:00
|
|
|
TRACE_AUDIO_CALLBACK_BUDGET(aFrames, mSampleRate);
|
2021-08-26 12:15:40 +03:00
|
|
|
TRACE("AudioCallbackDriver::DataCallback");
|
2018-04-12 18:51:35 +03:00
|
|
|
|
2018-05-07 20:36:14 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
AutoInCallback aic(this);
|
|
|
|
#endif
|
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
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)
|
2020-11-05 19:44:00 +03:00
|
|
|
uint32_t alreadyBuffered = mScratchBuffer.Empty(mBuffer);
|
2017-11-28 15:32:21 +03:00
|
|
|
|
|
|
|
// 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.
|
2019-12-19 01:50:45 +03:00
|
|
|
GraphTime nextStateComputedTime =
|
|
|
|
MediaTrackGraphImpl::RoundUpToEndOfAudioBlock(mStateComputedTime +
|
|
|
|
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].
|
2019-12-19 01:49:30 +03:00
|
|
|
GraphTime inGraph = mStateComputedTime - mIterationStart;
|
2017-11-28 15:32:21 +03:00
|
|
|
// We want the interval [mIterationStart; mIterationEnd] to be before the
|
2019-12-19 01:49:30 +03:00
|
|
|
// interval [mStateComputedTime; nextStateComputedTime]. We also want
|
2017-11-28 15:32:21 +03:00
|
|
|
// 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,
|
2018-04-17 18:11:13 +03:00
|
|
|
("%p: interval[%ld; %ld] state[%ld; %ld] (frames: %ld) (durationMS: %u) "
|
2017-11-28 15:32:21 +03:00
|
|
|
"(duration ticks: %ld)",
|
2019-12-19 01:51:57 +03:00
|
|
|
Graph(), (long)mIterationStart, (long)mIterationEnd,
|
2019-12-19 01:49:30 +03:00
|
|
|
(long)mStateComputedTime, (long)nextStateComputedTime, (long)aFrames,
|
2017-11-28 15:32:21 +03:00
|
|
|
(uint32_t)durationMS,
|
2019-12-19 01:49:30 +03:00
|
|
|
(long)(nextStateComputedTime - mStateComputedTime)));
|
2017-11-28 15:32:21 +03:00
|
|
|
|
2019-12-19 01:49:30 +03:00
|
|
|
if (mStateComputedTime < mIterationEnd) {
|
2019-12-19 01:51:57 +03:00
|
|
|
LOG(LogLevel::Error, ("%p: Media graph global underrun detected", Graph()));
|
2017-12-01 17:37:54 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("We should not underrun in full duplex");
|
2019-12-19 01:49:30 +03:00
|
|
|
mIterationEnd = mStateComputedTime;
|
2017-11-28 15:32:21 +03:00
|
|
|
}
|
|
|
|
|
2016-04-29 16:16:46 +03:00
|
|
|
// Process mic data if any/needed
|
2018-04-17 18:11:13 +03:00
|
|
|
if (aInputBuffer && mInputChannelCount > 0) {
|
2019-12-19 01:51:57 +03:00
|
|
|
Graph()->NotifyInputData(aInputBuffer, static_cast<size_t>(aFrames),
|
2020-11-05 19:44:00 +03:00
|
|
|
mSampleRate, mInputChannelCount, alreadyBuffered);
|
2016-04-29 16:16:46 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 01:51:05 +03:00
|
|
|
IterationResult result =
|
2021-01-22 00:37:16 +03:00
|
|
|
Graph()->OneIteration(nextStateComputedTime, mIterationEnd, &mMixer);
|
|
|
|
|
|
|
|
mStateComputedTime = nextStateComputedTime;
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2020-11-30 17:16:25 +03:00
|
|
|
MOZ_ASSERT(mBuffer.Available() == 0,
|
|
|
|
"The graph should have filled the buffer");
|
|
|
|
|
2014-08-26 19:04:38 +04:00
|
|
|
mBuffer.BufferFilled();
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2020-05-04 13:12:55 +03:00
|
|
|
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
|
2020-05-02 02:57:30 +03:00
|
|
|
// Prevent returning NaN to the OS mixer, and propagating NaN into the reverse
|
|
|
|
// stream of the AEC.
|
|
|
|
NaNToZeroInPlace(aOutputBuffer, aFrames * mOutputChannelCount);
|
2020-05-04 13:12:55 +03:00
|
|
|
#endif
|
2020-05-02 02:57:30 +03: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.
|
2019-12-19 01:51:57 +03:00
|
|
|
Graph()->NotifyOutputData(aOutputBuffer, static_cast<size_t>(aFrames),
|
2020-04-07 17:33:57 +03:00
|
|
|
mSampleRate, mOutputChannelCount);
|
2016-01-21 19:51:35 +03:00
|
|
|
|
2019-10-11 18:55:02 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// This only happens when the output is on a macbookpro's external speaker,
|
|
|
|
// that are stereo, but let's just be safe.
|
2020-04-07 17:33:57 +03:00
|
|
|
if (mNeedsPanning && mOutputChannelCount == 2) {
|
2019-10-11 18:55:02 +03:00
|
|
|
// hard pan to the right
|
|
|
|
for (uint32_t i = 0; i < aFrames * 2; i += 2) {
|
|
|
|
aOutputBuffer[i + 1] += aOutputBuffer[i];
|
|
|
|
aOutputBuffer[i] = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-07 17:33:57 +03:00
|
|
|
// No-op if MOZ_DUMP_AUDIO is not defined as an environment variable
|
|
|
|
if (aInputBuffer) {
|
|
|
|
mInputStreamFile.Write(static_cast<const AudioDataValue*>(aInputBuffer),
|
|
|
|
aFrames * mInputChannelCount);
|
|
|
|
}
|
|
|
|
mOutputStreamFile.Write(static_cast<const AudioDataValue*>(aOutputBuffer),
|
|
|
|
aFrames * mOutputChannelCount);
|
|
|
|
|
2019-12-19 01:51:05 +03:00
|
|
|
if (result.IsStop()) {
|
2020-11-05 19:43:24 +03:00
|
|
|
if (mInputDeviceID) {
|
|
|
|
mGraphInterface->NotifyInputStopped();
|
|
|
|
}
|
2019-12-19 01:51:12 +03:00
|
|
|
// Signal that we have stopped.
|
|
|
|
result.Stopped();
|
2018-10-13 02:45:18 +03:00
|
|
|
// Update the flag before handing over the graph and going to drain.
|
2019-12-19 01:53:28 +03:00
|
|
|
mAudioStreamState = AudioStreamState::Stopping;
|
2020-06-12 16:33:04 +03:00
|
|
|
if (!mSandboxed) {
|
2020-06-23 19:43:40 +03:00
|
|
|
CubebUtils::GetAudioThreadRegistry()->Unregister(mAudioThreadId);
|
2020-06-12 16:33:04 +03:00
|
|
|
}
|
2017-10-17 08:14:43 +03:00
|
|
|
return aFrames - 1;
|
|
|
|
}
|
2015-12-01 13:47:31 +03:00
|
|
|
|
2019-12-19 01:51:25 +03:00
|
|
|
if (GraphDriver* nextDriver = result.NextDriver()) {
|
2019-12-19 01:53:28 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("%p: Switching to %s driver.", Graph(),
|
|
|
|
nextDriver->AsAudioCallbackDriver() ? "audio" : "system"));
|
2020-11-05 19:43:24 +03:00
|
|
|
if (mInputDeviceID) {
|
|
|
|
mGraphInterface->NotifyInputStopped();
|
|
|
|
}
|
2019-12-19 01:51:25 +03:00
|
|
|
result.Switched();
|
2019-12-19 01:53:28 +03:00
|
|
|
mAudioStreamState = AudioStreamState::Stopping;
|
2019-12-19 01:51:25 +03:00
|
|
|
nextDriver->SetState(mIterationStart, mIterationEnd, mStateComputedTime);
|
|
|
|
nextDriver->Start();
|
2020-06-12 16:33:04 +03:00
|
|
|
if (!mSandboxed) {
|
2020-06-23 19:43:40 +03:00
|
|
|
CubebUtils::GetAudioThreadRegistry()->Unregister(mAudioThreadId);
|
2020-06-12 16:33:04 +03:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:51:25 +03:00
|
|
|
MOZ_ASSERT(result.IsStillProcessing());
|
2014-08-26 19:01:33 +04:00
|
|
|
return aFrames;
|
|
|
|
}
|
|
|
|
|
2019-05-14 19:00:22 +03:00
|
|
|
static const char* StateToString(cubeb_state aState) {
|
|
|
|
switch (aState) {
|
2019-05-24 14:26:01 +03:00
|
|
|
case CUBEB_STATE_STARTED:
|
|
|
|
return "STARTED";
|
|
|
|
case CUBEB_STATE_STOPPED:
|
|
|
|
return "STOPPED";
|
|
|
|
case CUBEB_STATE_DRAINED:
|
|
|
|
return "DRAINED";
|
|
|
|
case CUBEB_STATE_ERROR:
|
|
|
|
return "ERROR";
|
2019-05-14 19:00:22 +03:00
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected state!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
void AudioCallbackDriver::StateCallback(cubeb_state aState) {
|
2019-12-19 01:51:45 +03:00
|
|
|
MOZ_ASSERT(!InIteration());
|
2019-05-24 14:26:01 +03:00
|
|
|
LOG(LogLevel::Debug,
|
2020-04-21 19:14:00 +03:00
|
|
|
("AudioCallbackDriver(%p) State: %s", this, StateToString(aState)));
|
2017-09-08 17:41:36 +03:00
|
|
|
|
2020-11-30 17:16:59 +03:00
|
|
|
AudioStreamState streamState = mAudioStreamState;
|
|
|
|
if (aState != CUBEB_STATE_STARTED) {
|
|
|
|
// Clear the flag for the not running states: stopped, drained, error.
|
|
|
|
streamState = mAudioStreamState.exchange(AudioStreamState::None);
|
|
|
|
}
|
2018-05-25 11:58:45 +03:00
|
|
|
|
2019-12-19 01:52:33 +03:00
|
|
|
if (aState == CUBEB_STATE_ERROR) {
|
|
|
|
// 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.
|
2019-12-19 01:53:28 +03:00
|
|
|
if (streamState == AudioStreamState::Running) {
|
2019-12-19 01:52:33 +03:00
|
|
|
MOZ_ASSERT(!ThreadRunning());
|
2020-10-28 03:58:24 +03:00
|
|
|
mStarted = false;
|
2020-10-28 03:58:20 +03:00
|
|
|
if (mFallbackDriverState == FallbackDriverState::None) {
|
|
|
|
// Only switch to fallback if it's not already running. It could be
|
|
|
|
// running with the callback driver having started but not seen a single
|
|
|
|
// callback yet. I.e., handover from fallback to callback is not done.
|
2020-11-05 19:43:24 +03:00
|
|
|
if (mInputDeviceID) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
// No audio callback after an error. We're calling into the graph here
|
|
|
|
// so we need to be regarded as "in iteration".
|
|
|
|
AutoInCallback aic(this);
|
|
|
|
#endif
|
|
|
|
mGraphInterface->NotifyInputStopped();
|
|
|
|
}
|
2020-10-28 03:58:20 +03:00
|
|
|
FallbackToSystemClockDriver();
|
|
|
|
}
|
2019-12-19 01:52:33 +03:00
|
|
|
}
|
2018-06-29 11:05:56 +03:00
|
|
|
} else if (aState == CUBEB_STATE_STOPPED) {
|
|
|
|
MOZ_ASSERT(!ThreadRunning());
|
2020-10-28 03:58:24 +03:00
|
|
|
mStarted = false;
|
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) {
|
2019-12-19 01:51:45 +03:00
|
|
|
MOZ_ASSERT(InIteration());
|
2014-08-26 19:01:33 +04:00
|
|
|
uint32_t toWrite = mBuffer.Available();
|
|
|
|
|
2021-01-22 00:37:16 +03:00
|
|
|
if (!mBuffer.Available() && aFrames > 0) {
|
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
|
2021-08-26 12:15:40 +03:00
|
|
|
TRACE("AudioCallbackDriver::PanOutputIfNeeded");
|
2019-11-15 00:00:34 +03:00
|
|
|
cubeb_device* out = nullptr;
|
2014-08-26 19:02:31 +04:00
|
|
|
int rv;
|
|
|
|
char name[128];
|
|
|
|
size_t length = sizeof(name);
|
|
|
|
|
|
|
|
rv = sysctlbyname("hw.model", name, &length, NULL, 0);
|
|
|
|
if (rv) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 17:51:53 +03:00
|
|
|
int major, minor;
|
2020-04-29 13:38:53 +03:00
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
// skip the model name
|
|
|
|
if (isalpha(name[i])) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-09 17:51:53 +03:00
|
|
|
sscanf(name + i, "%d,%d", &major, &minor);
|
2020-04-29 13:38:53 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-09 17:51:53 +03:00
|
|
|
enum MacbookModel { MacBook, MacBookPro, MacBookAir, NotAMacbook };
|
2020-04-29 13:38:53 +03:00
|
|
|
|
|
|
|
MacbookModel model;
|
|
|
|
|
|
|
|
if (!strncmp(name, "MacBookPro", length)) {
|
|
|
|
model = MacBookPro;
|
|
|
|
} else if (strncmp(name, "MacBookAir", length)) {
|
|
|
|
model = MacBookAir;
|
|
|
|
} else if (strncmp(name, "MacBook", length)) {
|
|
|
|
model = MacBook;
|
|
|
|
} else {
|
|
|
|
model = NotAMacbook;
|
|
|
|
}
|
|
|
|
// For macbook pro before 2016 model (change of chassis), hard pan the audio
|
|
|
|
// to the right if the speakers are in use to avoid feedback.
|
|
|
|
if (model == MacBookPro && major <= 12) {
|
2014-08-26 19:02:31 +04:00
|
|
|
if (cubeb_stream_get_current_device(mAudioStream, &out) == CUBEB_OK) {
|
2019-11-15 00:00:34 +03:00
|
|
|
MOZ_ASSERT(out);
|
2014-08-26 19:02:31 +04:00
|
|
|
// Check if we are currently outputing sound on external speakers.
|
2019-11-15 00:00:34 +03:00
|
|
|
if (out->output_name && !strcmp(out->output_name, "ispk")) {
|
2014-08-26 19:02:31 +04:00
|
|
|
// Pan everything to the right speaker.
|
2019-10-11 18:55:02 +03:00
|
|
|
LOG(LogLevel::Debug, ("Using the built-in speakers, with%s audio input",
|
|
|
|
aMicrophoneActive ? "" : "out"));
|
|
|
|
mNeedsPanning = aMicrophoneActive;
|
2014-08-26 19:02:31 +04:00
|
|
|
} else {
|
2019-10-11 18:55:02 +03:00
|
|
|
LOG(LogLevel::Debug, ("Using an external output device"));
|
|
|
|
mNeedsPanning = false;
|
2014-08-26 19:02:31 +04:00
|
|
|
}
|
|
|
|
cubeb_stream_device_destroy(mAudioStream, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-05-25 11:58:45 +03:00
|
|
|
void AudioCallbackDriver::DeviceChangedCallback() {
|
2019-12-19 01:51:45 +03:00
|
|
|
MOZ_ASSERT(!InIteration());
|
2016-04-22 17:24:17 +03:00
|
|
|
// Tell the audio engine the device has changed, it might want to reset some
|
|
|
|
// state.
|
2019-12-19 01:51:57 +03:00
|
|
|
Graph()->DeviceChanged();
|
2016-04-22 17:24:19 +03:00
|
|
|
#ifdef XP_MACOSX
|
2019-10-11 18:45:18 +03:00
|
|
|
RefPtr<AudioCallbackDriver> self(this);
|
|
|
|
bool hasInput = mInputChannelCount;
|
2019-11-06 00:19:18 +03:00
|
|
|
NS_DispatchBackgroundTask(NS_NewRunnableFunction(
|
2019-10-11 18:45:18 +03:00
|
|
|
"PanOutputIfNeeded", [self{std::move(self)}, hasInput]() {
|
|
|
|
self->PanOutputIfNeeded(hasInput);
|
|
|
|
}));
|
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() {
|
2019-12-19 01:51:45 +03:00
|
|
|
MOZ_ASSERT(InIteration());
|
2014-08-26 19:01:33 +04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:52:33 +03:00
|
|
|
void AudioCallbackDriver::EnsureNextIteration() {
|
2019-12-19 01:53:28 +03:00
|
|
|
if (mFallbackDriverState == FallbackDriverState::Running) {
|
|
|
|
auto fallback = mFallback.Lock();
|
|
|
|
if (fallback.ref()) {
|
|
|
|
fallback.ref()->EnsureNextIteration();
|
|
|
|
}
|
2019-12-19 01:52:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
bool AudioCallbackDriver::IsStarted() { return mStarted; }
|
|
|
|
|
2019-07-12 14:28:03 +03:00
|
|
|
TimeDuration AudioCallbackDriver::AudioOutputLatency() {
|
2021-08-26 12:15:40 +03:00
|
|
|
TRACE("AudioCallbackDriver::AudioOutputLatency");
|
2019-07-29 15:50:44 +03:00
|
|
|
uint32_t latencyFrames;
|
|
|
|
int rv = cubeb_stream_get_latency(mAudioStream, &latencyFrames);
|
2019-07-12 14:28:03 +03:00
|
|
|
if (rv || mSampleRate == 0) {
|
|
|
|
return TimeDuration::FromSeconds(0.0);
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:50:44 +03:00
|
|
|
return TimeDuration::FromSeconds(static_cast<double>(latencyFrames) /
|
2019-07-12 14:28:03 +03:00
|
|
|
mSampleRate);
|
|
|
|
}
|
|
|
|
|
2020-11-30 17:16:57 +03:00
|
|
|
bool AudioCallbackDriver::OnFallback() const {
|
|
|
|
MOZ_ASSERT(InIteration());
|
|
|
|
return mFallbackDriverState == FallbackDriverState::Running;
|
|
|
|
}
|
|
|
|
|
2017-11-28 13:57:02 +03:00
|
|
|
void AudioCallbackDriver::FallbackToSystemClockDriver() {
|
2018-05-25 11:58:45 +03:00
|
|
|
MOZ_ASSERT(!ThreadRunning());
|
2019-12-19 01:53:33 +03:00
|
|
|
MOZ_ASSERT(mAudioStreamState == AudioStreamState::None ||
|
|
|
|
mAudioStreamState == AudioStreamState::Pending);
|
2019-12-19 01:53:28 +03:00
|
|
|
MOZ_ASSERT(mFallbackDriverState == FallbackDriverState::None);
|
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("%p: AudioCallbackDriver %p Falling back to SystemClockDriver.", Graph(),
|
|
|
|
this));
|
|
|
|
mFallbackDriverState = FallbackDriverState::Running;
|
2019-12-19 01:53:46 +03:00
|
|
|
mNextReInitBackoffStep =
|
|
|
|
TimeDuration::FromMilliseconds(AUDIO_INITIAL_FALLBACK_BACKOFF_STEP_MS);
|
|
|
|
mNextReInitAttempt = TimeStamp::Now() + mNextReInitBackoffStep;
|
2019-12-19 01:52:33 +03:00
|
|
|
auto fallback =
|
|
|
|
MakeRefPtr<FallbackWrapper>(Graph(), this, mSampleRate, mIterationStart,
|
|
|
|
mIterationEnd, mStateComputedTime);
|
|
|
|
{
|
|
|
|
auto driver = mFallback.Lock();
|
|
|
|
driver.ref() = fallback;
|
|
|
|
}
|
|
|
|
fallback->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioCallbackDriver::FallbackDriverStopped(GraphTime aIterationStart,
|
|
|
|
GraphTime aIterationEnd,
|
2019-12-19 01:53:28 +03:00
|
|
|
GraphTime aStateComputedTime,
|
|
|
|
FallbackDriverState aState) {
|
2019-12-19 01:52:33 +03:00
|
|
|
mIterationStart = aIterationStart;
|
|
|
|
mIterationEnd = aIterationEnd;
|
|
|
|
mStateComputedTime = aStateComputedTime;
|
2019-12-19 01:53:46 +03:00
|
|
|
mNextReInitAttempt = TimeStamp();
|
|
|
|
mNextReInitBackoffStep = TimeDuration();
|
2019-12-19 01:53:28 +03:00
|
|
|
{
|
|
|
|
auto fallback = mFallback.Lock();
|
|
|
|
MOZ_ASSERT(fallback.ref()->OnThread());
|
|
|
|
fallback.ref() = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aState == FallbackDriverState::None ||
|
|
|
|
aState == FallbackDriverState::Stopped);
|
|
|
|
MOZ_ASSERT_IF(aState == FallbackDriverState::None,
|
|
|
|
mAudioStreamState == AudioStreamState::Running);
|
|
|
|
mFallbackDriverState = aState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioCallbackDriver::MaybeStartAudioStream() {
|
|
|
|
AudioStreamState streamState = mAudioStreamState;
|
|
|
|
if (streamState != AudioStreamState::None) {
|
2019-12-19 01:53:46 +03:00
|
|
|
LOG(LogLevel::Verbose,
|
|
|
|
("%p: AudioCallbackDriver %p Cannot re-init.", Graph(), this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
if (now < mNextReInitAttempt) {
|
|
|
|
LOG(LogLevel::Verbose,
|
|
|
|
("%p: AudioCallbackDriver %p Not time to re-init yet. %.3fs left.",
|
|
|
|
Graph(), this, (mNextReInitAttempt - now).ToSeconds()));
|
2019-12-19 01:53:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(LogLevel::Debug, ("%p: AudioCallbackDriver %p Attempting to re-init "
|
|
|
|
"audio stream from fallback driver.",
|
|
|
|
Graph(), this));
|
2019-12-19 01:53:46 +03:00
|
|
|
mNextReInitBackoffStep = std::min(
|
|
|
|
mNextReInitBackoffStep * 2,
|
|
|
|
TimeDuration::FromMilliseconds(AUDIO_MAX_FALLBACK_BACKOFF_STEP_MS));
|
|
|
|
mNextReInitAttempt = now + mNextReInitBackoffStep;
|
2019-12-19 01:53:28 +03:00
|
|
|
Start();
|
2017-11-28 13:57:02 +03:00
|
|
|
}
|
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
|