/// <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); }