summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/dedicated-worker/mediasource-worker-util.js
blob: 7adaf82508d0d132423c74463b9a4e93bf59a0e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This script is intended to be imported into a worker's script, and provides
// common preparation for multiple test cases. Errors encountered are either
// postMessaged with subject of messageSubject.ERROR, or in the case of failed
// mediaLoadPromise, result in promise rejection.

importScripts("mediasource-message-util.js");

if (!this.MediaSource)
  postMessage({ subject: messageSubject.ERROR, info: "MediaSource API missing from Worker" });

let MEDIA_LIST = [
  {
    url: '../mp4/test.mp4',
    type: 'video/mp4; codecs="mp4a.40.2,avc1.4d400d"',
  },
  {
    url: '../webm/test.webm',
    type: 'video/webm; codecs="vp8, vorbis"',
  },
];

class MediaSourceWorkerUtil {
  constructor() {
    this.mediaSource = new MediaSource();

    // Find supported test media, if any.
    this.foundSupportedMedia = false;
    for (let i = 0; i < MEDIA_LIST.length; ++i) {
      this.mediaMetadata = MEDIA_LIST[i];
      if (MediaSource.isTypeSupported(this.mediaMetadata.type)) {
        this.foundSupportedMedia = true;
        break;
      }
    }

    // Begin asynchronous fetch of the test media.
    if (this.foundSupportedMedia) {
      this.mediaLoadPromise = MediaSourceWorkerUtil.loadBinaryAsync(this.mediaMetadata.url);
    } else {
      postMessage({ subject: messageSubject.ERROR, info: "No supported test media" });
    }
  }

  static loadBinaryAsync(url) {
    return new Promise((resolve, reject) => {
      let request = new XMLHttpRequest();
      request.open("GET", url, true);
      request.responseType = "arraybuffer";
      request.onerror = event => { reject(event); };
      request.onload = () => {
        if (request.status != 200) {
          reject("Unexpected loadData_ status code : " + request.status);
        }
        let response = new Uint8Array(request.response);
        resolve(response);
      };
      request.send();
    });
  }
}