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/. */
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2014-08-26 19:01:33 +04:00
|
|
|
#include <algorithm>
|
2016-06-08 18:29:38 +03:00
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsString.h"
|
2014-08-25 17:26:09 +04:00
|
|
|
#include "mozilla/Preferences.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"
|
2016-10-19 15:39:50 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2017-06-20 18:33:31 +03:00
|
|
|
#include "mozilla/Services.h"
|
2016-06-08 18:29:38 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2014-08-25 17:26:09 +04:00
|
|
|
#include "CubebUtils.h"
|
2014-09-15 10:09:00 +04:00
|
|
|
#include "nsAutoRef.h"
|
2014-08-26 19:01:33 +04:00
|
|
|
#include "prdtoa.h"
|
2017-06-20 18:33:31 +03:00
|
|
|
#include "MediaInfo.h"
|
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"
|
2017-01-20 15:52:29 +03:00
|
|
|
#define PREF_CUBEB_LOG_LEVEL "media.cubeb.log_level"
|
2014-08-25 17:26:09 +04:00
|
|
|
|
2017-01-26 12:35:31 +03:00
|
|
|
#define MASK_MONO (1 << AudioConfig::CHANNEL_MONO)
|
|
|
|
#define MASK_MONO_LFE (MASK_MONO | (1 << AudioConfig::CHANNEL_LFE))
|
|
|
|
#define MASK_STEREO ((1 << AudioConfig::CHANNEL_LEFT) | (1 << AudioConfig::CHANNEL_RIGHT))
|
|
|
|
#define MASK_STEREO_LFE (MASK_STEREO | (1 << AudioConfig::CHANNEL_LFE))
|
|
|
|
#define MASK_3F (MASK_STEREO | (1 << AudioConfig::CHANNEL_CENTER))
|
|
|
|
#define MASK_3F_LFE (MASK_3F | (1 << AudioConfig::CHANNEL_LFE))
|
|
|
|
#define MASK_2F1 (MASK_STEREO | (1 << AudioConfig::CHANNEL_RCENTER))
|
|
|
|
#define MASK_2F1_LFE (MASK_2F1 | (1 << AudioConfig::CHANNEL_LFE))
|
|
|
|
#define MASK_3F1 (MASK_3F | (1 < AudioConfig::CHANNEL_RCENTER))
|
|
|
|
#define MASK_3F1_LFE (MASK_3F1 | (1 << AudioConfig::CHANNEL_LFE))
|
|
|
|
#define MASK_2F2 (MASK_STEREO | (1 << AudioConfig::CHANNEL_LS) | (1 << AudioConfig::CHANNEL_RS))
|
|
|
|
#define MASK_2F2_LFE (MASK_2F2 | (1 << AudioConfig::CHANNEL_LFE))
|
|
|
|
#define MASK_3F2 (MASK_3F | (1 << AudioConfig::CHANNEL_LS) | (1 << AudioConfig::CHANNEL_RS))
|
|
|
|
#define MASK_3F2_LFE (MASK_3F2 | (1 << AudioConfig::CHANNEL_LFE))
|
|
|
|
#define MASK_3F3R_LFE (MASK_3F2_LFE | (1 << AudioConfig::CHANNEL_RCENTER))
|
|
|
|
#define MASK_3F4_LFE (MASK_3F2_LFE | (1 << AudioConfig::CHANNEL_RLS) | (1 << AudioConfig::CHANNEL_RRS))
|
|
|
|
|
2014-08-25 17:26:09 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
namespace {
|
|
|
|
|
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;
|
|
|
|
double sVolumeScale;
|
2016-09-09 16:21:50 +03:00
|
|
|
uint32_t sCubebPlaybackLatencyInMilliseconds;
|
|
|
|
uint32_t sCubebMSGLatencyInFrames;
|
|
|
|
bool sCubebPlaybackLatencyPrefSet;
|
|
|
|
bool sCubebMSGLatencyPrefSet;
|
2016-06-23 18:50:52 +03:00
|
|
|
bool sAudioStreamInitEverSucceeded = false;
|
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;
|
|
|
|
|
2017-02-04 03:22:15 +03:00
|
|
|
// We only support SMPTE layout in cubeb for now. If the value is
|
|
|
|
// CUBEB_LAYOUT_UNDEFINED, then it implies that the preferred layout is
|
|
|
|
// non-SMPTE format.
|
|
|
|
cubeb_channel_layout sPreferredChannelLayout;
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-09-15 10:09:00 +04:00
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
extern LazyLogModule gAudioStreamLog;
|
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 {
|
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) {
|
|
|
|
nsAdoptingString value = Preferences::GetString(aPref);
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (value.IsEmpty()) {
|
|
|
|
sVolumeScale = 1.0;
|
|
|
|
} else {
|
|
|
|
NS_ConvertUTF16toUTF8 utf8(value);
|
|
|
|
sVolumeScale = std::max<double>(0, PR_strtod(utf8.get(), nullptr));
|
|
|
|
}
|
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);
|
2017-01-20 15:52:29 +03:00
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_LOG_LEVEL) == 0) {
|
|
|
|
nsAdoptingString value = Preferences::GetString(aPref);
|
|
|
|
NS_ConvertUTF16toUTF8 utf8(value);
|
|
|
|
LogModule* cubebLog = LogModule::Get("cubeb");
|
|
|
|
if (strcmp(utf8.get(), "verbose") == 0) {
|
|
|
|
cubeb_set_log_callback(CUBEB_LOG_VERBOSE, CubebLogCallback);
|
|
|
|
cubebLog->SetLevel(LogLevel::Verbose);
|
|
|
|
} else if (strcmp(utf8.get(), "normal") == 0) {
|
|
|
|
cubeb_set_log_callback(CUBEB_LOG_NORMAL, CubebLogCallback);
|
|
|
|
cubebLog->SetLevel(LogLevel::Error);
|
|
|
|
} else if (utf8.IsEmpty()) {
|
|
|
|
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) {
|
|
|
|
nsAdoptingString value = Preferences::GetString(aPref);
|
|
|
|
if (value.IsEmpty()) {
|
|
|
|
sCubebBackendName = nullptr;
|
|
|
|
} else {
|
2017-03-14 20:57:03 +03:00
|
|
|
NS_LossyConvertUTF16toASCII ascii(value);
|
|
|
|
sCubebBackendName = new char[ascii.Length() + 1];
|
|
|
|
PodCopy(sCubebBackendName.get(), ascii.get(), ascii.Length());
|
|
|
|
sCubebBackendName[ascii.Length()] = 0;
|
2017-03-14 20:57:03 +03:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2016-09-18 07:32:22 +03:00
|
|
|
MOZ_ASSERT(sPreferredSampleRate);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PreferredSampleRate()
|
|
|
|
{
|
|
|
|
if (!InitPreferredSampleRate()) {
|
|
|
|
return 44100;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(sPreferredSampleRate);
|
|
|
|
return sPreferredSampleRate;
|
2014-08-25 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2017-02-04 03:22:15 +03:00
|
|
|
bool InitPreferredChannelLayout()
|
|
|
|
{
|
2017-02-10 17:14:36 +03:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (sPreferredChannelLayout != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-04 03:22:15 +03:00
|
|
|
}
|
2017-02-10 17:14:36 +03:00
|
|
|
|
|
|
|
cubeb* context = GetCubebContext();
|
2017-02-04 03:22:15 +03:00
|
|
|
if (!context) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-10 17:14:36 +03:00
|
|
|
|
|
|
|
// Favor calling cubeb api with the mutex unlock, potential deadlock.
|
|
|
|
cubeb_channel_layout layout;
|
|
|
|
if (cubeb_get_preferred_channel_layout(context, &layout) != CUBEB_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
sPreferredChannelLayout = layout;
|
|
|
|
return true;
|
2017-02-04 03:22:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PreferredChannelMap(uint32_t aChannels)
|
|
|
|
{
|
|
|
|
// The first element of the following mapping table is channel counts,
|
|
|
|
// and the second one is its bit mask. It will be used in many times,
|
|
|
|
// so we shoule avoid to allocate it in stack, or it will be created
|
|
|
|
// and removed repeatedly. Use static to allocate this local variable
|
|
|
|
// in data space instead of stack.
|
|
|
|
static uint32_t layoutInfo[CUBEB_LAYOUT_MAX][2] = {
|
|
|
|
{ 0, 0 }, // CUBEB_LAYOUT_UNDEFINED
|
|
|
|
{ 2, MASK_STEREO }, // CUBEB_LAYOUT_DUAL_MONO
|
|
|
|
{ 3, MASK_STEREO_LFE }, // CUBEB_LAYOUT_DUAL_MONO_LFE
|
|
|
|
{ 1, MASK_MONO }, // CUBEB_LAYOUT_MONO
|
|
|
|
{ 2, MASK_MONO_LFE }, // CUBEB_LAYOUT_MONO_LFE
|
|
|
|
{ 2, MASK_STEREO }, // CUBEB_LAYOUT_STEREO
|
|
|
|
{ 3, MASK_STEREO_LFE }, // CUBEB_LAYOUT_STEREO_LFE
|
|
|
|
{ 3, MASK_3F }, // CUBEB_LAYOUT_3F
|
|
|
|
{ 4, MASK_3F_LFE }, // CUBEB_LAYOUT_3F_LFE
|
|
|
|
{ 3, MASK_2F1 }, // CUBEB_LAYOUT_2F1
|
|
|
|
{ 4, MASK_2F1_LFE }, // CUBEB_LAYOUT_2F1_LFE
|
|
|
|
{ 4, MASK_3F1 }, // CUBEB_LAYOUT_3F1
|
|
|
|
{ 5, MASK_3F1_LFE }, // CUBEB_LAYOUT_3F1_LFE
|
|
|
|
{ 4, MASK_2F2 }, // CUBEB_LAYOUT_2F2
|
|
|
|
{ 5, MASK_2F2_LFE }, // CUBEB_LAYOUT_2F2_LFE
|
|
|
|
{ 5, MASK_3F2 }, // CUBEB_LAYOUT_3F2
|
|
|
|
{ 6, MASK_3F2_LFE }, // CUBEB_LAYOUT_3F2_LFE
|
|
|
|
{ 7, MASK_3F3R_LFE }, // CUBEB_LAYOUT_3F3R_LFE
|
|
|
|
{ 8, MASK_3F4_LFE }, // CUBEB_LAYOUT_3F4_LFE
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use SMPTE default channel map if we can't get preferred layout
|
|
|
|
// or the channel counts of preferred layout is different from input's one
|
|
|
|
if (!InitPreferredChannelLayout()
|
|
|
|
|| layoutInfo[sPreferredChannelLayout][0] != aChannels) {
|
|
|
|
AudioConfig::ChannelLayout smpteLayout(aChannels);
|
|
|
|
return smpteLayout.Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
return layoutInfo[sPreferredChannelLayout][1];
|
|
|
|
}
|
|
|
|
|
2016-06-08 18:29:38 +03:00
|
|
|
void InitBrandName()
|
|
|
|
{
|
|
|
|
if (sBrandName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsXPIDLString brandName;
|
|
|
|
nsCOMPtr<nsIStringBundleService> stringBundleService =
|
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
if (stringBundleService) {
|
|
|
|
nsCOMPtr<nsIStringBundle> brandBundle;
|
|
|
|
nsresult rv = stringBundleService->CreateBundle(kBrandBundleURL,
|
|
|
|
getter_AddRefs(brandBundle));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2016-07-21 08:03:25 +03:00
|
|
|
rv = brandBundle->GetStringFromName(u"brandShortName",
|
2016-06-08 18:29:38 +03:00
|
|
|
getter_Copies(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
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
cubeb* GetCubebContextUnlocked()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
sMutex.AssertCurrentThreadOwns();
|
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-03-14 20:57:03 +03:00
|
|
|
int rv = cubeb_init(&sCubebContext, sBrandName, sCubebBackendName.get());
|
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
|
|
|
|
2016-10-19 15:39:50 +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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<uint32_t> GetCubebMSGLatencyInFrames()
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (!sCubebMSGLatencyPrefSet) {
|
|
|
|
return Maybe<uint32_t>();
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(sCubebMSGLatencyInFrames > 0);
|
|
|
|
return Some(sCubebMSGLatencyInFrames);
|
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);
|
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_BACKEND);
|
|
|
|
Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_LOG_LEVEL);
|
2016-06-09 17:35:30 +03:00
|
|
|
#ifndef MOZ_WIDGET_ANDROID
|
2016-06-08 18:29:38 +03:00
|
|
|
NS_DispatchToMainThread(NS_NewRunnableFunction(&InitBrandName));
|
2016-06-09 17:35:30 +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);
|
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);
|
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY_MSG);
|
2017-01-20 15:52:29 +03:00
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LOG_LEVEL);
|
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;
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:35:31 +03:00
|
|
|
cubeb_channel_layout ConvertChannelMapToCubebLayout(uint32_t aChannelMap)
|
|
|
|
{
|
|
|
|
switch(aChannelMap) {
|
|
|
|
case MASK_MONO: return CUBEB_LAYOUT_MONO;
|
|
|
|
case MASK_MONO_LFE: return CUBEB_LAYOUT_MONO_LFE;
|
|
|
|
case MASK_STEREO: return CUBEB_LAYOUT_STEREO;
|
|
|
|
case MASK_STEREO_LFE: return CUBEB_LAYOUT_STEREO_LFE;
|
|
|
|
case MASK_3F: return CUBEB_LAYOUT_3F;
|
|
|
|
case MASK_3F_LFE: return CUBEB_LAYOUT_3F_LFE;
|
|
|
|
case MASK_2F1: return CUBEB_LAYOUT_2F1;
|
|
|
|
case MASK_2F1_LFE: return CUBEB_LAYOUT_2F1_LFE;
|
|
|
|
case MASK_3F1: return CUBEB_LAYOUT_3F1;
|
|
|
|
case MASK_3F1_LFE: return CUBEB_LAYOUT_3F1_LFE;
|
|
|
|
case MASK_2F2: return CUBEB_LAYOUT_2F2;
|
|
|
|
case MASK_2F2_LFE: return CUBEB_LAYOUT_2F2_LFE;
|
|
|
|
case MASK_3F2: return CUBEB_LAYOUT_3F2;
|
|
|
|
case MASK_3F2_LFE: return CUBEB_LAYOUT_3F2_LFE;
|
|
|
|
case MASK_3F3R_LFE: return CUBEB_LAYOUT_3F3R_LFE;
|
|
|
|
case MASK_3F4_LFE: return CUBEB_LAYOUT_3F4_LFE;
|
|
|
|
default:
|
|
|
|
NS_ERROR("The channel map is unsupported");
|
|
|
|
return CUBEB_LAYOUT_UNDEFINED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 17:26:09 +04:00
|
|
|
#if defined(__ANDROID__) && defined(MOZ_B2G)
|
2014-09-15 10:09:00 +04:00
|
|
|
cubeb_stream_type ConvertChannelToCubebType(dom::AudioChannel aChannel)
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
switch(aChannel) {
|
|
|
|
case dom::AudioChannel::Normal:
|
2014-10-28 06:12:47 +03:00
|
|
|
/* FALLTHROUGH */
|
2014-08-25 17:26:09 +04:00
|
|
|
case dom::AudioChannel::Content:
|
|
|
|
return CUBEB_STREAM_TYPE_MUSIC;
|
|
|
|
case dom::AudioChannel::Notification:
|
|
|
|
return CUBEB_STREAM_TYPE_NOTIFICATION;
|
|
|
|
case dom::AudioChannel::Alarm:
|
|
|
|
return CUBEB_STREAM_TYPE_ALARM;
|
|
|
|
case dom::AudioChannel::Telephony:
|
|
|
|
return CUBEB_STREAM_TYPE_VOICE_CALL;
|
|
|
|
case dom::AudioChannel::Ringer:
|
|
|
|
return CUBEB_STREAM_TYPE_RING;
|
2015-03-16 07:01:50 +03:00
|
|
|
case dom::AudioChannel::System:
|
|
|
|
return CUBEB_STREAM_TYPE_SYSTEM;
|
2014-08-25 17:26:09 +04:00
|
|
|
case dom::AudioChannel::Publicnotification:
|
|
|
|
return CUBEB_STREAM_TYPE_SYSTEM_ENFORCED;
|
|
|
|
default:
|
|
|
|
NS_ERROR("The value of AudioChannel is invalid");
|
|
|
|
return CUBEB_STREAM_TYPE_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
} // namespace CubebUtils
|
|
|
|
} // namespace mozilla
|