Skip to content

Commit aa799f9

Browse files
committed
Add logs for sfu-ws
Add some logs for debugging. FIXME: sfu-ws endless send offer when use plan-b.
1 parent 794232b commit aa799f9

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

sfu-ws/README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# sfu-ws
2+
23
sfu-ws is a many-to-many websocket based SFU. This is a more advanced version of [broadcast](https://github.com/pion/webrtc/tree/master/examples/broadcast)
34
and demonstrates the following features.
45

@@ -10,6 +11,7 @@ and demonstrates the following features.
1011
* Support for multiple browsers
1112

1213
We also provide a flutter client that supports the following platforms
14+
1315
* Android, iOS
1416
* Web
1517
* MacOS (Windows, Linux and Fuschia in the [future](https://github.com/flutter-webrtc/flutter-webrtc#functionality))
@@ -18,20 +20,33 @@ For a production application you should also explore [simulcast](https://github.
1820
metrics and robust error handling.
1921

2022
## Instructions
23+
2124
### Download sfu-ws
25+
2226
This example requires you to clone the repo since it is serving static HTML.
2327

24-
```
28+
```sh
2529
mkdir -p $GOPATH/src/github.com/pion
2630
cd $GOPATH/src/github.com/pion
2731
git clone https://github.com/pion/example-webrtc-applications.git
2832
cd webrtc/examples/sfu-ws
2933
```
3034

3135
### Run sfu-ws
32-
Execute `go run *.go`
36+
37+
```sh
38+
# Run sfu-ws
39+
go run *.go
40+
41+
# Run sfu-ws with logs
42+
PION_LOG_INFO=sfu-ws go run *.go
43+
44+
# Run sfu-ws with all logs
45+
PION_LOG_TRACE=all go run *.go
46+
```
3347

3448
### Open the Web UI
49+
3550
Open [http://localhost:8080](http://localhost:8080). This will automatically connect and send your video. Now join from other tabs and browsers!
3651

3752
Congrats, you have used Pion WebRTC! Now start building something cool

sfu-ws/main.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"encoding/json"
55
"flag"
66
"io/ioutil"
7-
"log"
87
"net/http"
98
"sync"
109
"text/template"
1110
"time"
1211

1312
"github.com/gorilla/websocket"
13+
"github.com/pion/logging"
1414
"github.com/pion/rtcp"
1515
"github.com/pion/webrtc/v3"
1616
)
@@ -26,6 +26,8 @@ var (
2626
listLock sync.RWMutex
2727
peerConnections []peerConnectionState
2828
trackLocals map[string]*webrtc.TrackLocalStaticRTP
29+
30+
log = logging.NewDefaultLoggerFactory().NewLogger("sfu-ws")
2931
)
3032

3133
type websocketMessage struct {
@@ -43,7 +45,6 @@ func main() {
4345
flag.Parse()
4446

4547
// Init other state
46-
log.SetFlags(0)
4748
trackLocals = map[string]*webrtc.TrackLocalStaticRTP{}
4849

4950
// Read index.html from disk into memory, serve whenever anyone requests /
@@ -58,8 +59,8 @@ func main() {
5859

5960
// index.html handler
6061
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)
6364
}
6465
})
6566

@@ -71,14 +72,18 @@ func main() {
7172
}()
7273

7374
// 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+
}
7578
}
7679

7780
// Add to list of tracks and fire renegotation for all PeerConnections
7881
func addTrack(t *webrtc.TrackRemote) *webrtc.TrackLocalStaticRTP {
7982
listLock.Lock()
8083
defer func() {
8184
listLock.Unlock()
85+
86+
// FIXED: If server and client use plan-b, this code will cause endless send offer to the client
8287
signalPeerConnections()
8388
}()
8489

@@ -165,9 +170,12 @@ func signalPeerConnections() {
165170

166171
offerString, err := json.Marshal(offer)
167172
if err != nil {
173+
log.Errorf("Failed to marshal offer to json: %v", err)
168174
return true
169175
}
170176

177+
log.Infof("Send offer to client: %v\n", offer)
178+
171179
if err = peerConnections[i].websocket.WriteJSON(&websocketMessage{
172180
Event: "offer",
173181
Data: string(offerString),
@@ -220,7 +228,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
220228
// Upgrade HTTP request to Websocket
221229
unsafeConn, err := upgrader.Upgrade(w, r, nil)
222230
if err != nil {
223-
log.Print("upgrade:", err)
231+
log.Errorf("Failed to upgrade HTTP to Websocket: ", err)
224232
return
225233
}
226234

@@ -232,7 +240,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
232240
// Create new PeerConnection
233241
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{})
234242
if err != nil {
235-
log.Print(err)
243+
log.Errorf("Failed to creates a PeerConnection: %v", err)
236244
return
237245
}
238246

@@ -244,7 +252,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
244252
if _, err := peerConnection.AddTransceiverFromKind(typ, webrtc.RTPTransceiverInit{
245253
Direction: webrtc.RTPTransceiverDirectionRecvonly,
246254
}); err != nil {
247-
log.Print(err)
255+
log.Errorf("Failed to add transceiver: %v", err)
248256
return
249257
}
250258
}
@@ -262,31 +270,37 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
262270

263271
candidateString, err := json.Marshal(i.ToJSON())
264272
if err != nil {
265-
log.Println(err)
273+
log.Errorf("Failed to marshal candidate to json: %v", err)
266274
return
267275
}
268276

277+
log.Infof("Send candidate to client: %s\n", candidateString)
278+
269279
if writeErr := c.WriteJSON(&websocketMessage{
270280
Event: "candidate",
271281
Data: string(candidateString),
272282
}); writeErr != nil {
273-
log.Println(writeErr)
283+
log.Errorf("Failed to write JSON: %v", writeErr)
274284
}
275285
})
276286

277287
// If PeerConnection is closed remove it from global list
278288
peerConnection.OnConnectionStateChange(func(p webrtc.PeerConnectionState) {
289+
log.Infof("Connection state change: %s\n", p)
290+
279291
switch p {
280292
case webrtc.PeerConnectionStateFailed:
281293
if err := peerConnection.Close(); err != nil {
282-
log.Print(err)
294+
log.Errorf("Failed to close PeerConnection: %v", err)
283295
}
284296
case webrtc.PeerConnectionStateClosed:
285297
signalPeerConnections()
286298
}
287299
})
288300

289301
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+
290304
// Create a track to fan out our incoming video to all peers
291305
trackLocal := addTrack(t)
292306
defer removeTrack(trackLocal)
@@ -304,43 +318,57 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
304318
}
305319
})
306320

321+
peerConnection.OnICEConnectionStateChange(func(is webrtc.ICEConnectionState) {
322+
log.Infof("ICE connection state changed: %s\n", is)
323+
})
324+
307325
// Signal for the new PeerConnection
308326
signalPeerConnections()
309327

310328
message := &websocketMessage{}
311329
for {
312330
_, raw, err := c.ReadMessage()
313331
if err != nil {
314-
log.Println(err)
332+
log.Errorf("Failed to read message: %v", err)
315333
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)
318340
return
319341
}
320342

321343
switch message.Event {
322344
case "candidate":
323345
candidate := webrtc.ICECandidateInit{}
324346
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)
326348
return
327349
}
328350

351+
log.Infof("Got candidate: %v\n", candidate)
352+
329353
if err := peerConnection.AddICECandidate(candidate); err != nil {
330-
log.Println(err)
354+
log.Errorf("Failed to add ICE candidate: %v", err)
331355
return
332356
}
333357
case "answer":
334358
answer := webrtc.SessionDescription{}
335359
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)
337361
return
338362
}
339363

364+
log.Infof("Got answer: %v\n", answer)
365+
340366
if err := peerConnection.SetRemoteDescription(answer); err != nil {
341-
log.Println(err)
367+
log.Errorf("Failed to set remote description: %v", err)
342368
return
343369
}
370+
default:
371+
log.Errorf("unknown message: %+v", message)
344372
}
345373
}
346374
}

0 commit comments

Comments
 (0)