2
2
3
3
let encoder , decoder , pl , started = false , stopped = false ;
4
4
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
+
5
79
self . addEventListener ( 'message' , async function ( e ) {
6
80
if ( stopped ) return ;
7
81
// In this demo, we expect at most two messages, one of each type.
@@ -63,6 +137,8 @@ class pipeline {
63
137
} )
64
138
} else {
65
139
try {
140
+ const queue = this . decoder . decodeQueueSize ;
141
+ decqueue_update ( queue ) ;
66
142
this . decoder . decode ( chunk ) ;
67
143
} catch ( e ) {
68
144
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 {
139
215
this . frameCounter ++ ;
140
216
try {
141
217
if ( this . encoder . state != "closed" ) {
218
+ const queue = this . encoder . encodeQueueSize ;
219
+ encqueue_update ( queue ) ;
142
220
this . encoder . encode ( frame , { keyFrame : insert_keyframe } ) ;
143
221
}
144
222
} catch ( e ) {
@@ -151,6 +229,10 @@ class pipeline {
151
229
}
152
230
153
231
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 ) } ) ;
154
236
if ( stopped ) return ;
155
237
stopped = true ;
156
238
this . stopped = true ;
0 commit comments