/
client_jsonrpc.go
234 lines (201 loc) · 5.94 KB
/
client_jsonrpc.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
package servers
import (
"context"
"encoding/json"
"fmt"
"github.com/golang/protobuf/proto"
noir "github.com/net-prophet/noir/pkg/noir"
pb "github.com/net-prophet/noir/pkg/proto"
log "github.com/pion/ion-log"
"github.com/pion/webrtc/v3"
"github.com/sourcegraph/jsonrpc2"
strings "strings"
"time"
)
type clientJSONRPCBridge struct {
pid string
manager *noir.Manager
}
// Trickle message sent when renegotiating the peer connection
type Trickle struct {
Target int `json:"target"`
Candidate webrtc.ICECandidateInit `json:"candidate"`
}
func NewClientJSONRPCBridge(pid string, manager *noir.Manager) *clientJSONRPCBridge {
return &clientJSONRPCBridge{pid: pid, manager: manager}
}
// Handle incoming RPC call events like join, answer, offer and trickle
func (s *clientJSONRPCBridge) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
replyError := func(err error) {
_ = conn.ReplyWithError(ctx, req.ID, &jsonrpc2.Error{
Code: 500,
Message: fmt.Sprintf("%s", err),
})
}
// TODO: why is this wrapped in quotes?
requestId := strings.Replace(req.ID.String(), "\"", "", -1)
router := (*s.manager).GetRouter()
routerQueue := (*router).GetQueue()
toPeerQueue := s.manager.GetQueue(pb.KeyTopicToPeer(s.pid))
log.Debugf("from jsonrpc %s %s", s.pid, req.Method)
switch req.Method {
case "join":
var join noir.Join
err := json.Unmarshal(*req.Params, &join)
if err != nil {
log.Errorf("connect: error parsing offer: %v", err)
replyError(err)
break
}
command := &pb.NoirRequest{
Command: &pb.NoirRequest_Signal{
Signal: &pb.SignalRequest{
// SignalRequest.id should be called pid but we are ion-sfu compatible
Id: s.pid,
RequestId: requestId,
Payload: &pb.SignalRequest_Join{&pb.JoinRequest{
Sid: join.Sid,
Description: []byte(join.Offer.SDP),
},
},
},
}}
noir.EnqueueRequest(*routerQueue, command)
go s.Listen(ctx, conn, req)
case "offer":
var negotiation noir.Negotiation
err := json.Unmarshal(*req.Params, &negotiation)
marshaled, _ := json.Marshal(negotiation)
if err != nil {
log.Errorf("connect: error parsing offer: %v", err)
replyError(err)
break
}
command := &pb.NoirRequest{
Command: &pb.NoirRequest_Signal{
Signal: &pb.SignalRequest{
// SignalRequest.id should be called pid but we are ion-sfu compatible
Id: s.pid,
RequestId: requestId,
Payload: &pb.SignalRequest_Description{
Description: marshaled,
},
},
}}
noir.EnqueueRequest(toPeerQueue, command)
case "answer":
var negotiation noir.Negotiation
err := json.Unmarshal(*req.Params, &negotiation)
marshaled, _ := json.Marshal(negotiation)
if err != nil {
log.Errorf("connect: error parsing offer: %v", err)
replyError(err)
break
}
command := &pb.NoirRequest{
Command: &pb.NoirRequest_Signal{
Signal: &pb.SignalRequest{
// SignalRequest.id should be called pid but we are ion-sfu compatible
Id: s.pid,
RequestId: requestId,
Payload: &pb.SignalRequest_Description{
Description: marshaled,
},
},
}}
noir.EnqueueRequest(toPeerQueue, command)
case "trickle":
var trickle Trickle
err := json.Unmarshal(*req.Params, &trickle)
if err != nil {
log.Errorf("connect: error parsing candidate: %v", err)
replyError(err)
break
}
var target pb.Trickle_Target
if trickle.Target == 1 {
target = pb.Trickle_PUBLISHER
} else {
target = pb.Trickle_SUBSCRIBER
}
marshaled, _ := json.Marshal(trickle.Candidate)
command := &pb.NoirRequest{
Command: &pb.NoirRequest_Signal{
Signal: &pb.SignalRequest{
// SignalRequest.id should be called pid but we are ion-sfu compatible
Id: s.pid,
RequestId: requestId,
Payload: &pb.SignalRequest_Trickle{Trickle: &pb.Trickle{
Target: target,
Init: string(marshaled),
}},
},
}}
noir.EnqueueRequest(toPeerQueue, command)
}
}
func (s *clientJSONRPCBridge) Close() {
s.manager.DisconnectUser(s.pid)
}
func (s *clientJSONRPCBridge) Listen(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
//send := s.manager.GetQueue(pb.KeyTopicToPeer(s.pid), noir.PeerPingFrequency)
recv := s.manager.GetQueue(pb.KeyTopicFromPeer(s.pid))
log.Infof("peer bridge %s", s.pid)
for {
message, err := recv.BlockUntilNext(0)
if err != nil {
log.Errorf("message err: %s", err)
time.Sleep(time.Second)
continue
}
var reply pb.NoirReply
err = proto.Unmarshal(message, &reply)
if err != nil {
log.Errorf("unmarshal err: %s", err)
continue
}
switch reply.Command.(type) {
case *pb.NoirReply_Signal:
signal := reply.GetSignal()
reqID := jsonrpc2.ID{Num: 0, Str: signal.RequestId, IsString: true}
switch signal.Payload.(type) {
case *pb.SignalReply_Kill:
return
case *pb.SignalReply_Join:
var answer webrtc.SessionDescription
json.Unmarshal(signal.GetJoin().Description, &answer)
conn.Reply(ctx, reqID, answer)
//log.Debugf("answer %s", answer)
case *pb.SignalReply_Description:
var desc webrtc.SessionDescription
json.Unmarshal(signal.GetDescription(), &desc)
var method string
if desc.Type == webrtc.SDPTypeAnswer {
method = "answer"
} else {
method = "offer"
}
if signal.RequestId == "" {
conn.Notify(ctx, method, desc)
//log.Debugf("notify %s %s", method, desc)
} else {
conn.Reply(ctx, reqID, desc)
//log.Debugf("notify %s %s", method, desc)
}
case *pb.SignalReply_Trickle:
trickle := signal.GetTrickle()
var candidate webrtc.ICECandidateInit
json.Unmarshal([]byte(trickle.GetInit()), &candidate)
conn.Notify(ctx, "trickle", Trickle{
Target: int(trickle.Target.Number()),
Candidate: candidate,
})
//log.Debugf("trickle %s", trickle)
default:
log.Errorf("unknown servers reply %s", signal)
}
default:
log.Warnf("non-servers reply on client channel %s", &reply)
}
}
}