fix logging for WebRTC.

This commit is contained in:
m1k1o 2021-02-14 21:39:05 +01:00
parent 8ef91be6ad
commit 29fc67aff9

View File

@ -120,10 +120,15 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
if err != nil { if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err return "", manager.config.ICELite, manager.config.ICEServers, err
} }
negotiated := true negotiated := true
connection.CreateDataChannel("data", &webrtc.DataChannelInit{ _, err = connection.CreateDataChannel("data", &webrtc.DataChannelInit{
Negotiated: &negotiated, Negotiated: &negotiated,
}) })
if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
connection.OnDataChannel(func(d *webrtc.DataChannel) { connection.OnDataChannel(func(d *webrtc.DataChannel) {
d.OnMessage(func(msg webrtc.DataChannelMessage) { d.OnMessage(func(msg webrtc.DataChannelMessage) {
if err = manager.handle(id, msg); err != nil { if err = manager.handle(id, msg); err != nil {
@ -135,7 +140,9 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
// Set the handler for ICE connection state // Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
connection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { connection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String()) manager.logger.Info().
Str("connection_state", connectionState.String()).
Msg("connection state has changed")
}) })
rtpSender, viderr := connection.AddTrack(manager.videoTrack) rtpSender, viderr := connection.AddTrack(manager.videoTrack)
@ -154,7 +161,7 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
err = connection.SetLocalDescription(description) err = connection.SetLocalDescription(description)
if err != nil { if err != nil {
panic(err) return "", manager.config.ICELite, manager.config.ICEServers, err
} }
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
@ -175,46 +182,50 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
}) })
connection.OnICECandidate(func(i *webrtc.ICECandidate) { connection.OnICECandidate(func(i *webrtc.ICECandidate) {
if i != nil { if i == nil {
candidateString, err := json.Marshal(i.ToJSON()) manager.logger.Info().Msg("sent all ICECandidates")
if err != nil { return
manager.logger.Info().Msg("error") }
return
}
if err = session.SignalCandidate(string(candidateString));err != nil { candidateString, err := json.Marshal(i.ToJSON())
manager.logger.Info().Msg("err") if err != nil {
return manager.logger.Warn().Err(err).Msg("converting ICECandidate to json failed")
} return
}
if err := session.SignalCandidate(string(candidateString)); err != nil {
manager.logger.Warn().Err(err).Msg("sending SignalCandidate failed")
return
} }
}) })
// Read incoming RTCP packets // Read incoming RTCP packets
// Before these packets are retuned they are processed by interceptors. For things // Before these packets are retuned they are processed by interceptors. For things
// like NACK this needs to be called. // like NACK this needs to be called.
go func() { go func() {
rtcpBuf := make([]byte, 1500) rtcpBuf := make([]byte, 1500)
for { for {
n, _, rtcpErr := rtpSender.Read(rtcpBuf) n, _, err := rtpSender.Read(rtcpBuf)
if rtcpErr != nil { if err != nil {
return return
} }
ps, err := rtcp.Unmarshal(rtcpBuf[:n]) ps, err := rtcp.Unmarshal(rtcpBuf[:n])
if err != nil { if err != nil {
log.Printf("Unmarshal RTCP: %v", err) manager.logger.Warn().Err(err).Msg("unmarshal RTCP failed")
continue continue
} }
for _, p := range ps { for _, p := range ps {
switch p.(type) { switch p.(type) {
case *rtcp.TransportLayerNack: case *rtcp.TransportLayerNack:
manager.logger.Info().Msg("got a nack") manager.logger.Warn().Msg("got a nack")
} }
} }
} }
}() }()
if err := session.SetPeer(&Peer{ if err := session.SetPeer(&Peer{
id: id, id: id,
api: api, api: api,
@ -232,31 +243,30 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
func (m *WebRTCManager) createTrack(codecName string) (*webrtc.TrackLocalStaticSample, webrtc.RTPCodecParameters, error) { func (m *WebRTCManager) createTrack(codecName string) (*webrtc.TrackLocalStaticSample, webrtc.RTPCodecParameters, error) {
var codec webrtc.RTPCodecParameters var codec webrtc.RTPCodecParameters
var fb []webrtc.RTCPFeedback
var fba []webrtc.RTCPFeedback fba := []webrtc.RTCPFeedback{}
fb = []webrtc.RTCPFeedback{ fbv := []webrtc.RTCPFeedback{
{"goog-remb", ""}, {"goog-remb", ""},
{"nack", ""}, {"nack", ""},
{"nack", "pli"}, {"nack", "pli"},
{"ccm", "fir"}, {"ccm", "fir"},
} }
fba = []webrtc.RTCPFeedback{}
switch codecName { switch codecName {
case "VP8": case "VP8":
codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fb}, PayloadType: 96,} codec = webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fbv}, PayloadType: 96}
case "VP9": case "VP9":
codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP9", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fb}, PayloadType: 98,} codec = webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP9", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fbv}, PayloadType: 98}
case "H264": case "H264":
codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/H264", ClockRate: 90000, Channels: 0, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f", RTCPFeedback: fb}, PayloadType: 102,} codec = webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/H264", ClockRate: 90000, Channels: 0, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f", RTCPFeedback: fbv}, PayloadType: 102}
case "Opus": case "Opus":
codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/opus", ClockRate: 48000, Channels: 2, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 111,} codec = webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/opus", ClockRate: 48000, Channels: 2, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 111}
case "G722": case "G722":
codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/G722", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 9,} codec = webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/G722", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 9}
case "PCMU": case "PCMU":
codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/PCMU", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 0,} codec = webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/PCMU", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 0}
case "PCMA": case "PCMA":
codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/PCMA", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 8,} codec = webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/PCMA", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 8}
default: default:
return nil, codec, fmt.Errorf("unknown codec %s", codecName) return nil, codec, fmt.Errorf("unknown codec %s", codecName)
} }