mirror of
https://github.com/pion/webrtc.git
synced 2025-10-27 17:21:27 +08:00
RTCP: Add util/GetPadding
Simple helper function to do 32bit padding calculation Relates to #119
This commit is contained in:
@@ -15,3 +15,11 @@ func RandSeq(n int) string {
|
|||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPadding Returns the padding required to make the length a multiple of 4
|
||||||
|
func GetPadding(len int) int {
|
||||||
|
if len%4 == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return 4 - (len % 4)
|
||||||
|
}
|
||||||
|
|||||||
40
internal/util/util_test.go
Normal file
40
internal/util/util_test.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRandSeq(t *testing.T) {
|
||||||
|
if len(RandSeq(10)) != 10 {
|
||||||
|
t.Errorf("RandSeq return invalid length")
|
||||||
|
}
|
||||||
|
|
||||||
|
var isLetter = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
|
||||||
|
if !isLetter(RandSeq(10)) {
|
||||||
|
t.Errorf("RandSeq should be AlphaNumeric only")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetPadding(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
type testCase struct {
|
||||||
|
input int
|
||||||
|
result int
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []testCase{
|
||||||
|
{input: 0, result: 0},
|
||||||
|
{input: 1, result: 3},
|
||||||
|
{input: 2, result: 2},
|
||||||
|
{input: 3, result: 1},
|
||||||
|
{input: 4, result: 0},
|
||||||
|
{input: 100, result: 0},
|
||||||
|
{input: 500, result: 0},
|
||||||
|
}
|
||||||
|
for _, testCase := range cases {
|
||||||
|
assert.Equalf(GetPadding(testCase.input), testCase.result, "Test case returned wrong value for input %d", testCase.input)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@ package rtcp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
|
||||||
|
"github.com/pions/webrtc/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The Goodbye packet indicates that one or more sources are no longer active.
|
// The Goodbye packet indicates that one or more sources are no longer active.
|
||||||
@@ -49,10 +51,7 @@ func (g Goodbye) Marshal() ([]byte, error) {
|
|||||||
rawPacket = append(rawPacket, reason...)
|
rawPacket = append(rawPacket, reason...)
|
||||||
|
|
||||||
// align to 32-bit boundary
|
// align to 32-bit boundary
|
||||||
if len(rawPacket)%4 != 0 {
|
rawPacket = append(rawPacket, make([]byte, util.GetPadding(len(rawPacket)))...)
|
||||||
padCount := 4 - len(rawPacket)%4
|
|
||||||
rawPacket = append(rawPacket, make([]byte, padCount)...)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h := Header{
|
h := Header{
|
||||||
@@ -96,7 +95,7 @@ func (g *Goodbye) Unmarshal(rawPacket []byte) error {
|
|||||||
return errWrongType
|
return errWrongType
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(rawPacket)%4 != 0 {
|
if util.GetPadding(len(rawPacket)) != 0 {
|
||||||
return errPacketTooShort
|
return errPacketTooShort
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package rtcp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
|
||||||
|
"github.com/pions/webrtc/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SDESType is the item type used in the RTCP SDES control packet.
|
// SDESType is the item type used in the RTCP SDES control packet.
|
||||||
@@ -187,10 +189,7 @@ func (s SourceDescriptionChunk) Marshal() ([]byte, error) {
|
|||||||
rawPacket = append(rawPacket, uint8(SDESEnd))
|
rawPacket = append(rawPacket, uint8(SDESEnd))
|
||||||
|
|
||||||
// additional null octets MUST be included if needed to pad until the next 32-bit boundary
|
// additional null octets MUST be included if needed to pad until the next 32-bit boundary
|
||||||
if size := len(rawPacket); size%4 != 0 {
|
rawPacket = append(rawPacket, make([]byte, util.GetPadding(len(rawPacket)))...)
|
||||||
padding := make([]byte, 4-size%4)
|
|
||||||
rawPacket = append(rawPacket, padding...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return rawPacket, nil
|
return rawPacket, nil
|
||||||
}
|
}
|
||||||
@@ -236,9 +235,7 @@ func (s SourceDescriptionChunk) len() int {
|
|||||||
len += sdesTypeLen // for terminating null octet
|
len += sdesTypeLen // for terminating null octet
|
||||||
|
|
||||||
// align to 32-bit boundary
|
// align to 32-bit boundary
|
||||||
if len%4 != 0 {
|
len += util.GetPadding(len)
|
||||||
len += 4 - (len % 4)
|
|
||||||
}
|
|
||||||
|
|
||||||
return len
|
return len
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user