-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
main.go
125 lines (105 loc) · 3.76 KB
/
main.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
package main
import (
"fmt"
"github.com/pion/logging"
"github.com/pion/webrtc/v3"
)
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
// customLogger satisfies the interface logging.LeveledLogger
// a logger is created per subsystem in Pion, so you can have custom
// behavior per subsystem (ICE, DTLS, SCTP...)
type customLogger struct {
}
// Print all messages except trace
func (c customLogger) Trace(msg string) {}
func (c customLogger) Tracef(format string, args ...interface{}) {}
func (c customLogger) Debug(msg string) { fmt.Printf("customLogger Debug: %s\n", msg) }
func (c customLogger) Debugf(format string, args ...interface{}) {
c.Debug(fmt.Sprintf(format, args...))
}
func (c customLogger) Info(msg string) { fmt.Printf("customLogger Info: %s\n", msg) }
func (c customLogger) Infof(format string, args ...interface{}) {
c.Trace(fmt.Sprintf(format, args...))
}
func (c customLogger) Warn(msg string) { fmt.Printf("customLogger Warn: %s\n", msg) }
func (c customLogger) Warnf(format string, args ...interface{}) {
c.Warn(fmt.Sprintf(format, args...))
}
func (c customLogger) Error(msg string) { fmt.Printf("customLogger Error: %s\n", msg) }
func (c customLogger) Errorf(format string, args ...interface{}) {
c.Error(fmt.Sprintf(format, args...))
}
// customLoggerFactory satisfies the interface logging.LoggerFactory
// This allows us to create different loggers per subsystem. So we can
// add custom behavior
type customLoggerFactory struct {
}
func (c customLoggerFactory) NewLogger(subsystem string) logging.LeveledLogger {
fmt.Printf("Creating logger for %s \n", subsystem)
return customLogger{}
}
func main() {
// Create a new API with a custom logger
// This SettingEngine allows non-standard WebRTC behavior
s := webrtc.SettingEngine{
LoggerFactory: customLoggerFactory{},
}
api := webrtc.NewAPI(webrtc.WithSettingEngine(s))
// Create a new RTCPeerConnection
offerPeerConnection, err := api.NewPeerConnection(webrtc.Configuration{})
if err != nil {
panic(err)
}
// Create a new RTCPeerConnection
answerPeerConnection, err := api.NewPeerConnection(webrtc.Configuration{})
if err != nil {
panic(err)
}
// Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
// send it to the other peer
answerPeerConnection.OnICECandidate(func(i *webrtc.ICECandidate) {
if i != nil {
if iceErr := offerPeerConnection.AddICECandidate(i.ToJSON()); iceErr != nil {
panic(iceErr)
}
}
})
// Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
// send it to the other peer
offerPeerConnection.OnICECandidate(func(i *webrtc.ICECandidate) {
if i != nil {
if iceErr := answerPeerConnection.AddICECandidate(i.ToJSON()); iceErr != nil {
panic(iceErr)
}
}
})
// Create an offer for the other PeerConnection
offer, err := offerPeerConnection.CreateOffer(nil)
if err != nil {
panic(err)
}
// SetLocalDescription, needed before remote gets offer
if err = offerPeerConnection.SetLocalDescription(offer); err != nil {
panic(err)
}
// Take offer from remote, answerPeerConnection is now able to contact
// the other PeerConnection
if err = answerPeerConnection.SetRemoteDescription(offer); err != nil {
panic(err)
}
// Create an Answer to send back to our originating PeerConnection
answer, err := answerPeerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
}
// Set the answerer's LocalDescription
if err = answerPeerConnection.SetLocalDescription(answer); err != nil {
panic(err)
}
// SetRemoteDescription on original PeerConnection, this finishes our signaling
// bother PeerConnections should be able to communicate with each other now
if err = offerPeerConnection.SetRemoteDescription(answer); err != nil {
panic(err)
}
select {}
}