Bug 1560215 - Accomodate extra async step added by RunOnGraph. r=padenot
authorAndreas Pehrson <apehrson@mozilla.com>
Tue, 02 Jul 2019 12:03:34 +0000
changeset 540590 44341752990fa3534e9d5e5affb2c75c00eb0ad6
parent 540589 c66b92abbb53e007868ec7456a76d4726993abe6
child 540591 ab46619fcd019ff310722dcdb6e5aff1e72852c2
push id11529
push userarchaeopteryx@coole-files.de
push dateThu, 04 Jul 2019 15:22:33 +0000
treeherdermozilla-beta@ebb510a784b8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1560215
milestone69.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 1560215 - Accomodate extra async step added by RunOnGraph. r=padenot It leads to a race with MediaRecorder::Session::Shutdown where the RunOnGraph runner was dispatched to the graph before, but tries to dispatch to the encoder thread after, the encoder thread had BeginShutdown() called on it. Allowing the encoder thread dispatch to fail in this case is reasonable and safe. Depends on D36508 Differential Revision: https://phabricator.services.mozilla.com/D36509
dom/media/encoder/MediaEncoder.cpp
--- a/dom/media/encoder/MediaEncoder.cpp
+++ b/dom/media/encoder/MediaEncoder.cpp
@@ -452,51 +452,52 @@ void MediaEncoder::RunOnGraph(already_Ad
     void Run() override { mRunnable->Run(); }
     const RefPtr<Runnable> mRunnable;
   };
   graph->AppendMessage(MakeUnique<Message>(std::move(aRunnable)));
 }
 
 void MediaEncoder::Suspend() {
   RunOnGraph(NS_NewRunnableFunction(
-      "MediaEncoder::Suspend",
+      "MediaEncoder::Suspend (graph)",
       [thread = mEncoderThread, audio = mAudioEncoder, video = mVideoEncoder] {
-        if (audio) {
-          nsresult rv = thread->Dispatch(
-              NewRunnableMethod("AudioTrackEncoder::Suspend", audio,
-                                &AudioTrackEncoder::Suspend));
-          MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
-          Unused << rv;
-        }
-        if (video) {
-          nsresult rv = thread->Dispatch(NewRunnableMethod<TimeStamp>(
-              "VideoTrackEncoder::Suspend", video, &VideoTrackEncoder::Suspend,
-              TimeStamp::Now()));
-          MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
-          Unused << rv;
+        if (NS_FAILED(thread->Dispatch(
+                NS_NewRunnableFunction("MediaEncoder::Suspend (encoder)",
+                                       [audio, video, now = TimeStamp::Now()] {
+                                         if (audio) {
+                                           audio->Suspend();
+                                         }
+                                         if (video) {
+                                           video->Suspend(now);
+                                         }
+                                       })))) {
+          // RunOnGraph added an extra async step, and now `thread` has shut
+          // down.
+          return;
         }
       }));
 }
 
 void MediaEncoder::Resume() {
   RunOnGraph(NS_NewRunnableFunction(
-      "MediaEncoder::Resume",
+      "MediaEncoder::Resume (graph)",
       [thread = mEncoderThread, audio = mAudioEncoder, video = mVideoEncoder] {
-        if (audio) {
-          nsresult rv = thread->Dispatch(NewRunnableMethod(
-              "AudioTrackEncoder::Resume", audio, &AudioTrackEncoder::Resume));
-          MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
-          Unused << rv;
-        }
-        if (video) {
-          nsresult rv = thread->Dispatch(NewRunnableMethod<TimeStamp>(
-              "VideoTrackEncoder::Resume", video, &VideoTrackEncoder::Resume,
-              TimeStamp::Now()));
-          MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
-          Unused << rv;
+        if (NS_FAILED(thread->Dispatch(
+                NS_NewRunnableFunction("MediaEncoder::Resume (encoder)",
+                                       [audio, video, now = TimeStamp::Now()] {
+                                         if (audio) {
+                                           audio->Resume();
+                                         }
+                                         if (video) {
+                                           video->Resume(now);
+                                         }
+                                       })))) {
+          // RunOnGraph added an extra async step, and now `thread` has shut
+          // down.
+          return;
         }
       }));
 }
 
 void MediaEncoder::ConnectAudioNode(AudioNode* aNode, uint32_t aOutput) {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (mAudioNode) {