github.com/pion/webrtc/v3@v3.2.24/examples/custom-logger/main.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 //go:build !js 5 // +build !js 6 7 // custom-logger is an example of how the Pion API provides an customizable logging API 8 package main 9 10 import ( 11 "fmt" 12 "os" 13 14 "github.com/pion/logging" 15 "github.com/pion/webrtc/v3" 16 ) 17 18 // Everything below is the Pion WebRTC API! Thanks for using it ❤️. 19 20 // customLogger satisfies the interface logging.LeveledLogger 21 // a logger is created per subsystem in Pion, so you can have custom 22 // behavior per subsystem (ICE, DTLS, SCTP...) 23 type customLogger struct{} 24 25 // Print all messages except trace 26 func (c customLogger) Trace(string) {} 27 func (c customLogger) Tracef(string, ...interface{}) {} 28 29 func (c customLogger) Debug(msg string) { fmt.Printf("customLogger Debug: %s\n", msg) } 30 func (c customLogger) Debugf(format string, args ...interface{}) { 31 c.Debug(fmt.Sprintf(format, args...)) 32 } 33 func (c customLogger) Info(msg string) { fmt.Printf("customLogger Info: %s\n", msg) } 34 func (c customLogger) Infof(format string, args ...interface{}) { 35 c.Trace(fmt.Sprintf(format, args...)) 36 } 37 func (c customLogger) Warn(msg string) { fmt.Printf("customLogger Warn: %s\n", msg) } 38 func (c customLogger) Warnf(format string, args ...interface{}) { 39 c.Warn(fmt.Sprintf(format, args...)) 40 } 41 func (c customLogger) Error(msg string) { fmt.Printf("customLogger Error: %s\n", msg) } 42 func (c customLogger) Errorf(format string, args ...interface{}) { 43 c.Error(fmt.Sprintf(format, args...)) 44 } 45 46 // customLoggerFactory satisfies the interface logging.LoggerFactory 47 // This allows us to create different loggers per subsystem. So we can 48 // add custom behavior 49 type customLoggerFactory struct{} 50 51 func (c customLoggerFactory) NewLogger(subsystem string) logging.LeveledLogger { 52 fmt.Printf("Creating logger for %s \n", subsystem) 53 return customLogger{} 54 } 55 56 func main() { 57 // Create a new API with a custom logger 58 // This SettingEngine allows non-standard WebRTC behavior 59 s := webrtc.SettingEngine{ 60 LoggerFactory: customLoggerFactory{}, 61 } 62 api := webrtc.NewAPI(webrtc.WithSettingEngine(s)) 63 64 // Create a new RTCPeerConnection 65 offerPeerConnection, err := api.NewPeerConnection(webrtc.Configuration{}) 66 if err != nil { 67 panic(err) 68 } 69 defer func() { 70 if cErr := offerPeerConnection.Close(); cErr != nil { 71 fmt.Printf("cannot close offerPeerConnection: %v\n", cErr) 72 } 73 }() 74 75 // We need a DataChannel so we can have ICE Candidates 76 if _, err = offerPeerConnection.CreateDataChannel("custom-logger", nil); err != nil { 77 panic(err) 78 } 79 80 // Create a new RTCPeerConnection 81 answerPeerConnection, err := api.NewPeerConnection(webrtc.Configuration{}) 82 if err != nil { 83 panic(err) 84 } 85 defer func() { 86 if cErr := answerPeerConnection.Close(); cErr != nil { 87 fmt.Printf("cannot close answerPeerConnection: %v\n", cErr) 88 } 89 }() 90 91 // Set the handler for Peer connection state 92 // This will notify you when the peer has connected/disconnected 93 offerPeerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) { 94 fmt.Printf("Peer Connection State has changed: %s (offerer)\n", s.String()) 95 96 if s == webrtc.PeerConnectionStateFailed { 97 // Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart. 98 // Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout. 99 // Note that the PeerConnection may come back from PeerConnectionStateDisconnected. 100 fmt.Println("Peer Connection has gone to failed exiting") 101 os.Exit(0) 102 } 103 }) 104 105 // Set the handler for Peer connection state 106 // This will notify you when the peer has connected/disconnected 107 answerPeerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) { 108 fmt.Printf("Peer Connection State has changed: %s (answerer)\n", s.String()) 109 110 if s == webrtc.PeerConnectionStateFailed { 111 // Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart. 112 // Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout. 113 // Note that the PeerConnection may come back from PeerConnectionStateDisconnected. 114 fmt.Println("Peer Connection has gone to failed exiting") 115 os.Exit(0) 116 } 117 }) 118 119 // Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate 120 // send it to the other peer 121 answerPeerConnection.OnICECandidate(func(i *webrtc.ICECandidate) { 122 if i != nil { 123 if iceErr := offerPeerConnection.AddICECandidate(i.ToJSON()); iceErr != nil { 124 panic(iceErr) 125 } 126 } 127 }) 128 129 // Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate 130 // send it to the other peer 131 offerPeerConnection.OnICECandidate(func(i *webrtc.ICECandidate) { 132 if i != nil { 133 if iceErr := answerPeerConnection.AddICECandidate(i.ToJSON()); iceErr != nil { 134 panic(iceErr) 135 } 136 } 137 }) 138 139 // Create an offer for the other PeerConnection 140 offer, err := offerPeerConnection.CreateOffer(nil) 141 if err != nil { 142 panic(err) 143 } 144 145 // SetLocalDescription, needed before remote gets offer 146 if err = offerPeerConnection.SetLocalDescription(offer); err != nil { 147 panic(err) 148 } 149 150 // Take offer from remote, answerPeerConnection is now able to contact 151 // the other PeerConnection 152 if err = answerPeerConnection.SetRemoteDescription(offer); err != nil { 153 panic(err) 154 } 155 156 // Create an Answer to send back to our originating PeerConnection 157 answer, err := answerPeerConnection.CreateAnswer(nil) 158 if err != nil { 159 panic(err) 160 } 161 162 // Set the answerer's LocalDescription 163 if err = answerPeerConnection.SetLocalDescription(answer); err != nil { 164 panic(err) 165 } 166 167 // SetRemoteDescription on original PeerConnection, this finishes our signaling 168 // bother PeerConnections should be able to communicate with each other now 169 if err = offerPeerConnection.SetRemoteDescription(answer); err != nil { 170 panic(err) 171 } 172 173 // Block forever 174 select {} 175 }