protocol/rtcp: wrote test for SourceDescription.Bytes() and made modifiations to make it pass.

This commit is contained in:
Saxon 2019-04-12 11:14:07 +09:30
parent 22d71f8a57
commit db81547962
2 changed files with 52 additions and 10 deletions

View File

@ -8,7 +8,13 @@ import (
// RTCP packet types.
const (
typeReceiverReport = 201
typeReceiverReport = 201
typeSourceDescription = 202
)
// SDES Item types.
const (
typeCName = 1
)
const (
@ -18,6 +24,7 @@ const (
type ReceiverReport struct {
Header
SenderSSRC uint32
Blocks []ReportBlock
Extensions [][4]byte
}
@ -28,6 +35,7 @@ func (r *ReceiverReport) Bytes() []byte {
l = 1 + reportBlockSize*len(r.Blocks) + len(r.Extensions)
r.writeHeader(buf, l)
binary.BigEndian.PutUint32(buf[4:], r.SenderSSRC)
idx := 8
for _, b := range r.Blocks {
@ -62,10 +70,14 @@ type SourceDescription struct {
func (d *SourceDescription) Bytes() []byte {
bodyLen := d.bodyLen()
l := 8 + bodyLen
rem := bodyLen % 4
if rem != 0 {
bodyLen += 4 - rem
}
l := 4 + bodyLen
buf := make([]byte, l)
d.writeHeader(buf, bodyLen/4)
idx := 8
idx := 4
for _, c := range d.Chunks {
binary.BigEndian.PutUint32(buf[idx:], c.SSRC)
idx += 4
@ -90,11 +102,10 @@ func (d *SourceDescription) bodyLen() int {
}
type Header struct {
Version uint8 // RTCP version.
Padding bool // Padding indicator.
ReportCount uint8 // Number of reports contained.
Type uint8 // Type of RTCP packet.
SenderSSRC uint32 // Source identifier.
Version uint8 // RTCP version.
Padding bool // Padding indicator.
ReportCount uint8 // Number of reports contained.
Type uint8 // Type of RTCP packet.
}
type ReportBlock struct {
@ -129,7 +140,6 @@ func (h Header) writeHeader(buf []byte, l int) {
buf[0] = h.Version<<6 | asByte(h.Padding)<<5 | 0x1f&h.ReportCount
buf[1] = h.Type
binary.BigEndian.PutUint16(buf[2:], uint16(l))
binary.BigEndian.PutUint32(buf[4:], h.SenderSSRC)
}
func asByte(b bool) byte {

View File

@ -24,8 +24,8 @@ func TestReceiverReportBytes(t *testing.T) {
Padding: false,
ReportCount: 1,
Type: typeReceiverReport,
SenderSSRC: 3605043418,
},
SenderSSRC: 3605043418,
Blocks: []ReportBlock{
ReportBlock{
SSRC: 1873625286,
@ -49,5 +49,37 @@ func TestReceiverReportBytes(t *testing.T) {
}
func TestSourceDescriptionBytes(t *testing.T) {
expect := []byte{
0x81, 0xca, 0x00, 0x04,
0xd6, 0xe0, 0x98, 0xda,
0x01, 0x08, 0x73, 0x61,
0x78, 0x6f, 0x6e, 0x2d,
0x70, 0x63, 0x00, 0x00,
}
description := SourceDescription{
Header: Header{
Version: 2,
Padding: false,
ReportCount: 1,
Type: typeSourceDescription,
},
Chunks: []Chunk{
Chunk{
SSRC: 3605043418,
Items: []SDESItem{
SDESItem{
Type: typeCName,
Text: []byte("saxon-pc"),
},
},
},
},
}
got := description.Bytes()
t.Logf("Got: %v\n", got)
t.Logf("Expect: %v\n", expect)
if !bytes.Equal(got, expect) {
t.Errorf("Did not get expected result.\nGot: %v\n Want: %v\n", got, expect)
}
}