2023-10-26 19:40:44 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sample struct {
|
|
|
|
*fmp4.PartSample
|
|
|
|
dts time.Duration
|
2024-01-21 18:15:31 +00:00
|
|
|
ntp time.Time
|
2023-10-26 19:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type agentInstance struct {
|
2023-12-02 14:15:17 +00:00
|
|
|
agent *Agent
|
2023-10-26 19:40:44 +00:00
|
|
|
|
2023-12-02 14:35:21 +00:00
|
|
|
pathFormat string
|
|
|
|
writer *asyncwriter.Writer
|
|
|
|
format format
|
2023-10-26 19:40:44 +00:00
|
|
|
|
|
|
|
terminate chan struct{}
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *agentInstance) initialize() {
|
2023-12-02 14:35:21 +00:00
|
|
|
a.pathFormat = a.agent.PathFormat
|
2023-10-26 19:40:44 +00:00
|
|
|
|
2024-01-23 19:52:05 +00:00
|
|
|
a.pathFormat = PathAddExtension(
|
|
|
|
strings.ReplaceAll(a.pathFormat, "%path", a.agent.PathName),
|
|
|
|
a.agent.Format,
|
|
|
|
)
|
2023-10-26 19:40:44 +00:00
|
|
|
|
|
|
|
a.terminate = make(chan struct{})
|
|
|
|
a.done = make(chan struct{})
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
a.writer = asyncwriter.New(a.agent.WriteQueueSize, a.agent)
|
2023-10-26 19:40:44 +00:00
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
switch a.agent.Format {
|
2023-10-26 19:40:44 +00:00
|
|
|
case conf.RecordFormatMPEGTS:
|
2023-12-02 14:03:37 +00:00
|
|
|
a.format = &formatMPEGTS{
|
2023-10-26 19:40:44 +00:00
|
|
|
a: a,
|
|
|
|
}
|
|
|
|
a.format.initialize()
|
|
|
|
|
|
|
|
default:
|
2023-12-02 14:03:37 +00:00
|
|
|
a.format = &formatFMP4{
|
2023-10-26 19:40:44 +00:00
|
|
|
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():
|
2023-12-02 14:15:17 +00:00
|
|
|
a.agent.Log(logger.Error, err.Error())
|
|
|
|
a.agent.Stream.RemoveReader(a.writer)
|
2023-10-26 19:40:44 +00:00
|
|
|
|
|
|
|
case <-a.terminate:
|
2023-12-02 14:15:17 +00:00
|
|
|
a.agent.Stream.RemoveReader(a.writer)
|
2023-10-26 19:40:44 +00:00
|
|
|
a.writer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
a.format.close()
|
|
|
|
}
|