1
1
'use strict' ;
2
2
3
3
let encoder , decoder , pl , started = false , stopped = false ;
4
- let enc_aggregate = {
5
- all : [ ] ,
6
- min : Number . MAX_VALUE ,
7
- max : 0 ,
8
- avg : 0 ,
9
- sum : 0 ,
10
- } ;
11
-
12
- let dec_aggregate = {
13
- all : [ ] ,
14
- min : Number . MAX_VALUE ,
15
- max : 0 ,
16
- avg : 0 ,
17
- sum : 0 ,
18
- } ;
19
-
20
- let encqueue_aggregate = {
21
- all : [ ] ,
22
- min : Number . MAX_VALUE ,
23
- max : 0 ,
24
- avg : 0 ,
25
- sum : 0 ,
26
- } ;
27
-
28
- let decqueue_aggregate = {
29
- all : [ ] ,
30
- min : Number . MAX_VALUE ,
31
- max : 0 ,
32
- avg : 0 ,
33
- sum : 0 ,
34
- } ;
35
-
36
- function enc_update ( duration ) {
37
- enc_aggregate . all . push ( duration ) ;
38
- enc_aggregate . min = Math . min ( enc_aggregate . min , duration ) ;
39
- enc_aggregate . max = Math . max ( enc_aggregate . max , duration ) ;
40
- enc_aggregate . sum += duration ;
41
- }
42
-
43
- function encqueue_update ( duration ) {
44
- encqueue_aggregate . all . push ( duration ) ;
45
- encqueue_aggregate . min = Math . min ( encqueue_aggregate . min , duration ) ;
46
- encqueue_aggregate . max = Math . max ( encqueue_aggregate . max , duration ) ;
47
- encqueue_aggregate . sum += duration ;
48
- }
49
-
50
- function enc_report ( ) {
51
- enc_aggregate . all . sort ( ) ;
52
- const len = enc_aggregate . all . length ;
53
- const half = len >> 1 ;
54
- const f = ( len + 1 ) >> 2 ;
55
- const t = ( 3 * ( len + 1 ) ) >> 2 ;
56
- const alpha1 = ( len + 1 ) / 4 - Math . trunc ( ( len + 1 ) / 4 ) ;
57
- const alpha3 = ( 3 * ( len + 1 ) / 4 ) - Math . trunc ( 3 * ( len + 1 ) / 4 ) ;
58
- const fquart = enc_aggregate . all [ f ] + alpha1 * ( enc_aggregate . all [ f + 1 ] - enc_aggregate . all [ f ] ) ;
59
- const tquart = enc_aggregate . all [ t ] + alpha3 * ( enc_aggregate . all [ t + 1 ] - enc_aggregate . all [ t ] ) ;
60
- const median = len % 2 === 1 ? enc_aggregate . all [ len >> 1 ] : ( enc_aggregate . all [ half - 1 ] + enc_aggregate . all [ half ] ) / 2 ;
61
- return {
62
- count : len ,
63
- min : enc_aggregate . min ,
64
- fquart : fquart ,
65
- avg : enc_aggregate . sum / len ,
66
- median : median ,
67
- tquart : tquart ,
68
- max : enc_aggregate . max ,
69
- } ;
70
- }
71
-
72
- function encqueue_report ( ) {
73
- encqueue_aggregate . all . sort ( ) ;
74
- const len = encqueue_aggregate . all . length ;
75
- const half = len >> 1 ;
76
- const f = ( len + 1 ) >> 2 ;
77
- const t = ( 3 * ( len + 1 ) ) >> 2 ;
78
- const alpha1 = ( len + 1 ) / 4 - Math . trunc ( ( len + 1 ) / 4 ) ;
79
- const alpha3 = ( 3 * ( len + 1 ) / 4 ) - Math . trunc ( 3 * ( len + 1 ) / 4 ) ;
80
- const fquart = encqueue_aggregate . all [ f ] + alpha1 * ( encqueue_aggregate . all [ f + 1 ] - encqueue_aggregate . all [ f ] ) ;
81
- const tquart = encqueue_aggregate . all [ t ] + alpha3 * ( encqueue_aggregate . all [ t + 1 ] - encqueue_aggregate . all [ t ] ) ;
82
- const median = len % 2 === 1 ? encqueue_aggregate . all [ len >> 1 ] : ( encqueue_aggregate . all [ half - 1 ] + encqueue_aggregate . all [ half ] ) / 2 ;
83
- return {
84
- count : len ,
85
- min : encqueue_aggregate . min ,
86
- fquart : fquart ,
87
- avg : encqueue_aggregate . sum / len ,
88
- median : median ,
89
- tquart : tquart ,
90
- max : encqueue_aggregate . max ,
91
- } ;
92
- }
93
-
94
- function dec_update ( duration ) {
95
- dec_aggregate . all . push ( duration ) ;
96
- dec_aggregate . min = Math . min ( dec_aggregate . min , duration ) ;
97
- dec_aggregate . max = Math . max ( dec_aggregate . max , duration ) ;
98
- dec_aggregate . sum += duration ;
99
- }
100
-
101
- function decqueue_update ( duration ) {
102
- decqueue_aggregate . all . push ( duration ) ;
103
- decqueue_aggregate . min = Math . min ( decqueue_aggregate . min , duration ) ;
104
- decqueue_aggregate . max = Math . max ( decqueue_aggregate . max , duration ) ;
105
- decqueue_aggregate . sum += duration ;
106
- }
107
-
108
- function dec_report ( ) {
109
- dec_aggregate . all . sort ( ) ;
110
- const len = dec_aggregate . all . length ;
111
- const half = len >> 1 ;
112
- const f = ( len + 1 ) >> 2 ;
113
- const t = ( 3 * ( len + 1 ) ) >> 2 ;
114
- const alpha1 = ( len + 1 ) / 4 - Math . trunc ( ( len + 1 ) / 4 ) ;
115
- const alpha3 = ( 3 * ( len + 1 ) / 4 ) - Math . trunc ( 3 * ( len + 1 ) / 4 ) ;
116
- const fquart = dec_aggregate . all [ f ] + alpha1 * ( dec_aggregate . all [ f + 1 ] - dec_aggregate . all [ f ] ) ;
117
- const tquart = dec_aggregate . all [ t ] + alpha3 * ( dec_aggregate . all [ t + 1 ] - dec_aggregate . all [ t ] ) ;
118
- const median = len % 2 === 1 ? dec_aggregate . all [ len >> 1 ] : ( dec_aggregate . all [ half - 1 ] + dec_aggregate . all [ half ] ) / 2 ;
119
- return {
120
- count : len ,
121
- min : dec_aggregate . min ,
122
- fquart : fquart ,
123
- avg : dec_aggregate . sum / len ,
124
- median : median ,
125
- tquart : tquart ,
126
- max : dec_aggregate . max ,
127
- } ;
128
- }
129
-
130
- function decqueue_report ( ) {
131
- decqueue_aggregate . all . sort ( ) ;
132
- const len = decqueue_aggregate . all . length ;
133
- const half = len >> 1 ;
134
- const f = ( len + 1 ) >> 2 ;
135
- const t = ( 3 * ( len + 1 ) ) >> 2 ;
136
- const alpha1 = ( len + 1 ) / 4 - Math . trunc ( ( len + 1 ) / 4 ) ;
137
- const alpha3 = ( 3 * ( len + 1 ) / 4 ) - Math . trunc ( 3 * ( len + 1 ) / 4 ) ;
138
- const fquart = decqueue_aggregate . all [ f ] + alpha1 * ( decqueue_aggregate . all [ f + 1 ] - decqueue_aggregate . all [ f ] ) ;
139
- const tquart = decqueue_aggregate . all [ t ] + alpha3 * ( decqueue_aggregate . all [ t + 1 ] - decqueue_aggregate . all [ t ] ) ;
140
- const median = len % 2 === 1 ? decqueue_aggregate . all [ len >> 1 ] : ( decqueue_aggregate . all [ half - 1 ] + decqueue_aggregate . all [ half ] ) / 2 ;
141
- return {
142
- count : len ,
143
- min : decqueue_aggregate . min ,
144
- fquart : fquart ,
145
- avg : decqueue_aggregate . sum / len ,
146
- median : median ,
147
- tquart : tquart ,
148
- max : decqueue_aggregate . max ,
149
- } ;
150
- }
151
4
152
5
self . addEventListener ( 'message' , async function ( e ) {
153
6
if ( stopped ) return ;
@@ -210,13 +63,7 @@ class pipeline {
210
63
} )
211
64
} else {
212
65
try {
213
- const queue = this . decoder . decodeQueueSize ;
214
- decqueue_update ( queue ) ;
215
- const before = performance . now ( ) ;
216
66
this . decoder . decode ( chunk ) ;
217
- const after = performance . now ( ) ;
218
- const duration = after - before ;
219
- dec_update ( duration ) ;
220
67
} catch ( e ) {
221
68
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 } ) ;
222
69
self . postMessage ( { severity : 'fatal' , text : `Catch Decode error: ${ e . message } ` } ) ;
@@ -292,13 +139,7 @@ class pipeline {
292
139
this . frameCounter ++ ;
293
140
try {
294
141
if ( this . encoder . state != "closed" ) {
295
- const queue = this . encoder . encodeQueueSize ;
296
- encqueue_update ( queue ) ;
297
- const before = performance . now ( ) ;
298
142
this . encoder . encode ( frame , { keyFrame : insert_keyframe } ) ;
299
- const after = performance . now ( ) ;
300
- const duration = after - before ;
301
- enc_update ( duration ) ;
302
143
}
303
144
} catch ( e ) {
304
145
self . postMessage ( { severity : 'fatal' , text : 'Encoder Error: ' + e . message } ) ;
@@ -310,14 +151,6 @@ class pipeline {
310
151
}
311
152
312
153
stop ( ) {
313
- const enc_stats = enc_report ( ) ;
314
- const encqueue_stats = encqueue_report ( ) ;
315
- const dec_stats = dec_report ( ) ;
316
- const decqueue_stats = decqueue_report ( ) ;
317
- self . postMessage ( { text : 'Encoder Time report: ' + JSON . stringify ( enc_stats ) } ) ;
318
- self . postMessage ( { text : 'Encoder Queue report: ' + JSON . stringify ( encqueue_stats ) } ) ;
319
- self . postMessage ( { text : 'Decoder Time report: ' + JSON . stringify ( dec_stats ) } ) ;
320
- self . postMessage ( { text : 'Decoder Queue report: ' + JSON . stringify ( decqueue_stats ) } ) ;
321
154
if ( stopped ) return ;
322
155
stopped = true ;
323
156
this . stopped = true ;
0 commit comments