Skip to content

Commit

Permalink
Expose SubType field from header
Browse files Browse the repository at this point in the history
  • Loading branch information
samitAtsyna committed Jun 29, 2024
1 parent 2ae8f17 commit 3f29b4e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
9 changes: 6 additions & 3 deletions application_defined.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (

// ApplicationDefined represents an RTCP application-defined packet.
type ApplicationDefined struct {
SSRC uint32
Name string
Data []byte
SubType uint8
SSRC uint32
Name string
Data []byte
}

// DestinationSSRC returns the SSRC value for this packet.
Expand Down Expand Up @@ -39,6 +40,7 @@ func (a ApplicationDefined) Marshal() ([]byte, error) {
Type: TypeApplicationDefined,
Length: uint16((packetSize / 4) - 1),
Padding: paddingSize != 0,
Count: a.SubType,
}

headerBytes, err := header.Marshal()
Expand Down Expand Up @@ -90,6 +92,7 @@ func (a *ApplicationDefined) Unmarshal(rawPacket []byte) error {
return errAppDefinedInvalidLength
}

a.SubType = header.Count
a.SSRC = binary.BigEndian.Uint32(rawPacket[4:8])
a.Name = string(rawPacket[8:12])

Expand Down
66 changes: 58 additions & 8 deletions application_defined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,29 @@ func TestTApplicationPacketUnmarshal(t *testing.T) {
0x41, 0x42, 0x43, 0x44,
},
Want: ApplicationDefined{
SSRC: 0x4baae1ab,
Name: "NAME",
Data: []byte{0x41, 0x42, 0x43, 0x44},
SubType: 0,
SSRC: 0x4baae1ab,
Name: "NAME",
Data: []byte{0x41, 0x42, 0x43, 0x44},
},
},
{
Name: "validCustomSsubType",
Data: []byte{
// Application Packet Type (SubType 31) + Length(0x0003)
0x9f, 0xcc, 0x00, 0x03,
// sender=0x4baae1ab
0x4b, 0xaa, 0xe1, 0xab,
// name='NAME'
0x4E, 0x41, 0x4D, 0x45,
// data='ABCD'
0x41, 0x42, 0x43, 0x44,
},
Want: ApplicationDefined{
SubType: 31,
SSRC: 0x4baae1ab,
Name: "NAME",
Data: []byte{0x41, 0x42, 0x43, 0x44},
},
},
{
Expand All @@ -49,9 +69,10 @@ func TestTApplicationPacketUnmarshal(t *testing.T) {
0x03, 0x03, 0x03,
},
Want: ApplicationDefined{
SSRC: 0x4baae1ab,
Name: "NAME",
Data: []byte{0x41, 0x42, 0x43, 0x44, 0x45},
SubType: 0,
SSRC: 0x4baae1ab,
Name: "NAME",
Data: []byte{0x41, 0x42, 0x43, 0x44, 0x45},
},
},
{
Expand Down Expand Up @@ -153,6 +174,25 @@ func TestTApplicationPacketMarshal(t *testing.T) {
Data: []byte{0x41, 0x42, 0x43, 0x44},
},
},
{
Name: "validCustomSubType",
Want: []byte{
// Application Packet Type (SubType 31) + Length(0x0003)
0x9f, 0xcc, 0x00, 0x03,
// sender=0x4baae1ab
0x4b, 0xaa, 0xe1, 0xab,
// name='NAME'
0x4E, 0x41, 0x4D, 0x45,
// data='ABCD'
0x41, 0x42, 0x43, 0x44,
},
Packet: ApplicationDefined{
SubType: 31,
SSRC: 0x4baae1ab,
Name: "NAME",
Data: []byte{0x41, 0x42, 0x43, 0x44},
},
},
{
Name: "validWithPadding",
Want: []byte{
Expand Down Expand Up @@ -191,20 +231,30 @@ func TestTApplicationPacketMarshal(t *testing.T) {
Data: []byte{0x41, 0x42, 0x43, 0x44},
},
},
{
Name: "InvalidSubType",
WantError: errInvalidHeader,
Packet: ApplicationDefined{
SubType: 32, // Must be up to 31
SSRC: 0x4baae1ab,
Name: "NAME",
Data: []byte{0x41, 0x42, 0x43, 0x44},
},
},
} {
rawPacket, err := test.Packet.Marshal()

// Check for expected errors
if got, want := err, test.WantError; !errors.Is(got, want) {
t.Fatalf("Unmarshal %q result: got = %v, want %v", test.Name, got, want)
t.Fatalf("Marshal %q result: got = %v, want %v", test.Name, got, want)
}
if err != nil {
continue
}

// Check for expected successful result
if got, want := rawPacket, test.Want; !reflect.DeepEqual(got, want) {
t.Fatalf("Unmarshal %q result: got %v, want %v", test.Name, got, want)
t.Fatalf("Marshal %q result: got %v, want %v", test.Name, got, want)
}

// Check if MarshalSize() is matching the marshaled bytes
Expand Down

0 comments on commit 3f29b4e

Please sign in to comment.