Skip to content

Commit

Permalink
Reducing unnecessary allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
pougetat committed Oct 24, 2023
1 parent 18ebb3e commit 411412d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
9 changes: 9 additions & 0 deletions pkg/flexfec/flexfec_coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ func NewCoverage(mediaPackets []rtp.Packet, numFecPackets uint32) *ProtectionCov
}
}

func (p *ProtectionCoverage) ResetCoverage() {
for i := uint32(0); i < MaxFecPackets; i++ {
for j := uint32(0); j < MaxMediaPackets; j++ {
p.packetMasks[i].SetBit(j, 0)
}
}

}

func (p *ProtectionCoverage) GetCoveredBy(fecPacketIndex uint32) *util.MediaPacketIterator {
coverage := make([]uint32, 0, p.numMediaPackets)
for mediaPacketIndex := uint32(0); mediaPacketIndex < p.numMediaPackets; mediaPacketIndex++ {
Expand Down
23 changes: 15 additions & 8 deletions pkg/flexfec/flexfec_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,49 @@ type FlexFecEncoder struct {
baseSN uint16
payloadType uint8
ssrc uint32
coverage *ProtectionCoverage
}

func NewFlexFecEncoder(baseSN uint16, payloadType uint8, ssrc uint32) *FlexFecEncoder {
return &FlexFecEncoder{
baseSN: baseSN,
payloadType: payloadType,
ssrc: ssrc,
coverage: nil,
}
}

// Returns the array of FEC packets protecting mediaPkts specified as parameter.
// Encoding will be skipped if there are missing Media packets.
func (flex *FlexFecEncoder) EncodeFec(mediaPackets []rtp.Packet, numFecPackets uint32) []rtp.Packet {
// Start by defining which FEC packets cover which media packets
coverage := NewCoverage(mediaPackets, numFecPackets)
// We transform mediaPackets array into a slice to avoid as many copies as possible.
if flex.coverage == nil {
flex.coverage = NewCoverage(mediaPackets[:], numFecPackets)
} else {
flex.coverage.ResetCoverage()
}

if coverage == nil {
if flex.coverage == nil {
return nil
}

// Generate FEC payloads
fecPackets := make([]rtp.Packet, numFecPackets)
for fecPacketIndex := uint32(0); fecPacketIndex < numFecPackets; fecPacketIndex++ {
fecPackets[fecPacketIndex] = flex.encodeFlexFecPacket(fecPacketIndex, coverage)
fecPackets[fecPacketIndex] = flex.encodeFlexFecPacket(fecPacketIndex)
}

return fecPackets
}

func (flex *FlexFecEncoder) encodeFlexFecPacket(fecPacketIndex uint32, coverage *ProtectionCoverage) rtp.Packet {
mediaPacketsIt := coverage.GetCoveredBy(fecPacketIndex)
func (flex *FlexFecEncoder) encodeFlexFecPacket(fecPacketIndex uint32) rtp.Packet {
mediaPacketsIt := flex.coverage.GetCoveredBy(fecPacketIndex)
flexFecHeader := flex.encodeFlexFecHeader(
mediaPacketsIt,
coverage.ExtractMask1(fecPacketIndex),
coverage.ExtractMask2(fecPacketIndex),
coverage.ExtractMask3(fecPacketIndex),
flex.coverage.ExtractMask1(fecPacketIndex),
flex.coverage.ExtractMask2(fecPacketIndex),
flex.coverage.ExtractMask3(fecPacketIndex),
)
flexFecRepairPayload := flex.encodeFlexFecRepairPayload(mediaPacketsIt.Reset())

Expand Down
8 changes: 8 additions & 0 deletions pkg/flexfec/util/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ func NewBitArray(sizeBits uint32) BitArray {
}

func (b *BitArray) SetBit(bitIndex uint32, bitValue uint32) {
byteIndex := bitIndex / 8
bitOffset := uint(bitIndex % 8)

// Set the specific bit to 1 using bitwise OR
if bitValue == 1 {
b.bytes[byteIndex] |= 1 << bitOffset
} else {
b.bytes[byteIndex] |= 0 << bitOffset
}
}

func (b *BitArray) GetBit(bitIndex uint32) uint8 {
Expand Down

0 comments on commit 411412d

Please sign in to comment.