2024-09-08 18:33:18 +00:00
|
|
|
// Package recorder contains the recorder.
|
|
|
|
package recorder
|
2023-09-16 15:27:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2023-10-14 20:52:10 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
2023-09-16 15:27:07 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/stream"
|
|
|
|
)
|
|
|
|
|
2024-06-11 16:30:40 +00:00
|
|
|
// OnSegmentCreateFunc is the prototype of the function passed as OnSegmentCreate
|
|
|
|
type OnSegmentCreateFunc = func(path string)
|
|
|
|
|
|
|
|
// OnSegmentCompleteFunc is the prototype of the function passed as OnSegmentComplete
|
|
|
|
type OnSegmentCompleteFunc = func(path string, duration time.Duration)
|
|
|
|
|
2024-09-08 18:33:18 +00:00
|
|
|
// Recorder writes recordings to disk.
|
|
|
|
type Recorder struct {
|
2023-12-02 14:35:21 +00:00
|
|
|
PathFormat string
|
2023-10-26 19:40:44 +00:00
|
|
|
Format conf.RecordFormat
|
|
|
|
PartDuration time.Duration
|
|
|
|
SegmentDuration time.Duration
|
|
|
|
PathName string
|
|
|
|
Stream *stream.Stream
|
2024-06-11 16:30:40 +00:00
|
|
|
OnSegmentCreate OnSegmentCreateFunc
|
|
|
|
OnSegmentComplete OnSegmentCompleteFunc
|
2023-10-26 19:40:44 +00:00
|
|
|
Parent logger.Writer
|
|
|
|
|
|
|
|
restartPause time.Duration
|
|
|
|
|
2024-10-04 22:49:44 +00:00
|
|
|
currentInstance *recorderInstance
|
2023-10-26 19:40:44 +00:00
|
|
|
|
|
|
|
terminate chan struct{}
|
|
|
|
done chan struct{}
|
2023-09-16 15:27:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-08 18:33:18 +00:00
|
|
|
// Initialize initializes Recorder.
|
2024-11-30 10:23:41 +00:00
|
|
|
func (r *Recorder) Initialize() {
|
|
|
|
if r.OnSegmentCreate == nil {
|
|
|
|
r.OnSegmentCreate = func(string) {
|
2023-11-23 12:36:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-30 10:23:41 +00:00
|
|
|
if r.OnSegmentComplete == nil {
|
|
|
|
r.OnSegmentComplete = func(string, time.Duration) {
|
2023-11-23 12:36:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-30 10:23:41 +00:00
|
|
|
if r.restartPause == 0 {
|
|
|
|
r.restartPause = 2 * time.Second
|
2023-10-14 20:52:10 +00:00
|
|
|
}
|
2023-09-16 15:27:07 +00:00
|
|
|
|
2024-11-30 10:23:41 +00:00
|
|
|
r.terminate = make(chan struct{})
|
|
|
|
r.done = make(chan struct{})
|
2023-09-16 15:27:07 +00:00
|
|
|
|
2024-11-30 10:23:41 +00:00
|
|
|
r.currentInstance = &recorderInstance{
|
|
|
|
rec: r,
|
2023-09-16 15:27:07 +00:00
|
|
|
}
|
2024-11-30 10:23:41 +00:00
|
|
|
r.currentInstance.initialize()
|
2023-09-16 15:27:07 +00:00
|
|
|
|
2024-11-30 10:23:41 +00:00
|
|
|
go r.run()
|
2023-09-16 15:27:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 18:17:17 +00:00
|
|
|
// Log implements logger.Writer.
|
2024-11-30 10:23:41 +00:00
|
|
|
func (r *Recorder) Log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
r.Parent.Log(level, "[recorder] "+format, args...)
|
2023-09-16 15:27:07 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 19:40:44 +00:00
|
|
|
// Close closes the agent.
|
2024-11-30 10:23:41 +00:00
|
|
|
func (r *Recorder) Close() {
|
|
|
|
r.Log(logger.Info, "recording stopped")
|
|
|
|
close(r.terminate)
|
|
|
|
<-r.done
|
2023-10-26 19:40:44 +00:00
|
|
|
}
|
2023-09-16 15:27:07 +00:00
|
|
|
|
2024-11-30 10:23:41 +00:00
|
|
|
func (r *Recorder) run() {
|
|
|
|
defer close(r.done)
|
2023-10-26 19:40:44 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2024-11-30 10:23:41 +00:00
|
|
|
case <-r.currentInstance.done:
|
|
|
|
r.currentInstance.close()
|
|
|
|
case <-r.terminate:
|
|
|
|
r.currentInstance.close()
|
2023-10-26 19:40:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2024-11-30 10:23:41 +00:00
|
|
|
case <-time.After(r.restartPause):
|
|
|
|
case <-r.terminate:
|
2023-10-26 19:40:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-30 10:23:41 +00:00
|
|
|
r.currentInstance = &recorderInstance{
|
|
|
|
rec: r,
|
2023-10-26 19:40:44 +00:00
|
|
|
}
|
2024-11-30 10:23:41 +00:00
|
|
|
r.currentInstance.initialize()
|
2023-09-16 15:27:07 +00:00
|
|
|
}
|
|
|
|
}
|