Bug 918861 - Expose a better samplerate though the AudioStream interface. r=kinetik
authorPaul Adenot <paul@paul.cx>
Thu, 17 Oct 2013 15:44:52 +0200
changeset 164943 ac461ea5f961f7c7c818917fd99daaa1523e5c07
parent 164942 7049d17385d99f98d6f1b5165807c394a3a27abb
child 164944 8c159c637fdbd81acb11c4bb1ddfebd9f8842b85
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskinetik
bugs918861
milestone27.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 918861 - Expose a better samplerate though the AudioStream interface. r=kinetik
content/media/AudioStream.cpp
content/media/AudioStream.h
--- a/content/media/AudioStream.cpp
+++ b/content/media/AudioStream.cpp
@@ -39,16 +39,19 @@ PRLogModuleInfo* gAudioStreamLog = nullp
 #define PREF_CUBEB_LATENCY "media.cubeb_latency_ms"
 
 static Mutex* gAudioPrefsLock = nullptr;
 static double gVolumeScale;
 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
  * dumped-audio-<nnn>.wav, one per nsBufferedAudioStream created, containing
  * the audio for the stream including any skips due to underruns.
  */
 static int gDumpedAudioCount = 0;
 
@@ -436,16 +439,33 @@ int AudioStream::MaxNumberOfChannels()
 
   if (rv != CUBEB_OK) {
     return 0;
   }
 
   return static_cast<int>(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;
   aDest[1] = aValue >> 8;
 }
 
 static void SetUint32LE(uint8_t* aDest, uint32_t aValue)
 {
--- a/content/media/AudioStream.h
+++ b/content/media/AudioStream.h
@@ -6,16 +6,17 @@
 #if !defined(AudioStream_h_)
 #define AudioStream_h_
 
 #include "AudioSampleFormat.h"
 #include "AudioChannelCommon.h"
 #include "nsAutoPtr.h"
 #include "nsCOMPtr.h"
 #include "Latency.h"
+#include "mozilla/StaticMutex.h"
 
 namespace soundtouch {
 class SoundTouch;
 }
 
 namespace mozilla {
 
 class AudioStream;
@@ -111,16 +112,20 @@ public:
   // AllocateStream will return either a local stream or a remoted stream
   // depending on where you call it from.  If you call this from a child process,
   // you may receive an implementation which forwards to a compositing process.
   static AudioStream* AllocateStream();
 
   // 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).
   virtual nsresult Init(int32_t aNumChannels, int32_t aRate,
                         const dom::AudioChannelType aAudioStreamType,
                         LatencyRequest aLatencyRequest) = 0;
 
   // Closes the stream. All future use of the stream is an error.
@@ -178,16 +183,21 @@ public:
   virtual nsresult EnsureTimeStretcherInitialized();
   // Set playback rate as a multiple of the intrinsic playback rate. This is to
   // be called only with aPlaybackRate > 0.0.
   virtual nsresult SetPlaybackRate(double aPlaybackRate);
   // Switch between resampling (if false) and time stretching (if true, default).
   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)
   int mOutRate;
   int mChannels;
   // Number of frames written to the buffers.
   int64_t mWritten;
   AudioClock mAudioClock;