Ejemplo n.º 1
0
        public void DtlsPacketReceived(IceCandidate iceCandidate, byte[] buffer, IPEndPoint remoteEndPoint)
        {
            logger.Debug("DTLS packet received " + buffer.Length + " bytes from " + remoteEndPoint.ToString() + ".");

            if (DtlsContext == null)
            {
                DtlsContext = new DtlsManaged();
                int res = DtlsContext.Init();
                logger.Debug("DtlsContext initialisation result=" + res);
            }

            int bytesWritten = DtlsContext.Write(buffer, buffer.Length);

            if (bytesWritten != buffer.Length)
            {
                logger.Warn("The required number of bytes were not successfully written to the DTLS context.");
            }
            else
            {
                byte[] dtlsOutBytes = new byte[2048];

                int bytesRead = DtlsContext.Read(dtlsOutBytes, dtlsOutBytes.Length);

                if (bytesRead == 0)
                {
                    logger.Debug("No bytes read from DTLS context :(.");
                }
                else
                {
                    logger.Debug(bytesRead + " bytes read from DTLS context sending to " + remoteEndPoint.ToString() + ".");
                    iceCandidate.LocalRtpSocket.SendTo(dtlsOutBytes, 0, bytesRead, SocketFlags.None, remoteEndPoint);

                    //if (client.DtlsContext.IsHandshakeComplete())
                    if (DtlsContext.GetState() == 3)
                    {
                        logger.Debug("DTLS negotiation complete for " + remoteEndPoint.ToString() + ".");
                        SrtpContext                    = new SRTPManaged(DtlsContext, false);
                        SrtpReceiveContext             = new SRTPManaged(DtlsContext, true);
                        Peer.IsDtlsNegotiationComplete = true;
                        iceCandidate.RemoteRtpEndPoint = remoteEndPoint;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void DtlsPacketReceived(IceCandidate iceCandidate, byte[] buffer, IPEndPoint remoteEndPoint)
        {
            logger.Debug("DTLS packet received for media type " + iceCandidate.MediaType.ToString().ToUpper() + " of " + buffer.Length + " bytes from " + remoteEndPoint.ToString() + ".");

            if (!File.Exists(_dtlsCertFilePath))
            {
                throw new ApplicationException($"The DTLS certificate file could not be found at {_dtlsCertFilePath}.");
            }

            if (!File.Exists(_dtlsKeyFilePath))
            {
                throw new ApplicationException($"The DTLS key file could not be found at {_dtlsKeyFilePath}.");
            }

            if (DtlsContext == null)
            {
                lock (_secureChannelInitMre)
                {
                    DtlsContext = new DtlsManaged(_dtlsCertFilePath, _dtlsKeyFilePath);
                    int res = DtlsContext.Init();
                    logger.Debug("DtlsContext initialisation result=" + res);
                }
            }

            int bytesWritten = DtlsContext.Write(buffer, buffer.Length);

            if (bytesWritten != buffer.Length)
            {
                logger.Warn("The required number of bytes were not successfully written to the DTLS context.");
            }
            else
            {
                byte[] dtlsOutBytes = new byte[4096];

                int bytesRead = DtlsContext.Read(dtlsOutBytes, dtlsOutBytes.Length);

                if (bytesRead == 0)
                {
                    logger.Debug("No bytes read from DTLS context :(.");
                }
                else
                {
                    logger.Debug(bytesRead + " bytes read from DTLS context sending to " + remoteEndPoint.ToString() + ".");
                    iceCandidate.LocalRtpSocket.SendTo(dtlsOutBytes, 0, bytesRead, SocketFlags.None, remoteEndPoint);

                    //if (client.DtlsContext.IsHandshakeComplete())
                    if (DtlsContext.GetState() == 3)
                    {
                        logger.Debug("DTLS negotiation complete for " + remoteEndPoint.ToString() + ".");

                        lock (_secureChannelInitMre)
                        {
                            SrtpContext        = new SRTPManaged(DtlsContext, false);
                            SrtpReceiveContext = new SRTPManaged(DtlsContext, true);
                        }

                        Peer.IsDtlsNegotiationComplete = true;
                        iceCandidate.RemoteRtpEndPoint = remoteEndPoint;

                        _audioRtpSession = new RTPSession((int)RTPPayloadTypesEnum.PCMU, SrtpContext.ProtectRTP, SrtpContext.ProtectRTCP);
                        _videoRtpSession = new RTPSession(VP8_PAYLOAD_TYPE_ID, SrtpContext.ProtectRTP, SrtpContext.ProtectRTCP);
                    }
                }
            }
        }