-
Notifications
You must be signed in to change notification settings - Fork 2
/
webrtc-client.go
525 lines (446 loc) · 14.2 KB
/
webrtc-client.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// go run sender.go --url="wss://$(minikube ip):8447/one2one"
package main
import (
"crypto/tls"
"net"
"flag"
"fmt"
"log"
"errors"
"os"
"path"
"sync"
"context"
"strings"
// "os/signal"
// "net/http"
"encoding/json"
"github.com/gorilla/websocket"
"github.com/pion/webrtc/v3"
"github.com/pion/ice/v2"
"webrtc-client-go/wmsg"
"webrtc-client-go/wcodec"
)
var Usage = func() {
fmt.Fprintf(os.Stderr, "%s <caller|callee> [args]\n", path.Base(os.Args[0]))
flag.PrintDefaults()
os.Exit(1)
}
const (
FREE int = iota
SETUP
BUSY
)
const DefaultUrl = "ws://localhost:8443/"
func describe(i interface{}) {
fmt.Printf("(%v, %T)\n", i, i)
}
// must be called when holding the candidateLock
func processCachedCandidates(pc *webrtc.PeerConnection, cache []webrtc.ICECandidateInit) () {
for len(cache) > 0 {
var c webrtc.ICECandidateInit
c, cache = cache[0], cache[1:]
log.Println("Adding cached remote ICE candidate:", c,
"SdpMid:", *c.SDPMid, "SdpMLineIndex:", *c.SDPMLineIndex)
if candidateErr := pc.AddICECandidate(c); candidateErr != nil {
log.Fatal(candidateErr)
}
}
}
func getIfaceForAddr(addr string) (string, error) {
if(addr == "") {
return "", errors.New("no IP given")
}
ifaces, err := net.Interfaces()
if err != nil {
log.Println("could not obtain local interface list:", err)
return "", errors.New("net.Interfaces")
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Printf("could not obtain IP address for interface %s: %v",
i.Name, err)
return "", errors.New("net.Addrs")
}
for _, a := range addrs {
ipnet, ok := a.(*net.IPNet)
if !ok { continue }
v4 := ipnet.IP.To4()
if v4 == nil { continue }
if(v4.String() == addr){
// log.Println("considering ifs:", a.String(), ", ", addr)
return i.Name, nil
}
}
}
return "", errors.New("addr not found")
}
func dumpCandidates(tr *webrtc.ICETransport) () {
pair, err := tr.GetSelectedCandidatePair()
if err != nil { return }
log.Printf("LOCAL candidate: %s", pair.Local.String())
log.Printf("REMOTE candidate: %s", pair.Remote.String())
}
/////////////////////////
func main() {
state := FREE
log.SetFlags(log.LstdFlags | log.Lshortfile)
// done := make(chan os.Signal, 1)
// signal.Notify(done, os.Interrupt)
// we need to consume the first positional arg
role := ""
if len(os.Args) > 1 && (os.Args[1] == "caller" || os.Args[1] == "callee") {
role = os.Args[1]
os.Args = os.Args[1:]
} else {
Usage()
}
// cmd line
Url := flag.String("url", DefaultUrl, "WebRtc server URL")
TLSDebug := flag.Bool("debug", false, "Debug the TLS connection using a keylogger: dumps data into /tmp/keylog")
// audioSrc := flag.String("audio", "audiotestsrc", "GStreamer audio src")
file := flag.String("file", "", "caller: media file to send / callee: media file to write (extension is either h264 or vp8/ivf, this selects receiver side codec)")
user := flag.String("user", "test1", "User name (will be registered with the WebRTC server)")
peer := flag.String("peer", "test2", "Peer name (will be registered with the WebRTC server)")
iceAddr := flag.String("ice-addr", "", "Use only the given IP address to generate local ICE candidates")
stunURI := flag.String("turn", "", "STUN/TURN server URI")
flag.Parse();
// Assert that we have an audio or video file
_, err := os.Stat(*file)
if role == "caller" && os.IsNotExist(err) {
log.Fatalf("Could not open file `%s`: %s\n", file, err)
}
// Select the receiver side codec
codec := webrtc.MimeTypeH264
switch strings.ToLower(path.Ext(*file)){
case ".h264", ".mkv":
codec = webrtc.MimeTypeH264
case ".vp8", ".ivf":
codec = webrtc.MimeTypeVP8
default:
log.Fatalf("Unknown codec %s: file extension must be either mkv/h264 or vp8/ivf",
strings.ToLower(path.Ext(*file)))
}
log.Printf("Starting %s: user=%s, peer=%s: video: %s\n", role, *user, *peer, *file)
//server uses self-signed certificate: switch to insecure TLS mode
dialer := *websocket.DefaultDialer
dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
if *TLSDebug {
kl, err := os.OpenFile("/tmp/keylog", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatal("keylog:", err)
}
defer kl.Close()
dialer.TLSClientConfig.KeyLogWriter = kl
fmt.Fprintf(kl, "# SSL/TLS secrets log file, generated by go\n")
}
// connect to the webrtc-server
log.Printf("connecting to %s", *Url)
c, _, err := dialer.Dial(*Url, nil)
if err != nil {
log.Fatalln("dial:", err)
}
defer c.Close()
// setup a peerconnection so that we can generate an SDP
s := webrtc.SettingEngine{}
// we are not interested in TPC or anything IPv6 for simplicity
s.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
// disable Multicast DNS
s.SetICEMulticastDNSMode(ice.MulticastDNSModeDisabled)
// filter ice candidates: if iceAddr is available from the command line, we generate ICE
// candidates only on the interface that has this IP: this removes lots of useless ICE
// trials
if *iceAddr != "" {
iceIface, err := getIfaceForAddr(*iceAddr)
if(err == nil){
log.Println("using ICE interface:", iceIface)
s.SetInterfaceFilter(func(i string) bool { return i == iceIface } )
} else {
log.Println("failed to use ICE interface:", err)
}
}
// s.SetAnsweringDTLSRole(webrtc.DTLSRoleClient)
s.SetAnsweringDTLSRole(webrtc.DTLSRoleServer)
// set up media codecs to enforce transcoding (use m.RegisterDefaultCodecs() if transcoding
// is not needed
m := &webrtc.MediaEngine{}
var regCodecs []webrtc.RTPCodecParameters
switch codec {
case webrtc.MimeTypeVP8:
regCodecs = wcodec.VP8Codecs
case webrtc.MimeTypeH264:
regCodecs = wcodec.H264Codecs
}
for _, c := range regCodecs {
if err := m.RegisterCodec(c, webrtc.RTPCodecTypeVideo); err != nil {
log.Fatalln("Could not register codec:", c)
}
}
if(*stunURI != ""){
log.Println("using STUN/TURN/ICE server:", *stunURI)
}
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{*stunURI},
Username: "user",
Credential: "pass",
CredentialType: webrtc.ICECredentialTypePassword,
},
},
ICETransportPolicy: webrtc.ICETransportPolicyRelay,
}
// setup the peer-connection at last
peerConnection, err :=
webrtc.NewAPI(webrtc.WithSettingEngine(s),webrtc.WithMediaEngine(m)).NewPeerConnection(config)
if err != nil {
log.Fatalln("NewPeerConnection:", err)
}
defer peerConnection.Close()
peerConnection.OnSignalingStateChange(func(ss webrtc.SignalingState) {
log.Println("Signaling state change:", ss)
})
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
iceConnectedCtx, iceConnectedCtxCancel := context.WithCancel(context.Background())
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
log.Println("Connection state change:", connectionState.String())
switch connectionState {
case webrtc.ICEConnectionStateConnected:
// dump active transport
senders := peerConnection.GetSenders()
for _, t := range senders {
dumpCandidates(t.Transport().ICETransport())
}
receivers := peerConnection.GetReceivers()
for _, t := range receivers {
dumpCandidates(t.Transport().ICETransport())
}
iceConnectedCtxCancel()
case webrtc.ICEConnectionStateDisconnected, webrtc.ICEConnectionStateFailed:
log.Fatalln("Disconnected/failed, exiting")
}
})
// reader: get messages from the webrtc-server
candidateLock := sync.RWMutex{}
candidateCache := []webrtc.ICECandidateInit{}
recv := make(chan map[string]interface{})
go func() {
defer close(recv)
for {
_, message, err := c.ReadMessage()
if err != nil {
// if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
// log.Fatalf("trying to read from closed websocket: %s\n", err)
// }
log.Println("readMessage:", err)
// if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
// return
// }
return
}
log.Printf("recv: %s\n", message)
// dunno the types yet, try to unmarschal
m := map[string]interface{}{}
err = json.Unmarshal(message, &m)
if err != nil {
log.Println("JSON unmarschal:", err)
continue
}
log.Printf("recv: %v", m)
// handle REMOTE ICECandidates: cache or add
if m["id"].(string) == "iceCandidate" {
c := m["candidate"].(map[string]interface{})
sdpmid := c["sdpMid"].(string)
sdpmlineindex := uint16(c["sdpMLineIndex"].(float64))
candidate := webrtc.ICECandidateInit{
Candidate: c["candidate"].(string),
SDPMid: &sdpmid,
SDPMLineIndex: &sdpmlineindex,
}
if peerConnection.RemoteDescription() == nil {
// no remote SDP yet, cache candidate
log.Println("Caching remote ICE candidate:", candidate,
"SdpMid:", *candidate.SDPMid,
"SdpMLineIndex:", *candidate.SDPMLineIndex)
candidateLock.Lock()
candidateCache = append(candidateCache, candidate)
candidateLock.Unlock()
} else {
log.Println("Adding remote ICE candidate:", candidate,
"SdpMid:", *candidate.SDPMid,
"SdpMLineIndex:", *candidate.SDPMLineIndex)
if candidateErr := peerConnection.AddICECandidate(candidate); candidateErr != nil {
log.Fatal(candidateErr)
}
}
continue;
}
recv <- m
}
}()
// sender: write messages to the webrtc-server
send := make(chan wmsg.Message)
go func() {
defer close(send)
for s := range send {
log.Printf("send: %s\n", s)
err := c.WriteJSON(s)
if err != nil {
log.Println("WriteJSON:", err)
return
}
}
}()
// register
log.Println("registering user:", *user)
r := wmsg.NewRegisterRequest(*user)
send <- r
reply, err := wmsg.NewRegisterResponse(<- recv)
if err != nil {
log.Fatal(err)
}
if reply.Response != "accepted" {
// log.Fatalf("could not register caller %s: %s\n", user, reply.Response)
log.Printf("WARN: could not register caller/callee %s: %s\n", user, reply.Response)
}
// handle LOCAL ICE candidates
// channel that is blocked until LOCAL!! ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
peerConnection.OnICECandidate(func(i *webrtc.ICECandidate) {
if i != nil {
send <- wmsg.NewOnICECandidate(i)
}
})
switch role {
case "caller":
log.Printf("starting call: %s -> %s\n", *user, *peer)
// audio&video
videoTrack, videoTrackErr := webrtc.NewTrackLocalStaticSample(
webrtc.RTPCodecCapability{MimeType: codec}, "video", "pion")
if videoTrackErr != nil {
log.Fatalln(videoTrackErr)
}
rtpSender, videoTrackErr := peerConnection.AddTrack(videoTrack)
if videoTrackErr != nil {
log.Fatalln(videoTrackErr)
}
wcodec.SendFile(iceConnectedCtx, rtpSender, *file, codec, videoTrack)
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
log.Fatalln("cannot create offer:", err)
}
// describe(offer)
if err = peerConnection.SetLocalDescription(offer); err != nil {
log.Fatalln("cannot set local SDP:", err)
}
state = SETUP
send <- wmsg.NewCallRequest(*user, *peer, offer.SDP)
// wait for a call response
var call_res wmsg.CallResponse
for {
call_res, err = wmsg.NewCallResponse(<- recv)
if err != nil {
log.Println("NewCallResponse:", err)
continue
}
break
}
log.Println("call response:", call_res.Response)
if call_res.Response != "accepted" {
log.Fatalln("call rejected with message:", call_res.Response)
}
state = BUSY
log.Println("new state:", state)
// remove conflicting fingerprints from SDP
desc, errSdp := wmsg.ParseSdp(webrtc.SDPTypeAnswer, call_res.Sdp)
if errSdp != nil {
log.Fatalln("could not parse SDP answer:", errSdp)
}
log.Printf("Setting remote session description: %v\n", *desc)
err = peerConnection.SetRemoteDescription(*desc)
if err != nil {
log.Fatalln("cannot set remote SDP:", err)
}
<-gatherComplete
// process remaining cached REMOTE ICE candidates
candidateLock.Lock()
processCachedCandidates(peerConnection, candidateCache);
candidateLock.Unlock()
log.Println("connection setup ready")
// Start pushing buffers on these tracks
// Block forever
select {}
case "callee":
// Allow us to receive 1 video track
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {
log.Fatalln(err)
}
// audio
// if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio); err != nil {
// panic(err)
// }
// Set a handler for when a new remote track starts
peerConnection.OnTrack(wcodec.ReceiveTrack(peerConnection, *file, codec))
// wait for a call request
var inc_req wmsg.IncomingCallRequest
for {
inc_req, err = wmsg.NewIncomingCallRequest(<- recv)
if err != nil {
log.Println("NewIncomingCallRequest:", err)
continue
}
break
}
log.Println("new call from:", inc_req.From)
// create offer
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
log.Fatalln("cannot create offer:", err)
}
// describe(offer)
// respond: accept
state = SETUP
if err = peerConnection.SetLocalDescription(offer); err != nil {
log.Fatalln("cannot set local SDP:", err)
}
send <- wmsg.NewIncomingCallResponse(inc_req.From, "accept", offer.SDP)
// wait for a startCommunication message
var start_com wmsg.StartCommunication
for {
start_com, err = wmsg.NewStartCommunication(<- recv)
if err != nil {
log.Println("NewStartCommunication:", err)
continue
}
break
}
log.Println("start communication:")
// remove conflicting fingerprints from SDP
desc, errSdp := wmsg.ParseSdp(webrtc.SDPTypeAnswer, start_com.Sdp)
if errSdp != nil {
log.Fatalln("could not parse SDP answer:", errSdp)
}
// desc := webrtc.SessionDescription{Type: webrtc.SDPTypeAnswer, SDP: start_com.Sdp}
log.Printf("Setting remote session description: %v\n", *desc)
err = peerConnection.SetRemoteDescription(*desc)
if err != nil {
log.Fatalln("cannot set remote SDP:", err)
}
state = BUSY
log.Println("new state:", state)
<-gatherComplete
// process remaining cached REMOTE ICE candidates
candidateLock.Lock()
processCachedCandidates(peerConnection, candidateCache);
candidateLock.Unlock()
log.Println("connection setup ready")
// Block forever
select {}
default:
log.Printf("Unknown role: %s\n", role)
os.Exit(1)
}
os.Exit(0)
}