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
content / test / data / gpu / webcodecs / encode-decode.html [blame]
<!DOCTYPE html>
<!--
The most basic GPU smoke test for WebCodecs: Accelerated VideoEncoder encodes
several frames and accelerated VideoDecoder needs to successfully decode them.
-->
<html>
<head>
<title>Encode, decode, render test</title>
<script src="webcodecs_common.js"></script>
<script type="text/javascript">
'use strict';
function showFrameForDebug(frame) {
const cnv = document.getElementById('debug_cnv');
var ctx = cnv.getContext('2d');
ctx.drawImage(frame, 0, 0);
}
async function main(arg) {
const width = 640;
const height = 480;
const frames_to_encode = 32;
let frames_encoded = 0;
let frames_decoded = 0;
let errors = 0;
const encoder_config = {
codec: arg.codec,
hardwareAcceleration: arg.acceleration,
width: width,
height: height,
bitrate: 5000000,
framerate: 24
};
if (arg.codec.startsWith('avc1')) {
encoder_config.avc = { format: 'annexb' };
} else if (arg.codec.startsWith('hvc1')) {
encoder_config.hevc = { format: 'annexb' };
}
TEST.log('Starting test with arguments: ' + JSON.stringify(arg));
let supported = false;
try {
supported = (await VideoEncoder.isConfigSupported(encoder_config)).supported;
} catch (e) {}
if (!supported) {
TEST.skip('Unsupported codec: ' + arg.codec);
return;
}
let decoder = new VideoDecoder({
output(frame) {
let dots = frames_decoded;
// Check that we have intended number of dots and no more.
// Completely black frame shouldn't pass the test.
// Only count dots for sources that can actually pain them.
if (['offscreen', 'arraybuffer'].includes(arg.source_type)) {
if(!validateBlackDots(frame, dots) ||
validateBlackDots(frame, dots + 1)) {
showFrameForDebug(frame);
TEST.reportFailure(
`Unexpected dot count ts:${frame.timestamp} dots:${dots}`);
}
}
frames_decoded++;
frame.close();
},
error(e) {
errors++;
TEST.log(e);
}
});
let timestamps = [];
const encoder_init = {
output(chunk, metadata) {
let config = metadata.decoderConfig;
if (config) {
decoder.configure(config);
}
decoder.decode(chunk);
frames_encoded++;
let expected_timestamp = timestamps.shift();
TEST.assert(
chunk.timestamp == expected_timestamp,
`EncodedVideoChunk timestamp mismatch. Expected: ${
expected_timestamp} got ${chunk.timestamp}`);
},
error(e) {
errors++;
TEST.log(e);
}
};
let encoder = new VideoEncoder(encoder_init);
encoder.configure(encoder_config);
let source = await createFrameSource(arg.source_type, width, height);
if (!source) {
TEST.skip('Unsupported source: ' + arg.source_type);
return;
}
for (let i = 0; i < frames_to_encode; i++) {
let frame = await source.getNextFrame();
let keyframe = (i % 10 == 0);
timestamps.push(frame.timestamp);
encoder.encode(frame, { keyFrame: keyframe });
frame.close();
await waitForNextFrame();
}
await encoder.flush();
await decoder.flush();
encoder.close();
decoder.close();
source.close();
TEST.assert(
frames_encoded == frames_to_encode,
'frames_encoded mismatch: ' + frames_encoded);
TEST.assert(
frames_decoded == frames_to_encode,
'frames_decoded mismatch: ' + frames_decoded);
TEST.assert(
errors == 0, 'Decoding or encoding errors occurred during the test');
TEST.log('Test completed');
}
addManualTestButton([{
'source_type': 'offscreen',
'codec': 'avc1.42001E',
'acceleration':'prefer-software'
},
{
'source_type': 'camera',
'codec': 'avc1.42001E',
'acceleration':'prefer-software'
},
{
'source_type': 'offscreen',
'codec': 'avc1.42001E',
'acceleration':'prefer-hardware'
},
{
'source_type': 'offscreen',
'codec': 'hvc1.1.6.L123.00',
'acceleration':'prefer-software'
},
{
'source_type': 'offscreen',
'codec': 'hvc1.1.6.L123.00',
'acceleration':'prefer-hardware'
}]);
</script>
</head>
<body>
<canvas id="debug_cnv" width="640" height="480"></canvas>
</body>
</html>