summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/mediacapture-insertable-streams/legacy/MediaStreamTrackGenerator-pipes-data-in-worker.https.html
blob: 61a6f038c4b6c480053f39990bdcce1543dc9c76 (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
<!doctype html>
<title>Test piping data through MediaStreamTrackGenerator in a worker</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script id="workerCode" type="javascript/worker">
self.onmessage = (e) => {
  try {
    const generator = new MediaStreamTrackGenerator({kind: 'video'});
    e.data.readable.pipeTo(generator.writable);
    self.postMessage({result: 'Success'});
  } catch (e) {
    self.postMessage({result: 'Failure', error: e});
  }
}
</script>
<script>
'use strict';

promise_test(async t => {
    const workerBlob = new Blob([document.querySelector('#workerCode').textContent],
        { type: "text/javascript" });
    const workerUrl = window.URL.createObjectURL(workerBlob);
    const worker = new Worker(workerUrl);
    window.URL.revokeObjectURL(workerUrl);
    const result = new Promise((resolve, reject) => {
        worker.onmessage = (e) => {
            if (e.data.result === 'Failure') {
                reject('Failed with error ' + e.data.error);
            } else {
                resolve();
            }
        };
    });
    const stream = await navigator.mediaDevices.getUserMedia({ video: true });
    const track = stream.getVideoTracks()[0];
    const processor = new MediaStreamTrackProcessor({ track: track });
    worker.postMessage({ readable: processor.readable },
        [processor.readable]);
    return result;
}, 'A worker is able to pipe data through a MediaStreamTrackGenerator without crashing');
</script>