From e1dd89b34863315fed8100bb3ad5e1ca54d44ad9 Mon Sep 17 00:00:00 2001 From: saxon Date: Thu, 27 Dec 2018 14:44:30 +1030 Subject: [PATCH 1/5] rtp: using static memory to hold bytes of rtp pkts --- stream/rtp/encoder.go | 11 ++++++----- stream/rtp/rtp.go | 14 +++++++++----- stream/rtp/rtp_test.go | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/stream/rtp/encoder.go b/stream/rtp/encoder.go index e20be442..88bebc4f 100644 --- a/stream/rtp/encoder.go +++ b/stream/rtp/encoder.go @@ -40,7 +40,7 @@ const ( timestampFreq = 90000 // Hz mtsSize = 188 bufferSize = 1000 - sendLength = 7 * 188 + sendLen = 7 * 188 ) // Encoder implements io writer and provides functionality to wrap data into @@ -53,6 +53,7 @@ type Encoder struct { frameInterval time.Duration fps int buffer []byte + pktSpace [defPktLen]byte } // NewEncoder returns a new Encoder type given an io.Writer - the destination @@ -71,9 +72,9 @@ func NewEncoder(dst io.Writer, fps int) *Encoder { // so that multiple layers of packetization can occur. func (e *Encoder) Write(data []byte) (int, error) { e.buffer = append(e.buffer, data...) - for len(e.buffer) >= sendLength { - e.Encode(e.buffer[:sendLength]) - e.buffer = e.buffer[sendLength:] + for len(e.buffer) >= sendLen { + e.Encode(e.buffer[:sendLen]) + e.buffer = e.buffer[sendLen:] } return len(data), nil } @@ -93,7 +94,7 @@ func (e *Encoder) Encode(payload []byte) error { Payload: payload, Padding: no, } - _, err := e.dst.Write(pkt.Bytes()) + _, err := e.dst.Write(pkt.Bytes(e.pktSpace[:defPktLen])) if err != nil { return err } diff --git a/stream/rtp/rtp.go b/stream/rtp/rtp.go index 4e393679..5d6e0ace 100644 --- a/stream/rtp/rtp.go +++ b/stream/rtp/rtp.go @@ -33,7 +33,10 @@ for rtp-h264 and rtp standards. package rtp const ( - rtpVer = 2 + rtpVer = 2 + headLen = 3 * 4 + defPayloadLen = 7 * 188 + defPktLen = headLen + defPayloadLen ) // Pkt provides fields consistent with RFC3550 definition of an rtp packet @@ -53,7 +56,11 @@ type Pkt struct { } // Bytes provides a byte slice of the packet -func (p *Pkt) Bytes() []byte { +func (p *Pkt) Bytes(buf []byte) []byte { + if buf == nil || len(buf) != defPktLen { + buf = make([]byte, headLen, defPktLen) + } + buf = buf[:headLen] if p.V == 0 { p.V = rtpVer } @@ -74,9 +81,6 @@ func (p *Pkt) Bytes() []byte { panic("rtp: CC (CSRC count) not 0, but CSRC headers not yet supported.") } - const headSize = 3 * 4 // bytes - buf := make([]byte, headSize, headSize+len(p.Payload)+int(p.Padding)) - buf[0] = p.V<<6 | p.p<<5 | p.CC buf[1] = p.M<<7 | p.PT buf[2] = byte(p.SN >> 8) diff --git a/stream/rtp/rtp_test.go b/stream/rtp/rtp_test.go index 85b33590..17e3d5bf 100644 --- a/stream/rtp/rtp_test.go +++ b/stream/rtp/rtp_test.go @@ -65,7 +65,7 @@ var rtpTests = []struct { func TestRtpPktToByteSlice(t *testing.T) { for _, test := range rtpTests { - got := test.pkt.Bytes() + got := test.pkt.Bytes(nil) if !reflect.DeepEqual(got, test.want) { t.Errorf("unexpected error for test %v: got:%v want:%v", test.num, got, test.want) From 037b51133093ae727c01d9474cc862551db271de Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 29 Dec 2018 16:55:02 +1030 Subject: [PATCH 2/5] revid: removed fatal when raspivid doesn't start (addressed in another PR) --- revid/revid.go | 1 - 1 file changed, 1 deletion(-) diff --git a/revid/revid.go b/revid/revid.go index 72fc964f..93bcdf38 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -487,7 +487,6 @@ func (r *Revid) startRaspivid() error { } err = r.cmd.Start() if err != nil { - r.config.Logger.Log(smartlogger.Fatal, pkg+"cannot start revid", "error", err.Error()) return err } From d5b6e91892cff15897335cfb8fdfd0be406cad44 Mon Sep 17 00:00:00 2001 From: saxon Date: Wed, 2 Jan 2019 10:32:48 +1030 Subject: [PATCH 3/5] rtp: using 'size' instead of 'len' --- stream/rtp/encoder.go | 4 ++-- stream/rtp/rtp.go | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/stream/rtp/encoder.go b/stream/rtp/encoder.go index 88bebc4f..20df9434 100644 --- a/stream/rtp/encoder.go +++ b/stream/rtp/encoder.go @@ -53,7 +53,7 @@ type Encoder struct { frameInterval time.Duration fps int buffer []byte - pktSpace [defPktLen]byte + pktSpace [defPktSize]byte } // NewEncoder returns a new Encoder type given an io.Writer - the destination @@ -94,7 +94,7 @@ func (e *Encoder) Encode(payload []byte) error { Payload: payload, Padding: no, } - _, err := e.dst.Write(pkt.Bytes(e.pktSpace[:defPktLen])) + _, err := e.dst.Write(pkt.Bytes(e.pktSpace[:defPktSize])) if err != nil { return err } diff --git a/stream/rtp/rtp.go b/stream/rtp/rtp.go index 5d6e0ace..34e30fd3 100644 --- a/stream/rtp/rtp.go +++ b/stream/rtp/rtp.go @@ -33,10 +33,10 @@ for rtp-h264 and rtp standards. package rtp const ( - rtpVer = 2 - headLen = 3 * 4 - defPayloadLen = 7 * 188 - defPktLen = headLen + defPayloadLen + rtpVer = 2 + headSize = 3 * 4 + defPayloadSize = 7 * 188 + defPktSize = headSize + defPayloadSize ) // Pkt provides fields consistent with RFC3550 definition of an rtp packet @@ -57,10 +57,10 @@ type Pkt struct { // Bytes provides a byte slice of the packet func (p *Pkt) Bytes(buf []byte) []byte { - if buf == nil || len(buf) != defPktLen { - buf = make([]byte, headLen, defPktLen) + if buf == nil || len(buf) != defPktSize { + buf = make([]byte, headSize, defPktSize) } - buf = buf[:headLen] + buf = buf[:headSize] if p.V == 0 { p.V = rtpVer } From cb762c04c680681cfa511f1ac8e989ca99e9c032 Mon Sep 17 00:00:00 2001 From: saxon Date: Sun, 13 Jan 2019 16:40:25 +1030 Subject: [PATCH 4/5] rtp: set defPayloadSIze to sendLen which is 7 *188 and also commented consts --- stream/rtp/rtp.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stream/rtp/rtp.go b/stream/rtp/rtp.go index 34e30fd3..37d6bbc0 100644 --- a/stream/rtp/rtp.go +++ b/stream/rtp/rtp.go @@ -34,9 +34,9 @@ package rtp const ( rtpVer = 2 - headSize = 3 * 4 - defPayloadSize = 7 * 188 - defPktSize = headSize + defPayloadSize + headSize = 3 * 4 // Header suze of an rtp packet. + defPayloadSize = sendLen // Default payload size for the rtp packet. + defPktSize = headSize + defPayloadSize // Default packet size is header size + payload size. ) // Pkt provides fields consistent with RFC3550 definition of an rtp packet From 7f140baf22bb37cf8c95b858fbaceb4a7d1fc20a Mon Sep 17 00:00:00 2001 From: saxon Date: Sun, 13 Jan 2019 16:42:30 +1030 Subject: [PATCH 5/5] rtp: corrected error in comment --- stream/rtp/rtp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stream/rtp/rtp.go b/stream/rtp/rtp.go index 37d6bbc0..47f4a91b 100644 --- a/stream/rtp/rtp.go +++ b/stream/rtp/rtp.go @@ -34,7 +34,7 @@ package rtp const ( rtpVer = 2 - headSize = 3 * 4 // Header suze of an rtp packet. + headSize = 3 * 4 // Header size of an rtp packet. defPayloadSize = sendLen // Default payload size for the rtp packet. defPktSize = headSize + defPayloadSize // Default packet size is header size + payload size. )