mirror of https://bitbucket.org/ausocean/av.git
Merged in BitrateCalculator (pull request #1)
BitrateCalculator Approved-by: Alan Noble <anoble@gmail.com>
This commit is contained in:
commit
26f88643ab
|
@ -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,8 @@ var (
|
|||
dumpCC int
|
||||
dumpPCRBase uint64
|
||||
rtpSequenceNum uint16
|
||||
ffmpegPath string
|
||||
tempDir string
|
||||
)
|
||||
|
||||
// command-line flags
|
||||
|
@ -102,6 +106,7 @@ var (
|
|||
)
|
||||
|
||||
func main() {
|
||||
setUpDirs()
|
||||
flag.Parse()
|
||||
|
||||
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
|
||||
// rewrites the video in various formats and/or different protocols (HTTP, UDP or RTP).
|
||||
func readWriteVideo(input string, output string) error {
|
||||
|
@ -217,6 +233,7 @@ func readWriteVideo(input string, output string) error {
|
|||
prevTime := now
|
||||
fmt.Printf("Looping\n")
|
||||
|
||||
elapsedTime := time.Duration(0)
|
||||
for {
|
||||
_, err := io.ReadFull(br, pkt)
|
||||
if err != nil {
|
||||
|
@ -238,6 +255,15 @@ func readWriteVideo(input string, output string) error {
|
|||
if err = sendClip(clip[:clipSize], output, conn); err != nil {
|
||||
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
|
||||
packetCount = 0
|
||||
prevTime = now
|
||||
|
@ -248,7 +274,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 {
|
||||
|
|
Loading…
Reference in New Issue