Added bitrateCalculator function to run as thread. Now need to debug.

This commit is contained in:
Saxon1 2017-11-22 14:17:34 +10:30
parent f3c15f622e
commit e6e7c2bcc6
1 changed files with 43 additions and 13 deletions

View File

@ -42,6 +42,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"runtime"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -64,7 +65,8 @@ const (
rtpPackets = 7 // # of RTP packets per ethernet frame (7 is the max) rtpPackets = 7 // # of RTP packets per ethernet frame (7 is the max)
rtpHeaderSize = 12 rtpHeaderSize = 12
rtpSSRC = 1 // any value will do rtpSSRC = 1 // any value will do
ffmpegPath = "/usr/bin/ffmpeg" bitsInByte = 8
bitrateOutputDelay = 60 // s
) )
// flag values // flag values
@ -89,6 +91,9 @@ var (
dumpCC int dumpCC int
dumpPCRBase uint64 dumpPCRBase uint64
rtpSequenceNum uint16 rtpSequenceNum uint16
bitSum int
ffmpegPath string
tempDir string
) )
// command-line flags // command-line flags
@ -102,6 +107,14 @@ var (
) )
func main() { func main() {
if runtime.GOOS == "windows" {
ffmpegPath = "C:/ffmpeg"
tempDir = "C:/Users/%%USERPROFILE%%/AppData/Local/Temp"
} else {
ffmpegPath = "/usr/bin/ffmpeg"
tempDir = "/tmp/"
}
flag.Parse() flag.Parse()
if *input == "" { if *input == "" {
@ -138,6 +151,9 @@ 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)
@ -146,6 +162,18 @@ func main() {
} }
} }
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 {
@ -237,6 +265,8 @@ 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
} }
clipSize = 0 clipSize = 0
packetCount = 0 packetCount = 0
@ -248,7 +278,7 @@ func readWriteVideo(input string, output string) error {
// sendClipToFile writes a video clip to a /tmp file. // sendClipToFile writes a video clip to a /tmp file.
func sendClipToFile(clip []byte, _ string, _ net.Conn) error { func sendClipToFile(clip []byte, _ string, _ net.Conn) error {
filename := fmt.Sprintf("/tmp/vid%03d.ts", clipCount) filename := fmt.Sprintf(tempDir + "vid%03d.ts", clipCount)
fmt.Printf("Writing %s (%d bytes)\n", filename, len(clip)) fmt.Printf("Writing %s (%d bytes)\n", filename, len(clip))
err := ioutil.WriteFile(filename, clip, 0644) err := ioutil.WriteFile(filename, clip, 0644)
if err != nil { if err != nil {