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-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"
|
2014-08-25 17:26:09 +04:00
|
|
|
|
|
|
|
#define PREF_VOLUME_SCALE "media.volume_scale"
|
|
|
|
#define PREF_CUBEB_LATENCY "media.cubeb_latency_ms"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This mutex protects the variables below.
|
|
|
|
StaticMutex sMutex;
|
|
|
|
cubeb* sCubebContext;
|
|
|
|
double sVolumeScale;
|
|
|
|
uint32_t sCubebLatency;
|
|
|
|
bool sCubebLatencyPrefSet;
|
2016-06-23 18:50:52 +03:00
|
|
|
bool sAudioStreamInitEverSucceeded = false;
|
2016-06-08 18:29:38 +03:00
|
|
|
StaticAutoPtr<char> sBrandName;
|
|
|
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
} else if (strcmp(aPref, PREF_CUBEB_LATENCY) == 0) {
|
|
|
|
// Arbitrary default stream latency of 100ms. The higher this
|
|
|
|
// value, the longer stream volume changes will take to become
|
|
|
|
// audible.
|
|
|
|
sCubebLatencyPrefSet = Preferences::HasUserValue(aPref);
|
|
|
|
uint32_t value = Preferences::GetUint(aPref, CUBEB_NORMAL_LATENCY_MS);
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
sCubebLatency = std::min<uint32_t>(std::max<uint32_t>(value, 1), 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
void InitPreferredSampleRate()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (sPreferredSampleRate == 0 &&
|
|
|
|
cubeb_get_preferred_sample_rate(GetCubebContextUnlocked(),
|
2014-09-17 22:39:17 +04:00
|
|
|
&sPreferredSampleRate) != CUBEB_OK) {
|
2014-09-15 10:10:00 +04:00
|
|
|
// Query failed, use a sensible default.
|
2014-08-25 17:26:09 +04:00
|
|
|
sPreferredSampleRate = 44100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
|
|
|
|
"Could not get the program name for a cubeb stream.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* cubeb expects a c-string. */
|
|
|
|
const char* ascii = NS_LossyConvertUTF16toASCII(brandName).get();
|
|
|
|
sBrandName = new char[brandName.Length() + 1];
|
|
|
|
PodCopy(sBrandName.get(), ascii, brandName.Length());
|
|
|
|
sBrandName[brandName.Length()] = 0;
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
cubeb* GetCubebContextUnlocked()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
sMutex.AssertCurrentThreadOwns();
|
2016-06-08 18:29:38 +03:00
|
|
|
if (sCubebContext) {
|
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 {
|
|
|
|
NS_WARN_IF_FALSE(sBrandName,
|
|
|
|
"Did not initialize sbrandName, and not on the main thread?");
|
|
|
|
}
|
2016-06-08 18:29:38 +03:00
|
|
|
|
|
|
|
DebugOnly<int> rv = cubeb_init(&sCubebContext, sBrandName);
|
|
|
|
NS_WARN_IF_FALSE(rv == CUBEB_OK, "Could not get a cubeb context.");
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
uint32_t GetCubebLatency()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
return sCubebLatency;
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
bool CubebLatencyPrefSet()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
return sCubebLatencyPrefSet;
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
void InitLibrary()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
PrefChanged(PREF_VOLUME_SCALE, nullptr);
|
|
|
|
Preferences::RegisterCallback(PrefChanged, PREF_VOLUME_SCALE);
|
|
|
|
PrefChanged(PREF_CUBEB_LATENCY, nullptr);
|
|
|
|
Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_LATENCY);
|
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);
|
|
|
|
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY);
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (sCubebContext) {
|
|
|
|
cubeb_destroy(sCubebContext);
|
|
|
|
sCubebContext = nullptr;
|
|
|
|
}
|
2016-06-08 18:29:38 +03:00
|
|
|
sBrandName = nullptr;
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:10:00 +04:00
|
|
|
uint32_t PreferredSampleRate()
|
2014-08-25 17:26:09 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(sPreferredSampleRate,
|
|
|
|
"sPreferredSampleRate has not been initialized!");
|
|
|
|
return sPreferredSampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2014-09-15 10:09:00 +04:00
|
|
|
} // namespace CubebUtils
|
|
|
|
} // namespace mozilla
|