Source code

Revision control

Copy as Markdown

Other Tools

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GraphDriver.h"
#include "AudioNodeEngine.h"
#include "cubeb/cubeb.h"
#include "mozilla/dom/AudioContext.h"
#include "mozilla/dom/AudioDeviceInfo.h"
#include "mozilla/dom/BaseAudioContextBinding.h"
#include "mozilla/SchedulerGroup.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Unused.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/StaticPrefs_media.h"
#include "CubebDeviceEnumerator.h"
#include "MediaTrackGraphImpl.h"
#include "CallbackThreadRegistry.h"
#include "Tracing.h"
#ifdef MOZ_WEBRTC
# include "webrtc/MediaEngineWebRTC.h"
#endif
#ifdef XP_MACOSX
# include <sys/sysctl.h>
# include "nsCocoaFeatures.h"
#endif
extern mozilla::LazyLogModule gMediaTrackGraphLog;
#ifdef LOG
# undef LOG
#endif // LOG
#define LOG(type, msg) MOZ_LOG(gMediaTrackGraphLog, type, msg)
namespace mozilla {
GraphDriver::GraphDriver(GraphInterface* aGraphInterface,
GraphDriver* aPreviousDriver, uint32_t aSampleRate)
: mGraphInterface(aGraphInterface),
mSampleRate(aSampleRate),
mPreviousDriver(aPreviousDriver) {}
void GraphDriver::SetStreamName(const nsACString& aStreamName) {
MOZ_ASSERT(InIteration() || (!ThreadRunning() && NS_IsMainThread()));
mStreamName = aStreamName;
LOG(LogLevel::Debug, ("%p: GraphDriver::SetStreamName driver=%p %s", Graph(),
this, mStreamName.get()));
}
void GraphDriver::SetState(const nsACString& aStreamName,
GraphTime aIterationEnd,
GraphTime aStateComputedTime) {
MOZ_ASSERT(InIteration() || !ThreadRunning());
mStreamName = aStreamName;
mIterationEnd = aIterationEnd;
mStateComputedTime = aStateComputedTime;
}
#ifdef DEBUG
bool GraphDriver::InIteration() const {
return OnThread() || Graph()->InDriverIteration(this);
}
#endif
GraphDriver* GraphDriver::PreviousDriver() {
MOZ_ASSERT(InIteration() || !ThreadRunning());
return mPreviousDriver;
}
void GraphDriver::SetPreviousDriver(GraphDriver* aPreviousDriver) {
MOZ_ASSERT(InIteration() || !ThreadRunning());
mPreviousDriver = aPreviousDriver;
}
ThreadedDriver::ThreadedDriver(GraphInterface* aGraphInterface,
GraphDriver* aPreviousDriver,
uint32_t aSampleRate)
: GraphDriver(aGraphInterface, aPreviousDriver, aSampleRate),
mThreadRunning(false) {}
class MediaTrackGraphShutdownThreadRunnable : public Runnable {
public:
explicit MediaTrackGraphShutdownThreadRunnable(
already_AddRefed<nsIThread> aThread)
: Runnable("MediaTrackGraphShutdownThreadRunnable"), mThread(aThread) {}
NS_IMETHOD Run() override {
TRACE("MediaTrackGraphShutdownThreadRunnable");
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mThread);
mThread->AsyncShutdown();
mThread = nullptr;
return NS_OK;
}
private:
nsCOMPtr<nsIThread> mThread;
};
ThreadedDriver::~ThreadedDriver() {
if (mThread) {
nsCOMPtr<nsIRunnable> event =
new MediaTrackGraphShutdownThreadRunnable(mThread.forget());
SchedulerGroup::Dispatch(event.forget());
}
}
class MediaTrackGraphInitThreadRunnable : public Runnable {
public:
explicit MediaTrackGraphInitThreadRunnable(ThreadedDriver* aDriver)
: Runnable("MediaTrackGraphInitThreadRunnable"), mDriver(aDriver) {}
NS_IMETHOD Run() override {
TRACE("MediaTrackGraphInitThreadRunnable");
MOZ_ASSERT(!mDriver->ThreadRunning());
LOG(LogLevel::Debug, ("Starting a new system driver for graph %p",
mDriver->mGraphInterface.get()));
if (GraphDriver* previousDriver = mDriver->PreviousDriver()) {
LOG(LogLevel::Debug,
("%p releasing an AudioCallbackDriver(%p), for graph %p",
mDriver.get(), previousDriver, mDriver->Graph()));
MOZ_ASSERT(!mDriver->AsAudioCallbackDriver());
AudioCallbackDriver* audioCallbackDriver =
previousDriver->AsAudioCallbackDriver();
MOZ_ALWAYS_SUCCEEDS(audioCallbackDriver->mCubebOperationThread->Dispatch(
NS_NewRunnableFunction(
"ThreadedDriver previousDriver::Stop()",
[audioCallbackDriver = RefPtr{audioCallbackDriver}] {
audioCallbackDriver->Stop();
})));
mDriver->SetPreviousDriver(nullptr);
}
mDriver->RunThread();
return NS_OK;
}
private:
RefPtr<ThreadedDriver> mDriver;
};
void ThreadedDriver::Start() {
MOZ_ASSERT(!ThreadRunning());
LOG(LogLevel::Debug,
("Starting thread for a SystemClockDriver %p", mGraphInterface.get()));
Unused << NS_WARN_IF(mThread);
MOZ_ASSERT(!mThread); // Ensure we haven't already started it
nsCOMPtr<nsIRunnable> event = new MediaTrackGraphInitThreadRunnable(this);
// Note: mThread may be null during event->Run() if we pass to NewNamedThread!
// See AudioInitTask
nsresult rv = NS_NewNamedThread("MediaTrackGrph", getter_AddRefs(mThread));
if (NS_SUCCEEDED(rv)) {
mThread->Dispatch(event.forget(), NS_DISPATCH_NORMAL);
}
}
void ThreadedDriver::Shutdown() {
NS_ASSERTION(NS_IsMainThread(), "Must be called on main thread");
// mGraph's thread is not running so it's OK to do whatever here
LOG(LogLevel::Debug, ("Stopping threads for MediaTrackGraph %p", this));
if (mThread) {
LOG(LogLevel::Debug,
("%p: Stopping ThreadedDriver's %p thread", Graph(), this));
mThread->AsyncShutdown();
mThread = nullptr;
}
}
SystemClockDriver::SystemClockDriver(GraphInterface* aGraphInterface,
GraphDriver* aPreviousDriver,
uint32_t aSampleRate)
: ThreadedDriver(aGraphInterface, aPreviousDriver, aSampleRate),
mInitialTimeStamp(TimeStamp::Now()),
mCurrentTimeStamp(TimeStamp::Now()),
mLastTimeStamp(TimeStamp::Now()) {}
SystemClockDriver::~SystemClockDriver() = default;
void ThreadedDriver::RunThread() {
mThreadRunning = true;
while (true) {
auto iterationStart = mIterationEnd;
mIterationEnd += GetIntervalForIteration();
if (mStateComputedTime < mIterationEnd) {
LOG(LogLevel::Warning, ("%p: Global underrun detected", Graph()));
mIterationEnd = mStateComputedTime;
}
if (iterationStart >= mIterationEnd) {
NS_ASSERTION(iterationStart == mIterationEnd, "Time can't go backwards!");
// This could happen due to low clock resolution, maybe?
LOG(LogLevel::Debug, ("%p: Time did not advance", Graph()));
}
GraphTime nextStateComputedTime =
MediaTrackGraphImpl::RoundUpToEndOfAudioBlock(
mIterationEnd + MillisecondsToMediaTime(AUDIO_TARGET_MS));
if (nextStateComputedTime < mStateComputedTime) {
// A previous driver may have been processing further ahead of
// iterationEnd.
LOG(LogLevel::Warning,
("%p: Prevent state from going backwards. interval[%ld; %ld] "
"state[%ld; "
"%ld]",
Graph(), (long)iterationStart, (long)mIterationEnd,
(long)mStateComputedTime, (long)nextStateComputedTime));
nextStateComputedTime = mStateComputedTime;
}
LOG(LogLevel::Verbose,
("%p: interval[%ld; %ld] state[%ld; %ld]", Graph(),
(long)iterationStart, (long)mIterationEnd, (long)mStateComputedTime,
(long)nextStateComputedTime));
mStateComputedTime = nextStateComputedTime;
IterationResult result =
Graph()->OneIteration(mStateComputedTime, mIterationEnd, nullptr);
if (result.IsStop()) {
// Signal that we're done stopping.
result.Stopped();
break;
}
WaitForNextIteration();
if (GraphDriver* nextDriver = result.NextDriver()) {
LOG(LogLevel::Debug, ("%p: Switching to AudioCallbackDriver", Graph()));
result.Switched();
nextDriver->SetState(mStreamName, mIterationEnd, mStateComputedTime);
nextDriver->Start();
break;
}
MOZ_ASSERT(result.IsStillProcessing());
}
mThreadRunning = false;
}
MediaTime SystemClockDriver::GetIntervalForIteration() {
TimeStamp now = TimeStamp::Now();
MediaTime interval =
SecondsToMediaTime((now - mCurrentTimeStamp).ToSeconds());
mCurrentTimeStamp = now;
MOZ_LOG(gMediaTrackGraphLog, LogLevel::Verbose,
("%p: Updating current time to %f (real %f, StateComputedTime() %f)",
Graph(), MediaTimeToSeconds(mIterationEnd + interval),
(now - mInitialTimeStamp).ToSeconds(),
MediaTimeToSeconds(mStateComputedTime)));
return interval;
}
void ThreadedDriver::EnsureNextIteration() {
mWaitHelper.EnsureNextIteration();
}
void ThreadedDriver::WaitForNextIteration() {
MOZ_ASSERT(mThread);
MOZ_ASSERT(OnThread());
mWaitHelper.WaitForNextIterationAtLeast(WaitInterval());
}
TimeDuration SystemClockDriver::WaitInterval() {
MOZ_ASSERT(mThread);
MOZ_ASSERT(OnThread());
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,
("%p: Waiting for next iteration; at %f, timeout=%f", Graph(),
(now - mInitialTimeStamp).ToSeconds(), timeoutMS / 1000.0));
return TimeDuration::FromMilliseconds(timeoutMS);
}
OfflineClockDriver::OfflineClockDriver(GraphInterface* aGraphInterface,
uint32_t aSampleRate, GraphTime aSlice)
: ThreadedDriver(aGraphInterface, nullptr, aSampleRate), mSlice(aSlice) {}
OfflineClockDriver::~OfflineClockDriver() = default;
void OfflineClockDriver::RunThread() {
nsCOMPtr<nsIThreadInternal> threadInternal = do_QueryInterface(mThread);
nsCOMPtr<nsIThreadObserver> observer = do_QueryInterface(Graph());
threadInternal->SetObserver(observer);
ThreadedDriver::RunThread();
}
MediaTime OfflineClockDriver::GetIntervalForIteration() {
return MillisecondsToMediaTime(mSlice);
}
/* 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,
const nsACString& aStreamName, GraphTime aIterationEnd,
GraphTime aStateComputedTime)
: mGraph(std::move(aGraph)),
mOwner(std::move(aOwner)),
mFallbackDriver(
MakeRefPtr<SystemClockDriver>(this, nullptr, aSampleRate)) {
mFallbackDriver->SetState(aStreamName, aIterationEnd, aStateComputedTime);
}
NS_DECL_THREADSAFE_ISUPPORTS
/* Proxied SystemClockDriver methods */
void Start() { mFallbackDriver->Start(); }
MOZ_CAN_RUN_SCRIPT void Shutdown() {
RefPtr<SystemClockDriver> driver = mFallbackDriver;
driver->Shutdown();
}
void SetStreamName(const nsACString& aStreamName) {
mFallbackDriver->SetStreamName(aStreamName);
}
void EnsureNextIteration() { mFallbackDriver->EnsureNextIteration(); }
#ifdef DEBUG
bool InIteration() { return mFallbackDriver->InIteration(); }
#endif
bool OnThread() { return mFallbackDriver->OnThread(); }
/* GraphInterface methods */
void NotifyInputStopped() override {
MOZ_CRASH("Unexpected NotifyInputStopped from fallback SystemClockDriver");
}
void NotifyInputData(const AudioDataValue* aBuffer, size_t aFrames,
TrackRate aRate, uint32_t aChannels,
uint32_t aAlreadyBuffered) override {
MOZ_CRASH("Unexpected NotifyInputData from fallback SystemClockDriver");
}
void NotifySetRequestedInputProcessingParamsResult(
AudioCallbackDriver* aDriver, int aGeneration,
Result<cubeb_input_processing_params, int>&& aResult) override {
MOZ_CRASH(
"Unexpected processing params result from fallback SystemClockDriver");
}
void DeviceChanged() override {
MOZ_CRASH("Unexpected DeviceChanged from fallback SystemClockDriver");
}
#ifdef DEBUG
bool InDriverIteration(const GraphDriver* aDriver) const override {
return mGraph->InDriverIteration(mOwner) && mOwner->OnFallback();
}
#endif
IterationResult OneIteration(GraphTime aStateComputedEnd,
GraphTime aIterationEnd,
MixerCallbackReceiver* aMixerReceiver) override {
MOZ_ASSERT(!aMixerReceiver);
#ifdef DEBUG
AutoInCallback aic(mOwner);
#endif
IterationResult result =
mGraph->OneIteration(aStateComputedEnd, aIterationEnd, aMixerReceiver);
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");
// After a devicechange event from the audio driver, wait for a five
// millisecond grace period before handing control to the audio driver. We
// do this because cubeb leaves no guarantee on audio callbacks coming in
// after a device change event.
if (audioState == AudioStreamState::ChangingDevice &&
mOwner->mChangingDeviceStartTime + TimeDuration::FromMilliseconds(5) <
TimeStamp::Now()) {
mOwner->mChangingDeviceStartTime = TimeStamp();
if (mOwner->mAudioStreamState.compareExchange(
AudioStreamState::ChangingDevice, AudioStreamState::Starting)) {
audioState = AudioStreamState::Starting;
LOG(LogLevel::Debug, ("%p: Fallback driver has started. Waiting for "
"audio driver to start.",
mOwner.get()));
}
}
if (audioState != AudioStreamState::Running && result.IsStillProcessing()) {
mOwner->MaybeStartAudioStream();
return result;
}
MOZ_ASSERT(result.IsStillProcessing() || result.IsStop() ||
result.IsSwitchDriver());
// 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, aIterationEnd,
aStateComputedEnd, result = std::move(result)]() mutable {
FallbackDriverState fallbackState =
result.IsStillProcessing() ? FallbackDriverState::None
: FallbackDriverState::Stopped;
mOwner->FallbackDriverStopped(aIterationEnd, aStateComputedEnd,
fallbackState);
if (fallbackState == FallbackDriverState::Stopped) {
#ifdef DEBUG
// 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);
#endif
if (GraphDriver* nextDriver = result.NextDriver()) {
LOG(LogLevel::Debug,
("%p: Switching from fallback to other driver.",
mOwner.get()));
result.Switched();
nextDriver->SetState(mOwner->mStreamName, aIterationEnd,
aStateComputedEnd);
nextDriver->Start();
} else if (result.IsStop()) {
LOG(LogLevel::Debug,
("%p: Stopping fallback driver.", mOwner.get()));
result.Stopped();
}
}
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;
};
NS_IMPL_ISUPPORTS0(AudioCallbackDriver::FallbackWrapper)
/* static */
already_AddRefed<TaskQueue> AudioCallbackDriver::CreateTaskQueue() {
return TaskQueue::Create(CubebUtils::GetCubebOperationThread(),
"AudioCallbackDriver cubeb task queue")
.forget();
}
AudioCallbackDriver::AudioCallbackDriver(
GraphInterface* aGraphInterface, GraphDriver* aPreviousDriver,
uint32_t aSampleRate, uint32_t aOutputChannelCount,
uint32_t aInputChannelCount, CubebUtils::AudioDeviceID aOutputDeviceID,
CubebUtils::AudioDeviceID aInputDeviceID, AudioInputType aAudioInputType,
Maybe<AudioInputProcessingParamsRequest> aRequestedInputProcessingParams)
: GraphDriver(aGraphInterface, aPreviousDriver, aSampleRate),
mOutputChannelCount(aOutputChannelCount),
mInputChannelCount(aInputChannelCount),
mOutputDeviceID(aOutputDeviceID),
mInputDeviceID(aInputDeviceID),
mIterationDurationMS(MEDIA_GRAPH_TARGET_PERIOD_MS),
mCubebOperationThread(CreateTaskQueue()),
mInputProcessingRequest(aRequestedInputProcessingParams.valueOr(
AudioInputProcessingParamsRequest{})),
mAudioThreadId(ProfilerThreadId{}),
mAudioThreadIdInCb(std::thread::id()),
mFallback("AudioCallbackDriver::mFallback"),
mSandboxed(CubebUtils::SandboxEnabled()) {
LOG(LogLevel::Debug, ("%p: AudioCallbackDriver %p ctor - input: device %p, "
"channel %d, output: device %p, channel %d",
Graph(), this, mInputDeviceID, mInputChannelCount,
mOutputDeviceID, mOutputChannelCount));
NS_WARNING_ASSERTION(mOutputChannelCount != 0,
"Invalid output channel count");
if (aAudioInputType == AudioInputType::Voice &&
StaticPrefs::
media_getusermedia_microphone_prefer_voice_stream_with_processing_enabled()) {
LOG(LogLevel::Debug,
("%p: AudioCallbackDriver %p ctor - using VOICE and requesting input "
"processing params %s (Gen %d).",
Graph(), this,
CubebUtils::ProcessingParamsToString(mInputProcessingRequest.mParams)
.get(),
mInputProcessingRequest.mGeneration));
mInputDevicePreference = CUBEB_DEVICE_PREF_VOICE;
CubebUtils::SetInCommunication(true);
} else {
mInputDevicePreference = CUBEB_DEVICE_PREF_ALL;
}
}
AudioCallbackDriver::~AudioCallbackDriver() {
if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE) {
CubebUtils::SetInCommunication(false);
}
}
bool IsMacbookOrMacbookAir() {
#ifdef XP_MACOSX
size_t len = 0;
sysctlbyname("hw.model", NULL, &len, NULL, 0);
if (len) {
UniquePtr<char[]> model(new char[len]);
// This string can be
// MacBook%d,%d for a normal MacBook
// MacBookAir%d,%d for a Macbook Air
sysctlbyname("hw.model", model.get(), &len, NULL, 0);
char* substring = strstr(model.get(), "MacBook");
if (substring) {
const size_t offset = strlen("MacBook");
if (!strncmp(model.get() + offset, "Air", 3) ||
isdigit(model[offset + 1])) {
return true;
}
}
}
#endif
return false;
}
void AudioCallbackDriver::Init(const nsCString& aStreamName) {
LOG(LogLevel::Debug,
("%p: AudioCallbackDriver::Init driver=%p", Graph(), this));
TRACE("AudioCallbackDriver::Init");
MOZ_ASSERT(OnCubebOperationThread());
MOZ_ASSERT(mAudioStreamState == AudioStreamState::Pending);
if (mFallbackDriverState == FallbackDriverState::Stopped) {
// The graph has already stopped us.
return;
}
RefPtr<CubebUtils::CubebHandle> handle = CubebUtils::GetCubeb();
if (!handle) {
NS_WARNING("Could not get cubeb context.");
LOG(LogLevel::Warning, ("%s: Could not get cubeb context", __func__));
mAudioStreamState = AudioStreamState::None;
if (TryStartingFallbackDriver().isOk()) {
CubebUtils::ReportCubebStreamInitFailure(true);
}
return;
}
cubeb_stream_params output;
cubeb_stream_params input;
bool firstStream = CubebUtils::GetFirstStream();
MOZ_ASSERT(!NS_IsMainThread(),
"This is blocking and should never run on the main thread.");
output.rate = mSampleRate;
output.format = CUBEB_SAMPLE_FLOAT32NE;
if (!mOutputChannelCount) {
LOG(LogLevel::Warning, ("Output number of channels is 0."));
mAudioStreamState = AudioStreamState::None;
if (TryStartingFallbackDriver().isOk()) {
CubebUtils::ReportCubebStreamInitFailure(firstStream);
}
return;
}
CubebUtils::AudioDeviceID forcedOutputDeviceId = nullptr;
char* forcedOutputDeviceName = CubebUtils::GetForcedOutputDevice();
if (forcedOutputDeviceName) {
RefPtr<CubebDeviceEnumerator> enumerator = Enumerator::GetInstance();
RefPtr<AudioDeviceInfo> device = enumerator->DeviceInfoFromName(
NS_ConvertUTF8toUTF16(forcedOutputDeviceName), EnumeratorSide::OUTPUT);
if (device && device->DeviceID()) {
forcedOutputDeviceId = device->DeviceID();
}
}
mBuffer = AudioCallbackBufferWrapper<AudioDataValue>(mOutputChannelCount);
mScratchBuffer =
SpillBuffer<AudioDataValue, WEBAUDIO_BLOCK_SIZE * 2>(mOutputChannelCount);
output.channels = mOutputChannelCount;
AudioConfig::ChannelLayout::ChannelMap channelMap =
AudioConfig::ChannelLayout(mOutputChannelCount).Map();
output.layout = static_cast<uint32_t>(channelMap);
output.prefs = CubebUtils::GetDefaultStreamPrefs(CUBEB_DEVICE_TYPE_OUTPUT);
if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE &&
CubebUtils::RouteOutputAsVoice()) {
output.prefs |= static_cast<cubeb_stream_prefs>(CUBEB_STREAM_PREF_VOICE);
}
uint32_t latencyFrames = CubebUtils::GetCubebMTGLatencyInFrames(&output);
LOG(LogLevel::Debug, ("Minimum latency in frames: %d", latencyFrames));
// Macbook and MacBook air don't have enough CPU to run very low latency
// MediaTrackGraphs, cap the minimal latency to 512 frames int this case.
if (IsMacbookOrMacbookAir()) {
latencyFrames = std::max((uint32_t)512, latencyFrames);
LOG(LogLevel::Debug,
("Macbook or macbook air, new latency: %d", latencyFrames));
}
// Buffer sizes lower than 10ms are nowadays common. It's not very useful
// when doing voice, because all the WebRTC code that does audio input
// processing deals 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 (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE) {
if (latencyFrames < mSampleRate / 100) {
latencyFrames = mozilla::RoundUpPow2(mSampleRate / 100);
LOG(LogLevel::Debug,
("AudioProcessing enabled, new latency %d", latencyFrames));
}
}
// It's not useful for the graph to run with a block size lower than the Web
// Audio API block size, but increasingly devices report that they can do
// audio latencies lower than that.
if (latencyFrames < WEBAUDIO_BLOCK_SIZE) {
LOG(LogLevel::Debug,
("Latency clamped to %d from %d", WEBAUDIO_BLOCK_SIZE, latencyFrames));
latencyFrames = WEBAUDIO_BLOCK_SIZE;
}
LOG(LogLevel::Debug, ("Effective latency in frames: %d", latencyFrames));
input = output;
input.channels = mInputChannelCount;
input.layout = CUBEB_LAYOUT_UNDEFINED;
input.prefs = CubebUtils::GetDefaultStreamPrefs(CUBEB_DEVICE_TYPE_INPUT);
if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE) {
input.prefs |= static_cast<cubeb_stream_prefs>(CUBEB_STREAM_PREF_VOICE);
}
cubeb_stream* stream = nullptr;
const char* streamName =
aStreamName.IsEmpty() ? "AudioCallbackDriver" : aStreamName.get();
bool inputWanted = mInputChannelCount > 0;
CubebUtils::AudioDeviceID outputId = mOutputDeviceID;
CubebUtils::AudioDeviceID inputId = mInputDeviceID;
if (CubebUtils::CubebStreamInit(
handle->Context(), &stream, streamName, inputId,
inputWanted ? &input : nullptr,
forcedOutputDeviceId ? forcedOutputDeviceId : outputId, &output,
latencyFrames, DataCallback_s, StateCallback_s, this) == CUBEB_OK) {
mCubeb = handle;
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(
"Could not create a cubeb stream for MediaTrackGraph, falling "
"back to a SystemClockDriver");
mAudioStreamState = AudioStreamState::None;
// 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 (TryStartingFallbackDriver().isOk()) {
CubebUtils::ReportCubebStreamInitFailure(firstStream);
}
return;
}
#ifdef XP_MACOSX
PanOutputIfNeeded(inputWanted);
#endif
if (inputWanted && InputDevicePreference() == AudioInputType::Voice) {
SetInputProcessingParams(mInputProcessingRequest);
}
cubeb_stream_register_device_changed_callback(
mAudioStream, AudioCallbackDriver::DeviceChangedCallback_s);
// 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);
if (NS_WARN_IF(!StartStream())) {
LOG(LogLevel::Warning,
("%p: AudioCallbackDriver couldn't start a cubeb stream.", Graph()));
return;
}
LOG(LogLevel::Debug, ("%p: AudioCallbackDriver started.", Graph()));
}
void AudioCallbackDriver::SetCubebStreamName(const nsCString& aStreamName) {
MOZ_ASSERT(OnCubebOperationThread());
MOZ_ASSERT(mAudioStream);
cubeb_stream_set_name(mAudioStream, aStreamName.get());
}
void AudioCallbackDriver::Start() {
MOZ_ASSERT(!IsStarted());
MOZ_ASSERT(mAudioStreamState == AudioStreamState::None);
MOZ_ASSERT_IF(PreviousDriver(), PreviousDriver()->InIteration());
mAudioStreamState = AudioStreamState::Pending;
// Starting an audio driver could take a while. We start a system driver in
// the meantime so that the graph is kept running.
(void)TryStartingFallbackDriver();
if (mPreviousDriver) {
if (AudioCallbackDriver* previousAudioCallback =
mPreviousDriver->AsAudioCallbackDriver()) {
LOG(LogLevel::Debug, ("Releasing audio driver off main thread."));
MOZ_ALWAYS_SUCCEEDS(
previousAudioCallback->mCubebOperationThread->Dispatch(
NS_NewRunnableFunction(
"AudioCallbackDriver previousDriver::Stop()",
[previousDriver = RefPtr{previousAudioCallback}] {
previousDriver->Stop();
})));
} else {
LOG(LogLevel::Debug,
("Dropping driver reference for SystemClockDriver."));
MOZ_ASSERT(mPreviousDriver->AsSystemClockDriver());
}
mPreviousDriver = nullptr;
}
LOG(LogLevel::Debug, ("Starting new audio driver off main thread, "
"to ensure it runs after previous shutdown."));
MOZ_ALWAYS_SUCCEEDS(mCubebOperationThread->Dispatch(
NS_NewRunnableFunction("AudioCallbackDriver Init()",
[self = RefPtr{this}, streamName = mStreamName] {
self->Init(streamName);
})));
}
bool AudioCallbackDriver::StartStream() {
TRACE("AudioCallbackDriver::StartStream");
MOZ_ASSERT(!IsStarted() && OnCubebOperationThread());
// Set STARTING before cubeb_stream_start, since starting the cubeb stream
// can result in a callback (that may read mAudioStreamState) before
// mAudioStreamState would otherwise be set.
mAudioStreamState = AudioStreamState::Starting;
if (cubeb_stream_start(mAudioStream) != CUBEB_OK) {
NS_WARNING("Could not start cubeb stream for MTG.");
return false;
}
return true;
}
void AudioCallbackDriver::Stop() {
LOG(LogLevel::Debug,
("%p: AudioCallbackDriver::Stop driver=%p", Graph(), this));
TRACE("AudioCallbackDriver::Stop");
MOZ_ASSERT(OnCubebOperationThread());
cubeb_stream_register_device_changed_callback(mAudioStream, nullptr);
if (cubeb_stream_stop(mAudioStream) != CUBEB_OK) {
NS_WARNING("Could not stop cubeb stream for MTG.");
} else {
mAudioStreamState = AudioStreamState::None;
}
}
void AudioCallbackDriver::Shutdown() {
MOZ_ASSERT(NS_IsMainThread());
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();
}
LOG(LogLevel::Debug,
("%p: Releasing audio driver off main thread (GraphDriver::Shutdown).",
Graph()));
nsLiteralCString reason("AudioCallbackDriver::Shutdown");
NS_DispatchAndSpinEventLoopUntilComplete(
reason, mCubebOperationThread,
NS_NewRunnableFunction(reason.get(),
[self = RefPtr{this}] { self->Stop(); }));
}