-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix unmarshal rtcp keep doing allocation
- Loading branch information
Yohan Totting
committed
Sep 18, 2024
1 parent
294f613
commit 1b2cf63
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package sfu | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/pion/rtcp" | ||
) | ||
|
||
type Packet interface { | ||
DestinationSSRC() []uint32 | ||
Marshal() ([]byte, error) | ||
Unmarshal(rawPacket []byte) error | ||
MarshalSize() int | ||
Release() | ||
} | ||
|
||
var ( | ||
pictureLossIndicationPool = sync.Pool{New: func() interface{} { return new(rtcp.PictureLossIndication) }} | ||
fullIntraRequestPool = sync.Pool{New: func() interface{} { return new(rtcp.FullIntraRequest) }} | ||
rawPacketPool = sync.Pool{New: func() interface{} { return new(rtcp.RawPacket) }} | ||
|
||
errPacketTooShort = errors.New("rtcp: packet too short") | ||
errInvalidHeader = errors.New("rtcp: invalid header") | ||
) | ||
|
||
func unmarshal(rawData []byte) (packet rtcp.Packet, bytesprocessed int, err error) { | ||
var h rtcp.Header | ||
|
||
err = h.Unmarshal(rawData) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
bytesprocessed = int(h.Length+1) * 4 | ||
if bytesprocessed > len(rawData) { | ||
return nil, 0, errPacketTooShort | ||
} | ||
inPacket := rawData[:bytesprocessed] | ||
|
||
switch h.Type { | ||
|
||
case rtcp.TypePayloadSpecificFeedback: | ||
switch h.Count { | ||
case rtcp.FormatPLI: | ||
packet = pictureLossIndicationPool.Get().(*rtcp.PictureLossIndication) | ||
case rtcp.FormatFIR: | ||
packet = fullIntraRequestPool.Get().(*rtcp.FullIntraRequest) | ||
default: | ||
packet = rawPacketPool.Get().(*rtcp.RawPacket) | ||
} | ||
default: | ||
packet = rawPacketPool.Get().(*rtcp.RawPacket) | ||
} | ||
|
||
err = packet.Unmarshal(inPacket) | ||
|
||
return packet, bytesprocessed, err | ||
} | ||
|
||
func Unmarshal(rawData []byte) ([]rtcp.Packet, error) { | ||
var packets []rtcp.Packet | ||
for len(rawData) != 0 { | ||
p, processed, err := unmarshal(rawData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
packets = append(packets, p) | ||
rawData = rawData[processed:] | ||
} | ||
|
||
switch len(packets) { | ||
// Empty packet | ||
case 0: | ||
return nil, errInvalidHeader | ||
// Multiple Packets | ||
default: | ||
return packets, nil | ||
} | ||
} |