Exemplo n.º 1
0
        int video_payload = 0;                   // usuallly 96 for H264 video which is the first dynamic payload type

        // Constructor
        public RTSPClient(String url)
        {
            this.url = url;

            // Use URI to extract hostname and port
            Uri uri = new Uri(url);

            // Connect to a RTSP Server
            tcp_socket = new Rtsp.RtspTcpTransport(uri.Host, (uri.IsDefaultPort?554:uri.Port));

            if (tcp_socket.Connected == false)
            {
                Console.WriteLine("Error - did not connect");
                return;
            }

            // Connect a RTSP Listener to the TCP Socket (or other Stream) to send messages and listen for replies
            rtsp_client = new Rtsp.RtspListener(tcp_socket);

            rtsp_client.MessageReceived += Rtsp_client_MessageReceived;
            rtsp_client.DataReceived    += Rtsp_client_DataReceived;

            rtsp_client.Start(); // start reading messages from the server

            // Send OPTIONS
            // In the Received Message handler we will send DESCRIBE, SETUP and PLAY
            Rtsp.Messages.RtspRequest options_message = new Rtsp.Messages.RtspRequestOptions();
            options_message.RtspUri = new Uri(url);
            rtsp_client.SendMessage(options_message);
        }
Exemplo n.º 2
0
        int video_payload = 0; // usuallly 96 for H264 video which is the first dynamic payload type

        #endregion Fields

        #region Constructors

        // Constructor
        public RTSPClient(String url)
        {
            this.url = url;

            // Use URI to extract hostname and port
            Uri uri = new Uri(url);

            // Connect to a RTSP Server
            tcp_socket = new Rtsp.RtspTcpTransport(uri.Host, (uri.IsDefaultPort?554:uri.Port));

            if (tcp_socket.Connected == false)
            {
                Console.WriteLine("Error - did not connect");
                return;
            }

            // Connect a RTSP Listener to the TCP Socket (or other Stream) to send messages and listen for replies
            rtsp_client = new Rtsp.RtspListener(tcp_socket);

            rtsp_client.MessageReceived += Rtsp_client_MessageReceived;
            rtsp_client.DataReceived += Rtsp_client_DataReceived;

            rtsp_client.Start(); // start reading messages from the server

            // Send OPTIONS
            // In the Received Message handler we will send DESCRIBE, SETUP and PLAY
            Rtsp.Messages.RtspRequest options_message = new Rtsp.Messages.RtspRequestOptions();
            options_message.RtspUri = new Uri(url);
            rtsp_client.SendMessage(options_message);
        }
Exemplo n.º 3
0
        public void Start(ClipDefinition clip)
        {
            if (clip == null)
            {
                throw new ArgumentNullException(nameof(clip), "clip cannot be null");
            }

            if (clip.Url.Length < 7)
            {
                throw new ArgumentException("clip url cannot be empty");
            }

            url = clip.Url;

            try
            {
                Uri       uri       = new Uri(url);
                TcpClient tcpClient = new TcpClient();
                var       task      = Task.Run(async() => { await tcpClient.ConnectAsync(uri.Host, uri.Port > 0 ? uri.Port : 554); });
                task.Wait();

                rtspSocket = new Rtsp.RtspTcpTransport(tcpClient);
                if (rtspSocket.Connected == false)
                {
                    Logger.Error("Did not connect");
                    return;
                }

                // Connect a RTSP Listener to the RTSP Socket (or other Stream) to send RTSP messages and listen for RTSP replies
                rtspListener = new Rtsp.RtspListener(rtspSocket);

                rtspListener.MessageReceived += RtspMessageReceived;
                rtspListener.DataReceived    += RtpDataReceived;

                rtspListener.Start();

                RtspRequest optionsMessage = new RtspRequestOptions
                {
                    RtspUri = new Uri(url)
                };

                rtspListener.SendMessage(optionsMessage);
            }
            catch (Exception e)
            {
                Logger.Error(e, "Did not connect");
            }
        }
Exemplo n.º 4
0
        private async Task ExecuteStart(ClipDefinition clip, CancellationToken ctx)
        {
            if (clip == null)
            {
                throw new ArgumentNullException(nameof(clip), "Clip cannot be null.");
            }

            if (clip.Url.Length < 7)
            {
                throw new ArgumentException("Clip URL cannot be empty.");
            }

            rtspUrl = clip.Url;
            TcpClient tcpClient = new TcpClient();

            tcpClient.SendTimeout    = 10000;
            tcpClient.ReceiveTimeout = 10000;
            try
            {
                Uri uri = new Uri(rtspUrl);
                await tcpClient.ConnectAsync(uri.Host, uri.Port > 0?uri.Port : 554).WaitAsync(ctx);
            }
            catch (Exception e)
            {
                Logger.Info(e.ToString());
                tcpClient.Dispose();
                throw;
            }

            rtspSocket = new Rtsp.RtspTcpTransport(tcpClient);
            if (rtspSocket.Connected == false)
            {
                throw new Exception("RTSP server not available at this time.");
            }

            rtspListener = new Rtsp.RtspListener(rtspSocket, rtspErrorSubject);
            rtspListener.MessageReceived += RtspMessageReceived;
            rtspListener.DataReceived    += RtpDataReceived;
            rtspListener.AutoReconnect    = false;
            rtspListener.Start();

            RtspRequest optionsMessage = new RtspRequestOptions
            {
                RtspUri = new Uri(rtspUrl)
            };

            rtspListener.SendMessage(optionsMessage);
        }
Exemplo n.º 5
0
        public void Connect(String url, RTP_TRANSPORT rtp_transport)
        {
            Rtsp.RtspUtils.RegisterUri();

            Console.WriteLine("Connecting to " + url);
            this.url = url;

            // Use URI to extract username and password
            // and to make a new URL without the username and password
            try {
                Uri uri = new Uri(this.url);
                hostname = uri.Host;
                port     = uri.Port;

                if (uri.UserInfo.Length > 0)
                {
                    username = uri.UserInfo.Split(new char[] { ':' })[0];
                    password = uri.UserInfo.Split(new char[] { ':' })[1];
                    this.url = uri.GetComponents((UriComponents.AbsoluteUri & ~UriComponents.UserInfo),
                                                 UriFormat.UriEscaped);
                }
            } catch {
                username = null;
                password = null;
            }

            // Connect to a RTSP Server. The RTSP session is a TCP connection
            rtsp_socket_status = RTSP_STATUS.Connecting;
            try
            {
                rtsp_socket = new Rtsp.RtspTcpTransport(hostname, port);
            }
            catch
            {
                rtsp_socket_status = RTSP_STATUS.ConnectFailed;
                Console.WriteLine("Error - did not connect");
                return;
            }

            if (rtsp_socket.Connected == false)
            {
                rtsp_socket_status = RTSP_STATUS.ConnectFailed;
                Console.WriteLine("Error - did not connect");
                return;
            }

            rtsp_socket_status = RTSP_STATUS.Connected;

            // Connect a RTSP Listener to the RTSP Socket (or other Stream) to send RTSP messages and listen for RTSP replies
            rtsp_client = new Rtsp.RtspListener(rtsp_socket);

            rtsp_client.AutoReconnect = false;

            rtsp_client.MessageReceived += Rtsp_MessageReceived;
            rtsp_client.DataReceived    += Rtp_DataReceived;

            rtsp_client.Start(); // start listening for messages from the server (messages fire the MessageReceived event)


            // Check the RTP Transport
            // If the RTP transport is TCP then we interleave the RTP packets in the RTSP stream
            // If the RTP transport is UDP, we initialise two UDP sockets (one for video, one for RTCP status messages)
            // If the RTP transport is MULTICAST, we have to wait for the SETUP message to get the Multicast Address from the RTSP server
            this.rtp_transport = rtp_transport;
            if (rtp_transport == RTP_TRANSPORT.UDP)
            {
                video_udp_pair = new Rtsp.UDPSocket(50000, 50020); // give a range of 10 pairs (20 addresses) to try incase some address are in use
                video_udp_pair.DataReceived += Rtp_DataReceived;
                video_udp_pair.Start();                            // start listening for data on the UDP ports
                audio_udp_pair = new Rtsp.UDPSocket(50000, 50020); // give a range of 10 pairs (20 addresses) to try incase some address are in use
                audio_udp_pair.DataReceived += Rtp_DataReceived;
                audio_udp_pair.Start();                            // start listening for data on the UDP ports
            }
            if (rtp_transport == RTP_TRANSPORT.TCP)
            {
                // Nothing to do. Data will arrive in the RTSP Listener
            }
            if (rtp_transport == RTP_TRANSPORT.MULTICAST)
            {
                // Nothing to do. Will open Multicast UDP sockets after the SETUP command
            }


            // Send OPTIONS
            // In the Received Message handler we will send DESCRIBE, SETUP and PLAY
            Rtsp.Messages.RtspRequest options_message = new Rtsp.Messages.RtspRequestOptions();
            options_message.RtspUri = new Uri(this.url);
            rtsp_client.SendMessage(options_message);
        }
Exemplo n.º 6
0
        // Constructor
        public RTSPClient(String url, RTP_TRANSPORT rtp_transport)
        {
            Rtsp.RtspUtils.RegisterUri();

            if (fs == null)
            {
                String filename = "rtsp_capture_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".264";
                fs = new FileStream(filename, FileMode.Create);

                String filename2 = "rtsp_capture_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".raw";
                fs2 = new StreamWriter(filename2);
            }


            Console.WriteLine("Connecting to " + url);
            this.url = url;

            // Use URI to extract hostname and port
            Uri uri = new Uri(url);

            // Connect to a RTSP Server. The RTSP session is a TCP connection
            try
            {
                rtsp_socket = new Rtsp.RtspTcpTransport(uri.Host, uri.Port);
            }
            catch
            {
                Console.WriteLine("Error - did not connect");
                return;
            }

            if (rtsp_socket.Connected == false)
            {
                Console.WriteLine("Error - did not connect");
                return;
            }

            // Connect a RTSP Listener to the RTSP Socket (or other Stream) to send RTSP messages and listen for RTSP replies
            rtsp_client = new Rtsp.RtspListener(rtsp_socket);

            rtsp_client.MessageReceived += Rtsp_MessageReceived;
            rtsp_client.DataReceived    += Rtp_DataReceived;

            rtsp_client.Start(); // start listening for messages from the server (messages fire the MessageReceived event)


            // Check the RTP Transport
            // If the RTP transport is TCP then we interleave the RTP packets in the RTSP stream
            // If the RTP transport is UDP, we initialise two UDP sockets (one for video, one for RTCP status messages)
            // If the RTP transport is MULTICAST, we have to wait for the SETUP message to get the Multicast Address from the RTSP server
            this.rtp_transport = rtp_transport;
            if (rtp_transport == RTP_TRANSPORT.UDP)
            {
                udp_pair = new UDPSocket(50000, 50020); // give a range of 10 pairs (20 addresses) to try incase some address are in use
                udp_pair.DataReceived += Rtp_DataReceived;
                udp_pair.Start();                       // start listening for data on the UDP ports
            }
            if (rtp_transport == RTP_TRANSPORT.TCP)
            {
                // Nothing to do. Data will arrive in the RTSP Listener
            }
            if (rtp_transport == RTP_TRANSPORT.MULTICAST)
            {
                // Nothing to do. Will open Multicast UDP sockets after the SETUP command
            }


            // Send OPTIONS
            // In the Received Message handler we will send DESCRIBE, SETUP and PLAY
            Rtsp.Messages.RtspRequest options_message = new Rtsp.Messages.RtspRequestOptions();
            options_message.RtspUri = new Uri(url);
            rtsp_client.SendMessage(options_message);
        }
Exemplo n.º 7
0
        byte[] video_sps = null; // SPS from SDP prop-parameter-set

        #endregion Fields

        #region Constructors

        // Constructor
        public RTSPClient(String url, RTP_TRANSPORT rtp_transport)
        {
            Rtsp.RtspUtils.RegisterUri();

            if (fs == null)
            {
                String filename = "rtsp_capture_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".264";
                fs = new FileStream(filename, FileMode.Create);

                String filename2 = "rtsp_capture_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".raw";
                fs2 = new StreamWriter(filename2);
            }

            Console.WriteLine("Connecting to " + url);
            this.url = url;

            // Use URI to extract hostname and port
            Uri uri = new Uri(url);

            // Connect to a RTSP Server. The RTSP session is a TCP connection
            try
            {
                rtsp_socket = new Rtsp.RtspTcpTransport(uri.Host, uri.Port);
            }
            catch
            {
                Console.WriteLine("Error - did not connect");
                return;
            }

            if (rtsp_socket.Connected == false)
            {
                Console.WriteLine("Error - did not connect");
                return;
            }

            // Connect a RTSP Listener to the RTSP Socket (or other Stream) to send RTSP messages and listen for RTSP replies
            rtsp_client = new Rtsp.RtspListener(rtsp_socket);

            rtsp_client.MessageReceived += Rtsp_MessageReceived;
            rtsp_client.DataReceived += Rtp_DataReceived;

            rtsp_client.Start(); // start listening for messages from the server (messages fire the MessageReceived event)

            // Check the RTP Transport
            // If the RTP transport is TCP then we interleave the RTP packets in the RTSP stream
            // If the RTP transport is UDP, we initialise two UDP sockets (one for video, one for RTCP status messages)
            // If the RTP transport is MULTICAST, we have to wait for the SETUP message to get the Multicast Address from the RTSP server
            this.rtp_transport = rtp_transport;
            if (rtp_transport == RTP_TRANSPORT.UDP)
            {
                udp_pair = new UDPSocket(50000, 50020); // give a range of 10 pairs (20 addresses) to try incase some address are in use
                udp_pair.DataReceived += Rtp_DataReceived;
                udp_pair.Start(); // start listening for data on the UDP ports
            }
            if (rtp_transport == RTP_TRANSPORT.TCP)
            {
                // Nothing to do. Data will arrive in the RTSP Listener
            }
            if (rtp_transport == RTP_TRANSPORT.MULTICAST)
            {
                // Nothing to do. Will open Multicast UDP sockets after the SETUP command
            }

            // Send OPTIONS
            // In the Received Message handler we will send DESCRIBE, SETUP and PLAY
            Rtsp.Messages.RtspRequest options_message = new Rtsp.Messages.RtspRequestOptions();
            options_message.RtspUri = new Uri(url);
            rtsp_client.SendMessage(options_message);
        }