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).
|
|
|
|
*/
|
|
|
|
|
2018-01-08 04:12:26 +03:00
|
|
|
package tscreator
|
2017-12-13 09:52:18 +03:00
|
|
|
|
2018-01-01 12:14:59 +03:00
|
|
|
import (
|
2018-01-04 10:04:33 +03:00
|
|
|
_ "fmt"
|
2018-01-08 04:12:26 +03:00
|
|
|
_"os"
|
|
|
|
"../mpegts"
|
|
|
|
"../pes"
|
|
|
|
"../tools"
|
|
|
|
"../rtp"
|
2018-01-01 12:14:59 +03:00
|
|
|
)
|
2017-12-30 11:31:50 +03:00
|
|
|
|
2018-01-08 04:12:26 +03:00
|
|
|
type TsConverter interface {
|
2018-01-04 10:04:33 +03:00
|
|
|
Convert()
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
|
|
|
|
2018-01-08 04:12:26 +03:00
|
|
|
type tsCreator struct {
|
|
|
|
TsChan <-chan *mpegts.MpegTsPacket
|
|
|
|
tsChan chan<- *mpegts.MpegTsPacket
|
|
|
|
InputChan chan<- rtp.RtpPacket
|
|
|
|
inputChan <-chan rtp.RtpPacket
|
2018-01-04 10:04:33 +03:00
|
|
|
NalInputChan chan<- []byte
|
|
|
|
nalInputChan <-chan []byte
|
2018-01-08 04:12:26 +03:00
|
|
|
currentTsPacket *mpegts.MpegTsPacket
|
2018-01-04 10:04:33 +03:00
|
|
|
payloadByteChan chan byte
|
|
|
|
currentCC byte
|
2018-01-08 04:12:26 +03:00
|
|
|
currentPtsTime float64
|
|
|
|
currentPcrTime float64
|
|
|
|
fps uint
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
|
|
|
|
2018-01-08 04:12:26 +03:00
|
|
|
func NewTsCreator(fps uint) (c *tsCreator) {
|
|
|
|
c = new(tsCreator)
|
|
|
|
tsChan := make(chan *mpegts.MpegTsPacket, 100)
|
2018-01-04 10:04:33 +03:00
|
|
|
c.TsChan = tsChan
|
|
|
|
c.tsChan = tsChan
|
2018-01-08 04:12:26 +03:00
|
|
|
inputChan := make(chan rtp.RtpPacket, 100)
|
2018-01-04 10:04:33 +03:00
|
|
|
c.InputChan = inputChan
|
|
|
|
c.inputChan = inputChan
|
|
|
|
nalInputChan := make(chan []byte, 10000)
|
|
|
|
c.NalInputChan = nalInputChan
|
|
|
|
c.nalInputChan = nalInputChan
|
|
|
|
c.currentCC = 0
|
2018-01-08 04:12:26 +03:00
|
|
|
c.fps = fps
|
|
|
|
c.currentPcrTime = .0
|
|
|
|
c.currentPtsTime = .7
|
2018-01-04 10:04:33 +03:00
|
|
|
return
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|
|
|
|
|
2018-01-08 04:12:26 +03:00
|
|
|
func (c* tsCreator) genPts()(pts uint64){
|
|
|
|
pts = uint64(c.currentPtsTime * float64(90000))
|
|
|
|
c.currentPtsTime += 1.0/float64(c.fps)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c* tsCreator) genPcr()(pcr uint64){
|
|
|
|
pcr = uint64(c.currentPcrTime * float64(90000))
|
|
|
|
c.currentPcrTime += 1.0/float64(c.fps)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tsCreator) Convert() {
|
2018-01-04 10:04:33 +03:00
|
|
|
pesPktChan := make(chan []byte, 1000)
|
|
|
|
payloadByteChan := make(chan byte, 100000)
|
2018-01-08 04:12:26 +03:00
|
|
|
var rtpBuffer [](*rtp.RtpPacket)
|
2018-01-04 10:04:33 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case rtpPacket := <-c.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
|
|
|
|
2018-01-04 10:04:33 +03:00
|
|
|
if len(rtpBuffer) > 200 {
|
2018-01-08 04:12:26 +03:00
|
|
|
for tools.GetOctectType(rtpBuffer[0]) != 7 {
|
2018-01-04 10:04:33 +03:00
|
|
|
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]) {
|
2018-01-04 10:04:33 +03:00
|
|
|
case 28:
|
2018-01-08 04:12:26 +03:00
|
|
|
if tools.GetStartBit(rtpBuffer[0]) == 1 {
|
2018-01-04 10:04:33 +03:00
|
|
|
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 {
|
2018-01-04 10:04:33 +03:00
|
|
|
rtpBuffer = rtpBuffer[1:]
|
|
|
|
c.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:]
|
|
|
|
c.NalInputChan <- buffer
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case nalUnit := <-c.nalInputChan:
|
2018-01-08 04:12:26 +03:00
|
|
|
pesPkt := pes.PESPacket{
|
|
|
|
StreamID: 0xE0,
|
|
|
|
PDI: byte(2),
|
|
|
|
PTS: c.genPts(),
|
|
|
|
Data: nalUnit,
|
|
|
|
HeaderLength: 5,
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
|
|
|
pesPktChan <- pesPkt.ToByteSlice()
|
|
|
|
case pesPkt := <-pesPktChan:
|
|
|
|
for ii := range pesPkt {
|
|
|
|
payloadByteChan <- pesPkt[ii]
|
|
|
|
}
|
2018-01-08 04:12:26 +03:00
|
|
|
pusi := true
|
2018-01-04 10:04:33 +03:00
|
|
|
for len(payloadByteChan) > 0 {
|
2018-01-08 04:12:26 +03:00
|
|
|
pkt := mpegts.MpegTsPacket{
|
|
|
|
PUSI: pusi,
|
|
|
|
PID: 256,
|
|
|
|
RAI: pusi,
|
|
|
|
CC: c.currentCC,
|
|
|
|
AFC: byte(3),
|
|
|
|
PCRF: pusi,
|
|
|
|
}
|
|
|
|
pkt.FillPayload(payloadByteChan)
|
|
|
|
if pusi {
|
|
|
|
pkt.PCR = c.genPcr()
|
|
|
|
pusi = false
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
|
|
|
if c.currentCC++; c.currentCC > 15 {
|
|
|
|
c.currentCC = 0
|
|
|
|
}
|
2018-01-08 04:12:26 +03:00
|
|
|
c.tsChan <- &pkt
|
2018-01-04 10:04:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 09:52:18 +03:00
|
|
|
}
|