2017-09-13 08:00:26 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
revid - a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Alan Noble <anoble@gmail.com>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
revid is Copyright (C) 2017 Alan Noble.
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2017-09-20 14:57:01 +03:00
|
|
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
2017-09-13 08:00:26 +03:00
|
|
|
*/
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
// revid is a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols.
|
2017-09-13 08:00:26 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2017-12-02 08:27:10 +03:00
|
|
|
"crypto/md5"
|
2017-09-13 08:00:26 +03:00
|
|
|
"encoding/binary"
|
2017-12-02 08:27:10 +03:00
|
|
|
"encoding/hex"
|
2017-09-13 08:00:26 +03:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2017-09-21 07:06:54 +03:00
|
|
|
"os"
|
2017-09-13 08:00:26 +03:00
|
|
|
"os/exec"
|
2017-11-22 06:47:34 +03:00
|
|
|
"runtime"
|
2017-09-13 08:00:26 +03:00
|
|
|
"strconv"
|
2017-09-20 14:57:01 +03:00
|
|
|
"strings"
|
2017-09-13 08:00:26 +03:00
|
|
|
"time"
|
2017-09-20 14:57:01 +03:00
|
|
|
|
2017-12-04 08:50:40 +03:00
|
|
|
"bitbucket.org/ausocean/av/ringbuffer"
|
2017-12-02 08:27:10 +03:00
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
"github.com/Comcast/gots/packet"
|
|
|
|
"github.com/Comcast/gots/packet/adaptationfield"
|
|
|
|
"github.com/Comcast/gots/psi"
|
2017-09-13 08:00:26 +03:00
|
|
|
)
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
// defaults and networking consts
|
|
|
|
const (
|
2017-11-22 06:47:34 +03:00
|
|
|
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
|
2017-12-02 08:27:10 +03:00
|
|
|
bufferSize = 1000
|
2017-11-22 06:47:34 +03:00
|
|
|
bitrateOutputDelay = 60 // s
|
2017-12-02 08:27:10 +03:00
|
|
|
httpTimeOut = 5 // s
|
|
|
|
clipDuration = 1 // s
|
2017-09-20 14:57:01 +03:00
|
|
|
)
|
2017-09-13 08:00:26 +03:00
|
|
|
|
2017-09-21 07:06:54 +03:00
|
|
|
// flag values
|
2017-09-13 08:00:26 +03:00
|
|
|
const (
|
2017-09-20 14:57:01 +03:00
|
|
|
filterFixPTS = 0x0001
|
|
|
|
filterDropAudio = 0x0002
|
|
|
|
filterScale640 = 0x0004
|
|
|
|
filterScale320 = 0x0008
|
|
|
|
filterFixContinuity = 0x0010
|
|
|
|
dumpProgramInfo = 0x0100 // 256
|
|
|
|
dumpPacketStats = 0x0200 // 512
|
|
|
|
dumpPacketHeader = 0x0400 // 1024
|
|
|
|
dumpPacketPayload = 0x0800 // 2048
|
2017-09-13 08:00:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// globals
|
2017-09-20 14:57:01 +03:00
|
|
|
var (
|
|
|
|
sendClip = sendClipToRTP
|
2017-09-21 07:06:54 +03:00
|
|
|
packetsPerFrame = rtpPackets
|
2017-09-20 14:57:01 +03:00
|
|
|
clipCount int
|
|
|
|
expectCC int
|
|
|
|
dumpCC int
|
2017-09-21 07:06:54 +03:00
|
|
|
dumpPCRBase uint64
|
2017-09-21 07:08:57 +03:00
|
|
|
rtpSequenceNum uint16
|
2017-12-02 08:27:10 +03:00
|
|
|
conn net.Conn
|
2017-11-23 01:05:36 +03:00
|
|
|
ffmpegPath string
|
|
|
|
tempDir string
|
2017-12-02 08:27:10 +03:00
|
|
|
inputErrChan chan error
|
|
|
|
outputErrChan chan error
|
2017-12-04 07:06:23 +03:00
|
|
|
ringBuffer ringbuffer.RingBuffer
|
2017-09-20 14:57:01 +03:00
|
|
|
)
|
2017-09-13 08:00:26 +03:00
|
|
|
|
2017-09-21 07:06:54 +03:00
|
|
|
// command-line flags
|
|
|
|
var (
|
2017-12-02 08:27:10 +03:00
|
|
|
inputURL = flag.String("i", "", "Input RTSP URL")
|
|
|
|
outputURL = flag.String("o", "", "Output URL (HTTP, UDP or RTP)")
|
2017-09-21 07:06:54 +03:00
|
|
|
mode = flag.String("m", "r", "Mode: one of f,h,u,r or d")
|
|
|
|
flags = flag.Int("f", 0, "Flags: see readme for explanation")
|
|
|
|
frameRate = flag.Int("r", defaultFrameRate, "Input video frame rate (25fps by default)")
|
|
|
|
selectedPID = flag.Int("p", defaultPID, "Select packets with this packet ID (PID)")
|
|
|
|
)
|
2017-09-13 08:00:26 +03:00
|
|
|
|
2017-09-21 07:06:54 +03:00
|
|
|
func main() {
|
2017-11-23 01:05:36 +03:00
|
|
|
setUpDirs()
|
2017-09-20 14:57:01 +03:00
|
|
|
flag.Parse()
|
2017-09-13 08:00:26 +03:00
|
|
|
|
2017-12-02 08:27:10 +03:00
|
|
|
if *inputURL == "" {
|
2017-09-20 14:57:01 +03:00
|
|
|
log.Fatal("Input (-i) required\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch *mode {
|
|
|
|
case "f":
|
|
|
|
sendClip = sendClipToFile
|
|
|
|
case "h":
|
|
|
|
sendClip = sendClipToHTTP
|
2017-12-02 08:27:10 +03:00
|
|
|
if *outputURL == "" {
|
|
|
|
*outputURL = defaultHTTPOutput
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
case "u":
|
|
|
|
sendClip = sendClipToUDP
|
2017-09-21 07:06:54 +03:00
|
|
|
packetsPerFrame = udpPackets
|
2017-12-02 08:27:10 +03:00
|
|
|
if *outputURL == "" {
|
|
|
|
*outputURL = defaultUDPOutput
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
case "r":
|
|
|
|
sendClip = sendClipToRTP
|
2017-09-21 07:06:54 +03:00
|
|
|
packetsPerFrame = rtpPackets
|
2017-12-02 08:27:10 +03:00
|
|
|
if *outputURL == "" {
|
|
|
|
*outputURL = defaultRTPOutput
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
case "d":
|
|
|
|
sendClip = sendClipToStdout
|
|
|
|
default:
|
2017-09-21 07:06:54 +03:00
|
|
|
log.Fatalf("Invalid mode %s\n", *mode)
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
if *flags&filterFixContinuity != 0 && *flags&dumpProgramInfo != 0 {
|
|
|
|
log.Fatal("Cannot combine filterFixContinuity and dumpProgramInfo flags\n")
|
|
|
|
}
|
|
|
|
|
2017-12-04 07:06:23 +03:00
|
|
|
ringBuffer = ringbuffer.NewRingBuffer(bufferSize, mp2tPacketSize*mp2tMaxPackets)
|
2017-12-02 08:27:10 +03:00
|
|
|
inputErrChan = make(chan error, 10)
|
|
|
|
outputErrChan = make(chan error, 10)
|
|
|
|
|
|
|
|
go input(*inputURL, *outputURL)
|
|
|
|
go output(*outputURL)
|
|
|
|
|
|
|
|
// Sit in here once we execute the go routines and handle errors that may
|
|
|
|
// appear in the error channels
|
2017-09-20 14:57:01 +03:00
|
|
|
for {
|
2017-12-02 08:27:10 +03:00
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case err := <-inputErrChan:
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
fmt.Fprintln(os.Stderr, "Trying again in 10s")
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
go input(*inputURL, *outputURL)
|
|
|
|
case err := <-outputErrChan:
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
fmt.Fprintln(os.Stderr, "Attempting to write again!")
|
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
2017-11-23 01:05:36 +03:00
|
|
|
|
2017-12-02 08:27:10 +03:00
|
|
|
// setUpDirs sets directories based on the OS that Revid is running on
|
2017-11-24 03:33:41 +03:00
|
|
|
func setUpDirs() {
|
2017-12-02 08:27:10 +03:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
2017-11-23 01:05:36 +03:00
|
|
|
ffmpegPath = "C:/ffmpeg/ffmpeg"
|
|
|
|
tempDir = "tmp/"
|
2017-12-02 08:27:10 +03:00
|
|
|
case "darwin":
|
|
|
|
ffmpegPath = "/usr/local/bin/ffmpeg"
|
|
|
|
tempDir = "/tmp/"
|
|
|
|
default:
|
2017-11-23 01:05:36 +03:00
|
|
|
ffmpegPath = "/usr/bin/ffmpeg"
|
|
|
|
tempDir = "/tmp/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 08:27:10 +03:00
|
|
|
// input handles the reading from the specified input
|
|
|
|
func input(input string, output string) {
|
2017-09-20 14:57:01 +03:00
|
|
|
fmt.Printf("Reading video from %s\n", input)
|
|
|
|
|
2017-09-21 07:06:54 +03:00
|
|
|
args := []string{
|
|
|
|
"-r", strconv.Itoa(*frameRate),
|
|
|
|
"-i", input,
|
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
|
2017-09-21 04:12:39 +03:00
|
|
|
if *flags&(filterFixPTS|filterScale640|filterScale320) == 0 {
|
2017-09-21 07:06:54 +03:00
|
|
|
args = append(args, "-vcodec", "copy")
|
2017-09-13 08:00:26 +03:00
|
|
|
} else {
|
2017-09-21 07:06:54 +03:00
|
|
|
vfArg := []string{}
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
if *flags&filterFixPTS != 0 {
|
2017-09-21 07:06:54 +03:00
|
|
|
vfArg = append(vfArg, "setpts='PTS-STARTPTS'") // start counting PTS from zero
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
if *flags&filterScale640 != 0 {
|
2017-09-21 07:06:54 +03:00
|
|
|
vfArg = append(vfArg, "scale=640:352")
|
2017-09-20 14:57:01 +03:00
|
|
|
} else if *flags&filterScale320 != 0 {
|
2017-09-21 07:06:54 +03:00
|
|
|
vfArg = append(vfArg, "scale=320:176")
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
2017-09-21 07:06:54 +03:00
|
|
|
args = append(args, "-vf", strings.Join(vfArg, ","))
|
2017-09-20 14:57:01 +03:00
|
|
|
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
|
|
|
|
2017-09-21 07:06:54 +03:00
|
|
|
if *flags&filterDropAudio == 0 {
|
|
|
|
args = append(args, "-acodec", "copy")
|
2017-09-20 14:57:01 +03:00
|
|
|
} else {
|
2017-09-21 07:06:54 +03:00
|
|
|
args = append(args, "-an")
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
2017-09-21 07:06:54 +03:00
|
|
|
args = append(args, "-f", "mpegts", "-")
|
2017-09-20 14:57:01 +03:00
|
|
|
|
2017-09-21 07:06:54 +03:00
|
|
|
fmt.Printf("Executing: %s %s\n", ffmpegPath, strings.Join(args, " "))
|
|
|
|
cmd := exec.Command(ffmpegPath, args...)
|
2017-09-20 14:57:01 +03:00
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2017-12-02 08:27:10 +03:00
|
|
|
inputErrChan <- err
|
|
|
|
return
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
err = cmd.Start()
|
2017-09-13 08:00:26 +03:00
|
|
|
if err != nil {
|
2017-12-02 08:27:10 +03:00
|
|
|
inputErrChan <- err
|
|
|
|
return
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
// (re)initialize globals
|
|
|
|
clipCount = 0
|
|
|
|
expectCC = -1
|
|
|
|
dumpCC = -1
|
2017-09-21 07:06:54 +03:00
|
|
|
dumpPCRBase = 0
|
2017-09-21 07:08:57 +03:00
|
|
|
rtpSequenceNum = uint16(rand.Intn(1 << 15))
|
2017-09-20 14:57:01 +03:00
|
|
|
|
|
|
|
// for UDP and RTP only dial once
|
2017-09-21 07:06:54 +03:00
|
|
|
if strings.HasPrefix(output, "udp://") || strings.HasPrefix(output, "rtp://") {
|
2017-09-20 14:57:01 +03:00
|
|
|
conn, err = net.Dial("udp", output[6:])
|
2017-09-13 08:00:26 +03:00
|
|
|
if err != nil {
|
2017-12-02 08:27:10 +03:00
|
|
|
inputErrChan <- err
|
|
|
|
return
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
br := bufio.NewReader(stdout)
|
|
|
|
clipSize := 0
|
|
|
|
packetCount := 0
|
2017-09-21 07:06:54 +03:00
|
|
|
now := time.Now()
|
|
|
|
prevTime := now
|
2017-09-20 14:57:01 +03:00
|
|
|
fmt.Printf("Looping\n")
|
2017-12-02 08:27:10 +03:00
|
|
|
for {
|
|
|
|
if clip, err := ringBuffer.Get(); err != nil {
|
|
|
|
inputErrChan <- err
|
|
|
|
return
|
2017-12-04 07:01:54 +03:00
|
|
|
} else {
|
|
|
|
for {
|
|
|
|
upperBound := clipSize + mp2tPacketSize
|
|
|
|
_, err := io.ReadFull(br, clip[clipSize:upperBound])
|
|
|
|
if err != nil {
|
2017-12-02 08:27:10 +03:00
|
|
|
inputErrChan <- err
|
|
|
|
return
|
|
|
|
}
|
2017-12-04 07:01:54 +03:00
|
|
|
if *flags&filterFixContinuity != 0 && mp2tFixContinuity(clip[clipSize:upperBound], packetCount, uint16(*selectedPID)) {
|
|
|
|
fmt.Printf("Packet #%d.%d fixed\n", clipCount, packetCount)
|
|
|
|
}
|
|
|
|
packetCount++
|
|
|
|
clipSize += mp2tPacketSize
|
|
|
|
// send if (1) our buffer is full or (2) 1 second has elapsed and we have % packetsPerFrame
|
|
|
|
now = time.Now()
|
|
|
|
if (packetCount == mp2tMaxPackets) ||
|
|
|
|
(now.Sub(prevTime) > clipDuration*time.Second && packetCount%packetsPerFrame == 0) {
|
|
|
|
clipCount++
|
|
|
|
if err := ringBuffer.DoneWriting(clipSize); err != nil {
|
|
|
|
inputErrChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clipSize = 0
|
|
|
|
packetCount = 0
|
|
|
|
prevTime = now
|
|
|
|
break
|
|
|
|
}
|
2017-12-02 08:27:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 08:00:26 +03:00
|
|
|
|
2017-12-02 08:27:10 +03:00
|
|
|
// output handles the writing to specified output
|
|
|
|
func output(output string) {
|
2017-12-04 09:37:39 +03:00
|
|
|
bitrateCalc := BitrateCalculator{60}
|
2017-09-13 08:00:26 +03:00
|
|
|
for {
|
2017-12-04 07:01:54 +03:00
|
|
|
if clip, err := ringBuffer.Read(); err == nil {
|
2017-12-04 09:37:39 +03:00
|
|
|
bitrateCalc.Start()
|
2017-12-04 07:01:54 +03:00
|
|
|
err := sendClip(clip, output, conn)
|
2017-12-02 08:27:10 +03:00
|
|
|
for err != nil {
|
|
|
|
outputErrChan <- err
|
2017-12-04 07:01:54 +03:00
|
|
|
err = sendClip(clip, output, conn)
|
2017-11-22 08:36:51 +03:00
|
|
|
}
|
2017-12-04 09:37:39 +03:00
|
|
|
noOfKB = float64(len(clip)*8) / 1024.0 // Convert bytes to kilobits
|
|
|
|
bitrateCalc.End(, true)
|
2017-12-02 08:27:10 +03:00
|
|
|
if err := ringBuffer.DoneReading(); err != nil {
|
|
|
|
outputErrChan <- err
|
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendClipToFile writes a video clip to a /tmp file.
|
|
|
|
func sendClipToFile(clip []byte, _ string, _ net.Conn) error {
|
2017-11-23 01:05:36 +03:00
|
|
|
filename := fmt.Sprintf(tempDir+"vid%03d.ts", clipCount)
|
2017-09-20 14:57:01 +03:00
|
|
|
fmt.Printf("Writing %s (%d bytes)\n", filename, len(clip))
|
|
|
|
err := ioutil.WriteFile(filename, clip, 0644)
|
|
|
|
if err != nil {
|
2017-09-21 07:06:54 +03:00
|
|
|
return fmt.Errorf("Error writing file %s: %s", filename, err)
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendClipToHTPP posts a video clip via HTTP, using a new TCP connection each time.
|
|
|
|
func sendClipToHTTP(clip []byte, output string, _ net.Conn) error {
|
2017-12-02 08:27:10 +03:00
|
|
|
timeout := time.Duration(httpTimeOut * time.Second)
|
|
|
|
client := http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
hash := md5.Sum(clip)
|
|
|
|
url := output + strconv.Itoa(len(clip)) + "." + hex.EncodeToString(hash[:]) // NB: append size.digest to output
|
2017-09-21 07:06:54 +03:00
|
|
|
fmt.Printf("Posting %s (%d bytes)\n", url, len(clip))
|
2017-12-02 08:27:10 +03:00
|
|
|
resp, err := client.Post(url, "video/mp2t", bytes.NewReader(clip)) // lighter than NewBuffer
|
2017-09-20 14:57:01 +03:00
|
|
|
if err != nil {
|
2017-09-21 07:06:54 +03:00
|
|
|
return fmt.Errorf("Error posting to %s: %s", output, err)
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err == nil {
|
2017-09-21 07:06:54 +03:00
|
|
|
fmt.Printf("%s\n", body)
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendClipToUDP sends a video clip over UDP.
|
|
|
|
func sendClipToUDP(clip []byte, _ string, conn net.Conn) error {
|
2017-09-21 07:06:54 +03:00
|
|
|
size := udpPackets * mp2tPacketSize
|
2017-09-20 14:57:01 +03:00
|
|
|
fmt.Printf("Sending %d UDP packets of size %d (%d bytes)\n", len(clip)/size, size, len(clip))
|
|
|
|
for offset := 0; offset < len(clip); offset += size {
|
2017-09-21 01:20:22 +03:00
|
|
|
pkt := clip[offset : offset+size]
|
2017-09-20 14:57:01 +03:00
|
|
|
_, err := conn.Write(pkt)
|
|
|
|
if err != nil {
|
2017-09-21 07:06:54 +03:00
|
|
|
return fmt.Errorf("UDP write error %s. Is your player listening?", err)
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-13 08:00:26 +03:00
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
// sendClipToRTP sends a video clip over RTP.
|
|
|
|
func sendClipToRTP(clip []byte, _ string, conn net.Conn) error {
|
2017-09-21 07:06:54 +03:00
|
|
|
size := rtpPackets * mp2tPacketSize
|
2017-09-20 14:57:01 +03:00
|
|
|
fmt.Printf("Sending %d RTP packets of size %d (%d bytes)\n",
|
2017-09-21 07:06:54 +03:00
|
|
|
len(clip)/size, size+rtpHeaderSize, len(clip))
|
|
|
|
pkt := make([]byte, rtpHeaderSize+rtpPackets*mp2tPacketSize)
|
2017-09-20 14:57:01 +03:00
|
|
|
for offset := 0; offset < len(clip); offset += size {
|
2017-12-03 02:31:56 +03:00
|
|
|
rtpEncapsulate(clip[offset:offset+size], pkt)
|
2017-09-20 14:57:01 +03:00
|
|
|
_, err := conn.Write(pkt)
|
2017-09-13 08:00:26 +03:00
|
|
|
if err != nil {
|
2017-09-21 07:06:54 +03:00
|
|
|
return fmt.Errorf("RTP write error %s. Is your player listening?", err)
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-02 19:04:14 +03:00
|
|
|
// checkContinuityCounts checks that the continuity of the clip is correct
|
2017-12-02 08:27:10 +03:00
|
|
|
func checkContinuityCounts(clip []byte) error {
|
|
|
|
for offset := 0; offset < len(clip); offset += mp2tPacketSize {
|
|
|
|
dumpCC = -1
|
|
|
|
pkt := clip[offset : offset+mp2tPacketSize]
|
|
|
|
cc := int(pkt[3] & 0xf)
|
|
|
|
if dumpCC != -1 && cc != dumpCC {
|
|
|
|
return fmt.Errorf("Continuity count out of order. Expected %v, Got: %v.", dumpCC, cc)
|
|
|
|
}
|
|
|
|
dumpCC = (cc + 1) % 16
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
// sendClipToStdout dumps video stats to stdout.
|
|
|
|
func sendClipToStdout(clip []byte, _ string, _ net.Conn) error {
|
|
|
|
fmt.Printf("Dumping clip (%d bytes)\n", len(clip))
|
|
|
|
|
|
|
|
if *flags&dumpProgramInfo != 0 {
|
|
|
|
return mp2tDumpProgram(clip)
|
|
|
|
}
|
|
|
|
|
|
|
|
packetCount := 0
|
|
|
|
discontinuities := 0
|
|
|
|
var cc int
|
|
|
|
|
|
|
|
for offset := 0; offset < len(clip); offset += mp2tPacketSize {
|
|
|
|
packetCount++
|
2017-09-21 01:20:22 +03:00
|
|
|
pkt := clip[offset : offset+mp2tPacketSize]
|
2017-09-20 14:57:01 +03:00
|
|
|
|
2017-09-21 07:06:54 +03:00
|
|
|
pktPID, err := packet.Pid(pkt)
|
2017-09-20 14:57:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-21 07:06:54 +03:00
|
|
|
if pktPID != uint16(*selectedPID) {
|
2017-09-13 08:00:26 +03:00
|
|
|
continue
|
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
|
|
|
|
if *flags&(dumpPacketHeader|dumpPacketPayload) != 0 {
|
|
|
|
fmt.Printf("Packet #%d.%d\n", clipCount, packetCount)
|
|
|
|
}
|
2017-09-21 01:20:22 +03:00
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
hasPayload := pkt[3]&0x10 != 0
|
2017-09-13 08:00:26 +03:00
|
|
|
if !hasPayload {
|
|
|
|
continue // nothing to do
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
// extract interesting info from header
|
|
|
|
tei := pkt[1] & 0x80 >> 7
|
|
|
|
pusi := pkt[1] & 0x40 >> 6
|
|
|
|
tp := pkt[1] & 0x20 >> 5
|
|
|
|
tcs := pkt[3] & 0xc0 >> 6
|
|
|
|
afc := pkt[3] & 0x30 >> 4
|
|
|
|
cc = int(pkt[3] & 0xf)
|
|
|
|
|
|
|
|
if dumpCC != -1 && cc != dumpCC {
|
|
|
|
discontinuities++
|
|
|
|
fmt.Printf("Warning: Packet #%d.%d continuity counter out of order! Got %d, expected %d.\n",
|
|
|
|
clipCount, packetCount, cc, dumpCC)
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
dumpCC = (cc + 1) % 16
|
2017-09-13 08:00:26 +03:00
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
if *flags&dumpPacketHeader != 0 {
|
2017-09-13 08:00:26 +03:00
|
|
|
fmt.Printf("\t\tTEI=%d, PUSI=%d, TP=%d, TSC=%d, AFC=%d, CC=%d\n", tei, pusi, tp, tcs, afc, cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if afc == 3 {
|
|
|
|
// adaptation field, followed by payload
|
|
|
|
afl := adaptationfield.Length(pkt)
|
|
|
|
if adaptationfield.HasPCR(pkt) {
|
2017-09-21 07:06:54 +03:00
|
|
|
pcrBase, pcrExt, _ := mp2tGetPCR(pkt)
|
2017-09-20 14:57:01 +03:00
|
|
|
if *flags&dumpPacketHeader != 0 {
|
2017-09-13 08:00:26 +03:00
|
|
|
fmt.Printf("\t\tAFL=%d, PCRbase=%d, PCRext=%d\n", afl, pcrBase, pcrExt)
|
|
|
|
}
|
2017-09-21 07:06:54 +03:00
|
|
|
if pcrBase < dumpPCRBase {
|
2017-09-13 08:00:26 +03:00
|
|
|
fmt.Printf("Warning: PCRbase went backwards!\n")
|
|
|
|
}
|
2017-09-21 07:06:54 +03:00
|
|
|
dumpPCRBase = pcrBase
|
2017-09-20 14:57:01 +03:00
|
|
|
} else if *flags&dumpPacketHeader != 0 {
|
2017-09-13 08:00:26 +03:00
|
|
|
fmt.Printf("\t\tAFL=%d\n", afl)
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
if *flags&dumpPacketPayload != 0 {
|
2017-09-13 08:00:26 +03:00
|
|
|
fmt.Printf("\t\tPayload=%x\n", pkt)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
if *flags&dumpPacketStats != 0 {
|
2017-09-13 08:00:26 +03:00
|
|
|
fmt.Printf("%d packets of size %d bytes (%d bytes, %d discontinuites)\n",
|
|
|
|
packetCount, packet.PacketSize, packetCount*packet.PacketSize, discontinuities)
|
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
return nil
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
|
|
|
|
2017-09-20 14:57:01 +03:00
|
|
|
// mp2tDumpProgram dumps MPEG-TS Program Association Table (PAT) and Program Map Tables (PMT).
|
|
|
|
func mp2tDumpProgram(clip []byte) error {
|
2017-09-21 01:20:22 +03:00
|
|
|
// NB: Comcast API requires a buffered reader
|
2017-09-20 14:57:01 +03:00
|
|
|
reader := bufio.NewReader(bytes.NewReader(clip))
|
|
|
|
|
|
|
|
_, err := packet.Sync(reader)
|
|
|
|
if err != nil {
|
2017-09-21 07:06:54 +03:00
|
|
|
return fmt.Errorf("Error reading sync byte: %s", err)
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
pat, err := psi.ReadPAT(reader)
|
2017-09-13 08:00:26 +03:00
|
|
|
if err != nil {
|
2017-09-21 07:06:54 +03:00
|
|
|
return fmt.Errorf("Error reading PAT: %s", err)
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
mp2tDumpPat(pat)
|
|
|
|
|
|
|
|
var pmts []psi.PMT
|
|
|
|
pm := pat.ProgramMap()
|
|
|
|
for pn, pid := range pm {
|
|
|
|
pmt, err := psi.ReadPMT(reader, pid)
|
|
|
|
if err != nil {
|
2017-09-21 07:06:54 +03:00
|
|
|
return fmt.Errorf("Error reading PMT: %s", err)
|
2017-09-20 14:57:01 +03:00
|
|
|
}
|
|
|
|
pmts = append(pmts, pmt)
|
|
|
|
mp2tDumpPmt(pn, pmt)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mp2tDumpPat(pat psi.PAT) {
|
|
|
|
fmt.Printf("Pat\n")
|
|
|
|
fmt.Printf("\tPMT PIDs %v\n", pat.ProgramMap())
|
|
|
|
fmt.Printf("\tNumber of Programs %v\n", pat.NumPrograms())
|
|
|
|
}
|
|
|
|
|
|
|
|
func mp2tDumpPmt(pn uint16, pmt psi.PMT) {
|
|
|
|
// pn = program number
|
|
|
|
fmt.Printf("Program #%v PMT\n", pn)
|
|
|
|
fmt.Printf("\tPIDs %v\n", pmt.Pids())
|
|
|
|
fmt.Printf("\tElementary Streams")
|
|
|
|
for _, es := range pmt.ElementaryStreams() {
|
2017-09-21 07:06:54 +03:00
|
|
|
fmt.Printf("\t\tPID %v : StreamType %v\n", es.ElementaryPid(), es.StreamType())
|
2017-09-20 14:57:01 +03:00
|
|
|
for _, d := range es.Descriptors() {
|
|
|
|
fmt.Printf("\t\t\t%+v\n", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mp2tFixContinuity fixes discontinous MPEG-TS continuity counts (CC)
|
|
|
|
func mp2tFixContinuity(pkt []byte, packetCount int, pid uint16) bool {
|
|
|
|
hasPayload, err := packet.ContainsPayload(pkt)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Warning: Packet #%d.%d bad.\n", clipCount, packetCount)
|
2017-09-13 08:00:26 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !hasPayload {
|
2017-09-20 14:57:01 +03:00
|
|
|
return false
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
2017-09-21 07:06:54 +03:00
|
|
|
if pktPID, _ := packet.Pid(pkt); pktPID != pid {
|
2017-09-20 14:57:01 +03:00
|
|
|
return false
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
|
|
|
fixed := false
|
|
|
|
// extract continuity counter from 2nd nibble of 4th byte of header
|
2017-09-20 14:57:01 +03:00
|
|
|
cc := int(pkt[3] & 0xf)
|
|
|
|
if expectCC == -1 {
|
|
|
|
expectCC = cc
|
|
|
|
} else if cc != expectCC {
|
|
|
|
pkt[3] = pkt[3]&0xf0 | byte(expectCC&0xf)
|
|
|
|
fixed = true
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
2017-09-20 14:57:01 +03:00
|
|
|
expectCC = (expectCC + 1) % 16
|
2017-09-13 08:00:26 +03:00
|
|
|
return fixed
|
|
|
|
}
|
|
|
|
|
2017-09-21 07:06:54 +03:00
|
|
|
// Mp2tGetPCR extracts the Program Clock Reference (PCR) from an MPEG-TS packet (if any)
|
|
|
|
func mp2tGetPCR(pkt []byte) (uint64, uint32, bool) {
|
2017-09-20 14:57:01 +03:00
|
|
|
if !adaptationfield.HasPCR(pkt) {
|
|
|
|
return 0, 0, false
|
|
|
|
}
|
|
|
|
pcrBytes, _ := adaptationfield.PCR(pkt) // 6 bytes
|
|
|
|
// first 33 bits are PCR base, next 6 bits are reserved, final 9 bits are PCR extension.
|
|
|
|
pcrBase := uint64(binary.BigEndian.Uint32(pcrBytes[:4]))<<1 | uint64(pcrBytes[4]&0x80>>7)
|
|
|
|
pcrExt := uint32(pcrBytes[4]&0x01)<<1 | uint32(pcrBytes[5])
|
|
|
|
return pcrBase, pcrExt, true
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
|
|
|
|
2017-12-03 02:31:56 +03:00
|
|
|
// rtpEncapsulate encapsulates MPEG-TS packets within an RTP header,
|
2017-09-21 01:20:22 +03:00
|
|
|
// setting the payload type accordingly (to 33) and incrementing the RTP sequence number.
|
2017-12-03 02:31:56 +03:00
|
|
|
func rtpEncapsulate(mp2tPacket []byte, pkt []byte) {
|
2017-09-13 08:00:26 +03:00
|
|
|
// RTP packet encapsulates the MP2T
|
|
|
|
// first 12 bytes is the header
|
|
|
|
// byte 0: version=2, padding=0, extension=0, cc=0
|
2017-09-20 14:57:01 +03:00
|
|
|
pkt[0] = 0x80 // version (2)
|
2017-09-13 08:00:26 +03:00
|
|
|
// byte 1: marker=0, pt = 33 (MP2T)
|
2017-09-20 14:57:01 +03:00
|
|
|
pkt[1] = 33
|
2017-09-13 08:00:26 +03:00
|
|
|
// bytes 2 & 3: sequence number
|
2017-09-21 07:08:57 +03:00
|
|
|
binary.BigEndian.PutUint16(pkt[2:4], rtpSequenceNum)
|
|
|
|
if rtpSequenceNum == ^uint16(0) {
|
|
|
|
rtpSequenceNum = 0
|
2017-09-13 08:00:26 +03:00
|
|
|
} else {
|
2017-09-21 07:08:57 +03:00
|
|
|
rtpSequenceNum++
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|
|
|
|
// bytes 4,5,6&7: timestamp
|
2017-09-20 14:57:01 +03:00
|
|
|
timestamp := uint32(time.Now().UnixNano() / 1e6) // ms timestamp
|
|
|
|
binary.BigEndian.PutUint32(pkt[4:8], timestamp)
|
2017-09-13 08:00:26 +03:00
|
|
|
// bytes 8,9,10&11: SSRC
|
2017-09-21 07:06:54 +03:00
|
|
|
binary.BigEndian.PutUint32(pkt[8:12], rtpSSRC)
|
2017-09-13 08:00:26 +03:00
|
|
|
|
|
|
|
// payload follows
|
2017-09-21 07:06:54 +03:00
|
|
|
copy(pkt[rtpHeaderSize:rtpHeaderSize+rtpPackets*mp2tPacketSize], mp2tPacket)
|
2017-09-13 08:00:26 +03:00
|
|
|
}
|