diff --git a/content/media/CubebUtils.cpp b/content/media/CubebUtils.cpp index 2ce4021ff9a9..3536048f1610 100644 --- a/content/media/CubebUtils.cpp +++ b/content/media/CubebUtils.cpp @@ -22,15 +22,22 @@ 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; +// 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; + } // anonymous namespace extern PRLogModuleInfo* gAudioStreamLog; @@ -89,6 +96,7 @@ void InitPreferredSampleRate() if (sPreferredSampleRate == 0 && cubeb_get_preferred_sample_rate(GetCubebContextUnlocked(), &sPreferredSampleRate) != CUBEB_OK) { + // Query failed, use a sensible default. sPreferredSampleRate = 44100; } } @@ -139,20 +147,20 @@ void ShutdownLibrary() } } -int MaxNumberOfChannels() +uint32_t MaxNumberOfChannels() { cubeb* cubebContext = GetCubebContext(); uint32_t maxNumberOfChannels; if (cubebContext && cubeb_get_max_channel_count(cubebContext, &maxNumberOfChannels) == CUBEB_OK) { - return static_cast(maxNumberOfChannels); + return maxNumberOfChannels; } return 0; } -int PreferredSampleRate() +uint32_t PreferredSampleRate() { MOZ_ASSERT(sPreferredSampleRate, "sPreferredSampleRate has not been initialized!"); diff --git a/content/media/CubebUtils.h b/content/media/CubebUtils.h index c40fdd85e54a..fe2aff278892 100644 --- a/content/media/CubebUtils.h +++ b/content/media/CubebUtils.h @@ -22,14 +22,15 @@ void InitLibrary(); void ShutdownLibrary(); // Returns the maximum number of channels supported by the audio hardware. -int MaxNumberOfChannels(); +uint32_t 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. +// PreferredSampleRate. void InitPreferredSampleRate(); -// Get the aformentionned sample rate. Does not lock. -int PreferredSampleRate(); + +// Get the aforementioned sample rate. Thread safe. +uint32_t PreferredSampleRate(); void PrefChanged(const char* aPref, void* aClosure); double GetVolumeScale();