Bug 1404997 - P18. Use new Await method with WebrtcMediaDataDecoder. r?gerald draft
authorJean-Yves Avenard <jyavenard@mozilla.com>
Fri, 08 Dec 2017 17:30:28 +0100
changeset 710548 569dde4e26d6af585fe3c5e3739c1fd3627cf122
parent 710547 3c29e42153410ecced1f28b28bd82fe908968606
child 710549 a5f6f40f52428c0f5c65aa52b029aba016082a51
push id92845
push userbmo:jyavenard@mozilla.com
push dateSun, 10 Dec 2017 22:43:10 +0000
reviewersgerald
bugs1404997
milestone59.0a1
Bug 1404997 - P18. Use new Await method with WebrtcMediaDataDecoder. r?gerald MozReview-Commit-ID: nRQL312aDE
media/webrtc/signaling/src/media-conduit/WebrtcMediaDataDecoderCodec.cpp
media/webrtc/signaling/src/media-conduit/WebrtcMediaDataDecoderCodec.h
--- a/media/webrtc/signaling/src/media-conduit/WebrtcMediaDataDecoderCodec.cpp
+++ b/media/webrtc/signaling/src/media-conduit/WebrtcMediaDataDecoderCodec.cpp
@@ -2,29 +2,29 @@
  * 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 "WebrtcMediaDataDecoderCodec.h"
 #include "ImageContainer.h"
 #include "Layers.h"
 #include "PDMFactory.h"
 #include "VideoUtils.h"
+#include "mozilla/media/MediaUtils.h"
 #include "mozilla/layers/ImageBridgeChild.h"
 #include "webrtc/base/keep_ref_until_done.h"
 
 namespace mozilla {
 
 WebrtcMediaDataDecoder::WebrtcMediaDataDecoder()
   : mTaskQueue(
       new TaskQueue(GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER),
                     "WebrtcMediaDataDecoder::mTaskQueue"))
   , mImageContainer(layers::LayerManager::CreateImageContainer(
       layers::ImageContainer::ASYNCHRONOUS))
   , mFactory(new PDMFactory())
-  , mMonitor("WebrtcMediaDataDecoder")
 {
 }
 
 WebrtcMediaDataDecoder::~WebrtcMediaDataDecoder()
 {
   mTaskQueue->BeginShutdown();
   mTaskQueue->AwaitShutdownAndIdle();
 }
@@ -63,35 +63,19 @@ WebrtcMediaDataDecoder::InitDecode(const
       mTrackType,
       mImageContainer,
       knowsCompositor });
 
   if (!mDecoder) {
     return WEBRTC_VIDEO_CODEC_ERROR;
   }
 
-  MonitorAutoLock lock(mMonitor);
-  bool done = false;
-  mDecoder->Init()->Then(mTaskQueue,
-                         __func__,
-                         [&](TrackInfo::TrackType) {
-                           MonitorAutoLock lock(mMonitor);
-                           done = true;
-                           mMonitor.Notify();
-                         },
-                         [&](const MediaResult& aError) {
-                           MonitorAutoLock lock(mMonitor);
-                           done = true;
-                           mError = aError;
-                           mMonitor.Notify();
-                         });
-
-  while (!done) {
-    mMonitor.Wait();
-  }
+  media::Await(mDecoder->Init(),
+               [](TrackInfo::TrackType) {},
+               [&](const MediaResult& aError) { mError = aError; });
 
   return NS_SUCCEEDED(mError) ? WEBRTC_VIDEO_CODEC_OK : WEBRTC_VIDEO_CODEC_ERROR;
 }
 
 int32_t
 WebrtcMediaDataDecoder::Decode(
   const webrtc::EncodedImage& aInputImage,
   bool aMissingFrames,
@@ -127,37 +111,21 @@ WebrtcMediaDataDecoder::Decode(
 
   compressedFrame->mTime =
     media::TimeUnit::FromMicroseconds(aInputImage._timeStamp);
   compressedFrame->mTimecode =
     media::TimeUnit::FromMicroseconds(aRenderTimeMs * 1000);
   compressedFrame->mKeyframe =
     aInputImage._frameType == webrtc::FrameType::kVideoFrameKey;
   {
-    MonitorAutoLock lock(mMonitor);
-    bool done = false;
-    mDecoder->Decode(compressedFrame)->Then(
-      mTaskQueue,
-      __func__,
-      [&](const MediaDataDecoder::DecodedData& aResults) {
-        MonitorAutoLock lock(mMonitor);
-        mResults = aResults;
-        done = true;
-        mMonitor.Notify();
-      },
-      [&](const MediaResult& aError) {
-        MonitorAutoLock lock(mMonitor);
-        mError = aError;
-        done = true;
-        mMonitor.Notify();
-      });
-
-    while (!done) {
-      mMonitor.Wait();
-    }
+    media::Await(mDecoder->Decode(compressedFrame),
+                 [&](const MediaDataDecoder::DecodedData& aResults) {
+                   mResults = aResults;
+                 },
+                 [&](const MediaResult& aError) { mError = aError; });
 
     for (auto& frame : mResults) {
       MOZ_ASSERT(frame->mType == MediaData::VIDEO_DATA);
       RefPtr<VideoData> video = frame->As<VideoData>();
       MOZ_ASSERT(video);
       if (!video->mImage) {
         // Nothing to display.
         continue;
@@ -183,35 +151,22 @@ WebrtcMediaDataDecoder::RegisterDecodeCo
 {
   mCallback = aCallback;
   return WEBRTC_VIDEO_CODEC_OK;
 }
 
 int32_t
 WebrtcMediaDataDecoder::Release()
 {
-  MonitorAutoLock lock(mMonitor);
-  bool done = false;
-  mDecoder->Flush()
-    ->Then(mTaskQueue,
-           __func__,
-           [this]() { return mDecoder->Shutdown(); },
-           [this](const MediaResult& aError) { return mDecoder->Shutdown(); })
-    ->Then(mTaskQueue,
-           __func__,
-           [&]() {
-             MonitorAutoLock lock(mMonitor);
-             done = true;
-             mMonitor.Notify();
-           },
-           []() { MOZ_ASSERT_UNREACHABLE("Shutdown promise always resolved"); });
-
-  while (!done) {
-    mMonitor.Wait();
-  }
+  RefPtr<ShutdownPromise> p =
+    mDecoder->Flush()->Then(mTaskQueue,
+                            __func__,
+                            [this]() { return mDecoder->Shutdown(); },
+                            [this]() { return mDecoder->Shutdown(); });
+  media::Await(p);
 
   mDecoder = nullptr;
   mNeedKeyframe = true;
 
   return WEBRTC_VIDEO_CODEC_OK;
 }
 
 bool
--- a/media/webrtc/signaling/src/media-conduit/WebrtcMediaDataDecoderCodec.h
+++ b/media/webrtc/signaling/src/media-conduit/WebrtcMediaDataDecoderCodec.h
@@ -67,17 +67,15 @@ private:
   const RefPtr<PDMFactory> mFactory;
   RefPtr<MediaDataDecoder> mDecoder;
   webrtc::DecodedImageCallback* mCallback = nullptr;
   VideoInfo mInfo;
   TrackInfo::TrackType mTrackType;
   bool mNeedKeyframe = true;
   MozPromiseRequestHolder<MediaDataDecoder::DecodePromise> mDecodeRequest;
 
-  Monitor mMonitor;
-  // Members below are accessed via mMonitor
   MediaResult mError = NS_OK;
   MediaDataDecoder::DecodedData mResults;
 };
 
 } // namespace mozilla
 
 #endif // WebrtcMediaDataDecoderCodec_h__