Bug 1067414 - Convert CubebUtils from a class of statics to a namespace. r=jesup
authorMatthew Gregan <kinetik@flim.org>
Mon, 15 Sep 2014 18:09:00 +1200
changeset 205581 750e7aaf09cbe56f7bee01d1890774e03ffa0938
parent 205580 66bad3def0255c3a692a6f1fed1781a9c1856292
child 205582 a68c9d18ffa5a540c8b91e4ed77c094fe636ed8d
push id27498
push userkwierso@gmail.com
push dateWed, 17 Sep 2014 00:06:56 +0000
treeherdermozilla-central@8252eae8278c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup
bugs1067414
milestone35.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1067414 - Convert CubebUtils from a class of statics to a namespace. r=jesup
content/media/CubebUtils.cpp
content/media/CubebUtils.h
content/media/GraphDriver.h
--- a/content/media/CubebUtils.cpp
+++ b/content/media/CubebUtils.cpp
@@ -2,36 +2,49 @@
 /* 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 "mozilla/Preferences.h"
+#include "mozilla/StaticMutex.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 {
 
+namespace {
+
+// This mutex protects the variables below.
+StaticMutex sMutex;
+cubeb* sCubebContext;
+
+// Prefered samplerate, in Hz (characteristic of the
+// hardware/mixer/platform/API used).
+uint32_t sPreferredSampleRate;
+
+double sVolumeScale;
+uint32_t sCubebLatency;
+bool sCubebLatencyPrefSet;
+
+} // anonymous namespace
+
 extern PRLogModuleInfo* gAudioStreamLog;
 
 static const uint32_t CUBEB_NORMAL_LATENCY_MS = 100;
 
-StaticMutex CubebUtils::sMutex;
-cubeb* CubebUtils::sCubebContext;
-uint32_t CubebUtils::sPreferredSampleRate;
-double CubebUtils::sVolumeScale;
-uint32_t CubebUtils::sCubebLatency;
-bool CubebUtils::sCubebLatencyPrefSet;
+namespace CubebUtils {
 
-/*static*/ void CubebUtils::PrefChanged(const char* aPref, void* aClosure)
+void PrefChanged(const char* aPref, void* aClosure)
 {
   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);
@@ -43,116 +56,116 @@ bool CubebUtils::sCubebLatencyPrefSet;
     // 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);
   }
 }
 
-/*static*/ bool CubebUtils::GetFirstStream()
+bool GetFirstStream()
 {
   static bool sFirstStream = true;
 
   StaticMutexAutoLock lock(sMutex);
   bool result = sFirstStream;
   sFirstStream = false;
   return result;
 }
 
-/*static*/ double CubebUtils::GetVolumeScale()
+double GetVolumeScale()
 {
   StaticMutexAutoLock lock(sMutex);
   return sVolumeScale;
 }
 
-/*static*/ cubeb* CubebUtils::GetCubebContext()
+cubeb* GetCubebContext()
 {
   StaticMutexAutoLock lock(sMutex);
   return GetCubebContextUnlocked();
 }
 
-/*static*/ void CubebUtils::InitPreferredSampleRate()
+void InitPreferredSampleRate()
 {
   StaticMutexAutoLock lock(sMutex);
   if (sPreferredSampleRate == 0 &&
       cubeb_get_preferred_sample_rate(GetCubebContextUnlocked(),
                                       &sPreferredSampleRate) != CUBEB_OK) {
     sPreferredSampleRate = 44100;
   }
 }
 
-/*static*/ cubeb* CubebUtils::GetCubebContextUnlocked()
+cubeb* GetCubebContextUnlocked()
 {
   sMutex.AssertCurrentThreadOwns();
   if (sCubebContext ||
       cubeb_init(&sCubebContext, "CubebUtils") == CUBEB_OK) {
     return sCubebContext;
   }
   NS_WARNING("cubeb_init failed");
   return nullptr;
 }
 
-/*static*/ uint32_t CubebUtils::GetCubebLatency()
+uint32_t GetCubebLatency()
 {
   StaticMutexAutoLock lock(sMutex);
   return sCubebLatency;
 }
 
-/*static*/ bool CubebUtils::CubebLatencyPrefSet()
+bool CubebLatencyPrefSet()
 {
   StaticMutexAutoLock lock(sMutex);
   return sCubebLatencyPrefSet;
 }
 
-/*static*/ void CubebUtils::InitLibrary()
+void InitLibrary()
 {
 #ifdef PR_LOGGING
   gAudioStreamLog = PR_NewLogModule("AudioStream");
 #endif
   PrefChanged(PREF_VOLUME_SCALE, nullptr);
   Preferences::RegisterCallback(PrefChanged, PREF_VOLUME_SCALE);
   PrefChanged(PREF_CUBEB_LATENCY, nullptr);
   Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_LATENCY);
 }
 
-/*static*/ void CubebUtils::ShutdownLibrary()
+void ShutdownLibrary()
 {
   Preferences::UnregisterCallback(PrefChanged, PREF_VOLUME_SCALE);
   Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY);
 
   StaticMutexAutoLock lock(sMutex);
   if (sCubebContext) {
     cubeb_destroy(sCubebContext);
     sCubebContext = nullptr;
   }
 }
 
-/*static*/ int CubebUtils::MaxNumberOfChannels()
+int MaxNumberOfChannels()
 {
-  cubeb* cubebContext = CubebUtils::GetCubebContext();
+  cubeb* cubebContext = GetCubebContext();
   uint32_t maxNumberOfChannels;
   if (cubebContext &&
       cubeb_get_max_channel_count(cubebContext,
                                   &maxNumberOfChannels) == CUBEB_OK) {
     return static_cast<int>(maxNumberOfChannels);
   }
 
   return 0;
 }
 
-/*static*/ int CubebUtils::PreferredSampleRate()
+int PreferredSampleRate()
 {
   MOZ_ASSERT(sPreferredSampleRate,
              "sPreferredSampleRate has not been initialized!");
   return sPreferredSampleRate;
 }
 
 #if defined(__ANDROID__) && defined(MOZ_B2G)
-/*static*/ cubeb_stream_type CubebUtils::ConvertChannelToCubebType(dom::AudioChannel aChannel)
+cubeb_stream_type ConvertChannelToCubebType(dom::AudioChannel aChannel)
 {
   switch(aChannel) {
     case dom::AudioChannel::Normal:
       return CUBEB_STREAM_TYPE_SYSTEM;
     case dom::AudioChannel::Content:
       return CUBEB_STREAM_TYPE_MUSIC;
     case dom::AudioChannel::Notification:
       return CUBEB_STREAM_TYPE_NOTIFICATION;
@@ -166,9 +179,10 @@ bool CubebUtils::sCubebLatencyPrefSet;
       return CUBEB_STREAM_TYPE_SYSTEM_ENFORCED;
     default:
       NS_ERROR("The value of AudioChannel is invalid");
       return CUBEB_STREAM_TYPE_MAX;
   }
 }
 #endif
 
-}
+} // namespace CubebUtils
+} // namespace mozilla
--- a/content/media/CubebUtils.h
+++ b/content/media/CubebUtils.h
@@ -3,70 +3,46 @@
 /* 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/. */
 
 #if !defined(CubebUtils_h_)
 #define CubebUtils_h_
 
 #include "cubeb/cubeb.h"
-#include "nsAutoRef.h"
-#include "mozilla/StaticMutex.h"
 #include "mozilla/dom/AudioChannelBinding.h"
 
-template <>
-class nsAutoRefTraits<cubeb_stream> : public nsPointerRefTraits<cubeb_stream>
-{
-public:
-  static void Release(cubeb_stream* aStream) { cubeb_stream_destroy(aStream); }
-};
-
 namespace mozilla {
+namespace CubebUtils {
 
-class CubebUtils {
-public:
-  // Initialize Audio Library. Some Audio backends require initializing the
-  // library before using it.
-  static void InitLibrary();
+// Initialize Audio Library. Some Audio backends require initializing the
+// library before using it.
+void InitLibrary();
 
-  // Shutdown Audio Library. Some Audio backends require shutting down the
-  // library after using it.
-  static void ShutdownLibrary();
+// Shutdown Audio Library. Some Audio backends require shutting down the
+// library after using it.
+void ShutdownLibrary();
 
-  // Returns the maximum number of channels supported by the audio hardware.
-  static int MaxNumberOfChannels();
+// Returns the maximum number of channels supported by the audio hardware.
+int MaxNumberOfChannels();
 
-  // Queries the samplerate the hardware/mixer runs at, and stores it.
-  // Can be called on any thread. When this returns, it is safe to call
-  // PreferredSampleRate without locking.
-  static void InitPreferredSampleRate();
-  // Get the aformentionned sample rate. Does not lock.
-  static int PreferredSampleRate();
+// Queries the samplerate the hardware/mixer runs at, and stores it.
+// Can be called on any thread. When this returns, it is safe to call
+// PreferredSampleRate without locking.
+void InitPreferredSampleRate();
+// Get the aformentionned sample rate. Does not lock.
+int PreferredSampleRate();
 
-  static void PrefChanged(const char* aPref, void* aClosure);
-  static double GetVolumeScale();
-  static bool GetFirstStream();
-  static cubeb* GetCubebContext();
-  static cubeb* GetCubebContextUnlocked();
-  static uint32_t GetCubebLatency();
-  static bool CubebLatencyPrefSet();
+void PrefChanged(const char* aPref, void* aClosure);
+double GetVolumeScale();
+bool GetFirstStream();
+cubeb* GetCubebContext();
+cubeb* GetCubebContextUnlocked();
+uint32_t GetCubebLatency();
+bool CubebLatencyPrefSet();
 #if defined(__ANDROID__) && defined(MOZ_B2G)
-  static cubeb_stream_type ConvertChannelToCubebType(dom::AudioChannel aChannel);
+cubeb_stream_type ConvertChannelToCubebType(dom::AudioChannel aChannel);
 #endif
 
-private:
-  // This mutex protects the static members below.
-  static StaticMutex sMutex;
-  static cubeb* sCubebContext;
-
-  // Prefered samplerate, in Hz (characteristic of the
-  // hardware/mixer/platform/API used).
-  static uint32_t sPreferredSampleRate;
-
-  static double sVolumeScale;
-  static uint32_t sCubebLatency;
-  static bool sCubebLatencyPrefSet;
-};
-}
-
-
+} // namespace CubebUtils
+} // namespace mozilla
 
 #endif // CubebUtils_h_
--- a/content/media/GraphDriver.h
+++ b/content/media/GraphDriver.h
@@ -9,19 +9,25 @@
 #include "nsAutoPtr.h"
 #include "nsAutoRef.h"
 #include "AudioBufferUtils.h"
 #include "AudioMixer.h"
 #include "AudioSegment.h"
 
 struct cubeb_stream;
 
+template <>
+class nsAutoRefTraits<cubeb_stream> : public nsPointerRefTraits<cubeb_stream>
+{
+public:
+  static void Release(cubeb_stream* aStream) { cubeb_stream_destroy(aStream); }
+};
+
 namespace mozilla {
 
-
 /**
  * Assume we can run an iteration of the MediaStreamGraph loop in this much time
  * or less.
  * We try to run the control loop at this rate.
  */
 static const int MEDIA_GRAPH_TARGET_PERIOD_MS = 10;
 
 /**