tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mediasource-worker-detach-element.html (3066B)


      1 <!DOCTYPE html>
      2 <html>
      3 <title>MediaSource-in-Worker buffering test case with media element detachment at various places</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="mediasource-message-util.js"></script>
      7 <body>
      8 <script>
      9 
     10 const AFTER_SETTING_SRCOBJECT = "after setting srcObject";
     11 const AFTER_STARTED_BUFFERING = "after receiving Started Buffering message from worker";
     12 const AFTER_FINISHED_BUFFERING = "after receiving Finished Buffering message from worker";
     13 
     14 [ AFTER_SETTING_SRCOBJECT, AFTER_STARTED_BUFFERING, AFTER_FINISHED_BUFFERING ].forEach(when => {
     15  for (let timeouts = 0; timeouts < 5; ++timeouts) {
     16    async_test(test => { startWorkerAndDetachElement(test, when, timeouts); },
     17        "Test element detachment from worker MediaSource after at least " + timeouts +
     18          " main thread setTimeouts, starting counting " + when);
     19  }
     20 });
     21 
     22 function detachElementAfterMultipleSetTimeouts(test, element, timeouts_remaining) {
     23  if (timeouts_remaining <= 0) {
     24    // While not the best way to detach, this triggers interoperable logic that
     25    // includes detachment.
     26    element.srcObject = null;
     27    test.step_timeout(() => { test.done(); }, 10);
     28  } else {
     29    test.step_timeout(() => {
     30      detachElementAfterMultipleSetTimeouts(test, element, --timeouts_remaining);
     31    }, 0);
     32  }
     33 }
     34 
     35 function startWorkerAndDetachElement(test, when_to_start_timeouts, timeouts_to_await) {
     36  // Fail fast if MSE-in-Workers is not supported.
     37  assert_true(MediaSource.hasOwnProperty("canConstructInDedicatedWorker"), "MediaSource hasOwnProperty 'canConstructInDedicatedWorker'");
     38  assert_true(MediaSource.canConstructInDedicatedWorker, "MediaSource.canConstructInDedicatedWorker");
     39 
     40  const worker = new Worker("mediasource-worker-detach-element.js");
     41  worker.onerror = test.unreached_func("worker error");
     42 
     43  const video = document.createElement("video");
     44  document.body.appendChild(video);
     45 
     46  worker.onmessage = test.step_func(e => {
     47    let subject = e.data.subject;
     48    assert_true(subject != undefined, "message must have a subject field");
     49    switch (subject) {
     50      case messageSubject.ERROR:
     51        assert_unreached("Worker error: " + e.data.info);
     52        break;
     53      case messageSubject.HANDLE:
     54        const handle = e.data.info;
     55        video.srcObject = handle;
     56        if (when_to_start_timeouts == AFTER_SETTING_SRCOBJECT) {
     57          detachElementAfterMultipleSetTimeouts(test, video, timeouts_to_await);
     58        }
     59        break;
     60      case messageSubject.STARTED_BUFFERING:
     61        if (when_to_start_timeouts == AFTER_STARTED_BUFFERING)
     62          detachElementAfterMultipleSetTimeouts(test, video, timeouts_to_await);
     63        break;
     64      case messageSubject.FINISHED_BUFFERING:
     65        if (when_to_start_timeouts == AFTER_FINISHED_BUFFERING)
     66          detachElementAfterMultipleSetTimeouts(test, video, timeouts_to_await);
     67        break;
     68      default:
     69        assert_unreached("Unrecognized message subject: " + subject);
     70    }
     71  });
     72 }
     73 </script>
     74 </body>
     75 </html>