Bug 1262746: P2. Use AudioConvert in AudioStream to downmix samples. r?jwwang draft
authorJean-Yves Avenard <jyavenard@mozilla.com>
Fri, 08 Apr 2016 17:37:31 +1000
changeset 349686 367b146936d2476e27514ddc2534a2275506b008
parent 349685 5aca58c1e893f55e84ca5e1bb4fb61dcdf40abe6
child 349687 7f4e1b6d991e3f35324d8bd9f9825c747dc3daeb
push id15157
push userbmo:jyavenard@mozilla.com
push dateTue, 12 Apr 2016 02:25:53 +0000
reviewersjwwang
bugs1262746
milestone48.0a1
Bug 1262746: P2. Use AudioConvert in AudioStream to downmix samples. r?jwwang MozReview-Commit-ID: 4jgKR8xw1dc
dom/media/AudioStream.cpp
dom/media/AudioStream.h
--- a/dom/media/AudioStream.cpp
+++ b/dom/media/AudioStream.cpp
@@ -13,16 +13,17 @@
 #include "mozilla/Monitor.h"
 #include "mozilla/Mutex.h"
 #include "mozilla/Snprintf.h"
 #include <algorithm>
 #include "mozilla/Telemetry.h"
 #include "CubebUtils.h"
 #include "nsPrintfCString.h"
 #include "gfxPrefs.h"
+#include "AudioConverter.h"
 
 namespace mozilla {
 
 #undef LOG
 #undef LOGW
 
 LazyLogModule gAudioStreamLog("AudioStream");
 // For simple logs
@@ -347,16 +348,19 @@ AudioStream::Init(uint32_t aNumChannels,
   if (params.stream_type == CUBEB_STREAM_TYPE_MAX) {
     return NS_ERROR_INVALID_ARG;
   }
 #endif
 
   params.format = ToCubebFormat<AUDIO_OUTPUT_FORMAT>::value;
   mAudioClock.Init();
 
+  AudioConfig inConfig(mChannels, mInRate);
+  AudioConfig outConfig(mOutChannels, mOutRate);
+  mAudioConverter = MakeUnique<AudioConverter>(inConfig, outConfig);
   return OpenCubeb(params);
 }
 
 // This code used to live inside AudioStream::Init(), but on Mac (others?)
 // it has been known to take 300-800 (or even 8500) ms to execute(!)
 nsresult
 AudioStream::OpenCubeb(cubeb_stream_params &aParams)
 {
@@ -552,20 +556,20 @@ AudioStream::Downmix(Chunk* aChunk)
     LOGW("mismatched sample %u, mInRate=%u", aChunk->Rate(), mInRate);
     return false;
   }
 
   if (aChunk->Channels() > 8) {
     return false;
   }
 
-  if (aChunk->Channels() > 2 && aChunk->Channels() <= 8) {
-    DownmixAudioToStereo(aChunk->GetWritable(),
-                         aChunk->Channels(),
-                         aChunk->Frames());
+  if (aChunk->Channels() > 2) {
+    MOZ_ASSERT(mAudioConverter);
+    mAudioConverter->Process(aChunk->GetWritable(),
+                             aChunk->Channels() * aChunk->Frames());
   }
 
   if (aChunk->Channels() >= 2 && mIsMonoAudioEnabled) {
     DownmixStereoToMono(aChunk->GetWritable(), aChunk->Frames());
   }
 
   return true;
 }
--- a/dom/media/AudioStream.h
+++ b/dom/media/AudioStream.h
@@ -24,16 +24,18 @@ struct CubebDestroyPolicy
 {
   void operator()(cubeb_stream* aStream) const {
     cubeb_stream_destroy(aStream);
   }
 };
 
 class AudioStream;
 class FrameHistory;
+class AudioConfig;
+class AudioConverter;
 
 class AudioClock
 {
 public:
   explicit AudioClock(AudioStream* aStream);
   // Initialize the clock with the current AudioStream. Need to be called
   // before querying the clock. Called on the audio thread.
   void Init();
@@ -362,17 +364,19 @@ private:
     STOPPED,     // Stopped by a call to Pause().
     DRAINED,     // StateCallback has indicated that the drain is complete.
     ERRORED,     // Stream disabled due to an internal error.
     SHUTDOWN     // Shutdown has been called
   };
 
   StreamState mState;
   bool mIsFirst;
-  // Get this value from the preferece, if true, we would downmix the stereo.
+  // Get this value from the preference, if true, we would downmix the stereo.
   bool mIsMonoAudioEnabled;
 
   DataSource& mDataSource;
+
+  UniquePtr<AudioConverter> mAudioConverter;
 };
 
 } // namespace mozilla
 
 #endif