Merged in rtp-field-names (pull request #417)

protocol/rtp/rtp.go: improve naming of Packet struct fields

Approved-by: Trek Hopton <trek.hopton@gmail.com>
This commit is contained in:
Saxon Milton 2020-05-20 03:45:56 +00:00
commit 7fb61edb0d
5 changed files with 79 additions and 89 deletions

View File

@ -74,7 +74,7 @@ func TestReceive(t *testing.T) {
}
// Create expected data and apply operation if there is one.
expect := (&Packet{V: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil)
expect := (&Packet{Version: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil)
// Compare.
got := buf[:n]
@ -101,7 +101,7 @@ func TestReceive(t *testing.T) {
// Send packets to the client.
for i := 0; i < packetsToSend; i++ {
p := (&Packet{V: rtpVer, Payload: []byte{byte(i)}}).Bytes(nil)
p := (&Packet{Version: rtpVer, Payload: []byte{byte(i)}}).Bytes(nil)
_, err := conn.Write(p)
if err != nil {
serverErr <- fmt.Errorf("could not write packet to conn, failed with err: %w\n", err)

View File

@ -98,13 +98,11 @@ func min(a, b int) int {
// writes to the io.Writer given in NewEncoder
func (e *Encoder) Encode(payload []byte) error {
pkt := Packet{
V: rtpVer, // version
X: false, // header extension
CC: 0, // CSRC count
M: false, // NOTE: need to check if this works (decoders should ignore this)
PT: defaultPktType, // 33 for mpegts
SN: e.nxtSeqNo(), // sequence number
TS: e.nxtTimestamp(), // timestamp
Version: rtpVer, // version
CSRCCount: 0, // CSRC count
PacketType: defaultPktType, // 33 for mpegts
Sync: e.nxtSeqNo(), // sequence number
Timestamp: e.nxtTimestamp(), // timestamp
SSRC: e.ssrc, // source identifier
Payload: payload,
Padding: nil,

View File

@ -35,7 +35,7 @@ import (
// TestVersion checks that we can correctly get the version from an RTP packet.
func TestVersion(t *testing.T) {
const expect = 1
got := version((&Packet{V: expect}).Bytes(nil))
got := version((&Packet{Version: expect}).Bytes(nil))
if got != expect {
t.Errorf("unexpected version for RTP packet. Got: %v\n Want: %v\n", got, expect)
}
@ -47,8 +47,8 @@ func TestCsrcCount(t *testing.T) {
const ver, expect = 2, 2
pkt := (&Packet{
V: ver,
CC: expect,
Version: ver,
CSRCCount: expect,
CSRC: make([][4]byte, expect),
}).Bytes(nil)
@ -65,8 +65,8 @@ func TestHasExt(t *testing.T) {
// First check for when there is an extension field.
pkt := &Packet{
V: ver,
X: true,
Version: ver,
ExtHeadFlag: true,
Extension: ExtensionHeader{
ID: 0,
Header: make([][4]byte, 0),
@ -79,7 +79,7 @@ func TestHasExt(t *testing.T) {
}
// Now check when there is not an extension field.
pkt.X = false
pkt.ExtHeadFlag = false
got = hasExt(pkt.Bytes(nil))
if got {
t.Error("did not expect to have extension indicator as true")
@ -94,20 +94,20 @@ func TestPayload(t *testing.T) {
testPkts := [][]byte{
(&Packet{
V: ver,
Version: ver,
Payload: expect,
}).Bytes(nil),
(&Packet{
V: ver,
CC: 3,
Version: ver,
CSRCCount: 3,
CSRC: make([][4]byte, 3),
Payload: expect,
}).Bytes(nil),
(&Packet{
V: ver,
X: true,
Version: ver,
ExtHeadFlag: true,
Extension: ExtensionHeader{
ID: 0,
Header: make([][4]byte, 3),
@ -116,8 +116,8 @@ func TestPayload(t *testing.T) {
}).Bytes(nil),
(&Packet{
V: ver,
CC: 3,
Version: ver,
CSRCCount: 3,
CSRC: make([][4]byte, 3),
Extension: ExtensionHeader{
ID: 0,

View File

@ -47,14 +47,14 @@ const (
// Pkt provides fields consistent with RFC3550 definition of an rtp packet
// The padding indicator does not need to be set manually, only the padding length
type Packet struct {
V uint8 // Version (currently 2).
p bool // Padding indicator (0 => padding, 1 => padding).
X bool // Extension header indicator.
CC uint8 // CSRC count.
M bool // Marker bit.
PT uint8 // Packet type.
SN uint16 // Synch number.
TS uint32 // Timestamp.
Version uint8 // Version (currently 2).
PaddingFlag bool // Padding indicator (0 => padding, 1 => padding).
ExtHeadFlag bool // Extension header indicator.
CSRCCount uint8 // CSRC count.
Marker bool // Marker bit.
PacketType uint8 // Packet type.
Sync uint16 // Sync number.
Timestamp uint32 // Timestamp.
SSRC uint32 // Synchronisation source identifier.
CSRC [][4]byte // Contributing source identifier.
Extension ExtensionHeader // Header extension.
@ -72,10 +72,10 @@ type ExtensionHeader struct {
func (p *Packet) Bytes(buf []byte) []byte {
// Calculate the required length for the RTP packet.
headerExtensionLen := 0
if p.X {
if p.ExtHeadFlag {
headerExtensionLen = int(4 + 4*len(p.Extension.Header))
}
requiredPktLen := defaultHeadSize + int(4*p.CC) + headerExtensionLen + len(p.Payload) + len(p.Padding)
requiredPktLen := defaultHeadSize + int(4*p.CSRCCount) + headerExtensionLen + len(p.Payload) + len(p.Padding)
// Create new space if no buffer is given, or it doesn't have sufficient capacity.
if buf == nil || requiredPktLen > cap(buf) {
@ -84,27 +84,27 @@ func (p *Packet) Bytes(buf []byte) []byte {
buf = buf[:requiredPktLen]
// Start encoding fields into the buffer.
buf[0] = p.V<<6 | asByte(p.p)<<5 | asByte(p.X)<<4 | p.CC
buf[1] = asByte(p.M)<<7 | p.PT
binary.BigEndian.PutUint16(buf[2:4], p.SN)
binary.BigEndian.PutUint32(buf[4:8], p.TS)
buf[0] = p.Version<<6 | asByte(p.PaddingFlag)<<5 | asByte(p.ExtHeadFlag)<<4 | p.CSRCCount
buf[1] = asByte(p.Marker)<<7 | p.PacketType
binary.BigEndian.PutUint16(buf[2:4], p.Sync)
binary.BigEndian.PutUint32(buf[4:8], p.Timestamp)
binary.BigEndian.PutUint32(buf[8:12], p.SSRC)
// If there is a CSRC count, add the CSRC to the buffer.
if p.CC != 0 {
if p.CC != uint8(len(p.CSRC)) {
if p.CSRCCount != 0 {
if p.CSRCCount != uint8(len(p.CSRC)) {
panic("CSRC count in RTP packet is incorrect")
}
for i := 0; i < int(p.CC); i++ {
for i := 0; i < int(p.CSRCCount); i++ {
copy(buf[12+i*4:], p.CSRC[i][:])
}
}
// This is our current index for writing to the buffer.
idx := int(12 + 4*p.CC)
idx := int(12 + 4*p.CSRCCount)
// If there is an extension field, add this to the buffer.
if p.X {
if p.ExtHeadFlag {
binary.BigEndian.PutUint16(buf[idx:idx+2], p.Extension.ID)
idx += 2
binary.BigEndian.PutUint16(buf[idx:idx+2], uint16(len(p.Extension.Header)))

View File

@ -42,14 +42,11 @@ var rtpTests = []struct {
{
num: 1,
pkt: Packet{
V: 2,
p: false,
X: false,
CC: 0,
M: false,
PT: 6,
SN: 167,
TS: 160,
Version: 2,
CSRCCount: 0,
PacketType: 6,
Sync: 167,
Timestamp: 160,
SSRC: 10,
Payload: []byte{
0x00, 0x01, 0x07, 0xf0,
@ -68,14 +65,12 @@ var rtpTests = []struct {
{
num: 2,
pkt: Packet{
V: 2,
p: true,
X: false,
CC: 0,
M: false,
PT: 6,
SN: 167,
TS: 160,
Version: 2,
PaddingFlag: true,
CSRCCount: 0,
PacketType: 6,
Sync: 167,
Timestamp: 160,
SSRC: 10,
Payload: []byte{
0x00, 0x01, 0x07, 0xf0,
@ -102,14 +97,12 @@ var rtpTests = []struct {
{
num: 3,
pkt: Packet{
V: 2,
p: true,
X: false,
CC: 2,
M: false,
PT: 6,
SN: 167,
TS: 160,
Version: 2,
PaddingFlag: true,
CSRCCount: 2,
PacketType: 6,
Sync: 167,
Timestamp: 160,
SSRC: 10,
CSRC: [][4]byte{
{0x01, 0x02, 0x03, 0x04},
@ -142,14 +135,13 @@ var rtpTests = []struct {
{
num: 4,
pkt: Packet{
V: 2,
p: true,
X: true,
CC: 2,
M: false,
PT: 6,
SN: 167,
TS: 160,
Version: 2,
PaddingFlag: true,
ExtHeadFlag: true,
CSRCCount: 2,
PacketType: 6,
Sync: 167,
Timestamp: 160,
SSRC: 10,
CSRC: [][4]byte{
{0x01, 0x02, 0x03, 0x04},