From 3f29b4e39f287689da3d8217f1f8f56c3c18febd Mon Sep 17 00:00:00 2001 From: Shlomi Amit Date: Sat, 29 Jun 2024 19:42:05 +0300 Subject: [PATCH] Expose SubType field from header --- application_defined.go | 9 +++-- application_defined_test.go | 66 ++++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/application_defined.go b/application_defined.go index b592262..77a1193 100644 --- a/application_defined.go +++ b/application_defined.go @@ -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. @@ -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() @@ -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]) diff --git a/application_defined_test.go b/application_defined_test.go index b49a255..6db4c4f 100644 --- a/application_defined_test.go +++ b/application_defined_test.go @@ -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}, }, }, { @@ -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}, }, }, { @@ -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{ @@ -191,12 +231,22 @@ 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 @@ -204,7 +254,7 @@ func TestTApplicationPacketMarshal(t *testing.T) { // 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