Bug 1407354 - Replace MediaRecorder Session's DispatchStartEventRunnable with a NewRunnableMethod based impl. r?pehrsons draft
authorBryce Van Dyk <bvandyk@mozilla.com>
Tue, 10 Oct 2017 16:13:54 +1300
changeset 681123 a4216dbb34bf9a3830979bc441d685579a3b3a9b
parent 681122 a710be862405eda7d98825c83c983cbeb4d2b2e6
child 736096 468de53c16512c8a1d3816111b224800167476cc
push id84766
push userbvandyk@mozilla.com
push dateTue, 17 Oct 2017 01:14:58 +0000
reviewerspehrsons
bugs1407354
milestone58.0a1
Bug 1407354 - Replace MediaRecorder Session's DispatchStartEventRunnable with a NewRunnableMethod based impl. r?pehrsons MozReview-Commit-ID: Iq2SyCLjSDc
dom/media/MediaRecorder.cpp
--- a/dom/media/MediaRecorder.cpp
+++ b/dom/media/MediaRecorder.cpp
@@ -285,45 +285,16 @@ class MediaRecorder::Session: public Pri
                                                 mBuffer[i].Length());
         }
       }
 
       return NS_OK;
     }
   };
 
-  // Fire start event and set mimeType, run in main thread task.
-  class DispatchStartEventRunnable : public Runnable
-  {
-  public:
-    DispatchStartEventRunnable(Session* aSession, const nsAString& aEventName)
-      : Runnable("dom::MediaRecorder::Session::DispatchStartEventRunnable")
-      , mSession(aSession)
-      , mEventName(aEventName)
-    { }
-
-    NS_IMETHOD Run() override
-    {
-      LOG(LogLevel::Debug, ("Session.DispatchStartEventRunnable s=(%p)", mSession.get()));
-      MOZ_ASSERT(NS_IsMainThread());
-
-      NS_ENSURE_TRUE(mSession->mRecorder, NS_OK);
-      RefPtr<MediaRecorder> recorder = mSession->mRecorder;
-
-      recorder->SetMimeType(mSession->mMimeType);
-      recorder->DispatchSimpleEvent(mEventName);
-
-      return NS_OK;
-    }
-
-  private:
-    RefPtr<Session> mSession;
-    nsString mEventName;
-  };
-
   // To ensure that MediaRecorder has tracks to record.
   class TracksAvailableCallback : public OnTracksAvailableCallback
   {
   public:
     explicit TracksAvailableCallback(Session *aSession)
      : mSession(aSession) {}
 
     virtual void NotifyTracksAvailable(DOMMediaStream* aStream)
@@ -567,18 +538,37 @@ public:
 private:
   // Only Destroy() is allowed to delete Session object on main thread.
   virtual ~Session()
   {
     MOZ_ASSERT(NS_IsMainThread());
     MOZ_ASSERT(mShutdownPromise);
     LOG(LogLevel::Debug, ("Session.~Session (%p)", this));
   }
+
+  // Fire start event and set mimeType, run in main thread task and done
+  // synchronously.
+  void DispatchStartEvent()
+  {
+    LOG(LogLevel::Debug, ("Session.DispatchStartEvent s=(%p)", this));
+    MOZ_ASSERT(NS_IsMainThread());
+
+    if (!mRecorder) {
+      // We're defensive just in case, but this shouldn't happen
+      MOZ_ASSERT_UNREACHABLE(
+        "DispatchStartEvent() called on already shutdown session");
+      return;
+    }
+
+    mRecorder->SetMimeType(mMimeType);
+    mRecorder->DispatchSimpleEvent(NS_LITERAL_STRING("start"));
+  }
+
   // Pull encoded media data from MediaEncoder and put into MutableBlobStorage.
-  // Destroy this session object in the end of this function.
+  // Destroy this session object in the end of this function if aShouldDestroy.
   // If the bool aForceFlush is true, we will force to dispatch a
   // PushBlobRunnable to main thread.
   void Extract(bool aForceFlush, bool aShouldDestroy)
   {
     MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
 
     LOG(LogLevel::Debug, ("Session.Extract %p", this));
 
@@ -914,18 +904,23 @@ private:
     mNeedSessionEndTask = false;
   }
 
   // application should get blob and onstop event
   void DoSessionEndTask(nsresult rv)
   {
     MOZ_ASSERT(NS_IsMainThread());
     if (!mIsStartEventFired) {
-      NS_DispatchToMainThread(
-        new DispatchStartEventRunnable(this, NS_LITERAL_STRING("start")));
+      if (NS_FAILED(NS_DispatchToMainThread(
+            NewRunnableMethod("dom::MediaRecorder:Session::DispatchStartEvent",
+                              this,
+                              &Session::DispatchStartEvent)))) {
+        MOZ_ASSERT(false, "NS_DispatchToMainThread DispatchStartEvent failed");
+      }
+      mIsStartEventFired = true;
     }
 
     if (NS_FAILED(rv)) {
       mRecorder->ForceInactive();
       NS_DispatchToMainThread(
         NewRunnableMethod<nsresult>("dom::MediaRecorder::NotifyError",
                                     mRecorder,
                                     &MediaRecorder::NotifyError,
@@ -965,18 +960,22 @@ private:
                                                            Move(encodedBuf)));
   }
 
   void MediaEncoderDataAvailable()
   {
     MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
 
     if (!mIsStartEventFired) {
-      NS_DispatchToMainThread(
-        new DispatchStartEventRunnable(this, NS_LITERAL_STRING("start")));
+      if (NS_FAILED(NS_DispatchToMainThread(
+            NewRunnableMethod("dom::MediaRecorder:Session::DispatchStartEvent",
+                              this,
+                              &Session::DispatchStartEvent)))) {
+        MOZ_ASSERT(false, "NS_DispatchToMainThread DispatchStartEvent failed");
+      }
       mIsStartEventFired = true;
     }
 
     Extract(false, false);
   }
 
   void MediaEncoderError()
   {