Bug 1270350 - per comment 0, use SyncRunnable to repalce the boilerplate code. r=jya. draft
authorJW Wang <jwwang@mozilla.com>
Thu, 05 May 2016 11:54:44 +0800
changeset 363592 45540a2b6f4bbb330b485aa87e5b1742dd4d2b12
parent 363591 bb228bb3f5f711f21241fb3602e40930fad158a7
child 520083 0944135472288aacb23459b1e925048cde66a0f9
push id17252
push userjwwang@mozilla.com
push dateThu, 05 May 2016 03:57:46 +0000
reviewersjya
bugs1270350
milestone49.0a1
Bug 1270350 - per comment 0, use SyncRunnable to repalce the boilerplate code. r=jya. MozReview-Commit-ID: 9hqmUnNC3Vm
dom/media/platforms/apple/AppleVDADecoder.cpp
dom/media/platforms/apple/AppleVDADecoder.h
dom/media/platforms/apple/AppleVTDecoder.cpp
dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp
dom/media/platforms/ffmpeg/FFmpegDataDecoder.h
dom/media/platforms/wmf/WMFMediaDataDecoder.cpp
dom/media/platforms/wmf/WMFMediaDataDecoder.h
--- a/dom/media/platforms/apple/AppleVDADecoder.cpp
+++ b/dom/media/platforms/apple/AppleVDADecoder.cpp
@@ -10,16 +10,17 @@
 #include "AppleUtils.h"
 #include "AppleVDADecoder.h"
 #include "AppleVDALinker.h"
 #include "MediaInfo.h"
 #include "mp4_demuxer/H264.h"
 #include "MP4Decoder.h"
 #include "MediaData.h"
 #include "mozilla/ArrayUtils.h"
+#include "mozilla/SyncRunnable.h"
 #include "nsAutoPtr.h"
 #include "nsThreadUtils.h"
 #include "mozilla/Logging.h"
 #include "VideoUtils.h"
 #include <algorithm>
 #include "gfxPlatform.h"
 
 #ifndef MOZ_WIDGET_UIKIT
@@ -144,21 +145,18 @@ AppleVDADecoder::Input(MediaRawData* aSa
 nsresult
 AppleVDADecoder::Flush()
 {
   MOZ_ASSERT(mCallback->OnReaderTaskQueue());
   mIsFlushing = true;
   mTaskQueue->Flush();
   nsCOMPtr<nsIRunnable> runnable =
     NS_NewRunnableMethod(this, &AppleVDADecoder::ProcessFlush);
-  MonitorAutoLock mon(mMonitor);
-  mTaskQueue->Dispatch(runnable.forget());
-  while (mIsFlushing) {
-    mon.Wait();
-  }
+  SyncRunnable::DispatchToThread(mTaskQueue, runnable);
+  mIsFlushing = false;
   mInputIncoming = 0;
   return NS_OK;
 }
 
 nsresult
 AppleVDADecoder::Drain()
 {
   MOZ_ASSERT(mCallback->OnReaderTaskQueue());
@@ -174,19 +172,16 @@ AppleVDADecoder::ProcessFlush()
   MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
 
   OSStatus rv = VDADecoderFlush(mDecoder, 0 /*dont emit*/);
   if (rv != noErr) {
     LOG("AppleVDADecoder::Flush failed waiting for platform decoder "
         "with error:%d.", rv);
   }
   ClearReorderedFrames();
-  MonitorAutoLock mon(mMonitor);
-  mIsFlushing = false;
-  mon.NotifyAll();
 }
 
 void
 AppleVDADecoder::ProcessDrain()
 {
   MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
 
   OSStatus rv = VDADecoderFlush(mDecoder, kVDADecoderFlush_EmitFrames);
--- a/dom/media/platforms/apple/AppleVDADecoder.h
+++ b/dom/media/platforms/apple/AppleVDADecoder.h
@@ -120,17 +120,16 @@ protected:
   const bool mUseSoftwareImages;
   const bool mIs106;
 
   // Number of times a sample was queued via Input(). Will be decreased upon
   // the decoder's callback being invoked.
   // This is used to calculate how many frames has been buffered by the decoder.
   Atomic<uint32_t> mQueuedSamples;
 
-  // For wait on mIsFlushing during Shutdown() process.
   // Protects mReorderQueue.
   Monitor mMonitor;
   // Set on reader/decode thread calling Flush() to indicate that output is
   // not required and so input samples on mTaskQueue need not be processed.
   // Cleared on mTaskQueue in ProcessDrain().
   Atomic<bool> mIsFlushing;
   ReorderQueue mReorderQueue;
 
--- a/dom/media/platforms/apple/AppleVTDecoder.cpp
+++ b/dom/media/platforms/apple/AppleVTDecoder.cpp
@@ -118,19 +118,16 @@ AppleVTDecoder::ProcessFlush()
 {
   MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
   nsresult rv = WaitForAsynchronousFrames();
   if (NS_FAILED(rv)) {
     LOG("AppleVTDecoder::Flush failed waiting for platform decoder "
         "with error:%d.", rv);
   }
   ClearReorderedFrames();
-  MonitorAutoLock mon(mMonitor);
-  mIsFlushing = false;
-  mon.NotifyAll();
 }
 
 void
 AppleVTDecoder::ProcessDrain()
 {
   MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
   nsresult rv = WaitForAsynchronousFrames();
   if (NS_FAILED(rv)) {
--- a/dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp
+++ b/dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp
@@ -1,14 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "mozilla/SyncRunnable.h"
 #include "mozilla/TaskQueue.h"
 
 #include <string.h>
 #ifdef __GNUC__
 #include <unistd.h>
 #endif
 
 #include "FFmpegLog.h"
@@ -26,18 +27,16 @@ StaticMutex FFmpegDataDecoder<LIBAV_VER>
                                                   AVCodecID aCodecID)
   : mLib(aLib)
   , mTaskQueue(aTaskQueue)
   , mCallback(aCallback)
   , mCodecContext(nullptr)
   , mFrame(NULL)
   , mExtraData(nullptr)
   , mCodecID(aCodecID)
-  , mMonitor("FFMpegaDataDecoder")
-  , mIsFlushing(false)
 {
   MOZ_ASSERT(aLib);
   MOZ_COUNT_CTOR(FFmpegDataDecoder);
 }
 
 FFmpegDataDecoder<LIBAV_VER>::~FFmpegDataDecoder()
 {
   MOZ_COUNT_DTOR(FFmpegDataDecoder);
@@ -111,25 +110,20 @@ FFmpegDataDecoder<LIBAV_VER>::Shutdown()
   }
   return NS_OK;
 }
 
 nsresult
 FFmpegDataDecoder<LIBAV_VER>::Flush()
 {
   MOZ_ASSERT(mCallback->OnReaderTaskQueue());
-  mIsFlushing = true;
   mTaskQueue->Flush();
   nsCOMPtr<nsIRunnable> runnable =
     NS_NewRunnableMethod(this, &FFmpegDataDecoder<LIBAV_VER>::ProcessFlush);
-  MonitorAutoLock mon(mMonitor);
-  mTaskQueue->Dispatch(runnable.forget());
-  while (mIsFlushing) {
-    mon.Wait();
-  }
+  SyncRunnable::DispatchToThread(mTaskQueue, runnable);
   return NS_OK;
 }
 
 nsresult
 FFmpegDataDecoder<LIBAV_VER>::Drain()
 {
   MOZ_ASSERT(mCallback->OnReaderTaskQueue());
   nsCOMPtr<nsIRunnable> runnable =
@@ -140,19 +134,16 @@ FFmpegDataDecoder<LIBAV_VER>::Drain()
 
 void
 FFmpegDataDecoder<LIBAV_VER>::ProcessFlush()
 {
   MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
   if (mCodecContext) {
     mLib->avcodec_flush_buffers(mCodecContext);
   }
-  MonitorAutoLock mon(mMonitor);
-  mIsFlushing = false;
-  mon.NotifyAll();
 }
 
 void
 FFmpegDataDecoder<LIBAV_VER>::ProcessShutdown()
 {
   StaticMutexAutoLock mon(sMonitor);
 
   if (mCodecContext) {
--- a/dom/media/platforms/ffmpeg/FFmpegDataDecoder.h
+++ b/dom/media/platforms/ffmpeg/FFmpegDataDecoder.h
@@ -52,23 +52,15 @@ protected:
   RefPtr<FlushableTaskQueue> mTaskQueue;
   MediaDataDecoderCallback* mCallback;
 
   AVCodecContext* mCodecContext;
   AVFrame*        mFrame;
   RefPtr<MediaByteBuffer> mExtraData;
   AVCodecID mCodecID;
 
-  // For wait on mIsFlushing during Shutdown() process.
-  // Protects mReorderQueue.
-  Monitor mMonitor;
-  // Set on reader/decode thread calling Flush() to indicate that output is
-  // not required and so input samples on mTaskQueue need not be processed.
-  // Cleared on mTaskQueue in ProcessDrain().
-  Atomic<bool> mIsFlushing;
-
 private:
   static StaticMutex sMonitor;
 };
 
 } // namespace mozilla
 
 #endif // __FFmpegDataDecoder_h__
--- a/dom/media/platforms/wmf/WMFMediaDataDecoder.cpp
+++ b/dom/media/platforms/wmf/WMFMediaDataDecoder.cpp
@@ -6,29 +6,29 @@
 
 #include "WMFMediaDataDecoder.h"
 #include "VideoUtils.h"
 #include "WMFUtils.h"
 #include "nsTArray.h"
 #include "mozilla/Telemetry.h"
 
 #include "mozilla/Logging.h"
+#include "mozilla/SyncRunnable.h"
 
 extern mozilla::LogModule* GetPDMLog();
 #define LOG(...) MOZ_LOG(GetPDMLog(), mozilla::LogLevel::Debug, (__VA_ARGS__))
 
 namespace mozilla {
 
 WMFMediaDataDecoder::WMFMediaDataDecoder(MFTManager* aMFTManager,
                                          FlushableTaskQueue* aTaskQueue,
                                          MediaDataDecoderCallback* aCallback)
   : mTaskQueue(aTaskQueue)
   , mCallback(aCallback)
   , mMFTManager(aMFTManager)
-  , mMonitor("WMFMediaDataDecoder")
   , mIsFlushing(false)
   , mIsShutDown(false)
 {
 }
 
 WMFMediaDataDecoder::~WMFMediaDataDecoder()
 {
 }
@@ -115,22 +115,19 @@ WMFMediaDataDecoder::Input(MediaRawData*
       RefPtr<MediaRawData>(aSample));
   mTaskQueue->Dispatch(runnable.forget());
   return NS_OK;
 }
 
 void
 WMFMediaDataDecoder::ProcessDecode(MediaRawData* aSample)
 {
-  {
-    MonitorAutoLock mon(mMonitor);
-    if (mIsFlushing) {
-      // Skip sample, to be released by runnable.
-      return;
-    }
+  if (mIsFlushing) {
+    // Skip sample, to be released by runnable.
+    return;
   }
 
   HRESULT hr = mMFTManager->Input(aSample);
   if (FAILED(hr)) {
     NS_WARNING("MFTManager rejected sample");
     mCallback->Error();
     if (!mRecordedError) {
       SendTelemetry(hr);
@@ -169,47 +166,36 @@ WMFMediaDataDecoder::ProcessOutput()
 }
 
 void
 WMFMediaDataDecoder::ProcessFlush()
 {
   if (mMFTManager) {
     mMFTManager->Flush();
   }
-  MonitorAutoLock mon(mMonitor);
-  mIsFlushing = false;
-  mon.NotifyAll();
 }
 
 nsresult
 WMFMediaDataDecoder::Flush()
 {
   MOZ_ASSERT(mCallback->OnReaderTaskQueue());
   MOZ_DIAGNOSTIC_ASSERT(!mIsShutDown);
 
+  mIsFlushing = true;
   nsCOMPtr<nsIRunnable> runnable =
     NS_NewRunnableMethod(this, &WMFMediaDataDecoder::ProcessFlush);
-  MonitorAutoLock mon(mMonitor);
-  mIsFlushing = true;
-  mTaskQueue->Dispatch(runnable.forget());
-  while (mIsFlushing) {
-    mon.Wait();
-  }
+  SyncRunnable::DispatchToThread(mTaskQueue, runnable);
+  mIsFlushing = false;
   return NS_OK;
 }
 
 void
 WMFMediaDataDecoder::ProcessDrain()
 {
-  bool isFlushing;
-  {
-    MonitorAutoLock mon(mMonitor);
-    isFlushing = mIsFlushing;
-  }
-  if (!isFlushing && mMFTManager) {
+  if (!mIsFlushing && mMFTManager) {
     // Order the decoder to drain...
     mMFTManager->Drain();
     // Then extract all available output.
     ProcessOutput();
   }
   mCallback->DrainComplete();
 }
 
--- a/dom/media/platforms/wmf/WMFMediaDataDecoder.h
+++ b/dom/media/platforms/wmf/WMFMediaDataDecoder.h
@@ -120,22 +120,20 @@ private:
   MediaDataDecoderCallback* mCallback;
 
   nsAutoPtr<MFTManager> mMFTManager;
 
   // The last offset into the media resource that was passed into Input().
   // This is used to approximate the decoder's position in the media resource.
   int64_t mLastStreamOffset;
 
-  // For access to and waiting on mIsFlushing
-  Monitor mMonitor;
   // Set on reader/decode thread calling Flush() to indicate that output is
   // not required and so input samples on mTaskQueue need not be processed.
   // Cleared on mTaskQueue.
-  bool mIsFlushing;
+  Atomic<bool> mIsFlushing;
 
   bool mIsShutDown;
 
   // For telemetry
   bool mHasSuccessfulOutput = false;
   bool mRecordedError = false;
 };