Merged in BitrateCalculator (pull request #1)

BitrateCalculator

Approved-by: Alan Noble <anoble@gmail.com>
This commit is contained in:
saxon.milton@gmail.com 2017-11-24 00:45:45 +00:00 committed by Alan Noble
commit 26f88643ab
1 changed files with 39 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"
@ -53,18 +54,19 @@ import (
// defaults and networking consts // defaults and networking consts
const ( const (
defaultPID = 256 defaultPID = 256
defaultFrameRate = 25 defaultFrameRate = 25
defaultHTTPOutput = "http://localhost:8080?" defaultHTTPOutput = "http://localhost:8080?"
defaultUDPOutput = "udp://0.0.0.0:16384" defaultUDPOutput = "udp://0.0.0.0:16384"
defaultRTPOutput = "rtp://0.0.0.0:16384" defaultRTPOutput = "rtp://0.0.0.0:16384"
mp2tPacketSize = 188 // MPEG-TS packet size mp2tPacketSize = 188 // MPEG-TS packet size
mp2tMaxPackets = 2016 // # first multiple of 7 and 8 greater than 2000 mp2tMaxPackets = 2016 // # first multiple of 7 and 8 greater than 2000
udpPackets = 7 // # of UDP packets per ethernet frame (8 is the max) udpPackets = 7 // # of UDP packets per ethernet frame (8 is the max)
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,8 @@ var (
dumpCC int dumpCC int
dumpPCRBase uint64 dumpPCRBase uint64
rtpSequenceNum uint16 rtpSequenceNum uint16
ffmpegPath string
tempDir string
) )
// command-line flags // command-line flags
@ -102,6 +106,7 @@ var (
) )
func main() { func main() {
setUpDirs()
flag.Parse() flag.Parse()
if *input == "" { if *input == "" {
@ -146,6 +151,17 @@ func main() {
} }
} }
// set up directories based on the OS we're running on
func setUpDirs() {
if runtime.GOOS == "windows" {
ffmpegPath = "C:/ffmpeg/ffmpeg"
tempDir = "tmp/"
} else {
ffmpegPath = "/usr/bin/ffmpeg"
tempDir = "/tmp/"
}
}
// 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 {
@ -217,6 +233,7 @@ func readWriteVideo(input string, output string) error {
prevTime := now prevTime := now
fmt.Printf("Looping\n") fmt.Printf("Looping\n")
elapsedTime := time.Duration(0)
for { for {
_, err := io.ReadFull(br, pkt) _, err := io.ReadFull(br, pkt)
if err != nil { if err != nil {
@ -238,6 +255,15 @@ func readWriteVideo(input string, output string) error {
if err = sendClip(clip[:clipSize], output, conn); err != nil { if err = sendClip(clip[:clipSize], output, conn); err != nil {
return err return err
} }
// Calculate bitrate and Output
deltaTime := now.Sub(prevTime)
elapsedTime += deltaTime
if elapsedTime > bitrateOutputDelay*time.Nanosecond {
noOfBits := float64(clipSize * bitsInByte) / 1024.0 // to kbits
fmt.Printf("Bitrate: %d kbps\n", int64(noOfBits/float64(deltaTime/1e9)))
elapsedTime = time.Duration(0)
}
clipSize = 0 clipSize = 0
packetCount = 0 packetCount = 0
prevTime = now prevTime = now
@ -248,7 +274,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 {