Bug 1511542: Send RefPtr<WebrtcVideoConduit> back to main to be released in STS dispatches. r=dminor
authorByron Campen [:bwc] <docfaraday@gmail.com>
Fri, 05 Apr 2019 17:24:31 +0000
changeset 468358 f43307f2bad981db27268a2de5002cbb904b1a4d
parent 468357 55b2b70ee03601598418a38be63294b287e9444e
child 468359 ac6e176b391c4003109e0e3d286d021fbf891c22
push id35835
push useraciure@mozilla.com
push dateMon, 08 Apr 2019 19:00:29 +0000
treeherdermozilla-central@40456af7da1c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdminor
bugs1511542
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1511542: Send RefPtr<WebrtcVideoConduit> back to main to be released in STS dispatches. r=dminor Differential Revision: https://phabricator.services.mozilla.com/D26312
media/webrtc/signaling/src/media-conduit/VideoConduit.cpp
--- a/media/webrtc/signaling/src/media-conduit/VideoConduit.cpp
+++ b/media/webrtc/signaling/src/media-conduit/VideoConduit.cpp
@@ -974,21 +974,23 @@ bool WebrtcVideoConduit::SetRemoteSSRCLo
                   "%s: Failed to unset SSRC %u (0x%x) on other conduits,"
                   " bailing",
                   __FUNCTION__, ssrc, ssrc);
       return false;
     }
   }
 
   mRecvStreamConfig.rtp.remote_ssrc = ssrc;
-  mStsThread->Dispatch(
-      NS_NewRunnableFunction("WebrtcVideoConduit::WaitingForInitialSsrcNoMore",
-                             [this, self = RefPtr<WebrtcVideoConduit>(this)]() {
-                               mWaitingForInitialSsrc = false;
-                             }));
+  mStsThread->Dispatch(NS_NewRunnableFunction(
+      "WebrtcVideoConduit::WaitingForInitialSsrcNoMore",
+      [this, self = RefPtr<WebrtcVideoConduit>(this)]() mutable {
+        mWaitingForInitialSsrc = false;
+        NS_ReleaseOnMainThreadSystemGroup(
+            "WebrtcVideoConduit::WaitingForInitialSsrcNoMore", self.forget());
+      }));
   // On the next StartReceiving() or ConfigureRecvMediaCodec, force
   // building a new RecvStream to switch SSRCs.
   DeleteRecvStream();
 
   if (wasReceiving) {
     if (StartReceivingLocked() != kMediaConduitNoError) {
       return false;
     }
@@ -1092,36 +1094,40 @@ void WebrtcVideoConduit::PollStats() {
         "WebrtcVideoConduit::RecvStreamStatistics::Update",
         [this, self = RefPtr<WebrtcVideoConduit>(this),
          stats = std::move(stats)]() { mRecvStreamStats.Update(stats); }));
   }
   webrtc::Call::Stats stats = mCall->Call()->GetStats();
   mStsThread->Dispatch(NS_NewRunnableFunction(
       "WebrtcVideoConduit::UpdateStreamStatistics",
       [this, self = RefPtr<WebrtcVideoConduit>(this), stats = std::move(stats),
-       runnables = std::move(runnables)]() {
+       runnables = std::move(runnables)]() mutable {
         mCallStats.Update(stats);
         for (const auto& runnable : runnables) {
           runnable->Run();
         }
+        NS_ReleaseOnMainThreadSystemGroup(
+            "WebrtcVideoConduit::UpdateStreamStatistics", self.forget());
       }));
 }
 
 void WebrtcVideoConduit::UpdateVideoStatsTimer() {
   MOZ_ASSERT(NS_IsMainThread());
 
   bool transmitting = mEngineTransmitting;
   bool receiving = mEngineReceiving;
-  mStsThread->Dispatch(
-      NS_NewRunnableFunction("WebrtcVideoConduit::SetSendStreamStatsActive",
-                             [this, self = RefPtr<WebrtcVideoConduit>(this),
-                              transmitting, receiving]() {
-                               mSendStreamStats.SetActive(transmitting);
-                               mRecvStreamStats.SetActive(receiving);
-                             }));
+  mStsThread->Dispatch(NS_NewRunnableFunction(
+      "WebrtcVideoConduit::SetSendStreamStatsActive",
+      [this, self = RefPtr<WebrtcVideoConduit>(this), transmitting,
+       receiving]() mutable {
+        mSendStreamStats.SetActive(transmitting);
+        mRecvStreamStats.SetActive(receiving);
+        NS_ReleaseOnMainThreadSystemGroup(
+            "WebrtcVideoConduit::SetSendStreamStatsActive", self.forget());
+      }));
 
   bool shouldBeActive = transmitting || receiving;
   if (mVideoStatsTimerActive == shouldBeActive) {
     return;
   }
   mVideoStatsTimerActive = shouldBeActive;
   if (shouldBeActive) {
     nsTimerCallbackFunc callback = [](nsITimer*, void* aClosure) {
@@ -1881,21 +1887,23 @@ MediaConduitErrorCode WebrtcVideoConduit
     i420Buffer->CropAndScaleFrom(*frame.video_frame_buffer()->GetI420().get(),
                                  cropX, cropY, cropWidth, cropHeight);
     buffer = i420Buffer;
   }
 
   mVideoBroadcaster.OnFrame(webrtc::VideoFrame(
       buffer, frame.timestamp(), frame.render_time_ms(), frame.rotation()));
 
-  mStsThread->Dispatch(
-      NS_NewRunnableFunction("SendStreamStatistics::FrameDeliveredToEncoder",
-                             [self = RefPtr<WebrtcVideoConduit>(this), this]() {
-                               mSendStreamStats.FrameDeliveredToEncoder();
-                             }));
+  mStsThread->Dispatch(NS_NewRunnableFunction(
+      "SendStreamStatistics::FrameDeliveredToEncoder",
+      [self = RefPtr<WebrtcVideoConduit>(this), this]() mutable {
+        mSendStreamStats.FrameDeliveredToEncoder();
+        NS_ReleaseOnMainThreadSystemGroup(
+            "SendStreamStatistics::FrameDeliveredToEncoder", self.forget());
+      }));
   return kMediaConduitNoError;
 }
 
 // Transport Layer Callbacks
 
 MediaConduitErrorCode WebrtcVideoConduit::DeliverPacket(const void* data,
                                                         int len) {
   ASSERT_ON_THREAD(mStsThread);
@@ -1951,22 +1959,26 @@ MediaConduitErrorCode WebrtcVideoConduit
             // creating a GMPVideoCodec (in particular, H264) so it can
             // communicate errors to the PC.
             WebrtcGmpPCHandleSetter setter(mPCHandle);
             SetRemoteSSRC(
                 ssrc);  // this will likely re-create the VideoReceiveStream
             // We want to unblock the queued packets on the original thread
             mStsThread->Dispatch(NS_NewRunnableFunction(
                 "WebrtcVideoConduit::QueuedPacketsHandler",
-                [this, self, ssrc]() mutable {
+                [this, self = RefPtr<WebrtcVideoConduit>(this),
+                 ssrc]() mutable {
                   if (ssrc != mRecvSSRC) {
                     // this is an intermediate switch; another is in-flight
                     return;
                   }
-                  self->mRtpPacketQueue.DequeueAll(self);
+                  mRtpPacketQueue.DequeueAll(this);
+                  NS_ReleaseOnMainThreadSystemGroup(
+                      "WebrtcVideoConduit::QueuedPacketsHandler",
+                      self.forget());
                 }));
           }));
       return kMediaConduitNoError;
     }
   }
 
   CSFLogVerbose(LOGTAG, "%s: seq# %u, Len %d, SSRC %u (0x%x) ", __FUNCTION__,
                 (uint16_t)ntohs(((uint16_t*)data)[1]), len,