mediamtx/internal/record/agent_instance.go
Alessandro Ros 57c2d5aecb
add playback server (#2452) (#2906)
* add playback server

* add playback switch

* update readme
2024-01-23 20:52:05 +01:00

86 lines
1.5 KiB
Go

package record
import (
"strings"
"time"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
"github.com/bluenviron/mediamtx/internal/asyncwriter"
"github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/logger"
)
// OnSegmentFunc is the prototype of the function passed as runOnSegmentStart / runOnSegmentComplete
type OnSegmentFunc = func(string)
type sample struct {
*fmp4.PartSample
dts time.Duration
ntp time.Time
}
type agentInstance struct {
agent *Agent
pathFormat string
writer *asyncwriter.Writer
format format
terminate chan struct{}
done chan struct{}
}
func (a *agentInstance) initialize() {
a.pathFormat = a.agent.PathFormat
a.pathFormat = PathAddExtension(
strings.ReplaceAll(a.pathFormat, "%path", a.agent.PathName),
a.agent.Format,
)
a.terminate = make(chan struct{})
a.done = make(chan struct{})
a.writer = asyncwriter.New(a.agent.WriteQueueSize, a.agent)
switch a.agent.Format {
case conf.RecordFormatMPEGTS:
a.format = &formatMPEGTS{
a: a,
}
a.format.initialize()
default:
a.format = &formatFMP4{
a: a,
}
a.format.initialize()
}
go a.run()
}
func (a *agentInstance) close() {
close(a.terminate)
<-a.done
}
func (a *agentInstance) run() {
defer close(a.done)
a.writer.Start()
select {
case err := <-a.writer.Error():
a.agent.Log(logger.Error, err.Error())
a.agent.Stream.RemoveReader(a.writer)
case <-a.terminate:
a.agent.Stream.RemoveReader(a.writer)
a.writer.Stop()
}
a.format.close()
}