tor-browser

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

mediasource-worker-play.js (2848B)


      1 importScripts("mediasource-worker-util.js");
      2 
      3 // Note, we do not use testharness.js utilities within the worker context
      4 // because it also communicates using postMessage to the main HTML document's
      5 // harness, and would confuse the test case message parsing there.
      6 
      7 onmessage = function(evt) {
      8  postMessage({ subject: messageSubject.ERROR, info: "No message expected by Worker"});
      9 };
     10 
     11 let util = new MediaSourceWorkerUtil();
     12 let handle = util.mediaSource.handle;
     13 
     14 util.mediaSource.addEventListener('sourceopen', () => {
     15  // Immediately re-verify the SameObject property of the handle we transferred.
     16  if (handle !== util.mediaSource.handle) {
     17    postMessage({
     18      subject: messageSubject.ERROR,
     19      info: 'mediaSource.handle changed from the original value'
     20    });
     21  }
     22 
     23  // Also verify that transferring the already-transferred handle instance is
     24  // prevented correctly.
     25  try {
     26    postMessage(
     27        {
     28          subject: messageSubject.ERROR,
     29          info:
     30              'This postMessage should fail: the handle has already been transferred',
     31          extra_info: util.mediaSource.handle
     32        },
     33        {transfer: [util.mediaSource.handle]});
     34  } catch (e) {
     35    if (e.name != 'DataCloneError') {
     36      postMessage({
     37        subject: messageSubject.ERROR,
     38        info: 'Expected handle retransfer exception did not occur'
     39      });
     40    }
     41  }
     42 
     43  sourceBuffer = util.mediaSource.addSourceBuffer(util.mediaMetadata.type);
     44  sourceBuffer.onerror = (err) => {
     45    postMessage({ subject: messageSubject.ERROR, info: err });
     46  };
     47  sourceBuffer.onupdateend = () => {
     48    // Reset the parser. Unnecessary for this buffering, except helps with test
     49    // coverage.
     50    sourceBuffer.abort();
     51    // Shorten the buffered media and test playback duration to avoid timeouts.
     52    sourceBuffer.remove(0.5, Infinity);
     53    sourceBuffer.onupdateend = () => {
     54      util.mediaSource.duration = 0.5;
     55      // Issue changeType to the same type that we've already buffered.
     56      // Unnecessary for this buffering, except helps with test coverage.
     57      sourceBuffer.changeType(util.mediaMetadata.type);
     58      util.mediaSource.endOfStream();
     59      // Sanity check the duration.
     60      // Unnecessary for this buffering, except helps with test coverage.
     61      var duration = util.mediaSource.duration;
     62      if (isNaN(duration) || duration <= 0.0 || duration >= 1.0) {
     63        postMessage({
     64          subject: messageSubject.ERROR,
     65          info: "mediaSource.duration " + duration + " is not within expected range (0,1)"
     66        });
     67      }
     68    };
     69  };
     70  util.mediaLoadPromise.then(mediaData => { sourceBuffer.appendBuffer(mediaData); },
     71                             err => { postMessage({ subject: messageSubject.ERROR, info: err }) });
     72 }, {once: true});
     73 
     74 postMessage({ subject: messageSubject.HANDLE, info: handle }, { transfer: [handle] });