From 757564a2ed16abdbcb3b5b166908aa3e9a36f404 Mon Sep 17 00:00:00 2001 From: Saxon Date: Fri, 12 Apr 2019 18:02:27 +0930 Subject: [PATCH] protocol/rtcp: wrote body for Timestamp and added testing. --- protocol/rtcp/parse.go | 26 ++++++++++++++++++++++++++ protocol/rtcp/parse_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 protocol/rtcp/parse.go create mode 100644 protocol/rtcp/parse_test.go diff --git a/protocol/rtcp/parse.go b/protocol/rtcp/parse.go new file mode 100644 index 00000000..9795bbf6 --- /dev/null +++ b/protocol/rtcp/parse.go @@ -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 +} diff --git a/protocol/rtcp/parse_test.go b/protocol/rtcp/parse_test.go new file mode 100644 index 00000000..8692f73c --- /dev/null +++ b/protocol/rtcp/parse_test.go @@ -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) + } +}