Skip to content

Commit 9a47c5e

Browse files
asnyatkovSean-Der
authored andcommitted
Expose MarshalSize as a public interface function
Lenth of the packet is an important piece of information which can be used for stats reporting and to calculate size or pre-allocated memory buffers.
1 parent 546aef8 commit 9a47c5e

16 files changed

Lines changed: 71 additions & 32 deletions

compound_packet.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ func (c CompoundPacket) Marshal() ([]byte, error) {
110110
return Marshal(p)
111111
}
112112

113+
// MarshalSize returns the size of the packet once marshaled
114+
func (c CompoundPacket) MarshalSize() int {
115+
l := 0
116+
for _, p := range c {
117+
l += p.MarshalSize()
118+
}
119+
return l
120+
}
121+
113122
// Unmarshal decodes a CompoundPacket from binary.
114123
func (c *CompoundPacket) Unmarshal(rawData []byte) error {
115124
out := make(CompoundPacket, 0)

extended_report.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@ func (b *UnknownReportBlock) setupBlockHeader() {
545545
func (b *UnknownReportBlock) unpackBlockHeader() {
546546
}
547547

548+
// MarshalSize returns the size of the packet once marshaled
549+
func (x ExtendedReport) MarshalSize() int {
550+
return wireSize(x)
551+
}
552+
548553
// Marshal encodes the ExtendedReport in binary
549554
func (x ExtendedReport) Marshal() ([]byte, error) {
550555
for _, p := range x.Reports {

full_intra_request.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ func (p *FullIntraRequest) Header() Header {
8888
return Header{
8989
Count: FormatFIR,
9090
Type: TypePayloadSpecificFeedback,
91-
Length: uint16((p.len() / 4) - 1),
91+
Length: uint16((p.MarshalSize() / 4) - 1),
9292
}
9393
}
9494

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

goodbye.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (g Goodbye) Marshal() ([]byte, error) {
3232
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3333
*/
3434

35-
rawPacket := make([]byte, g.len())
35+
rawPacket := make([]byte, g.MarshalSize())
3636
packetBody := rawPacket[headerLength:]
3737

3838
if len(g.Sources) > countMax {
@@ -126,11 +126,12 @@ func (g *Goodbye) Header() Header {
126126
Padding: false,
127127
Count: uint8(len(g.Sources)),
128128
Type: TypeGoodbye,
129-
Length: uint16((g.len() / 4) - 1),
129+
Length: uint16((g.MarshalSize() / 4) - 1),
130130
}
131131
}
132132

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

packet.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Packet interface {
1010

1111
Marshal() ([]byte, error)
1212
Unmarshal(rawPacket []byte) error
13+
MarshalSize() int
1314
}
1415

1516
// Unmarshal takes an entire udp datagram (which may consist of multiple RTCP packets) and

picture_loss_indication.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (p PictureLossIndication) Marshal() ([]byte, error) {
2929
*
3030
* The semantics of this FB message is independent of the payload type.
3131
*/
32-
rawPacket := make([]byte, p.len())
32+
rawPacket := make([]byte, p.MarshalSize())
3333
packetBody := rawPacket[headerLength:]
3434

3535
binary.BigEndian.PutUint32(packetBody, p.SenderSSRC)
@@ -78,7 +78,8 @@ func (p *PictureLossIndication) Header() Header {
7878
}
7979
}
8080

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

rapid_resynchronization_request.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (p RapidResynchronizationRequest) Marshal() ([]byte, error) {
3535
*
3636
* The semantics of this FB message is independent of the payload type.
3737
*/
38-
rawPacket := make([]byte, p.len())
38+
rawPacket := make([]byte, p.MarshalSize())
3939
packetBody := rawPacket[headerLength:]
4040

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

73-
func (p *RapidResynchronizationRequest) len() int {
73+
// MarshalSize returns the size of the packet once marshaled
74+
func (p *RapidResynchronizationRequest) MarshalSize() int {
7475
return headerLength + rrrHeaderLength
7576
}
7677

raw_packet.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ func (r RawPacket) String() string {
4343
out := fmt.Sprintf("RawPacket: %v", ([]byte)(r))
4444
return out
4545
}
46+
47+
// MarshalSize returns the size of the packet once marshaled
48+
func (r RawPacket) MarshalSize() int {
49+
return len(r)
50+
}

receiver_estimated_maximum_bitrate.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ func (p ReceiverEstimatedMaximumBitrate) Marshal() (buf []byte, err error) {
4242
return buf, nil
4343
}
4444

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

receiver_report.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (r ReceiverReport) Marshal() ([]byte, error) {
5858
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5959
*/
6060

61-
rawPacket := make([]byte, r.len())
61+
rawPacket := make([]byte, r.MarshalSize())
6262
packetBody := rawPacket[headerLength:]
6363

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

160-
func (r *ReceiverReport) len() int {
160+
// MarshalSize returns the size of the packet once marshaled
161+
func (r *ReceiverReport) MarshalSize() int {
161162
repsLength := 0
162163
for _, rep := range r.Reports {
163164
repsLength += rep.len()
@@ -170,7 +171,7 @@ func (r *ReceiverReport) Header() Header {
170171
return Header{
171172
Count: uint8(len(r.Reports)),
172173
Type: TypeReceiverReport,
173-
Length: uint16((r.len()/4)-1) + uint16(getPadding(len(r.ProfileExtensions))),
174+
Length: uint16((r.MarshalSize()/4)-1) + uint16(getPadding(len(r.ProfileExtensions))),
174175
}
175176
}
176177

0 commit comments

Comments
 (0)