-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
257 lines (232 loc) · 7.16 KB
/
syncer.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
package syncer
import (
"io"
log "github.com/pion/ion-log"
"github.com/pion/ion/pkg/ion"
pbion "github.com/pion/ion/proto/ion"
"github.com/yindaheng98/dion/pkg/isglb"
pb "github.com/yindaheng98/dion/proto"
"github.com/yindaheng98/dion/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
// ISGLBSyncer is a ISGLBClient to sync SFUStatus
type ISGLBSyncer struct {
client *isglb.ISGLBClient
node *ion.Node
descSFU *pbion.Node
router trackRouter
reporter *qualityReporter
session SessionTracker
clientSet *util.DisorderSet
forwardTrackSet *util.DisorderSet
proceedTrackSet *util.DisorderSet
// Just recv and send latest status
statusRecvCh chan *pb.SFUStatus
statusSendCh chan bool
sessionEventCh chan *SessionEvent
}
func NewSFUStatusSyncer(node *ion.Node, peerID string, descSFU *pbion.Node, toolbox ToolBox) *ISGLBSyncer {
isglbClient := isglb.NewISGLBClient(node, peerID, map[string]interface{}{})
if isglbClient == nil {
return nil
}
forwarder, processor, session := toolbox.TrackForwarder, toolbox.TrackProcessor, toolbox.SessionTracker
if forwarder == nil {
forwarder = StupidTrackForwarder{}
}
if processor == nil {
processor = StupidTrackProcesser{}
}
if session == nil {
session = StupidSessionTracker{}
}
tr, cr := toolbox.TransmissionReporter, toolbox.ComputationReporter
if tr == nil {
tr = &StupidTransmissionReporter{}
}
if cr == nil {
cr = &StupidComputationReporter{}
}
s := &ISGLBSyncer{
client: isglbClient,
node: node,
descSFU: descSFU,
router: trackRouter{
TrackForwarder: forwarder,
TrackProcessor: processor,
},
reporter: newQualityReporter(tr, cr),
session: session,
clientSet: util.NewDisorderSet(),
forwardTrackSet: util.NewDisorderSet(),
proceedTrackSet: util.NewDisorderSet(),
statusRecvCh: make(chan *pb.SFUStatus, 1),
statusSendCh: make(chan bool, 1),
sessionEventCh: make(chan *SessionEvent, 1024),
}
isglbClient.OnSFUStatusRecv = func(st *pb.SFUStatus) {
select {
case _, ok := <-s.statusRecvCh:
if !ok {
return
}
default:
}
s.statusRecvCh <- st
}
return s
}
func (s *ISGLBSyncer) NotifySFUStatus() {
// Only send latest status
select {
case s.statusSendCh <- true:
default:
}
}
// ↓↓↓↓↓ should access Index, so keep single thread ↓↓↓↓↓
// getSelfStatus get the current SFUStatus
// MUST be single threaded
func (s *ISGLBSyncer) getSelfStatus() *pb.SFUStatus {
return &pb.SFUStatus{
SFU: proto.Clone(s.descSFU).(*pbion.Node),
ForwardTracks: util.ItemList(s.forwardTrackSet.Sort()).ToForwardTracks(),
ProceedTracks: util.ItemList(s.proceedTrackSet.Sort()).ToProceedTracks(),
Clients: util.ItemList(s.clientSet.Sort()).ToClientSessions(),
}
}
// syncStatus sync the current SFUStatus with the expected SFUStatus
// MUST be single threaded
func (s *ISGLBSyncer) syncStatus(expectedStatus *pb.SFUStatus) {
if expectedStatus.SFU.String() != s.descSFU.String() { // Check if the SFU status is mine
// If not
log.Warnf("Received SFU status is not mine, drop it: %s", expectedStatus.SFU)
s.NotifySFUStatus() // The server must re-consider the status for our SFU
return // And we should wait for the right SFU status to come
}
// Check if the client needed Client is same
sessionIndexDataList := util.ClientNeededSessions(expectedStatus.Clients).ToDisorderSetItemList()
if !s.clientSet.IsSame(sessionIndexDataList) { // Check if the ClientNeededSessions is same
// If not
log.Warnf("Received SFU status have different Client list, drop it: %s", expectedStatus.Clients)
s.NotifySFUStatus() // The server must re-consider the status for our SFU
return // And we should wait for the right SFU status to come
}
// Perform track forward change
forwardIndexDataList := util.ForwardTracks(expectedStatus.ForwardTracks).ToDisorderSetItemList()
forwardAdd, forwardDel, forwardReplace := s.forwardTrackSet.Update(forwardIndexDataList)
for _, track := range forwardDel {
s.router.StopForwardTrack(track.(util.ForwardTrackItem).Track)
}
for _, track := range forwardReplace {
s.router.ReplaceForwardTrack(
track.Old.(util.ForwardTrackItem).Track,
track.New.(util.ForwardTrackItem).Track,
)
}
for _, track := range forwardAdd {
s.router.StartForwardTrack(track.(util.ForwardTrackItem).Track)
}
//Perform track proceed change
proceedIndexDataList := util.ProceedTracks(expectedStatus.ProceedTracks).ToDisorderSetItemList()
proceedAdd, proceedDel, proceedReplace := s.proceedTrackSet.Update(proceedIndexDataList)
for _, track := range proceedDel {
s.router.StopProceedTrack(track.(util.ProceedTrackItem).Track)
}
for _, track := range proceedReplace {
s.router.ReplaceProceedTrack(
track.Old.(util.ProceedTrackItem).Track,
track.New.(util.ProceedTrackItem).Track,
)
}
for _, track := range proceedAdd {
s.router.StartProceedTrack(track.(util.ProceedTrackItem).Track)
}
}
// handleSessionEvent handle the SessionEvent
// MUST be single threaded
func (s *ISGLBSyncer) handleSessionEvent(event *SessionEvent) {
// Just add or remove it, and sand latest status
switch event.State {
case SessionEvent_ADD:
s.clientSet.Add(util.ClientNeededSessionItem{Client: event.Session})
s.NotifySFUStatus()
case SessionEvent_REMOVE:
s.clientSet.Del(util.ClientNeededSessionItem{Client: event.Session})
s.NotifySFUStatus()
}
}
// main is the "main function" goroutine of the NewSFUStatusSyncer
// All the methods about Index should be here, to ensure the assess is single-threaded
func (s *ISGLBSyncer) main() {
for {
select {
case event, ok := <-s.sessionEventCh: // handle an event
if !ok {
return
}
s.handleSessionEvent(event) // should access Index, so keep single thread
case st, ok := <-s.statusRecvCh: // handle a received SFU status
if !ok {
return
}
s.syncStatus(st) // should access Index, so keep single thread
case _, ok := <-s.statusSendCh: // handle SFU status send event
if !ok {
return
}
st := s.getSelfStatus() // should access Index, so keep single thread
go s.send(&pb.SyncRequest{Request: &pb.SyncRequest_Status{Status: st}})
}
}
}
// ↑↑↑↑↑ should access Index, so keep single thread ↑↑↑↑↑
func (s *ISGLBSyncer) sessionFetcher() {
defer func() {
if err := recover(); err != nil {
log.Debugf("error on close: %+v", err)
}
}()
for {
event := s.session.FetchSessionEvent()
if event == nil {
return
}
s.sessionEventCh <- event.Clone()
}
}
func (s *ISGLBSyncer) reportFetcher() {
for {
report := s.reporter.FetchReport()
if report == nil {
return
}
go s.send(&pb.SyncRequest{Request: &pb.SyncRequest_Report{Report: report}})
}
}
func (s *ISGLBSyncer) send(r *pb.SyncRequest) {
err := s.client.SendSyncRequest(r)
if err != nil {
if err == io.EOF {
return
}
errStatus, _ := status.FromError(err)
if errStatus.Code() == codes.Canceled {
return
}
log.Errorf("%v SFU request send error", err)
}
}
func (s *ISGLBSyncer) Start() {
go s.main()
go s.reportFetcher()
go s.sessionFetcher()
s.client.Connect()
}
func (s *ISGLBSyncer) Stop() {
s.client.Close()
close(s.statusRecvCh)
close(s.statusSendCh)
close(s.sessionEventCh)
}