import Logger from '../logger/Logger' ; import BackgroundFilterVideoFrameProcessorDelegate from './BackgroundFilterVideoFrameProcessorDelegate' ; const FILTER_DURATION_FACTOR = 0.8 ; /**
 * The frame counter tracks frame rates of video and segmentation.
 */ internal export default class BackgroundFilterFrameCounter { private _processingFilter : boolean = true ; private lastReportedEventTimestamp : number = 0 ; private lastFilterCompleteTimestamp : number = 0 ; private filterTotalMillis : number = 0 ; private filterCount : number = 0 ; private framerate : number = 0 ; private filterDurationNotifyMillis = 0 ; constructor ( private delegate , private reportingPeriodMillis , private filterCPUUtilization , private logger ) { this . setSegmentationDuration ( ) ; } /**
     * Report events once per period.
     */ private reportEvent ( timestamp ) { const timeDiff = timestamp - this . lastReportedEventTimestamp ; if ( timeDiff >= this . reportingPeriodMillis ) { const currentFilterCount = this . filterCount ; const currentFilterTotalMillis = this . filterTotalMillis ; this . filterCount = 0 ; this . filterTotalMillis = 0 ; this . lastReportedEventTimestamp = timestamp ; if ( this . framerate === 0 || currentFilterCount === 0 ) { return ; } const avgFilterDurationMillis = Math . round ( currentFilterTotalMillis / currentFilterCount ) ; const framesDropped = Math . round ( this . framerate * ( timeDiff / 1000 ) ) - currentFilterCount ; const cpuUtilization = Math . round ( ( 100 * currentFilterTotalMillis ) / timeDiff ) ; if ( avgFilterDurationMillis >= this . filterDurationNotifyMillis ) { this . delegate . filterFrameDurationHigh ( { framesDropped , avgFilterDurationMillis , framerate : this . framerate , periodMillis : timeDiff , } ) ; } if ( cpuUtilization >= this . filterCPUUtilization ) { this . delegate . filterCPUUtilizationHigh ( { cpuUtilization , filterMillis : currentFilterTotalMillis , periodMillis : timeDiff , } ) ; } } } private setSegmentationDuration ( ) { this . filterDurationNotifyMillis = Math . round ( ( 1000 / this . framerate ) * FILTER_DURATION_FACTOR ) ; } frameReceived ( framerate ) { if ( framerate !== this . framerate ) { this . framerate = framerate ; this . logger . info ( `frame counter setting frame rate to ${ this . framerate }` ) ; this . setSegmentationDuration ( ) ; } const timestamp = Date . now ( ) ; this . reportEvent ( timestamp ) ; } filterSubmitted ( ) { this . _processingFilter = true ; this . lastFilterCompleteTimestamp = Date . now ( ) ; } filterComplete ( ) { this . filterTotalMillis += Date . now ( ) - this . lastFilterCompleteTimestamp ; this . _processingFilter = false ; this . filterCount ++ ; } get processingSegment ( ) : boolean { return this . _processingFilter ; } }