From 1bcf05830d7b3e84856000158948ac242b5bb3a3 Mon Sep 17 00:00:00 2001 From: Saxon Date: Wed, 17 Apr 2019 12:20:36 +0930 Subject: [PATCH] protocol/rtsp: removing rtp package We already have an rtp package, which I will extend with client functionality, so we don't need this here. --- protocol/rtsp/rtp/rtp.go | 150 ---------------------------------- protocol/rtsp/rtp/rtp_test.go | 21 ----- 2 files changed, 171 deletions(-) delete mode 100644 protocol/rtsp/rtp/rtp.go delete mode 100644 protocol/rtsp/rtp/rtp_test.go diff --git a/protocol/rtsp/rtp/rtp.go b/protocol/rtsp/rtp/rtp.go deleted file mode 100644 index 8d2712c2..00000000 --- a/protocol/rtsp/rtp/rtp.go +++ /dev/null @@ -1,150 +0,0 @@ -package rtp - -import ( - "fmt" - "net" -) - -const ( - RTP_VERSION = 2 -) - -const ( - hasRtpPadding = 1 << 2 - hasRtpExt = 1 << 3 -) - -// Packet as per https://tools.ietf.org/html/rfc1889#section-5.1 -// -// 0 1 2 3 -// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 -// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -// |V=2|P|X| CC |M| PT | sequence number | -// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -// | timestamp | -// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -// | synchronization source (SSRC) identifier | -// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ -// | contributing source (CSRC) identifiers | -// | .... | -// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -type RtpPacket struct { - Version byte - Padding bool - Ext bool - Marker bool - PayloadType byte - SequenceNumber uint - Timestamp uint - SyncSource uint - - CSRC []uint - - ExtHeader uint - ExtData []byte - - Payload []byte -} - -type Session struct { - Rtp net.PacketConn - Rtcp net.PacketConn - - RtpChan <-chan RtpPacket - RtcpChan <-chan []byte - - rtpChan chan<- RtpPacket - rtcpChan chan<- []byte -} - -func New(rtp, rtcp net.PacketConn) *Session { - rtpChan := make(chan RtpPacket, 10) - rtcpChan := make(chan []byte, 10) - s := &Session{ - Rtp: rtp, - Rtcp: rtcp, - RtpChan: rtpChan, - RtcpChan: rtcpChan, - rtpChan: rtpChan, - rtcpChan: rtcpChan, - } - go s.HandleRtpConn(rtp) - go s.HandleRtcpConn(rtcp) - return s -} - -func toUint(arr []byte) (ret uint) { - for i, b := range arr { - ret |= uint(b) << (8 * uint(len(arr)-i-1)) - } - return ret -} - -func (s *Session) HandleRtpConn(conn net.PacketConn) { - buf := make([]byte, 4096) - for { - n, _, err := conn.ReadFrom(buf) - if err != nil { - panic(err) - } - - cpy := make([]byte, n) - copy(cpy, buf) - go s.handleRtp(cpy) - } -} - -func (s *Session) HandleRtcpConn(conn net.PacketConn) { - buf := make([]byte, 4096) - for { - n, _, err := conn.ReadFrom(buf) - if err != nil { - panic(err) - } - cpy := make([]byte, n) - copy(cpy, buf) - go s.handleRtcp(cpy) - } -} - -func (s *Session) handleRtp(buf []byte) { - packet := RtpPacket{ - Version: buf[0] & 0x03, - Padding: buf[0]&hasRtpPadding != 0, - Ext: buf[0]&hasRtpExt != 0, - CSRC: make([]uint, buf[0]>>4), - Marker: buf[1]&1 != 0, - PayloadType: buf[1] >> 1, - SequenceNumber: toUint(buf[2:4]), - Timestamp: toUint(buf[4:8]), - SyncSource: toUint(buf[8:12]), - } - if packet.Version != RTP_VERSION { - fmt.Printf("version: %v\n", packet.Version) - } - - i := 12 - - for j := range packet.CSRC { - packet.CSRC[j] = toUint(buf[i : i+4]) - i += 4 - } - - if packet.Ext { - packet.ExtHeader = toUint(buf[i : i+2]) - length := toUint(buf[i+2 : i+4]) - i += 4 - if length > 0 { - packet.ExtData = buf[i : i+int(length)*4] - i += int(length) * 4 - } - } - - packet.Payload = buf[i:] - - s.rtpChan <- packet -} - -func (s *Session) handleRtcp(buf []byte) { - // TODO: implement rtcp -} diff --git a/protocol/rtsp/rtp/rtp_test.go b/protocol/rtsp/rtp/rtp_test.go deleted file mode 100644 index da54d96c..00000000 --- a/protocol/rtsp/rtp/rtp_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package rtp - -import ( - "testing" -) - -func TestToUint(t *testing.T) { - tests := []struct { - arr []byte - exp uint - }{ - {[]byte{1, 2}, 0x102}, - {[]byte{3, 2, 1, 0}, 0x3020100}, - } - for _, tst := range tests { - val := toUint(tst.arr) - if val != tst.exp { - t.Errorf("%d != %d for % x", val, tst.exp, tst.arr) - } - } -}