This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
367 lines (314 loc) · 10.9 KB
/
client.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
package biz
import (
"encoding/json"
"fmt"
nprotoo "github.com/cloudwebrtc/nats-protoo"
"github.com/pion/ion/pkg/discovery"
"github.com/pion/ion/pkg/log"
"github.com/pion/ion/pkg/proto"
"github.com/pion/ion/pkg/signal"
"github.com/pion/ion/pkg/util"
)
// Entry is the biz entry
func Entry(method string, peer *signal.Peer, msg map[string]interface{}, accept signal.AcceptFunc, reject signal.RejectFunc) {
log.Infof("method => %s, data => %v", method, msg)
var result map[string]interface{}
err := util.NewNpError(400, fmt.Sprintf("Unkown method [%s]", method))
switch method {
case proto.ClientClose:
result, err = clientClose(peer, msg)
case proto.ClientLogin:
result, err = login(peer, msg)
case proto.ClientJoin:
result, err = join(peer, msg)
case proto.ClientLeave:
result, err = leave(peer, msg)
case proto.ClientPublish:
result, err = publish(peer, msg)
case proto.ClientUnPublish:
result, err = unpublish(peer, msg)
case proto.ClientSubscribe:
result, err = subscribe(peer, msg)
case proto.ClientUnSubscribe:
result, err = unsubscribe(peer, msg)
case proto.ClientBroadcast:
result, err = broadcast(peer, msg)
case proto.ClientTrickleICE:
result, err = trickle(peer, msg)
}
if err != nil {
reject(err.Code, err.Reason)
} else {
accept(result)
}
}
func getRPCForIslb() (*nprotoo.Requestor, bool) {
for _, item := range services {
if item.Info["service"] == "islb" {
id := item.Info["id"]
rpc, found := rpcs[id]
if !found {
rpcID := discovery.GetRPCChannel(item)
log.Infof("Create rpc [%s] for islb", rpcID)
rpc = protoo.NewRequestor(rpcID)
rpcs[id] = rpc
}
return rpc, true
}
}
log.Warnf("No islb node was found.")
return nil, false
}
func handleSFUBroadCast(msg map[string]interface{}, subj string) {
go func(msg map[string]interface{}) {
method := util.Val(msg, "method")
data := msg["data"].(map[string]interface{})
log.Infof("handleSFUBroadCast: method=%s, data=%v", method, data)
rid := util.Val(data, "rid")
uid := util.Val(data, "uid")
switch method {
case proto.SFUTrickleICE:
signal.NotifyAllWithoutID(rid, uid, proto.ClientOnStreamAdd, data)
case proto.SFUStreamRemove:
mid := util.Val(data, "mid")
islb, found := getRPCForIslb()
if found {
islb.AsyncRequest(proto.IslbOnStreamRemove, util.Map("mid", mid))
}
}
}(msg)
}
func getRPCForSFU(mid string) (string, *nprotoo.Requestor, *nprotoo.Error) {
islb, found := getRPCForIslb()
if !found {
return "", nil, util.NewNpError(500, "Not found any node for islb.")
}
result, err := islb.SyncRequest(proto.IslbFindService, util.Map("service", "sfu", "mid", mid))
if err != nil {
return "", nil, err
}
log.Infof("SFU result => %v", result)
rpcID := result["rpc-id"].(string)
eventID := result["event-id"].(string)
nodeID := result["id"].(string)
rpc, found := rpcs[rpcID]
if !found {
rpc = protoo.NewRequestor(rpcID)
protoo.OnBroadcast(eventID, handleSFUBroadCast)
rpcs[rpcID] = rpc
}
return nodeID, rpc, nil
}
func login(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.login peer.ID()=%s msg=%v", peer.ID(), msg)
//TODO auth check, maybe jwt
return emptyMap, nil
}
// join room
func join(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.join peer.ID()=%s msg=%v", peer.ID(), msg)
if ok, err := verifyData(msg, "rid"); !ok {
return nil, err
}
rid := util.Val(msg, "rid")
//already joined this room
if signal.HasPeer(rid, peer) {
return emptyMap, nil
}
signal.AddPeer(rid, peer)
islb, found := getRPCForIslb()
if !found {
return nil, util.NewNpError(500, "Not found any node for islb.")
}
// Send join => islb
info := util.Val(msg, "info")
uid := peer.ID()
islb.SyncRequest(proto.IslbClientOnJoin, util.Map("rid", rid, "uid", uid, "info", info))
// Send getPubs => islb
islb.AsyncRequest(proto.IslbGetPubs, util.Map("rid", rid, "uid", uid)).Then(
func(result map[string]interface{}) {
log.Infof("IslbGetPubs: result=%v", result)
if result["pubs"] == nil {
return
}
pubs := result["pubs"].([]interface{})
for _, pub := range pubs {
info := pub.(map[string]interface{})["info"].(string)
mid := pub.(map[string]interface{})["mid"].(string)
uid := pub.(map[string]interface{})["uid"].(string)
rid := result["rid"].(string)
tracks := pub.(map[string]interface{})["tracks"].(map[string]interface{})
var infoObj map[string]interface{}
err := json.Unmarshal([]byte(info), &infoObj)
if err != nil {
log.Errorf("json.Unmarshal: err=%v", err)
}
log.Infof("IslbGetPubs: mid=%v info=%v", mid, infoObj)
// peer <= range pubs(mid)
if mid != "" {
peer.Notify(proto.ClientOnStreamAdd, util.Map("rid", rid, "uid", uid, "mid", mid, "info", infoObj, "tracks", tracks))
}
}
},
func(err *nprotoo.Error) {
})
return emptyMap, nil
}
func leave(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.leave peer.ID()=%s msg=%v", peer.ID(), msg)
defer util.Recover("biz.leave")
if ok, err := verifyData(msg, "rid"); !ok {
return nil, err
}
rid := util.Val(msg, "rid")
uid := peer.ID()
islb, found := getRPCForIslb()
if !found {
return nil, util.NewNpError(500, "Not found any node for islb.")
}
islb.AsyncRequest(proto.IslbOnStreamRemove, util.Map("rid", rid, "uid", uid, "mid", ""))
islb.SyncRequest(proto.IslbClientOnLeave, util.Map("rid", rid, "uid", uid))
signal.DelPeer(rid, peer.ID())
return emptyMap, nil
}
func clientClose(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.close peer.ID()=%s msg=%v", peer.ID(), msg)
return leave(peer, msg)
}
func publish(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.publish peer.ID()=%s", peer.ID())
nid, sfu, err := getRPCForSFU("")
if err != nil {
log.Warnf("Not found any sfu node, reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
}
jsep := msg["jsep"].(map[string]interface{})
options := msg["options"].(map[string]interface{})
room := signal.GetRoomByPeer(peer.ID())
if room == nil {
return nil, util.NewNpError(codeRoomErr, codeStr(codeRoomErr))
}
uid := peer.ID()
result, err := sfu.SyncRequest(proto.ClientPublish, util.Map("uid", uid, "jsep", jsep, "options", options))
if err != nil {
log.Warnf("reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
}
log.Infof("publish: result => %v", result)
mid := util.Val(result, "mid")
rid := room.ID()
tracks := result["tracks"]
islb, found := getRPCForIslb()
if !found {
return nil, util.NewNpError(500, "Not found any node for islb.")
}
islb.AsyncRequest(proto.IslbOnStreamAdd, util.Map("rid", rid, "nid", nid, "uid", uid, "mid", mid, "tracks", tracks))
return result, nil
}
// unpublish from app
func unpublish(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("signal.unpublish peer.ID()=%s msg=%v", peer.ID(), msg)
mid := util.Val(msg, "mid")
rid := util.Val(msg, "rid")
uid := peer.ID()
_, sfu, err := getRPCForSFU(mid)
if err != nil {
log.Warnf("Not found any sfu node, reject: %d => %s", err.Code, err.Reason)
return nil, err
}
_, err = sfu.SyncRequest(proto.ClientUnPublish, util.Map("mid", mid))
if err != nil {
return nil, err
}
islb, found := getRPCForIslb()
if !found {
return nil, util.NewNpError(500, "Not found any node for islb.")
}
// if this mid is a webrtc pub
// tell islb stream-remove, `rtc.DelPub(mid)` will be done when islb broadcast stream-remove
islb.AsyncRequest(proto.IslbOnStreamRemove, util.Map("rid", rid, "uid", uid, "mid", mid))
return emptyMap, nil
}
func subscribe(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.subscribe peer.ID()=%s ", peer.ID())
if ok, err := verifyData(msg, "jsep", "mid"); !ok {
return nil, err
}
mid := util.Val(msg, "mid")
nodeID, sfu, err := getRPCForSFU(mid)
if err != nil {
log.Warnf("Not found any sfu node, reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
}
// TODO:
if nodeID != "node for mid" {
log.Warnf("Not the same node, need to enable sfu-sfu relay!")
}
room := signal.GetRoomByPeer(peer.ID())
uid := peer.ID()
rid := room.ID()
jsep := msg["jsep"].(map[string]interface{})
islb, found := getRPCForIslb()
if !found {
return nil, util.NewNpError(500, "Not found any node for islb.")
}
result, err := islb.SyncRequest(proto.IslbGetMediaInfo, util.Map("rid", rid, "mid", mid))
if err != nil {
log.Warnf("reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
}
result, err = sfu.SyncRequest(proto.ClientSubscribe, util.Map("uid", uid, "mid", mid, "tracks", result["tracks"], "jsep", jsep))
if err != nil {
log.Warnf("reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
}
log.Infof("subscribe: result => %v", result)
return result, nil
}
func unsubscribe(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.unsubscribe peer.ID()=%s msg=%v", peer.ID(), msg)
if ok, err := verifyData(msg, "mid"); !ok {
return nil, err
}
mid := util.Val(msg, "mid")
_, sfu, err := getRPCForSFU(mid)
if err != nil {
log.Warnf("Not found any sfu node, reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
}
result, err := sfu.SyncRequest(proto.ClientUnSubscribe, util.Map("mid", mid))
if err != nil {
log.Warnf("reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
}
log.Infof("publish: result => %v", result)
return result, nil
}
func broadcast(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.unsubscribe peer.ID()=%s msg=%v", peer.ID(), msg)
if ok, err := verifyData(msg, "rid", "uid", "info"); !ok {
return nil, err
}
islb, found := getRPCForIslb()
if !found {
return nil, util.NewNpError(500, "Not found any node for islb.")
}
rid, uid, info := util.Val(msg, "rid"), util.Val(msg, "uid"), util.Val(msg, "info")
islb.AsyncRequest(proto.IslbOnBroadcast, util.Map("rid", rid, "uid", uid, "info", info))
return emptyMap, nil
}
func trickle(peer *signal.Peer, msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
log.Infof("biz.trickle peer.ID()=%s msg=%v", peer.ID(), msg)
mid := util.Val(msg, "mid")
if ok, err := verifyData(msg, "rid", "uid", "info"); !ok {
return nil, err
}
_, sfu, err := getRPCForSFU(mid)
if err != nil {
log.Warnf("Not found any sfu node, reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
}
trickle := msg["trickle"].(map[string]interface{})
sfu.AsyncRequest(proto.ClientTrickleICE, util.Map("mid", mid, "trickle", trickle))
return emptyMap, nil
}