-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathwait-tracks.go
93 lines (84 loc) · 1.82 KB
/
wait-tracks.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
package engine
import (
"m7s.live/engine/v4/common"
. "m7s.live/engine/v4/common"
"m7s.live/engine/v4/track"
"m7s.live/engine/v4/util"
)
type waitTrackNames []string
// Waiting是否正在等待
func (w waitTrackNames) Waiting() bool {
return w != nil
}
// Waitany 是否等待任意的
func (w waitTrackNames) Waitany() bool {
return len(w) == 0
}
// Wait 设置需要等待的名称,空数组为等待任意的
func (w *waitTrackNames) Wait(names ...string) {
if names == nil {
*w = make([]string, 0)
} else {
*w = names
}
}
// StopWait 不再需要等待了
func (w *waitTrackNames) StopWait() {
*w = nil
}
func (w waitTrackNames) InviteTrack(suber ISubscriber) {
if len(w) > 0 {
InviteTrack(w[0], suber)
}
}
// Accept 检查名称是否在等待候选项中
func (w *waitTrackNames) Accept(name string) bool {
if !w.Waiting() {
return false
}
if w.Waitany() {
w.StopWait()
return true
} else {
for _, n := range *w {
if n == name {
w.StopWait()
return true
}
}
}
return false
}
type waitTracks struct {
*util.Promise[ISubscriber] // 等待中的Promise
audio waitTrackNames
video waitTrackNames
data waitTrackNames
}
// NeedWait 是否需要等待Track
func (w *waitTracks) NeedWait() bool {
return w.audio.Waiting() || w.video.Waiting() || w.data.Waiting()
}
// Accept 有新的Track来到,检查是否可以不再需要等待了
func (w *waitTracks) Accept(t Track) bool {
suber := w.Promise.Value
switch t.(type) {
case *track.Audio:
if w.audio.Accept(t.GetName()) {
suber.OnEvent(t)
}
case *track.Video:
if w.video.Accept(t.GetName()) {
suber.OnEvent(t)
}
case common.Track:
w.data.Accept(t.GetName())
suber.OnEvent(t)
}
if w.NeedWait() {
return false
} else {
w.Resolve()
return true
}
}