2
2
3
3
let encoder , decoder , pl , started = false , stopped = false ;
4
4
5
+ let enc_aggregate = {
6
+ all : [ ] ,
7
+ } ;
8
+
9
+ let enc_time = {
10
+ all : [ ] ,
11
+ min : Number . MAX_VALUE ,
12
+ max : 0 ,
13
+ sum : 0
14
+ } ;
15
+
16
+ let dec_aggregate = {
17
+ all : [ ] ,
18
+ } ;
19
+
20
+ let dec_time = {
21
+ all : [ ] ,
22
+ min : Number . MAX_VALUE ,
23
+ max : 0 ,
24
+ sum : 0
25
+ } ;
26
+
5
27
let encqueue_aggregate = {
6
28
all : [ ] ,
7
29
min : Number . MAX_VALUE ,
@@ -18,13 +40,44 @@ let decqueue_aggregate = {
18
40
sum : 0 ,
19
41
} ;
20
42
43
+ function enc_update ( data ) {
44
+ enc_aggregate . all . push ( data ) ;
45
+ }
46
+
21
47
function encqueue_update ( duration ) {
22
48
encqueue_aggregate . all . push ( duration ) ;
23
49
encqueue_aggregate . min = Math . min ( encqueue_aggregate . min , duration ) ;
24
50
encqueue_aggregate . max = Math . max ( encqueue_aggregate . max , duration ) ;
25
51
encqueue_aggregate . sum += duration ;
26
52
}
27
53
54
+ function enc_report ( ) {
55
+ enc_aggregate . all . sort ( ( a , b ) => {
56
+ return ( 100000 * ( a . timestamp - b . timestamp ) + a . output - b . output ) ;
57
+ } ) ;
58
+ const len = enc_aggregate . all . length ;
59
+ if ( len < 2 ) return ;
60
+ for ( let i = 1 ; i < len ; i ++ ) {
61
+ if ( ( enc_aggregate . all [ i ] . output == 1 ) && ( enc_aggregate . all [ i - 1 ] . output == 0 ) && ( enc_aggregate . all [ i ] . timestamp == enc_aggregate . all [ i - 1 ] . timestamp ) ) {
62
+ const timestamp = enc_aggregate . all [ i ] . timestamp ;
63
+ const enc_delay = enc_aggregate . all [ i ] . time - enc_aggregate . all [ i - 1 ] . time ;
64
+ const data = [ timestamp , enc_delay ] ;
65
+ enc_time . all . push ( data ) ;
66
+ enc_time . min = Math . min ( enc_time . min , enc_delay ) ;
67
+ enc_time . max = Math . max ( enc_time . max , enc_delay ) ;
68
+ enc_time . sum += enc_delay ;
69
+ }
70
+ }
71
+ const avg = enc_time . sum / enc_time . all . length ;
72
+ //self.postMessage({text: 'Encode Time Data dump: ' + JSON.stringify(enc_time.all)});
73
+ return {
74
+ count : enc_time . all . length ,
75
+ min : enc_time . min ,
76
+ avg : avg ,
77
+ max : enc_time . max
78
+ } ;
79
+ }
80
+
28
81
function encqueue_report ( ) {
29
82
encqueue_aggregate . all . sort ( ) ;
30
83
const len = encqueue_aggregate . all . length ;
@@ -47,6 +100,37 @@ function encqueue_report() {
47
100
} ;
48
101
}
49
102
103
+ function dec_update ( data ) {
104
+ dec_aggregate . all . push ( data ) ;
105
+ }
106
+
107
+ function dec_report ( ) {
108
+ dec_aggregate . all . sort ( ( a , b ) => {
109
+ return ( 100000 * ( a . timestamp - b . timestamp ) + a . output - b . output ) ;
110
+ } ) ;
111
+ const len = dec_aggregate . all . length ;
112
+ if ( len < 2 ) return ;
113
+ for ( let i = 1 ; i < len ; i ++ ) {
114
+ if ( ( dec_aggregate . all [ i ] . output == 1 ) && ( dec_aggregate . all [ i - 1 ] . output == 0 ) && ( dec_aggregate . all [ i ] . timestamp == dec_aggregate . all [ i - 1 ] . timestamp ) ) {
115
+ const timestamp = dec_aggregate . all [ i ] . timestamp ;
116
+ const dec_delay = dec_aggregate . all [ i ] . time - dec_aggregate . all [ i - 1 ] . time ;
117
+ const data = [ timestamp , dec_delay ] ;
118
+ dec_time . all . push ( data ) ;
119
+ dec_time . min = Math . min ( dec_time . min , dec_delay ) ;
120
+ dec_time . max = Math . max ( dec_time . max , dec_delay ) ;
121
+ dec_time . sum += dec_delay ;
122
+ }
123
+ }
124
+ const avg = dec_time . sum / dec_time . all . length ;
125
+ //self.postMessage({text: 'Decode Time Data dump: ' + JSON.stringify(dec_time.all)});
126
+ return {
127
+ count : dec_time . all . length ,
128
+ min : dec_time . min ,
129
+ avg : avg ,
130
+ max : dec_time . max
131
+ } ;
132
+ }
133
+
50
134
function decqueue_update ( duration ) {
51
135
decqueue_aggregate . all . push ( duration ) ;
52
136
decqueue_aggregate . min = Math . min ( decqueue_aggregate . min , duration ) ;
@@ -114,9 +198,13 @@ class pipeline {
114
198
return new TransformStream ( {
115
199
start ( controller ) {
116
200
this . decoder = decoder = new VideoDecoder ( {
117
- output : frame => controller . enqueue ( frame ) ,
201
+ output : ( frame ) => {
202
+ const after = performance . now ( ) ;
203
+ dec_update ( { output : 1 , timestamp : frame . timestamp , time : after } ) ;
204
+ controller . enqueue ( frame ) ;
205
+ } ,
118
206
error : ( e ) => {
119
- self . postMessage ( { severity : 'fatal' , text : `Init Decoder error: ${ e . message } ` } ) ;
207
+ self . postMessage ( { severity : 'fatal' , text : `Decoder error: ${ e . message } ` } ) ;
120
208
}
121
209
} ) ;
122
210
} ,
@@ -139,6 +227,8 @@ class pipeline {
139
227
try {
140
228
const queue = this . decoder . decodeQueueSize ;
141
229
decqueue_update ( queue ) ;
230
+ const before = performance . now ( ) ;
231
+ dec_update ( { output : 0 , timestamp : chunk . timestamp , time : before } ) ;
142
232
this . decoder . decode ( chunk ) ;
143
233
} catch ( e ) {
144
234
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 } ) ;
@@ -175,6 +265,10 @@ class pipeline {
175
265
config : decoderConfig
176
266
} ;
177
267
controller . enqueue ( configChunk ) ;
268
+ }
269
+ if ( chunk . type != 'config' ) {
270
+ const after = performance . now ( ) ;
271
+ enc_update ( { output : 1 , timestamp : chunk . timestamp , time : after } ) ;
178
272
}
179
273
chunk . temporalLayerId = 0 ;
180
274
if ( cfg . svc ) {
@@ -218,6 +312,8 @@ class pipeline {
218
312
if ( this . encoder . state != "closed" ) {
219
313
const queue = this . encoder . encodeQueueSize ;
220
314
encqueue_update ( queue ) ;
315
+ const before = performance . now ( ) ;
316
+ enc_update ( { output : 0 , timestamp : frame . timestamp , time : before } ) ;
221
317
this . encoder . encode ( frame , { keyFrame : insert_keyframe } ) ;
222
318
}
223
319
} catch ( e ) {
@@ -236,10 +332,16 @@ class pipeline {
236
332
this . stopped = true ;
237
333
const len = encqueue_aggregate . all . length ;
238
334
if ( len > 1 ) {
335
+ const enc_stats = enc_report ( ) ;
239
336
const encqueue_stats = encqueue_report ( ) ;
337
+ const dec_stats = dec_report ( ) ;
240
338
const decqueue_stats = decqueue_report ( ) ;
241
- self . postMessage ( { severity : 'chart' } ) ;
339
+ self . postMessage ( { severity : 'chart' , x : 'Frame Number' , y : 'Glass-Glass Latency' , label : 'Glass-Glass Latency (ms) by Frame Number' , div : 'chart2_div' , text : '' } ) ;
340
+ self . postMessage ( { severity : 'chart' , x : 'Timestamp' , y : 'Encoding Time' , label : 'Encoding Time (ms) by Timestamp' , div : 'chart3_div' , text : JSON . stringify ( enc_time . all ) } ) ;
341
+ self . postMessage ( { severity : 'chart' , x : 'Timestamp' , y : 'Decoding Time' , label : 'Decoding Time (ms) by Timestamp' , div : 'chart4_div' , text : JSON . stringify ( dec_time . all ) } ) ;
342
+ self . postMessage ( { text : 'Encoder Time report: ' + JSON . stringify ( enc_stats ) } ) ;
242
343
self . postMessage ( { text : 'Encoder Queue report: ' + JSON . stringify ( encqueue_stats ) } ) ;
344
+ self . postMessage ( { text : 'Decoder Time report: ' + JSON . stringify ( dec_stats ) } ) ;
243
345
self . postMessage ( { text : 'Decoder Queue report: ' + JSON . stringify ( decqueue_stats ) } ) ;
244
346
}
245
347
self . postMessage ( { text : 'stop(): frame, encoder and decoder closed' } ) ;
0 commit comments