Bug 845648 - Use shortBrand for the name of cubeb stream. r?kinetik
MozReview-Commit-ID: 1PUY8wHVOxy
--- a/dom/media/CubebUtils.cpp
+++ b/dom/media/CubebUtils.cpp
@@ -1,18 +1,23 @@
/* -*- 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>
#include <algorithm>
+#include "nsIStringBundle.h"
+#include "nsDebug.h"
+#include "nsString.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticMutex.h"
+#include "mozilla/StaticPtr.h"
+#include "nsThreadUtils.h"
#include "CubebUtils.h"
#include "nsAutoRef.h"
#include "prdtoa.h"
#define PREF_VOLUME_SCALE "media.volume_scale"
#define PREF_CUBEB_LATENCY "media.cubeb_latency_ms"
namespace mozilla {
@@ -20,16 +25,19 @@ namespace mozilla {
namespace {
// This mutex protects the variables below.
StaticMutex sMutex;
cubeb* sCubebContext;
double sVolumeScale;
uint32_t sCubebLatency;
bool sCubebLatencyPrefSet;
+StaticAutoPtr<char> sBrandName;
+
+const char kBrandBundleURL[] = "chrome://branding/locale/brand.properties";
// 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
@@ -96,25 +104,56 @@ void InitPreferredSampleRate()
if (sPreferredSampleRate == 0 &&
cubeb_get_preferred_sample_rate(GetCubebContextUnlocked(),
&sPreferredSampleRate) != CUBEB_OK) {
// Query failed, use a sensible default.
sPreferredSampleRate = 44100;
}
}
+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)) {
+ rv = brandBundle->GetStringFromName(MOZ_UTF16("brandShortName"),
+ 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;
+}
+
cubeb* GetCubebContextUnlocked()
{
sMutex.AssertCurrentThreadOwns();
- if (sCubebContext ||
- cubeb_init(&sCubebContext, "CubebUtils") == CUBEB_OK) {
+ if (sCubebContext) {
return sCubebContext;
}
- NS_WARNING("cubeb_init failed");
- return nullptr;
+
+ NS_WARN_IF_FALSE(sBrandName, "Could not get brandName?");
+
+ NS_WARN_IF_FALSE(
+ cubeb_init(&sCubebContext, sBrandName) == CUBEB_OK,
+ "Could not get a cubeb context.");
+
+ return sCubebContext;
}
uint32_t GetCubebLatency()
{
StaticMutexAutoLock lock(sMutex);
return sCubebLatency;
}
@@ -125,28 +164,30 @@ bool CubebLatencyPrefSet()
}
void InitLibrary()
{
PrefChanged(PREF_VOLUME_SCALE, nullptr);
Preferences::RegisterCallback(PrefChanged, PREF_VOLUME_SCALE);
PrefChanged(PREF_CUBEB_LATENCY, nullptr);
Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_LATENCY);
+ NS_DispatchToMainThread(NS_NewRunnableFunction(&InitBrandName));
}
void ShutdownLibrary()
{
Preferences::UnregisterCallback(PrefChanged, PREF_VOLUME_SCALE);
Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY);
StaticMutexAutoLock lock(sMutex);
if (sCubebContext) {
cubeb_destroy(sCubebContext);
sCubebContext = nullptr;
}
+ sBrandName = nullptr;
}
uint32_t MaxNumberOfChannels()
{
cubeb* cubebContext = GetCubebContext();
uint32_t maxNumberOfChannels;
if (cubebContext &&
cubeb_get_max_channel_count(cubebContext,