private async void WebRtcStartCall(WebSocketSharp.Net.WebSockets.WebSocketContext context, string webSocketID, MediaSourceEnum mediaSource) { logger.LogDebug($"New WebRTC client added for web socket connection {webSocketID}."); if (!_webRtcConnections.Any(x => x.Key == webSocketID)) { var webRtcSession = new WebRtcSession(AddressFamily.InterNetwork, _dtlsCertificateThumbprint, null, null); webRtcSession.addTrack(SDPMediaTypesEnum.video, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.VP8) }); // Don't need an audio track for the test pattern feed. if (mediaSource == MediaSourceEnum.Max) { webRtcSession.addTrack(SDPMediaTypesEnum.audio, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.PCMU) }); } WebRtcConnection conn = new WebRtcConnection(webRtcSession); if (_webRtcConnections.TryAdd(webSocketID, conn)) { webRtcSession.OnClose += (reason) => PeerClosed(webSocketID, reason); webRtcSession.RtpSession.OnRtcpBye += (reason) => PeerClosed(webSocketID, reason); var offerSdp = await webRtcSession.createOffer(); webRtcSession.setLocalDescription(offerSdp); logger.LogDebug($"Sending SDP offer to client {context.UserEndPoint}."); context.WebSocket.Send(webRtcSession.SDP.ToString()); if (DoDtlsHandshake(webRtcSession)) { if (mediaSource == MediaSourceEnum.Max) { OnMp4MediaSampleReady += conn.SendMedia; if (!_isMp4Sampling) { _ = Task.Run(SampleMp4Media); } } else if (mediaSource == MediaSourceEnum.TestPattern) { OnTestPatternSampleReady += conn.SendMedia; if (!_isTestPatternSampling) { _ = Task.Run(SampleTestPattern); } } } else { PeerClosed(webSocketID, "dtls handshake failed"); } } else { logger.LogError("Failed to add new WebRTC client."); } } }