mediamtx/internal/stream/stream_reader.go
Alessandro Ros 986e270862
Some checks are pending
code_lint / golangci_lint (push) Waiting to run
code_lint / mod_tidy (push) Waiting to run
code_lint / api_docs (push) Waiting to run
code_test / test_64 (push) Waiting to run
code_test / test_32 (push) Waiting to run
code_test / test_highlevel (push) Waiting to run
count and log all discarded frames, decode errors, lost packets (#4363)
Discarded frames, decode errors and lost packets were logged
individually, then there was a mechanism that prevented more than 1 log
entry per second from being printed, resulting in inaccurate reports.

Now discarded frames, decode errors and lost packets are accurately
counted, and their count is printed once every second.
2025-03-25 21:59:58 +01:00

86 lines
1.4 KiB
Go

package stream
import (
"fmt"
"github.com/bluenviron/gortsplib/v4/pkg/ringbuffer"
"github.com/bluenviron/mediamtx/internal/counterdumper"
"github.com/bluenviron/mediamtx/internal/logger"
)
type streamReader struct {
queueSize int
parent logger.Writer
buffer *ringbuffer.RingBuffer
started bool
discardedFrames *counterdumper.CounterDumper
// out
err chan error
}
func (w *streamReader) initialize() {
buffer, _ := ringbuffer.New(uint64(w.queueSize))
w.buffer = buffer
w.err = make(chan error)
}
func (w *streamReader) start() {
w.started = true
w.discardedFrames = &counterdumper.CounterDumper{
OnReport: func(val uint64) {
w.parent.Log(logger.Warn, "connection is too slow, discarding %d %s",
val,
func() string {
if val == 1 {
return "frame"
}
return "frames"
}())
},
}
w.discardedFrames.Start()
go w.run()
}
func (w *streamReader) stop() {
w.buffer.Close()
if w.started {
w.discardedFrames.Stop()
<-w.err
}
}
func (w *streamReader) error() chan error {
return w.err
}
func (w *streamReader) run() {
w.err <- w.runInner()
close(w.err)
}
func (w *streamReader) runInner() error {
for {
cb, ok := w.buffer.Pull()
if !ok {
return fmt.Errorf("terminated")
}
err := cb.(func() error)()
if err != nil {
return err
}
}
}
func (w *streamReader) push(cb func() error) {
ok := w.buffer.Push(cb)
if !ok {
w.discardedFrames.Increase()
}
}