MSE-in-Workers: Cross-thread function and tests (#26109)

Includes implementation of CrossThreadMediaSourceAttachment (CTMSA) and
updates to MediaSource and SourceBuffer to use it.

CTMSA owns a lock that it takes on most operations. It also provides a
utility method (RunExclusively()), which is used by the MSE API to take
that same lock during most operations on the API. The same thread
attachment provides a basic version of the same, though no lock is used.

CTMSA understands whether or not it is attached, has ever started
closing, and whether or not either of the attached contexts has ever
destructed. It conditions cross-thread operations to safely behave when
one or both contexts is being destructed.

MediaSource is given its own small lock that it uses to guard its
reference to the attachment instance, when attached. This is required
because attachment start is synchronous on the main thread, even if the
MediaSource is owned by worker context. CTMSA and MediaSource cooperate
to ensure mutex and safety of the two-stage attachment start. In
MediaSource::ContextDestroyed(), further care is taken to prevent
attachment start success when worker context destruction is possibly
racing the main thread's attempt to start attachment.
`context_already_destroyed_`, protected by MediaSource's lock, is used
to prevent that start from succeeding.

MediaSource's context destruction can no longer always assume that
accessing the demuxer via the WebMediaSource (and WebSourceBuffers) is
safe. The worker version of MediaSource context destruction uses the
attachment's RunExclusively() to safely know if it can cleanly Close the
underlying demuxer, or if instead it must do a simpler shutdown.

Future specification work will likely determine some signal to the media
element when the worker-owned MediaSource's context is shutdown, yet the
element itself is still alive. For now, sane results are returned by the
attachment (nothing buffered nor seekable), with no other error
provided. Possible app workarounds might include main and worker
watchdogs and being careful about when the main thread explicitly
terminates the worker.

This experimental implementation attempts to retain
BackgroundVideoOptimization, even for MSE-in-Workers, but does not
support AudioVideoTracks in the worker MediaSource or SourceBuffer.
Plumbing of appropriately-identified track metadata parsed from
initialization segment in the worker is used to populate (and remove)
media element AudioVideoTracks that should agree with the track id's
used internally in WebMediaPlayerImpl to accomplish
BackgroundVideoOptimization.

As a simplification, CTMSA assumes it can be used for at most one
attachment. Effectively, this assumes that the currently on-by-default
MediaSourceRevokeObjectURLOnAttach feature behavior will be always-on
soon. If CTMSA is not unregistered automatically after successful
attachment start (e.g., if that feature is disabled) and if the app
attempts to re-use the corresponding object URL for a later attachment,
that attachment will be rejected by CTMSA. Note that the same-thread
attachment would still allow such re-use when that feature is disabled.

Updated web-test:
  mediasource-worker-attach.html changed to ...-play.html and fixed
New web-test:
  mediasource-worker-play-terminate-worker.html
Manual test: page refreshing while running mediasource-worker-play*.html

BUG=878133

Change-Id: I21f6542b90d51bdc28096500fb1441d202ab4ee8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2459431
Commit-Queue: Matthew Wolenetz <wolenetz@chromium.org>
Reviewed-by: Will Cassella <cassew@google.com>
Cr-Commit-Position: refs/heads/master@{#818174}

Co-authored-by: Matt Wolenetz <wolenetz@chromium.org>
diff --git a/media-source/dedicated-worker/mediasource-worker-play-terminate-worker.html b/media-source/dedicated-worker/mediasource-worker-play-terminate-worker.html
new file mode 100644
index 0000000..d1c1c3d
--- /dev/null
+++ b/media-source/dedicated-worker/mediasource-worker-play-terminate-worker.html
@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<html>
+<title>MediaSource-in-Worker looped playback test case with worker termination at various places</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<body>
+<script>
+
+function terminateWorkerAfterMultipleSetTimeouts(test, worker, timeouts_remaining) {
+  if (timeouts_remaining <= 0) {
+    worker.terminate();
+    test.step_timeout(() => { test.done(); }, 0);
+  } else {
+    test.step_timeout(() => {
+      terminateWorkerAfterMultipleSetTimeouts(test, worker, --timeouts_remaining);
+    }, 0);
+  }
+}
+
+function startWorkerAndTerminateWorker(test, when_to_start_timeouts, timeouts_to_await) {
+  const worker = new Worker("mediasource-worker-util.js");
+  worker.onerror = test.unreached_func("worker error");
+
+  const video = document.createElement("video");
+  document.body.appendChild(video);
+  video.onerror = test.unreached_func("video element error");
+
+  if (when_to_start_timeouts == "after first ended event") {
+    video.addEventListener("ended", test.step_func(() => {
+      terminateWorkerAfterMultipleSetTimeouts(test, worker, timeouts_to_await);
+      video.currentTime = 0;
+      video.loop = true;
+    }), { once : true });
+  } else {
+    video.loop = true;
+  }
+
+  if (when_to_start_timeouts == "before setting src") {
+    terminateWorkerAfterMultipleSetTimeouts(test, worker, timeouts_to_await);
+  }
+
+  worker.onmessage = test.step_func((e) => {
+    if (e.data.substr(0,6) == "Error:") {
+      assert_unreached("Worker error: " + e.data);
+    } else {
+      const url = e.data
+      assert_true(url.match(/^blob:.+/) != null);
+      video.src = url;
+      if (when_to_start_timeouts == "after setting src") {
+        terminateWorkerAfterMultipleSetTimeouts(test, worker, timeouts_to_await);
+      }
+      video.play().catch(error => {
+        // Only rejections due to MEDIA_ERR_SRC_NOT_SUPPORTED are expected to possibly
+        // occur, except if we expect to reach at least 1 'ended' event.
+        assert_not_equals(when_to_start_timeouts, "after first ended event");
+        assert_true(video.error != null);
+        assert_equals(video.error.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED);
+        // Do not rethrow. Instead, wait for the step_timeouts to finish the test.
+      });
+    }
+  });
+}
+
+[ "before setting src", "after setting src", "after first ended event" ].forEach((when) => {
+  for (let timeouts = 0; timeouts < 10; ++timeouts) {
+    async_test((test) => { startWorkerAndTerminateWorker(test, when, timeouts); },
+        "Test worker MediaSource termination after at least " + timeouts +
+          " main thread setTimeouts, starting counting " + when);
+  }
+});
+
+</script>
+</body>
+</html>
diff --git a/media-source/dedicated-worker/mediasource-worker-attach.html b/media-source/dedicated-worker/mediasource-worker-play.html
similarity index 60%
rename from media-source/dedicated-worker/mediasource-worker-attach.html
rename to media-source/dedicated-worker/mediasource-worker-play.html
index b09f05c..200f8a8 100644
--- a/media-source/dedicated-worker/mediasource-worker-attach.html
+++ b/media-source/dedicated-worker/mediasource-worker-play.html
@@ -1,6 +1,6 @@
 <!DOCTYPE html>
 <html>
-<title>Test attachment to dedicated worker MediaSource</title>
+<title>Simple MediaSource-in-Worker playback test case</title>
 <script src="/resources/testharness.js"></script>
 <script src="/resources/testharnessreport.js"></script>
 <body>
@@ -9,15 +9,8 @@
 async_test((t) => {
   const video = document.createElement('video');
   document.body.appendChild(video);
-
-  // TODO(https://crbug.com/878133): Enable attachment success by
-  // completing the CrossThreadAttachment implementation. Currently,
-  // a custom Chromium MediaError.message is confirmed.
-  video.onerror = t.step_func(() => {
-    assert_not_equals(video.error, null);
-    assert_equals(video.error.message, "MEDIA_ELEMENT_ERROR: Unable to attach MediaSource");
-    t.done();
-  });
+  video.onerror = t.unreached_func("video element error");
+  video.onended = t.step_func_done();
 
   let worker = new Worker("mediasource-worker-util.js");
   worker.onerror = t.unreached_func("worker error");
@@ -28,9 +21,10 @@
       const url = e.data;
       assert_true(url.match(/^blob:.+/) != null);
       video.src = url;
+      video.play();
     }
   });
-}, "Test worker MediaSource attachment (currently should fail to attach)");
+}, "Test worker MediaSource construction, attachment, buffering and basic playback");
 
 // TODO(https://crbug.com/878133): Test multiple attachments to same worker
 // MediaSource racing each other: precisely one should win the race.
diff --git a/media-source/dedicated-worker/mediasource-worker-util.js b/media-source/dedicated-worker/mediasource-worker-util.js
index e0c4bdc..4cee486 100644
--- a/media-source/dedicated-worker/mediasource-worker-util.js
+++ b/media-source/dedicated-worker/mediasource-worker-util.js
@@ -13,11 +13,11 @@
 // Find supported test media, if any.
 let MEDIA_LIST = [
   {
-    url: 'mp4/test.mp4',
+    url: '../mp4/test.mp4',
     type: 'video/mp4; codecs="mp4a.40.2,avc1.4d400d"',
   },
   {
-    url: 'webm/test.webm',
+    url: '../webm/test.webm',
     type: 'video/webm; codecs="vp8, vorbis"',
   },
 ];
@@ -56,9 +56,6 @@
   postMessage("Error: No message expected by Worker");
 };
 
-// TODO(https://crbug.com/878133): Enable this path by completing the
-// CrossThreadMediaSourceAttachment implementation such that attachment can
-// actually succeed and 'sourceopen' be dispatched.
 mediaSource.addEventListener("sourceopen", () => {
   URL.revokeObjectURL(mediaSourceObjectUrl);
   sourceBuffer = mediaSource.addSourceBuffer(mediaMetadata.type);
@@ -66,10 +63,16 @@
     postMessage("Error: " + err);
   };
   sourceBuffer.onupdateend = () => {
+    // Reset the parser. Unnecessary for this buffering, except helps with test
+    // coverage.
+    sourceBuffer.abort();
     // Shorten the buffered media and test playback duration to avoid timeouts.
     sourceBuffer.remove(0.5, Infinity);
     sourceBuffer.onupdateend = () => {
       sourceBuffer.duration = 0.5;
+      // Issue changeType to the same type that we've already buffered.
+      // Unnecessary for this buffering, except helps with test coverage.
+      sourceBuffer.changeType(mediaMetadata.type);
       mediaSource.endOfStream();
     };
   };