golanggohlsrtmpwebrtcmedia-serverobs-studiortcprtmp-proxyrtmp-serverrtprtsprtsp-proxyrtsp-relayrtsp-serversrtstreamingwebrtc-proxy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
1.8 KiB
96 lines
1.8 KiB
package record |
|
|
|
import ( |
|
"io" |
|
"os" |
|
"time" |
|
|
|
"github.com/bluenviron/mediacommon/pkg/formats/fmp4" |
|
"github.com/bluenviron/mediacommon/pkg/formats/fmp4/seekablebuffer" |
|
|
|
"github.com/bluenviron/mediamtx/internal/logger" |
|
) |
|
|
|
func writeInit(f io.Writer, tracks []*formatFMP4Track) error { |
|
fmp4Tracks := make([]*fmp4.InitTrack, len(tracks)) |
|
for i, track := range tracks { |
|
fmp4Tracks[i] = track.initTrack |
|
} |
|
|
|
init := fmp4.Init{ |
|
Tracks: fmp4Tracks, |
|
} |
|
|
|
var buf seekablebuffer.Buffer |
|
err := init.Marshal(&buf) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
_, err = f.Write(buf.Bytes()) |
|
return err |
|
} |
|
|
|
type formatFMP4Segment struct { |
|
f *formatFMP4 |
|
startDTS time.Duration |
|
startNTP time.Time |
|
|
|
path string |
|
fi *os.File |
|
curPart *formatFMP4Part |
|
} |
|
|
|
func (s *formatFMP4Segment) initialize() { |
|
} |
|
|
|
func (s *formatFMP4Segment) close() error { |
|
var err error |
|
|
|
if s.curPart != nil { |
|
err = s.curPart.close() |
|
} |
|
|
|
if s.fi != nil { |
|
s.f.a.agent.Log(logger.Debug, "closing segment %s", s.path) |
|
err2 := s.fi.Close() |
|
if err == nil { |
|
err = err2 |
|
} |
|
|
|
if err2 == nil { |
|
s.f.a.agent.OnSegmentComplete(s.path) |
|
} |
|
} |
|
|
|
return err |
|
} |
|
|
|
func (s *formatFMP4Segment) record(track *formatFMP4Track, sample *sample) error { |
|
if s.curPart == nil { |
|
s.curPart = &formatFMP4Part{ |
|
s: s, |
|
sequenceNumber: s.f.nextSequenceNumber, |
|
startDTS: sample.dts, |
|
} |
|
s.curPart.initialize() |
|
s.f.nextSequenceNumber++ |
|
} else if s.curPart.duration() >= s.f.a.agent.PartDuration { |
|
err := s.curPart.close() |
|
s.curPart = nil |
|
|
|
if err != nil { |
|
return err |
|
} |
|
|
|
s.curPart = &formatFMP4Part{ |
|
s: s, |
|
sequenceNumber: s.f.nextSequenceNumber, |
|
startDTS: sample.dts, |
|
} |
|
s.curPart.initialize() |
|
s.f.nextSequenceNumber++ |
|
} |
|
|
|
return s.curPart.record(track, sample) |
|
}
|
|
|