2014-08-25 17:26:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "CubebUtils.h"
|
|
|
|
|
|
|
|
#include "MediaInfo.h"
|
2017-08-08 05:57:08 +03:00
|
|
|
#include "mozilla/AbstractThread.h"
|
2017-12-20 04:51:11 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/ipc/FileDescriptor.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2014-08-25 17:26:09 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "mozilla/Services.h"
|
2017-06-22 10:50:52 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2014-09-15 10:09:00 +04:00
|
|
|
#include "mozilla/StaticMutex.h"
|
2016-06-08 18:29:38 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2016-06-23 18:50:52 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-09-15 10:09:00 +04:00
|
|
|
#include "nsAutoRef.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2014-08-26 19:01:33 +04:00
|
|
|
#include "prdtoa.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include <algorithm>
|
|
|
|
#include <stdint.h>
|
2018-02-20 16:37:07 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
#include "GeneratedJNIWrappers.h"
|
|
|
|
#endif
|
2014-08-25 17:26:09 +04:00
|
|
|
|
2018-03-22 08:33:25 +03:00
|
|
|
#define AUDIOIPC_POOL_SIZE_DEFAULT 1
|
|
|
|
#define AUDIOIPC_STACK_SIZE_DEFAULT (64*1024)
|
|
|
|
|
2014-08-25 17:26:09 +04:00
|
|
|
#define PREF_VOLUME_SCALE "media.volume_scale"
|
2017-03-14 20:57:03 +03:00
|
|
|
#define PREF_CUBEB_BACKEND "media.cubeb.backend"
|
2016-09-09 16:21:50 +03:00
|
|
|
#define PREF_CUBEB_LATENCY_PLAYBACK "media.cubeb_latency_playback_ms"
|
|
|
|
#define PREF_CUBEB_LATENCY_MSG "media.cubeb_latency_msg_frames"
|
2018-03-02 21:27:57 +03:00
|
|
|
// Allows to get something non-default for the preferred sample-rate, to allow
|
|
|
|
// troubleshooting in the field and testing.
|
|
|
|
#define PREF_CUBEB_FORCE_SAMPLE_RATE "media.cubeb.force_sample_rate"
|
2017-06-24 00:35:45 +03:00
|
|
|
#define PREF_CUBEB_LOGGING_LEVEL "media.cubeb.logging_level"
|
2018-03-20 18:41:28 +03:00
|
|
|
// Hidden pref used by tests to force failure to obtain cubeb context
|
|
|
|
#define PREF_CUBEB_FORCE_NULL_CONTEXT "media.cubeb.force_null_context"
|
2018-04-30 10:59:32 +03:00
|
|
|
// Hidden pref to disable BMO 1427011 experiment; can be removed once proven.
|
|
|
|
#define PREF_CUBEB_DISABLE_DEVICE_SWITCHING "media.cubeb.disable_device_switching"
|
2017-07-27 11:12:07 +03:00
|
|
|
#define PREF_CUBEB_SANDBOX "media.cubeb.sandbox"
|
2018-03-22 08:33:25 +03:00
|
|
|
#define PREF_AUDIOIPC_POOL_SIZE "media.audioipc.pool_size"
|
|
|
|
#define PREF_AUDIOIPC_STACK_SIZE "media.audioipc.stack_size"
|
2014-08-25 17:26:09 +04:00
|
|
|
|
2017-11-29 00:51:11 +03:00
|
|
|
#if (defined(XP_LINUX) && !defined(MOZ_WIDGET_ANDROID)) || defined(XP_MACOSX)
|
2017-10-12 03:32:29 +03:00
|
|
|
#define MOZ_CUBEB_REMOTING
|
|
|
|
#endif
|
|
|
|
|
2017-07-27 11:12:07 +03:00
|
|
|
extern "C" {
|
2018-03-22 08:33:25 +03:00
|
|
|
|
|
|
|
struct AudioIpcInitParams {
|
|
|
|
int mServerConnection;
|
|
|
|
size_t mPoolSize;
|
|
|
|
size_t mStackSize;
|
|
|
|
};
|
|
|
|
|
2017-07-27 11:12:07 +03:00
|
|
|
// These functions are provided by audioipc-server crate
|
|
|
|
extern void* audioipc_server_start();
|
2017-12-20 04:51:11 +03:00
|
|
|
extern mozilla::ipc::FileDescriptor::PlatformHandleType audioipc_server_new_client(void*);
|
2017-07-27 11:12:07 +03:00
|
|
|
extern void audioipc_server_stop(void*);
|
|
|
|
// These functions are provided by audioipc-client crate
|
2018-03-22 08:33:25 +03:00
|
|
|
extern int audioipc_client_init(cubeb**, const char*, const AudioIpcInitParams*);
|
2017-07-27 11:12:07 +03:00
|
|
|
}
|
|
|
|
|
2014-08-25 17:26:09 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
namespace {
|
|
|
|
|
2017-07-27 11:12:07 +03:00
|
|
|
#ifdef MOZ_CUBEB_REMOTING
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Cubeb Sound Server Thread
|
|
|
|
void* sServerHandle = nullptr;
|
|
|
|
|
2017-12-20 04:51:11 +03:00
|
|
|
// Initialized during early startup, protected by sMutex.
|
2018-01-17 05:51:29 +03:00
|
|
|
StaticAutoPtr<ipc::FileDescriptor> sIPCConnection;
|
2017-12-20 04:51:11 +03:00
|
|
|
|
2017-07-27 11:12:07 +03:00
|
|
|
static bool
|
|
|
|
StartSoundServer()
|
|
|
|
{
|
|
|
|
sServerHandle = audioipc_server_start();
|
|
|
|
return sServerHandle != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ShutdownSoundServer()
|
|
|
|
{
|
|
|
|
if (!sServerHandle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
audioipc_server_stop(sServerHandle);
|
|
|
|
sServerHandle = nullptr;
|
|
|
|
}
|
|
|
|
#endif // MOZ_CUBEB_REMOTING
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-19 15:39:50 +03:00
|
|
|
LazyLogModule gCubebLog("cubeb");
|
|
|
|
|
|
|
|
void CubebLogCallback(const char* aFmt, ...)
|
|
|
|
{
|
2016-10-25 14:34:19 +03:00
|
|
|
char buffer[256];
|
|
|
|
|
2016-10-19 15:39:50 +03:00
|
|
|
va_list arglist;
|
|
|
|
va_start(arglist, aFmt);
|
2016-10-25 14:34:19 +03:00
|
|
|
VsprintfLiteral (buffer, aFmt, arglist);
|
2016-11-07 13:25:16 +03:00
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Error, ("%s", buffer));
|
2016-10-19 15:39:50 +03:00
|
|
|
va_end(arglist);
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
// This mutex protects the variables below.
|
|
|
|
StaticMutex sMutex;
|
2016-08-31 03:03:04 +03:00
|
|
|
enum class CubebState {
|
|
|
|
Uninitialized = 0,
|
|
|
|
Initialized,
|
|
|
|
Shutdown
|
|
|
|
} sCubebState = CubebState::Uninitialized;
|
2014-09-15 10:09:00 +04:00
|
|
|
cubeb* sCubebContext;
|
2017-09-14 15:29:30 +03:00
|
|
|
double sVolumeScale = 1.0;
|
|
|
|
uint32_t sCubebPlaybackLatencyInMilliseconds = 100;
|
|
|
|
uint32_t sCubebMSGLatencyInFrames = 512;
|
2018-03-02 21:27:57 +03:00
|
|
|
// If sCubebForcedSampleRate is zero, PreferredSampleRate will return the
|
|
|
|
// preferred sample-rate for the audio backend in use. Otherwise, it will be
|
|
|
|
// used as the preferred sample-rate.
|
|
|
|
uint32_t sCubebForcedSampleRate = 0;
|
2018-02-20 16:37:07 +03:00
|
|
|
bool sCubebPlaybackLatencyPrefSet = false;
|
|
|
|
bool sCubebMSGLatencyPrefSet = false;
|
2016-06-23 18:50:52 +03:00
|
|
|
bool sAudioStreamInitEverSucceeded = false;
|
2018-03-20 18:41:28 +03:00
|
|
|
bool sCubebForceNullContext = false;
|
2018-04-30 10:59:32 +03:00
|
|
|
bool sCubebDisableDeviceSwitching = true;
|
2017-08-26 04:08:32 +03:00
|
|
|
#ifdef MOZ_CUBEB_REMOTING
|
2018-03-22 08:33:25 +03:00
|
|
|
bool sCubebSandbox = false;
|
|
|
|
size_t sAudioIPCPoolSize;
|
|
|
|
size_t sAudioIPCStackSize;
|
2017-08-26 04:08:32 +03:00
|
|
|
#endif
|
2016-06-08 18:29:38 +03:00
|
|
|
StaticAutoPtr<char> sBrandName;
|
2017-03-14 20:57:03 +03:00
|
|
|
StaticAutoPtr<char> sCubebBackendName;
|
2016-06-08 18:29:38 +03:00
|
|
|
|
|
|
|
const char kBrandBundleURL[] = "chrome://branding/locale/brand.properties";
|
2014-09-15 10:09:00 +04:00
|
|
|
|
2016-06-23 18:50:52 +03:00
|
|
|
const char* AUDIOSTREAM_BACKEND_ID_STR[] = {
|
|
|
|
"jack",
|
|
|
|
"pulse",
|
|
|
|
"alsa",
|
|
|
|
"audiounit",
|
|
|
|
"audioqueue",
|
|
|
|
"wasapi",
|
|
|
|
"winmm",
|
|
|
|
"directsound",
|
|
|
|
"sndio",
|
|
|
|
"opensl",
|
|
|
|
"audiotrack",
|
|
|
|
"kai"
|
|
|
|
};
|
|
|
|
/* Index for failures to create an audio stream the first time. */
|
|
|
|
const int CUBEB_BACKEND_INIT_FAILURE_FIRST =
|
|
|
|
ArrayLength(AUDIOSTREAM_BACKEND_ID_STR);
|
|
|
|
/* Index for failures to create an audio stream after the first time */
|
|
|
|
const int CUBEB_BACKEND_INIT_FAILURE_OTHER = CUBEB_BACKEND_INIT_FAILURE_FIRST + 1;
|
|
|
|
/* Index for an unknown backend. */
|
|
|
|
const int CUBEB_BACKEND_UNKNOWN = CUBEB_BACKEND_INIT_FAILURE_FIRST + 2;
|
|
|
|
|
2014-09-15 10:10:00 +04:00
|
|
|
// Prefered samplerate, in Hz (characteristic of the hardware, mixer, platform,
|
|
|
|
// and API used).
|
|
|
|
//
|
|
|
|
// sMutex protects *initialization* of this, which must be performed from each
|
|
|
|
// thread before fetching, after which it is safe to fetch without holding the
|
|
|
|
// mutex because it is only written once per process execution (by the first
|
|
|
|
// initialization to complete). Since the init must have been called on a
|
|
|
|
// given thread before fetching the value, it's guaranteed (via the mutex) that
|
|
|
|
// sufficient memory barriers have occurred to ensure the correct value is
|
|
|
|
// visible on the querying thread/CPU.
|
|
|
|
uint32_t sPreferredSampleRate;
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-09-15 10:09:00 +04:00
|
|
|
|
2014-08-25 17:26:09 +04:00
|
|
|
static const uint32_t CUBEB_NORMAL_LATENCY_MS = 100;
|
2016-09-09 16:21:50 +03:00
|
|
|
// Consevative default that can work on all platforms.
|
|
|
|
static const uint32_t CUBEB_NORMAL_LATENCY_FRAMES = 1024;
|
2014-08-25 17:26:09 +04:00
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
namespace CubebUtils {
|
2018-01-17 05:51:28 +03:00
|
|
|
cubeb* GetCubebContextUnlocked();
|
2014-08-25 17:26:09 +04:00
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
void PrefChanged(const char* aPref, void* aClosure)
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
if (strcmp(aPref, PREF_VOLUME_SCALE) == 0) {
|
2017-07-31 07:23:50 +03:00
|
|
|
nsAutoCString value;
|
|
|
|
Preferences::GetCString(aPref, value);
|
2014-08-25 17:26:09 +04:00
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (value.IsEmpty()) {
|
|
|
|
sVolumeScale = 1.0;
|
|
|
|
} else {
|
2017-07-31 07:23:50 +03:00
|
|
|
sVolumeScale = std::max<double>(0, PR_strtod(value.get(), nullptr));
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
2016-09-09 16:21:50 +03:00
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_LATENCY_PLAYBACK) == 0) {
|
2014-08-25 17:26:09 +04:00
|
|
|
// Arbitrary default stream latency of 100ms. The higher this
|
|
|
|
// value, the longer stream volume changes will take to become
|
|
|
|
// audible.
|
2016-09-09 16:21:50 +03:00
|
|
|
sCubebPlaybackLatencyPrefSet = Preferences::HasUserValue(aPref);
|
2014-08-25 17:26:09 +04:00
|
|
|
uint32_t value = Preferences::GetUint(aPref, CUBEB_NORMAL_LATENCY_MS);
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
2016-09-09 16:21:50 +03:00
|
|
|
sCubebPlaybackLatencyInMilliseconds = std::min<uint32_t>(std::max<uint32_t>(value, 1), 1000);
|
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_LATENCY_MSG) == 0) {
|
|
|
|
sCubebMSGLatencyPrefSet = Preferences::HasUserValue(aPref);
|
|
|
|
uint32_t value = Preferences::GetUint(aPref, CUBEB_NORMAL_LATENCY_FRAMES);
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
// 128 is the block size for the Web Audio API, which limits how low the
|
|
|
|
// latency can be here.
|
|
|
|
// We don't want to limit the upper limit too much, so that people can
|
|
|
|
// experiment.
|
|
|
|
sCubebMSGLatencyInFrames = std::min<uint32_t>(std::max<uint32_t>(value, 128), 1e6);
|
2018-03-02 21:27:57 +03:00
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_FORCE_SAMPLE_RATE) == 0) {
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
sCubebForcedSampleRate = Preferences::GetUint(aPref);
|
2017-06-24 00:35:45 +03:00
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_LOGGING_LEVEL) == 0) {
|
2017-07-31 07:23:50 +03:00
|
|
|
nsAutoCString value;
|
|
|
|
Preferences::GetCString(aPref, value);
|
2017-01-20 15:52:29 +03:00
|
|
|
LogModule* cubebLog = LogModule::Get("cubeb");
|
2017-07-31 07:23:50 +03:00
|
|
|
if (value.EqualsLiteral("verbose")) {
|
2017-01-20 15:52:29 +03:00
|
|
|
cubeb_set_log_callback(CUBEB_LOG_VERBOSE, CubebLogCallback);
|
|
|
|
cubebLog->SetLevel(LogLevel::Verbose);
|
2017-07-31 07:23:50 +03:00
|
|
|
} else if (value.EqualsLiteral("normal")) {
|
2017-01-20 15:52:29 +03:00
|
|
|
cubeb_set_log_callback(CUBEB_LOG_NORMAL, CubebLogCallback);
|
|
|
|
cubebLog->SetLevel(LogLevel::Error);
|
2017-07-31 07:23:50 +03:00
|
|
|
} else if (value.IsEmpty()) {
|
2017-01-20 15:52:29 +03:00
|
|
|
cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr);
|
|
|
|
cubebLog->SetLevel(LogLevel::Disabled);
|
|
|
|
}
|
2017-03-14 20:57:03 +03:00
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_BACKEND) == 0) {
|
2017-07-31 07:23:50 +03:00
|
|
|
nsAutoCString value;
|
|
|
|
Preferences::GetCString(aPref, value);
|
2017-03-14 20:57:03 +03:00
|
|
|
if (value.IsEmpty()) {
|
|
|
|
sCubebBackendName = nullptr;
|
|
|
|
} else {
|
2017-07-31 07:23:50 +03:00
|
|
|
sCubebBackendName = new char[value.Length() + 1];
|
|
|
|
PodCopy(sCubebBackendName.get(), value.get(), value.Length());
|
|
|
|
sCubebBackendName[value.Length()] = 0;
|
2017-03-14 20:57:03 +03:00
|
|
|
}
|
2018-03-20 18:41:28 +03:00
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_FORCE_NULL_CONTEXT) == 0) {
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
sCubebForceNullContext = Preferences::GetBool(aPref, false);
|
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Verbose,
|
|
|
|
("%s: %s", PREF_CUBEB_FORCE_NULL_CONTEXT, sCubebForceNullContext ? "true" : "false"));
|
2018-04-30 10:59:32 +03:00
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_DISABLE_DEVICE_SWITCHING) == 0) {
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
sCubebDisableDeviceSwitching = Preferences::GetBool(aPref, true);
|
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Verbose,
|
|
|
|
("%s: %s", PREF_CUBEB_DISABLE_DEVICE_SWITCHING, sCubebDisableDeviceSwitching ? "true" : "false"));
|
|
|
|
}
|
2017-07-27 11:12:07 +03:00
|
|
|
#ifdef MOZ_CUBEB_REMOTING
|
|
|
|
else if (strcmp(aPref, PREF_CUBEB_SANDBOX) == 0) {
|
2017-08-26 04:08:32 +03:00
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
sCubebSandbox = Preferences::GetBool(aPref);
|
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Verbose, ("%s: %s", PREF_CUBEB_SANDBOX, sCubebSandbox ? "true" : "false"));
|
2017-07-27 11:12:07 +03:00
|
|
|
|
2017-08-26 04:08:32 +03:00
|
|
|
if (sCubebSandbox && !sServerHandle && XRE_IsParentProcess()) {
|
2017-07-27 11:12:07 +03:00
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Debug, ("Starting cubeb server..."));
|
|
|
|
StartSoundServer();
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 08:33:25 +03:00
|
|
|
else if (strcmp(aPref, PREF_AUDIOIPC_POOL_SIZE) == 0) {
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
sAudioIPCPoolSize = Preferences::GetUint(PREF_AUDIOIPC_POOL_SIZE,
|
|
|
|
AUDIOIPC_POOL_SIZE_DEFAULT);
|
|
|
|
}
|
|
|
|
else if (strcmp(aPref, PREF_AUDIOIPC_STACK_SIZE) == 0) {
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
sAudioIPCStackSize = Preferences::GetUint(PREF_AUDIOIPC_STACK_SIZE,
|
|
|
|
AUDIOIPC_STACK_SIZE_DEFAULT);
|
|
|
|
}
|
2017-07-27 11:12:07 +03:00
|
|
|
#endif
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
bool GetFirstStream()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
static bool sFirstStream = true;
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
bool result = sFirstStream;
|
|
|
|
sFirstStream = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
double GetVolumeScale()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
return sVolumeScale;
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
cubeb* GetCubebContext()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
return GetCubebContextUnlocked();
|
|
|
|
}
|
|
|
|
|
2016-09-18 07:32:22 +03:00
|
|
|
bool InitPreferredSampleRate()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
2016-09-18 07:32:22 +03:00
|
|
|
if (sPreferredSampleRate != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-18 17:16:04 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
sPreferredSampleRate = AndroidGetAudioOutputSampleRate();
|
|
|
|
#else
|
2016-09-18 07:32:22 +03:00
|
|
|
cubeb* context = GetCubebContextUnlocked();
|
|
|
|
if (!context) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (cubeb_get_preferred_sample_rate(context,
|
|
|
|
&sPreferredSampleRate) != CUBEB_OK) {
|
|
|
|
|
|
|
|
return false;
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
2018-02-20 16:37:07 +03:00
|
|
|
#endif
|
2016-09-18 07:32:22 +03:00
|
|
|
MOZ_ASSERT(sPreferredSampleRate);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PreferredSampleRate()
|
|
|
|
{
|
2018-03-02 21:27:57 +03:00
|
|
|
if (sCubebForcedSampleRate) {
|
|
|
|
return sCubebForcedSampleRate;
|
|
|
|
}
|
2016-09-18 07:32:22 +03:00
|
|
|
if (!InitPreferredSampleRate()) {
|
|
|
|
return 44100;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(sPreferredSampleRate);
|
|
|
|
return sPreferredSampleRate;
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2016-06-08 18:29:38 +03:00
|
|
|
void InitBrandName()
|
|
|
|
{
|
|
|
|
if (sBrandName) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString brandName;
|
2016-06-08 18:29:38 +03:00
|
|
|
nsCOMPtr<nsIStringBundleService> stringBundleService =
|
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
if (stringBundleService) {
|
|
|
|
nsCOMPtr<nsIStringBundle> brandBundle;
|
|
|
|
nsresult rv = stringBundleService->CreateBundle(kBrandBundleURL,
|
|
|
|
getter_AddRefs(brandBundle));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-08-04 07:40:52 +03:00
|
|
|
rv = brandBundle->GetStringFromName("brandShortName", brandName);
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rv), "Could not get the program name for a cubeb stream.");
|
2016-06-08 18:29:38 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 20:57:03 +03:00
|
|
|
NS_LossyConvertUTF16toASCII ascii(brandName);
|
|
|
|
sBrandName = new char[ascii.Length() + 1];
|
|
|
|
PodCopy(sBrandName.get(), ascii.get(), ascii.Length());
|
|
|
|
sBrandName[ascii.Length()] = 0;
|
2016-06-08 18:29:38 +03:00
|
|
|
}
|
|
|
|
|
2017-12-20 04:51:11 +03:00
|
|
|
#ifdef MOZ_CUBEB_REMOTING
|
|
|
|
void InitAudioIPCConnection()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
auto contentChild = dom::ContentChild::GetSingleton();
|
|
|
|
auto promise = contentChild->SendCreateAudioIPCConnection();
|
2018-01-17 05:51:28 +03:00
|
|
|
promise->Then(AbstractThread::MainThread(),
|
2017-12-20 04:51:11 +03:00
|
|
|
__func__,
|
|
|
|
[](ipc::FileDescriptor aFD) {
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
2018-01-17 05:51:29 +03:00
|
|
|
MOZ_ASSERT(!sIPCConnection);
|
|
|
|
sIPCConnection = new ipc::FileDescriptor(aFD);
|
2017-12-20 04:51:11 +03:00
|
|
|
},
|
|
|
|
[](mozilla::ipc::ResponseRejectReason aReason) {
|
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Error, ("SendCreateAudioIPCConnection failed: %d",
|
|
|
|
int(aReason)));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ipc::FileDescriptor CreateAudioIPCConnection()
|
|
|
|
{
|
|
|
|
#ifdef MOZ_CUBEB_REMOTING
|
|
|
|
MOZ_ASSERT(sServerHandle);
|
|
|
|
int rawFD = audioipc_server_new_client(sServerHandle);
|
|
|
|
ipc::FileDescriptor fd(rawFD);
|
|
|
|
close(rawFD);
|
|
|
|
return fd;
|
|
|
|
#else
|
|
|
|
return ipc::FileDescriptor();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
cubeb* GetCubebContextUnlocked()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
sMutex.AssertCurrentThreadOwns();
|
2018-03-20 18:41:28 +03:00
|
|
|
if (sCubebForceNullContext) {
|
|
|
|
// Pref set such that we should return a null context
|
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Debug,
|
|
|
|
("%s: returning null context due to %s!", __func__, PREF_CUBEB_FORCE_NULL_CONTEXT));
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-08-31 03:03:04 +03:00
|
|
|
if (sCubebState != CubebState::Uninitialized) {
|
|
|
|
// If we have already passed the initialization point (below), just return
|
|
|
|
// the current context, which may be null (e.g., after error or shutdown.)
|
2015-06-24 03:02:51 +03:00
|
|
|
return sCubebContext;
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
2016-06-08 18:29:38 +03:00
|
|
|
|
2016-07-19 17:28:56 +03:00
|
|
|
if (!sBrandName && NS_IsMainThread()) {
|
|
|
|
InitBrandName();
|
|
|
|
} else {
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
sBrandName, "Did not initialize sbrandName, and not on the main thread?");
|
2016-07-19 17:28:56 +03:00
|
|
|
}
|
2016-06-08 18:29:38 +03:00
|
|
|
|
2017-07-27 11:12:07 +03:00
|
|
|
#ifdef MOZ_CUBEB_REMOTING
|
2018-03-22 08:33:25 +03:00
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Info, ("%s: %s", PREF_CUBEB_SANDBOX, sCubebSandbox ? "true" : "false"));
|
|
|
|
|
|
|
|
int rv = CUBEB_OK;
|
2017-12-24 06:22:36 +03:00
|
|
|
if (sCubebSandbox) {
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
// TODO: Don't use audio IPC when within the same process.
|
2018-01-17 05:51:29 +03:00
|
|
|
MOZ_ASSERT(!sIPCConnection);
|
|
|
|
sIPCConnection = new ipc::FileDescriptor(CreateAudioIPCConnection());
|
2017-12-24 06:22:36 +03:00
|
|
|
} else {
|
2018-01-17 05:51:29 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sIPCConnection);
|
2017-12-24 06:22:36 +03:00
|
|
|
}
|
2017-12-20 04:51:11 +03:00
|
|
|
|
2018-03-22 08:33:25 +03:00
|
|
|
AudioIpcInitParams initParams;
|
|
|
|
initParams.mPoolSize = sAudioIPCPoolSize;
|
|
|
|
initParams.mStackSize = sAudioIPCStackSize;
|
|
|
|
initParams.mServerConnection = sIPCConnection->ClonePlatformHandle().release();
|
2018-03-22 08:33:25 +03:00
|
|
|
|
2018-03-22 08:33:25 +03:00
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Debug, ("%s: %d", PREF_AUDIOIPC_POOL_SIZE, (int) initParams.mPoolSize));
|
|
|
|
MOZ_LOG(gCubebLog, LogLevel::Debug, ("%s: %d", PREF_AUDIOIPC_STACK_SIZE, (int) initParams.mStackSize));
|
|
|
|
|
|
|
|
rv = audioipc_client_init(&sCubebContext, sBrandName, &initParams);
|
|
|
|
} else {
|
|
|
|
rv = cubeb_init(&sCubebContext, sBrandName, sCubebBackendName.get());
|
|
|
|
}
|
2018-01-17 05:51:29 +03:00
|
|
|
sIPCConnection = nullptr;
|
2017-07-27 11:12:07 +03:00
|
|
|
#else // !MOZ_CUBEB_REMOTING
|
2017-03-14 20:57:03 +03:00
|
|
|
int rv = cubeb_init(&sCubebContext, sBrandName, sCubebBackendName.get());
|
2017-07-27 11:12:07 +03:00
|
|
|
#endif // MOZ_CUBEB_REMOTING
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(rv == CUBEB_OK, "Could not get a cubeb context.");
|
2016-09-18 07:32:22 +03:00
|
|
|
sCubebState = (rv == CUBEB_OK) ? CubebState::Initialized : CubebState::Uninitialized;
|
2016-06-08 18:29:38 +03:00
|
|
|
|
|
|
|
return sCubebContext;
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2016-06-23 18:50:52 +03:00
|
|
|
void ReportCubebBackendUsed()
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
|
|
|
|
sAudioStreamInitEverSucceeded = true;
|
|
|
|
|
|
|
|
bool foundBackend = false;
|
|
|
|
for (uint32_t i = 0; i < ArrayLength(AUDIOSTREAM_BACKEND_ID_STR); i++) {
|
|
|
|
if (!strcmp(cubeb_get_backend_id(sCubebContext), AUDIOSTREAM_BACKEND_ID_STR[i])) {
|
|
|
|
Telemetry::Accumulate(Telemetry::AUDIOSTREAM_BACKEND_USED, i);
|
|
|
|
foundBackend = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundBackend) {
|
|
|
|
Telemetry::Accumulate(Telemetry::AUDIOSTREAM_BACKEND_USED,
|
|
|
|
CUBEB_BACKEND_UNKNOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReportCubebStreamInitFailure(bool aIsFirst)
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (!aIsFirst && !sAudioStreamInitEverSucceeded) {
|
|
|
|
// This machine has no audio hardware, or it's in really bad shape, don't
|
|
|
|
// send this info, since we want CUBEB_BACKEND_INIT_FAILURE_OTHER to detect
|
|
|
|
// failures to open multiple streams in a process over time.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Telemetry::Accumulate(Telemetry::AUDIOSTREAM_BACKEND_USED,
|
|
|
|
aIsFirst ? CUBEB_BACKEND_INIT_FAILURE_FIRST
|
|
|
|
: CUBEB_BACKEND_INIT_FAILURE_OTHER);
|
|
|
|
}
|
|
|
|
|
2016-09-09 16:21:50 +03:00
|
|
|
uint32_t GetCubebPlaybackLatencyInMilliseconds()
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
return sCubebPlaybackLatencyInMilliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CubebPlaybackLatencyPrefSet()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
2016-09-09 16:21:50 +03:00
|
|
|
return sCubebPlaybackLatencyPrefSet;
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2016-09-09 16:21:50 +03:00
|
|
|
bool CubebMSGLatencyPrefSet()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
2016-09-09 16:21:50 +03:00
|
|
|
return sCubebMSGLatencyPrefSet;
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:37:07 +03:00
|
|
|
uint32_t GetCubebMSGLatencyInFrames(cubeb_stream_params * params)
|
2016-09-09 16:21:50 +03:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
2018-02-20 16:37:07 +03:00
|
|
|
if (sCubebMSGLatencyPrefSet) {
|
|
|
|
MOZ_ASSERT(sCubebMSGLatencyInFrames > 0);
|
|
|
|
return sCubebMSGLatencyInFrames;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
return AndroidGetAudioOutputFramesPerBuffer();
|
|
|
|
#else
|
|
|
|
cubeb* context = GetCubebContextUnlocked();
|
|
|
|
if (!context) {
|
|
|
|
return sCubebMSGLatencyInFrames; // default 512
|
2016-09-09 16:21:50 +03:00
|
|
|
}
|
2018-02-20 16:37:07 +03:00
|
|
|
uint32_t latency_frames = 0;
|
|
|
|
if (cubeb_get_min_latency(context, params, &latency_frames) != CUBEB_OK) {
|
|
|
|
NS_WARNING("Could not get minimal latency from cubeb.");
|
|
|
|
return sCubebMSGLatencyInFrames; // default 512
|
|
|
|
}
|
|
|
|
return latency_frames;
|
|
|
|
#endif
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
void InitLibrary()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
2017-03-21 21:59:02 +03:00
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_VOLUME_SCALE);
|
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_LATENCY_PLAYBACK);
|
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_LATENCY_MSG);
|
2018-03-02 21:27:57 +03:00
|
|
|
Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_FORCE_SAMPLE_RATE);
|
2017-03-21 21:59:02 +03:00
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_BACKEND);
|
2018-03-20 18:41:28 +03:00
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_FORCE_NULL_CONTEXT);
|
2017-07-27 11:12:07 +03:00
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_SANDBOX);
|
2018-03-22 08:33:25 +03:00
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_AUDIOIPC_POOL_SIZE);
|
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_AUDIOIPC_STACK_SIZE);
|
2018-01-19 07:50:36 +03:00
|
|
|
if (MOZ_LOG_TEST(gCubebLog, LogLevel::Verbose)) {
|
|
|
|
cubeb_set_log_callback(CUBEB_LOG_VERBOSE, CubebLogCallback);
|
|
|
|
} else if (MOZ_LOG_TEST(gCubebLog, LogLevel::Error)) {
|
|
|
|
cubeb_set_log_callback(CUBEB_LOG_NORMAL, CubebLogCallback);
|
|
|
|
}
|
2017-06-24 00:35:45 +03:00
|
|
|
// We don't want to call the callback on startup, because the pref is the
|
|
|
|
// empty string by default ("", which means "logging disabled"). Because the
|
|
|
|
// logging can be enabled via environment variables (MOZ_LOG="module:5"),
|
2018-01-19 07:50:36 +03:00
|
|
|
// calling this callback on init would immediately re-disable the logging.
|
2017-06-24 00:35:45 +03:00
|
|
|
Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_LOGGING_LEVEL);
|
2016-06-09 17:35:30 +03:00
|
|
|
#ifndef MOZ_WIDGET_ANDROID
|
2017-07-18 12:26:58 +03:00
|
|
|
AbstractThread::MainThread()->Dispatch(
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_NewRunnableFunction("CubebUtils::InitLibrary", &InitBrandName));
|
2016-06-09 17:35:30 +03:00
|
|
|
#endif
|
2017-12-20 04:51:11 +03:00
|
|
|
#ifdef MOZ_CUBEB_REMOTING
|
2017-12-24 06:22:36 +03:00
|
|
|
if (sCubebSandbox && XRE_IsContentProcess()) {
|
2018-01-17 05:51:28 +03:00
|
|
|
InitAudioIPCConnection();
|
2017-12-20 04:51:11 +03:00
|
|
|
}
|
|
|
|
#endif
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
void ShutdownLibrary()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_VOLUME_SCALE);
|
2018-03-22 08:33:25 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_AUDIOIPC_STACK_SIZE);
|
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_AUDIOIPC_POOL_SIZE);
|
2017-07-27 11:12:07 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_SANDBOX);
|
2017-03-14 20:57:03 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_BACKEND);
|
2016-09-09 16:21:50 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY_PLAYBACK);
|
2018-03-02 21:27:57 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_FORCE_SAMPLE_RATE);
|
2016-09-09 16:21:50 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY_MSG);
|
2017-06-24 00:35:45 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LOGGING_LEVEL);
|
2018-03-20 18:41:28 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_FORCE_NULL_CONTEXT);
|
2014-08-25 17:26:09 +04:00
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (sCubebContext) {
|
|
|
|
cubeb_destroy(sCubebContext);
|
|
|
|
sCubebContext = nullptr;
|
|
|
|
}
|
2016-06-08 18:29:38 +03:00
|
|
|
sBrandName = nullptr;
|
2017-03-14 20:57:03 +03:00
|
|
|
sCubebBackendName = nullptr;
|
2016-08-31 03:03:04 +03:00
|
|
|
// This will ensure we don't try to re-create a context.
|
|
|
|
sCubebState = CubebState::Shutdown;
|
2017-07-27 11:12:07 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_CUBEB_REMOTING
|
2018-01-17 05:51:29 +03:00
|
|
|
sIPCConnection = nullptr;
|
2017-07-27 11:12:07 +03:00
|
|
|
ShutdownSoundServer();
|
|
|
|
#endif
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2014-09-15 10:10:00 +04:00
|
|
|
uint32_t MaxNumberOfChannels()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
2014-09-15 10:09:00 +04:00
|
|
|
cubeb* cubebContext = GetCubebContext();
|
2014-08-25 17:26:09 +04:00
|
|
|
uint32_t maxNumberOfChannels;
|
|
|
|
if (cubebContext &&
|
|
|
|
cubeb_get_max_channel_count(cubebContext,
|
|
|
|
&maxNumberOfChannels) == CUBEB_OK) {
|
2014-09-15 10:10:00 +04:00
|
|
|
return maxNumberOfChannels;
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:50:14 +03:00
|
|
|
void GetCurrentBackend(nsAString& aBackend)
|
|
|
|
{
|
2016-08-31 03:20:10 +03:00
|
|
|
cubeb* cubebContext = GetCubebContext();
|
|
|
|
if (cubebContext) {
|
|
|
|
const char* backend = cubeb_get_backend_id(cubebContext);
|
|
|
|
if (backend) {
|
|
|
|
aBackend.AssignASCII(backend);
|
|
|
|
return;
|
|
|
|
}
|
2016-07-26 01:50:14 +03:00
|
|
|
}
|
2016-08-31 03:20:10 +03:00
|
|
|
aBackend.AssignLiteral("unknown");
|
2016-07-26 01:50:14 +03:00
|
|
|
}
|
|
|
|
|
2017-07-18 11:09:10 +03:00
|
|
|
uint16_t ConvertCubebType(cubeb_device_type aType)
|
|
|
|
{
|
|
|
|
uint16_t map[] = {
|
|
|
|
nsIAudioDeviceInfo::TYPE_UNKNOWN, // CUBEB_DEVICE_TYPE_UNKNOWN
|
|
|
|
nsIAudioDeviceInfo::TYPE_INPUT, // CUBEB_DEVICE_TYPE_INPUT,
|
|
|
|
nsIAudioDeviceInfo::TYPE_OUTPUT // CUBEB_DEVICE_TYPE_OUTPUT
|
|
|
|
};
|
|
|
|
return map[aType];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ConvertCubebState(cubeb_device_state aState)
|
|
|
|
{
|
|
|
|
uint16_t map[] = {
|
|
|
|
nsIAudioDeviceInfo::STATE_DISABLED, // CUBEB_DEVICE_STATE_DISABLED
|
|
|
|
nsIAudioDeviceInfo::STATE_UNPLUGGED, // CUBEB_DEVICE_STATE_UNPLUGGED
|
|
|
|
nsIAudioDeviceInfo::STATE_ENABLED // CUBEB_DEVICE_STATE_ENABLED
|
|
|
|
};
|
|
|
|
return map[aState];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ConvertCubebPreferred(cubeb_device_pref aPreferred)
|
|
|
|
{
|
|
|
|
if (aPreferred == CUBEB_DEVICE_PREF_NONE) {
|
|
|
|
return nsIAudioDeviceInfo::PREF_NONE;
|
|
|
|
} else if (aPreferred == CUBEB_DEVICE_PREF_ALL) {
|
|
|
|
return nsIAudioDeviceInfo::PREF_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t preferred = 0;
|
|
|
|
if (aPreferred & CUBEB_DEVICE_PREF_MULTIMEDIA) {
|
|
|
|
preferred |= nsIAudioDeviceInfo::PREF_MULTIMEDIA;
|
|
|
|
}
|
|
|
|
if (aPreferred & CUBEB_DEVICE_PREF_VOICE) {
|
|
|
|
preferred |= nsIAudioDeviceInfo::PREF_VOICE;
|
|
|
|
}
|
|
|
|
if (aPreferred & CUBEB_DEVICE_PREF_NOTIFICATION) {
|
|
|
|
preferred |= nsIAudioDeviceInfo::PREF_NOTIFICATION;
|
|
|
|
}
|
|
|
|
return preferred;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ConvertCubebFormat(cubeb_device_fmt aFormat)
|
|
|
|
{
|
|
|
|
uint16_t format = 0;
|
|
|
|
if (aFormat & CUBEB_DEVICE_FMT_S16LE) {
|
|
|
|
format |= nsIAudioDeviceInfo::FMT_S16LE;
|
|
|
|
}
|
|
|
|
if (aFormat & CUBEB_DEVICE_FMT_S16BE) {
|
|
|
|
format |= nsIAudioDeviceInfo::FMT_S16BE;
|
|
|
|
}
|
|
|
|
if (aFormat & CUBEB_DEVICE_FMT_F32LE) {
|
|
|
|
format |= nsIAudioDeviceInfo::FMT_F32LE;
|
|
|
|
}
|
|
|
|
if (aFormat & CUBEB_DEVICE_FMT_F32BE) {
|
|
|
|
format |= nsIAudioDeviceInfo::FMT_F32BE;
|
|
|
|
}
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetDeviceCollection(nsTArray<RefPtr<AudioDeviceInfo>>& aDeviceInfos,
|
|
|
|
Side aSide)
|
|
|
|
{
|
|
|
|
cubeb* context = GetCubebContext();
|
|
|
|
if (context) {
|
|
|
|
cubeb_device_collection collection = { nullptr, 0 };
|
|
|
|
if (cubeb_enumerate_devices(context,
|
|
|
|
aSide == Input ? CUBEB_DEVICE_TYPE_INPUT :
|
|
|
|
CUBEB_DEVICE_TYPE_OUTPUT,
|
|
|
|
&collection) == CUBEB_OK) {
|
|
|
|
for (unsigned int i = 0; i < collection.count; ++i) {
|
|
|
|
auto device = collection.device[i];
|
|
|
|
RefPtr<AudioDeviceInfo> info =
|
2017-07-28 10:41:20 +03:00
|
|
|
new AudioDeviceInfo(NS_ConvertUTF8toUTF16(device.friendly_name),
|
|
|
|
NS_ConvertUTF8toUTF16(device.group_id),
|
|
|
|
NS_ConvertUTF8toUTF16(device.vendor_name),
|
2017-07-18 11:09:10 +03:00
|
|
|
ConvertCubebType(device.type),
|
|
|
|
ConvertCubebState(device.state),
|
|
|
|
ConvertCubebPreferred(device.preferred),
|
|
|
|
ConvertCubebFormat(device.format),
|
|
|
|
ConvertCubebFormat(device.default_format),
|
|
|
|
device.max_channels,
|
|
|
|
device.default_rate,
|
|
|
|
device.max_rate,
|
|
|
|
device.min_rate,
|
|
|
|
device.latency_hi,
|
|
|
|
device.latency_lo);
|
|
|
|
aDeviceInfos.AppendElement(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cubeb_device_collection_destroy(context, &collection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 10:59:32 +03:00
|
|
|
cubeb_stream_prefs GetDefaultStreamPrefs()
|
|
|
|
{
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// Investigation for bug 1427011 - if we're in E10S mode, rely on the
|
|
|
|
// AudioNotification IPC to detect device changes.
|
|
|
|
if (sCubebDisableDeviceSwitching &&
|
|
|
|
(XRE_IsE10sParentProcess() || XRE_IsContentProcess())) {
|
|
|
|
return CUBEB_STREAM_PREF_DISABLE_DEVICE_SWITCHING;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return CUBEB_STREAM_PREF_NONE;
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:37:07 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
uint32_t AndroidGetAudioOutputSampleRate()
|
|
|
|
{
|
|
|
|
int32_t sample_rate = java::GeckoAppShell::GetAudioOutputSampleRate();
|
|
|
|
MOZ_ASSERT(sample_rate > 0);
|
|
|
|
return sample_rate;
|
|
|
|
}
|
|
|
|
uint32_t AndroidGetAudioOutputFramesPerBuffer()
|
|
|
|
{
|
|
|
|
int32_t frames = java::GeckoAppShell::GetAudioOutputFramesPerBuffer();
|
|
|
|
MOZ_ASSERT(frames > 0);
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
} // namespace CubebUtils
|
|
|
|
} // namespace mozilla
|