mirror of https://bitbucket.org/ausocean/av.git
Might have made some progress
This commit is contained in:
parent
067b36b1cd
commit
fd6c76a348
|
@ -84,7 +84,8 @@ func (p *MpegTsPacket) ToByteSlice() (output []byte) {
|
||||||
for ii := 4; ii-4 < len(p.AF); ii++ {
|
for ii := 4; ii-4 < len(p.AF); ii++ {
|
||||||
output[ii] = p.AF[ii-4]
|
output[ii] = p.AF[ii-4]
|
||||||
}
|
}
|
||||||
headerSize := packetLength-len(p.Payload)
|
//headerSize := packetLength-len(p.Payload)
|
||||||
|
headerSize := 4 + len(p.AF)
|
||||||
for ii := headerSize; ii < packetLength; ii++ {
|
for ii := headerSize; ii < packetLength; ii++ {
|
||||||
output[ii] = p.Payload[ii-headerSize]
|
output[ii] = p.Payload[ii-headerSize]
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ package packets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -94,6 +95,7 @@ func toUint(arr []byte) (ret uint) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) HandleRtpConn(conn net.PacketConn) {
|
func (s *Session) HandleRtpConn(conn net.PacketConn) {
|
||||||
|
file,_ := os.Create("video")
|
||||||
buf := make([]byte, 4096)
|
buf := make([]byte, 4096)
|
||||||
for {
|
for {
|
||||||
n, _, err := conn.ReadFrom(buf)
|
n, _, err := conn.ReadFrom(buf)
|
||||||
|
@ -102,6 +104,7 @@ func (s *Session) HandleRtpConn(conn net.PacketConn) {
|
||||||
}
|
}
|
||||||
cpy := make([]byte, n)
|
cpy := make([]byte, n)
|
||||||
copy(cpy, buf)
|
copy(cpy, buf)
|
||||||
|
file.Write(cpy)
|
||||||
go s.handleRtp(cpy)
|
go s.handleRtp(cpy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,11 @@ LICENSE
|
||||||
|
|
||||||
package packets
|
package packets
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
type RtpToTsConverter interface {
|
type RtpToTsConverter interface {
|
||||||
Convert()
|
Convert()
|
||||||
|
@ -53,35 +57,99 @@ func NewRtpToTsConverter() (c *rtpToTsConverter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c* rtpToTsConverter) Convert(rtpSession *Session) {
|
func (c* rtpToTsConverter) Convert(rtpSession *Session) {
|
||||||
|
file,_ := os.Create("video")
|
||||||
|
var sps []byte
|
||||||
|
var pps []byte
|
||||||
|
// Frist find sps
|
||||||
|
fmt.Println("finding sps")
|
||||||
|
for {
|
||||||
|
rtpPacket := <-rtpSession.RtpChan
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("finding pps")
|
||||||
|
// now find pps
|
||||||
|
for {
|
||||||
|
rtpPacket := <-rtpSession.RtpChan
|
||||||
|
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 {
|
for {
|
||||||
select{
|
select{
|
||||||
default:
|
default:
|
||||||
case rtpPacket := <-rtpSession.RtpChan:
|
case rtpPacket := <-rtpSession.RtpChan:
|
||||||
|
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:]
|
||||||
|
|
||||||
|
if fragmentType == 28 {
|
||||||
|
if startBit == 1 {
|
||||||
|
file.Write(sps)
|
||||||
|
file.Write(pps)
|
||||||
|
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]
|
||||||
|
}
|
||||||
|
file.Write(buffer)
|
||||||
|
} else {
|
||||||
|
file.Write(fragmentData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for ii := range rtpPacket.Payload {
|
for ii := range rtpPacket.Payload {
|
||||||
c.payloadByteChan<-rtpPacket.Payload[ii]
|
c.payloadByteChan<-rtpPacket.Payload[ii]
|
||||||
}
|
}
|
||||||
|
if rtpPacket.Marker == true {
|
||||||
|
firstPacket:=true
|
||||||
for len(c.payloadByteChan) > 0 {
|
for len(c.payloadByteChan) > 0 {
|
||||||
lengthOfByteChan := len(c.payloadByteChan)
|
lengthOfByteChan := len(c.payloadByteChan)
|
||||||
c.currentTsPacket = new(MpegTsPacket)
|
c.currentTsPacket = new(MpegTsPacket)
|
||||||
c.currentTsPacket.SyncByte = 0x47
|
c.currentTsPacket.SyncByte = 0x47
|
||||||
c.currentTsPacket.TEI = false
|
c.currentTsPacket.TEI = false
|
||||||
if lengthOfByteChan == len(rtpPacket.Payload) { // if it's the start of the payload
|
|
||||||
c.currentTsPacket.PUSI = true
|
|
||||||
} else {
|
|
||||||
c.currentTsPacket.PUSI = false
|
c.currentTsPacket.PUSI = false
|
||||||
|
if firstPacket { // if it's the start of the payload
|
||||||
|
c.currentTsPacket.PUSI = true
|
||||||
|
firstPacket = false
|
||||||
}
|
}
|
||||||
c.currentTsPacket.Priority = false
|
c.currentTsPacket.Priority = false
|
||||||
c.currentTsPacket.PID = 256
|
c.currentTsPacket.PID = 256
|
||||||
c.currentTsPacket.TSC = 0
|
c.currentTsPacket.TSC = 0
|
||||||
c.currentTsPacket.AFC = 3 // adaptationfield followed by payload
|
|
||||||
c.currentTsPacket.CC = c.currentCC
|
c.currentTsPacket.CC = c.currentCC
|
||||||
if c.currentCC++; c.currentCC > 15 { c.currentCC = 0 }
|
if c.currentCC++; c.currentCC > 15 { c.currentCC = 0 }
|
||||||
payloadLength := 140
|
payloadLength := 182
|
||||||
if lengthOfByteChan < 140 {
|
if lengthOfByteChan < 182 {
|
||||||
payloadLength = lengthOfByteChan
|
payloadLength = lengthOfByteChan
|
||||||
}
|
}
|
||||||
stuffingLength := 140-payloadLength
|
c.currentTsPacket.AFC = 3
|
||||||
|
stuffingLength := 182-payloadLength
|
||||||
c.currentTsPacket.AF = make([]byte,2 + stuffingLength) // adaptationfield flag length = 16
|
c.currentTsPacket.AF = make([]byte,2 + stuffingLength) // adaptationfield flag length = 16
|
||||||
c.currentTsPacket.AF[0] = byte(1 + stuffingLength)
|
c.currentTsPacket.AF[0] = byte(1 + stuffingLength)
|
||||||
c.currentTsPacket.AF[1] = 0
|
c.currentTsPacket.AF[1] = 0
|
||||||
|
@ -97,3 +165,4 @@ func (c* rtpToTsConverter) Convert(rtpSession *Session) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -276,8 +276,8 @@ func input(input string, output string) {
|
||||||
|
|
||||||
clipSize := 0
|
clipSize := 0
|
||||||
packetCount := 0
|
packetCount := 0
|
||||||
now := time.Now()
|
//now := time.Now()
|
||||||
prevTime := now
|
//prevTime := now
|
||||||
fmt.Printf("Looping\n")
|
fmt.Printf("Looping\n")
|
||||||
for {
|
for {
|
||||||
if clip, err := ringBuffer.Get(); err != nil {
|
if clip, err := ringBuffer.Get(); err != nil {
|
||||||
|
@ -294,6 +294,7 @@ func input(input string, output string) {
|
||||||
_, err = io.ReadFull(br, clip[clipSize:upperBound])
|
_, err = io.ReadFull(br, clip[clipSize:upperBound])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if *flags&filterFixContinuity != 0 && mp2tFixContinuity(clip[clipSize:upperBound], uint16(*selectedPID)) {
|
if *flags&filterFixContinuity != 0 && mp2tFixContinuity(clip[clipSize:upperBound], uint16(*selectedPID)) {
|
||||||
fmt.Printf("Packet #%d.%d fixed\n", clipCount, packetCount)
|
fmt.Printf("Packet #%d.%d fixed\n", clipCount, packetCount)
|
||||||
}
|
}
|
||||||
|
@ -302,17 +303,18 @@ func input(input string, output string) {
|
||||||
clipSize += mp2tPacketSize
|
clipSize += mp2tPacketSize
|
||||||
|
|
||||||
// send if (1) our buffer is full or (2) 1 second has elapsed and we have % packetsPerFrame
|
// send if (1) our buffer is full or (2) 1 second has elapsed and we have % packetsPerFrame
|
||||||
now = time.Now()
|
//now = time.Now()
|
||||||
if (packetCount == mp2tMaxPackets) ||
|
if packetCount == 3 {
|
||||||
(now.Sub(prevTime) > clipDuration*time.Second && packetCount%packetsPerFrame == 0) {
|
|
||||||
clipCount++
|
clipCount++
|
||||||
if err := ringBuffer.DoneWriting(clipSize); err != nil {
|
if err := ringBuffer.DoneWriting(clipSize); err != nil {
|
||||||
inputErrChan <- err
|
inputErrChan <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Println("here1")
|
||||||
|
time.Sleep(10*time.Second)
|
||||||
clipSize = 0
|
clipSize = 0
|
||||||
packetCount = 0
|
packetCount = 0
|
||||||
prevTime = now
|
//prevTime = now
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -328,6 +330,8 @@ func output(output string) {
|
||||||
for {
|
for {
|
||||||
if clip, err := ringBuffer.Read(); err == nil {
|
if clip, err := ringBuffer.Read(); err == nil {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
fmt.Println(clip)
|
||||||
|
sendClipToStdout(clip,output,conn)
|
||||||
for err = sendClip(clip, output, conn); err != nil; {
|
for err = sendClip(clip, output, conn); err != nil; {
|
||||||
outputErrChan <- err
|
outputErrChan <- err
|
||||||
err = sendClip(clip, output, conn)
|
err = sendClip(clip, output, conn)
|
||||||
|
@ -440,13 +444,16 @@ func sendClipToStdout(clip []byte, _ string, _ net.Conn) error {
|
||||||
packetCount++
|
packetCount++
|
||||||
pkt := clip[offset : offset+mp2tPacketSize]
|
pkt := clip[offset : offset+mp2tPacketSize]
|
||||||
|
|
||||||
pktPID, err := packet.Pid(pkt)
|
//pktPID, err := packet.Pid(pkt)
|
||||||
if err != nil {
|
//fmt.Printf("pktID: %v\n", pkt)
|
||||||
return err
|
//if err != nil {
|
||||||
}
|
//return err
|
||||||
|
//}
|
||||||
|
/*
|
||||||
if pktPID != uint16(*selectedPID) {
|
if pktPID != uint16(*selectedPID) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if *flags&(dumpPacketHeader|dumpPacketPayload) != 0 {
|
if *flags&(dumpPacketHeader|dumpPacketPayload) != 0 {
|
||||||
fmt.Printf("Packet #%d.%d\n", clipCount, packetCount)
|
fmt.Printf("Packet #%d.%d\n", clipCount, packetCount)
|
||||||
|
|
|
@ -292,16 +292,31 @@ func input(input string, output string) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
prevTime := now
|
prevTime := now
|
||||||
fmt.Printf("Looping\n")
|
fmt.Printf("Looping\n")
|
||||||
|
|
||||||
|
startPackets := [][]byte{
|
||||||
|
{71,64,17,16,0,66,240,65,0,1,193,0,0,255,1,255,0,1,252,128,48,72,46,1,6,70,70,109,112,101,103,37,115,116,114,101,97,109,101,100,32,98,121,32,116,104,101,32,71,101,111,86,105,115,105,111,110,32,82,116,115,112,32,83,101,114,118,101,114,99,176,214,195,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255},
|
||||||
|
{71,64,0,16,0,0,176,13,0,1,193,0,0,0,1,240,0,42,177,4,178,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255},
|
||||||
|
{71,80,0,16,0,2,176,18,0,1,193,0,0,225,0,240,0,27,225,0,240,0,21,189,77,86,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255},
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
if clip, err := ringBuffer.Get(); err != nil {
|
if clip, err := ringBuffer.Get(); err != nil {
|
||||||
inputErrChan <- err
|
inputErrChan <- err
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
ii := 0
|
||||||
for {
|
for {
|
||||||
upperBound := clipSize + mp2tPacketSize
|
upperBound := clipSize + mp2tPacketSize
|
||||||
|
if ii < 3 {
|
||||||
|
packetByteSlice := startPackets[ii]
|
||||||
|
copy(clip[clipSize:upperBound],packetByteSlice)
|
||||||
|
ii++
|
||||||
|
} else {
|
||||||
packet := <-converter.TsChan
|
packet := <-converter.TsChan
|
||||||
packetByteSlice := packet.ToByteSlice()
|
packetByteSlice := packet.ToByteSlice()
|
||||||
copy(clip[clipSize:upperBound],packetByteSlice)
|
copy(clip[clipSize:upperBound],packetByteSlice)
|
||||||
|
}
|
||||||
|
|
||||||
|
//fmt.Println(clip[clipSize:upperBound])
|
||||||
packetCount++
|
packetCount++
|
||||||
clipSize += mp2tPacketSize
|
clipSize += mp2tPacketSize
|
||||||
// send if (1) our buffer is full or (2) 1 second has elapsed and we have % packetsPerFrame
|
// send if (1) our buffer is full or (2) 1 second has elapsed and we have % packetsPerFrame
|
||||||
|
@ -331,7 +346,6 @@ func output(output string) {
|
||||||
for {
|
for {
|
||||||
if clip, err := ringBuffer.Read(); err == nil {
|
if clip, err := ringBuffer.Read(); err == nil {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
sendClipToStdout(clip)
|
|
||||||
for err = sendClip(clip, output, conn); err != nil; {
|
for err = sendClip(clip, output, conn); err != nil; {
|
||||||
outputErrChan <- err
|
outputErrChan <- err
|
||||||
err = sendClip(clip, output, conn)
|
err = sendClip(clip, output, conn)
|
||||||
|
|
Loading…
Reference in New Issue