22
33let encoder , decoder , pl , started = false , stopped = false ;
44
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+
527let encqueue_aggregate = {
628 all : [ ] ,
729 min : Number . MAX_VALUE ,
@@ -18,13 +40,44 @@ let decqueue_aggregate = {
1840 sum : 0 ,
1941} ;
2042
43+ function enc_update ( data ) {
44+ enc_aggregate . all . push ( data ) ;
45+ }
46+
2147function encqueue_update ( duration ) {
2248 encqueue_aggregate . all . push ( duration ) ;
2349 encqueue_aggregate . min = Math . min ( encqueue_aggregate . min , duration ) ;
2450 encqueue_aggregate . max = Math . max ( encqueue_aggregate . max , duration ) ;
2551 encqueue_aggregate . sum += duration ;
2652}
2753
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+
2881function encqueue_report ( ) {
2982 encqueue_aggregate . all . sort ( ) ;
3083 const len = encqueue_aggregate . all . length ;
@@ -47,6 +100,37 @@ function encqueue_report() {
47100 } ;
48101}
49102
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+
50134function decqueue_update ( duration ) {
51135 decqueue_aggregate . all . push ( duration ) ;
52136 decqueue_aggregate . min = Math . min ( decqueue_aggregate . min , duration ) ;
@@ -114,9 +198,13 @@ class pipeline {
114198 return new TransformStream ( {
115199 start ( controller ) {
116200 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+ } ,
118206 error : ( e ) => {
119- self . postMessage ( { severity : 'fatal' , text : `Init Decoder error: ${ e . message } ` } ) ;
207+ self . postMessage ( { severity : 'fatal' , text : `Decoder error: ${ e . message } ` } ) ;
120208 }
121209 } ) ;
122210 } ,
@@ -139,6 +227,8 @@ class pipeline {
139227 try {
140228 const queue = this . decoder . decodeQueueSize ;
141229 decqueue_update ( queue ) ;
230+ const before = performance . now ( ) ;
231+ dec_update ( { output : 0 , timestamp : chunk . timestamp , time : before } ) ;
142232 this . decoder . decode ( chunk ) ;
143233 } catch ( e ) {
144234 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 {
175265 config : decoderConfig
176266 } ;
177267 controller . enqueue ( configChunk ) ;
268+ }
269+ if ( chunk . type != 'config' ) {
270+ const after = performance . now ( ) ;
271+ enc_update ( { output : 1 , timestamp : chunk . timestamp , time : after } ) ;
178272 }
179273 chunk . temporalLayerId = 0 ;
180274 if ( cfg . svc ) {
@@ -218,6 +312,8 @@ class pipeline {
218312 if ( this . encoder . state != "closed" ) {
219313 const queue = this . encoder . encodeQueueSize ;
220314 encqueue_update ( queue ) ;
315+ const before = performance . now ( ) ;
316+ enc_update ( { output : 0 , timestamp : frame . timestamp , time : before } ) ;
221317 this . encoder . encode ( frame , { keyFrame : insert_keyframe } ) ;
222318 }
223319 } catch ( e ) {
@@ -236,10 +332,16 @@ class pipeline {
236332 this . stopped = true ;
237333 const len = encqueue_aggregate . all . length ;
238334 if ( len > 1 ) {
335+ const enc_stats = enc_report ( ) ;
239336 const encqueue_stats = encqueue_report ( ) ;
337+ const dec_stats = dec_report ( ) ;
240338 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 ) } ) ;
242343 self . postMessage ( { text : 'Encoder Queue report: ' + JSON . stringify ( encqueue_stats ) } ) ;
344+ self . postMessage ( { text : 'Decoder Time report: ' + JSON . stringify ( dec_stats ) } ) ;
243345 self . postMessage ( { text : 'Decoder Queue report: ' + JSON . stringify ( decqueue_stats ) } ) ;
244346 }
245347 self . postMessage ( { text : 'stop(): frame, encoder and decoder closed' } ) ;
0 commit comments