av/packet/RtpToTsConverter.go

94 lines
2.6 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 packet
import "fmt"
type RtpToTsConverter interface {
Convert()
}
type rtpToTsConverter struct {
TsChan <-chan *MpegTsPacket
RtpChan chan<- RtpPacket
tsChan chan<- *MpegTsPacket
rtpChan <-chan RtpPacket
currentTsPacket *MpegTsPacket
currentCC byte
payloadByteChan chan byte
}
func NewRtpToTsConverter() (c *rtpToTsConverter) {
c = new(rtpToTsConverter)
tsChan := make(chan *MpegTsPacket)
rtpChan := make(chan RtpPacket)
c.TsChan = tsChan
c.RtpChan = rtpChan
c.tsChan = tsChan
c.rtpChan = rtpChan
c.currentTsPacket = new(MpegTsPacket)
c.currentCC = 0
c.payloadByteChan = make(chan byte, 100)
return
}
func (c* rtpToTsConverter) Convert() {
for {
select{
default:
case rtpPacket := <-c.rtpChan:
fmt.Println("here4")
for ii := range rtpPacket.Payload {
c.payloadByteChan<-rtpPacket.Payload[ii]
}
// If we have 156 bytes of payload then we can fill up a mpegts packet
fmt.Println("here5")
if len(c.payloadByteChan) > 156 {
fmt.Println("here6")
c.currentTsPacket = new(MpegTsPacket)
c.currentTsPacket.SyncByte = 0x47
c.currentTsPacket.TEI = false
c.currentTsPacket.PUSI = true
c.currentTsPacket.Priority = false
c.currentTsPacket.PID = 4096
c.currentTsPacket.TSC = 0
c.currentTsPacket.AFC = 1 // no adaptation field - payload only
c.currentTsPacket.Payload = make([]byte, 156)
fmt.Println("here7")
for ii:=0; ii < 156; ii++ {
fmt.Println("here8")
c.currentTsPacket.Payload[ii] = <-c.payloadByteChan
}
c.tsChan<-c.currentTsPacket
}
}
}
}