@@ -998,7 +998,7 @@ func (p *H265Payloader) Payload(mtu uint16, payload []byte) [][]byte { //nolint:
998998 }
999999 } else {
10001000 // construct an aggregation packet
1001- aggregationPacketSize := aggregationBufferSize + 2
1001+ aggregationPacketSize := aggregationBufferSize
10021002 buf := make ([]byte , aggregationPacketSize )
10031003
10041004 layerID := uint8 (math .MaxUint8 )
@@ -1039,23 +1039,32 @@ func (p *H265Payloader) Payload(mtu uint16, payload []byte) [][]byte { //nolint:
10391039 aggregationBufferSize = 0
10401040 }
10411041
1042+ calcMarginalAggregationSize := func (nalu []byte ) int {
1043+ marginalAggregationSize := len (nalu ) + 2 // +2 is NALU size Field size
1044+ if len (bufferedNALUs ) == 1 {
1045+ marginalAggregationSize = len (nalu ) + 4 // +4 are Aggregation header + NALU size Field size
1046+ }
1047+ if p .AddDONL {
1048+ marginalAggregationSize ++
1049+ }
1050+ return marginalAggregationSize
1051+ }
1052+
10421053 emitNalus (payload , func (nalu []byte ) {
10431054 if len (nalu ) < 2 {
10441055 // NALU header is 2 bytes
10451056 return
10461057 }
10471058
1048- if len (nalu ) <= int (mtu ) {
1059+ naluLen := len (nalu ) + 2
1060+ if naluLen <= int (mtu ) {
10491061 // this nalu fits into a single packet, either it can be emitted as
10501062 // a single nalu or appended to the previous aggregation packet
1051-
1052- marginalAggregationSize := len (nalu ) + 2
1053- if p .AddDONL {
1054- marginalAggregationSize ++
1055- }
1063+ marginalAggregationSize := calcMarginalAggregationSize (nalu )
10561064
10571065 if aggregationBufferSize + marginalAggregationSize > int (mtu ) {
10581066 flushBufferedNals ()
1067+ marginalAggregationSize = calcMarginalAggregationSize (nalu )
10591068 }
10601069 bufferedNALUs = append (bufferedNALUs , nalu )
10611070 aggregationBufferSize += marginalAggregationSize
0 commit comments