Bug 1496281 - do not create audio stream if audio has ended. r=jya
authoralwu <alwu@mozilla.com>
Fri, 12 Oct 2018 22:23:40 +0000
changeset 489419 25bff27f9944c7bfea0372c2b6dc55e35977fb83
parent 489418 b8b3597d7702e4e6d7c3358f19384b49d8c220c0
child 489420 a5d9e6a7242dcb49be48adc839f071dd9a9a0316
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersjya
bugs1496281
milestone64.0a1
Bug 1496281 - do not create audio stream if audio has ended. r=jya We should not init the audio steam when the audio queue is marked as finished. Differential Revision: https://phabricator.services.mozilla.com/D8031
dom/media/mediasink/AudioSink.cpp
--- a/dom/media/mediasink/AudioSink.cpp
+++ b/dom/media/mediasink/AudioSink.cpp
@@ -71,16 +71,21 @@ AudioSink::~AudioSink()
 {
 }
 
 nsresult
 AudioSink::Init(const PlaybackParams& aParams, RefPtr<GenericPromise>& aEndPromise)
 {
   MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
 
+  if (mAudioQueue.IsFinished()) {
+    aEndPromise = GenericPromise::CreateAndResolve(true, __func__);
+    return NS_OK;
+  }
+
   mAudioQueueListener = mAudioQueue.PushEvent().Connect(
     mOwnerThread, this, &AudioSink::OnAudioPushed);
   mAudioQueueFinishListener = mAudioQueue.FinishEvent().Connect(
     mOwnerThread, this, &AudioSink::NotifyAudioNeeded);
   mProcessedQueueListener = mProcessedQueue.PopEvent().Connect(
     mOwnerThread, this, &AudioSink::OnAudioPopped);
 
   // To ensure at least one audio packet will be popped from AudioQueue and
@@ -126,19 +131,19 @@ AudioSink::HasUnplayedFrames()
          (mAudioStream && mAudioStream->GetPositionInFrames() + 1 < total);
 }
 
 void
 AudioSink::Shutdown()
 {
   MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
 
-  mAudioQueueListener.Disconnect();
-  mAudioQueueFinishListener.Disconnect();
-  mProcessedQueueListener.Disconnect();
+  mAudioQueueListener.DisconnectIfExists();
+  mAudioQueueFinishListener.DisconnectIfExists();
+  mProcessedQueueListener.DisconnectIfExists();
 
   if (mAudioStream) {
     mAudioStream->Shutdown();
     mAudioStream = nullptr;
   }
   mProcessedQueue.Reset();
   mProcessedQueue.Finish();
   mEndPromise.ResolveIfExists(true, __func__);