Think I've finished the actual code, just need to write some testing utilities

This commit is contained in:
Saxon Milton 2017-12-06 08:54:20 +10:30
parent ca48e4a9d4
commit f013a3e1f3
1 changed files with 15 additions and 18 deletions

View File

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