-
Notifications
You must be signed in to change notification settings - Fork 116
/
whipclient.go
203 lines (169 loc) · 3.95 KB
/
whipclient.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
package rtpconn
import (
"context"
"errors"
"sync"
"github.com/jech/galene/conn"
"github.com/jech/galene/group"
"github.com/pion/webrtc/v3"
)
type WhipClient struct {
group *group.Group
id string
token string
username string
mu sync.Mutex
permissions []string
connection *rtpUpConnection
}
func NewWhipClient(g *group.Group, id string, token string) *WhipClient {
return &WhipClient{group: g, id: id, token: token}
}
func (c *WhipClient) Group() *group.Group {
return c.group
}
func (c *WhipClient) Id() string {
return c.id
}
func (c *WhipClient) Token() string {
return c.token
}
func (c *WhipClient) Username() string {
return c.username
}
func (c *WhipClient) SetUsername(username string) {
c.username = username
}
func (c *WhipClient) Permissions() []string {
c.mu.Lock()
defer c.mu.Unlock()
return c.permissions
}
func (c *WhipClient) SetPermissions(perms []string) {
c.mu.Lock()
defer c.mu.Unlock()
c.permissions = perms
}
func (c *WhipClient) Data() map[string]interface{} {
return nil
}
func (c *WhipClient) PushConn(g *group.Group, id string, conn conn.Up, tracks []conn.UpTrack, replace string) error {
return nil
}
func (c *WhipClient) RequestConns(target group.Client, g *group.Group, id string) error {
if g != c.group {
return nil
}
c.mu.Lock()
up := c.connection
c.mu.Unlock()
if up == nil {
return nil
}
tracks := up.getTracks()
ts := make([]conn.UpTrack, len(tracks))
for i, t := range tracks {
ts[i] = t
}
target.PushConn(g, up.Id(), up, ts, "")
return nil
}
func (c *WhipClient) Joined(group, kind string) error {
return nil
}
func (c *WhipClient) PushClient(group, kind, id, username string, permissions []string, status map[string]interface{}) error {
return nil
}
func (c *WhipClient) Kick(id string, user *string, message string) error {
return c.Close()
}
func (c *WhipClient) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
g := c.group
if g == nil {
return nil
}
if c.connection != nil {
id := c.connection.Id()
c.connection.pc.OnICEConnectionStateChange(nil)
c.connection.pc.Close()
c.connection = nil
for _, c := range g.GetClients(c) {
c.PushConn(g, id, nil, nil, "")
}
c.connection = nil
}
group.DelClient(c)
c.group = nil
return nil
}
func (c *WhipClient) NewConnection(ctx context.Context, offer []byte) ([]byte, error) {
conn, err := newUpConn(c, c.id, "", string(offer))
if err != nil {
return nil, err
}
conn.pc.OnICEConnectionStateChange(
func(state webrtc.ICEConnectionState) {
switch state {
case webrtc.ICEConnectionStateFailed,
webrtc.ICEConnectionStateClosed:
c.Close()
}
})
c.mu.Lock()
defer c.mu.Unlock()
if c.connection != nil {
conn.pc.OnICEConnectionStateChange(nil)
conn.pc.Close()
return nil, errors.New("duplicate connection")
}
c.connection = conn
answer, err := c.gotOffer(ctx, offer)
if err != nil {
conn.pc.OnICEConnectionStateChange(nil)
conn.pc.Close()
return nil, err
}
return answer, nil
}
func (c *WhipClient) GotOffer(ctx context.Context, offer []byte) ([]byte, error) {
c.mu.Lock()
defer c.mu.Unlock()
return c.gotOffer(ctx, offer)
}
// called locked
func (c *WhipClient) gotOffer(ctx context.Context, offer []byte) ([]byte, error) {
conn := c.connection
err := conn.pc.SetRemoteDescription(webrtc.SessionDescription{
Type: webrtc.SDPTypeOffer,
SDP: string(offer),
})
if err != nil {
return nil, err
}
answer, err := conn.pc.CreateAnswer(nil)
if err != nil {
return nil, err
}
gatherComplete := webrtc.GatheringCompletePromise(conn.pc)
err = conn.pc.SetLocalDescription(answer)
if err != nil {
return nil, err
}
conn.flushICECandidates()
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-gatherComplete:
}
return []byte(conn.pc.CurrentLocalDescription().SDP), nil
}
func (c *WhipClient) GotICECandidate(init webrtc.ICECandidateInit) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.connection == nil {
return nil
}
return c.connection.addICECandidate(&init)
}