From e6e7c2bcc6e48322f13bc1ff5275f2f7711a870e Mon Sep 17 00:00:00 2001 From: Saxon1 Date: Wed, 22 Nov 2017 14:17:34 +1030 Subject: [PATCH] Added bitrateCalculator function to run as thread. Now need to debug. --- revid/revid.go | 56 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/revid/revid.go b/revid/revid.go index ac3a0ed4..abfbf525 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -42,6 +42,7 @@ import ( "net/http" "os" "os/exec" + "runtime" "strconv" "strings" "time" @@ -53,18 +54,19 @@ import ( // defaults and networking consts const ( - defaultPID = 256 - defaultFrameRate = 25 - defaultHTTPOutput = "http://localhost:8080?" - defaultUDPOutput = "udp://0.0.0.0:16384" - defaultRTPOutput = "rtp://0.0.0.0:16384" - mp2tPacketSize = 188 // MPEG-TS packet size - mp2tMaxPackets = 2016 // # first multiple of 7 and 8 greater than 2000 - udpPackets = 7 // # of UDP packets per ethernet frame (8 is the max) - rtpPackets = 7 // # of RTP packets per ethernet frame (7 is the max) - rtpHeaderSize = 12 - rtpSSRC = 1 // any value will do - ffmpegPath = "/usr/bin/ffmpeg" + defaultPID = 256 + defaultFrameRate = 25 + defaultHTTPOutput = "http://localhost:8080?" + defaultUDPOutput = "udp://0.0.0.0:16384" + defaultRTPOutput = "rtp://0.0.0.0:16384" + mp2tPacketSize = 188 // MPEG-TS packet size + mp2tMaxPackets = 2016 // # first multiple of 7 and 8 greater than 2000 + udpPackets = 7 // # of UDP packets per ethernet frame (8 is the max) + rtpPackets = 7 // # of RTP packets per ethernet frame (7 is the max) + rtpHeaderSize = 12 + rtpSSRC = 1 // any value will do + bitsInByte = 8 + bitrateOutputDelay = 60 // s ) // flag values @@ -89,6 +91,9 @@ var ( dumpCC int dumpPCRBase uint64 rtpSequenceNum uint16 + bitSum int + ffmpegPath string + tempDir string ) // command-line flags @@ -102,6 +107,14 @@ var ( ) 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() if *input == "" { @@ -138,6 +151,9 @@ func main() { log.Fatal("Cannot combine filterFixContinuity and dumpProgramInfo flags\n") } + // Start a thread to calculate bitrate + go bitrateCalculator() + for { err := readWriteVideo(*input, *output) 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 // rewrites the video in various formats and/or different protocols (HTTP, UDP or RTP). func readWriteVideo(input string, output string) error { @@ -237,6 +265,8 @@ func readWriteVideo(input string, output string) error { clipCount++ if err = sendClip(clip[:clipSize], output, conn); err != nil { return err + } else { + bitSum += clipSize * bitsInByte } clipSize = 0 packetCount = 0 @@ -248,7 +278,7 @@ func readWriteVideo(input string, output string) error { // sendClipToFile writes a video clip to a /tmp file. 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)) err := ioutil.WriteFile(filename, clip, 0644) if err != nil {