File: RTCEncodedFrame-copy-construction.https.html

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (106 lines) | stat: -rw-r--r-- 4,259 bytes parent folder | download | duplicates (2)
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name='timeout' content='long'>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src="../webrtc/RTCPeerConnection-helper.js"></script>
<script src="helper.js"></script>
</head>
<body>
<script>

const assert_json_equals = (a, b) => assert_equals(JSON.stringify(a), JSON.stringify(b));
const assert_json_not_equals = (a, b) => assert_not_equals(JSON.stringify(a), JSON.stringify(b));

const reuse = {}; // speed up tests

promise_test(async t => {
   const frame = await createRTCEncodedFrameFromScratch("video");
   reuse["video"] = frame;
   assert_true(frame instanceof RTCEncodedVideoFrame);
   assert_equals(frame.type, "key"); // first frame is key

   const clone = new RTCEncodedVideoFrame(frame);
   assert_true(clone instanceof RTCEncodedVideoFrame);
   assert_equals(clone.type, frame.type);
   assert_true(areArrayBuffersEqual(clone.data, frame.data));
   assert_json_equals(clone.getMetadata(), frame.getMetadata());
}, "RTCEncodedVideoFrame copy construction on main thread.");

promise_test(async t => {
   const frame = await createRTCEncodedFrameFromScratch("audio");
   reuse["audio"] = frame;
   assert_true(frame instanceof RTCEncodedAudioFrame);

   const clone = new RTCEncodedAudioFrame(frame);
   assert_true(clone instanceof RTCEncodedAudioFrame);
   assert_equals(clone.type, frame.type);
   assert_true(areArrayBuffersEqual(clone.data, frame.data));
   assert_json_equals(clone.getMetadata(), frame.getMetadata());
}, "RTCEncodedAudioFrame copy construction on main thread.");

function different(value) {
   switch (typeof value) {
     case "number": return value + 1;
     case "string": return value + "2";
     case "object": return Array.isArray(value) ? value.concat([2]) : {};
     default: assert_unreached(`unexpected type ${typeof value}`);
   }
}

["RTCEncodedVideoFrame", "RTCEncodedAudioFrame"].forEach(constr => {
  const kind = constr.includes("Video")? "video" : "audio";

  promise_test(async t => {
    const frame = reuse[kind];
    const oldData = frame.getMetadata();
    // test single key replacement
    for (const key of Object.keys(oldData)) {
      const metadata = {[key]: different(oldData[key])};
      // Spec says "The new frame’s [[metadata]] is a deep copy
      // of originalFrame.[[metadata]], with fields replaced with
      // deep copies of the fields present in options.[metadata]."
      // This compares well to how Object.assign() works
      const expected = Object.assign(structuredClone(oldData), metadata);
      const clone = new window[constr](frame, {metadata});
      assert_json_equals(clone.getMetadata(), expected);
      assert_json_not_equals(clone.getMetadata(), oldData);
    }
    // test cumulative key replacement
    let metadata = {};
    for (const key of Object.keys(oldData)) {
      Object.assign(metadata, {[key]: different(oldData[key])});
      const expected = Object.assign(structuredClone(oldData), metadata);
      const clone = new window[constr](frame, {metadata});
      assert_json_equals(clone.getMetadata(), expected);
      assert_json_not_equals(clone.getMetadata(), oldData);
    }
  }, `${constr} copy construction metadata override on main thread.`);

  promise_test(async t => {
    const frame = reuse[kind];
    assert_greater_than(frame.data.byteLength, 0);
    const length = frame.data.byteLength;
    const clone = structuredClone(frame);
    assert_equals(frame.data.byteLength, length, "not transferred");
    assert_true(areArrayBuffersEqual(clone.data, frame.data));
    assert_json_equals(clone.getMetadata(), frame.getMetadata());
  }, `${constr} structuredClone on main thread.`);

  promise_test(async t => {
    const frame = reuse[kind];
    assert_greater_than(frame.data.byteLength, 0);
    const length = frame.data.byteLength;
    const clone = structuredClone(frame, {transfer: [frame.data]});
    assert_equals(frame.data.byteLength, 0, "was transferred");
    assert_equals(clone.data.byteLength, length);
  }, `${constr} structuredClone transfer on main thread.`);

});
</script>
</body>
</html>