2023-10-14 20:52:10 +00:00
|
|
|
package record
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
rtspformat "github.com/bluenviron/gortsplib/v4/pkg/format"
|
2023-10-14 20:52:10 +00:00
|
|
|
"github.com/bluenviron/mediacommon/pkg/codecs/ac3"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/codecs/h264"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4video"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
|
|
|
|
|
2024-01-15 11:08:14 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/defs"
|
2023-10-14 20:52:10 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/unit"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mpegtsMaxBufferSize = 64 * 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
func durationGoToMPEGTS(v time.Duration) int64 {
|
|
|
|
return int64(v.Seconds() * 90000)
|
|
|
|
}
|
|
|
|
|
|
|
|
type dynamicWriter struct {
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dynamicWriter) Write(p []byte) (int, error) {
|
|
|
|
return d.w.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dynamicWriter) setTarget(w io.Writer) {
|
|
|
|
d.w = w
|
|
|
|
}
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
type formatMPEGTS struct {
|
2023-10-26 19:40:44 +00:00
|
|
|
a *agentInstance
|
2023-10-14 20:52:10 +00:00
|
|
|
|
|
|
|
dw *dynamicWriter
|
|
|
|
bw *bufio.Writer
|
|
|
|
mw *mpegts.Writer
|
|
|
|
hasVideo bool
|
2023-12-02 14:03:37 +00:00
|
|
|
currentSegment *formatMPEGTSSegment
|
2023-10-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
func (f *formatMPEGTS) initialize() {
|
2023-10-14 20:52:10 +00:00
|
|
|
var tracks []*mpegts.Track
|
2024-01-15 11:08:14 +00:00
|
|
|
var formats []rtspformat.Format
|
2023-10-14 20:52:10 +00:00
|
|
|
|
2024-01-15 11:08:14 +00:00
|
|
|
addTrack := func(format rtspformat.Format, codec mpegts.Codec) *mpegts.Track {
|
2023-10-14 20:52:10 +00:00
|
|
|
track := &mpegts.Track{
|
|
|
|
Codec: codec,
|
|
|
|
}
|
2024-01-15 11:08:14 +00:00
|
|
|
|
2023-10-14 20:52:10 +00:00
|
|
|
tracks = append(tracks, track)
|
2024-01-15 11:08:14 +00:00
|
|
|
formats = append(formats, format)
|
2023-10-14 20:52:10 +00:00
|
|
|
return track
|
|
|
|
}
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
for _, media := range f.a.agent.Stream.Desc().Medias {
|
2023-10-14 20:52:10 +00:00
|
|
|
for _, forma := range media.Formats {
|
|
|
|
switch forma := forma.(type) {
|
2023-12-02 14:03:37 +00:00
|
|
|
case *rtspformat.H265:
|
2024-01-15 11:08:14 +00:00
|
|
|
track := addTrack(forma, &mpegts.CodecH265{})
|
2023-10-14 20:52:10 +00:00
|
|
|
|
|
|
|
var dtsExtractor *h265.DTSExtractor
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
tunit := u.(*unit.H265)
|
|
|
|
if tunit.AU == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
randomAccess := h265.IsRandomAccess(tunit.AU)
|
|
|
|
|
|
|
|
if dtsExtractor == nil {
|
|
|
|
if !randomAccess {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dtsExtractor = h265.NewDTSExtractor()
|
|
|
|
}
|
|
|
|
|
|
|
|
dts, err := dtsExtractor.Extract(tunit.AU, tunit.PTS)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
return f.recordH26x(track, tunit.PTS, dts, tunit.NTP, randomAccess, tunit.AU)
|
2023-10-14 20:52:10 +00:00
|
|
|
})
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
case *rtspformat.H264:
|
2024-01-15 11:08:14 +00:00
|
|
|
track := addTrack(forma, &mpegts.CodecH264{})
|
2023-10-14 20:52:10 +00:00
|
|
|
|
|
|
|
var dtsExtractor *h264.DTSExtractor
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
tunit := u.(*unit.H264)
|
|
|
|
if tunit.AU == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
idrPresent := h264.IDRPresent(tunit.AU)
|
|
|
|
|
|
|
|
if dtsExtractor == nil {
|
|
|
|
if !idrPresent {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dtsExtractor = h264.NewDTSExtractor()
|
|
|
|
}
|
|
|
|
|
|
|
|
dts, err := dtsExtractor.Extract(tunit.AU, tunit.PTS)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
return f.recordH26x(track, tunit.PTS, dts, tunit.NTP, idrPresent, tunit.AU)
|
2023-10-14 20:52:10 +00:00
|
|
|
})
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
case *rtspformat.MPEG4Video:
|
2024-01-15 11:08:14 +00:00
|
|
|
track := addTrack(forma, &mpegts.CodecMPEG4Video{})
|
2023-10-14 20:52:10 +00:00
|
|
|
|
|
|
|
firstReceived := false
|
|
|
|
var lastPTS time.Duration
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
tunit := u.(*unit.MPEG4Video)
|
|
|
|
if tunit.Frame == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !firstReceived {
|
|
|
|
firstReceived = true
|
|
|
|
} else if tunit.PTS < lastPTS {
|
|
|
|
return fmt.Errorf("MPEG-4 Video streams with B-frames are not supported (yet)")
|
|
|
|
}
|
|
|
|
lastPTS = tunit.PTS
|
|
|
|
|
|
|
|
f.hasVideo = true
|
|
|
|
randomAccess := bytes.Contains(tunit.Frame, []byte{0, 0, 1, byte(mpeg4video.GroupOfVOPStartCode)})
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
err := f.setupSegment(tunit.PTS, tunit.NTP, true, randomAccess)
|
2023-10-14 20:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.mw.WriteMPEG4Video(track, durationGoToMPEGTS(tunit.PTS), tunit.Frame)
|
|
|
|
})
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
case *rtspformat.MPEG1Video:
|
2024-01-15 11:08:14 +00:00
|
|
|
track := addTrack(forma, &mpegts.CodecMPEG1Video{})
|
2023-10-14 20:52:10 +00:00
|
|
|
|
|
|
|
firstReceived := false
|
|
|
|
var lastPTS time.Duration
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
tunit := u.(*unit.MPEG1Video)
|
|
|
|
if tunit.Frame == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !firstReceived {
|
|
|
|
firstReceived = true
|
|
|
|
} else if tunit.PTS < lastPTS {
|
|
|
|
return fmt.Errorf("MPEG-1 Video streams with B-frames are not supported (yet)")
|
|
|
|
}
|
|
|
|
lastPTS = tunit.PTS
|
|
|
|
|
|
|
|
f.hasVideo = true
|
|
|
|
randomAccess := bytes.Contains(tunit.Frame, []byte{0, 0, 1, 0xB8})
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
err := f.setupSegment(tunit.PTS, tunit.NTP, true, randomAccess)
|
2023-10-14 20:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.mw.WriteMPEG1Video(track, durationGoToMPEGTS(tunit.PTS), tunit.Frame)
|
|
|
|
})
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
case *rtspformat.Opus:
|
2024-01-15 11:08:14 +00:00
|
|
|
track := addTrack(forma, &mpegts.CodecOpus{
|
2023-10-14 20:52:10 +00:00
|
|
|
ChannelCount: func() int {
|
|
|
|
if forma.IsStereo {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}(),
|
|
|
|
})
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
tunit := u.(*unit.Opus)
|
|
|
|
if tunit.Packets == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
err := f.setupSegment(tunit.PTS, tunit.NTP, false, true)
|
2023-10-14 20:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.mw.WriteOpus(track, durationGoToMPEGTS(tunit.PTS), tunit.Packets)
|
|
|
|
})
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
case *rtspformat.MPEG4Audio:
|
2024-01-15 11:08:14 +00:00
|
|
|
track := addTrack(forma, &mpegts.CodecMPEG4Audio{
|
2023-10-14 20:52:10 +00:00
|
|
|
Config: *forma.GetConfig(),
|
|
|
|
})
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
tunit := u.(*unit.MPEG4Audio)
|
|
|
|
if tunit.AUs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
err := f.setupSegment(tunit.PTS, tunit.NTP, false, true)
|
2023-10-14 20:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.mw.WriteMPEG4Audio(track, durationGoToMPEGTS(tunit.PTS), tunit.AUs)
|
|
|
|
})
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
case *rtspformat.MPEG1Audio:
|
2024-01-15 11:08:14 +00:00
|
|
|
track := addTrack(forma, &mpegts.CodecMPEG1Audio{})
|
2023-10-14 20:52:10 +00:00
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
tunit := u.(*unit.MPEG1Audio)
|
|
|
|
if tunit.Frames == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
err := f.setupSegment(tunit.PTS, tunit.NTP, false, true)
|
2023-10-14 20:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.mw.WriteMPEG1Audio(track, durationGoToMPEGTS(tunit.PTS), tunit.Frames)
|
|
|
|
})
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
case *rtspformat.AC3:
|
2024-01-15 11:08:14 +00:00
|
|
|
track := addTrack(forma, &mpegts.CodecAC3{})
|
2023-10-14 20:52:10 +00:00
|
|
|
|
|
|
|
sampleRate := time.Duration(forma.SampleRate)
|
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
tunit := u.(*unit.AC3)
|
|
|
|
if tunit.Frames == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, frame := range tunit.Frames {
|
|
|
|
framePTS := tunit.PTS + time.Duration(i)*ac3.SamplesPerFrame*
|
|
|
|
time.Second/sampleRate
|
|
|
|
|
|
|
|
err := f.mw.WriteAC3(track, durationGoToMPEGTS(framePTS), frame)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f.dw = &dynamicWriter{}
|
|
|
|
f.bw = bufio.NewWriterSize(f.dw, mpegtsMaxBufferSize)
|
|
|
|
f.mw = mpegts.NewWriter(f.bw, tracks)
|
|
|
|
|
2024-01-15 11:08:14 +00:00
|
|
|
f.a.agent.Log(logger.Info, "recording %s",
|
|
|
|
defs.FormatsInfo(formats))
|
2023-10-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 14:03:37 +00:00
|
|
|
func (f *formatMPEGTS) close() {
|
2023-10-14 20:52:10 +00:00
|
|
|
if f.currentSegment != nil {
|
|
|
|
f.currentSegment.close() //nolint:errcheck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
func (f *formatMPEGTS) setupSegment(
|
|
|
|
dts time.Duration,
|
|
|
|
ntp time.Time,
|
|
|
|
isVideo bool,
|
|
|
|
randomAccess bool,
|
|
|
|
) error {
|
2023-10-14 20:52:10 +00:00
|
|
|
switch {
|
|
|
|
case f.currentSegment == nil:
|
2024-01-21 18:15:31 +00:00
|
|
|
f.currentSegment = &formatMPEGTSSegment{
|
|
|
|
f: f,
|
|
|
|
startDTS: dts,
|
|
|
|
startNTP: ntp,
|
|
|
|
}
|
|
|
|
f.currentSegment.initialize()
|
2023-10-14 20:52:10 +00:00
|
|
|
case (!f.hasVideo || isVideo) &&
|
|
|
|
randomAccess &&
|
2023-12-02 14:15:17 +00:00
|
|
|
(dts-f.currentSegment.startDTS) >= f.a.agent.SegmentDuration:
|
2023-10-14 20:52:10 +00:00
|
|
|
err := f.currentSegment.close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
f.currentSegment = &formatMPEGTSSegment{
|
|
|
|
f: f,
|
|
|
|
startDTS: dts,
|
|
|
|
startNTP: ntp,
|
|
|
|
}
|
|
|
|
f.currentSegment.initialize()
|
2023-10-14 20:52:10 +00:00
|
|
|
|
2023-12-02 14:15:17 +00:00
|
|
|
case (dts - f.currentSegment.lastFlush) >= f.a.agent.PartDuration:
|
2023-10-14 20:52:10 +00:00
|
|
|
err := f.bw.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.currentSegment.lastFlush = dts
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
func (f *formatMPEGTS) recordH26x(
|
|
|
|
track *mpegts.Track,
|
|
|
|
pts time.Duration,
|
|
|
|
dts time.Duration,
|
|
|
|
ntp time.Time,
|
|
|
|
randomAccess bool,
|
|
|
|
au [][]byte,
|
2023-10-14 20:52:10 +00:00
|
|
|
) error {
|
|
|
|
f.hasVideo = true
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
err := f.setupSegment(dts, ntp, true, randomAccess)
|
2023-10-14 20:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:15:31 +00:00
|
|
|
return f.mw.WriteH26x(track, durationGoToMPEGTS(pts), durationGoToMPEGTS(dts), randomAccess, au)
|
2023-10-14 20:52:10 +00:00
|
|
|
}
|