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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
// META: global=window,dedicatedworker
// META: script=/common/media.js
// META: script=/webcodecs/utils.js
// META: script=/webcodecs/video-encoder-utils.js
const defaultConfig = {
codec: 'vp8',
width: 640,
height: 480
};
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(async t => {
let output_chunks = [];
let codecInit = getDefaultCodecInit(t);
let decoderConfig = null;
let encoderConfig = {
codec: 'vp8',
width: 640,
height: 480,
displayWidth: 800,
displayHeight: 600,
};
codecInit.output = (chunk, metadata) => {
assert_not_equals(metadata, null);
if (metadata.decoderConfig)
decoderConfig = metadata.decoderConfig;
output_chunks.push(chunk);
}
let encoder = new VideoEncoder(codecInit);
encoder.configure(encoderConfig);
let frame1 = createFrame(640, 480, 0);
let frame2 = createFrame(640, 480, 33333);
t.add_cleanup(() => {
frame1.close();
frame2.close();
});
encoder.encode(frame1);
encoder.encode(frame2);
await encoder.flush();
// Decoder config should be given with the first chunk
assert_not_equals(decoderConfig, null);
assert_equals(decoderConfig.codec, encoderConfig.codec);
assert_greater_than_equal(decoderConfig.codedHeight, encoderConfig.height);
assert_greater_than_equal(decoderConfig.codedWidth, encoderConfig.width);
assert_equals(decoderConfig.displayAspectHeight, encoderConfig.displayHeight);
assert_equals(decoderConfig.displayAspectWidth, encoderConfig.displayWidth);
assert_not_equals(decoderConfig.colorSpace.primaries, null);
assert_not_equals(decoderConfig.colorSpace.transfer, null);
assert_not_equals(decoderConfig.colorSpace.matrix, null);
assert_not_equals(decoderConfig.colorSpace.fullRange, null);
assert_equals(output_chunks.length, 2);
assert_equals(output_chunks[0].timestamp, frame1.timestamp);
assert_equals(output_chunks[0].duration, frame1.duration);
assert_equals(output_chunks[1].timestamp, frame2.timestamp);
assert_equals(output_chunks[1].duration, frame2.duration);
}, 'Test successful configure(), encode(), and flush()');
promise_test(async t => {
let codecInit = getDefaultCodecInit(t);
let encoderConfig = {
codec: 'vp8',
width: 320,
height: 200
};
codecInit.output = (chunk, metadata) => {}
let encoder = new VideoEncoder(codecInit);
// No encodes yet.
assert_equals(encoder.encodeQueueSize, 0);
encoder.configure(encoderConfig);
// Still no encodes.
assert_equals(encoder.encodeQueueSize, 0);
const frames_count = 100;
let frames = [];
for (let i = 0; i < frames_count; i++) {
let frame = createFrame(320, 200, i * 16000);
frames.push(frame);
}
let lastDequeueSize = Infinity;
encoder.ondequeue = () => {
assert_greater_than(lastDequeueSize, 0, "Dequeue event after queue empty");
assert_greater_than(lastDequeueSize, encoder.encodeQueueSize,
"Dequeue event without decreased queue size");
lastDequeueSize = encoder.encodeQueueSize;
};
for (let frame of frames)
encoder.encode(frame);
assert_greater_than_equal(encoder.encodeQueueSize, 0);
assert_less_than_equal(encoder.encodeQueueSize, frames_count);
await encoder.flush();
// We can guarantee that all encodes are processed after a flush.
assert_equals(encoder.encodeQueueSize, 0);
// Last dequeue event should fire when the queue is empty.
assert_equals(lastDequeueSize, 0);
// Reset this to Infinity to track the decline of queue size for this next
// batch of encodes.
lastDequeueSize = Infinity;
for (let frame of frames) {
encoder.encode(frame);
frame.close();
}
assert_greater_than_equal(encoder.encodeQueueSize, 0);
encoder.reset();
assert_equals(encoder.encodeQueueSize, 0);
}, 'encodeQueueSize test');
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 original = createFrame(320, 200, 0);
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(original, { timestamp: timestamp });
timestamp += timestamp_step;
encoder.encode(frame);
frame.close();
}
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 = createFrame(800, 600, timestamp + 1);
timestamp += timestamp_step;
encoder.encode(frame);
frame.close();
}
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 = [];
const codecInit = {
output: chunk => output_chunks.push(chunk),
};
const error = new Promise(resolve => codecInit.error = e => {
resolve(e);
});
let encoder = new VideoEncoder(codecInit);
// No encodes yet.
assert_equals(encoder.encodeQueueSize, 0);
let config = defaultConfig;
encoder.configure(config);
let frame1 = createFrame(640, 480, 0);
let frame2 = createFrame(640, 480, 33333);
encoder.encode(frame1);
encoder.configure(config);
encoder.encode(frame2);
await encoder.flush();
// We can guarantee that all encodes are processed after a flush.
assert_equals(encoder.encodeQueueSize, 0, "queue size after encode");
assert_equals(output_chunks.length, 2, "number of chunks");
assert_equals(output_chunks[0].timestamp, frame1.timestamp);
assert_equals(output_chunks[1].timestamp, frame2.timestamp);
output_chunks = [];
let frame3 = createFrame(640, 480, 66666);
encoder.encode(frame3);
let badConfig = { ...defaultConfig };
badConfig.codec = '';
assert_throws_js(TypeError, () => encoder.configure(badConfig));
badConfig.codec = 'bogus';
encoder.configure(badConfig);
let e = await error;
assert_true(e instanceof DOMException);
assert_equals(e.name, 'NotSupportedError');
assert_equals(encoder.state, 'closed', 'state');
// We may or may not have received frame3 before closing.
}, 'Test successful encode() after re-configure().');
promise_test(async t => {
let encoder = new VideoEncoder(getDefaultCodecInit(t));
let frame = createFrame(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 = createFrame(640, 480, 0);
return testUnconfiguredCodec(t, encoder, frame);
}, 'Verify unconfigured VideoEncoder operations');
promise_test(async t => {
let encoder = new VideoEncoder(getDefaultCodecInit(t));
let frame = createFrame(640, 480, 0);
frame.close();
encoder.configure(defaultConfig);
assert_throws_js(TypeError, () => {
encoder.encode(frame);
});
}, 'Verify encoding closed frames throws.');
promise_test(async t => {
let output_chunks = [];
let codecInit = getDefaultCodecInit(t);
codecInit.output = chunk => output_chunks.push(chunk);
let encoder = new VideoEncoder(codecInit);
let config = defaultConfig;
encoder.configure(config);
let frame = createFrame(640, 480, -10000);
encoder.encode(frame);
frame.close();
await encoder.flush();
encoder.close();
assert_equals(output_chunks.length, 1);
assert_equals(output_chunks[0].timestamp, -10000, "first chunk timestamp");
assert_greater_than(output_chunks[0].byteLength, 0);
}, 'Encode video with negative timestamp');
|