From 7e3394fe0ecfbd7992f5bda9b5e559f984a3e2a6 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 1 Jan 2018 19:44:59 +1030 Subject: [PATCH] Updating remote --- packets/RtpToTsConverter.go | 146 +++++++++++++++++++++++++++--------- revid/revidv2.go | 1 - 2 files changed, 111 insertions(+), 36 deletions(-) diff --git a/packets/RtpToTsConverter.go b/packets/RtpToTsConverter.go index 7523ffa1..223a1ae4 100644 --- a/packets/RtpToTsConverter.go +++ b/packets/RtpToTsConverter.go @@ -28,7 +28,10 @@ LICENSE package packets -import _"fmt" +import ( + "fmt" + "os" +) type RtpToTsConverter interface { Convert() @@ -57,50 +60,123 @@ func NewRtpToTsConverter() (c *rtpToTsConverter) { } func (c* rtpToTsConverter) Convert() { - nalUnitChan := make(chan NALUnit, 10000) + file,_ := os.Create("video") pesPktChan := make(chan []byte, 10000) pesDataChan := make(chan byte, 15000) + nalAccessChan := make(chan []byte, 100) + data := make(chan byte,10000) payloadByteChan := make(chan byte, 15000) // Get nal units from incoming rtp + var sps []byte + var pps []byte + // Frist find sps + for { + rtpPacket := <-c.inputChan + fragmentType := rtpPacket.Payload[0] & 0x1F + if fragmentType == 7 { + sps = make([]byte,4+len(rtpPacket.Payload)) + sps[0] = 0x00 + sps[1] = 0x00 + sps[2] = 0x00 + sps[3] = 0x01 + for i := range rtpPacket.Payload { + sps[i+4] = rtpPacket.Payload[i] + } + break + } + } + // now find pps + for { + rtpPacket := <-c.inputChan + fragmentType := rtpPacket.Payload[0] & 0x1F + if fragmentType == 8 { + pps = make([]byte,4+len(rtpPacket.Payload)) + pps[0] = 0x00 + pps[1] = 0x00 + pps[2] = 0x00 + pps[3] = 0x01 + for i := range rtpPacket.Payload { + pps[i+4] = rtpPacket.Payload[i] + } + break + } + } for { select { default: case rtpPacket := <-c.inputChan: - if GetNalType( rtpPacket.Payload ) == 28 { - nalUnitChan<-ParseNALFragment(rtpPacket.Payload) - } else { - nalUnitChan<-ParseNALSpsPps(rtpPacket.Payload) - } - case nalUnit := <-nalUnitChan: - nalUnitByteSlice := nalUnit.ToByteSlice() - for ii := range nalUnitByteSlice { - pesDataChan<-nalUnitByteSlice[ii] - } - if nalFragment, isNALFragment := nalUnit.(*NALFragment); - (isNALFragment && nalFragment.End) || !isNALFragment { - pesDataChanLen := len(pesDataChan) - 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>7 + fiveNUBs := rtpPacket.Payload[1] & 0x1F + fragmentData := rtpPacket.Payload[2:] + fmt.Printf("fragmentType: %v\n", fragmentType) + if fragmentType == 28 { + if startBit == 1 { + if len(data) > 0 { + dataArray := make([]byte, len(data)) + for ii:=0; len(data) > 0; ii++ { + dataArray[ii] = <-data + } + fmt.Printf("data array size: %v\n", len(dataArray)) + nalAccessChan<-dataArray + file.Write(dataArray) + } + // load sps into data + for ii := range sps { + data<-sps[ii] + } + // load pps into data + for ii := range pps { + data<-pps[ii] + } + buffer := make([]byte,5+len(fragmentData)) + buffer[0] = 0x00 + buffer[1] = 0x00 + buffer[2] = 0x00 + buffer[3] = 0x01 + buffer[4] = threeNUBs | fiveNUBs + for i := range fragmentData { + buffer[i+5] = fragmentData[i] + } + // load data into data chan + for ii := range buffer { + data<-buffer[ii] + } + } else { + if len(data) > 0 { + for ii := range fragmentData { + data<-fragmentData[ii] + } + } } - pesPktChan<-pesPkt.ToByteSlice() } + 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