av/tsgenerator/TsGenerator.go

270 lines
8.8 KiB
Go
Raw Normal View History

2017-12-13 09:52:18 +03:00
/*
NAME
RtpToTsConverter.go - provides utilities for the conversion of Rtp packets
to equivalent MpegTs packets.
DESCRIPTION
See Readme.md
AUTHOR
Saxon Nelson-Milton <saxon.milton@gmail.com>
LICENSE
RtpToTsConverter.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
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).
*/
package tsgenerator
2017-12-13 09:52:18 +03:00
2018-01-01 12:14:59 +03:00
import (
2018-01-17 06:48:47 +03:00
_"fmt"
2018-01-08 04:12:26 +03:00
_"os"
"bitbucket.org/ausocean/av/mpegts"
"bitbucket.org/ausocean/av/pes"
"bitbucket.org/ausocean/av/tools"
"bitbucket.org/ausocean/av/rtp"
//"../mpegts"
//"../pes"
//"../tools"
//"../rtp"
2018-01-01 12:14:59 +03:00
)
2018-01-16 08:49:18 +03:00
var (
2018-01-16 10:17:38 +03:00
PatTable = []byte{0, 0, 176, 13, 0, 1, 193, 0, 0, 0, 1, 240, 0, 42, 177, 4, 178, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,}
2018-01-16 08:49:18 +03:00
2018-01-16 10:17:38 +03:00
PmtTable = []byte{0, 2, 176, 18, 0, 1, 193, 0, 0, 0xE1, 0x00, 0xF0, 0, 0x1B, 0xE1, 0, 0xF0, 0, 0x15, 0xBD, 0x4D, 0x56, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,}
2018-01-16 08:49:18 +03:00
)
const (
SdtPid = 17
PatPid = 0
PmtPid = 4096
VideoPid = 256
)
2018-01-10 04:32:16 +03:00
type TsGenerator interface {
2018-01-24 07:12:22 +03:00
generate()
GetNalInputChan() chan []byte
GetTsOutputChan() <-chan *mpegts.MpegTsPacket
2018-01-24 07:22:52 +03:00
Start()
genPts()(pts uint64)
genPcr()(pts uint64)
2017-12-13 09:52:18 +03:00
}
2018-01-10 04:32:16 +03:00
type tsGenerator struct {
2018-01-08 04:12:26 +03:00
TsChan <-chan *mpegts.MpegTsPacket
tsChan chan<- *mpegts.MpegTsPacket
InputChan chan<- rtp.RtpPacket
inputChan <-chan rtp.RtpPacket
nalInputChan chan []byte
2018-01-08 04:12:26 +03:00
currentTsPacket *mpegts.MpegTsPacket
payloadByteChan chan byte
currentCC byte
2018-01-08 04:12:26 +03:00
currentPtsTime float64
currentPcrTime float64
fps uint
2018-01-16 08:49:18 +03:00
pesPktChan chan []byte
ccMap map[int]int
}
func (g *tsGenerator)GetNalInputChan() chan []byte {
return g.nalInputChan
}
func (g *tsGenerator)GetTsOutputChan() <-chan *mpegts.MpegTsPacket {
2018-01-16 08:49:18 +03:00
return g.TsChan
2017-12-13 09:52:18 +03:00
}
2018-01-10 06:57:56 +03:00
func NewTsGenerator(fps uint) (g *tsGenerator) {
g = new(tsGenerator)
2018-01-08 04:12:26 +03:00
tsChan := make(chan *mpegts.MpegTsPacket, 100)
2018-01-10 06:57:56 +03:00
g.TsChan = tsChan
g.tsChan = tsChan
2018-01-08 04:12:26 +03:00
inputChan := make(chan rtp.RtpPacket, 100)
2018-01-10 06:57:56 +03:00
g.InputChan = inputChan
g.inputChan = inputChan
g.nalInputChan = make(chan []byte, 10000)
2018-01-10 06:57:56 +03:00
g.currentCC = 0
g.fps = fps
g.currentPcrTime = .0
g.currentPtsTime = .7
g.pesPktChan = make(chan []byte, 1000)
g.payloadByteChan = make(chan byte, 100000)
2018-01-16 10:17:38 +03:00
g.ccMap = make(map[int]int, 4)
2018-01-16 08:49:18 +03:00
g.ccMap[SdtPid] = 0
g.ccMap[PatPid] = 0
g.ccMap[PmtPid] = 0
g.ccMap[VideoPid] = 0
return
2017-12-13 09:52:18 +03:00
}
2018-01-10 06:57:56 +03:00
func (g *tsGenerator) genPts()(pts uint64){
pts = uint64(g.currentPtsTime * float64(90000))
g.currentPtsTime += 1.0/float64(g.fps)
2018-01-08 04:12:26 +03:00
return
}
2018-01-10 06:57:56 +03:00
func (g *tsGenerator) genPcr()(pcr uint64){
pcr = uint64(g.currentPcrTime * float64(90000))
g.currentPcrTime += 1.0/float64(g.fps)
2018-01-08 04:12:26 +03:00
return
}
2018-01-24 07:12:22 +03:00
func (g *tsGenerator) Start(){
go g.generate()
}
func (g *tsGenerator) generate() {
2018-01-08 04:12:26 +03:00
var rtpBuffer [](*rtp.RtpPacket)
2018-01-17 06:48:47 +03:00
for {
select {
2018-01-10 06:57:56 +03:00
case rtpPacket := <-g.inputChan:
rtpBuffer = append(rtpBuffer, &rtpPacket)
if len(rtpBuffer) > 2 {
// if there's something weird going on with sequence numbers then
// insertion sort
if rtpPacket.SequenceNumber < rtpBuffer[len(rtpBuffer)-2].SequenceNumber {
for i := 1; i < len(rtpBuffer); i++ {
for j := i; j > 0 && rtpBuffer[j].SequenceNumber < rtpBuffer[j-1].SequenceNumber; j-- {
temp := rtpBuffer[j]
rtpBuffer[j] = rtpBuffer[j-1]
rtpBuffer[j-1] = temp
}
}
}
}
2018-01-08 04:12:26 +03:00
if len(rtpBuffer) > 200 {
2018-01-08 04:12:26 +03:00
for tools.GetOctectType(rtpBuffer[0]) != 7 {
rtpBuffer = rtpBuffer[1:]
}
sps := make([]byte, len(rtpBuffer[0].Payload))
copy(sps[:], rtpBuffer[0].Payload[:])
rtpBuffer = rtpBuffer[1:]
pps := make([]byte, len(rtpBuffer[0].Payload))
copy(pps[:], rtpBuffer[0].Payload[:])
rtpBuffer = rtpBuffer[1:]
sei := make([]byte, len(rtpBuffer[0].Payload))
copy(sei[:], rtpBuffer[0].Payload[:])
rtpBuffer = rtpBuffer[1:]
2018-01-08 04:12:26 +03:00
for tools.GetOctectType(rtpBuffer[0]) != 7 {
switch tools.GetOctectType(rtpBuffer[0]) {
case 28:
2018-01-08 04:12:26 +03:00
if tools.GetStartBit(rtpBuffer[0]) == 1 {
var buffer []byte
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, []byte{0x09, 0x10}...)
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, sps...)
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, pps...)
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, sei...)
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, rtpBuffer[0].Payload[0]&0xE0|rtpBuffer[0].Payload[1]&0x1F)
buffer = append(buffer, rtpBuffer[0].Payload[2:]...)
rtpBuffer = rtpBuffer[1:]
for {
buffer = append(buffer, rtpBuffer[0].Payload[2:]...)
2018-01-08 04:12:26 +03:00
if tools.GetEndBit(rtpBuffer[0]) == 1 {
rtpBuffer = rtpBuffer[1:]
g.nalInputChan <- buffer
break
}
rtpBuffer = rtpBuffer[1:]
}
}
case 1:
var buffer []byte
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, []byte{0x09, 0x10}...)
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, sps...)
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, pps...)
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, sei...)
buffer = append(buffer, []byte{0x00, 0x00, 0x01}...)
buffer = append(buffer, rtpBuffer[0].Payload[0]&0xE0|rtpBuffer[0].Payload[1]&0x1F)
buffer = append(buffer, rtpBuffer[0].Payload[2:]...)
rtpBuffer = rtpBuffer[1:]
g.nalInputChan <- buffer
default:
}
}
}
2018-01-10 06:57:56 +03:00
case nalUnit := <-g.nalInputChan:
2018-01-08 04:12:26 +03:00
pesPkt := pes.PESPacket{
StreamID: 0xE0,
PDI: byte(2),
2018-01-10 06:57:56 +03:00
PTS: g.genPts(),
2018-01-08 04:12:26 +03:00
Data: nalUnit,
HeaderLength: 5,
}
g.pesPktChan <- pesPkt.ToByteSlice()
case pesPkt := <-g.pesPktChan:
for ii := range pesPkt {
g.payloadByteChan <- pesPkt[ii]
}
2018-01-08 04:12:26 +03:00
pusi := true
for len(g.payloadByteChan) > 0 {
2018-01-08 04:12:26 +03:00
pkt := mpegts.MpegTsPacket{
PUSI: pusi,
2018-01-16 08:49:18 +03:00
PID: VideoPid,
2018-01-08 04:12:26 +03:00
RAI: pusi,
2018-01-16 10:17:38 +03:00
CC: byte(g.getCC(VideoPid)),
2018-01-08 04:12:26 +03:00
AFC: byte(3),
PCRF: pusi,
}
pkt.FillPayload(g.payloadByteChan)
2018-01-24 07:12:22 +03:00
2018-01-08 04:12:26 +03:00
if pusi {
2018-01-16 08:49:18 +03:00
// Create pat table and send off
patPkt := mpegts.MpegTsPacket{
PUSI: pusi,
PID: PatPid,
2018-01-16 10:17:38 +03:00
CC: byte(g.getCC(PatPid)),
2018-01-16 08:49:18 +03:00
AFC: 1,
2018-01-16 10:17:38 +03:00
Payload: PatTable,
2018-01-16 08:49:18 +03:00
}
g.tsChan <- &patPkt
// Create pmt table and send off
pmtPkt := mpegts.MpegTsPacket{
PUSI: pusi,
PID: PmtPid,
2018-01-16 10:17:38 +03:00
CC: byte(g.getCC(PmtPid)),
2018-01-16 08:49:18 +03:00
AFC: 1,
2018-01-16 10:17:38 +03:00
Payload: PmtTable,
2018-01-16 08:49:18 +03:00
}
g.tsChan <- &pmtPkt
2018-01-10 06:57:56 +03:00
pkt.PCR = g.genPcr()
2018-01-08 04:12:26 +03:00
pusi = false
}
2018-01-16 08:49:18 +03:00
2018-01-10 06:57:56 +03:00
g.tsChan <- &pkt
}
}
}
2017-12-13 09:52:18 +03:00
}
2018-01-16 08:49:18 +03:00
2018-01-16 10:17:38 +03:00
func (g *tsGenerator) getCC(pid int) int {
temp := g.ccMap[pid]
2018-01-16 08:49:18 +03:00
if g.ccMap[pid]++; g.ccMap[pid] > 15 {
g.ccMap[pid] = 0
}
2018-01-16 10:17:38 +03:00
return temp
2018-01-16 08:49:18 +03:00
}