Skip to content

Commit

Permalink
Fix AudioLevel Extension
Browse files Browse the repository at this point in the history
* ID/len handled by rtp package now
  • Loading branch information
tarrencev committed May 20, 2020
1 parent b923238 commit 3ea3501
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 107 deletions.
40 changes: 10 additions & 30 deletions audiolevelextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import (
)

const (
// AudioLevelOneByteExtensionSize One byte header size
AudioLevelOneByteExtensionSize = 2
// AudioLevelTwoByteExtensionSize Two byte header size
AudioLevelTwoByteExtensionSize = 4
// audioLevelExtensionSize One byte header size
audioLevelExtensionSize = 1
)

var (
errInvalidSize = errors.New("invalid buffer size")
errInvalidExtensonLength = errors.New("invalid extension length")
errAudioLevelOverflow = errors.New("audio level overflow")
errAudioLevelOverflow = errors.New("audio level overflow")
)

// AudioLevelExtension is a extension payload format described in
Expand All @@ -37,7 +33,6 @@ var (
// | ID | len=1 |V| level | 0 (pad) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
type AudioLevelExtension struct {
ID uint8
Level uint8
Voice bool
}
Expand All @@ -51,32 +46,17 @@ func (a *AudioLevelExtension) Marshal() ([]byte, error) {
if a.Voice {
voice = 0x80
}
buf := make([]byte, AudioLevelOneByteExtensionSize)
buf[0] = a.ID << 4 & 0xf0
buf[1] = voice | a.Level
buf := make([]byte, audioLevelExtensionSize)
buf[0] = voice | a.Level
return buf, nil
}

// Unmarshal parses the passed byte slice and stores the result in the members
func (a *AudioLevelExtension) Unmarshal(rawData []byte) error {
// one byte format
switch len(rawData) {
case AudioLevelOneByteExtensionSize:
if rawData[0]&^0xF0 != 0 {
return errInvalidExtensonLength
}
a.ID = rawData[0] >> 4
a.Level = rawData[1] & 0x7F
a.Voice = rawData[1]&0x80 != 0
return nil
case AudioLevelTwoByteExtensionSize:
if rawData[1] != 1 {
return errInvalidExtensonLength
}
a.ID = rawData[0]
a.Level = rawData[2] & 0x7F
a.Voice = rawData[2]&0x80 != 0
return nil
if len(rawData) < audioLevelExtensionSize {
return errTooSmall
}
return errInvalidSize
a.Level = rawData[0] & 0x7F
a.Voice = rawData[0]&0x80 != 0
return nil
}
84 changes: 7 additions & 77 deletions audiolevelextension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,23 @@ func TestAudioLevelExtensionTooSmall(t *testing.T) {

rawData := []byte{}

if err := a.Unmarshal(rawData); err != errInvalidSize {
t.Fatal("err != errInvalidSize")
if err := a.Unmarshal(rawData); err != errTooSmall {
t.Fatal("err != errTooSmall")
}
}

func TestAudioLevelExtensionTooBig(t *testing.T) {
a := AudioLevelExtension{}

rawData := []byte{
0x00, 0x00, 0x00, 0x00, 0x00,
}

if err := a.Unmarshal(rawData); err != errInvalidSize {
t.Fatal("err != errInvalidSize")
}
}

func TestAudioLevelOneByteExtensionInvalidLength(t *testing.T) {
a := AudioLevelExtension{}

rawData := []byte{
0x31, 0x88,
}

if err := a.Unmarshal(rawData); err != errInvalidExtensonLength {
t.Fatal("err != errInvalidExtensonLength")
}
}

func TestAudioLevelTwoByteExtensionInvalidLength(t *testing.T) {
a := AudioLevelExtension{}

rawData := []byte{
0x30, 0x00, 0x00, 0x00,
}

if err := a.Unmarshal(rawData); err != errInvalidExtensonLength {
t.Fatal("err != errInvalidExtensonLength")
}
}

func TestAudioLevelOneByteExtensionVoiceTrue(t *testing.T) {
func TestAudioLevelExtensionVoiceTrue(t *testing.T) {
a1 := AudioLevelExtension{}

rawData := []byte{
0x30, 0x88,
0x88,
}

if err := a1.Unmarshal(rawData); err != nil {
t.Fatal("Unmarshal error on extension data")
}

a2 := AudioLevelExtension{
ID: 3,
Level: 8,
Voice: true,
}
Expand All @@ -78,19 +41,18 @@ func TestAudioLevelOneByteExtensionVoiceTrue(t *testing.T) {
}
}

func TestAudioLevelOneByteExtensionVoiceFalse(t *testing.T) {
func TestAudioLevelExtensionVoiceFalse(t *testing.T) {
a1 := AudioLevelExtension{}

rawData := []byte{
0x30, 0x8,
0x8,
}

if err := a1.Unmarshal(rawData); err != nil {
t.Fatal("Unmarshal error on extension data")
}

a2 := AudioLevelExtension{
ID: 3,
Level: 8,
Voice: false,
}
Expand All @@ -105,9 +67,8 @@ func TestAudioLevelOneByteExtensionVoiceFalse(t *testing.T) {
}
}

func TestAudioLevelOneByteExtensionLevelOverflow(t *testing.T) {
func TestAudioLevelExtensionLevelOverflow(t *testing.T) {
a := AudioLevelExtension{
ID: 3,
Level: 128,
Voice: false,
}
Expand All @@ -116,34 +77,3 @@ func TestAudioLevelOneByteExtensionLevelOverflow(t *testing.T) {
t.Fatal("err != errAudioLevelOverflow")
}
}

func TestAudioLevelTwoByteExtensionVoiceFalse(t *testing.T) {
a1 := AudioLevelExtension{}

oneByteRawData := []byte{
0x30, 0x8,
}

twoByteRawData := []byte{
0x3, 0x1, 0x8, 0x0,
}

if err := a1.Unmarshal(twoByteRawData); err != nil {
t.Fatal("Unmarshal error on extension data")
}

a2 := AudioLevelExtension{
ID: 3,
Level: 8,
Voice: false,
}

if a1 != a2 {
t.Error("Unmarshal failed")
}

dstData, _ := a2.Marshal()
if !bytes.Equal(dstData, oneByteRawData) {
t.Error("Marshal failed")
}
}

0 comments on commit 3ea3501

Please sign in to comment.