Bug 1407354 - Replace MediaRecorder Session's PushBlobRunnable with a NewRunnableMethod based impl. r?pehrsons draft
authorBryce Van Dyk <bvandyk@mozilla.com>
Tue, 10 Oct 2017 15:47:17 +1300
changeset 680595 f5a755ef013e83b0ae28b7bec81d9227081fefa2
parent 680594 9e7d3c14edd47b1d7a6698f23c10ae1d7f0001b2
child 680596 e07dad35a031cbc4fafd419eea2e4d0aa28946f4
push id84556
push userbvandyk@mozilla.com
push dateSun, 15 Oct 2017 18:09:08 +0000
reviewerspehrsons
bugs1407354
milestone58.0a1
Bug 1407354 - Replace MediaRecorder Session's PushBlobRunnable with a NewRunnableMethod based impl. r?pehrsons MozReview-Commit-ID: 88KlCCpxbGs
dom/media/MediaRecorder.cpp
--- a/dom/media/MediaRecorder.cpp
+++ b/dom/media/MediaRecorder.cpp
@@ -193,48 +193,16 @@ NS_IMPL_RELEASE_INHERITED(MediaRecorder,
  * 3) MediaRecorder::Stop is called by user or the document is going to
  *    inactive or invisible.
  */
 class MediaRecorder::Session: public PrincipalChangeObserver<MediaStreamTrack>,
                               public DOMMediaStream::TrackListener
 {
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Session)
 
-  // Main thread task.
-  // Create a blob event and send back to client.
-  class PushBlobRunnable : public Runnable
-  {
-  public:
-    explicit PushBlobRunnable(Session* aSession)
-      : Runnable("dom::MediaRecorder::Session::PushBlobRunnable")
-      , mSession(aSession)
-    { }
-
-    NS_IMETHOD Run() override
-    {
-      LOG(LogLevel::Debug, ("Session.PushBlobRunnable s=(%p)", mSession.get()));
-      MOZ_ASSERT(NS_IsMainThread());
-
-      RefPtr<MediaRecorder> recorder = mSession->mRecorder;
-      if (!recorder) {
-        return NS_OK;
-      }
-
-      nsresult rv = recorder->CreateAndDispatchBlobEvent(mSession->GetEncodedData());
-      if (NS_FAILED(rv)) {
-        recorder->NotifyError(rv);
-      }
-
-      return NS_OK;
-    }
-
-  private:
-    RefPtr<Session> mSession;
-  };
-
   // Notify encoder error, run in main thread task. (Bug 1095381)
   class EncoderErrorNotifierRunnable : public Runnable
   {
   public:
     explicit EncoderErrorNotifierRunnable(Session* aSession)
       : Runnable("dom::MediaRecorder::Session::EncoderErrorNotifierRunnable")
       , mSession(aSession)
     { }
@@ -348,17 +316,16 @@ class MediaRecorder::Session: public Pri
     }
 
   protected:
     RefPtr<TaskQueue> mEncoderThread;
     RefPtr<Session> mSession;
   };
 
   friend class EncoderErrorNotifierRunnable;
-  friend class PushBlobRunnable;
   friend class TracksAvailableCallback;
 
 public:
   Session(MediaRecorder* aRecorder, int32_t aTimeSlice)
     : mRecorder(aRecorder)
     , mTimeSlice(aTimeSlice)
     , mStopIssued(false)
     , mIsStartEventFired(false)
@@ -478,19 +445,19 @@ public:
     return NS_OK;
   }
 
   nsresult RequestData()
   {
     LOG(LogLevel::Debug, ("Session.RequestData"));
     MOZ_ASSERT(NS_IsMainThread());
 
-    if (NS_FAILED(NS_DispatchToMainThread(new PushBlobRunnable(this)))) {
+    if (NS_FAILED(NS_DispatchToMainThread(NewRunnableMethod(
+          "dom::MediaRecorder:Session::PushBlob", this, &Session::PushBlob)))) {
       MOZ_ASSERT(false, "RequestData NS_DispatchToMainThread failed");
-      return NS_ERROR_FAILURE;
     }
 
     return NS_OK;
   }
 
   already_AddRefed<nsIDOMBlob> GetEncodedData()
   {
     MOZ_ASSERT(NS_IsMainThread());
@@ -520,20 +487,40 @@ 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));
   }
+
+  // Main thread task.
+  // Create a blob event and send back to client.
+  void PushBlob()
+  {
+    LOG(LogLevel::Debug, ("Session.PushBlob s=(%p)", this));
+    MOZ_ASSERT(NS_IsMainThread());
+
+    if (!mRecorder) {
+      // We're defensive just in case, but this shouldn't happen
+      MOZ_ASSERT_UNREACHABLE("PushBlob() called on already shutdown session");
+      return;
+    }
+
+    nsresult rv = mRecorder->CreateAndDispatchBlobEvent(GetEncodedData());
+    if (NS_FAILED(rv)) {
+      mRecorder->NotifyError(rv);
+    }
+  }
+
   // Pull encoded media data from MediaEncoder and put into EncodedBufferCache.
   // Destroy this session object in the end of this function.
   // If the bool aForceFlush is true, we will force to dispatch a
-  // PushBlobRunnable to main thread.
+  // PushBlob to main thread.
   void Extract(bool aForceFlush)
   {
     MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
 
     LOG(LogLevel::Debug, ("Session.Extract %p", this));
 
     AUTO_PROFILER_LABEL("MediaRecorder::Session::Extract", OTHER);
 
@@ -557,21 +544,23 @@ private:
     // need a flush.
     bool pushBlob = aForceFlush;
     if (!pushBlob &&
         mTimeSlice > 0 &&
         (TimeStamp::Now()-mLastBlobTimeStamp).ToMilliseconds() > mTimeSlice) {
       pushBlob = true;
     }
     if (pushBlob) {
-      if (NS_FAILED(NS_DispatchToMainThread(new PushBlobRunnable(this)))) {
-        MOZ_ASSERT(false, "NS_DispatchToMainThread PushBlobRunnable failed");
-      } else {
-        mLastBlobTimeStamp = TimeStamp::Now();
+      if (NS_FAILED(NS_DispatchToMainThread(
+            NewRunnableMethod("dom::MediaRecorder:Session::PushBlob",
+                              this,
+                              &Session::PushBlob)))) {
+        MOZ_ASSERT(false, "RequestData NS_DispatchToMainThread failed");
       }
+      mLastBlobTimeStamp = TimeStamp::Now();
     }
   }
 
   // Perform the steps needed to destroy a session. May reenter as this
   // function needs to stop the recorder before the session can be properly
   // shut down.
   void Destroy()
   {
@@ -878,18 +867,21 @@ private:
       NS_DispatchToMainThread(
         NewRunnableMethod<nsresult>("dom::MediaRecorder::NotifyError",
                                     mRecorder,
                                     &MediaRecorder::NotifyError,
                                     rv));
     }
     if (rv != NS_ERROR_DOM_SECURITY_ERR) {
       // Don't push a blob if there was a security error.
-      if (NS_FAILED(NS_DispatchToMainThread(new PushBlobRunnable(this)))) {
-        MOZ_ASSERT(false, "NS_DispatchToMainThread PushBlobRunnable failed");
+      if (NS_FAILED(NS_DispatchToMainThread(
+            NewRunnableMethod("dom::MediaRecorder:Session::PushBlob",
+                              this,
+                              &Session::PushBlob)))) {
+        MOZ_ASSERT(false, "RequestData NS_DispatchToMainThread failed");
       }
     }
     if (NS_FAILED(NS_DispatchToMainThread(NewRunnableMethod(
           "dom::MediaRecorder:Session::Destroy", this, &Session::Destroy)))) {
       MOZ_ASSERT(false, "NS_DispatchToMainThread DestroyRunnable failed");
     }
     mNeedSessionEndTask = false;
   }