Skip to content

Commit

Permalink
Add SetZeroAllocation
Browse files Browse the repository at this point in the history
SetZeroAllocation enables a higher performance depacketizer, with a
reduced feature set. Currently only enabled for H264.

By default, the H264Packet.Unmarshal performs reassembly
of FU-A NALUs.  Not only is this ineficient, it also assumes
that no packet reordering or packet loss ever happens.
  • Loading branch information
jech authored and Sean-Der committed Apr 23, 2024
1 parent 32ee92e commit a663858
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion codecs/common.go
Expand Up @@ -22,8 +22,18 @@ func (d *audioDepacketizer) IsPartitionHead(_ []byte) bool {
}

// videoDepacketizer is a mixin for video codec depacketizers
type videoDepacketizer struct{}
type videoDepacketizer struct {
zeroAllocation bool
}

func (d *videoDepacketizer) IsPartitionTail(marker bool, _ []byte) bool {
return marker
}

// SetZeroAllocation enables Zero Allocation mode for the depacketizer
// By default the Depacketizers will allocate as they parse. These allocations
// are needed for Metadata and other optional values. If you don't need this information
// enabling SetZeroAllocation gives you higher performance at a reduced feature set.
func (d *videoDepacketizer) SetZeroAllocation(zeroAllocation bool) {
d.zeroAllocation = zeroAllocation
}
8 changes: 8 additions & 0 deletions codecs/h264_packet.go
Expand Up @@ -208,6 +208,14 @@ func (p *H264Packet) IsDetectedFinalPacketInSequence(rtpPacketMarketBit bool) bo

// Unmarshal parses the passed byte slice and stores the result in the H264Packet this method is called upon
func (p *H264Packet) Unmarshal(payload []byte) ([]byte, error) {
if p.zeroAllocation {
return payload, nil
}

return p.parseBody(payload)
}

func (p *H264Packet) parseBody(payload []byte) ([]byte, error) {
if len(payload) == 0 {
return nil, fmt.Errorf("%w: %d <=0", errShortPacket, len(payload))
}
Expand Down
4 changes: 4 additions & 0 deletions depacketizer.go
Expand Up @@ -5,11 +5,15 @@ package rtp

// Depacketizer depacketizes a RTP payload, removing any RTP specific data from the payload
type Depacketizer interface {
// Unmarshal parses the RTP payload and returns media.
// Metadata may be stored on the Depacketizer itself
Unmarshal(packet []byte) ([]byte, error)

// Checks if the packet is at the beginning of a partition. This
// should return false if the result could not be determined, in
// which case the caller will detect timestamp discontinuities.
IsPartitionHead(payload []byte) bool

// Checks if the packet is at the end of a partition. This should
// return false if the result could not be determined.
IsPartitionTail(marker bool, payload []byte) bool
Expand Down

0 comments on commit a663858

Please sign in to comment.