-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
format_fmp4_track.go
60 lines (50 loc) · 1.35 KB
/
format_fmp4_track.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package record
import (
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
)
type formatFMP4Track struct {
f *formatFMP4
initTrack *fmp4.InitTrack
nextSample *sample
}
func (t *formatFMP4Track) record(sample *sample) error {
// wait the first video sample before setting hasVideo
if t.initTrack.Codec.IsVideo() {
t.f.hasVideo = true
}
sample, t.nextSample = t.nextSample, sample
if sample == nil {
return nil
}
sample.Duration = uint32(durationGoToMp4(t.nextSample.dts-sample.dts, t.initTrack.TimeScale))
if t.f.currentSegment == nil {
t.f.currentSegment = &formatFMP4Segment{
f: t.f,
startDTS: sample.dts,
startNTP: sample.ntp,
}
t.f.currentSegment.initialize()
// BaseTime is negative, this is not supported by fMP4. Reject the sample silently.
} else if (sample.dts - t.f.currentSegment.startDTS) < 0 {
return nil
}
err := t.f.currentSegment.record(t, sample)
if err != nil {
return err
}
if (!t.f.hasVideo || t.initTrack.Codec.IsVideo()) &&
!t.nextSample.IsNonSyncSample &&
(t.nextSample.dts-t.f.currentSegment.startDTS) >= t.f.a.agent.SegmentDuration {
err := t.f.currentSegment.close()
if err != nil {
return err
}
t.f.currentSegment = &formatFMP4Segment{
f: t.f,
startDTS: t.nextSample.dts,
startNTP: t.nextSample.ntp,
}
t.f.currentSegment.initialize()
}
return nil
}