mirror of https://bitbucket.org/ausocean/av.git
protocol/rtp/rtp.go: improve naming of Packet struct fields
This commit is contained in:
parent
5d20e4ea85
commit
8f881364ad
|
@ -74,7 +74,7 @@ func TestReceive(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create expected data and apply operation if there is one.
|
// 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.
|
// Compare.
|
||||||
got := buf[:n]
|
got := buf[:n]
|
||||||
|
@ -101,7 +101,7 @@ func TestReceive(t *testing.T) {
|
||||||
|
|
||||||
// Send packets to the client.
|
// Send packets to the client.
|
||||||
for i := 0; i < packetsToSend; i++ {
|
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)
|
_, err := conn.Write(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr <- fmt.Errorf("could not write packet to conn, failed with err: %w\n", err)
|
serverErr <- fmt.Errorf("could not write packet to conn, failed with err: %w\n", err)
|
||||||
|
|
|
@ -98,13 +98,11 @@ func min(a, b int) int {
|
||||||
// writes to the io.Writer given in NewEncoder
|
// writes to the io.Writer given in NewEncoder
|
||||||
func (e *Encoder) Encode(payload []byte) error {
|
func (e *Encoder) Encode(payload []byte) error {
|
||||||
pkt := Packet{
|
pkt := Packet{
|
||||||
V: rtpVer, // version
|
Version: rtpVer, // version
|
||||||
X: false, // header extension
|
CSRCCount: 0, // CSRC count
|
||||||
CC: 0, // CSRC count
|
PacketType: defaultPktType, // 33 for mpegts
|
||||||
M: false, // NOTE: need to check if this works (decoders should ignore this)
|
Sync: e.nxtSeqNo(), // sequence number
|
||||||
PT: defaultPktType, // 33 for mpegts
|
Timestamp: e.nxtTimestamp(), // timestamp
|
||||||
SN: e.nxtSeqNo(), // sequence number
|
|
||||||
TS: e.nxtTimestamp(), // timestamp
|
|
||||||
SSRC: e.ssrc, // source identifier
|
SSRC: e.ssrc, // source identifier
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
Padding: nil,
|
Padding: nil,
|
||||||
|
|
|
@ -35,7 +35,7 @@ import (
|
||||||
// TestVersion checks that we can correctly get the version from an RTP packet.
|
// TestVersion checks that we can correctly get the version from an RTP packet.
|
||||||
func TestVersion(t *testing.T) {
|
func TestVersion(t *testing.T) {
|
||||||
const expect = 1
|
const expect = 1
|
||||||
got := version((&Packet{V: expect}).Bytes(nil))
|
got := version((&Packet{Version: expect}).Bytes(nil))
|
||||||
if got != expect {
|
if got != expect {
|
||||||
t.Errorf("unexpected version for RTP packet. Got: %v\n Want: %v\n", 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
|
const ver, expect = 2, 2
|
||||||
|
|
||||||
pkt := (&Packet{
|
pkt := (&Packet{
|
||||||
V: ver,
|
Version: ver,
|
||||||
CC: expect,
|
CSRCCount: expect,
|
||||||
CSRC: make([][4]byte, expect),
|
CSRC: make([][4]byte, expect),
|
||||||
}).Bytes(nil)
|
}).Bytes(nil)
|
||||||
|
|
||||||
|
@ -65,8 +65,8 @@ func TestHasExt(t *testing.T) {
|
||||||
|
|
||||||
// First check for when there is an extension field.
|
// First check for when there is an extension field.
|
||||||
pkt := &Packet{
|
pkt := &Packet{
|
||||||
V: ver,
|
Version: ver,
|
||||||
X: true,
|
ExtHeadFlag: true,
|
||||||
Extension: ExtensionHeader{
|
Extension: ExtensionHeader{
|
||||||
ID: 0,
|
ID: 0,
|
||||||
Header: make([][4]byte, 0),
|
Header: make([][4]byte, 0),
|
||||||
|
@ -79,7 +79,7 @@ func TestHasExt(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now check when there is not an extension field.
|
// Now check when there is not an extension field.
|
||||||
pkt.X = false
|
pkt.ExtHeadFlag = false
|
||||||
got = hasExt(pkt.Bytes(nil))
|
got = hasExt(pkt.Bytes(nil))
|
||||||
if got {
|
if got {
|
||||||
t.Error("did not expect to have extension indicator as true")
|
t.Error("did not expect to have extension indicator as true")
|
||||||
|
@ -94,20 +94,20 @@ func TestPayload(t *testing.T) {
|
||||||
|
|
||||||
testPkts := [][]byte{
|
testPkts := [][]byte{
|
||||||
(&Packet{
|
(&Packet{
|
||||||
V: ver,
|
Version: ver,
|
||||||
Payload: expect,
|
Payload: expect,
|
||||||
}).Bytes(nil),
|
}).Bytes(nil),
|
||||||
|
|
||||||
(&Packet{
|
(&Packet{
|
||||||
V: ver,
|
Version: ver,
|
||||||
CC: 3,
|
CSRCCount: 3,
|
||||||
CSRC: make([][4]byte, 3),
|
CSRC: make([][4]byte, 3),
|
||||||
Payload: expect,
|
Payload: expect,
|
||||||
}).Bytes(nil),
|
}).Bytes(nil),
|
||||||
|
|
||||||
(&Packet{
|
(&Packet{
|
||||||
V: ver,
|
Version: ver,
|
||||||
X: true,
|
ExtHeadFlag: true,
|
||||||
Extension: ExtensionHeader{
|
Extension: ExtensionHeader{
|
||||||
ID: 0,
|
ID: 0,
|
||||||
Header: make([][4]byte, 3),
|
Header: make([][4]byte, 3),
|
||||||
|
@ -116,8 +116,8 @@ func TestPayload(t *testing.T) {
|
||||||
}).Bytes(nil),
|
}).Bytes(nil),
|
||||||
|
|
||||||
(&Packet{
|
(&Packet{
|
||||||
V: ver,
|
Version: ver,
|
||||||
CC: 3,
|
CSRCCount: 3,
|
||||||
CSRC: make([][4]byte, 3),
|
CSRC: make([][4]byte, 3),
|
||||||
Extension: ExtensionHeader{
|
Extension: ExtensionHeader{
|
||||||
ID: 0,
|
ID: 0,
|
||||||
|
|
|
@ -47,14 +47,14 @@ const (
|
||||||
// Pkt provides fields consistent with RFC3550 definition of an rtp packet
|
// 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
|
// The padding indicator does not need to be set manually, only the padding length
|
||||||
type Packet struct {
|
type Packet struct {
|
||||||
V uint8 // Version (currently 2).
|
Version uint8 // Version (currently 2).
|
||||||
p bool // Padding indicator (0 => padding, 1 => padding).
|
PaddingFlag bool // Padding indicator (0 => padding, 1 => padding).
|
||||||
X bool // Extension header indicator.
|
ExtHeadFlag bool // Extension header indicator.
|
||||||
CC uint8 // CSRC count.
|
CSRCCount uint8 // CSRC count.
|
||||||
M bool // Marker bit.
|
Marker bool // Marker bit.
|
||||||
PT uint8 // Packet type.
|
PacketType uint8 // Packet type.
|
||||||
SN uint16 // Synch number.
|
Sync uint16 // Sync number.
|
||||||
TS uint32 // Timestamp.
|
Timestamp uint32 // Timestamp.
|
||||||
SSRC uint32 // Synchronisation source identifier.
|
SSRC uint32 // Synchronisation source identifier.
|
||||||
CSRC [][4]byte // Contributing source identifier.
|
CSRC [][4]byte // Contributing source identifier.
|
||||||
Extension ExtensionHeader // Header extension.
|
Extension ExtensionHeader // Header extension.
|
||||||
|
@ -72,10 +72,10 @@ type ExtensionHeader struct {
|
||||||
func (p *Packet) Bytes(buf []byte) []byte {
|
func (p *Packet) Bytes(buf []byte) []byte {
|
||||||
// Calculate the required length for the RTP packet.
|
// Calculate the required length for the RTP packet.
|
||||||
headerExtensionLen := 0
|
headerExtensionLen := 0
|
||||||
if p.X {
|
if p.ExtHeadFlag {
|
||||||
headerExtensionLen = int(4 + 4*len(p.Extension.Header))
|
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.
|
// Create new space if no buffer is given, or it doesn't have sufficient capacity.
|
||||||
if buf == nil || requiredPktLen > cap(buf) {
|
if buf == nil || requiredPktLen > cap(buf) {
|
||||||
|
@ -84,27 +84,27 @@ func (p *Packet) Bytes(buf []byte) []byte {
|
||||||
buf = buf[:requiredPktLen]
|
buf = buf[:requiredPktLen]
|
||||||
|
|
||||||
// Start encoding fields into the buffer.
|
// Start encoding fields into the buffer.
|
||||||
buf[0] = p.V<<6 | asByte(p.p)<<5 | asByte(p.X)<<4 | p.CC
|
buf[0] = p.Version<<6 | asByte(p.PaddingFlag)<<5 | asByte(p.ExtHeadFlag)<<4 | p.CSRCCount
|
||||||
buf[1] = asByte(p.M)<<7 | p.PT
|
buf[1] = asByte(p.Marker)<<7 | p.PacketType
|
||||||
binary.BigEndian.PutUint16(buf[2:4], p.SN)
|
binary.BigEndian.PutUint16(buf[2:4], p.Sync)
|
||||||
binary.BigEndian.PutUint32(buf[4:8], p.TS)
|
binary.BigEndian.PutUint32(buf[4:8], p.Timestamp)
|
||||||
binary.BigEndian.PutUint32(buf[8:12], p.SSRC)
|
binary.BigEndian.PutUint32(buf[8:12], p.SSRC)
|
||||||
|
|
||||||
// If there is a CSRC count, add the CSRC to the buffer.
|
// If there is a CSRC count, add the CSRC to the buffer.
|
||||||
if p.CC != 0 {
|
if p.CSRCCount != 0 {
|
||||||
if p.CC != uint8(len(p.CSRC)) {
|
if p.CSRCCount != uint8(len(p.CSRC)) {
|
||||||
panic("CSRC count in RTP packet is incorrect")
|
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][:])
|
copy(buf[12+i*4:], p.CSRC[i][:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is our current index for writing to the buffer.
|
// 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 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)
|
binary.BigEndian.PutUint16(buf[idx:idx+2], p.Extension.ID)
|
||||||
idx += 2
|
idx += 2
|
||||||
binary.BigEndian.PutUint16(buf[idx:idx+2], uint16(len(p.Extension.Header)))
|
binary.BigEndian.PutUint16(buf[idx:idx+2], uint16(len(p.Extension.Header)))
|
||||||
|
|
|
@ -42,14 +42,11 @@ var rtpTests = []struct {
|
||||||
{
|
{
|
||||||
num: 1,
|
num: 1,
|
||||||
pkt: Packet{
|
pkt: Packet{
|
||||||
V: 2,
|
Version: 2,
|
||||||
p: false,
|
CSRCCount: 0,
|
||||||
X: false,
|
PacketType: 6,
|
||||||
CC: 0,
|
Sync: 167,
|
||||||
M: false,
|
Timestamp: 160,
|
||||||
PT: 6,
|
|
||||||
SN: 167,
|
|
||||||
TS: 160,
|
|
||||||
SSRC: 10,
|
SSRC: 10,
|
||||||
Payload: []byte{
|
Payload: []byte{
|
||||||
0x00, 0x01, 0x07, 0xf0,
|
0x00, 0x01, 0x07, 0xf0,
|
||||||
|
@ -68,14 +65,12 @@ var rtpTests = []struct {
|
||||||
{
|
{
|
||||||
num: 2,
|
num: 2,
|
||||||
pkt: Packet{
|
pkt: Packet{
|
||||||
V: 2,
|
Version: 2,
|
||||||
p: true,
|
PaddingFlag: true,
|
||||||
X: false,
|
CSRCCount: 0,
|
||||||
CC: 0,
|
PacketType: 6,
|
||||||
M: false,
|
Sync: 167,
|
||||||
PT: 6,
|
Timestamp: 160,
|
||||||
SN: 167,
|
|
||||||
TS: 160,
|
|
||||||
SSRC: 10,
|
SSRC: 10,
|
||||||
Payload: []byte{
|
Payload: []byte{
|
||||||
0x00, 0x01, 0x07, 0xf0,
|
0x00, 0x01, 0x07, 0xf0,
|
||||||
|
@ -102,14 +97,12 @@ var rtpTests = []struct {
|
||||||
{
|
{
|
||||||
num: 3,
|
num: 3,
|
||||||
pkt: Packet{
|
pkt: Packet{
|
||||||
V: 2,
|
Version: 2,
|
||||||
p: true,
|
PaddingFlag: true,
|
||||||
X: false,
|
CSRCCount: 2,
|
||||||
CC: 2,
|
PacketType: 6,
|
||||||
M: false,
|
Sync: 167,
|
||||||
PT: 6,
|
Timestamp: 160,
|
||||||
SN: 167,
|
|
||||||
TS: 160,
|
|
||||||
SSRC: 10,
|
SSRC: 10,
|
||||||
CSRC: [][4]byte{
|
CSRC: [][4]byte{
|
||||||
{0x01, 0x02, 0x03, 0x04},
|
{0x01, 0x02, 0x03, 0x04},
|
||||||
|
@ -142,14 +135,13 @@ var rtpTests = []struct {
|
||||||
{
|
{
|
||||||
num: 4,
|
num: 4,
|
||||||
pkt: Packet{
|
pkt: Packet{
|
||||||
V: 2,
|
Version: 2,
|
||||||
p: true,
|
PaddingFlag: true,
|
||||||
X: true,
|
ExtHeadFlag: true,
|
||||||
CC: 2,
|
CSRCCount: 2,
|
||||||
M: false,
|
PacketType: 6,
|
||||||
PT: 6,
|
Sync: 167,
|
||||||
SN: 167,
|
Timestamp: 160,
|
||||||
TS: 160,
|
|
||||||
SSRC: 10,
|
SSRC: 10,
|
||||||
CSRC: [][4]byte{
|
CSRC: [][4]byte{
|
||||||
{0x01, 0x02, 0x03, 0x04},
|
{0x01, 0x02, 0x03, 0x04},
|
||||||
|
|
Loading…
Reference in New Issue