Skip to content

Commit 9f26adc

Browse files
abobadalecurtis
authored andcommitted
Restore queue size metrics, change default bitrate and GoP size
1 parent 4ccbf3b commit 9f26adc

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

samples/encode-decode-worker/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ <h2>WebCodecs in Worker</h2>
6666
<div id="rateInput">
6767
<label for="rate">bitrate: </label>
6868
<input type="text" name="rate" id="rate"
69-
value=1000000>
69+
value=2000000>
7070
</div>
7171

7272
<div id="keyInput">
7373
<label for="keygap">keyframe interval: </label>
7474
<input type="text" name="keygap" id="keygap"
75-
value=300>
75+
value=3000>
7676
</div>
7777

7878
<div id="codecButtons">

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,80 @@
22

33
let encoder, decoder, pl, started = false, stopped = false;
44

5+
let encqueue_aggregate = {
6+
all: [],
7+
min: Number.MAX_VALUE,
8+
max: 0,
9+
avg: 0,
10+
sum: 0,
11+
};
12+
13+
let decqueue_aggregate = {
14+
all: [],
15+
min: Number.MAX_VALUE,
16+
max: 0,
17+
avg: 0,
18+
sum: 0,
19+
};
20+
21+
function encqueue_update(duration) {
22+
encqueue_aggregate.all.push(duration);
23+
encqueue_aggregate.min = Math.min(encqueue_aggregate.min, duration);
24+
encqueue_aggregate.max = Math.max(encqueue_aggregate.max, duration);
25+
encqueue_aggregate.sum += duration;
26+
}
27+
28+
function encqueue_report() {
29+
encqueue_aggregate.all.sort();
30+
const len = encqueue_aggregate.all.length;
31+
const half = len >> 1;
32+
const f = (len + 1) >> 2;
33+
const t = (3 * (len + 1)) >> 2;
34+
const alpha1 = (len + 1)/4 - Math.trunc((len + 1)/4);
35+
const alpha3 = (3 * (len + 1)/4) - Math.trunc(3 * (len + 1)/4);
36+
const fquart = encqueue_aggregate.all[f] + alpha1 * (encqueue_aggregate.all[f + 1] - encqueue_aggregate.all[f]);
37+
const tquart = encqueue_aggregate.all[t] + alpha3 * (encqueue_aggregate.all[t + 1] - encqueue_aggregate.all[t]);
38+
const median = len % 2 === 1 ? encqueue_aggregate.all[len >> 1] : (encqueue_aggregate.all[half - 1] + encqueue_aggregate.all[half]) / 2;
39+
return {
40+
count: len,
41+
min: encqueue_aggregate.min,
42+
fquart: fquart,
43+
avg: encqueue_aggregate.sum / len,
44+
median: median,
45+
tquart: tquart,
46+
max: encqueue_aggregate.max,
47+
};
48+
}
49+
50+
function decqueue_update(duration) {
51+
decqueue_aggregate.all.push(duration);
52+
decqueue_aggregate.min = Math.min(decqueue_aggregate.min, duration);
53+
decqueue_aggregate.max = Math.max(decqueue_aggregate.max, duration);
54+
decqueue_aggregate.sum += duration;
55+
}
56+
57+
function decqueue_report() {
58+
decqueue_aggregate.all.sort();
59+
const len = decqueue_aggregate.all.length;
60+
const half = len >> 1;
61+
const f = (len + 1) >> 2;
62+
const t = (3 * (len + 1)) >> 2;
63+
const alpha1 = (len + 1)/4 - Math.trunc((len + 1)/4);
64+
const alpha3 = (3 * (len + 1)/4) - Math.trunc(3 * (len + 1)/4);
65+
const fquart = decqueue_aggregate.all[f] + alpha1 * (decqueue_aggregate.all[f + 1] - decqueue_aggregate.all[f]);
66+
const tquart = decqueue_aggregate.all[t] + alpha3 * (decqueue_aggregate.all[t + 1] - decqueue_aggregate.all[t]);
67+
const median = len % 2 === 1 ? decqueue_aggregate.all[len >> 1] : (decqueue_aggregate.all[half - 1] + decqueue_aggregate.all[half]) / 2;
68+
return {
69+
count: len,
70+
min: decqueue_aggregate.min,
71+
fquart: fquart,
72+
avg: decqueue_aggregate.sum / len,
73+
median: median,
74+
tquart: tquart,
75+
max: decqueue_aggregate.max,
76+
};
77+
}
78+
579
self.addEventListener('message', async function(e) {
680
if (stopped) return;
781
// In this demo, we expect at most two messages, one of each type.
@@ -63,6 +137,8 @@ class pipeline {
63137
})
64138
} else {
65139
try {
140+
const queue = this.decoder.decodeQueueSize;
141+
decqueue_update(queue);
66142
this.decoder.decode(chunk);
67143
} catch (e) {
68144
self.postMessage({severity: 'fatal', text: 'Derror size: ' + chunk.byteLength + ' seq: ' + chunk.seqNo + ' kf: ' + chunk.keyframeIndex + ' delta: ' + chunk.deltaframeIndex + ' dur: ' + chunk.duration + ' ts: ' + chunk.timestamp + ' ssrc: ' + chunk.ssrc + ' pt: ' + chunk.pt + ' tid: ' + chunk.temporalLayerId + ' type: ' + chunk.type});
@@ -139,6 +215,8 @@ class pipeline {
139215
this.frameCounter++;
140216
try {
141217
if (this.encoder.state != "closed") {
218+
const queue = this.encoder.encodeQueueSize;
219+
encqueue_update(queue);
142220
this.encoder.encode(frame, { keyFrame: insert_keyframe });
143221
}
144222
} catch(e) {
@@ -151,6 +229,10 @@ class pipeline {
151229
}
152230

153231
stop() {
232+
const encqueue_stats = encqueue_report();
233+
const decqueue_stats = decqueue_report();
234+
self.postMessage({text: 'Encoder Queue report: ' + JSON.stringify(encqueue_stats)});
235+
self.postMessage({text: 'Decoder Queue report: ' + JSON.stringify(decqueue_stats)});
154236
if (stopped) return;
155237
stopped = true;
156238
this.stopped = true;

0 commit comments

Comments
 (0)