-
Notifications
You must be signed in to change notification settings - Fork 1
/
media.go
419 lines (336 loc) · 9.59 KB
/
media.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package hub
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"github.com/castyapp/cli/helpers"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
cmap "github.com/orcaman/concurrent-map"
"github.com/pion/interceptor"
"github.com/pion/webrtc/v3"
)
type Hub struct {
rooms cmap.ConcurrentMap
webrtcApi *webrtc.API
}
func (h *Hub) FindRoom(roomId string) (*Room, error) {
cmapRoom, ok := h.rooms.Get(roomId)
if !ok {
return nil, fmt.Errorf("Room not exists!")
}
room, exists := cmapRoom.(*Room)
if !exists {
return nil, fmt.Errorf("Room not exists!")
}
return room, nil
}
func NewHub() *Hub {
return &Hub{
rooms: cmap.New(),
}
}
func NewSingPortWebrtcHub(listener *net.UDPConn) (*Hub, error) {
// Enable Extension Headers needed for Simulcast
m := &webrtc.MediaEngine{}
//exts := []string{
//"urn:ietf:params:rtp-hdrext:sdes:mid",
//"urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id",
//"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id",
//}
if err := m.RegisterDefaultCodecs(); err != nil {
return nil, err
}
//for _, extension := range exts {
//if err := m.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: extension}, webrtc.RTPCodecTypeAudio); err != nil {
//return nil, err
//}
//}
// Create a InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
// This provides NACKs, RTCP Reports and other features. If you use `webrtc.NewPeerConnection`
// this is enabled by default. If you are manually managing You MUST create a InterceptorRegistry
// for each PeerConnection.
i := &interceptor.Registry{}
// Use the default set of Interceptors
if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil {
return nil, err
}
// Create a SettingEngine, this allows non-standard WebRTC behavior
settingEngine := webrtc.SettingEngine{}
// Configure our SettingEngine to use our UDPMux. By default a PeerConnection has
// no global state. The API+SettingEngine allows the user to share state between them.
// In this case we are sharing our listening port across many.
settingEngine.SetICEUDPMux(webrtc.NewICEUDPMux(nil, listener))
return &Hub{
rooms: cmap.New(),
webrtcApi: webrtc.NewAPI(
webrtc.WithSettingEngine(settingEngine),
webrtc.WithMediaEngine(m),
webrtc.WithInterceptorRegistry(i),
),
}, nil
}
func (h *Hub) CreateOrFindRoom(join *JoinRoomRequest) *Room {
var room *Room
cmapRoom, ok := h.rooms.Get(join.RoomId)
if ok {
room = cmapRoom.(*Room)
} else {
room = NewRoom(join)
h.rooms.Set(room.id, room)
}
return room
}
func (h *Hub) CreateOrJoinRoom(conn net.Conn, join *JoinRoomRequest) (string, error) {
// Prepare the configuration
config := webrtc.Configuration{
BundlePolicy: webrtc.BundlePolicyMaxCompat,
RTCPMuxPolicy: webrtc.RTCPMuxPolicyRequire,
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
room := h.CreateOrFindRoom(join)
// Create a new RTCPeerConnection
peerConnection, err := h.webrtcApi.NewPeerConnection(config)
if err != nil {
return "", err
}
userPeer := &User{
Id: join.UserId,
peer: peerConnection,
gatewayConn: conn,
}
if err := userPeer.CreateLocalTrack(room); err != nil {
return "", err
}
room.UserJoined(userPeer)
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
log.Println("Got track")
for {
// Read RTP packets being sent to Pion
packet, _, readErr := track.ReadRTP()
if readErr != nil {
log.Println(readErr)
return
}
if writeErr := userPeer.track.WriteRTP(packet); writeErr != nil && !errors.Is(writeErr, io.ErrClosedPipe) {
log.Println(writeErr)
return
}
}
})
//if err := room.signalPeerConnections(userPeer); err != nil {
//log.Fatalln(err)
//}
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
switch connectionState {
case webrtc.ICEConnectionStateConnected:
go room.SendPeerToAllUsers(userPeer)
//if err := room.signalPeerConnections(userPeer); err != nil {
//log.Println("AddUsersTracks Err: ", err)
//}
break
case webrtc.ICEConnectionStateClosed:
//if err := room.signalPeerConnections(userPeer); err != nil {
//log.Fatalln(err)
//}
}
})
peerConnection.OnNegotiationNeeded(func() {
log.Println(fmt.Sprintf("Negotiation Needed [%s]", userPeer.Id))
//newOffer, err := peerConnection.CreateOffer(nil)
//if err != nil {
//log.Println("Err on negotiontion offer : ", err)
//}
//log.Println("Nego Offer: ", newOffer)
//if peerConnection.SignalingState() != webrtc.SignalingStateStable {
//return
//}
//offerbase64, err := helpers.Encode(newOffer)
//if err != nil {
//log.Println("Err on encoding offer : ", err)
//}
//packetData, _ := json.Marshal(&Packet{
//Type: "negotiationneeded",
//Data: offerbase64,
//})
//if err := wsutil.WriteServerText(userPeer.gatewayConn, packetData); err != nil {
//log.Fatal(err)
//}
})
if err := room.AddUsersTracks(userPeer); err != nil {
return "", err
}
offer := webrtc.SessionDescription{}
if err := helpers.DecodeBase64(join.Offer, &offer); err != nil {
return "", err
}
if err := peerConnection.SetRemoteDescription(offer); err != nil {
return "", err
}
// Create an answer
answer, err := peerConnection.CreateAnswer(&webrtc.AnswerOptions{
OfferAnswerOptions: webrtc.OfferAnswerOptions{
VoiceActivityDetection: true,
},
})
if err != nil {
return "", err
}
// Sets the LocalDescription, and starts our UDP listeners
if err := peerConnection.SetLocalDescription(answer); err != nil {
return "", err
}
answerChan := make(chan *webrtc.SessionDescription)
peerConnection.OnICEGatheringStateChange(func(is webrtc.ICEGathererState) {
if is.String() == "complete" {
answerChan <- peerConnection.LocalDescription()
}
})
description := <-answerChan
log.Println("DESK : ", description)
// Output the answer in base64 so we can paste it in browser
answerBase64, err := helpers.EncodeBase64(description)
if err != nil {
return "", err
}
return answerBase64, nil
//return "", nil
}
func (h *Hub) HTTPHandlerFindRoom(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
roomID := r.URL.Query().Get("room_id")
if roomID == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"success": false, "message": "RoomId required"}`))
return
}
h.CreateOrFindRoom(&JoinRoomRequest{RoomId: roomID})
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"success": true}`))
return
}
func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Upgrade connection to websocket
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
log.Println(err)
return
}
log.Println("New Client")
for {
data, _, err := wsutil.ReadClientData(conn)
if err != nil {
log.Println(err)
return
}
if data != nil {
packet := new(Packet)
if err := json.Unmarshal(data, packet); err != nil {
return
}
switch packet.Type {
case "ping":
packetData, _ := json.Marshal(&Packet{Type: "pong"})
if err := wsutil.WriteServerText(conn, packetData); err != nil {
log.Fatal(err)
}
break
case "relaySessionDescription":
room, err := h.FindRoom(packet.RoomId)
if err != nil {
log.Println("Room not exists!")
return
}
user, err := room.FindUser(packet.UserId)
if err != nil {
log.Println("User not exists!")
return
}
var remoteDesc webrtc.SessionDescription
helpers.DecodeBase64(packet.Data.(string), &remoteDesc)
if err := user.peer.SetRemoteDescription(remoteDesc); err != nil {
log.Println(err)
}
answer, err := helpers.EncodeBase64(user.peer.LocalDescription())
if err != nil {
log.Println(err)
}
packetData, _ := json.Marshal(&Packet{
Type: "answer",
Data: answer,
})
if err := wsutil.WriteServerText(conn, packetData); err != nil {
log.Fatal(err)
}
break
case "negotiationneeded":
room, err := h.FindRoom(packet.RoomId)
if err != nil {
log.Println("Room not exists!")
return
}
user, err := room.FindUser(packet.UserId)
if err != nil {
log.Println("User not exists!")
return
}
sd := webrtc.SessionDescription{}
if err := helpers.DecodeBase64(packet.Data.(string), &sd); err != nil {
log.Println(err)
}
if err := user.peer.SetRemoteDescription(sd); err != nil {
log.Println(err)
}
break
case "join":
response := &JoinRoomRequest{
Offer: packet.Data.(string),
RoomId: packet.RoomId,
UserId: packet.UserId,
}
sdpAnswer, err := h.CreateOrJoinRoom(conn, response)
if err != nil {
log.Fatal(err)
}
packetData, _ := json.Marshal(&Packet{
Type: "answer",
Data: sdpAnswer,
})
if err := wsutil.WriteServerText(conn, packetData); err != nil {
log.Fatal(err)
}
break
case "relayICECandidate":
room, err := h.FindRoom(packet.RoomId)
if err != nil {
log.Println("Room not exists!")
return
}
user, err := room.FindUser(packet.UserId)
if err != nil {
log.Println("User not exists!")
return
}
candidate := packet.Data.(map[string]interface{})
sdpMLineIndex := uint16(candidate["sdpMLineIndex"].(float64))
iceCandidate := webrtc.ICECandidateInit{
Candidate: candidate["candidate"].(string),
SDPMLineIndex: &sdpMLineIndex,
}
if err := user.peer.AddICECandidate(iceCandidate); err != nil {
log.Println("Err on adding iceCandidate: ", err)
}
break
}
}
}
}