@@ -4,13 +4,13 @@ import (
4
4
"encoding/json"
5
5
"flag"
6
6
"io/ioutil"
7
- "log"
8
7
"net/http"
9
8
"sync"
10
9
"text/template"
11
10
"time"
12
11
13
12
"github.com/gorilla/websocket"
13
+ "github.com/pion/logging"
14
14
"github.com/pion/rtcp"
15
15
"github.com/pion/webrtc/v3"
16
16
)
26
26
listLock sync.RWMutex
27
27
peerConnections []peerConnectionState
28
28
trackLocals map [string ]* webrtc.TrackLocalStaticRTP
29
+
30
+ log = logging .NewDefaultLoggerFactory ().NewLogger ("sfu-ws" )
29
31
)
30
32
31
33
type websocketMessage struct {
@@ -43,7 +45,6 @@ func main() {
43
45
flag .Parse ()
44
46
45
47
// Init other state
46
- log .SetFlags (0 )
47
48
trackLocals = map [string ]* webrtc.TrackLocalStaticRTP {}
48
49
49
50
// Read index.html from disk into memory, serve whenever anyone requests /
@@ -58,8 +59,8 @@ func main() {
58
59
59
60
// index.html handler
60
61
http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
61
- if err : = indexTemplate .Execute (w , "ws://" + r .Host + "/websocket" ); err != nil {
62
- log .Fatal ( err )
62
+ if err = indexTemplate .Execute (w , "ws://" + r .Host + "/websocket" ); err != nil {
63
+ log .Errorf ( "Failed to parse index template: %v" , err )
63
64
}
64
65
})
65
66
@@ -71,14 +72,18 @@ func main() {
71
72
}()
72
73
73
74
// start HTTP server
74
- log .Fatal (http .ListenAndServe (* addr , nil ))
75
+ if err = http .ListenAndServe (* addr , nil ); err != nil {
76
+ log .Errorf ("Failed to start http server: %v" , err )
77
+ }
75
78
}
76
79
77
80
// Add to list of tracks and fire renegotation for all PeerConnections
78
81
func addTrack (t * webrtc.TrackRemote ) * webrtc.TrackLocalStaticRTP {
79
82
listLock .Lock ()
80
83
defer func () {
81
84
listLock .Unlock ()
85
+
86
+ // FIXED: If server and client use plan-b, this code will cause endless send offer to the client
82
87
signalPeerConnections ()
83
88
}()
84
89
@@ -165,9 +170,12 @@ func signalPeerConnections() {
165
170
166
171
offerString , err := json .Marshal (offer )
167
172
if err != nil {
173
+ log .Errorf ("Failed to marshal offer to json: %v" , err )
168
174
return true
169
175
}
170
176
177
+ log .Infof ("Send offer to client: %v\n " , offer )
178
+
171
179
if err = peerConnections [i ].websocket .WriteJSON (& websocketMessage {
172
180
Event : "offer" ,
173
181
Data : string (offerString ),
@@ -220,7 +228,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
220
228
// Upgrade HTTP request to Websocket
221
229
unsafeConn , err := upgrader .Upgrade (w , r , nil )
222
230
if err != nil {
223
- log .Print ( " upgrade: " , err )
231
+ log .Errorf ( "Failed to upgrade HTTP to Websocket: " , err )
224
232
return
225
233
}
226
234
@@ -232,7 +240,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
232
240
// Create new PeerConnection
233
241
peerConnection , err := webrtc .NewPeerConnection (webrtc.Configuration {})
234
242
if err != nil {
235
- log .Print ( err )
243
+ log .Errorf ( "Failed to creates a PeerConnection: %v" , err )
236
244
return
237
245
}
238
246
@@ -244,7 +252,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
244
252
if _ , err := peerConnection .AddTransceiverFromKind (typ , webrtc.RTPTransceiverInit {
245
253
Direction : webrtc .RTPTransceiverDirectionRecvonly ,
246
254
}); err != nil {
247
- log .Print ( err )
255
+ log .Errorf ( "Failed to add transceiver: %v" , err )
248
256
return
249
257
}
250
258
}
@@ -262,31 +270,37 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
262
270
263
271
candidateString , err := json .Marshal (i .ToJSON ())
264
272
if err != nil {
265
- log .Println ( err )
273
+ log .Errorf ( "Failed to marshal candidate to json: %v" , err )
266
274
return
267
275
}
268
276
277
+ log .Infof ("Send candidate to client: %s\n " , candidateString )
278
+
269
279
if writeErr := c .WriteJSON (& websocketMessage {
270
280
Event : "candidate" ,
271
281
Data : string (candidateString ),
272
282
}); writeErr != nil {
273
- log .Println ( writeErr )
283
+ log .Errorf ( "Failed to write JSON: %v" , writeErr )
274
284
}
275
285
})
276
286
277
287
// If PeerConnection is closed remove it from global list
278
288
peerConnection .OnConnectionStateChange (func (p webrtc.PeerConnectionState ) {
289
+ log .Infof ("Connection state change: %s\n " , p )
290
+
279
291
switch p {
280
292
case webrtc .PeerConnectionStateFailed :
281
293
if err := peerConnection .Close (); err != nil {
282
- log .Print ( err )
294
+ log .Errorf ( "Failed to close PeerConnection: %v" , err )
283
295
}
284
296
case webrtc .PeerConnectionStateClosed :
285
297
signalPeerConnections ()
286
298
}
287
299
})
288
300
289
301
peerConnection .OnTrack (func (t * webrtc.TrackRemote , _ * webrtc.RTPReceiver ) {
302
+ log .Infof ("Got remote track: Kind=%s, ID=%s, PayloadType=%d\n " , t .Kind (), t .ID (), t .PayloadType ())
303
+
290
304
// Create a track to fan out our incoming video to all peers
291
305
trackLocal := addTrack (t )
292
306
defer removeTrack (trackLocal )
@@ -304,43 +318,57 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
304
318
}
305
319
})
306
320
321
+ peerConnection .OnICEConnectionStateChange (func (is webrtc.ICEConnectionState ) {
322
+ log .Infof ("ICE connection state changed: %s\n " , is )
323
+ })
324
+
307
325
// Signal for the new PeerConnection
308
326
signalPeerConnections ()
309
327
310
328
message := & websocketMessage {}
311
329
for {
312
330
_ , raw , err := c .ReadMessage ()
313
331
if err != nil {
314
- log .Println ( err )
332
+ log .Errorf ( "Failed to read message: %v" , err )
315
333
return
316
- } else if err := json .Unmarshal (raw , & message ); err != nil {
317
- log .Println (err )
334
+ }
335
+
336
+ log .Infof ("Got message: %s" , raw )
337
+
338
+ if err := json .Unmarshal (raw , & message ); err != nil {
339
+ log .Errorf ("Failed to unmarshal json to message: %v" , err )
318
340
return
319
341
}
320
342
321
343
switch message .Event {
322
344
case "candidate" :
323
345
candidate := webrtc.ICECandidateInit {}
324
346
if err := json .Unmarshal ([]byte (message .Data ), & candidate ); err != nil {
325
- log .Println ( err )
347
+ log .Errorf ( "Failed to unmarshal json to candidate: %v" , err )
326
348
return
327
349
}
328
350
351
+ log .Infof ("Got candidate: %v\n " , candidate )
352
+
329
353
if err := peerConnection .AddICECandidate (candidate ); err != nil {
330
- log .Println ( err )
354
+ log .Errorf ( "Failed to add ICE candidate: %v" , err )
331
355
return
332
356
}
333
357
case "answer" :
334
358
answer := webrtc.SessionDescription {}
335
359
if err := json .Unmarshal ([]byte (message .Data ), & answer ); err != nil {
336
- log .Println ( err )
360
+ log .Errorf ( "Failed to unmarshal json to answer: %v" , err )
337
361
return
338
362
}
339
363
364
+ log .Infof ("Got answer: %v\n " , answer )
365
+
340
366
if err := peerConnection .SetRemoteDescription (answer ); err != nil {
341
- log .Println ( err )
367
+ log .Errorf ( "Failed to set remote description: %v" , err )
342
368
return
343
369
}
370
+ default :
371
+ log .Errorf ("unknown message: %+v" , message )
344
372
}
345
373
}
346
374
}
0 commit comments