Skip to content

Commit

Permalink
Fixing linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pougetat committed Oct 24, 2023
1 parent 6e01458 commit f78bb04
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
9 changes: 5 additions & 4 deletions pkg/flexfec/flexfec_coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
// We are not supporting the possibility of having an FEC packet protect multiple
// SSRC source packets for now.
// https://datatracker.ietf.org/doc/html/rfc8627#section-4.2.2.1
const MaxMediaPackets uint32 = 110
const MaxFecPackets uint32 = MaxMediaPackets
const (
MaxMediaPackets uint32 = 110
MaxFecPackets uint32 = MaxMediaPackets
)

type ProtectionCoverage struct {

Check warning on line 20 in pkg/flexfec/flexfec_coverage.go

View workflow job for this annotation

GitHub Actions / lint / Go

exported: exported type ProtectionCoverage should have comment or be unexported (revive)
// Array of masks, each mask capable of covering up to maxMediaPkts = 110.
Expand Down Expand Up @@ -66,7 +68,6 @@ func (p *ProtectionCoverage) ResetCoverage() {
p.packetMasks[i].SetBit(j, 0)
}
}

}

func (p *ProtectionCoverage) GetCoveredBy(fecPacketIndex uint32) *util.MediaPacketIterator {

Check warning on line 73 in pkg/flexfec/flexfec_coverage.go

View workflow job for this annotation

GitHub Actions / lint / Go

exported: exported method ProtectionCoverage.GetCoveredBy should have comment or be unexported (revive)
Expand All @@ -92,5 +93,5 @@ func (p *ProtectionCoverage) ExtractMask2(fecPacketIndex uint32) uint32 {
}

func (p *ProtectionCoverage) ExtractMask3(fecPacketIndex uint32) uint64 {
return uint64(p.packetMasks[fecPacketIndex].GetBitValue(46, 109))
return p.packetMasks[fecPacketIndex].GetBitValue(46, 109)
}
17 changes: 12 additions & 5 deletions pkg/flexfec/flexfec_encoder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// Package flexfec implements FlexFEC to recover missing RTP packets due to packet loss.
// https://datatracker.ietf.org/doc/html/rfc8627
package flexfec

import (
Expand All @@ -10,8 +12,10 @@ import (
"github.com/pion/rtp"
)

const BaseRtpHeaderSize = 12
const BaseFecHeaderSize = 12
const (
BaseRtpHeaderSize = 12
BaseFecHeaderSize = 12
)

type FlexFecEncoder struct {
baseSN uint16
Expand All @@ -33,9 +37,8 @@ func NewFlexFecEncoder(baseSN uint16, payloadType uint8, ssrc uint32) *FlexFecEn
// 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
// We transform mediaPackets array into a slice to avoid as many copies as possible.
if flex.coverage == nil {
flex.coverage = NewCoverage(mediaPackets[:], numFecPackets)
flex.coverage = NewCoverage(mediaPackets, numFecPackets)
} else {
flex.coverage.ResetCoverage()
}
Expand Down Expand Up @@ -118,7 +121,11 @@ func (flex *FlexFecEncoder) encodeFlexFecHeader(mediaPackets *util.MediaPacketIt
tmpMediaPacketBuf := make([]byte, headerSize)
for mediaPackets.HasNext() {
mediaPacket := mediaPackets.Next()
mediaPacket.MarshalTo(tmpMediaPacketBuf)
n, err := mediaPacket.MarshalTo(tmpMediaPacketBuf)

if n == 0 || err != nil {
return nil
}

// XOR the first 2 bytes of the header: V, P, X, CC, M, PT fields
flexFecHeader[0] ^= tmpMediaPacketBuf[0]
Expand Down
12 changes: 8 additions & 4 deletions pkg/flexfec/util/bitarray.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// Package util implements utilities to better support Fec decoding / encoding.
package util

// BitArray provides support for bitmask manipulations.
type BitArray struct {
bytes []byte
}

// Returns a bit array as an array of bytes, where sizeBits represents
// the number of bits that the byte array should hold.
// NewBitArray returns a new BitArray. It takes sizeBits as parameter which represents
// the size of the underlying bitmask.
func NewBitArray(sizeBits uint32) BitArray {
sizeBytes := uint32(0)

Check failure on line 15 in pkg/flexfec/util/bitarray.go

View workflow job for this annotation

GitHub Actions / lint / Go

assigned to sizeBytes, but reassigned without using the value (wastedassign)
if sizeBits%8 == 0 {
Expand All @@ -22,6 +24,7 @@ func NewBitArray(sizeBits uint32) BitArray {
}
}

// SetBit sets a bit to the specified bit value on the bitmask.
func (b *BitArray) SetBit(bitIndex uint32, bitValue uint32) {
byteIndex := bitIndex / 8
bitOffset := uint(bitIndex % 8)
Expand All @@ -34,6 +37,7 @@ func (b *BitArray) SetBit(bitIndex uint32, bitValue uint32) {
}
}

// GetBit returns the bit value at a specified index of the bitmask.
func (b *BitArray) GetBit(bitIndex uint32) uint8 {
return b.bytes[bitIndex/8]
}
Expand All @@ -60,13 +64,13 @@ func (b *BitArray) GetBitValue(i int, j int) uint64 {
// Loop through the bytes and concatenate the bits
for idx, b := range subArray {
if idx == 0 {
b = b << uint(startBit)
b <<= uint(startBit)
}
result |= uint64(b)
}

// Mask the bits that are not part of the desired range
result = result & (1<<uint(j-i+1) - 1)
result &= (1<<uint(j-i+1) - 1)

return result
}
7 changes: 7 additions & 0 deletions pkg/flexfec/util/media_packet_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ package util

import "github.com/pion/rtp"

// MediaPacketIterator supports iterating through a list of media packets protected by
// a specific Fec packet.
type MediaPacketIterator struct {
mediaPackets []rtp.Packet
coveredIndices []uint32
nextIndex int
}

// NewMediaPacketIterator returns a new MediaPacketIterator.
func NewMediaPacketIterator(mediaPackets []rtp.Packet, coveredIndices []uint32) *MediaPacketIterator {
return &MediaPacketIterator{
mediaPackets: mediaPackets,
Expand All @@ -19,15 +22,19 @@ func NewMediaPacketIterator(mediaPackets []rtp.Packet, coveredIndices []uint32)
}
}

// Reset sets the starting iterating index back to 0.
func (m *MediaPacketIterator) Reset() *MediaPacketIterator {
m.nextIndex = 0
return m
}

// HasNext indicates whether or not there are more media packets
// that can be iterated through.
func (m *MediaPacketIterator) HasNext() bool {
return m.nextIndex < len(m.coveredIndices)
}

// Next returns the next media packet to iterate through.
func (m *MediaPacketIterator) Next() *rtp.Packet {
if m.nextIndex == len(m.coveredIndices) {
return nil
Expand Down

0 comments on commit f78bb04

Please sign in to comment.