private static async void MessageReceived(WebSocketContext context, string msg) { //Console.WriteLine($"websocket recv: {msg}"); var offerSDP = SDP.ParseSDPDescription(msg); Console.WriteLine($"offer sdp: {offerSDP}"); var webRtcSession = new WebRtcSession( AddressFamily.InterNetwork, DTLS_CERTIFICATE_FINGERPRINT, null, null); webRtcSession.setRemoteDescription(new RTCSessionDescription { sdp = offerSDP, type = RTCSdpType.offer }); webRtcSession.OnReceiveReport += RtpSession_OnReceiveReport; webRtcSession.OnSendReport += RtpSession_OnSendReport; webRtcSession.OnRtpPacketReceived += RtpSession_OnRtpPacketReceived; webRtcSession.OnClose += (reason) => { Console.WriteLine($"webrtc session closed: {reason}"); _webRtcSessions.Remove(webRtcSession); }; // Add local recvonly tracks. This ensures that the SDP answer includes only // the codecs we support. MediaStreamTrack audioTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.audio, false, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.PCMU) }); audioTrack.Transceiver.SetStreamStatus(MediaStreamStatusEnum.RecvOnly); webRtcSession.addTrack(audioTrack); MediaStreamTrack videoTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.video, false, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.VP8) }); videoTrack.Transceiver.SetStreamStatus(MediaStreamStatusEnum.RecvOnly); webRtcSession.addTrack(videoTrack); var answerSdp = await webRtcSession.createAnswer(); webRtcSession.setLocalDescription(new RTCSessionDescription { sdp = answerSdp, type = RTCSdpType.answer }); Console.WriteLine($"answer sdp: {answerSdp}"); context.WebSocket.Send(answerSdp.ToString()); if (DoDtlsHandshake(webRtcSession)) { _webRtcSessions.Add(webRtcSession); } else { webRtcSession.Close("dtls handshake failed."); } }
private static async Task <WebRtcSession> SendSDPOffer(WebSocketContext context) { logger.LogDebug($"Web socket client connection from {context.UserEndPoint}."); var webRtcSession = new WebRtcSession( AddressFamily.InterNetwork, DTLS_CERTIFICATE_FINGERPRINT, null, null); MediaStreamTrack audioTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.audio, false, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.PCMU) }); webRtcSession.addTrack(audioTrack); MediaStreamTrack videoTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.video, false, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.VP8) }); webRtcSession.addTrack(videoTrack); //webRtcSession.RtpSession.OnReceiveReport += RtpSession_OnReceiveReport; webRtcSession.OnSendReport += RtpSession_OnSendReport; OnMediaSampleReady += webRtcSession.SendMedia; webRtcSession.OnClose += (reason) => { logger.LogDebug($"WebRtcSession was closed with reason {reason}"); OnMediaSampleReady -= webRtcSession.SendMedia; webRtcSession.OnReceiveReport -= RtpSession_OnReceiveReport; webRtcSession.OnSendReport -= RtpSession_OnSendReport; }; var offerSdp = await webRtcSession.createOffer(null); webRtcSession.setLocalDescription(new RTCSessionDescription { sdp = offerSdp, type = RTCSdpType.offer }); logger.LogDebug($"Sending SDP offer to client {context.UserEndPoint}."); context.WebSocket.Send(offerSdp.ToString()); if (DoDtlsHandshake(webRtcSession)) { if (!_isSampling) { _ = Task.Run(StartMedia); } } else { webRtcSession.Close("dtls handshake failed."); } return(webRtcSession); }
private static WebRtcSession SendSDPOffer(WebSocketContext context) { Log.LogDebug($"Web socket client connection from {context.UserEndPoint}."); _webRtcSession = new WebRtcSession( AddressFamily.InterNetwork, DTLS_CERTIFICATE_FINGERPRINT, null, null); _webRtcSession.OnReceiveReport += RtpSession_OnReceiveReport; _webRtcSession.OnSendReport += RtpSession_OnSendReport; Log.LogDebug($"Sending SDP offer to client {context.UserEndPoint}."); _webRtcSession.OnClose += (reason) => { Log.LogDebug($"WebRtcSession was closed with reason {reason}"); _webRtcSession.OnReceiveReport -= RtpSession_OnReceiveReport; _webRtcSession.OnSendReport -= RtpSession_OnSendReport; }; MediaStreamTrack audioTrack = new MediaStreamTrack(null, SDPMediaTypesEnum.audio, false, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.PCMU) }); _webRtcSession.addTrack(audioTrack); var offer = _webRtcSession.createOffer(null).Result; _webRtcSession.setLocalDescription(new RTCSessionDescription { type = RTCSdpType.offer, sdp = offer }); context.WebSocket.Send(offer.ToString()); if (DoDtlsHandshake(_webRtcSession)) { Log.LogInformation("DTLS handshake completed successfully."); } else { _webRtcSession.Close("dtls handshake failed."); } return(_webRtcSession); }
private static async Task <WebRtcSession> SendSDPOffer(WebSocketContext context) { logger.LogDebug($"Web socket client connection from {context.UserEndPoint}."); var webRtcSession = new WebRtcSession( AddressFamily.InterNetwork, DTLS_CERTIFICATE_FINGERPRINT, null, null); webRtcSession.addTrack(SDPMediaTypesEnum.video, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.VP8) }); webRtcSession.RtpSession.OnReceiveReport += RtpSession_OnReceiveReport; webRtcSession.RtpSession.OnSendReport += RtpSession_OnSendReport; webRtcSession.OnClose += (reason) => { logger.LogDebug($"WebRtcSession was closed with reason {reason}"); OnTestPatternSampleReady -= webRtcSession.SendMedia; webRtcSession.RtpSession.OnReceiveReport -= RtpSession_OnReceiveReport; webRtcSession.RtpSession.OnSendReport -= RtpSession_OnSendReport; }; var offerSdp = await webRtcSession.createOffer(); webRtcSession.setLocalDescription(offerSdp); logger.LogDebug($"Sending SDP offer to client {context.UserEndPoint}."); logger.LogDebug(webRtcSession.SDP.ToString()); context.WebSocket.Send(webRtcSession.SDP.ToString()); if (DoDtlsHandshake(webRtcSession)) { OnTestPatternSampleReady += webRtcSession.SendMedia; } else { webRtcSession.Close("dtls handshake failed."); } return(webRtcSession); }
private static WebRtcSession SendSDPOffer(WebSocketContext context) { Log.LogDebug($"Web socket client connection from {context.UserEndPoint}."); var webRtcSession = new WebRtcSession( AddressFamily.InterNetwork, DTLS_CERTIFICATE_FINGERPRINT, null, null); //webRtcSession.AudioStreamStatus = MediaStreamStatusEnum.SendOnly; //webRtcSession.VideoStreamStatus = MediaStreamStatusEnum.SendOnly; webRtcSession.RtpSession.OnReceiveReport += RtpSession_OnReceiveReport; webRtcSession.RtpSession.OnSendReport += RtpSession_OnSendReport; Log.LogDebug($"Sending SDP offer to client {context.UserEndPoint}."); webRtcSession.OnClose += (reason) => { Log.LogDebug($"WebRtcSession was closed with reason {reason}"); OnMediaSampleReady -= webRtcSession.SendMedia; webRtcSession.RtpSession.OnReceiveReport -= RtpSession_OnReceiveReport; webRtcSession.RtpSession.OnSendReport -= RtpSession_OnSendReport; }; webRtcSession.addTrack(SDPMediaTypesEnum.audio, new List <SDPMediaFormat> { new SDPMediaFormat(SDPMediaFormatsEnum.PCMU) }); if (DoDtlsHandshake(webRtcSession)) { context.WebSocket.Send(webRtcSession.SDP.ToString()); } else { webRtcSession.Close("dtls handshake failed."); } return(webRtcSession); }
private static void SDPAnswerReceived(WebRtcSession webRtcSession, string sdpAnswer) { try { logger.LogDebug("Answer SDP: " + sdpAnswer); var answerSDP = SDP.ParseSDPDescription(sdpAnswer); webRtcSession.SdpSessionID = answerSDP.SessionId; webRtcSession.RemoteIceUser = answerSDP.IceUfrag; webRtcSession.RemoteIcePassword = answerSDP.IcePwd; // All browsers seem to have gone to trickling ICE candidates now but just // in case one or more are given we can start the STUN dance immediately. if (answerSDP.IceCandidates != null) { foreach (var iceCandidate in answerSDP.IceCandidates) { webRtcSession.AppendRemoteIceCandidate(iceCandidate); } } OnTestPatternSampleReady += (timestamp, sample) => { try { webRtcSession.SendMedia(SDPMediaTypesEnum.video, timestamp, sample); } catch (Exception sendExcp) { logger.LogWarning("Exception OnTestPatternSampleReady. " + sendExcp.Message); webRtcSession.Close(); } }; } catch (Exception excp) { logger.LogError("Exception SDPAnswerReceived. " + excp.Message); } }