av/bitrate/BitrateCalculator.go

73 lines
2.0 KiB
Go
Raw Normal View History

2017-12-06 05:23:22 +03:00
/*
NAME
2017-12-06 06:11:32 +03:00
BitrateCalculator.go - is a simple struct with methods to allow for easy
calculation of bitrate.
2017-12-06 05:23:22 +03:00
DESCRIPTION
See Readme.md
AUTHOR
2017-12-06 06:11:32 +03:00
Saxon Nelson-Milton <saxon.milton@gmail.com>
2017-12-06 05:23:22 +03:00
LICENSE
2017-12-06 06:11:32 +03:00
BitrateCalculator.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
2017-12-06 05:23:22 +03:00
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
package bitrate
import (
2017-12-06 05:23:22 +03:00
"fmt"
"time"
)
2017-12-06 05:23:22 +03:00
// bitrateCalculator implements interface BitrateCalculator
type BitrateCalculator struct {
2017-12-06 06:11:32 +03:00
outputDelay int // sec
now time.Time
prev time.Time
startedBefore bool
elapsedTime time.Duration
lastDisplayTime time.Time
}
2017-12-06 05:23:22 +03:00
// Place this at the start of the code segment that you would like to time
func (bc *BitrateCalculator) Start(outputDelay int) {
2017-12-06 06:11:32 +03:00
if outputDelay >= 0 {
bc.outputDelay = outputDelay
} else {
bc.outputDelay = 0
}
bc.prev = time.Now()
2017-12-06 05:23:22 +03:00
if !bc.startedBefore {
bc.startedBefore = true
bc.elapsedTime = time.Duration(0)
2017-12-06 06:11:32 +03:00
bc.lastDisplayTime = time.Now()
2017-12-06 05:23:22 +03:00
}
}
2017-12-06 05:23:22 +03:00
// Place this at the end of the code segment that you would like to time
func (bc *BitrateCalculator) Stop(noOfKB float64) (bitrate int64) {
2017-12-06 06:11:32 +03:00
bc.now = time.Now()
2017-12-06 05:23:22 +03:00
deltaTime := bc.now.Sub(bc.prev)
if bc.now.Sub(bc.lastDisplayTime) > time.Duration(bc.outputDelay)*time.Second {
bitrate = int64(noOfKB / float64(deltaTime/1e9))
fmt.Printf("Bitrate: %d kbps\n", bitrate)
bc.elapsedTime = time.Duration(0)
2017-12-06 06:11:32 +03:00
bc.lastDisplayTime = bc.now
2017-12-06 05:23:22 +03:00
}
return
}