Example #1
0
        public WebRtcSession(string dtlsFingerprint, List <IPAddress> offerAddresses)
        {
            _dtlsCertificateFingerprint = dtlsFingerprint;

            SessionID = Guid.NewGuid().ToString();

            _rtpSession     = new RTPSession((int)SDPMediaFormatsEnum.PCMU, AddressFamily.InterNetwork, true);
            _videoSessionID = _rtpSession.AddStream(VP8_PAYLOAD_TYPE_ID, null);
            _rtpChannel     = _rtpSession.RtpChannel;
            _rtpChannel.OnRTPDataReceived += OnRTPDataReceived;
            _rtpSession.OnRtpClosed       += Close;

            _offerAddresses = offerAddresses;
        }
Example #2
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="dtlsFingerprint">The fingerprint of our DTLS certificate (we always act as the DTLS server).
        /// It gets placed in the SDP offer sent to the remote party.</param>
        /// <param name="offerAddresses">Optional. A list of the IP addresses used as local ICE candidates.
        /// If null then all local IP addresses get used.</param>
        /// <param name="turnServerEndPoint">Optional. A parameter that can be used include a TURN
        /// server in this session's ICE candidate gathering.</param>
        public WebRtcSession(
            AddressFamily addrFamily,
            string dtlsFingerprint,
            List <IPAddress> offerAddresses,
            IPEndPoint turnServerEndPoint)
        {
            _dtlsCertificateFingerprint = dtlsFingerprint;
            _offerAddresses             = offerAddresses;
            _turnServerEndPoint         = turnServerEndPoint;

            SessionID = Guid.NewGuid().ToString();

            RtpSession = new RTPSession(addrFamily, true, true);

            _rtpChannel = RtpSession.RtpChannel;
            _rtpChannel.OnRTPDataReceived += OnRTPDataReceived;
            RtpSession.OnRtpClosed        += Close;
            RtpSession.OnRtcpBye          += Close;
        }
Example #3
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="dtlsFingerprint">The fingerprint of our DTLS certificate (we always act as the DTLS server).
        /// It gets placed in the SDP offer sent to the remote party.</param>
        /// <param name="supportedAudioFormats">List of audio codecs that we support. Can be null or empty if
        /// the session is not supporting audio.</param>
        /// <param name="supportedVideoFormats">List of video codecs that we support. Can be null or empty if
        /// the session is not supporting video.</param>
        /// <param name="offerAddresses">Optional. A list of the IP addresses used as local ICE candidates.
        /// If null then all local IP addresses get used.</param>
        public WebRtcSession(
            string dtlsFingerprint,
            List <SDPMediaFormat> supportedAudioFormats,
            List <SDPMediaFormat> supportedVideoFormats,
            List <IPAddress> offerAddresses)
        {
            if (supportedAudioFormats == null && supportedVideoFormats == null)
            {
                throw new ApplicationException("At least one of the audio or video supported formats must be specified.");
            }

            _dtlsCertificateFingerprint = dtlsFingerprint;
            _supportedAudioFormats      = supportedAudioFormats;
            _supportedVideoFormats      = supportedVideoFormats;

            SessionID = Guid.NewGuid().ToString();

            if (_supportedAudioFormats != null && supportedAudioFormats.Count > 0)
            {
                RtpSession = new RTPSession(SDPMediaTypesEnum.audio, (int)supportedAudioFormats.First().FormatCodec, AddressFamily.InterNetwork, true, true);
            }
            else if (_supportedVideoFormats != null && supportedVideoFormats.Count > 0)
            {
                RtpSession = new RTPSession(SDPMediaTypesEnum.video, (int)supportedVideoFormats.First().FormatCodec, AddressFamily.InterNetwork, true, true);
            }

            if (RtpSession == null)
            {
                throw new ApplicationException("No supported audio or video types were provided.");
            }

            _rtpChannel = RtpSession.RtpChannel;
            _rtpChannel.OnRTPDataReceived += OnRTPDataReceived;
            RtpSession.OnRtpClosed        += Close;

            _offerAddresses = offerAddresses;
        }
Example #4
0
        public void Start(string url)
        {
            _url = url;

            Match urlMatch = Regex.Match(url, @"rtsp://(?<hostname>\S+?)/", RegexOptions.IgnoreCase);

            if (!urlMatch.Success)
            {
                throw new ApplicationException("The URL provided to the RTSP client was not recognised, " + url + ".");
            }
            else
            {
                string hostname = urlMatch.Result("${hostname}");
                int    port     = RTSP_PORT;

                if (hostname.Contains(':'))
                {
                    port     = SIPSorcery.Sys.IPSocket.ParsePortFromSocket(hostname);
                    hostname = SIPSorcery.Sys.IPSocket.ParseHostFromSocket(hostname);
                }

                logger.LogDebug("RTSP client connecting to " + hostname + ", port " + port + ".");

                _rtspConnection = new TcpClient(hostname, port);
                _rtspStream     = _rtspConnection.GetStream();

                _rtpSession = new RTPSession();
                _rtpSession.RTPPayloadHeaderLength = _rtpPayloadHeaderLength;
                _rtpSession.ReservePorts();
                _rtpSession.OnRTPQueueFull += RTPQueueFull;

                RTSPRequest rtspRequest = new RTSPRequest(RTSPMethodsEnum.SETUP, url);
                RTSPHeader  rtspHeader  = new RTSPHeader(_cseq++, null);
                rtspHeader.Transport = new RTSPTransportHeader()
                {
                    ClientRTPPortRange = _rtspSession.RTPPort + "-" + _rtspSession.ControlPort
                };
                rtspRequest.Header = rtspHeader;
                string rtspReqStr = rtspRequest.ToString();

                RTSPMessage rtspMessage = null;

                System.Diagnostics.Debug.WriteLine(rtspReqStr);

                byte[] rtspRequestBuffer = Encoding.UTF8.GetBytes(rtspReqStr);
                _rtspStream.Write(rtspRequestBuffer, 0, rtspRequestBuffer.Length);

                byte[] buffer    = new byte[2048];
                int    bytesRead = _rtspStream.Read(buffer, 0, 2048);

                if (bytesRead > 0)
                {
                    System.Diagnostics.Debug.WriteLine(Encoding.UTF8.GetString(buffer, 0, bytesRead));

                    rtspMessage = RTSPMessage.ParseRTSPMessage(buffer, null, null);

                    if (rtspMessage.RTSPMessageType == RTSPMessageTypesEnum.Response)
                    {
                        var setupResponse = RTSPResponse.ParseRTSPResponse(rtspMessage);

                        if (setupResponse.Status == RTSPResponseStatusCodesEnum.OK)
                        {
                            _rtspSession.SessionID      = setupResponse.Header.Session;
                            _rtspSession.RemoteEndPoint = new IPEndPoint((_rtspConnection.Client.RemoteEndPoint as IPEndPoint).Address, setupResponse.Header.Transport.GetServerRTPPort());
                            _rtspSession.Start();

                            logger.LogDebug("RTSP Response received to SETUP: " + setupResponse.Status + ", session ID " + _rtspSession.SessionID + ", server RTP endpoint " + _rtspSession.RemoteEndPoint + ".");

                            if (OnSetupSuccess != null)
                            {
                                OnSetupSuccess(this);
                            }
                        }
                        else
                        {
                            logger.LogWarning("RTSP Response received to SETUP: " + setupResponse.Status + ".");
                            throw new ApplicationException("An error response of " + setupResponse.Status + " was received for an RTSP setup request.");
                        }
                    }
                }
                else
                {
                    throw new ApplicationException("Zero bytes were read from the RTSP client socket in response to a SETUP request.");
                }
            }
        }