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

This commit is contained in:
Saxon 2020-05-20 12:58:53 +09:30
parent 5d20e4ea85
commit 8f881364ad
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,16 +98,14 @@ 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
SSRC: e.ssrc, // source identifier
Payload: payload,
Padding: nil,
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,
}
_, err := e.dst.Write(pkt.Bytes(e.pktSpace[:defPktSize]))
if err != 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,9 +47,9 @@ func TestCsrcCount(t *testing.T) {
const ver, expect = 2, 2
pkt := (&Packet{
V: ver,
CC: expect,
CSRC: make([][4]byte, expect),
Version: ver,
CSRCCount: expect,
CSRC: make([][4]byte, expect),
}).Bytes(nil)
got := csrcCount(pkt)
@ -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,
CSRC: make([][4]byte, 3),
Payload: expect,
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,9 +116,9 @@ func TestPayload(t *testing.T) {
}).Bytes(nil),
(&Packet{
V: ver,
CC: 3,
CSRC: make([][4]byte, 3),
Version: ver,
CSRCCount: 3,
CSRC: make([][4]byte, 3),
Extension: ExtensionHeader{
ID: 0,
Header: make([][4]byte, 3),

View File

@ -47,19 +47,19 @@ 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.
SSRC uint32 // Synchronisation source identifier.
CSRC [][4]byte // Contributing source identifier.
Extension ExtensionHeader // Header extension.
Payload []byte // Payload data.
Padding []byte // No of bytes of padding.
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.
Payload []byte // Payload data.
Padding []byte // No of bytes of padding.
}
// ExtensionHeader header provides fields for an RTP packet extension header.
@ -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,15 +42,12 @@ var rtpTests = []struct {
{
num: 1,
pkt: Packet{
V: 2,
p: false,
X: false,
CC: 0,
M: false,
PT: 6,
SN: 167,
TS: 160,
SSRC: 10,
Version: 2,
CSRCCount: 0,
PacketType: 6,
Sync: 167,
Timestamp: 160,
SSRC: 10,
Payload: []byte{
0x00, 0x01, 0x07, 0xf0,
0x56, 0x37, 0x0a, 0x0f,
@ -68,15 +65,13 @@ var rtpTests = []struct {
{
num: 2,
pkt: Packet{
V: 2,
p: true,
X: false,
CC: 0,
M: false,
PT: 6,
SN: 167,
TS: 160,
SSRC: 10,
Version: 2,
PaddingFlag: true,
CSRCCount: 0,
PacketType: 6,
Sync: 167,
Timestamp: 160,
SSRC: 10,
Payload: []byte{
0x00, 0x01, 0x07, 0xf0,
0x56, 0x37, 0x0a, 0x0f,
@ -102,15 +97,13 @@ var rtpTests = []struct {
{
num: 3,
pkt: Packet{
V: 2,
p: true,
X: false,
CC: 2,
M: false,
PT: 6,
SN: 167,
TS: 160,
SSRC: 10,
Version: 2,
PaddingFlag: true,
CSRCCount: 2,
PacketType: 6,
Sync: 167,
Timestamp: 160,
SSRC: 10,
CSRC: [][4]byte{
{0x01, 0x02, 0x03, 0x04},
{0x05, 0x06, 0x07, 0x08},
@ -142,15 +135,14 @@ var rtpTests = []struct {
{
num: 4,
pkt: Packet{
V: 2,
p: true,
X: true,
CC: 2,
M: false,
PT: 6,
SN: 167,
TS: 160,
SSRC: 10,
Version: 2,
PaddingFlag: true,
ExtHeadFlag: true,
CSRCCount: 2,
PacketType: 6,
Sync: 167,
Timestamp: 160,
SSRC: 10,
CSRC: [][4]byte{
{0x01, 0x02, 0x03, 0x04},
{0x05, 0x06, 0x07, 0x08},