From b84c19f719b78e446976b676570fb3f41b24bb77 Mon Sep 17 00:00:00 2001 From: deepch Date: Fri, 2 Feb 2024 02:48:16 +0300 Subject: [PATCH] testing --- example/test/main.go | 25 +++++++++++++++++++++++++ format/mp4f/muxer.go | 13 +++++++++---- format/rtsp/client.go | 43 ++++++++++++++++++++++++++++++++++++++----- format/rtsp/server.go | 1 + format/ts/demuxer.go | 6 +++++- 5 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 example/test/main.go create mode 100644 format/rtsp/server.go diff --git a/example/test/main.go b/example/test/main.go new file mode 100644 index 00000000..45ac920a --- /dev/null +++ b/example/test/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/deepch/vdk/format/ts" + "log" + "os" +) + +func main() { + f, _ := os.Open("edb9708f29b24ba9b175808d6b9df9c6541e25766d4a40209a8f903948b72f3f.ts") + m := ts.NewDemuxer(f) + var i int + for { + p, err := m.ReadPacket() + if err != nil { + return + } + if p.IsKeyFrame { + i = 0 + } + log.Println(i, p.Time, p.Data[4:10], len(p.Data)) + i++ + + } +} diff --git a/format/mp4f/muxer.go b/format/mp4f/muxer.go index 4042c9b8..c2f0f474 100644 --- a/format/mp4f/muxer.go +++ b/format/mp4f/muxer.go @@ -3,6 +3,7 @@ package mp4f import ( "bufio" "fmt" + "log" "os" "time" @@ -238,14 +239,15 @@ func (self *Muxer) WriteTrailer() (err error) { return } -func (element *Muxer) WriteHeader(streams []av.CodecData) (err error) { +func (element *Muxer) WriteHeader(streams []av.CodecData) error { element.streams = []*Stream{} for _, stream := range streams { - if err = element.newStream(stream); err != nil { - return + if err := element.newStream(stream); err != nil { + log.Println("WriteHeader", err) } } - return + + return nil } func (element *Muxer) GetInit(streams []av.CodecData) (string, []byte) { @@ -285,6 +287,9 @@ func (element *Muxer) GetInit(streams []av.CodecData) (string, []byte) { } func (element *Muxer) WritePacket(pkt av.Packet, GOP bool) (bool, []byte, error) { + if pkt.Idx+1 > int8(len(element.streams)) { + return false, nil, nil + } stream := element.streams[pkt.Idx] if GOP { ts := time.Duration(0) diff --git a/format/rtsp/client.go b/format/rtsp/client.go index 89a9d89a..2aa4791a 100644 --- a/format/rtsp/client.go +++ b/format/rtsp/client.go @@ -40,9 +40,10 @@ const ( ) type Client struct { - DebugRtsp bool - DebugRtp bool - Headers []string + DebugRtsp bool + DebugRtp bool + DisableAudio bool + Headers []string SkipErrRtpBlock bool @@ -1076,12 +1077,44 @@ func (self *Stream) handleRtpPacket(packet []byte) (err error) { err = fmt.Errorf("rtp: packet too short") return } - payloadOffset := 12 + int(packet[0]&0xf)*4 + + timestamp := binary.BigEndian.Uint32(packet[4:8]) + + /* + Test offset + */ + Padding := (packet[0]>>5)&1 == 1 + Extension := (packet[0]>>4)&1 == 1 + CSRCCnt := int(packet[0] & 0x0f) + + RTPHeaderSize := 12 + + payloadOffset := RTPHeaderSize + end := len(packet) + if end-payloadOffset >= 4*CSRCCnt { + payloadOffset += 4 * CSRCCnt + } + + if Extension && end-payloadOffset >= 4 { + extLen := 4 * int(binary.BigEndian.Uint16(packet[payloadOffset+2:])) + payloadOffset += 4 + if end-payloadOffset >= extLen { + payloadOffset += extLen + } + } + + if Padding && end-payloadOffset > 0 { + paddingLen := int(packet[end-1]) + if end-payloadOffset >= paddingLen { + end -= paddingLen + } + } + if payloadOffset > len(packet) { err = fmt.Errorf("rtp: packet too short") return } - timestamp := binary.BigEndian.Uint32(packet[4:8]) + payload := packet[payloadOffset:] /* diff --git a/format/rtsp/server.go b/format/rtsp/server.go new file mode 100644 index 00000000..185db25a --- /dev/null +++ b/format/rtsp/server.go @@ -0,0 +1 @@ +package rtsp diff --git a/format/ts/demuxer.go b/format/ts/demuxer.go index 8c028252..3b6e122a 100644 --- a/format/ts/demuxer.go +++ b/format/ts/demuxer.go @@ -284,7 +284,11 @@ func (self *Stream) payloadEnd() (n int, err error) { b := make([]byte, 4+len(nalu)) pio.PutU32BE(b[0:4], uint32(len(nalu))) copy(b[4:], nalu) - self.addPacket(b, time.Duration(0), (1000*time.Millisecond)/time.Duration(self.fps)) + fps := self.fps + if self.fps == 0 { + fps = 25 + } + self.addPacket(b, time.Duration(0), (1000*time.Millisecond)/time.Duration(fps)) n++ } }