summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webcodecs/videoFrame-serialization.crossAgentCluster.https.html
blob: 5822cb8415e2202f39347793482216d1afe231b8 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE html>
<html>
<head>
  <script src='/resources/testharness.js'></script>
  <script src='/resources/testharnessreport.js'></script>
  <script src='/common/get-host-info.sub.js'></script>
  <script id='workerCode' type='javascript/worker'>
    self.onmessage = (e) => {
      postMessage(e.data);
    };
  </script>
  <script id='sharedWorkerCode' type='javascript/worker'>
    self.onconnect = function (event) {
      const port = event.ports[0];
      port.onmessage = function (e) {
        port.postMessage(e.data);
      };
    };
  </script>
</head>
<body>
<script>
const HELPER = '/webcodecs/videoFrame-serialization.crossAgentCluster.helper.html';
const SAMEORIGIN_BASE = get_host_info().HTTPS_ORIGIN;
const CROSSORIGIN_BASE = get_host_info().HTTPS_NOTSAMESITE_ORIGIN;
const SAMEORIGIN_HELPER = SAMEORIGIN_BASE + HELPER;
const CROSSORIGIN_HELPER = CROSSORIGIN_BASE + HELPER;

promise_test(async () => {
  const target = (await appendIframe(SAMEORIGIN_HELPER)).contentWindow;
  let frame = createVideoFrame(10);
  assert_true(await canSendVideoFrame(target, frame));
}, 'Verify frames can be passed within the same agent clusters');

promise_test(async () => {
  const target = (await appendIframe(CROSSORIGIN_HELPER)).contentWindow;
  let frame = createVideoFrame(20);
  assert_false(await canSendVideoFrame(target, frame));
}, 'Verify frames cannot be passed accross the different agent clusters');

promise_test(async () => {
  const blob = new Blob([document.querySelector('#workerCode').textContent], {
    type: 'text/javascript',
  });
  const worker = new Worker(window.URL.createObjectURL(blob));
  let frame = createVideoFrame(30);
  worker.postMessage(frame);
  const received = await new Promise(resolve => worker.onmessage = e => {
    resolve(e.data);
  });
  assert_equals(received.toString(), '[object VideoFrame]');
  assert_equals(received.timestamp, 30);
}, 'Verify frames can be passed back and forth between main and worker');

promise_test(async () => {
  const blob = new Blob([document.querySelector('#sharedWorkerCode').textContent], {
    type: 'text/javascript',
  });
  const worker = new SharedWorker(window.URL.createObjectURL(blob));
  let frame = createVideoFrame(40);
  worker.port.postMessage(frame);
  const received = await new Promise(resolve => worker.port.onmessage = e => {
    resolve(e.data);
  });
  assert_equals(received.toString(), '[object VideoFrame]');
  assert_equals(received.timestamp, 40);
}, 'Verify frames can be passed back and forth between main and sharedworker');

promise_test(async () => {
  navigator.serviceWorker.register('videoFrame-serialization.crossAgentCluster.serviceworker.js');
  navigator.serviceWorker.ready.then((registration) => {
    let frame = createVideoFrame(50);
    registration.active.postMessage(frame);
    registration.active.postMessage({'id': 50});
  });
  const received = await new Promise(resolve => navigator.serviceWorker.onmessage = (e) => {
    resolve(e.data);
  });
  assert_equals(received, 'NOT_RECEIVED');
}, 'Verify frames cannot be passed to serviceworker');

function appendIframe(src) {
  const frame = document.createElement('iframe');
  document.body.appendChild(frame);
  frame.src = src;
  return new Promise(resolve => frame.onload = () => resolve(frame));
};

function createVideoFrame(ts) {
  let data = new Uint8Array([
    1, 2, 3, 4, 5, 6, 7, 8,
    9, 10, 11, 12, 13, 14, 15, 16,
  ]);
  return new VideoFrame(data, {
    timestamp: ts,
    codedWidth: 2,
    codedHeight: 2,
    format: 'RGBA',
  });
}

function canSendVideoFrame(target, vf) {
  target.postMessage(vf, '*');
  target.postMessage({'id': vf.timestamp}, '*');
  return new Promise(resolve => window.onmessage = e => {
    resolve(e.data == 'RECEIVED');
  });
};
</script>
</body>
</html>