Managed to get raw H264 working but still no mpegts

This commit is contained in:
Unknown 2018-01-03 13:41:27 +10:30
parent 69a7481671
commit 971fe0270f
1 changed files with 80 additions and 87 deletions

View File

@ -59,11 +59,11 @@ func NewRtpToTsConverter() (c *rtpToTsConverter) {
return
}
func getOctectType(p *RtpPacket) byte {
func GetOctectType(p *RtpPacket) byte {
return p.Payload[0] & 0x1F
}
func getStartBit(p *RtpPacket) byte {
func GetStartBit(p *RtpPacket) byte {
return (p.Payload[1] & 0x80)>>7
}
@ -72,100 +72,93 @@ func getEndBit(p *RtpPacket) byte {
}
func (c* rtpToTsConverter) Convert() {
file,_ := os.Create("video")
pesPktChan := make(chan []byte, 10)
pesPktChan := make(chan []byte, 1000)
pesDataChan := make(chan byte, 50000)
nalAccessChan := make(chan []byte, 10)
nalAccessChan := make(chan []byte, 10000)
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
}
}
var rtpBuffer [](*RtpPacket)
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
rtpBuffer = append(rtpBuffer,&rtpPacket)
if len(rtpBuffer) > 2 {
// if there's something weird going on with sequence numbers then sort
if rtpPacket.SequenceNumber < rtpBuffer[len(rtpBuffer)-2].SequenceNumber {
for i := 1; i < len(rtpBuffer); i++ {
for j := i; j > 0 && rtpBuffer[j].SequenceNumber < rtpBuffer[j - 1].SequenceNumber; j-- {
temp := rtpBuffer[j]
rtpBuffer[j] = rtpBuffer[j-1]
rtpBuffer[j-1] = temp
}
}
}
} 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)
}
if len(rtpBuffer) > 200 {
// Discard everything before a type 7
for GetOctectType(rtpBuffer[0]) != 7 {
rtpBuffer = rtpBuffer[1:]
}
// get sps
sps := make([]byte,len(rtpBuffer[0].Payload))
copy(sps[:],rtpBuffer[0].Payload[:])
rtpBuffer = rtpBuffer[1:]
// get pps
pps := make([]byte,len(rtpBuffer[0].Payload))
copy(pps[:],rtpBuffer[0].Payload[:])
rtpBuffer = rtpBuffer[1:]
// get sei
sei := make([]byte, len(rtpBuffer[0].Payload))
copy(sei[:],rtpBuffer[0].Payload[:])
rtpBuffer = rtpBuffer[1:]
// while we haven't reached the next sps in the buffer
for GetOctectType(rtpBuffer[0]) != 7 {
switch(GetOctectType(rtpBuffer[0])){
case 28:
if GetStartBit(rtpBuffer[0]) == 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, rtpBuffer[0].Payload[0] & 0xE0 | rtpBuffer[0].Payload[1] & 0x1F )
buffer = append(buffer, rtpBuffer[0].Payload[2:]...)
rtpBuffer = rtpBuffer[1:]
for {
buffer = append(buffer, rtpBuffer[0].Payload[2:]...)
if getEndBit(rtpBuffer[0]) == 1 {
rtpBuffer = rtpBuffer[1:]
nalAccessChan<-buffer
file.Write(buffer)
break
}
rtpBuffer = rtpBuffer[1:]
}
}
case 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, rtpBuffer[0].Payload[0] & 0xE0 | rtpBuffer[0].Payload[1] & 0x1F )
buffer = append(buffer, rtpBuffer[0].Payload[2:]...)
rtpBuffer = rtpBuffer[1:]
nalAccessChan<-buffer
file.Write(buffer)
default:
}
}
}
case nalUnit := <-nalAccessChan: