From 69a7481671f038edbf9c7d41f1335cc55dfc84a7 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 2 Jan 2018 16:37:53 +1030 Subject: [PATCH] making vlc seg faultttt! --- packets/RtpToTsConverter.go | 167 ++++++++++++++++++++---------------- 1 file changed, 92 insertions(+), 75 deletions(-) diff --git a/packets/RtpToTsConverter.go b/packets/RtpToTsConverter.go index 223a1ae4..f052c360 100644 --- a/packets/RtpToTsConverter.go +++ b/packets/RtpToTsConverter.go @@ -29,7 +29,7 @@ LICENSE package packets import ( - "fmt" + _"fmt" "os" ) @@ -59,97 +59,114 @@ func NewRtpToTsConverter() (c *rtpToTsConverter) { 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, 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 + 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,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 - } + 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,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 - } + 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: - threeNUBs := rtpPacket.Payload[0] & 0xE0 - fragmentType := rtpPacket.Payload[0] & 0x1F - startBit := (rtpPacket.Payload[1] & 0x80)>>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 + 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 } - 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] + 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 {