Skip to content

Commit

Permalink
Add NewCNAMESourceDescription helper method
Browse files Browse the repository at this point in the history
This specific source description is very common. This change introduces
a helper method to simplify the creation of CNAME source descriptions.

#22
  • Loading branch information
kevmo314 committed Nov 19, 2021
1 parent 20c9758 commit 2a0c93d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 73 deletions.
30 changes: 3 additions & 27 deletions compound_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,7 @@ func TestBadCompound(t *testing.T) {
}

func TestValidPacket(t *testing.T) {
cname := &SourceDescription{
Chunks: []SourceDescriptionChunk{{
Source: 1234,
Items: []SourceDescriptionItem{{
Type: SDESCNAME,
Text: "cname",
}},
}},
}
cname := NewCNAMESourceDescription(1234, "cname")

for _, test := range []struct {
Name string
Expand Down Expand Up @@ -147,15 +139,7 @@ func TestValidPacket(t *testing.T) {
}

func TestCNAME(t *testing.T) {
cname := &SourceDescription{
Chunks: []SourceDescriptionChunk{{
Source: 1234,
Items: []SourceDescriptionItem{{
Type: SDESCNAME,
Text: "cname",
}},
}},
}
cname := NewCNAMESourceDescription(1234, "cname")

for _, test := range []struct {
Name string
Expand Down Expand Up @@ -241,15 +225,7 @@ func TestCNAME(t *testing.T) {
}

func TestCompoundPacketRoundTrip(t *testing.T) {
cname := &SourceDescription{
Chunks: []SourceDescriptionChunk{{
Source: 1234,
Items: []SourceDescriptionItem{{
Type: SDESCNAME,
Text: "cname",
}},
}},
}
cname := NewCNAMESourceDescription(1234, "cname")

for _, test := range []struct {
Name string
Expand Down
1 change: 1 addition & 0 deletions fuzz.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package rtcp
Expand Down
14 changes: 1 addition & 13 deletions packet_stringifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,7 @@ func TestPrint(t *testing.T) {
"\tProfileExtensions: []\n",
},
{
&SourceDescription{
Chunks: []SourceDescriptionChunk{
{
Source: 0x902f9e2e,
Items: []SourceDescriptionItem{
{
Type: SDESCNAME,
Text: "{9c00eb92-1afb-9d49-a47d-91f64eee69f5}",
},
},
},
},
},
NewCNAMESourceDescription(0x902f9e2e, "{9c00eb92-1afb-9d49-a47d-91f64eee69f5}"),
"rtcp.SourceDescription:\n" +
"\tChunks:\n" +
"\t\t0:\n" +
Expand Down
14 changes: 1 addition & 13 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,7 @@ func TestUnmarshal(t *testing.T) {
}},
ProfileExtensions: []byte{},
},
&SourceDescription{
Chunks: []SourceDescriptionChunk{
{
Source: 0x902f9e2e,
Items: []SourceDescriptionItem{
{
Type: SDESCNAME,
Text: "{9c00eb92-1afb-9d49-a47d-91f64eee69f5}",
},
},
},
},
},
NewCNAMESourceDescription(0x902f9e2e, "{9c00eb92-1afb-9d49-a47d-91f64eee69f5}"),
&Goodbye{
Sources: []uint32{0x902f9e2e},
},
Expand Down
13 changes: 13 additions & 0 deletions source_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ type SourceDescription struct {
Chunks []SourceDescriptionChunk
}

// NewCNAMESourceDescription creates a new SourceDescription with a single CNAME item.
func NewCNAMESourceDescription(ssrc uint32, cname string) *SourceDescription {
return &SourceDescription{
Chunks: []SourceDescriptionChunk{{
Source: ssrc,
Items: []SourceDescriptionItem{{
Type: SDESCNAME,
Text: cname,
}},
}},
}
}

// Marshal encodes the SourceDescription in binary
func (s SourceDescription) Marshal() ([]byte, error) {
/*
Expand Down
22 changes: 2 additions & 20 deletions source_description_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,7 @@ func TestSourceDescriptionUnmarshal(t *testing.T) {
// END + padding
0x00, 0x00,
},
Want: SourceDescription{
Chunks: []SourceDescriptionChunk{{
Source: 0x01020304,
Items: []SourceDescriptionItem{
{
Type: SDESCNAME,
Text: "",
},
},
}},
},
Want: *NewCNAMESourceDescription(0x01020304, ""),
},
{
Name: "two items",
Expand Down Expand Up @@ -321,15 +311,7 @@ func TestSourceDescriptionRoundTrip(t *testing.T) {
},
{
Name: "empty text",
Desc: SourceDescription{
Chunks: []SourceDescriptionChunk{{
Source: 1,
Items: []SourceDescriptionItem{{
Type: SDESCNAME,
Text: "",
}},
}},
},
Desc: *NewCNAMESourceDescription(1, ""),
},
{
Name: "text too long",
Expand Down

0 comments on commit 2a0c93d

Please sign in to comment.