diff --git a/examples/janus-gateway/streaming/main.go b/examples/janus-gateway/streaming/main.go index 313f84cc252..bb04b5310b1 100644 --- a/examples/janus-gateway/streaming/main.go +++ b/examples/janus-gateway/streaming/main.go @@ -52,6 +52,7 @@ func main() { peerConnection, err := webrtc.New(config) util.Check(err) + peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { fmt.Printf("Connection State has changed %s \n", connectionState.String()) }) diff --git a/pkg/logging/logging.go b/pkg/logging/logging.go new file mode 100644 index 00000000000..677a47d0bc2 --- /dev/null +++ b/pkg/logging/logging.go @@ -0,0 +1,91 @@ +package logging + +import ( + "io/ioutil" + "log" + "os" +) + +type logLevel int + +const( + Error = iota + 1 + Warn + Info + Debug + Trace +) + +// RTCPeerLogger is the default logging struct for rtcpeerconnection. +type RTCPeerLogger struct{ + info, debug, warning, + trace, error *log.Logger +} + +// webrtcLogger is the interface that must be implemented if a custom +// logger is used. +type webrtcLogger interface{ + Info(msg string) + Trace(msg string) + Debug(msg string) + Warning(msg string) + Error(msg string) +} + +func (pclog *RTCPeerLogger) Debug(debug string){ + pclog.debug.Println(debug) +} + +func (pclog *RTCPeerLogger) Error(error string){ + pclog.error.Println(error) +} + +func (pclog *RTCPeerLogger) Info(info string){ + pclog.info.Println(info) +} + +func (pclog *RTCPeerLogger) Warning(warning string){ + pclog.warning.Println(warning) +} + +func (pclog *RTCPeerLogger) Trace(trace string){ + pclog.trace.Println(trace) +} + +func (pclog *RTCPeerLogger) SetLogLevel(l logLevel) { + + debugOut := ioutil.Discard + infoOut := ioutil.Discard + traceOut := ioutil.Discard + warningOut := ioutil.Discard + errorOut := ioutil.Discard + + switch l { + case Error: + errorOut = os.Stdout + case Warn: + errorOut = os.Stdout + warningOut = os.Stdout + case Info: + errorOut = os.Stdout + warningOut = os.Stdout + infoOut = os.Stdout + case Debug: + errorOut = os.Stdout + warningOut = os.Stdout + infoOut = os.Stdout + debugOut = os.Stdout + case Trace: + errorOut = os.Stdout + warningOut = os.Stdout + infoOut = os.Stdout + debugOut = os.Stdout + traceOut = os.Stdout + } + + pclog.debug = log.New(debugOut, "DEBUG: ", log.Lshortfile) + pclog.info = log.New(infoOut, "INFO: ", log.Lshortfile) + pclog.trace = log.New(traceOut, "TRACE: ", log.Lshortfile) + pclog.warning = log.New(warningOut,"WARNING: ", log.Lshortfile) + pclog.error = log.New(errorOut, "ERROR: ", log.Lshortfile) +} \ No newline at end of file diff --git a/rtcpeerconnection.go b/rtcpeerconnection.go index 101b970ff1b..c8b38fe3658 100644 --- a/rtcpeerconnection.go +++ b/rtcpeerconnection.go @@ -6,6 +6,7 @@ import ( "crypto/elliptic" "crypto/rand" "fmt" + "github.com/pions/webrtc/pkg/logging" "net" "strings" "sync" @@ -27,6 +28,7 @@ import ( // comparisons when no value was defined. const Unknown = iota + // RTCPeerConnection represents a WebRTC connection that establishes a // peer-to-peer communications with another RTCPeerConnection instance in a // browser, or to another endpoint implementing the required protocols. @@ -109,6 +111,10 @@ type RTCPeerConnection struct { // Deprecated: Internal mechanism which will be removed. networkManager *network.Manager + + // Default Logging member + pclog *logging.RTCPeerLogger + } // New creates a new RTCPeerConfiguration with the provided configuration @@ -137,8 +143,11 @@ func New(configuration RTCConfiguration) (*RTCPeerConnection, error) { mediaEngine: DefaultMediaEngine, sctpTransport: newRTCSctpTransport(), dataChannels: make(map[uint16]*RTCDataChannel), + pclog: &logging.RTCPeerLogger{}, } + pc.pclog.SetLogLevel(logging.Debug) + var err error if err = pc.initConfiguration(configuration); err != nil { return nil, err @@ -159,7 +168,7 @@ func New(configuration RTCConfiguration) (*RTCPeerConnection, error) { err = pc.networkManager.AddURL(url) if err != nil { - fmt.Println(err) + pc.pclog.Debug(err.Error()) } } } @@ -702,7 +711,7 @@ func (pc *RTCPeerConnection) SetRemoteDescription(desc RTCSessionDescription) er if c := sdp.ICECandidateUnmarshal(*a.String()); c != nil { pc.networkManager.IceAgent.AddRemoteCandidate(c) } else { - fmt.Printf("Tried to parse ICE candidate, but failed %s ", a) + pc.pclog.Debug(fmt.Sprintf("Tried to parse ICE candidate, but failed %s ", a)) } } else if strings.HasPrefix(*a.String(), "ice-ufrag") { remoteUfrag = (*a.String())[len("ice-ufrag:"):] @@ -1007,6 +1016,7 @@ func (pc *RTCPeerConnection) Close() error { return nil } + /* Everything below is private */ func (pc *RTCPeerConnection) generateChannel(ssrc uint32, payloadType uint8) (buffers chan<- *rtp.Packet) { pc.RLock() @@ -1018,13 +1028,13 @@ func (pc *RTCPeerConnection) generateChannel(ssrc uint32, payloadType uint8) (bu sdpCodec, err := pc.CurrentLocalDescription.parsed.GetCodecForPayloadType(payloadType) if err != nil { - fmt.Printf("No codec could be found in RemoteDescription for payloadType %d \n", payloadType) + pc.pclog.Debug(fmt.Sprintf("No codec could be found in RemoteDescription for payloadType %d \n", payloadType)) return nil } codec, err := pc.mediaEngine.getCodecSDP(sdpCodec) if err != nil { - fmt.Printf("Codec %s in not registered\n", sdpCodec) + pc.pclog.Debug(fmt.Sprintf("Codec %s in not registered\n", sdpCodec)) return nil } @@ -1074,7 +1084,7 @@ func (pc *RTCPeerConnection) dataChannelEventHandler(e network.DataChannelEvent) dc.onMessage(event.Payload) } else { pc.RUnlock() - fmt.Printf("No datachannel found for streamIdentifier %d \n", e.StreamIdentifier()) + pc.pclog.Debug(fmt.Sprintf("No datachannel found for streamIdentifier %d \n", e.StreamIdentifier())) } case *network.DataChannelOpen: pc.RLock() @@ -1083,7 +1093,7 @@ func (pc *RTCPeerConnection) dataChannelEventHandler(e network.DataChannelEvent) dc.Lock() err := dc.sendOpenChannelMessage() if err != nil { - fmt.Println("failed to send openchannel", err) + pc.pclog.Debug("failed to send openchannel" + err.Error()) dc.Unlock() continue } @@ -1093,7 +1103,7 @@ func (pc *RTCPeerConnection) dataChannelEventHandler(e network.DataChannelEvent) dc.onOpen() // TODO: move to ChannelAck handling } default: - fmt.Printf("Unhandled DataChannelEvent %v \n", event) + pc.pclog.Debug(fmt.Sprintf("Unhandled DataChannelEvent %v \n", event)) } } @@ -1290,3 +1300,4 @@ func (pc *RTCPeerConnection) newRTCRtpTransceiver( pc.rtpTransceivers = append(pc.rtpTransceivers, t) return t } +