/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="rtpSocket">The RTP socket being used for the WebRTC session.</param> private static int DoDtlsHandshake(WebRtcSession webRtcSession, Socket rtpSocket, out ProtectRtpPacket protectRtp, out ProtectRtpPacket protectRtcp) { logger.LogDebug("DoDtlsHandshake started."); protectRtp = null; protectRtcp = null; if (!File.Exists(DTLS_CERTIFICATE_PATH)) { throw new ApplicationException($"The DTLS certificate file could not be found at {DTLS_CERTIFICATE_PATH}."); } else if (!File.Exists(DTLS_KEY_PATH)) { throw new ApplicationException($"The DTLS key file could not be found at {DTLS_KEY_PATH}."); } var dtls = new Dtls(DTLS_CERTIFICATE_PATH, DTLS_KEY_PATH); webRtcSession.OnClose += (reason) => dtls.Shutdown(); int res = dtls.DoHandshake((ulong)rtpSocket.Handle); logger.LogDebug("DtlsContext initialisation result=" + res); if (dtls.GetState() == (int)DtlsState.OK) { logger.LogDebug("DTLS negotiation complete."); var srtpContext = new Srtp(dtls, false); protectRtp = srtpContext.ProtectRTP; protectRtcp = srtpContext.ProtectRTCP; } return(res); }
protected override void OnClose(CloseEventArgs e) { Server.RemoveClient(this); Server.BroadcastState(); SrtpContext?.Dispose(); SrtpContext = null; Dtls?.Dispose(); Dtls = null; base.OnClose(e); }
static void Main() { Console.WriteLine("WebRTC Server Sample Program"); Console.WriteLine("Press ctrl-c to exit."); // Plumbing code to facilitate a graceful exit. CancellationTokenSource exitCts = new CancellationTokenSource(); // Cancellation token to stop the SIP transport and RTP stream. ManualResetEvent exitMre = new ManualResetEvent(false); AddConsoleLogger(); // Initialise OpenSSL & libsrtp, saves a couple of seconds for the first client connection. Console.WriteLine("Initialising OpenSSL and libsrtp..."); Dtls.InitialiseOpenSSL(); Srtp.InitialiseLibSrtp(); Task.Run(SendTestPattern); // Start web socket. Console.WriteLine("Starting web socket server..."); _webSocketServer = new WebSocketServer(IPAddress.Any, WEBSOCKET_PORT, true); _webSocketServer.SslConfiguration.ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(WEBSOCKET_CERTIFICATE_PATH); _webSocketServer.SslConfiguration.CheckCertificateRevocation = false; //_webSocketServer.Log.Level = WebSocketSharp.LogLevel.Debug; _webSocketServer.AddWebSocketService <SDPExchange>("/", (sdpExchanger) => { sdpExchanger.WebSocketOpened += SendSDPOffer; sdpExchanger.SDPAnswerReceived += SDPAnswerReceived; }); _webSocketServer.Start(); Console.WriteLine($"Waiting for browser web socket connection to {_webSocketServer.Address}:{_webSocketServer.Port}..."); // Ctrl-c will gracefully exit the call at any point. Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; exitMre.Set(); }; // Wait for a signal saying the call failed, was cancelled with ctrl-c or completed. exitMre.WaitOne(); }
static void Main() { Console.WriteLine("WebRTC Server Sample Program"); Console.WriteLine("Press ctrl-c to exit."); // Plumbing code to facilitate a graceful exit. CancellationTokenSource exitCts = new CancellationTokenSource(); // Cancellation token to stop the SIP transport and RTP stream. ManualResetEvent exitMre = new ManualResetEvent(false); AddConsoleLogger(); if (!File.Exists(MP4_FILE_PATH)) { throw new ApplicationException($"The media file at does not exist at {MP4_FILE_PATH}."); } // Initialise OpenSSL & libsrtp, saves a couple of seconds for the first client connection. Console.WriteLine("Initialising OpenSSL and libsrtp..."); Dtls.InitialiseOpenSSL(); Srtp.InitialiseLibSrtp(); // Initialise the Media Foundation library that will pull the samples from the mp4 file. _mfSampleGrabber = new SIPSorceryMedia.MFSampleGrabber(); _mfSampleGrabber.OnClockStartEvent += OnClockStartEvent; _mfSampleGrabber.OnVideoResolutionChangedEvent += OnVideoResolutionChangedEvent; unsafe { _mfSampleGrabber.OnProcessSampleEvent += OnProcessSampleEvent; } Task.Run(() => _mfSampleGrabber.Run(MP4_FILE_PATH, true)); // Hook up event handlers to send the media samples to the network. //InitMediaToWebRtcClients(); // Start web socket. Console.WriteLine("Starting web socket server..."); _webSocketServer = new WebSocketServer(IPAddress.Any, WEBSOCKET_PORT, true); _webSocketServer.SslConfiguration.ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(WEBSOCKET_CERTIFICATE_PATH); _webSocketServer.SslConfiguration.CheckCertificateRevocation = false; //_webSocketServer.Log.Level = WebSocketSharp.LogLevel.Debug; _webSocketServer.AddWebSocketService <SDPExchange>("/", (sdpExchanger) => { sdpExchanger.WebSocketOpened += SendSDPOffer; sdpExchanger.SDPAnswerReceived += SDPAnswerReceived; }); _webSocketServer.Start(); Console.WriteLine($"Waiting for browser web socket connection to {_webSocketServer.Address}:{_webSocketServer.Port}..."); // Ctrl-c will gracefully exit the call at any point. Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; exitMre.Set(); }; // Wait for a signal saying the call failed, was cancelled with ctrl-c or completed. exitMre.WaitOne(); _mfSampleGrabber.StopAndExit(); _webSocketServer.Stop(); }