mirror of
https://github.com/bluenviron/mediamtx.git
synced 2025-12-20 02:00:05 -08:00
* normalize variable names * fix file name * fix crash when recording a stream with unsupported tracks (#3978)
104 lines
2.2 KiB
Go
104 lines
2.2 KiB
Go
package recorder
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
|
|
"github.com/bluenviron/mediacommon/pkg/formats/fmp4/seekablebuffer"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
|
"github.com/bluenviron/mediamtx/internal/recordstore"
|
|
)
|
|
|
|
func writePart(
|
|
f io.Writer,
|
|
sequenceNumber uint32,
|
|
partTracks map[*formatFMP4Track]*fmp4.PartTrack,
|
|
) error {
|
|
fmp4PartTracks := make([]*fmp4.PartTrack, len(partTracks))
|
|
i := 0
|
|
for _, partTrack := range partTracks {
|
|
fmp4PartTracks[i] = partTrack
|
|
i++
|
|
}
|
|
|
|
part := &fmp4.Part{
|
|
SequenceNumber: sequenceNumber,
|
|
Tracks: fmp4PartTracks,
|
|
}
|
|
|
|
var buf seekablebuffer.Buffer
|
|
err := part.Marshal(&buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = f.Write(buf.Bytes())
|
|
return err
|
|
}
|
|
|
|
type formatFMP4Part struct {
|
|
s *formatFMP4Segment
|
|
sequenceNumber uint32
|
|
startDTS time.Duration
|
|
|
|
partTracks map[*formatFMP4Track]*fmp4.PartTrack
|
|
endDTS time.Duration
|
|
}
|
|
|
|
func (p *formatFMP4Part) initialize() {
|
|
p.partTracks = make(map[*formatFMP4Track]*fmp4.PartTrack)
|
|
}
|
|
|
|
func (p *formatFMP4Part) close() error {
|
|
if p.s.fi == nil {
|
|
p.s.path = recordstore.Path{Start: p.s.startNTP}.Encode(p.s.f.ri.pathFormat)
|
|
p.s.f.ri.Log(logger.Debug, "creating segment %s", p.s.path)
|
|
|
|
err := os.MkdirAll(filepath.Dir(p.s.path), 0o755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fi, err := os.Create(p.s.path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.s.f.ri.rec.OnSegmentCreate(p.s.path)
|
|
|
|
err = writeInit(fi, p.s.f.tracks)
|
|
if err != nil {
|
|
fi.Close()
|
|
return err
|
|
}
|
|
|
|
p.s.fi = fi
|
|
}
|
|
|
|
return writePart(p.s.fi, p.sequenceNumber, p.partTracks)
|
|
}
|
|
|
|
func (p *formatFMP4Part) write(track *formatFMP4Track, sample *sample, dtsDuration time.Duration) error {
|
|
partTrack, ok := p.partTracks[track]
|
|
if !ok {
|
|
partTrack = &fmp4.PartTrack{
|
|
ID: track.initTrack.ID,
|
|
BaseTime: uint64(multiplyAndDivide(int64(dtsDuration-p.s.startDTS),
|
|
int64(track.initTrack.TimeScale), int64(time.Second))),
|
|
}
|
|
p.partTracks[track] = partTrack
|
|
}
|
|
|
|
partTrack.Samples = append(partTrack.Samples, sample.PartSample)
|
|
p.endDTS = dtsDuration
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *formatFMP4Part) duration() time.Duration {
|
|
return p.endDTS - p.startDTS
|
|
}
|