Bug 1374068: P2. Rewrite bits with lambdas. r=gerald
authorJean-Yves Avenard <jyavenard@mozilla.com>
Sun, 18 Jun 2017 17:57:35 +0200
changeset 596555 fd18e49efaaafb4eb3594faf956e0e963b9eba7a
parent 596554 7f312f55453ffd8406f61341e4ec6dd0e49dea74
child 596556 0d08acc5a759cb6e148ba482f2e597f9857602b7
push id64675
push usermak77@bonardo.net
push dateMon, 19 Jun 2017 12:08:05 +0000
reviewersgerald
bugs1374068
milestone56.0a1
Bug 1374068: P2. Rewrite bits with lambdas. r=gerald Allow to get rid of the mPendingSample member, making the logic easier to follow. MozReview-Commit-ID: F7a25p1TP8J
dom/media/platforms/wrappers/H264Converter.cpp
dom/media/platforms/wrappers/H264Converter.h
--- a/dom/media/platforms/wrappers/H264Converter.cpp
+++ b/dom/media/platforms/wrappers/H264Converter.cpp
@@ -190,17 +190,16 @@ H264Converter::Drain()
 RefPtr<ShutdownPromise>
 H264Converter::Shutdown()
 {
   mInitPromiseRequest.DisconnectIfExists();
   mDecodePromiseRequest.DisconnectIfExists();
   mFlushRequest.DisconnectIfExists();
   mShutdownRequest.DisconnectIfExists();
   mFlushPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
-  mPendingSample = nullptr;
   mNeedAVCC.reset();
 
   if (mShutdownPromise) {
     // We have a shutdown in progress, return that promise instead as we can't
     // shutdown a decoder twice.
     return mShutdownPromise.forget();
   }
   if (mDecoder) {
@@ -293,65 +292,56 @@ H264Converter::CreateDecoderAndInit(Medi
   if (inbandExtradata) {
     UpdateConfigFromExtraData(extra_data);
   }
 
   nsresult rv =
     CreateDecoder(mCurrentConfig, /* DecoderDoctorDiagnostics* */ nullptr);
 
   if (NS_SUCCEEDED(rv)) {
-    // Queue the incoming sample.
-    mPendingSample = aSample;
-
+    RefPtr<H264Converter> self = this;
+    RefPtr<MediaRawData> sample = aSample;
     mDecoder->Init()
-      ->Then(AbstractThread::GetCurrent()->AsTaskQueue(), __func__, this,
-             &H264Converter::OnDecoderInitDone,
-             &H264Converter::OnDecoderInitFailed)
+      ->Then(
+        AbstractThread::GetCurrent()->AsTaskQueue(),
+        __func__,
+        [self, sample, this](const TrackType aTrackType) {
+          mInitPromiseRequest.Complete();
+          mNeedAVCC =
+            Some(mDecoder->NeedsConversion() == ConversionRequired::kNeedAVCC);
+          mCanRecycleDecoder = Some(CanRecycleDecoder());
+
+          if (!mFlushPromise.IsEmpty()) {
+            // A Flush is pending, abort the current operation.
+            mFlushPromise.Resolve(true, __func__);
+            return;
+          }
+
+          DecodeFirstSample(sample);
+        },
+        [self, this](const MediaResult& aError) {
+          mInitPromiseRequest.Complete();
+
+          if (!mFlushPromise.IsEmpty()) {
+            // A Flush is pending, abort the current operation.
+            mFlushPromise.Reject(aError, __func__);
+            return;
+          }
+
+          mDecodePromise.Reject(
+            MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR,
+                        RESULT_DETAIL("Unable to initialize H264 decoder")),
+            __func__);
+        })
       ->Track(mInitPromiseRequest);
     return NS_ERROR_DOM_MEDIA_INITIALIZING_DECODER;
   }
   return rv;
 }
 
-void
-H264Converter::OnDecoderInitDone(const TrackType aTrackType)
-{
-  mInitPromiseRequest.Complete();
-  RefPtr<MediaRawData> sample = mPendingSample.forget();
-
-  mNeedAVCC =
-    Some(mDecoder->NeedsConversion() == ConversionRequired::kNeedAVCC);
-  mCanRecycleDecoder = Some(CanRecycleDecoder());
-
-  if (!mFlushPromise.IsEmpty()) {
-    // A Flush is pending, abort the current operation.
-    mFlushPromise.Resolve(true, __func__);
-    return;
-  }
-
-  DecodeFirstSample(sample);
-}
-
-void
-H264Converter::OnDecoderInitFailed(const MediaResult& aError)
-{
-  mInitPromiseRequest.Complete();
-
-  if (!mFlushPromise.IsEmpty()) {
-    // A Flush is pending, abort the current operation.
-    mFlushPromise.Reject(aError, __func__);
-    return;
-  }
-
-  mDecodePromise.Reject(
-    MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR,
-                RESULT_DETAIL("Unable to initialize H264 decoder")),
-    __func__);
-}
-
 bool
 H264Converter::CanRecycleDecoder() const
 {
   MOZ_ASSERT(mDecoder);
   return MediaPrefs::MediaDecoderCheckRecycling()
          && mDecoder->SupportDecoderRecycling();
 }
 
--- a/dom/media/platforms/wrappers/H264Converter.h
+++ b/dom/media/platforms/wrappers/H264Converter.h
@@ -66,34 +66,30 @@ private:
   // Returns NS_ERROR_FAILURE if error is permanent and can't be recovered and
   // will set mError accordingly.
   nsresult CreateDecoder(const VideoInfo& aConfig,
                          DecoderDoctorDiagnostics* aDiagnostics);
   nsresult CreateDecoderAndInit(MediaRawData* aSample);
   nsresult CheckForSPSChange(MediaRawData* aSample);
   void UpdateConfigFromExtraData(MediaByteBuffer* aExtraData);
 
-  void OnDecoderInitDone(const TrackType aTrackType);
-  void OnDecoderInitFailed(const MediaResult& aError);
-
   bool CanRecycleDecoder() const;
 
   void DecodeFirstSample(MediaRawData* aSample);
   void DrainThenFlushDecoder(MediaRawData* aPendingSample);
   void FlushThenShutdownDecoder(MediaRawData* aPendingSample);
 
   RefPtr<PlatformDecoderModule> mPDM;
   const VideoInfo mOriginalConfig;
   VideoInfo mCurrentConfig;
   // Current out of band extra data (as found in metadata's VideoInfo).
   RefPtr<MediaByteBuffer> mOriginalExtraData;
   RefPtr<layers::KnowsCompositor> mKnowsCompositor;
   RefPtr<layers::ImageContainer> mImageContainer;
   const RefPtr<TaskQueue> mTaskQueue;
-  RefPtr<MediaRawData> mPendingSample;
   RefPtr<MediaDataDecoder> mDecoder;
   MozPromiseRequestHolder<InitPromise> mInitPromiseRequest;
   MozPromiseRequestHolder<DecodePromise> mDecodePromiseRequest;
   MozPromiseHolder<DecodePromise> mDecodePromise;
   MozPromiseRequestHolder<FlushPromise> mFlushRequest;
   MediaDataDecoder::DecodedData mPendingFrames;
   MozPromiseRequestHolder<DecodePromise> mDrainRequest;
   MozPromiseRequestHolder<ShutdownPromise> mShutdownRequest;