mediamtx/internal/recorder/recoder_instance.go
Alessandro Ros 4c3ac34425
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
fix memory leak in case of errors during initialization of a reader (#3831)
2024-10-05 00:49:44 +02:00

83 lines
1.5 KiB
Go

package recorder
import (
"strings"
"time"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
"github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/recordstore"
)
type sample struct {
*fmp4.PartSample
dts time.Duration
ntp time.Time
}
type recorderInstance struct {
agent *Recorder
pathFormat string
format format
terminate chan struct{}
done chan struct{}
}
// Log implements logger.Writer.
func (ai *recorderInstance) Log(level logger.Level, format string, args ...interface{}) {
ai.agent.Log(level, format, args...)
}
func (ai *recorderInstance) initialize() {
ai.pathFormat = ai.agent.PathFormat
ai.pathFormat = recordstore.PathAddExtension(
strings.ReplaceAll(ai.pathFormat, "%path", ai.agent.PathName),
ai.agent.Format,
)
ai.terminate = make(chan struct{})
ai.done = make(chan struct{})
switch ai.agent.Format {
case conf.RecordFormatMPEGTS:
ai.format = &formatMPEGTS{
ai: ai,
}
ai.format.initialize()
default:
ai.format = &formatFMP4{
ai: ai,
}
ai.format.initialize()
}
ai.agent.Stream.StartReader(ai)
go ai.run()
}
func (ai *recorderInstance) close() {
close(ai.terminate)
<-ai.done
}
func (ai *recorderInstance) run() {
defer close(ai.done)
select {
case err := <-ai.agent.Stream.ReaderError(ai):
ai.Log(logger.Error, err.Error())
case <-ai.terminate:
}
ai.agent.Stream.RemoveReader(ai)
ai.format.close()
}