-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathclient_stream_downloader.go
389 lines (319 loc) · 8.51 KB
/
client_stream_downloader.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package gohlslib
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/bluenviron/gohlslib/v2/pkg/playlist"
)
func findSegmentWithInvPosition(segments []*playlist.MediaSegment, invPos int) (*playlist.MediaSegment, int) {
index := len(segments) - invPos
if index < 0 {
return nil, 0
}
return segments[index], index
}
func findSegmentWithID(seqNo int, segments []*playlist.MediaSegment, id int) (*playlist.MediaSegment, int, int) {
index := int(int64(id) - int64(seqNo))
if index < 0 || index >= len(segments) {
return nil, 0, 0
}
return segments[index], index, len(segments) - index
}
func dateTimeOfPreloadHint(pl *playlist.Media) *time.Time {
if len(pl.Segments) == 0 {
return nil
}
lastSeg := pl.Segments[len(pl.Segments)-1]
if lastSeg.DateTime == nil {
return nil
}
d := lastSeg.DateTime.Add(lastSeg.Duration)
for _, part := range pl.Parts {
d = d.Add(part.Duration)
}
return &d
}
type clientStreamDownloaderClient interface {
setLeadingTimeConv(ts clientTimeConv)
waitLeadingTimeConv(ctx context.Context) bool
getLeadingTimeConv() clientTimeConv
}
type clientStreamDownloader struct {
isLeading bool
httpClient *http.Client
onRequest ClientOnRequestFunc
onDownloadStreamPlaylist ClientOnDownloadStreamPlaylistFunc
onDownloadSegment ClientOnDownloadSegmentFunc
onDownloadPart ClientOnDownloadPartFunc
onDecodeError ClientOnDecodeErrorFunc
playlistURL *url.URL
rendition *playlist.MultivariantRendition
firstPlaylist *playlist.Media
rp *clientRoutinePool
client clientStreamDownloaderClient
segmentQueue *clientSegmentQueue
curSegmentID *int
// out
chTracks chan []*Track
chEnded chan struct{}
// in
chStartStreaming chan map[*Track]*clientTrack
}
func (d *clientStreamDownloader) initialize() {
d.chTracks = make(chan []*Track)
d.chEnded = make(chan struct{})
d.chStartStreaming = make(chan map[*Track]*clientTrack)
}
func (d *clientStreamDownloader) run(ctx context.Context) error {
if d.firstPlaylist == nil {
var err error
d.firstPlaylist, err = d.downloadPlaylist(ctx, false)
if err != nil {
return err
}
}
d.segmentQueue = &clientSegmentQueue{}
d.segmentQueue.initialize()
if d.firstPlaylist.Map != nil && d.firstPlaylist.Map.URI != "" {
initFile, err := d.downloadSegment(
ctx,
d.firstPlaylist.Map.URI,
d.firstPlaylist.Map.ByteRangeStart,
d.firstPlaylist.Map.ByteRangeLength)
if err != nil {
return err
}
proc := &clientStreamProcessorFMP4{
ctx: ctx,
isLeading: d.isLeading,
rendition: d.rendition,
initFile: initFile,
segmentQueue: d.segmentQueue,
rp: d.rp,
streamDownloader: d,
client: d.client,
}
proc.initialize()
d.rp.add(proc)
} else {
proc := &clientStreamProcessorMPEGTS{
onDecodeError: d.onDecodeError,
isLeading: d.isLeading,
segmentQueue: d.segmentQueue,
rp: d.rp,
streamDownloader: d,
client: d.client,
}
proc.initialize()
d.rp.add(proc)
}
if d.firstPlaylist.ServerControl != nil &&
d.firstPlaylist.ServerControl.CanBlockReload &&
d.firstPlaylist.PreloadHint != nil {
return d.runLowLatency(ctx)
}
return d.runTraditional(ctx)
}
func (d *clientStreamDownloader) runLowLatency(ctx context.Context) error {
pl := d.firstPlaylist
for {
byts, err := d.downloadPreloadHint(ctx, pl.PreloadHint)
if err != nil {
return err
}
d.segmentQueue.push(&segmentData{
dateTime: dateTimeOfPreloadHint(pl),
payload: byts,
})
pl, err = d.downloadPlaylist(ctx, d.firstPlaylist.ServerControl.CanSkipUntil != nil)
if err != nil {
return err
}
if pl.PreloadHint == nil {
return fmt.Errorf("preload hint disappeared")
}
}
}
func (d *clientStreamDownloader) runTraditional(ctx context.Context) error {
pl := d.firstPlaylist
for {
err := d.fillSegmentQueue(ctx, pl)
if err != nil {
return err
}
ok := d.segmentQueue.waitUntilSizeIsBelow(ctx, 1)
if !ok {
return fmt.Errorf("terminated")
}
pl, err = d.downloadPlaylist(ctx, false)
if err != nil {
return err
}
}
}
func (d *clientStreamDownloader) downloadPlaylist(
ctx context.Context,
skipUntil bool,
) (*playlist.Media, error) {
ur := d.playlistURL
if skipUntil {
newUR := cloneURL(ur)
q := newUR.Query()
q.Add("_HLS_skip", "YES")
newUR.RawQuery = q.Encode()
ur = newUR
}
d.onDownloadStreamPlaylist(ur.String())
pl, err := downloadPlaylist(ctx, d.httpClient, d.onRequest, ur)
if err != nil {
return nil, err
}
plt, ok := pl.(*playlist.Media)
if !ok {
return nil, fmt.Errorf("invalid playlist")
}
return plt, nil
}
func (d *clientStreamDownloader) downloadPreloadHint(
ctx context.Context,
preloadHint *playlist.MediaPreloadHint,
) ([]byte, error) {
u, err := clientAbsoluteURL(d.playlistURL, preloadHint.URI)
if err != nil {
return nil, err
}
d.onDownloadPart(u.String())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
if preloadHint.ByteRangeLength != nil {
req.Header.Add("Range", "bytes="+strconv.FormatUint(preloadHint.ByteRangeStart, 10)+
"-"+strconv.FormatUint(preloadHint.ByteRangeStart+*preloadHint.ByteRangeLength-1, 10))
}
d.onRequest(req)
res, err := d.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusPartialContent {
return nil, fmt.Errorf("bad status code: %d", res.StatusCode)
}
byts, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return byts, nil
}
func (d *clientStreamDownloader) downloadSegment(
ctx context.Context,
uri string,
start *uint64,
length *uint64,
) ([]byte, error) {
u, err := clientAbsoluteURL(d.playlistURL, uri)
if err != nil {
return nil, err
}
d.onDownloadSegment(u.String())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
if length != nil {
if start == nil {
v := uint64(0)
start = &v
}
req.Header.Add("Range", "bytes="+strconv.FormatUint(*start, 10)+
"-"+strconv.FormatUint(*start+*length-1, 10))
}
d.onRequest(req)
res, err := d.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusPartialContent {
return nil, fmt.Errorf("bad status code: %d", res.StatusCode)
}
byts, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return byts, nil
}
func (d *clientStreamDownloader) fillSegmentQueue(
ctx context.Context,
pl *playlist.Media,
) error {
var seg *playlist.MediaSegment
var segPos int
if d.curSegmentID == nil {
if d.firstPlaylist.PlaylistType != nil &&
*d.firstPlaylist.PlaylistType == playlist.MediaPlaylistTypeVOD {
// VOD stream: start from the beginning
if len(pl.Segments) == 0 {
return fmt.Errorf("no segments found")
}
seg = pl.Segments[0]
} else {
// live stream: start from clientLiveInitialDistance
seg, segPos = findSegmentWithInvPosition(pl.Segments, clientLiveInitialDistance)
if seg == nil {
return fmt.Errorf("there aren't enough segments to fill the buffer")
}
}
} else {
var invPos int
seg, segPos, invPos = findSegmentWithID(pl.MediaSequence, pl.Segments, *d.curSegmentID+1)
if seg == nil {
return fmt.Errorf("next segment not found or not ready yet")
}
if !pl.Endlist && invPos > clientLiveMaxDistanceFromEnd {
return fmt.Errorf("playback is too late")
}
}
v := pl.MediaSequence + segPos
d.curSegmentID = &v
byts, err := d.downloadSegment(ctx, seg.URI, seg.ByteRangeStart, seg.ByteRangeLength)
if err != nil {
return err
}
d.segmentQueue.push(&segmentData{
dateTime: seg.DateTime,
payload: byts,
})
if pl.Endlist && pl.Segments[len(pl.Segments)-1] == seg {
d.segmentQueue.push(nil)
<-ctx.Done()
return fmt.Errorf("terminated")
}
return nil
}
func (d *clientStreamDownloader) setTracks(ctx context.Context, tracks []*Track) ([]*clientTrack, bool) {
select {
case d.chTracks <- tracks:
case <-ctx.Done():
return nil, false
}
var allTracks map[*Track]*clientTrack
select {
case allTracks = <-d.chStartStreaming:
case <-ctx.Done():
return nil, false
}
streamTracks := make([]*clientTrack, len(tracks))
for i, track := range tracks {
streamTracks[i] = allTracks[track]
}
return streamTracks, true
}
func (d *clientStreamDownloader) setEnded() {
close(d.chEnded)
}