av/bitrate/BitrateCalculator.go

39 lines
745 B
Go

package bitrate
import (
"time"
"fmt"
)
type BitrateCalculator struct {
Delay int // sec
now
prev
isFirstTime bool
elapsedTime time.Time
}
// The bitrate calculator
func (bc *BitrateCalculator) Start() {
if bc.isFirstTime {
if Delay == nil {
bc.Delay = 5 * time.Second
}
bc.now = time.Now()
bc.prev = now
bc.isFirstTime = false
bc.elapsedTime = time.Duration(0)
}
bc.now = time.Now()
}
func (bc *BitrateCalculator) Stop(noOfKB int) (bitrate int) {
deltaTime := now.Sub(prevTime)
bc.elapsedTime += deltaTime
if bc.elapsedTime > bc.Delay*time.Second {
fmt.Printf("Bitrate: %d kbps\n", int64(noOfKB/float64(deltaTime/1e9)))
bc.elapsedTime = time.Duration(0)
}
bc.prev = now
}