Modified bitrate calc to hopefully be more efficient

This commit is contained in:
Saxon1 2017-11-22 16:06:51 +10:30
parent e6e7c2bcc6
commit 5f6f34cb25
1 changed files with 14 additions and 21 deletions

View File

@ -67,6 +67,7 @@ const (
rtpSSRC = 1 // any value will do rtpSSRC = 1 // any value will do
bitsInByte = 8 bitsInByte = 8
bitrateOutputDelay = 60 // s bitrateOutputDelay = 60 // s
nanoSecPerSec = 1000000000
) )
// flag values // flag values
@ -91,9 +92,10 @@ var (
dumpCC int dumpCC int
dumpPCRBase uint64 dumpPCRBase uint64
rtpSequenceNum uint16 rtpSequenceNum uint16
bitSum int bitSum int = 0
ffmpegPath string ffmpegPath string
tempDir string tempDir string
bitRateOutCount float64 = 0.0
) )
// command-line flags // command-line flags
@ -108,8 +110,8 @@ var (
func main() { func main() {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
ffmpegPath = "C:/ffmpeg" ffmpegPath = "C:/ffmpeg/ffmpeg"
tempDir = "C:/Users/%%USERPROFILE%%/AppData/Local/Temp" tempDir = "tmp/"
} else { } else {
ffmpegPath = "/usr/bin/ffmpeg" ffmpegPath = "/usr/bin/ffmpeg"
tempDir = "/tmp/" tempDir = "/tmp/"
@ -151,9 +153,6 @@ func main() {
log.Fatal("Cannot combine filterFixContinuity and dumpProgramInfo flags\n") log.Fatal("Cannot combine filterFixContinuity and dumpProgramInfo flags\n")
} }
// Start a thread to calculate bitrate
go bitrateCalculator()
for { for {
err := readWriteVideo(*input, *output) err := readWriteVideo(*input, *output)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
@ -161,19 +160,6 @@ func main() {
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
} }
} }
func bitrateCalculator() {
prevTime := time.Now()
for {
now := time.Now()
if now.Sub(prevTime) > bitrateOutputDelay*time.Second {
fmt.Printf("Bitrate: %d bps\n", int64(float64(bitSum)/(float64(now.Sub(prevTime)/1000000000))))
bitSum = 0
prevTime = now
}
}
}
// readWriteVideo reads video from an RTSP stream (specified by the input URL) and // readWriteVideo reads video from an RTSP stream (specified by the input URL) and
// rewrites the video in various formats and/or different protocols (HTTP, UDP or RTP). // rewrites the video in various formats and/or different protocols (HTTP, UDP or RTP).
func readWriteVideo(input string, output string) error { func readWriteVideo(input string, output string) error {
@ -265,8 +251,15 @@ func readWriteVideo(input string, output string) error {
clipCount++ clipCount++
if err = sendClip(clip[:clipSize], output, conn); err != nil { if err = sendClip(clip[:clipSize], output, conn); err != nil {
return err return err
} else { }
bitSum += clipSize * bitsInByte
// Calculate bitrate and Output
deltaTime := float64(now.Sub(prevTime)/nanoSecPerSec)
bitRateOutCount += deltaTime
if bitRateOutCount > bitrateOutputDelay {
noOfBits := clipSize * bitsInByte
fmt.Printf("Bitrate: %d bps\n",int64(float64(noOfBits)/deltaTime))
bitRateOutCount = 0.0
} }
clipSize = 0 clipSize = 0
packetCount = 0 packetCount = 0