/
blockwait.go
125 lines (116 loc) · 2.44 KB
/
blockwait.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
package hls
import (
"context"
"errors"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"eaglesong.dev/hls/internal/segment"
)
type (
subscriber chan struct{}
subMap map[subscriber]struct{}
)
// block until segment with the given number is ready or ctx is cancelled
func (p *Publisher) waitForSegment(ctx context.Context, want segment.PartMSN) hlsState {
ctx, cancel := context.WithTimeout(ctx, 35*time.Second)
defer cancel()
// subscribe to segment updates
ch := p.addSub()
defer p.delSub(ch)
for {
state, _ := p.state.Load().(hlsState)
if !state.Valid() {
// publisher closed
return state
}
if state.complete.Satisfies(want) {
return state
}
// wait for notify or for timeout/disconnect
select {
case <-ch:
case <-ctx.Done():
return hlsState{}
}
}
}
func (p *Publisher) addSub() subscriber {
ch := make(subscriber, 1)
p.subsMu.Lock()
if p.subs == nil {
p.subs = make(subMap)
}
p.subs[ch] = struct{}{}
p.subsMu.Unlock()
return ch
}
func (p *Publisher) delSub(ch subscriber) {
p.subsMu.Lock()
delete(p.subs, ch)
p.subsMu.Unlock()
}
// notify subscribers that segment is ready
func (p *Publisher) notifySegment() {
p.subsMu.Lock()
defer p.subsMu.Unlock()
for sub := range p.subs {
// non-blocking send
select {
case sub <- struct{}{}:
default:
}
}
}
func parseBlock(q url.Values) (want segment.PartMSN, err error) {
want = segment.PartMSN{MSN: -1, Part: -1}
v := q.Get("_HLS_msn")
if v == "" {
// not blocking
return
}
vv, err := strconv.ParseInt(v, 10, 64)
if err != nil || vv < 0 {
return want, errors.New("invalid _HLS_msn")
}
want.MSN = segment.MSN(vv)
v = q.Get("_HLS_part")
if v == "" {
// block for whole segment
return
}
vv, err = strconv.ParseInt(v, 10, 64)
if err != nil || vv < 0 {
return want, errors.New("invalid _HLS_part")
}
// block for part
want.Part = int(vv)
return
}
func (p *Publisher) waitForEtag(req *http.Request, state hlsState) hlsState {
previous := strings.TrimPrefix(req.Header.Get("If-None-Match"), "W/")
if previous == "" || state.mpd.etag == "" || state.mpd.etag != previous {
return state
}
ctx, cancel := context.WithTimeout(req.Context(), 10*time.Second)
defer cancel()
ch := p.addSub()
defer p.delSub(ch)
for {
state, _ := p.state.Load().(hlsState)
if !state.Valid() {
// publisher closed
return state
}
if state.mpd.etag != previous {
return state
}
select {
case <-ch:
case <-ctx.Done():
return state
}
}
}