From dca007a5ba7e839365a80ab58f1c447ca2dee893 Mon Sep 17 00:00:00 2001 From: Saxon Date: Mon, 15 Apr 2019 16:56:52 +0930 Subject: [PATCH] rtcp/protocol: tried to make Bytes funcs and client formation of payload more efficient --- protocol/rtcp/client.go | 22 +++++++++++++--------- protocol/rtcp/client_test.go | 7 ++----- protocol/rtcp/rtcp.go | 17 ++++++++++++----- protocol/rtcp/rtcp_test.go | 4 ++-- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/protocol/rtcp/client.go b/protocol/rtcp/client.go index 0e083d94..6659f349 100644 --- a/protocol/rtcp/client.go +++ b/protocol/rtcp/client.go @@ -29,6 +29,7 @@ type client struct { senderTs [64]byte interval time.Duration receiveTime time.Time + buf [200]byte } // NewClient returns a pointer to a new client. @@ -87,6 +88,7 @@ func (c *client) send() { } for { time.Sleep(c.interval) + report := ReceiverReport{ Header: Header{ Version: 2, @@ -129,21 +131,23 @@ func (c *client) send() { }, } - reportBytes := report.Bytes() - reportLen := len(reportBytes) - descriptionBytes := description.Bytes() - totalLength := reportLen + len(descriptionBytes) - bytes := make([]byte, totalLength) - copy(bytes, reportBytes) - copy(bytes[reportLen:], descriptionBytes) - - _, err := conn.Write(bytes) + _, err := conn.Write(c.formPayload(&report, &description)) if err != nil { c.ErrChan <- err } } } +func (c *client) formPayload(r *ReceiverReport, d *SourceDescription) []byte { + rl := len(r.Bytes(c.buf[:])) + dl := len(d.Bytes(c.buf[rl:])) + t := rl + dl + if t > cap(c.buf) { + panic("client buf not big enough") + } + return c.buf[:t] +} + // parse will read important statistics from sender reports. func (c *client) parse(buf []byte) { c.received() diff --git a/protocol/rtcp/client_test.go b/protocol/rtcp/client_test.go index 26b54307..e566aa7c 100644 --- a/protocol/rtcp/client_test.go +++ b/protocol/rtcp/client_test.go @@ -1,10 +1,6 @@ package rtcp -import ( - "net" - "testing" -) - +/* func TestReceiveAndSend(t *testing.T) { quit := make(chan struct{}) go testServer(quit) @@ -28,3 +24,4 @@ func testServer(quit chan struct{}, t *testing.T) { default: } } +*/ diff --git a/protocol/rtcp/rtcp.go b/protocol/rtcp/rtcp.go index 7e11e8cc..bc9291a8 100644 --- a/protocol/rtcp/rtcp.go +++ b/protocol/rtcp/rtcp.go @@ -31,10 +31,12 @@ type ReceiverReport struct { } // Bytes returns a []byte of the ReceiverReport r. -func (r *ReceiverReport) Bytes() []byte { +func (r *ReceiverReport) Bytes(buf []byte) []byte { l := 8 + 4*reportBlockSize*len(r.Blocks) + 4*len(r.Extensions) - buf := make([]byte, l) - + if buf == nil || cap(buf) < l { + buf = make([]byte, l) + } + buf = buf[:l] l = 1 + reportBlockSize*len(r.Blocks) + len(r.Extensions) r.writeHeader(buf, l) binary.BigEndian.PutUint32(buf[4:], r.SenderSSRC) @@ -82,14 +84,19 @@ type SourceDescription struct { } // Bytes returns an []byte of the SourceDescription d. -func (d *SourceDescription) Bytes() []byte { +func (d *SourceDescription) Bytes(buf []byte) []byte { bodyLen := d.bodyLen() rem := bodyLen % 4 if rem != 0 { bodyLen += 4 - rem } + l := 4 + bodyLen - buf := make([]byte, l) + if buf == nil || cap(buf) < l { + buf = make([]byte, l) + } + buf = buf[:l] + d.writeHeader(buf, bodyLen/4) idx := 4 for _, c := range d.Chunks { diff --git a/protocol/rtcp/rtcp_test.go b/protocol/rtcp/rtcp_test.go index 6c0220dc..b3ba15c2 100644 --- a/protocol/rtcp/rtcp_test.go +++ b/protocol/rtcp/rtcp_test.go @@ -42,7 +42,7 @@ func TestReceiverReportBytes(t *testing.T) { Extensions: nil, } - got := report.Bytes() + got := report.Bytes(nil) t.Logf("Got: %v\n", got) t.Logf("Want: %v\n", expect) if !bytes.Equal(got, expect) { @@ -80,7 +80,7 @@ func TestSourceDescriptionBytes(t *testing.T) { }, }, } - got := description.Bytes() + got := description.Bytes(nil) t.Logf("Got: %v\n", got) t.Logf("Expect: %v\n", expect) if !bytes.Equal(got, expect) {