/
ws.go
143 lines (122 loc) · 3.15 KB
/
ws.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
package ws
import (
"context"
"encoding/json"
"fmt"
"github.com/go-logr/logr"
"github.com/pion/ion-sfu/pkg/sfu"
"github.com/pion/webrtc/v3"
"github.com/sourcegraph/jsonrpc2"
)
var defaultConfig = sfu.JoinConfig{
NoPublish: false,
NoSubscribe: false,
NoAutoSubscribe: false,
}
// Join message sent when initializing a peer connection
type Join struct {
SID string `json:"sid"`
UID string `json:"uid"`
Offer webrtc.SessionDescription `json:"offer"`
Config sfu.JoinConfig `json:"config"`
}
// Negotiation message sent when renegotiating the peer connection
type Negotiation struct {
Desc webrtc.SessionDescription `json:"desc"`
}
// Trickle message sent when renegotiating the peer connection
type Trickle struct {
Target int `json:"target"`
Candidate webrtc.ICECandidateInit `json:"candidate"`
}
type Connection struct {
*sfu.PeerLocal
logr.Logger
}
func NewConnection(p *sfu.PeerLocal, l logr.Logger) *Connection {
return &Connection{p, l}
}
func (c *Connection) 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),
})
}
switch req.Method {
case "join":
var join Join
err := json.Unmarshal(*req.Params, &join)
if err != nil {
c.Logger.Error(err, "connect: error parsing offer")
replyError(err)
break
}
c.Logger.Info(fmt.Sprintf("Join msg %+v", join))
c.OnOffer = func(offer *webrtc.SessionDescription) {
if err := conn.Notify(ctx, "offer", offer); err != nil {
c.Logger.Error(err, "error sending offer")
}
}
c.OnIceCandidate = func(candidate *webrtc.ICECandidateInit, target int) {
if err := conn.Notify(ctx, "trickle", Trickle{
Candidate: *candidate,
Target: target,
}); err != nil {
c.Logger.Error(err, "error sending ice candidate")
}
}
err = c.Join(join.SID, join.UID, join.Config)
if err != nil {
replyError(err)
break
}
if !join.Config.NoPublish {
answer, err := c.Answer(join.Offer)
if err != nil {
replyError(err)
break
}
_ = conn.Reply(ctx, req.ID, answer)
}
case "offer":
var negotiation Negotiation
err := json.Unmarshal(*req.Params, &negotiation)
if err != nil {
c.Logger.Error(err, "connect: error parsing offer")
replyError(err)
break
}
answer, err := c.Answer(negotiation.Desc)
if err != nil {
replyError(err)
break
}
_ = conn.Reply(ctx, req.ID, answer)
case "answer":
var negotiation Negotiation
err := json.Unmarshal(*req.Params, &negotiation)
if err != nil {
c.Logger.Error(err, "connect: error parsing answer")
replyError(err)
break
}
err = c.SetRemoteDescription(negotiation.Desc)
if err != nil {
replyError(err)
}
case "trickle":
var trickle Trickle
err := json.Unmarshal(*req.Params, &trickle)
if err != nil {
c.Logger.Error(err, "connect: error parsing candidate")
replyError(err)
break
}
err = c.Trickle(trickle.Candidate, trickle.Target)
if err != nil {
c.Logger.Error(err, "error setting trickle")
replyError(err)
}
}
}