diff --git a/pkg/format/h264.go b/pkg/format/h264.go index 72316863..3709df41 100644 --- a/pkg/format/h264.go +++ b/pkg/format/h264.go @@ -136,7 +136,7 @@ func (f *H264) PTSEqualsDTS(pkt *rtp.Packet) bool { case 24: // STAP-A payload := pkt.Payload[1:] - for len(payload) > 0 { + for { if len(payload) < 2 { return false } @@ -156,6 +156,10 @@ func (f *H264) PTSEqualsDTS(pkt *rtp.Packet) bool { case h264.NALUTypeIDR, h264.NALUTypeSPS, h264.NALUTypePPS: return true } + + if len(payload) == 0 { + break + } } case 28: // FU-A diff --git a/pkg/format/h265.go b/pkg/format/h265.go index 80674869..ff11cbe6 100644 --- a/pkg/format/h265.go +++ b/pkg/format/h265.go @@ -142,7 +142,7 @@ func (f *H265) PTSEqualsDTS(pkt *rtp.Packet) bool { case h265.NALUType_AggregationUnit: payload := pkt.Payload[2:] - for len(payload) > 0 { + for { if len(payload) < 2 { return false } @@ -163,6 +163,10 @@ func (f *H265) PTSEqualsDTS(pkt *rtp.Packet) bool { h265.NALUType_VPS_NUT, h265.NALUType_SPS_NUT, h265.NALUType_PPS_NUT: return true } + + if len(payload) == 0 { + break + } } case h265.NALUType_FragmentationUnit: diff --git a/pkg/format/rtph264/decoder.go b/pkg/format/rtph264/decoder.go index 9590262b..4c566826 100644 --- a/pkg/format/rtph264/decoder.go +++ b/pkg/format/rtph264/decoder.go @@ -117,7 +117,7 @@ func (d *Decoder) decodeNALUs(pkt *rtp.Packet) ([][]byte, error) { payload := pkt.Payload[1:] - for len(payload) > 0 { + for { if len(payload) < 2 { return nil, fmt.Errorf("invalid STAP-A packet (invalid size)") } @@ -136,6 +136,10 @@ func (d *Decoder) decodeNALUs(pkt *rtp.Packet) ([][]byte, error) { nalus = append(nalus, payload[:size]) payload = payload[size:] + + if len(payload) == 0 { + break + } } if nalus == nil { diff --git a/pkg/format/rtph265/decoder.go b/pkg/format/rtph265/decoder.go index 27769ad0..be7311fb 100644 --- a/pkg/format/rtph265/decoder.go +++ b/pkg/format/rtph265/decoder.go @@ -67,7 +67,7 @@ func (d *Decoder) decodeNALUs(pkt *rtp.Packet) ([][]byte, error) { payload := pkt.Payload[2:] - for len(payload) > 0 { + for { if len(payload) < 2 { return nil, fmt.Errorf("invalid aggregation unit (invalid size)") } @@ -85,6 +85,10 @@ func (d *Decoder) decodeNALUs(pkt *rtp.Packet) ([][]byte, error) { nalus = append(nalus, payload[:size]) payload = payload[size:] + + if len(payload) == 0 { + break + } } if nalus == nil { diff --git a/pkg/format/rtpmjpeg/encoder.go b/pkg/format/rtpmjpeg/encoder.go index 65fdb395..0d9df8bb 100644 --- a/pkg/format/rtpmjpeg/encoder.go +++ b/pkg/format/rtpmjpeg/encoder.go @@ -191,7 +191,7 @@ outer: return nil, fmt.Errorf("JPEG type %d is not supported", sof.Type) } - if data == nil { + if len(data) == 0 { return nil, fmt.Errorf("image data not found") } @@ -211,7 +211,7 @@ outer: offset := 0 var ret []*rtp.Packet - for len(data) > 0 { + for { var buf []byte jh.FragmentOffset = uint32(offset) @@ -266,6 +266,10 @@ outer: Payload: buf, }) e.sequenceNumber++ + + if len(data) == 0 { + break + } } return ret, nil