av/packets/RtpToTsConverter.go

241 lines
7.1 KiB
Go

/*
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 packets
import (
_"fmt"
"os"
)
type RtpToTsConverter interface {
Convert()
}
type rtpToTsConverter struct {
TsChan <-chan *MpegTsPacket
tsChan chan<- *MpegTsPacket
InputChan chan<- RtpPacket
inputChan <-chan RtpPacket
currentTsPacket *MpegTsPacket
payloadByteChan chan byte
currentCC byte
}
func NewRtpToTsConverter() (c *rtpToTsConverter) {
c = new(rtpToTsConverter)
tsChan := make(chan *MpegTsPacket,100)
c.TsChan = tsChan
c.tsChan = tsChan
inputChan := make(chan RtpPacket,100)
c.InputChan = inputChan
c.inputChan = inputChan
c.currentCC = 0
return
}
func getOctectType(p *RtpPacket) byte {
return p.Payload[0] & 0x1F
}
func getStartBit(p *RtpPacket) byte {
return (p.Payload[1] & 0x80)>>7
}
func getEndBit(p *RtpPacket) byte {
return (p.Payload[1] & 0x40)>>6
}
func (c* rtpToTsConverter) Convert() {
file,_ := os.Create("video")
pesPktChan := make(chan []byte, 10)
pesDataChan := make(chan byte, 50000)
nalAccessChan := make(chan []byte, 10)
payloadByteChan := make(chan byte, 100000)
// grab sps pps and sei
var sps []byte
var pps []byte
var sei []byte
// Frist find sps
for {
rtpPacket := <-c.inputChan
fragmentType := rtpPacket.Payload[0] & 0x1F
if fragmentType == 7 {
sps = make([]byte,len(rtpPacket.Payload))
for i := range rtpPacket.Payload {
sps[i] = rtpPacket.Payload[i]
}
break
}
}
// now find pps
for {
rtpPacket := <-c.inputChan
fragmentType := rtpPacket.Payload[0] & 0x1F
if fragmentType == 8 {
pps = make([]byte,len(rtpPacket.Payload))
for i := range rtpPacket.Payload {
pps[i] = rtpPacket.Payload[i]
}
break
}
}
// now find sei
for {
rtpPacket := <-c.inputChan
fragmentType := rtpPacket.Payload[0] & 0x1F
if fragmentType == 6 {
sei = make([]byte,len(rtpPacket.Payload))
for i := range rtpPacket.Payload {
sei[i] = rtpPacket.Payload[i]
}
break
}
}
for {
select {
default:
case rtpPacket := <-c.inputChan:
if getOctectType(&rtpPacket) == 28 {
if getStartBit(&rtpPacket) == 1 {
var buffer []byte
// au delimiter
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, rtpPacket.Payload[0] & 0xE0 | rtpPacket.Payload[1] & 0x1F )
buffer = append(buffer, rtpPacket.Payload[2:]...)
for {
rtpPacket := <-c.inputChan
if getOctectType(&rtpPacket) != 28 || getStartBit(&rtpPacket) == 1 {
nalAccessChan<-buffer
file.Write(buffer)
break
}
buffer = append(buffer, rtpPacket.Payload[2:]...)
if getEndBit(&rtpPacket) == 1 {
nalAccessChan<-buffer
file.Write(buffer)
break
}
}
}
} else {
if getOctectType(&rtpPacket) == 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, rtpPacket.Payload...)
nalAccessChan<-buffer
file.Write(buffer)
}
}
case nalUnit := <-nalAccessChan:
for ii := range nalUnit {
pesDataChan<-nalUnit[ii]
}
pesDataChanLen := len(nalUnit)
pesPkt := new(PESPacket)
pesPkt.StreamID = 0xE0
pesPkt.Length = uint16( 3 + pesDataChanLen )
pesPkt.ScramblingControl = 0
pesPkt.Priority = true
pesPkt.DAI = false
pesPkt.Copyright = false
pesPkt.Original = true
pesPkt.PDI = 0
pesPkt.ESCR = false
pesPkt.ESRate = false
pesPkt.DSMTrickMode = false
pesPkt.ACI = false
pesPkt.CRC = false
pesPkt.Ext = false
pesPkt.HeaderLength = 0
pesPkt.Data = make([]byte,pesDataChanLen)
for ii:=0; ii<pesDataChanLen; ii++ {
pesPkt.Data[ii] = <-pesDataChan
}
pesPktChan<-pesPkt.ToByteSlice()
case pesPkt := <-pesPktChan:
for ii:=range pesPkt {
payloadByteChan<-pesPkt[ii]
}
firstPacket:=true
for len(payloadByteChan) > 0 {
lengthOfByteChan := len(payloadByteChan)
c.currentTsPacket = new(MpegTsPacket)
c.currentTsPacket.SyncByte = 0x47
c.currentTsPacket.TEI = false
c.currentTsPacket.PUSI = false
if firstPacket { // if it's the start of the payload
c.currentTsPacket.PUSI = true
firstPacket = false
}
c.currentTsPacket.Priority = false
c.currentTsPacket.PID = 256
c.currentTsPacket.TSC = 0
c.currentTsPacket.CC = c.currentCC
if c.currentCC++; c.currentCC > 15 { c.currentCC = 0 }
payloadLength := 182
if lengthOfByteChan < 182 {
payloadLength = lengthOfByteChan
}
c.currentTsPacket.AFC = 3
stuffingLength := 182-payloadLength
c.currentTsPacket.AF = make([]byte,2 + stuffingLength) // adaptationfield flag length = 16
c.currentTsPacket.AF[0] = byte(1 + stuffingLength)
c.currentTsPacket.AF[1] = 0
if c.currentTsPacket.PUSI {
c.currentTsPacket.AF[1] = 0x00
}
for ii := 0; ii < stuffingLength; ii++ {
c.currentTsPacket.AF[2+ii] = 0xFF
}
c.currentTsPacket.Payload = make([]byte, payloadLength)
for ii:=0; ii < payloadLength; ii++ {
c.currentTsPacket.Payload[ii] = <-payloadByteChan
}
c.tsChan<-c.currentTsPacket
}
}
}
}