Bug 1276838 - improve coding style of calling cubeb functions. r=kinetik.
authorJW Wang <jwwang@mozilla.com>
Mon, 30 May 2016 21:12:35 +0800
changeset 340693 373e67c2caaddfec3d3901e038804543c3da9afb
parent 340692 d813f7d5f90cba36d4401e970f4b559cb3153f72
child 340694 619441944e8a9ca62bcab61bca61b78599f7da6c
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskinetik
bugs1276838
milestone49.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 1276838 - improve coding style of calling cubeb functions. r=kinetik. MozReview-Commit-ID: 1KjjF6StM0a
dom/media/AudioStream.cpp
dom/media/AudioStream.h
--- a/dom/media/AudioStream.cpp
+++ b/dom/media/AudioStream.cpp
@@ -310,16 +310,23 @@ struct ToCubebFormat {
   static const cubeb_sample_format value = CUBEB_SAMPLE_FLOAT32NE;
 };
 
 template <>
 struct ToCubebFormat<AUDIO_FORMAT_S16> {
   static const cubeb_sample_format value = CUBEB_SAMPLE_S16NE;
 };
 
+template <typename Function, typename... Args>
+int AudioStream::InvokeCubeb(Function aFunction, Args&&... aArgs)
+{
+  MonitorAutoUnlock mon(mMonitor);
+  return aFunction(mCubebStream.get(), Forward<Args>(aArgs)...);
+}
+
 nsresult
 AudioStream::Init(uint32_t aNumChannels, uint32_t aRate,
                   const dom::AudioChannel aAudioChannel)
 {
   auto startTime = TimeStamp::Now();
   mIsFirst = CubebUtils::GetFirstStream();
 
   if (!CubebUtils::GetCubebContext()) {
@@ -409,26 +416,21 @@ AudioStream::SetVolume(double aVolume)
   }
 }
 
 void
 AudioStream::Start()
 {
   MonitorAutoLock mon(mMonitor);
   if (mState == INITIALIZED) {
+    // DataCallback might be called before InvokeCubeb returns
+    // if cubeb_stream_start() succeeds. mState must be set to STARTED
+    // beforehand.
     mState = STARTED;
-    int r;
-    {
-      MonitorAutoUnlock mon(mMonitor);
-      r = cubeb_stream_start(mCubebStream.get());
-      // DataCallback might be called before we exit this scope
-      // if cubeb_stream_start() succeeds. mState must be set to STARTED
-      // beforehand.
-    }
-    if (r != CUBEB_OK) {
+    if (InvokeCubeb(cubeb_stream_start) != CUBEB_OK) {
       mState = ERRORED;
     }
     LOG("started, state %s", mState == STARTED ? "STARTED" : "ERRORED");
   }
 }
 
 void
 AudioStream::Pause()
@@ -439,39 +441,31 @@ AudioStream::Pause()
     return;
   }
 
   if (mState != STARTED && mState != RUNNING) {
     mState = STOPPED; // which also tells async OpenCubeb not to start, just init
     return;
   }
 
-  int r;
-  {
-    MonitorAutoUnlock mon(mMonitor);
-    r = cubeb_stream_stop(mCubebStream.get());
-  }
+  int r = InvokeCubeb(cubeb_stream_stop);
   if (mState != ERRORED && r == CUBEB_OK) {
     mState = STOPPED;
   }
 }
 
 void
 AudioStream::Resume()
 {
   MonitorAutoLock mon(mMonitor);
   if (mState != STOPPED) {
     return;
   }
 
-  int r;
-  {
-    MonitorAutoUnlock mon(mMonitor);
-    r = cubeb_stream_start(mCubebStream.get());
-  }
+  int r = InvokeCubeb(cubeb_stream_start);
   if (mState != ERRORED && r == CUBEB_OK) {
     mState = STARTED;
   }
 }
 
 void
 AudioStream::Shutdown()
 {
@@ -509,23 +503,19 @@ AudioStream::GetPositionInFramesUnlocked
 {
   mMonitor.AssertCurrentThreadOwns();
 
   if (mState == ERRORED) {
     return -1;
   }
 
   uint64_t position = 0;
-  {
-    MonitorAutoUnlock mon(mMonitor);
-    if (cubeb_stream_get_position(mCubebStream.get(), &position) != CUBEB_OK) {
-      return -1;
-    }
+  if (InvokeCubeb(cubeb_stream_get_position, &position) != CUBEB_OK) {
+    return -1;
   }
-
   return std::min<uint64_t>(position, INT64_MAX);
 }
 
 bool
 AudioStream::IsPaused()
 {
   MonitorAutoLock mon(mMonitor);
   return mState == STOPPED;
--- a/dom/media/AudioStream.h
+++ b/dom/media/AudioStream.h
@@ -335,16 +335,19 @@ private:
 
   // Return true if audio frames are valid (correct sampling rate and valid
   // channel count) otherwise false.
   bool IsValidAudioFormat(Chunk* aChunk);
 
   void GetUnprocessed(AudioBufferWriter& aWriter);
   void GetTimeStretched(AudioBufferWriter& aWriter);
 
+  template <typename Function, typename... Args>
+  int InvokeCubeb(Function aFunction, Args&&... aArgs);
+
   // The monitor is held to protect all access to member variables.
   Monitor mMonitor;
 
   // Input rate in Hz (characteristic of the media being played)
   uint32_t mInRate;
   // Output rate in Hz (characteristic of the playback rate)
   uint32_t mOutRate;
   uint32_t mChannels;