making vlc seg faultttt!

This commit is contained in:
Unknown 2018-01-02 16:37:53 +10:30
parent 7e3394fe0e
commit 69a7481671
1 changed files with 92 additions and 75 deletions

View File

@ -29,7 +29,7 @@ LICENSE
package packets package packets
import ( import (
"fmt" _"fmt"
"os" "os"
) )
@ -59,97 +59,114 @@ func NewRtpToTsConverter() (c *rtpToTsConverter) {
return 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() { func (c* rtpToTsConverter) Convert() {
file,_ := os.Create("video") file,_ := os.Create("video")
pesPktChan := make(chan []byte, 10000) pesPktChan := make(chan []byte, 10)
pesDataChan := make(chan byte, 15000) pesDataChan := make(chan byte, 50000)
nalAccessChan := make(chan []byte, 100) nalAccessChan := make(chan []byte, 10)
data := make(chan byte,10000) payloadByteChan := make(chan byte, 100000)
payloadByteChan := make(chan byte, 15000) // grab sps pps and sei
// Get nal units from incoming rtp
var sps []byte var sps []byte
var pps []byte var pps []byte
var sei []byte
// Frist find sps // Frist find sps
for { for {
rtpPacket := <-c.inputChan rtpPacket := <-c.inputChan
fragmentType := rtpPacket.Payload[0] & 0x1F fragmentType := rtpPacket.Payload[0] & 0x1F
if fragmentType == 7 { if fragmentType == 7 {
sps = make([]byte,4+len(rtpPacket.Payload)) sps = make([]byte,len(rtpPacket.Payload))
sps[0] = 0x00 for i := range rtpPacket.Payload {
sps[1] = 0x00 sps[i] = rtpPacket.Payload[i]
sps[2] = 0x00 }
sps[3] = 0x01 break
for i := range rtpPacket.Payload { }
sps[i+4] = rtpPacket.Payload[i]
}
break
}
} }
// now find pps // now find pps
for { for {
rtpPacket := <-c.inputChan rtpPacket := <-c.inputChan
fragmentType := rtpPacket.Payload[0] & 0x1F fragmentType := rtpPacket.Payload[0] & 0x1F
if fragmentType == 8 { if fragmentType == 8 {
pps = make([]byte,4+len(rtpPacket.Payload)) pps = make([]byte,len(rtpPacket.Payload))
pps[0] = 0x00 for i := range rtpPacket.Payload {
pps[1] = 0x00 pps[i] = rtpPacket.Payload[i]
pps[2] = 0x00 }
pps[3] = 0x01 break
for i := range rtpPacket.Payload { }
pps[i+4] = rtpPacket.Payload[i] }
} // now find sei
break 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 { for {
select { select {
default: default:
case rtpPacket := <-c.inputChan: case rtpPacket := <-c.inputChan:
threeNUBs := rtpPacket.Payload[0] & 0xE0 if getOctectType(&rtpPacket) == 28 {
fragmentType := rtpPacket.Payload[0] & 0x1F if getStartBit(&rtpPacket) == 1 {
startBit := (rtpPacket.Payload[1] & 0x80)>>7 var buffer []byte
fiveNUBs := rtpPacket.Payload[1] & 0x1F // au delimiter
fragmentData := rtpPacket.Payload[2:] buffer = append(buffer, []byte{0x00,0x00,0x01}...)
fmt.Printf("fragmentType: %v\n", fragmentType) buffer = append(buffer, []byte{0x09,0x10}...)
if fragmentType == 28 { buffer = append(buffer, []byte{0x00,0x00,0x01}...)
if startBit == 1 { buffer = append(buffer, sps...)
if len(data) > 0 { buffer = append(buffer, []byte{0x00,0x00,0x01}...)
dataArray := make([]byte, len(data)) buffer = append(buffer, pps...)
for ii:=0; len(data) > 0; ii++ { buffer = append(buffer, []byte{0x00,0x00,0x01}...)
dataArray[ii] = <-data 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)) buffer = append(buffer, rtpPacket.Payload[2:]...)
nalAccessChan<-dataArray if getEndBit(&rtpPacket) == 1 {
file.Write(dataArray) nalAccessChan<-buffer
} file.Write(buffer)
// load sps into data break
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]
} }
} }
} }
} 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: case nalUnit := <-nalAccessChan:
for ii := range nalUnit { for ii := range nalUnit {