Bug 1273405. Part 2 - remove use of FlushableTaskQueue::Flush(). r=jya. draft 1273405_VorbisDataDecoder_Flush
authorJW Wang <jwwang@mozilla.com>
Tue, 17 May 2016 17:22:45 +0800
branch1273405_VorbisDataDecoder_Flush
changeset 369076 80c1bd7e8a524035946a99b5c53bcd046326b52b
parent 369075 f4810f323ad9e78f856d7834e9c3b8f4b8d9af3c
child 369077 5f3f85c59dff9de7109fa79980230300ffd8ae4b
push id18726
push userjwwang@mozilla.com
push dateFri, 20 May 2016 07:01:13 +0000
reviewersjya
bugs1273405
milestone49.0a1
Bug 1273405. Part 2 - remove use of FlushableTaskQueue::Flush(). r=jya. MozReview-Commit-ID: bchmGXw1fw
dom/media/platforms/agnostic/VorbisDecoder.cpp
dom/media/platforms/agnostic/VorbisDecoder.h
--- a/dom/media/platforms/agnostic/VorbisDecoder.cpp
+++ b/dom/media/platforms/agnostic/VorbisDecoder.cpp
@@ -4,16 +4,17 @@
  * 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 "VorbisDecoder.h"
 #include "VorbisUtils.h"
 #include "XiphExtradata.h"
 
 #include "mozilla/PodOperations.h"
+#include "mozilla/SyncRunnable.h"
 #include "nsAutoPtr.h"
 
 #undef LOG
 extern mozilla::LogModule* GetPDMLog();
 #define LOG(type, msg) MOZ_LOG(GetPDMLog(), type, msg)
 
 namespace mozilla {
 
@@ -34,16 +35,17 @@ ogg_packet InitVorbisPacket(const unsign
 VorbisDataDecoder::VorbisDataDecoder(const AudioInfo& aConfig,
                                      FlushableTaskQueue* aTaskQueue,
                                      MediaDataDecoderCallback* aCallback)
   : mInfo(aConfig)
   , mTaskQueue(aTaskQueue)
   , mCallback(aCallback)
   , mPacketCount(0)
   , mFrames(0)
+  , mIsFlushing(false)
 {
   // Zero these member vars to avoid crashes in Vorbis clear functions when
   // destructor is called before |Init|.
   PodZero(&mVorbisBlock);
   PodZero(&mVorbisDsp);
   PodZero(&mVorbisInfo);
   PodZero(&mVorbisComment);
 }
@@ -133,16 +135,19 @@ VorbisDataDecoder::Input(MediaRawData* a
                        this, &VorbisDataDecoder::ProcessDecode, aSample));
 
   return NS_OK;
 }
 
 void
 VorbisDataDecoder::ProcessDecode(MediaRawData* aSample)
 {
+  if (mIsFlushing) {
+    return;
+  }
   if (DoDecode(aSample) == -1) {
     mCallback->Error();
   } else if (mTaskQueue->IsEmpty()) {
     mCallback->InputExhausted();
   }
 }
 
 int
@@ -264,22 +269,26 @@ VorbisDataDecoder::Drain()
 {
   mTaskQueue->Dispatch(NewRunnableMethod(this, &VorbisDataDecoder::ProcessDrain));
   return NS_OK;
 }
 
 nsresult
 VorbisDataDecoder::Flush()
 {
-  mTaskQueue->Flush();
-  // Ignore failed results from vorbis_synthesis_restart. They
-  // aren't fatal and it fails when ResetDecode is called at a
-  // time when no vorbis data has been read.
-  vorbis_synthesis_restart(&mVorbisDsp);
-  mLastFrameTime.reset();
+  mIsFlushing = true;
+  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([this] () {
+    // Ignore failed results from vorbis_synthesis_restart. They
+    // aren't fatal and it fails when ResetDecode is called at a
+    // time when no vorbis data has been read.
+    vorbis_synthesis_restart(&mVorbisDsp);
+    mLastFrameTime.reset();
+  });
+  SyncRunnable::DispatchToThread(mTaskQueue, r);
+  mIsFlushing = false;
   return NS_OK;
 }
 
 /* static */
 bool
 VorbisDataDecoder::IsVorbis(const nsACString& aMimeType)
 {
   return aMimeType.EqualsLiteral("audio/webm; codecs=vorbis") ||
--- a/dom/media/platforms/agnostic/VorbisDecoder.h
+++ b/dom/media/platforms/agnostic/VorbisDecoder.h
@@ -56,12 +56,13 @@ private:
   vorbis_comment mVorbisComment;
   vorbis_dsp_state mVorbisDsp;
   vorbis_block mVorbisBlock;
 
   int64_t mPacketCount;
   int64_t mFrames;
   Maybe<int64_t> mLastFrameTime;
   UniquePtr<AudioConverter> mAudioConverter;
+  Atomic<bool> mIsFlushing;
 };
 
 } // namespace mozilla
 #endif