mirror of https://bitbucket.org/ausocean/av.git
531 lines
14 KiB
Go
531 lines
14 KiB
Go
/*
|
|
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
|
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
*/
|
|
|
|
// revid is a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols.
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/binary"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"math/rand"
|
|
"net"
|
|
"net/http"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Comcast/gots/packet"
|
|
"github.com/Comcast/gots/packet/adaptationfield"
|
|
"github.com/Comcast/gots/psi"
|
|
)
|
|
|
|
// program modes
|
|
const (
|
|
File = iota
|
|
HTTP
|
|
UDP
|
|
RTP
|
|
Dump
|
|
)
|
|
|
|
// 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"
|
|
)
|
|
|
|
// flags
|
|
const (
|
|
filterFixPTS = 0x0001
|
|
filterDropAudio = 0x0002
|
|
filterScale640 = 0x0004
|
|
filterScale320 = 0x0008
|
|
filterFixContinuity = 0x0010
|
|
dumpProgramInfo = 0x0100 // 256
|
|
dumpPacketStats = 0x0200 // 512
|
|
dumpPacketHeader = 0x0400 // 1024
|
|
dumpPacketPayload = 0x0800 // 2048
|
|
)
|
|
|
|
// globals
|
|
var (
|
|
sendClip = sendClipToRTP
|
|
packetsPerFrame = RTPPackets
|
|
flags *int
|
|
frameRate *int
|
|
selectedPid *int
|
|
clipCount int
|
|
expectCC int
|
|
dumpCC int
|
|
dumpPcrBase uint64
|
|
RTPSequenceNum uint16
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
input = flag.String("i", "", "Input RTSP URL")
|
|
output = flag.String("o", "", "Output URL (HTTP, UDP or RTP)")
|
|
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)")
|
|
|
|
flag.Parse()
|
|
|
|
if *input == "" {
|
|
log.Fatal("Input (-i) required\n")
|
|
}
|
|
|
|
switch *mode {
|
|
case "f":
|
|
sendClip = sendClipToFile
|
|
case "h":
|
|
sendClip = sendClipToHTTP
|
|
if *output == "" {
|
|
*output = defaultHTTPOutput
|
|
}
|
|
case "u":
|
|
sendClip = sendClipToUDP
|
|
packetsPerFrame = UDPPackets
|
|
if *output == "" {
|
|
*output = defaultUDPOutput
|
|
}
|
|
case "r":
|
|
sendClip = sendClipToRTP
|
|
packetsPerFrame = RTPPackets
|
|
if *output == "" {
|
|
*output = defaultRTPOutput
|
|
}
|
|
case "d":
|
|
sendClip = sendClipToStdout
|
|
default:
|
|
log.Fatal("Invalid mode %s\n", *mode)
|
|
}
|
|
|
|
if *flags&filterFixContinuity != 0 && *flags&dumpProgramInfo != 0 {
|
|
log.Fatal("Cannot combine filterFixContinuity and dumpProgramInfo flags\n")
|
|
}
|
|
|
|
for {
|
|
_ = readWriteVideo(*input, *output)
|
|
fmt.Printf("Trying again in 10s\n")
|
|
time.Sleep(1e10 * time.Nanosecond)
|
|
}
|
|
}
|
|
|
|
// 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 {
|
|
fmt.Printf("Reading video from %s\n", input)
|
|
|
|
var videoArg, audioArg [2]string
|
|
|
|
if *flags&(filterFixPTS|*flags&filterScale640|*flags&filterScale320) == 0 {
|
|
videoArg[0] = "-vcodec"
|
|
videoArg[1] = "copy"
|
|
} else {
|
|
videoArg[0] = "-vf"
|
|
videoArg[1] = ""
|
|
if *flags&filterFixPTS != 0 {
|
|
videoArg[1] += "setpts='PTS-STARTPTS'" // start counting PTS from zero
|
|
}
|
|
if *flags&filterScale640 != 0 {
|
|
videoArg[1] += ", scale=640:352"
|
|
} else if *flags&filterScale320 != 0 {
|
|
videoArg[1] += ", scale=320:176"
|
|
}
|
|
}
|
|
|
|
if *flags&filterDropAudio == 0 {
|
|
audioArg[0] = "-acodec"
|
|
audioArg[1] = "copy"
|
|
} else {
|
|
audioArg[0] = "-an"
|
|
audioArg[1] = ""
|
|
}
|
|
|
|
fmt.Printf("Executing: %s -r %d -i %s %s \"%s\" %s %s -f mpegts -\n",
|
|
ffmpegPath, *frameRate, input, videoArg[0], videoArg[1], audioArg[0], audioArg[1])
|
|
|
|
var cmd *exec.Cmd
|
|
if audioArg[1] == "" {
|
|
cmd = exec.Command(ffmpegPath,
|
|
"-r", strconv.Itoa(*frameRate),
|
|
"-i", input,
|
|
videoArg[0], videoArg[1],
|
|
audioArg[0],
|
|
"-f", "mpegts", "-")
|
|
|
|
} else {
|
|
cmd = exec.Command(ffmpegPath,
|
|
"-r", strconv.Itoa(*frameRate),
|
|
"-i", input,
|
|
videoArg[0], videoArg[1],
|
|
audioArg[0], audioArg[1],
|
|
"-f", "mpegts", "-")
|
|
}
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
fmt.Printf("Error creating pipe: %s\n", err)
|
|
return err
|
|
}
|
|
err = cmd.Start()
|
|
if err != nil {
|
|
fmt.Printf("Error starting pipe: %s\n", err)
|
|
return err
|
|
}
|
|
|
|
// (re)initialize globals
|
|
clipCount = 0
|
|
expectCC = -1
|
|
dumpCC = -1
|
|
dumpPcrBase = 0
|
|
RTPSequenceNum = uint16(rand.Intn(1 << 15))
|
|
|
|
// for UDP and RTP only dial once
|
|
var conn net.Conn
|
|
if strings.Index(output, "udp://") == 0 || strings.Index(output, "rtp://") == 0 {
|
|
conn, err = net.Dial("udp", output[6:])
|
|
if err != nil {
|
|
fmt.Printf("Error dialing %s: %s\n", output, err)
|
|
return err
|
|
}
|
|
defer conn.Close()
|
|
}
|
|
|
|
br := bufio.NewReader(stdout)
|
|
pkt := make([]byte, mp2tPacketSize)
|
|
clip := make([]byte, mp2tMaxPackets*mp2tPacketSize)
|
|
clipSize := 0
|
|
packetCount := 0
|
|
prevTime := time.Now()
|
|
fmt.Printf("Looping\n")
|
|
|
|
for {
|
|
_, err := io.ReadFull(br, pkt)
|
|
if err != nil {
|
|
fmt.Printf("Error reading from ffmpeg: %s\n", err)
|
|
return err
|
|
|
|
}
|
|
if *flags&filterFixContinuity != 0 && mp2tFixContinuity(pkt, packetCount, (uint16)(*selectedPid)) {
|
|
fmt.Printf("Packet #%d.%d fixed\n", clipCount, packetCount)
|
|
}
|
|
copy(clip[clipSize:], pkt)
|
|
packetCount++
|
|
clipSize += mp2tPacketSize
|
|
|
|
// send if (1) our buffer is full or (2) 1 second has elapsed and we have % packetsPerFrame
|
|
if (packetCount == mp2tMaxPackets) ||
|
|
(time.Now().Sub(prevTime) > 1*time.Second && packetCount%packetsPerFrame == 0) {
|
|
clipCount++
|
|
if err = sendClip(clip[:clipSize], output, conn); err != nil {
|
|
return err
|
|
}
|
|
clipSize = 0
|
|
packetCount = 0
|
|
prevTime = time.Now()
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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)
|
|
fmt.Printf("Writing %s (%d bytes)\n", filename, len(clip))
|
|
err := ioutil.WriteFile(filename, clip, 0644)
|
|
if err != nil {
|
|
fmt.Printf("Error writing file %s: %s\n", filename, err)
|
|
return err
|
|
}
|
|
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 {
|
|
URL := output + strconv.Itoa(len(clip)) // NB: append the size to the output
|
|
fmt.Printf("Posting %s (%d bytes)\n", URL, len(clip))
|
|
resp, err := http.Post(URL, "video/mp2t", bytes.NewReader(clip)) // lighter than NewBuffer
|
|
if err != nil {
|
|
fmt.Printf("Error posting to %s: %s\n", output, err)
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err == nil {
|
|
fmt.Printf(string(body) + "\n")
|
|
}
|
|
return err
|
|
}
|
|
|
|
// sendClipToUDP sends a video clip over UDP.
|
|
func sendClipToUDP(clip []byte, _ string, conn net.Conn) error {
|
|
size := UDPPackets * mp2tPacketSize
|
|
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 {
|
|
pkt := clip[offset : offset+size]
|
|
_, err := conn.Write(pkt)
|
|
if err != nil {
|
|
fmt.Printf("UDP write error %s. Is your player listening?\n", err)
|
|
return err
|
|
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// sendClipToRTP sends a video clip over RTP.
|
|
func sendClipToRTP(clip []byte, _ string, conn net.Conn) error {
|
|
size := RTPPackets * mp2tPacketSize
|
|
fmt.Printf("Sending %d RTP packets of size %d (%d bytes)\n",
|
|
len(clip)/size, size+RTPHeaderSize, len(clip))
|
|
pkt := make([]byte, RTPHeaderSize+RTPPackets*mp2tPacketSize)
|
|
for offset := 0; offset < len(clip); offset += size {
|
|
RTPEncapsulate(clip[offset:offset+size], pkt)
|
|
_, err := conn.Write(pkt)
|
|
if err != nil {
|
|
fmt.Printf("RTP write error %s. Is your player listening?\n", err)
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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++
|
|
pkt := clip[offset : offset+mp2tPacketSize]
|
|
|
|
pktPid, err := packet.Pid(pkt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if pktPid != (uint16)(*selectedPid) {
|
|
continue
|
|
}
|
|
|
|
if *flags&(dumpPacketHeader|dumpPacketPayload) != 0 {
|
|
fmt.Printf("Packet #%d.%d\n", clipCount, packetCount)
|
|
}
|
|
|
|
hasPayload := pkt[3]&0x10 != 0
|
|
if !hasPayload {
|
|
continue // nothing to do
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
dumpCC = (cc + 1) % 16
|
|
|
|
if *flags&dumpPacketHeader != 0 {
|
|
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) {
|
|
pcrBase, pcrExt, _ := mp2tGetPcr(pkt)
|
|
if *flags&dumpPacketHeader != 0 {
|
|
fmt.Printf("\t\tAFL=%d, PCRbase=%d, PCRext=%d\n", afl, pcrBase, pcrExt)
|
|
}
|
|
if pcrBase < dumpPcrBase {
|
|
fmt.Printf("Warning: PCRbase went backwards!\n")
|
|
}
|
|
dumpPcrBase = pcrBase
|
|
} else if *flags&dumpPacketHeader != 0 {
|
|
fmt.Printf("\t\tAFL=%d\n", afl)
|
|
}
|
|
}
|
|
if *flags&dumpPacketPayload != 0 {
|
|
fmt.Printf("\t\tPayload=%x\n", pkt)
|
|
}
|
|
|
|
}
|
|
if *flags&dumpPacketStats != 0 {
|
|
fmt.Printf("%d packets of size %d bytes (%d bytes, %d discontinuites)\n",
|
|
packetCount, packet.PacketSize, packetCount*packet.PacketSize, discontinuities)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// mp2tDumpProgram dumps MPEG-TS Program Association Table (PAT) and Program Map Tables (PMT).
|
|
func mp2tDumpProgram(clip []byte) error {
|
|
// NB: Comcast API requires a buffered reader
|
|
reader := bufio.NewReader(bytes.NewReader(clip))
|
|
|
|
_, err := packet.Sync(reader)
|
|
if err != nil {
|
|
fmt.Println("Warning: Bad MPEG-TS sync byte")
|
|
return err
|
|
}
|
|
pat, err := psi.ReadPAT(reader)
|
|
if err != nil {
|
|
fmt.Printf("ReadPAT error: %s\n", err)
|
|
return err
|
|
}
|
|
mp2tDumpPat(pat)
|
|
|
|
var pmts []psi.PMT
|
|
pm := pat.ProgramMap()
|
|
for pn, pid := range pm {
|
|
pmt, err := psi.ReadPMT(reader, pid)
|
|
if err != nil {
|
|
fmt.Printf("ReadPMT error: %s\n", err)
|
|
return err
|
|
}
|
|
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() {
|
|
fmt.Printf("\t\tPid %v : StreamType %v\n", es.ElementaryPid(), es.StreamType())
|
|
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)
|
|
return false
|
|
}
|
|
if !hasPayload {
|
|
return false
|
|
}
|
|
if pktPid, _ := packet.Pid(pkt); pktPid != pid {
|
|
return false
|
|
}
|
|
fixed := false
|
|
// extract continuity counter from 2nd nibble of 4th byte of header
|
|
cc := int(pkt[3] & 0xf)
|
|
if expectCC == -1 {
|
|
expectCC = cc
|
|
} else if cc != expectCC {
|
|
pkt[3] = pkt[3]&0xf0 | byte(expectCC&0xf)
|
|
fixed = true
|
|
}
|
|
expectCC = (expectCC + 1) % 16
|
|
return fixed
|
|
}
|
|
|
|
// Mp2tGetPcr extracts the Program Clock Reference (PCR) from an MPEG-TS packet (if any)
|
|
func mp2tGetPcr(pkt []byte) (uint64, uint32, bool) {
|
|
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
|
|
}
|
|
|
|
// RTPEncapsulate encapsulates MPEG-TS packets within an RTP header,
|
|
// setting the payload type accordingly (to 33) and incrementing the RTP sequence number.
|
|
func RTPEncapsulate(mp2tPacket []byte, pkt []byte) {
|
|
// RTP packet encapsulates the MP2T
|
|
// first 12 bytes is the header
|
|
// byte 0: version=2, padding=0, extension=0, cc=0
|
|
pkt[0] = 0x80 // version (2)
|
|
// byte 1: marker=0, pt = 33 (MP2T)
|
|
pkt[1] = 33
|
|
// bytes 2 & 3: sequence number
|
|
binary.BigEndian.PutUint16(pkt[2:4], RTPSequenceNum)
|
|
if RTPSequenceNum == ^uint16(0) {
|
|
RTPSequenceNum = 0
|
|
} else {
|
|
RTPSequenceNum++
|
|
}
|
|
// bytes 4,5,6&7: timestamp
|
|
timestamp := uint32(time.Now().UnixNano() / 1e6) // ms timestamp
|
|
binary.BigEndian.PutUint32(pkt[4:8], timestamp)
|
|
// bytes 8,9,10&11: SSRC
|
|
binary.BigEndian.PutUint32(pkt[8:12], RTPSSRC)
|
|
|
|
// payload follows
|
|
copy(pkt[RTPHeaderSize:RTPHeaderSize+RTPPackets*mp2tPacketSize], mp2tPacket)
|
|
}
|