From 3110806a49c6bb186b30e8d554e9cdbb9f8e0b6c Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Thu, 17 Oct 2013 15:44:52 +0200 Subject: [PATCH] Bug 918861 - Expose a better samplerate though the AudioStream interface. r=kinetik --HG-- extra : rebase_source : d085d173aeb00f47f56016c0a1d1bf7fe11fb6f3 --- content/media/AudioStream.cpp | 20 ++++++++++++++++++++ content/media/AudioStream.h | 10 ++++++++++ 2 files changed, 30 insertions(+) diff --git a/content/media/AudioStream.cpp b/content/media/AudioStream.cpp index e255b23ae18..649551d562e 100644 --- a/content/media/AudioStream.cpp +++ b/content/media/AudioStream.cpp @@ -44,6 +44,9 @@ static uint32_t gCubebLatency; static bool gCubebLatencyPrefSet; static const uint32_t CUBEB_NORMAL_LATENCY_MS = 100; +StaticMutex AudioStream::mMutex; +uint32_t AudioStream::mPreferredSampleRate = 0; + /** * When MOZ_DUMP_AUDIO is set in the environment (to anything), * we'll drop a series of files in the current working directory named @@ -441,6 +444,23 @@ int AudioStream::MaxNumberOfChannels() return static_cast(maxNumberOfChannels); } +int AudioStream::PreferredSampleRate() +{ + StaticMutexAutoLock lock(AudioStream::mMutex); + // Get the preferred samplerate for this platform, or fallback to something + // sensible if we fail. We cache the value, because this might be accessed + // often, and the complexity of the function call below depends on the + // backend used. + const int fallbackSampleRate = 44100; + if (mPreferredSampleRate == 0) { + if (cubeb_get_preferred_sample_rate(GetCubebContext(), &mPreferredSampleRate) != CUBEB_OK) { + mPreferredSampleRate = fallbackSampleRate; + } + } + + return mPreferredSampleRate; +} + static void SetUint16LE(uint8_t* aDest, uint16_t aValue) { aDest[0] = aValue & 0xFF; diff --git a/content/media/AudioStream.h b/content/media/AudioStream.h index 482a2f43019..5c527dca427 100644 --- a/content/media/AudioStream.h +++ b/content/media/AudioStream.h @@ -11,6 +11,7 @@ #include "nsAutoPtr.h" #include "nsCOMPtr.h" #include "Latency.h" +#include "mozilla/StaticMutex.h" namespace soundtouch { class SoundTouch; @@ -116,6 +117,10 @@ public: // Returns the maximum number of channels supported by the audio hardware. static int MaxNumberOfChannels(); + // Returns the samplerate the systems prefer, because it is the + // samplerate the hardware/mixer supports. + static int PreferredSampleRate(); + // Initialize the audio stream. aNumChannels is the number of audio // channels (1 for mono, 2 for stereo, etc) and aRate is the sample rate // (22050Hz, 44100Hz, etc). @@ -183,6 +188,11 @@ public: virtual nsresult SetPreservesPitch(bool aPreservesPitch); protected: + // This mutex protects the mPreferedSamplerate member below. + static StaticMutex mMutex; + // Prefered samplerate, in Hz (characteristic of the + // hardware/mixer/platform/API used). + static uint32_t mPreferredSampleRate; // Input rate in Hz (characteristic of the media being played) int mInRate; // Output rate in Hz (characteristic of the playback rate)