summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webcodecs/videoFrame-serialization.crossAgentCluster.https.html
blob: 8fe7cf44cc686e041fc1fdc677c060905e7d76d0 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!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 src='/webcodecs/utils.js'></script>
  <script id='workerCode' type='javascript/worker'>
    self.onmessage = (e) => {
      let frame = e.data.frame;
      if (e.data.transfer) {
        postMessage(frame, [frame]);
      } else {
        postMessage(frame);
      }
    };
  </script>
  <script id='sharedWorkerCode' type='javascript/worker'>
    const data = new Uint8Array([
      1, 2, 3, 4, 5, 6, 7, 8,
      9, 10, 11, 12, 13, 14, 15, 16,
    ]);
    let received = new Map();
    self.onconnect = function (event) {
      const port = event.ports[0];
      port.onmessage = function (e) {
        if (e.data == 'create-frame') {
          let frameOrError = null;
          try {
            frameOrError = new VideoFrame(data, {
              timestamp: 0,
              codedWidth: 2,
              codedHeight: 2,
              format: 'RGBA',
            });
          } catch (error) {
            frameOrError = error
          }
          port.postMessage(frameOrError);
          return;
        }
        if (e.data.hasOwnProperty('id')) {
          port.postMessage(
            received.get(e.data.id) ? 'RECEIVED' : 'NOT_RECEIVED');
          return;
        }
        if (e.data.toString() == '[object VideoFrame]') {
          received.set(e.data.timestamp, 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 canSerializeVideoFrame(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 canSerializeVideoFrame(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: frame, transfer: false});
  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);
  worker.port.postMessage({'id': 40});
  const received = await new Promise(resolve => worker.port.onmessage = e => {
    resolve(e.data);
  });
  assert_equals(received, 'NOT_RECEIVED');
}, 'Verify frames cannot be passed to 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');

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

promise_test(async () => {
  const target = (await appendIframe(CROSSORIGIN_HELPER)).contentWindow;
  let frame = createVideoFrame(70);
  assert_false(await canTransferVideoFrame(target, frame));
}, 'Verify frames cannot be transferred 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(80);
  worker.postMessage({frame: frame, transfer: true}, [frame]);
  const received = await new Promise(resolve => worker.onmessage = e => {
    resolve(e.data);
  });
  assert_equals(received.toString(), '[object VideoFrame]');
  assert_equals(received.timestamp, 80);
}, 'Verify frames can be transferred 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(90);
  worker.port.postMessage(frame, [frame]);
  worker.port.postMessage({'id': 90});
  const received = await new Promise(resolve => worker.port.onmessage = e => {
    resolve(e.data);
  });
  assert_equals(received, 'NOT_RECEIVED');
}, 'Verify frames cannot be transferred to a sharedworker');

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

promise_test(async () => {
  const blob = new Blob([document.querySelector('#sharedWorkerCode').textContent], {
    type: 'text/javascript',
  });
  const worker = new SharedWorker(window.URL.createObjectURL(blob));
  worker.port.postMessage('create-frame');
  const received = await new Promise(resolve => worker.port.onmessage = e => {
    resolve(e.data);
  });
  assert_true(received instanceof ReferenceError);
}, 'Verify frames is unavailable in sharedworker');

promise_test(async () => {
  navigator.serviceWorker.register('videoFrame-serialization.crossAgentCluster.serviceworker.js');
  let registration = await navigator.serviceWorker.ready;
  registration.active.postMessage('create-frame');
  const received = await new Promise(resolve => navigator.serviceWorker.onmessage = (e) => {
    resolve(e.data);
  });
  assert_true(received instanceof ReferenceError);
}, 'Verify frames is unavailable in 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 canSerializeVideoFrame(target, vf) {
  return canPostVideoFrame(target, vf, false);
};

function canTransferVideoFrame(target, vf) {
  return canPostVideoFrame(target, vf, true);
};

function canPostVideoFrame(target, vf, transfer) {
  if (transfer) {
    target.postMessage(vf, '*', [vf]);
    assert_true(isFrameClosed(vf));
  } else {
    target.postMessage(vf, '*');
  }
  // vf.timestamp doesn't change after vf is closed, so it's fine to use it.
  target.postMessage({'id': vf.timestamp}, '*');
  return new Promise(resolve => window.onmessage = e => {
    resolve(e.data == 'RECEIVED');
  });
};
</script>
</body>
</html>