forked from bluenviron/gohlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuxer_segment_fmp4.go
200 lines (169 loc) · 3.94 KB
/
muxer_segment_fmp4.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package gohlslib
import (
"fmt"
"io"
"time"
"github.com/bluenviron/gohlslib/pkg/storage"
)
type muxerSegmentFMP4 struct {
lowLatency bool
id uint64
startNTP time.Time
startDTS time.Duration
segmentMaxSize uint64
videoTrack *Track
audioTrack *Track
audioTimeScale uint32
prefix string
forceSwitched bool
genPartID func() uint64
publishPart func(*muxerPart)
name string
storage storage.File
size uint64
parts []*muxerPart
currentPart *muxerPart
endDTS time.Duration
}
func newMuxerSegmentFMP4(
lowLatency bool,
id uint64,
startNTP time.Time,
startDTS time.Duration,
segmentMaxSize uint64,
videoTrack *Track,
audioTrack *Track,
audioTimeScale uint32,
prefix string,
forceSwitched bool,
factory storage.Factory,
genPartID func() uint64,
publishPart func(*muxerPart),
) (*muxerSegmentFMP4, error) {
s := &muxerSegmentFMP4{
lowLatency: lowLatency,
id: id,
startNTP: startNTP,
startDTS: startDTS,
segmentMaxSize: segmentMaxSize,
videoTrack: videoTrack,
audioTrack: audioTrack,
audioTimeScale: audioTimeScale,
prefix: prefix,
forceSwitched: forceSwitched,
genPartID: genPartID,
publishPart: publishPart,
name: segmentName(prefix, id, true),
}
var err error
s.storage, err = factory.NewFile(s.name)
if err != nil {
return nil, err
}
s.currentPart = newMuxerPart(
startDTS,
s.videoTrack,
s.audioTrack,
s.audioTimeScale,
prefix,
s.genPartID(),
s.storage.NewPart(),
)
return s, nil
}
func (s *muxerSegmentFMP4) close() {
s.storage.Remove()
}
func (s *muxerSegmentFMP4) getName() string {
return s.name
}
func (s *muxerSegmentFMP4) getDuration() time.Duration {
return s.endDTS - s.startDTS
}
func (s *muxerSegmentFMP4) getSize() uint64 {
return s.storage.Size()
}
func (s *muxerSegmentFMP4) isForceSwitched() bool {
return s.forceSwitched
}
func (s *muxerSegmentFMP4) reader() (io.ReadCloser, error) {
return s.storage.Reader()
}
func (s *muxerSegmentFMP4) finalize(nextDTS time.Duration) error {
if s.currentPart.videoSamples != nil || s.currentPart.audioSamples != nil {
err := s.currentPart.finalize(nextDTS)
if err != nil {
return err
}
s.publishPart(s.currentPart)
s.parts = append(s.parts, s.currentPart)
}
s.currentPart = nil
s.storage.Finalize()
s.endDTS = nextDTS
return nil
}
func (s *muxerSegmentFMP4) writeVideo(
sample *augmentedVideoSample,
nextDTS time.Duration,
adjustedPartDuration time.Duration,
) error {
size := uint64(len(sample.Payload))
if (s.size + size) > s.segmentMaxSize {
return fmt.Errorf("reached maximum segment size")
}
s.size += size
s.currentPart.writeVideo(sample)
// switch part
if s.lowLatency &&
s.currentPart.computeDuration(nextDTS) >= adjustedPartDuration {
err := s.currentPart.finalize(nextDTS)
if err != nil {
return err
}
s.parts = append(s.parts, s.currentPart)
s.publishPart(s.currentPart)
s.currentPart = newMuxerPart(
nextDTS,
s.videoTrack,
s.audioTrack,
s.audioTimeScale,
s.prefix,
s.genPartID(),
s.storage.NewPart(),
)
}
return nil
}
func (s *muxerSegmentFMP4) writeAudio(
sample *augmentedAudioSample,
nextAudioSampleDTS time.Duration,
adjustedPartDuration time.Duration,
) error {
size := uint64(len(sample.Payload))
if (s.size + size) > s.segmentMaxSize {
return fmt.Errorf("reached maximum segment size")
}
s.size += size
s.currentPart.writeAudio(sample)
// switch part
if s.lowLatency && s.videoTrack == nil &&
s.currentPart.computeDuration(nextAudioSampleDTS) >= adjustedPartDuration {
err := s.currentPart.finalize(nextAudioSampleDTS)
if err != nil {
return err
}
s.parts = append(s.parts, s.currentPart)
s.publishPart(s.currentPart)
s.currentPart = newMuxerPart(
nextAudioSampleDTS,
s.videoTrack,
s.audioTrack,
s.audioTimeScale,
s.prefix,
s.genPartID(),
s.storage.NewPart(),
)
}
return nil
}