File tree 2 files changed +17
-6
lines changed 2 files changed +17
-6
lines changed Original file line number Diff line number Diff line change 1
1
export const calculateMean = ( values : number [ ] ) => values . reduce ( ( acc , val ) => acc + val , 0 ) / values . length ;
2
2
3
+ export const calculateVariance = ( mean : number , values : number [ ] ) => values
4
+ . reduce ( ( sum , val ) => sum + ( val - mean ) ** 2 , 0 ) / values . length ;
5
+
6
+ export const calculateStandardDeviation = ( values : number [ ] ) => {
7
+ const mean = calculateMean ( values ) ;
8
+ const variance = calculateVariance ( mean , values ) ;
9
+ return Math . sqrt ( variance ) ;
10
+ } ;
11
+
3
12
export const calculateVolatility = ( values : number [ ] ) => {
4
13
if ( values . length === 0 ) {
5
14
throw new Error ( 'Cannot calculate volatility for empty array' ) ;
Original file line number Diff line number Diff line change 1
1
import { WebRTCStatsParsedWithNetworkScores } from '../types' ;
2
+ import { calculateStandardDeviation } from './calc' ;
2
3
3
4
export const isDtxLikeBehavior = (
4
5
ssrc : number ,
5
6
allProcessedStats : WebRTCStatsParsedWithNetworkScores [ ] ,
6
7
stdDevThreshold = 30 ,
7
8
) : boolean => {
8
9
const frameIntervals : number [ ] = [ ] ;
9
- for ( let i = 0 ; i < allProcessedStats . length - 1 ; i += 1 ) {
10
- const videoStreamStats = allProcessedStats [ i ] . video . inbound . find (
10
+ for ( let i = 1 ; i < allProcessedStats . length - 1 ; i += 1 ) {
11
+ const videoStreamStats = allProcessedStats [ i ] ? .video ? .inbound . find (
11
12
( stream ) => stream . ssrc === ssrc ,
12
13
) ;
13
14
15
+ if ( ! videoStreamStats ) {
16
+ continue ;
17
+ }
18
+
14
19
const previousVideoStreamStats = allProcessedStats [ i - 1 ] ?. video ?. inbound ?. find (
15
20
( stream ) => stream . ssrc === ssrc ,
16
21
) ;
@@ -32,9 +37,6 @@ export const isDtxLikeBehavior = (
32
37
return false ;
33
38
}
34
39
35
- const mean = frameIntervals . reduce ( ( a , b ) => a + b , 0 ) / frameIntervals . length ;
36
- const variance = frameIntervals
37
- . reduce ( ( sum , val ) => sum + ( val - mean ) ** 2 , 0 ) / frameIntervals . length ;
38
- const stdDev = Math . sqrt ( variance ) ;
40
+ const stdDev = calculateStandardDeviation ( frameIntervals ) ;
39
41
return stdDev > stdDevThreshold ;
40
42
} ;
You can’t perform that action at this time.
0 commit comments