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 370007 dde2ff867bb6de3d9e00fdd55c66ac7e8f97438b
parent 370006 f9000b2562512a633c35a16fa338354514d0107c
child 370008 7b09188e5c4af2d7bc861d5a60167f466b795b07
push id18977
push userjwwang@mozilla.com
push dateTue, 24 May 2016 03:49:46 +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