mirror of
https://github.com/bluenviron/mediamtx
synced 2024-12-12 01:35:02 +00:00
57c2d5aecb
* add playback server * add playback switch * update readme
86 lines
1.5 KiB
Go
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()
|
|
}
|