protocol/rtcp: wrote body for Timestamp and added testing.

This commit is contained in:
Saxon 2019-04-12 18:02:27 +09:30
parent 8f452e1155
commit 757564a2ed
2 changed files with 60 additions and 0 deletions

26
protocol/rtcp/parse.go Normal file
View File

@ -0,0 +1,26 @@
package rtcp
import (
"encoding/binary"
"errors"
)
// Timestamp gets the timestamp from a receiver report and returns as the most
// significant word, and the least significant word. If the given bytes do not
// represent a valid receiver report, and error is returned.
func Timestamp(buf []byte) (msw, lsw uint32, err error) {
// First check version of rtcp
if (buf[0] & 0xc0 >> 6) != 2 {
return 0, 0, errors.New("incompatible RTCP version")
}
// Check type of packet
if buf[1] != typeSenderReport {
return 0, 0, errors.New("rtcp packet is not of sender report type")
}
msw = binary.BigEndian.Uint32(buf[8:])
lsw = binary.BigEndian.Uint32(buf[12:])
return
}

View File

@ -0,0 +1,34 @@
package rtcp
import (
"testing"
)
// TestTimestamp checks that Timestamp correctly returns the most signicicant
// word, and least signiciant word, of a receiver report timestamp.
func TestTimestamp(t *testing.T) {
const expectedMSW = 2209003992
const expectedLSW = 1956821460
report := []byte{
0x80, 0xc8, 0x00, 0x06,
0x6f, 0xad, 0x40, 0xc6,
0x83, 0xaa, 0xb9, 0xd8, // Most significant word of timestamp (2209003992)
0x74, 0xa2, 0xb9, 0xd4, // Least significant word of timestamp (1956821460)
0x4b, 0x1c, 0x5a, 0xa5,
0x00, 0x00, 0x00, 0x66,
0x00, 0x01, 0xc2, 0xc5,
}
msw, lsw, err := Timestamp(report)
if err != nil {
t.Fatalf("did not expect error: %v", err)
}
if msw != expectedMSW {
t.Errorf("most significant word of timestamp is not what's expected. \nGot: %v\n Want: %v\n", msw, expectedMSW)
}
if lsw != expectedLSW {
t.Errorf("least significant word of timestamp is not what's expected. \nGot: %v\n Want: %v\n", lsw, expectedLSW)
}
}