diff --git a/packets/MpegTs.go b/packets/MpegTs.go index 9302d1bd..f601d68c 100644 --- a/packets/MpegTs.go +++ b/packets/MpegTs.go @@ -84,7 +84,8 @@ func (p *MpegTsPacket) ToByteSlice() (output []byte) { for ii := 4; ii-4 < len(p.AF); ii++ { 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++ { output[ii] = p.Payload[ii-headerSize] } diff --git a/packets/Rtp.go b/packets/Rtp.go index 0e6b99d3..b4746535 100644 --- a/packets/Rtp.go +++ b/packets/Rtp.go @@ -33,6 +33,7 @@ package packets import ( "net" + "os" ) const ( @@ -94,6 +95,7 @@ func toUint(arr []byte) (ret uint) { } func (s *Session) HandleRtpConn(conn net.PacketConn) { + file,_ := os.Create("video") buf := make([]byte, 4096) for { n, _, err := conn.ReadFrom(buf) @@ -102,6 +104,7 @@ func (s *Session) HandleRtpConn(conn net.PacketConn) { } cpy := make([]byte, n) copy(cpy, buf) + file.Write(cpy) go s.handleRtp(cpy) } } diff --git a/packets/RtpToTsConverter.go b/packets/RtpToTsConverter.go index 0d2e99a1..972dad56 100644 --- a/packets/RtpToTsConverter.go +++ b/packets/RtpToTsConverter.go @@ -28,7 +28,11 @@ LICENSE package packets -import "fmt" +import ( + "os" + "fmt" +) + type RtpToTsConverter interface { Convert() @@ -53,46 +57,111 @@ func NewRtpToTsConverter() (c *rtpToTsConverter) { } 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 { select{ default: 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 { c.payloadByteChan<-rtpPacket.Payload[ii] } - - for len(c.payloadByteChan) > 0 { - lengthOfByteChan := len(c.payloadByteChan) - c.currentTsPacket = new(MpegTsPacket) - c.currentTsPacket.SyncByte = 0x47 - c.currentTsPacket.TEI = false - if lengthOfByteChan == len(rtpPacket.Payload) { // if it's the start of the payload - c.currentTsPacket.PUSI = true - } else { + if rtpPacket.Marker == true { + firstPacket:=true + for len(c.payloadByteChan) > 0 { + lengthOfByteChan := len(c.payloadByteChan) + c.currentTsPacket = new(MpegTsPacket) + c.currentTsPacket.SyncByte = 0x47 + c.currentTsPacket.TEI = 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.PID = 256 + c.currentTsPacket.TSC = 0 + c.currentTsPacket.CC = c.currentCC + if c.currentCC++; c.currentCC > 15 { c.currentCC = 0 } + payloadLength := 182 + if lengthOfByteChan < 182 { + payloadLength = lengthOfByteChan + } + c.currentTsPacket.AFC = 3 + stuffingLength := 182-payloadLength + c.currentTsPacket.AF = make([]byte,2 + stuffingLength) // adaptationfield flag length = 16 + c.currentTsPacket.AF[0] = byte(1 + stuffingLength) + c.currentTsPacket.AF[1] = 0 + for ii := 0; ii < stuffingLength; ii++ { + c.currentTsPacket.AF[2+ii] = 0xFF + } + c.currentTsPacket.Payload = make([]byte, payloadLength) + for ii:=0; ii < payloadLength; ii++ { + c.currentTsPacket.Payload[ii] = <-c.payloadByteChan + } + c.tsChan<-c.currentTsPacket } - c.currentTsPacket.Priority = false - c.currentTsPacket.PID = 256 - c.currentTsPacket.TSC = 0 - c.currentTsPacket.AFC = 3 // adaptationfield followed by payload - c.currentTsPacket.CC = c.currentCC - if c.currentCC++; c.currentCC > 15 { c.currentCC = 0 } - payloadLength := 140 - if lengthOfByteChan < 140 { - payloadLength = lengthOfByteChan - } - stuffingLength := 140-payloadLength - c.currentTsPacket.AF = make([]byte,2 + stuffingLength) // adaptationfield flag length = 16 - c.currentTsPacket.AF[0] = byte(1 + stuffingLength) - c.currentTsPacket.AF[1] = 0 - for ii := 0; ii < stuffingLength; ii++ { - c.currentTsPacket.AF[2+ii] = 0xFF - } - c.currentTsPacket.Payload = make([]byte, payloadLength) - for ii:=0; ii < payloadLength; ii++ { - c.currentTsPacket.Payload[ii] = <-c.payloadByteChan - } - c.tsChan<-c.currentTsPacket } } } diff --git a/revid/revid.go b/revid/revid.go index 85bec253..b6f6ba0f 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -276,8 +276,8 @@ func input(input string, output string) { clipSize := 0 packetCount := 0 - now := time.Now() - prevTime := now + //now := time.Now() + //prevTime := now fmt.Printf("Looping\n") for { if clip, err := ringBuffer.Get(); err != nil { @@ -294,6 +294,7 @@ func input(input string, output string) { _, err = io.ReadFull(br, clip[clipSize:upperBound]) } + if *flags&filterFixContinuity != 0 && mp2tFixContinuity(clip[clipSize:upperBound], uint16(*selectedPID)) { fmt.Printf("Packet #%d.%d fixed\n", clipCount, packetCount) } @@ -302,17 +303,18 @@ func input(input string, output string) { clipSize += mp2tPacketSize // send if (1) our buffer is full or (2) 1 second has elapsed and we have % packetsPerFrame - now = time.Now() - if (packetCount == mp2tMaxPackets) || - (now.Sub(prevTime) > clipDuration*time.Second && packetCount%packetsPerFrame == 0) { + //now = time.Now() + if packetCount == 3 { clipCount++ if err := ringBuffer.DoneWriting(clipSize); err != nil { inputErrChan <- err return } + fmt.Println("here1") + time.Sleep(10*time.Second) clipSize = 0 packetCount = 0 - prevTime = now + //prevTime = now break } } @@ -328,6 +330,8 @@ func output(output string) { for { if clip, err := ringBuffer.Read(); err == nil { now := time.Now() + fmt.Println(clip) + sendClipToStdout(clip,output,conn) for err = sendClip(clip, output, conn); err != nil; { outputErrChan <- err err = sendClip(clip, output, conn) @@ -440,13 +444,16 @@ func sendClipToStdout(clip []byte, _ string, _ net.Conn) error { packetCount++ pkt := clip[offset : offset+mp2tPacketSize] - pktPID, err := packet.Pid(pkt) - if err != nil { - return err - } + //pktPID, err := packet.Pid(pkt) + //fmt.Printf("pktID: %v\n", pkt) + //if err != nil { + //return err + //} + /* if pktPID != uint16(*selectedPID) { continue } + */ if *flags&(dumpPacketHeader|dumpPacketPayload) != 0 { fmt.Printf("Packet #%d.%d\n", clipCount, packetCount) diff --git a/revid/revidv2.go b/revid/revidv2.go index 34b08dc5..72857787 100644 --- a/revid/revidv2.go +++ b/revid/revidv2.go @@ -292,16 +292,31 @@ func input(input string, output string) { now := time.Now() prevTime := now 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 { if clip, err := ringBuffer.Get(); err != nil { inputErrChan <- err return } else { + ii := 0 for { upperBound := clipSize + mp2tPacketSize - packet := <-converter.TsChan - packetByteSlice := packet.ToByteSlice() - copy(clip[clipSize:upperBound],packetByteSlice) + if ii < 3 { + packetByteSlice := startPackets[ii] + copy(clip[clipSize:upperBound],packetByteSlice) + ii++ + } else { + packet := <-converter.TsChan + packetByteSlice := packet.ToByteSlice() + copy(clip[clipSize:upperBound],packetByteSlice) + } + + //fmt.Println(clip[clipSize:upperBound]) packetCount++ clipSize += mp2tPacketSize // 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 { if clip, err := ringBuffer.Read(); err == nil { now := time.Now() - sendClipToStdout(clip) for err = sendClip(clip, output, conn); err != nil; { outputErrChan <- err err = sendClip(clip, output, conn)