diff --git a/packets/RtpToTsConverter.go b/packets/RtpToTsConverter.go index 6e601272..90a6704f 100644 --- a/packets/RtpToTsConverter.go +++ b/packets/RtpToTsConverter.go @@ -29,7 +29,6 @@ LICENSE package packets import ( - "os" "fmt" ) @@ -57,110 +56,46 @@ 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] } - 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 + 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 } } } diff --git a/revid/revid.go b/revid/revid.go index b6f6ba0f..56036f2d 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 { @@ -303,18 +303,17 @@ 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 == 3 { + now = time.Now() + if (packetCount == mp2tMaxPackets) || + (now.Sub(prevTime) > clipDuration*time.Second && packetCount%packetsPerFrame == 0) { 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 } } @@ -331,7 +330,6 @@ func output(output string) { 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)