-
Notifications
You must be signed in to change notification settings - Fork 3k
/
video-encoder.any.js
275 lines (205 loc) · 8.12 KB
/
video-encoder.any.js
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// META: global=window,dedicatedworker
// META: script=/common/media.js
// META: script=/webcodecs/utils.js
const defaultConfig = {
codec: 'vp8',
framerate: 25,
width: 640,
height: 480
};
async function generateBitmap(width, height) {
const src = "pattern.png";
var size = {
resizeWidth: width,
resizeHeight: height
};
return fetch(src)
.then(response => response.blob())
.then(blob => createImageBitmap(blob, size));
}
async function createVideoFrame(width, height, timestamp) {
let bitmap = await generateBitmap(width, height);
return new VideoFrame(bitmap, { timestamp: timestamp });
}
promise_test(t => {
// VideoEncoderInit lacks required fields.
assert_throws_js(TypeError, () => { new VideoEncoder({}); });
// VideoEncoderInit has required fields.
let encoder = new VideoEncoder(getDefaultCodecInit(t));
assert_equals(encoder.state, "unconfigured");
encoder.close();
return endAfterEventLoopTurn();
}, 'Test VideoEncoder construction');
promise_test(t => {
let encoder = new VideoEncoder(getDefaultCodecInit(t));
let badCodecsList = [
'', // Empty codec
'bogus', // Non exsitent codec
'vorbis', // Audio codec
'vp9', // Ambiguous codec
'video/webm; codecs="vp9"' // Codec with mime type
]
testConfigurations(encoder, defaultConfig, badCodecsList);
return endAfterEventLoopTurn();
}, 'Test VideoEncoder.configure()');
promise_test(async t => {
let output_chunks = [];
let codecInit = getDefaultCodecInit(t);
codecInit.output = (chunk, metadata) => {
assert_not_equals(metadata, null);
output_chunks.push(chunk);
}
let encoder = new VideoEncoder(codecInit);
// No encodes yet.
assert_equals(encoder.encodeQueueSize, 0);
encoder.configure(defaultConfig);
// Still no encodes.
assert_equals(encoder.encodeQueueSize, 0);
let frame1 = await createVideoFrame(640, 480, 0);
let frame2 = await createVideoFrame(640, 480, 33333);
encoder.encode(frame1.clone());
encoder.encode(frame2.clone());
// Could be 0, 1, or 2. We can't guarantee this check runs before the UA has
// processed the encodes.
assert_true(encoder.encodeQueueSize >= 0 && encoder.encodeQueueSize <= 2)
await encoder.flush();
// We can guarantee that all encodes are processed after a flush.
assert_equals(encoder.encodeQueueSize, 0);
assert_equals(output_chunks.length, 2);
assert_equals(output_chunks[0].timestamp, frame1.timestamp);
assert_equals(output_chunks[1].timestamp, frame2.timestamp);
}, 'Test successful configure(), encode(), and flush()');
promise_test(async t => {
let timestamp = 0;
let callbacks_before_reset = 0;
let callbacks_after_reset = 0;
const timestamp_step = 40000;
const expected_callbacks_before_reset = 3;
let codecInit = getDefaultCodecInit(t);
let bitmap = await generateBitmap(320, 200);
let encoder = null;
let reset_completed = false;
codecInit.output = (chunk, metadata) => {
if (chunk.timestamp % 2 == 0) {
// pre-reset frames have even timestamp
callbacks_before_reset++;
if (callbacks_before_reset == expected_callbacks_before_reset) {
encoder.reset();
reset_completed = true;
}
} else {
// after-reset frames have odd timestamp
callbacks_after_reset++;
}
}
encoder = new VideoEncoder(codecInit);
encoder.configure(defaultConfig);
await encoder.flush();
// Send 10x frames to the encoder, call reset() on it after x outputs,
// and make sure no more chunks are emitted afterwards.
let encodes_before_reset = expected_callbacks_before_reset * 10;
for (let i = 0; i < encodes_before_reset; i++) {
let frame = new VideoFrame(bitmap, { timestamp: timestamp });
timestamp += timestamp_step;
encoder.encode(frame);
}
await t.step_wait(() => reset_completed,
"Reset() should be called by output callback", 10000, 1);
assert_equals(callbacks_before_reset, expected_callbacks_before_reset);
assert_true(reset_completed);
assert_equals(encoder.encodeQueueSize, 0);
let newConfig = { ...defaultConfig };
newConfig.width = 800;
newConfig.height = 600;
encoder.configure(newConfig);
const frames_after_reset = 5;
for (let i = 0; i < frames_after_reset; i++) {
let frame = await createVideoFrame(800, 600, timestamp + 1);
timestamp += timestamp_step;
encoder.encode(frame);
}
await encoder.flush();
assert_equals(callbacks_after_reset, frames_after_reset,
"not all after-reset() outputs have been emitted");
assert_equals(callbacks_before_reset, expected_callbacks_before_reset,
"pre-reset() outputs were emitter after reset() and flush()");
assert_equals(encoder.encodeQueueSize, 0);
}, 'Test successful reset() and re-confiugre()');
promise_test(async t => {
let output_chunks = [];
let codecInit = getDefaultCodecInit(t);
codecInit.output = chunk => output_chunks.push(chunk);
let encoder = new VideoEncoder(codecInit);
// No encodes yet.
assert_equals(encoder.encodeQueueSize, 0);
let config = defaultConfig;
encoder.configure(config);
let frame1 = await createVideoFrame(640, 480, 0);
let frame2 = await createVideoFrame(640, 480, 33333);
encoder.encode(frame1.clone());
encoder.configure(config);
encoder.encode(frame2.clone());
await encoder.flush();
// We can guarantee that all encodes are processed after a flush.
assert_equals(encoder.encodeQueueSize, 0);
// The first frame may have been dropped when reconfiguring.
// This shouldn't happen, and should be fixed/called out in the spec, but
// this is preptively added to prevent flakiness.
// TODO: Remove these checks when implementations handle this correctly.
assert_true(output_chunks.length == 1 || output_chunks.length == 2);
if (output_chunks.length == 1) {
// If we only have one chunk frame, make sure we droped the frame that was
// in flight when we reconfigured.
assert_equals(output_chunks[0].timestamp, frame2.timestamp);
} else {
assert_equals(output_chunks[0].timestamp, frame1.timestamp);
assert_equals(output_chunks[1].timestamp, frame2.timestamp);
}
output_chunks = [];
let frame3 = await createVideoFrame(640, 480, 66666);
let frame4 = await createVideoFrame(640, 480, 100000);
encoder.encode(frame3.clone());
// Verify that a failed call to configure does not change the encoder's state.
let badConfig = { ...defaultConfig };
badConfig.codec = 'bogus';
assert_throws_js(TypeError, () => encoder.configure(badConfig));
encoder.encode(frame4.clone());
await encoder.flush();
assert_equals(output_chunks[0].timestamp, frame3.timestamp);
assert_equals(output_chunks[1].timestamp, frame4.timestamp);
}, 'Test successful encode() after re-configure().');
promise_test(async t => {
let output_chunks = [];
let codecInit = getDefaultCodecInit(t);
codecInit.output = chunk => output_chunks.push(chunk);
let encoder = new VideoEncoder(codecInit);
let timestamp = 33333;
let frame = await createVideoFrame(640, 480, timestamp);
encoder.configure(defaultConfig);
assert_equals(encoder.state, "configured");
encoder.encode(frame);
// |frame| is not longer valid since it has been closed.
assert_not_equals(frame.timestamp, timestamp);
assert_throws_dom("InvalidStateError", () => frame.clone());
encoder.close();
return endAfterEventLoopTurn();
}, 'Test encoder consumes (closes) frames.');
promise_test(async t => {
let encoder = new VideoEncoder(getDefaultCodecInit(t));
let frame = await createVideoFrame(640, 480, 0);
return testClosedCodec(t, encoder, defaultConfig, frame);
}, 'Verify closed VideoEncoder operations');
promise_test(async t => {
let encoder = new VideoEncoder(getDefaultCodecInit(t));
let frame = await createVideoFrame(640, 480, 0);
return testUnconfiguredCodec(t, encoder, frame);
}, 'Verify unconfigured VideoEncoder operations');
promise_test(async t => {
let encoder = new VideoEncoder(getDefaultCodecInit(t));
let frame = await createVideoFrame(640, 480, 0);
frame.close();
encoder.configure(defaultConfig);
assert_throws_dom("OperationError", () => {
encoder.encode(frame)
});
}, 'Verify encoding closed frames throws.');