Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
y-kawawa committed Jan 10, 2025
2 parents d854f8c + fee4338 commit 8b9ba4e
Show file tree
Hide file tree
Showing 45 changed files with 741 additions and 472 deletions.
49 changes: 31 additions & 18 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,32 @@ linters-settings:
- ^os.Exit$
- ^panic$
- ^print(ln)?$
varnamelen:
max-distance: 12
min-name-length: 2
ignore-type-assert-ok: true
ignore-map-index-ok: true
ignore-chan-recv-ok: true
ignore-decls:
- i int
- n int
- w io.Writer
- r io.Reader
- b []byte

linters:
enable:
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
- bidichk # Checks for dangerous unicode character sequences
- bodyclose # checks whether HTTP response body is closed successfully
- containedctx # containedctx is a linter that detects struct contained context.Context field
- contextcheck # check the function whether use a non-inherited context
- cyclop # checks function and package cyclomatic complexity
- decorder # check declaration order and count of types, constants, variables and functions
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
- dupl # Tool for code clone detection
- durationcheck # check for two durations multiplied together
- err113 # Golang linter to check the errors handling expressions
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted.
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.
Expand All @@ -44,68 +59,66 @@ linters:
- exportloopref # checks for pointers to enclosing loop variables
- forbidigo # Forbids identifiers
- forcetypeassert # finds forced type assertions
- funlen # Tool for detection of long functions
- gci # Gci control golang package import order and make it always deterministic.
- gochecknoglobals # Checks that no globals are present in Go code
- gochecknoinits # Checks that no init functions are present in Go code
- gocognit # Computes and checks the cognitive complexity of functions
- goconst # Finds repeated strings that could be replaced by a constant
- gocritic # The most opinionated Go source code linter
- gocyclo # Computes and checks the cyclomatic complexity of functions
- godot # Check if comments end in a period
- godox # Tool for detection of FIXME, TODO and other comment keywords
- err113 # Golang linter to check the errors handling expressions
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
- gofumpt # Gofumpt checks whether code was gofumpt-ed.
- goheader # Checks is file header matches to pattern
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
- goprintffuncname # Checks that printf-like functions are named with `f` at the end
- gosec # Inspects source code for security problems
- gosimple # Linter for Go source code that specializes in simplifying a code
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- grouper # An analyzer to analyze expression groups.
- importas # Enforces consistent import aliases
- ineffassign # Detects when assignments to existing variables are not used
- lll # Reports long lines
- maintidx # maintidx measures the maintainability index of each function.
- makezero # Finds slice declarations with non-zero initial length
- misspell # Finds commonly misspelled English words in comments
- nakedret # Finds naked returns in functions greater than a specified function length
- nestif # Reports deeply nested if statements
- nilerr # Finds the code that returns nil even if it checks that the error is not nil.
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value.
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
- noctx # noctx finds sending http request without context.Context
- predeclared # find code that shadows one of Go's predeclared identifiers
- revive # golint replacement, finds style mistakes
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
- stylecheck # Stylecheck is a replacement for golint
- tagliatelle # Checks the struct tags.
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
- unconvert # Remove unnecessary type conversions
- unparam # Reports unused function parameters
- unused # Checks Go code for unused constants, variables, functions and types
- varnamelen # checks that the length of a variable's name matches its scope
- wastedassign # wastedassign finds wasted assignment statements
- whitespace # Tool for detection of leading and trailing whitespace
disable:
- depguard # Go linter that checks if package imports are in a list of acceptable packages
- containedctx # containedctx is a linter that detects struct contained context.Context field
- cyclop # checks function and package cyclomatic complexity
- funlen # Tool for detection of long functions
- gocyclo # Computes and checks the cyclomatic complexity of functions
- godot # Check if comments end in a period
- gomnd # An analyzer to detect magic numbers.
- gochecknoinits # Checks that no init functions are present in Go code
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
- interfacebloat # A linter that checks length of interface.
- ireturn # Accept Interfaces, Return Concrete Types
- lll # Reports long lines
- maintidx # maintidx measures the maintainability index of each function.
- makezero # Finds slice declarations with non-zero initial length
- nakedret # Finds naked returns in functions greater than a specified function length
- nestif # Reports deeply nested if statements
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
- mnd # An analyzer to detect magic numbers
- nolintlint # Reports ill-formed or insufficient nolint directives
- paralleltest # paralleltest detects missing usage of t.Parallel() method in your Go test
- prealloc # Finds slice declarations that could potentially be preallocated
- promlinter # Check Prometheus metrics naming via promlint
- rowserrcheck # checks whether Err of rows is checked successfully
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed.
- testpackage # linter that makes you use a separate _test package
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
- varnamelen # checks that the length of a variable's name matches its scope
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
- wrapcheck # Checks that errors returned from external packages are wrapped
- wsl # Whitespace Linter - Forces you to use empty lines!

Expand Down
17 changes: 13 additions & 4 deletions abscapturetimeextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
absCaptureTimeExtendedExtensionSize = 16
)

// AbsCaptureTimeExtension is a extension payload format in
// AbsCaptureTimeExtension is a extension payload format in.
// http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
Expand All @@ -24,6 +24,7 @@ const (
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | ... (56-63) |
// +-+-+-+-+-+-+-+-+
// .
type AbsCaptureTimeExtension struct {
Timestamp uint64
EstimatedCaptureClockOffset *int64
Expand All @@ -34,11 +35,13 @@ func (t AbsCaptureTimeExtension) Marshal() ([]byte, error) {
if t.EstimatedCaptureClockOffset != nil {
buf := make([]byte, 16)
binary.BigEndian.PutUint64(buf[0:8], t.Timestamp)
binary.BigEndian.PutUint64(buf[8:16], uint64(*t.EstimatedCaptureClockOffset))
binary.BigEndian.PutUint64(buf[8:16], uint64(*t.EstimatedCaptureClockOffset)) // nolint: gosec // G115

return buf, nil
}
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf[0:8], t.Timestamp)

return buf, nil
}

Expand All @@ -49,9 +52,10 @@ func (t *AbsCaptureTimeExtension) Unmarshal(rawData []byte) error {
}
t.Timestamp = binary.BigEndian.Uint64(rawData[0:8])
if len(rawData) >= absCaptureTimeExtendedExtensionSize {
offset := int64(binary.BigEndian.Uint64(rawData[8:16]))
offset := int64(binary.BigEndian.Uint64(rawData[8:16])) // nolint: gosec // G115 false positive
t.EstimatedCaptureClockOffset = &offset
}

return nil
}

Expand All @@ -75,6 +79,7 @@ func (t AbsCaptureTimeExtension) EstimatedCaptureClockOffsetDuration() *time.Dur
if negative {
duration = -duration
}

return &duration
}

Expand All @@ -86,7 +91,10 @@ func NewAbsCaptureTimeExtension(captureTime time.Time) *AbsCaptureTimeExtension
}

// NewAbsCaptureTimeExtensionWithCaptureClockOffset makes new AbsCaptureTimeExtension from time.Time and a clock offset.
func NewAbsCaptureTimeExtensionWithCaptureClockOffset(captureTime time.Time, captureClockOffset time.Duration) *AbsCaptureTimeExtension {
func NewAbsCaptureTimeExtensionWithCaptureClockOffset(
captureTime time.Time,
captureClockOffset time.Duration,
) *AbsCaptureTimeExtension {
ns := captureClockOffset.Nanoseconds()
negative := false
if ns < 0 {
Expand All @@ -99,6 +107,7 @@ func NewAbsCaptureTimeExtensionWithCaptureClockOffset(captureTime time.Time, cap
if negative {
offset = -offset
}

return &AbsCaptureTimeExtension{
Timestamp: toNtpTime(captureTime),
EstimatedCaptureClockOffset: &offset,
Expand Down
2 changes: 1 addition & 1 deletion abscapturetimeextension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
)

func TestAbsCaptureTimeExtension_Roundtrip(t *testing.T) {
func TestAbsCaptureTimeExtension_Roundtrip(t *testing.T) { // nolint: funlen,cyclop
t.Run("positive captureClockOffset", func(t *testing.T) {
t0 := time.Now()
e1 := NewAbsCaptureTimeExtension(t0)
Expand Down
5 changes: 3 additions & 2 deletions abssendtimeextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (t *AbsSendTimeExtension) Unmarshal(rawData []byte) error {
return errTooSmall
}
t.Timestamp = uint64(rawData[0])<<16 | uint64(rawData[1])<<8 | uint64(rawData[2])

return nil
}

Expand All @@ -58,7 +59,7 @@ func NewAbsSendTimeExtension(sendTime time.Time) *AbsSendTimeExtension {
func toNtpTime(t time.Time) uint64 {
var s uint64
var f uint64
u := uint64(t.UnixNano())
u := uint64(t.UnixNano()) // nolint: gosec // G115 false positive
s = u / 1e9
s += 0x83AA7E80 // offset in seconds between unix epoch and ntp epoch
f = u % 1e9
Expand All @@ -77,5 +78,5 @@ func toTime(t uint64) time.Time {
s -= 0x83AA7E80
u := s*1e9 + f

return time.Unix(0, int64(u))
return time.Unix(0, int64(u)) // nolint: gosec // G115 false positive
}
10 changes: 7 additions & 3 deletions audiolevelextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
// audioLevelExtensionSize One byte header size
// audioLevelExtensionSize One byte header size.
audioLevelExtensionSize = 1
)

Expand All @@ -33,12 +33,14 @@ var errAudioLevelOverflow = errors.New("audio level overflow")
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | ID | len=1 |V| level | 0 (pad) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
//nolint:lll
type AudioLevelExtension struct {
Level uint8
Voice bool
}

// Marshal serializes the members to buffer
// Marshal serializes the members to buffer.
func (a AudioLevelExtension) Marshal() ([]byte, error) {
if a.Level > 127 {
return nil, errAudioLevelOverflow
Expand All @@ -49,15 +51,17 @@ func (a AudioLevelExtension) Marshal() ([]byte, error) {
}
buf := make([]byte, audioLevelExtensionSize)
buf[0] = voice | a.Level

return buf, nil
}

// Unmarshal parses the passed byte slice and stores the result in the members
// Unmarshal parses the passed byte slice and stores the result in the members.
func (a *AudioLevelExtension) Unmarshal(rawData []byte) error {
if len(rawData) < audioLevelExtensionSize {
return errTooSmall
}
a.Level = rawData[0] & 0x7F
a.Voice = rawData[0]&0x80 != 0

return nil
}
4 changes: 3 additions & 1 deletion codecs/av1/frame/av1.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ func (f *AV1) pushOBUElement(isFirstOBUFragment *bool, obuElement []byte, obuLis
obuElement = append(f.obuBuffer, obuElement...)
f.obuBuffer = nil
}

return append(obuList, obuElement)
}

// ReadFrames processes the codecs.AV1Packet and returns fully constructed frames
// ReadFrames processes the codecs.AV1Packet and returns fully constructed frames.
func (f *AV1) ReadFrames(pkt *codecs.AV1Packet) ([][]byte, error) {
OBUs := [][]byte{}
isFirstOBUFragment := pkt.Z
Expand All @@ -43,5 +44,6 @@ func (f *AV1) ReadFrames(pkt *codecs.AV1Packet) ([][]byte, error) {
f.obuBuffer = append(f.obuBuffer, append([]byte{}, OBUs[len(OBUs)-1]...)...)
OBUs = OBUs[:len(OBUs)-1]
}

return OBUs, nil
}
33 changes: 21 additions & 12 deletions codecs/av1/frame/av1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,68 @@ import (

// First is Fragment (and no buffer)
// Self contained OBU
// OBU spread across 3 packets
// OBU spread across 3 packets.
func TestAV1_ReadFrames(t *testing.T) {
// First is Fragment of OBU, but no OBU Elements is cached
f := &AV1{}
frames, err := f.ReadFrames(&codecs.AV1Packet{Z: true, OBUElements: [][]byte{{0x01}}})
fragm := &AV1{}
frames, err := fragm.ReadFrames(&codecs.AV1Packet{Z: true, OBUElements: [][]byte{{0x01}}})
if err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(frames, [][]byte{}) {
t.Fatalf("No frames should be generated, %v", frames)
}

f = &AV1{}
frames, err = f.ReadFrames(&codecs.AV1Packet{OBUElements: [][]byte{{0x01}}})
fragm = &AV1{}
frames, err = fragm.ReadFrames(&codecs.AV1Packet{OBUElements: [][]byte{{0x01}}})
if err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(frames, [][]byte{{0x01}}) {
t.Fatalf("One frame should be generated, %v", frames)
}

f = &AV1{}
frames, err = f.ReadFrames(&codecs.AV1Packet{Y: true, OBUElements: [][]byte{{0x00}}})
fragm = &AV1{}
frames, err = fragm.ReadFrames(&codecs.AV1Packet{Y: true, OBUElements: [][]byte{{0x00}}})
if err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(frames, [][]byte{}) {
t.Fatalf("No frames should be generated, %v", frames)
}

frames, err = f.ReadFrames(&codecs.AV1Packet{Z: true, OBUElements: [][]byte{{0x01}}})
frames, err = fragm.ReadFrames(&codecs.AV1Packet{Z: true, OBUElements: [][]byte{{0x01}}})
if err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(frames, [][]byte{{0x00, 0x01}}) {
t.Fatalf("One frame should be generated, %v", frames)
}
}

// Marshal some AV1 Frames to RTP, assert that AV1 can get them back in the original format
// Marshal some AV1 Frames to RTP, assert that AV1 can get them back in the original format.
func TestAV1_ReadFrames_E2E(t *testing.T) {
const mtu = 1500
frames := [][]byte{
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A},
{0x00, 0x01},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A},
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
},
{0x00, 0x01},
}

frames = append(frames, []byte{})
for i := 0; i <= 5; i++ {
frames[len(frames)-1] = append(frames[len(frames)-1], []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A}...)
frames[len(frames)-1] = append(
frames[len(frames)-1],
[]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A}...,
)
}

frames = append(frames, []byte{})
for i := 0; i <= 500; i++ {
frames[len(frames)-1] = append(frames[len(frames)-1], []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A}...)
frames[len(frames)-1] = append(
frames[len(frames)-1],
[]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A}...,
)
}

payloader := &codecs.AV1Payloader{}
Expand Down
8 changes: 4 additions & 4 deletions codecs/av1/obu/leb128.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const (
msbBitmask = uint(0b10000000)
)

// ErrFailedToReadLEB128 indicates that a buffer ended before a LEB128 value could be successfully read
// ErrFailedToReadLEB128 indicates that a buffer ended before a LEB128 value could be successfully read.
var ErrFailedToReadLEB128 = errors.New("payload ended before LEB128 was finished")

// EncodeLEB128 encodes a uint as LEB128
// EncodeLEB128 encodes a uint as LEB128.
func EncodeLEB128(in uint) (out uint) {
for {
// Copy seven bits from in and discard
Expand Down Expand Up @@ -50,15 +50,15 @@ func decodeLEB128(in uint) (out uint) {

// ReadLeb128 scans an buffer and decodes a Leb128 value.
// If the end of the buffer is reached and all MSB are set
// an error is returned
// an error is returned.
func ReadLeb128(in []byte) (uint, uint, error) {
var encodedLength uint

for i := range in {
encodedLength |= uint(in[i])

if in[i]&byte(msbBitmask) == 0 {
return decodeLEB128(encodedLength), uint(i + 1), nil
return decodeLEB128(encodedLength), uint(i + 1), nil // nolint: gosec // G115
}

// Make more room for next read
Expand Down
Loading

0 comments on commit 8b9ba4e

Please sign in to comment.