Skip to content

Commit

Permalink
Make parsing of H.264 NALUs optional
Browse files Browse the repository at this point in the history
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.

Make this optional.
  • Loading branch information
jech committed Apr 15, 2024
1 parent 9c98664 commit 0d6707b
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions codecs/h264_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,23 @@ func (p *H264Payloader) Payload(mtu uint16, payload []byte) [][]byte {

// H264Packet represents the H264 header that is stored in the payload of an RTP Packet
type H264Packet struct {
IsAVC bool
fuaBuffer []byte
IsAVC bool
fuaBuffer []byte
dontParseBody bool

videoDepacketizer
}

// SetDontParseBody specifies whether to skip parsing the body.
// If set to true, Unmarshal returns the raw packet body. If
// set to false (the default), Unmarshal performs reassembly of
// FU-A NALUs, which is inefficient and only works if the packets
// arrive in order. It is recommended that applications set this
// to false and perform their own fragment reassembly.
func (p *H264Packet) SetDontParseBody(dont bool) {

Check warning on line 198 in codecs/h264_packet.go

View workflow job for this annotation

GitHub Actions / lint / Go

unused-parameter: parameter 'dont' seems to be unused, consider removing or renaming it as _ (revive)
p.dontParseBody = true

Check warning on line 199 in codecs/h264_packet.go

View check run for this annotation

Codecov / codecov/patch

codecs/h264_packet.go#L198-L199

Added lines #L198 - L199 were not covered by tests
}

func (p *H264Packet) doPackaging(buf, nalu []byte) []byte {
if p.IsAVC {
buf = binary.BigEndian.AppendUint32(buf, uint32(len(nalu)))
Expand All @@ -208,6 +219,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.dontParseBody {
return payload, nil

Check warning on line 223 in codecs/h264_packet.go

View check run for this annotation

Codecov / codecov/patch

codecs/h264_packet.go#L223

Added line #L223 was not covered by tests
}

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

0 comments on commit 0d6707b

Please sign in to comment.