Skip to content

Commit 07bf77b

Browse files
authored
Merge pull request #834 from w3c/aboba-patch-6
Encode/decode in worker sample: Update for low latency, encoder state
2 parents 6fd01eb + 6e0a12e commit 07bf77b

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

samples/encode-decode-worker/js/stream_worker.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ let decqueue_aggregate = {
4242

4343
function enc_update(data) {
4444
enc_aggregate.all.push(data);
45+
//self.postMessage({text: 'enc_update called: ' + JSON.stringify(data)});
4546
}
4647

4748
function encqueue_update(duration) {
@@ -214,7 +215,7 @@ class pipeline {
214215
let config = JSON.parse(chunk.config);
215216
try {
216217
const decoderSupport = await VideoDecoder.isConfigSupported(config);
217-
if (decoderSupport.supported) {
218+
if (decoderSupport.supported) {
218219
this.decoder.configure(decoderSupport.config);
219220
self.postMessage({text: 'Decoder successfully configured:\n' + JSON.stringify(decoderSupport.config)});
220221
} else {
@@ -252,8 +253,7 @@ class pipeline {
252253
output: (chunk, cfg) => {
253254
if (cfg.decoderConfig) {
254255
cfg.decoderConfig.hardwareAcceleration = config.decHwAcceleration;
255-
cfg.decoderConfig.optimizeForLatency = true;
256-
if (config.latencyPref == 'quality') cfg.decoderConfig.optimizeForLatency = false;
256+
cfg.decoderConfig.optimizeForLatency = (config.latencyMode == "realtime");
257257
const decoderConfig = JSON.stringify(cfg.decoderConfig);
258258
self.postMessage({text: 'Configuration: ' + decoderConfig});
259259
const configChunk =
@@ -264,14 +264,14 @@ class pipeline {
264264
deltaframeIndex: this.deltaframeIndex,
265265
timestamp: 0,
266266
pt: 0,
267-
config: decoderConfig
267+
config: decoderConfig
268268
};
269-
controller.enqueue(configChunk);
269+
controller.enqueue(configChunk);
270270
}
271271
if (chunk.type != 'config'){
272272
const after = performance.now();
273273
enc_update({output: 1, timestamp: chunk.timestamp, time: after});
274-
}
274+
}
275275
chunk.temporalLayerId = 0;
276276
if (cfg.svc) {
277277
chunk.temporalLayerId = cfg.svc.temporalLayerId;
@@ -282,7 +282,7 @@ class pipeline {
282282
this.deltaframeIndex = 0;
283283
} else {
284284
this.deltaframeIndex++;
285-
}
285+
}
286286
this.pending_outputs--;
287287
chunk.seqNo = this.seqNo;
288288
chunk.keyframeIndex = this.keyframeIndex;
@@ -296,11 +296,11 @@ class pipeline {
296296
try {
297297
const encoderSupport = await VideoEncoder.isConfigSupported(config);
298298
if (encoderSupport.supported) {
299-
this.encoder.configure(encoderSupport.config);
300-
self.postMessage({text: 'Encoder successfully configured:\n' + JSON.stringify(encoderSupport.config)});
301-
} else {
302-
self.postMessage({severity: 'fatal', text: 'Config not supported:\n' + JSON.stringify(encoderSupport.config)});
303-
}
299+
this.encoder.configure(encoderSupport.config);
300+
self.postMessage({text: 'Encoder successfully configured:\n' + JSON.stringify(encoderSupport.config)});
301+
} else {
302+
self.postMessage({severity: 'fatal', text: 'Config not supported:\n' + JSON.stringify(encoderSupport.config)});
303+
}
304304
} catch (e) {
305305
self.postMessage({severity: 'fatal', text: `Configuration error: ${e.message}`});
306306
}
@@ -311,13 +311,13 @@ class pipeline {
311311
const insert_keyframe = (this.frameCounter % config.keyInterval) == 0;
312312
this.frameCounter++;
313313
try {
314-
if (this.encoder.state != "closed") {
314+
if (this.encoder.state == "configured") {
315315
const queue = this.encoder.encodeQueueSize;
316316
encqueue_update(queue);
317317
const before = performance.now();
318318
enc_update({output: 0, timestamp: frame.timestamp, time: before});
319319
this.encoder.encode(frame, { keyFrame: insert_keyframe });
320-
}
320+
}
321321
} catch(e) {
322322
self.postMessage({severity: 'fatal', text: 'Encoder Error: ' + e.message});
323323
}
@@ -331,7 +331,7 @@ class pipeline {
331331
if (encoder.state != "closed") encoder.close();
332332
if (decoder.state != "closed") decoder.close();
333333
stopped = true;
334-
this.stopped = true;
334+
this.stopped = true;
335335
const len = encqueue_aggregate.all.length;
336336
if (len > 1) {
337337
const enc_stats = enc_report();
@@ -355,7 +355,7 @@ class pipeline {
355355
started = true;
356356
let duplexStream, readStream, writeStream;
357357
self.postMessage({text: 'Start method called.'});
358-
try {
358+
try {
359359
await this.inputStream
360360
.pipeThrough(this.EncodeVideoStream(self,this.config))
361361
.pipeThrough(this.DecodeVideoStream(self))

0 commit comments

Comments
 (0)