-
Notifications
You must be signed in to change notification settings - Fork 27
/
muxer_part.go
73 lines (57 loc) · 1.42 KB
/
muxer_part.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
61
62
63
64
65
66
67
68
69
70
71
72
73
package gohlslib
import (
"io"
"time"
"github.com/bluenviron/gohlslib/v2/pkg/storage"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
)
type muxerPart struct {
stream *muxerStream
segment *muxerSegmentFMP4
startDTS time.Duration
prefix string
id uint64
storage storage.Part
path string
isIndependent bool
endDTS time.Duration
}
func (p *muxerPart) initialize() {
p.path = partPath(p.prefix, p.stream.id, p.id)
}
func (p *muxerPart) reader() (io.ReadCloser, error) {
return p.storage.Reader()
}
func (p *muxerPart) getDuration() time.Duration {
return p.endDTS - p.startDTS
}
func (p *muxerPart) finalize(endDTS time.Duration) error {
part := fmp4.Part{
SequenceNumber: uint32(p.id),
}
for i, track := range p.stream.tracks {
if track.fmp4Samples != nil {
part.Tracks = append(part.Tracks, &fmp4.PartTrack{
ID: 1 + i,
BaseTime: uint64(track.fmp4StartDTS),
Samples: track.fmp4Samples,
})
track.fmp4Samples = nil
}
}
err := part.Marshal(p.storage.Writer())
if err != nil {
return err
}
p.endDTS = endDTS
return nil
}
func (p *muxerPart) writeSample(track *muxerTrack, sample *fmp4AugmentedSample) {
if track.fmp4Samples == nil {
track.fmp4StartDTS = sample.dts
}
if (track.isLeading || len(track.stream.tracks) == 1) && !sample.IsNonSyncSample {
p.isIndependent = true
}
track.fmp4Samples = append(track.fmp4Samples, &sample.PartSample)
}