mirror of https://bitbucket.org/ausocean/av.git
rtcp/protocol: tried to make Bytes funcs and client formation of payload more efficient
This commit is contained in:
parent
2669862ced
commit
dca007a5ba
|
@ -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()
|
||||
|
|
|
@ -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:
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue