From 3f0d727e03a928b70bbeec6898f84628ffb22c82 Mon Sep 17 00:00:00 2001 From: ypothoma Date: Thu, 2 Nov 2023 12:07:14 -0700 Subject: [PATCH] Fixing linter issues --- pkg/flexfec/encoder_interceptor.go | 30 ++++++++++++++++++------------ pkg/flexfec/flexfec_coverage.go | 7 ++++--- pkg/flexfec/flexfec_encoder.go | 1 + pkg/flexfec/flexfec_encoder_03.go | 9 ++++----- pkg/flexfec/util/bitarray.go | 15 ++++++++------- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/pkg/flexfec/encoder_interceptor.go b/pkg/flexfec/encoder_interceptor.go index d98ea356..7506fd25 100644 --- a/pkg/flexfec/encoder_interceptor.go +++ b/pkg/flexfec/encoder_interceptor.go @@ -8,6 +8,7 @@ import ( "github.com/pion/rtp" ) +// FecInterceptor implements FlexFec. type FecInterceptor struct { interceptor.NoOp flexFecEncoder FlexEncoder @@ -15,25 +16,26 @@ type FecInterceptor struct { minNumMediaPackets uint32 } -type FECEncoderOption func(d *FecInterceptor) error +// Option can be used to set initial options on Fec encoder interceptors. +type FecOption func(d *FecInterceptor) error -type FECInterceptorFactory struct { - opts []FECEncoderOption +// FecInterceptorFactory creates new FecInterceptors. +type FecInterceptorFactory struct { + opts []FecOption } func streamSupportFec(info *interceptor.StreamInfo) bool { - // Need to check stream info to see if we should be sending FEC packets - if info.MimeType == "video/VP8" { - return true - } - return false + // TODO: Need to check stream info to see if we should be sending FEC packets + return true } -func NewFECInterceptor(opts ...FECEncoderOption) (*FECInterceptorFactory, error) { - return &FECInterceptorFactory{}, nil +// NewFecInterceptor returns a new Fec interceptor factory. +func NewFecInterceptor(opts ...FecOption) (*FecInterceptorFactory, error) { + return &FecInterceptorFactory{}, nil } -func (r *FECInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { +// FlexEncoder implements the Fec encoding mechanism for the "Flex" variant of FlexFec. +func (r *FecInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { // Hardcoded for now: // Min num media packets to encode FEC -> 5 // Min num fec packets -> 1 @@ -71,7 +73,11 @@ func (r *FecInterceptor) BindLocalStream(info *interceptor.StreamInfo, writer in fecPackets = r.flexFecEncoder.EncodeFec(r.packetBuffer, 2) for _, fecPacket := range fecPackets { - writer.Write(&fecPacket.Header, fecPacket.Payload, attributes) + opResult, err := writer.Write(&fecPacket.Header, fecPacket.Payload, attributes) + + if err != nil && opResult == 0 { + break + } } // Reset the packet buffer now that we've sent the corresponding FEC packets. r.packetBuffer = nil diff --git a/pkg/flexfec/flexfec_coverage.go b/pkg/flexfec/flexfec_coverage.go index 85a97f7f..c9383ecc 100644 --- a/pkg/flexfec/flexfec_coverage.go +++ b/pkg/flexfec/flexfec_coverage.go @@ -56,6 +56,8 @@ func NewCoverage(mediaPackets []rtp.Packet, numFecPackets uint32) *ProtectionCov return coverage } +// UpdateCoverage updates the ProtectionCoverage object with new bitmasks accounting for the numFecPackets +// we want to use to protect the batch of media packets. func (p *ProtectionCoverage) UpdateCoverage(mediaPackets []rtp.Packet, numFecPackets uint32) { numMediaPackets := uint32(len(mediaPackets)) @@ -124,7 +126,6 @@ func (p *ProtectionCoverage) ExtractMask1(fecPacketIndex uint32) uint16 { // ExtractMask2 returns the second section of the bitmask as defined by the FEC header. // https://datatracker.ietf.org/doc/html/rfc8627#section-4.2.2.1 func (p *ProtectionCoverage) ExtractMask2(fecPacketIndex uint32) uint32 { - //return uint32(p.packetMasks[fecPacketIndex].GetBitValue(15, 45)) mask := p.packetMasks[fecPacketIndex] // We remove the first 15 bits mask2 := mask.Lo << 15 @@ -136,11 +137,11 @@ func (p *ProtectionCoverage) ExtractMask2(fecPacketIndex uint32) uint32 { // ExtractMask3 returns the third section of the bitmask as defined by the FEC header. // https://datatracker.ietf.org/doc/html/rfc8627#section-4.2.2.1 func (p *ProtectionCoverage) ExtractMask3(fecPacketIndex uint32) uint64 { - //return p.packetMasks[fecPacketIndex].GetBitValue(46, 109) + // return p.packetMasks[fecPacketIndex].GetBitValue(46, 109) return 0 } func (p *ProtectionCoverage) ExtractMask3_03(fecPacketIndex uint32) uint64 { return 0 - //return p.packetMasks[fecPacketIndex].GetBitValue(47, 109) + // return p.packetMasks[fecPacketIndex].GetBitValue(47, 109) } diff --git a/pkg/flexfec/flexfec_encoder.go b/pkg/flexfec/flexfec_encoder.go index 5c1507cd..463e5434 100644 --- a/pkg/flexfec/flexfec_encoder.go +++ b/pkg/flexfec/flexfec_encoder.go @@ -20,6 +20,7 @@ const ( BaseFecHeaderSize = 12 ) +// FlexEncoder is the interface that FecInterceptor uses to encode Fec packets. type FlexEncoder interface { EncodeFec(mediaPackets []rtp.Packet, numFecPackets uint32) []rtp.Packet } diff --git a/pkg/flexfec/flexfec_encoder_03.go b/pkg/flexfec/flexfec_encoder_03.go index 7c624308..15958a4b 100644 --- a/pkg/flexfec/flexfec_encoder_03.go +++ b/pkg/flexfec/flexfec_encoder_03.go @@ -13,12 +13,12 @@ import ( ) const ( - // BaseFecHeaderSize represents the minium FEC payload's header size including the + // BaseFec03HeaderSize represents the minium FEC payload's header size including the // required first mask. BaseFec03HeaderSize = 20 ) -// FlexEncoder implements the Fec encoding mechanism for the "Flex" variant of FlexFec. +// FlexEncoder03 implements the Fec encoding mechanism for the "Flex" variant of FlexFec. type FlexEncoder03 struct { fecBaseSn uint16 payloadType uint8 @@ -26,7 +26,7 @@ type FlexEncoder03 struct { coverage *ProtectionCoverage } -// NewFlexEncoder returns a new FlexFecEncer. +// NewFlexEncoder03 returns a new FlexFecEncoder. func NewFlexEncoder03(payloadType uint8, ssrc uint32) *FlexEncoder03 { return &FlexEncoder03{ payloadType: payloadType, @@ -180,9 +180,8 @@ func (flex *FlexEncoder03) encodeFlexFecHeader(mediaPackets *util.MediaPacketIte if optionalMask2 == 0 { flexFecHeader[18] |= 0b10000000 return flexFecHeader - } else { - binary.BigEndian.PutUint32(flexFecHeader[20:24], optionalMask2) } + binary.BigEndian.PutUint32(flexFecHeader[20:24], optionalMask2) if optionalMask3 == 0 { flexFecHeader[20] |= 0b10000000 diff --git a/pkg/flexfec/util/bitarray.go b/pkg/flexfec/util/bitarray.go index 4706e6b3..c081f0a4 100644 --- a/pkg/flexfec/util/bitarray.go +++ b/pkg/flexfec/util/bitarray.go @@ -20,6 +20,7 @@ func (b *BitArray) SetBit(bitIndex uint32) { } } +// Reset clears the bitmask. func (b *BitArray) Reset() { b.Lo = 0 b.Hi = 0 @@ -33,12 +34,12 @@ func (b *BitArray) GetBit(bitIndex uint32) uint8 { return 1 } return 0 - } else { - hiBitIndex := bitIndex - 64 - result := (b.Hi & (uint64(0b1) << (63 - hiBitIndex))) - if result > 0 { - return 1 - } - return 0 } + + hiBitIndex := bitIndex - 64 + result := (b.Hi & (uint64(0b1) << (63 - hiBitIndex))) + if result > 0 { + return 1 + } + return 0 }