started writing BitrateCalculatorStruct

This commit is contained in:
Saxon1 2017-12-04 17:07:39 +10:30
parent 6c0f923480
commit f8aa535ec7
2 changed files with 41 additions and 12 deletions

View File

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

View File

@ -298,25 +298,17 @@ func input(input string, output string) {
// output handles the writing to specified output
func output(output string) {
elapsedTime := time.Duration(0)
now := time.Now()
prevTime := now
bitrateCalc := BitrateCalculator{60}
for {
if clip, err := ringBuffer.Read(); err == nil {
now := time.Now()
bitrateCalc.Start()
err := sendClip(clip, output, conn)
for err != nil {
outputErrChan <- err
err = sendClip(clip, output, conn)
}
deltaTime := now.Sub(prevTime)
elapsedTime += deltaTime
if elapsedTime > bitrateOutputDelay*time.Second {
noOfBits := float64(len(clip)*8) / 1024.0 // convert bytes to kilobits
fmt.Printf("Bitrate: %d kbps\n", int64(noOfBits/float64(deltaTime/1e9)))
elapsedTime = time.Duration(0)
}
prevTime = now
noOfKB = float64(len(clip)*8) / 1024.0 // Convert bytes to kilobits
bitrateCalc.End(, true)
if err := ringBuffer.DoneReading(); err != nil {
outputErrChan <- err
}