Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose MarshalSize as a public interface function #165

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compound_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@
return Marshal(p)
}

// MarshalSize returns the size of the packet once marshaled
func (c CompoundPacket) MarshalSize() int {
l := 0
for _, p := range c {
l += p.MarshalSize()
}
return l

Check warning on line 119 in compound_packet.go

View check run for this annotation

Codecov / codecov/patch

compound_packet.go#L114-L119

Added lines #L114 - L119 were not covered by tests
}

// Unmarshal decodes a CompoundPacket from binary.
func (c *CompoundPacket) Unmarshal(rawData []byte) error {
out := make(CompoundPacket, 0)
Expand Down
5 changes: 5 additions & 0 deletions extended_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@
func (b *UnknownReportBlock) unpackBlockHeader() {
}

// MarshalSize returns the size of the packet once marshaled
func (x ExtendedReport) MarshalSize() int {
return wireSize(x)

Check warning on line 550 in extended_report.go

View check run for this annotation

Codecov / codecov/patch

extended_report.go#L549-L550

Added lines #L549 - L550 were not covered by tests
}

// Marshal encodes the ExtendedReport in binary
func (x ExtendedReport) Marshal() ([]byte, error) {
for _, p := range x.Reports {
Expand Down
5 changes: 3 additions & 2 deletions full_intra_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ func (p *FullIntraRequest) Header() Header {
return Header{
Count: FormatFIR,
Type: TypePayloadSpecificFeedback,
Length: uint16((p.len() / 4) - 1),
Length: uint16((p.MarshalSize() / 4) - 1),
}
}

func (p *FullIntraRequest) len() int {
// MarshalSize returns the size of the packet once marshaled
func (p *FullIntraRequest) MarshalSize() int {
return headerLength + firOffset + len(p.FIR)*8
}

Expand Down
7 changes: 4 additions & 3 deletions goodbye.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (g Goodbye) Marshal() ([]byte, error) {
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

rawPacket := make([]byte, g.len())
rawPacket := make([]byte, g.MarshalSize())
packetBody := rawPacket[headerLength:]

if len(g.Sources) > countMax {
Expand Down Expand Up @@ -126,11 +126,12 @@ func (g *Goodbye) Header() Header {
Padding: false,
Count: uint8(len(g.Sources)),
Type: TypeGoodbye,
Length: uint16((g.len() / 4) - 1),
Length: uint16((g.MarshalSize() / 4) - 1),
}
}

func (g *Goodbye) len() int {
// MarshalSize returns the size of the packet once marshaled
func (g *Goodbye) MarshalSize() int {
srcsLength := len(g.Sources) * ssrcLength
reasonLength := len(g.Reason) + 1

Expand Down
1 change: 1 addition & 0 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Packet interface {

Marshal() ([]byte, error)
Unmarshal(rawPacket []byte) error
MarshalSize() int
}

// Unmarshal takes an entire udp datagram (which may consist of multiple RTCP packets) and
Expand Down
5 changes: 3 additions & 2 deletions picture_loss_indication.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (p PictureLossIndication) Marshal() ([]byte, error) {
*
* The semantics of this FB message is independent of the payload type.
*/
rawPacket := make([]byte, p.len())
rawPacket := make([]byte, p.MarshalSize())
packetBody := rawPacket[headerLength:]

binary.BigEndian.PutUint32(packetBody, p.SenderSSRC)
Expand Down Expand Up @@ -78,7 +78,8 @@ func (p *PictureLossIndication) Header() Header {
}
}

func (p *PictureLossIndication) len() int {
// MarshalSize returns the size of the packet once marshaled
func (p *PictureLossIndication) MarshalSize() int {
return headerLength + ssrcLength*2
}

Expand Down
5 changes: 3 additions & 2 deletions rapid_resynchronization_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (p RapidResynchronizationRequest) Marshal() ([]byte, error) {
*
* The semantics of this FB message is independent of the payload type.
*/
rawPacket := make([]byte, p.len())
rawPacket := make([]byte, p.MarshalSize())
packetBody := rawPacket[headerLength:]

binary.BigEndian.PutUint32(packetBody, p.SenderSSRC)
Expand Down Expand Up @@ -70,7 +70,8 @@ func (p *RapidResynchronizationRequest) Unmarshal(rawPacket []byte) error {
return nil
}

func (p *RapidResynchronizationRequest) len() int {
// MarshalSize returns the size of the packet once marshaled
func (p *RapidResynchronizationRequest) MarshalSize() int {
return headerLength + rrrHeaderLength
}

Expand Down
5 changes: 5 additions & 0 deletions raw_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@
out := fmt.Sprintf("RawPacket: %v", ([]byte)(r))
return out
}

// MarshalSize returns the size of the packet once marshaled
func (r RawPacket) MarshalSize() int {
return len(r)

Check warning on line 49 in raw_packet.go

View check run for this annotation

Codecov / codecov/patch

raw_packet.go#L48-L49

Added lines #L48 - L49 were not covered by tests
}
5 changes: 2 additions & 3 deletions receiver_estimated_maximum_bitrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ func (p ReceiverEstimatedMaximumBitrate) Marshal() (buf []byte, err error) {
return buf, nil
}

// MarshalSize returns the size of the packet when marshaled.
// This can be used in conjunction with `MarshalTo` to avoid allocations.
func (p ReceiverEstimatedMaximumBitrate) MarshalSize() (n int) {
// MarshalSize returns the size of the packet once marshaled
func (p ReceiverEstimatedMaximumBitrate) MarshalSize() int {
return 20 + 4*len(p.SSRCs)
}

Expand Down
7 changes: 4 additions & 3 deletions receiver_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r ReceiverReport) Marshal() ([]byte, error) {
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

rawPacket := make([]byte, r.len())
rawPacket := make([]byte, r.MarshalSize())
packetBody := rawPacket[headerLength:]

binary.BigEndian.PutUint32(packetBody, r.SSRC)
Expand Down Expand Up @@ -157,7 +157,8 @@ func (r *ReceiverReport) Unmarshal(rawPacket []byte) error {
return nil
}

func (r *ReceiverReport) len() int {
// MarshalSize returns the size of the packet once marshaled
func (r *ReceiverReport) MarshalSize() int {
repsLength := 0
for _, rep := range r.Reports {
repsLength += rep.len()
Expand All @@ -170,7 +171,7 @@ func (r *ReceiverReport) Header() Header {
return Header{
Count: uint8(len(r.Reports)),
Type: TypeReceiverReport,
Length: uint16((r.len()/4)-1) + uint16(getPadding(len(r.ProfileExtensions))),
Length: uint16((r.MarshalSize()/4)-1) + uint16(getPadding(len(r.ProfileExtensions))),
}
}

Expand Down
7 changes: 6 additions & 1 deletion rfc8888.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@

// Len returns the length of the report in bytes
func (b *CCFeedbackReport) Len() int {
return b.MarshalSize()

Check warning on line 97 in rfc8888.go

View check run for this annotation

Codecov / codecov/patch

rfc8888.go#L97

Added line #L97 was not covered by tests
}

// MarshalSize returns the size of the packet once marshaled
func (b *CCFeedbackReport) MarshalSize() int {
n := 0
for _, block := range b.ReportBlocks {
n += block.len()
Expand All @@ -107,7 +112,7 @@
Padding: false,
Count: FormatCCFB,
Type: TypeTransportSpecificFeedback,
Length: uint16(b.Len()/4 - 1),
Length: uint16(b.MarshalSize()/4 - 1),
}
}

Expand Down
7 changes: 4 additions & 3 deletions sender_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r SenderReport) Marshal() ([]byte, error) {
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

rawPacket := make([]byte, r.len())
rawPacket := make([]byte, r.MarshalSize())
packetBody := rawPacket[headerLength:]

binary.BigEndian.PutUint32(packetBody[srSSRCOffset:], r.SSRC)
Expand Down Expand Up @@ -228,7 +228,8 @@ func (r *SenderReport) DestinationSSRC() []uint32 {
return out
}

func (r *SenderReport) len() int {
// MarshalSize returns the size of the packet once marshaled
func (r *SenderReport) MarshalSize() int {
repsLength := 0
for _, rep := range r.Reports {
repsLength += rep.len()
Expand All @@ -241,7 +242,7 @@ func (r *SenderReport) Header() Header {
return Header{
Count: uint8(len(r.Reports)),
Type: TypeSenderReport,
Length: uint16((r.len() / 4) - 1),
Length: uint16((r.MarshalSize() / 4) - 1),
}
}

Expand Down
5 changes: 3 additions & 2 deletions slice_loss_indication.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func (p *SliceLossIndication) Unmarshal(rawPacket []byte) error {
return nil
}

func (p *SliceLossIndication) len() int {
// MarshalSize returns the size of the packet once marshaled
func (p *SliceLossIndication) MarshalSize() int {
return headerLength + sliOffset + (len(p.SLI) * 4)
}

Expand All @@ -102,7 +103,7 @@ func (p *SliceLossIndication) Header() Header {
return Header{
Count: FormatSLI,
Type: TypeTransportSpecificFeedback,
Length: uint16((p.len() / 4) - 1),
Length: uint16((p.MarshalSize() / 4) - 1),
}
}

Expand Down
14 changes: 8 additions & 6 deletions source_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s SourceDescription) Marshal() ([]byte, error) {
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*/

rawPacket := make([]byte, s.len())
rawPacket := make([]byte, s.MarshalSize())
packetBody := rawPacket[headerLength:]

chunkOffset := 0
Expand Down Expand Up @@ -169,7 +169,8 @@ func (s *SourceDescription) Unmarshal(rawPacket []byte) error {
return nil
}

func (s *SourceDescription) len() int {
// MarshalSize returns the size of the packet once marshaled
func (s *SourceDescription) MarshalSize() int {
chunksLength := 0
for _, c := range s.Chunks {
chunksLength += c.len()
Expand All @@ -182,7 +183,7 @@ func (s *SourceDescription) Header() Header {
return Header{
Count: uint8(len(s.Chunks)),
Type: TypeSourceDescription,
Length: uint16((s.len() / 4) - 1),
Length: uint16((s.MarshalSize() / 4) - 1),
}
}

Expand Down Expand Up @@ -251,7 +252,7 @@ func (s *SourceDescriptionChunk) Unmarshal(rawPacket []byte) error {
return err
}
s.Items = append(s.Items, it)
i += it.len()
i += it.Len()
}

return errPacketTooShort
Expand All @@ -260,7 +261,7 @@ func (s *SourceDescriptionChunk) Unmarshal(rawPacket []byte) error {
func (s SourceDescriptionChunk) len() int {
chunkLen := sdesSourceLen
for _, it := range s.Items {
chunkLen += it.len()
chunkLen += it.Len()
}
chunkLen += sdesTypeLen // for terminating null octet

Expand All @@ -280,7 +281,8 @@ type SourceDescriptionItem struct {
Text string
}

func (s SourceDescriptionItem) len() int {
// Len returns the length of the SourceDescriptionItem when encoded as binary.
func (s SourceDescriptionItem) Len() int {
/*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
Expand Down
11 changes: 8 additions & 3 deletions transport_layer_cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,18 @@

// Len return total bytes with padding
func (t *TransportLayerCC) Len() uint16 {
return uint16(t.MarshalSize())

Check warning on line 366 in transport_layer_cc.go

View check run for this annotation

Codecov / codecov/patch

transport_layer_cc.go#L366

Added line #L366 was not covered by tests
}

// MarshalSize returns the size of the packet once marshaled
func (t *TransportLayerCC) MarshalSize() int {
n := t.packetLen()
// has padding
if n%4 != 0 {
n = (n/4 + 1) * 4
}

return n
return int(n)
}

func (t TransportLayerCC) String() string {
Expand Down Expand Up @@ -399,7 +404,7 @@
return nil, err
}

payload := make([]byte, t.Len()-headerLength)
payload := make([]byte, t.MarshalSize()-headerLength)
binary.BigEndian.PutUint32(payload, t.SenderSSRC)
binary.BigEndian.PutUint32(payload[4:], t.MediaSSRC)
binary.BigEndian.PutUint16(payload[baseSequenceNumberOffset:], t.BaseSequenceNumber)
Expand Down Expand Up @@ -430,7 +435,7 @@
}

if t.Header.Padding {
payload[len(payload)-1] = uint8(t.Len() - t.packetLen())
payload[len(payload)-1] = uint8(t.MarshalSize() - int(t.packetLen()))
}

return append(header, payload...), nil
Expand Down
5 changes: 3 additions & 2 deletions transport_layer_nack.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ func (p *TransportLayerNack) Unmarshal(rawPacket []byte) error {
return nil
}

func (p *TransportLayerNack) len() int {
// MarshalSize returns the size of the packet once marshaled
func (p *TransportLayerNack) MarshalSize() int {
return headerLength + nackOffset + (len(p.Nacks) * 4)
}

Expand All @@ -160,7 +161,7 @@ func (p *TransportLayerNack) Header() Header {
return Header{
Count: FormatTLN,
Type: TypeTransportSpecificFeedback,
Length: uint16((p.len() / 4) - 1),
Length: uint16((p.MarshalSize() / 4) - 1),
}
}

Expand Down
Loading