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 return
} }
func getOctectType(p *RtpPacket) byte { func GetOctectType(p *RtpPacket) byte {
return p.Payload[0] & 0x1F return p.Payload[0] & 0x1F
} }
func getStartBit(p *RtpPacket) byte { func GetStartBit(p *RtpPacket) byte {
return (p.Payload[1] & 0x80)>>7 return (p.Payload[1] & 0x80)>>7
} }
@ -72,100 +72,93 @@ func getEndBit(p *RtpPacket) byte {
} }
func (c* rtpToTsConverter) Convert() { func (c* rtpToTsConverter) Convert() {
file,_ := os.Create("video") file,_ := os.Create("video")
pesPktChan := make(chan []byte, 10) pesPktChan := make(chan []byte, 1000)
pesDataChan := make(chan byte, 50000) pesDataChan := make(chan byte, 50000)
nalAccessChan := make(chan []byte, 10) nalAccessChan := make(chan []byte, 10000)
payloadByteChan := make(chan byte, 100000) payloadByteChan := make(chan byte, 100000)
// grab sps pps and sei var rtpBuffer [](*RtpPacket)
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
}
}
for { for {
select { select {
default: default:
case rtpPacket := <-c.inputChan: case rtpPacket := <-c.inputChan:
if getOctectType(&rtpPacket) == 28 { rtpBuffer = append(rtpBuffer,&rtpPacket)
if getStartBit(&rtpPacket) == 1 { if len(rtpBuffer) > 2 {
var buffer []byte // if there's something weird going on with sequence numbers then sort
// au delimiter if rtpPacket.SequenceNumber < rtpBuffer[len(rtpBuffer)-2].SequenceNumber {
buffer = append(buffer, []byte{0x00,0x00,0x01}...) for i := 1; i < len(rtpBuffer); i++ {
buffer = append(buffer, []byte{0x09,0x10}...) for j := i; j > 0 && rtpBuffer[j].SequenceNumber < rtpBuffer[j - 1].SequenceNumber; j-- {
buffer = append(buffer, []byte{0x00,0x00,0x01}...) temp := rtpBuffer[j]
buffer = append(buffer, sps...) rtpBuffer[j] = rtpBuffer[j-1]
buffer = append(buffer, []byte{0x00,0x00,0x01}...) rtpBuffer[j-1] = temp
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
} }
} }
} }
} else { }
if getOctectType(&rtpPacket) == 1 { if len(rtpBuffer) > 200 {
var buffer []byte // Discard everything before a type 7
buffer = append(buffer, []byte{0x00,0x00,0x01}...) for GetOctectType(rtpBuffer[0]) != 7 {
buffer = append(buffer, []byte{0x09,0x10}...) rtpBuffer = rtpBuffer[1:]
buffer = append(buffer, []byte{0x00,0x00,0x01}...) }
buffer = append(buffer, sps...) // get sps
buffer = append(buffer, []byte{0x00,0x00,0x01}...) sps := make([]byte,len(rtpBuffer[0].Payload))
buffer = append(buffer, pps...) copy(sps[:],rtpBuffer[0].Payload[:])
buffer = append(buffer, []byte{0x00,0x00,0x01}...) rtpBuffer = rtpBuffer[1:]
buffer = append(buffer, sei...) // get pps
buffer = append(buffer, []byte{0x00,0x00,0x01}...) pps := make([]byte,len(rtpBuffer[0].Payload))
buffer = append(buffer, rtpPacket.Payload...) copy(pps[:],rtpBuffer[0].Payload[:])
nalAccessChan<-buffer rtpBuffer = rtpBuffer[1:]
file.Write(buffer) // 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: case nalUnit := <-nalAccessChan: