Esempio n. 1
0
 public void Pause()
 {
     if (rtsp_client != null)
     {
         // Send PAUSE
         Rtsp.Messages.RtspRequest pause_message = new Rtsp.Messages.RtspRequestPause();
         pause_message.RtspUri = new Uri(url);
         pause_message.Session = session;
         if (auth_type != null)
         {
             AddAuthorization(pause_message, username, password, auth_type, realm, nonce, url);
         }
         rtsp_client.SendMessage(pause_message);
     }
 }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
 public void Pause()
 {
     if (rtsp_client != null)
     {
         Rtsp.Messages.RtspRequest pause_message = new Rtsp.Messages.RtspRequestPause();
         pause_message.RtspUri = new Uri(url);
         pause_message.Session = session;
         rtsp_client.SendMessage(pause_message);
     }
 }
Esempio n. 5
0
        public void Pause()
        {
            RtspRequest pauseMessage = new RtspRequestPause
            {
                RtspUri = new Uri(url),
                Session = session
            };

            rtspListener.SendMessage(pauseMessage);
        }
Esempio n. 6
0
        public void Stop()
        {
            Rtsp.Messages.RtspRequest teardown_message = new Rtsp.Messages.RtspRequestTeardown();
            teardown_message.RtspUri = new Uri(url);
            teardown_message.Session = session;
            rtsp_client.SendMessage(teardown_message);

            // clear up any UDP sockets
            if (udp_pair != null)
            {
                udp_pair.Stop();
            }

            // Stop the keepalive timer
            if (keepalive_timer != null)
            {
                keepalive_timer.Stop();
            }

            // Drop the RTSP session
            rtsp_client.Stop();
        }
Esempio n. 7
0
        public void Stop()
        {
            IsStarted = false;

            rtspListener?.SendMessage(new RtspRequestTeardown
            {
                RtspUri = new Uri(rtspUrl),
                Session = rtspSession
            });

            udpSocketPair?.Stop(); // clear up any UDP sockets
            timer?.Dispose();      // Stop the keepalive timer
            rtspListener?.Stop();  // Drop the RTSP session
        }
Esempio n. 8
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");
            }
        }
Esempio n. 9
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);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        private void Rtsp_client_MessageReceived(object sender, Rtsp.RtspChunkEventArgs e)
        {
            Console.WriteLine("Message Received " + e.ToString());

            Rtsp.Messages.RtspResponse message = e.Message as Rtsp.Messages.RtspResponse;

            Console.WriteLine("Received " + message.OriginalRequest.ToString());

            if (message.OriginalRequest != null && message.OriginalRequest is Rtsp.Messages.RtspRequestOptions)
            {
                // send the Describe
                Rtsp.Messages.RtspRequest describe_message = new Rtsp.Messages.RtspRequestDescribe();
                describe_message.RtspUri = new Uri(url);
                rtsp_client.SendMessage(describe_message);
            }

            if (message.OriginalRequest != null && message.OriginalRequest is Rtsp.Messages.RtspRequestDescribe)
            {
                // Got a reply for DESCRIBE
                // Examine the SDP

                Console.Write(System.Text.Encoding.UTF8.GetString(message.Data));

                Rtsp.Sdp.SdpFile sdp_data;
                using (StreamReader sdp_stream = new StreamReader(new MemoryStream(message.Data)))
                {
                    sdp_data = Rtsp.Sdp.SdpFile.Read(sdp_stream);
                }


                // Process each 'Media' Attribute in the SDP.
                // If the attribute is for Video, then carry out a SETUP and a PLAY

                for (int x = 0; x < sdp_data.Medias.Count; x++)
                {
                    if (sdp_data.Medias[x].GetMediaType() == Rtsp.Sdp.Media.MediaType.video)
                    {
                        // seach the atributes for control, fmtp and rtpmap
                        String control = ""; // the "track" or "stream id"
                        String fmtp    = ""; // holds SPS and PPS
                        String rtpmap  = ""; // holds Payload format, eg 96 often used with H264 as first dynamic payload value
                        foreach (Rtsp.Sdp.Attribut attrib in sdp_data.Medias[x].Attributs)
                        {
                            if (attrib.Key.Equals("control"))
                            {
                                control = attrib.Value;
                            }
                            if (attrib.Key.Equals("fmtp"))
                            {
                                fmtp = attrib.Value;
                            }
                            if (attrib.Key.Equals("rtpmap"))
                            {
                                rtpmap = attrib.Value;
                            }
                        }

                        String[] split_rtpmap = rtpmap.Split(' ');
                        video_payload = 0;
                        bool result = Int32.TryParse(split_rtpmap[0], out video_payload);


                        // Transport: RTP/AVP;unicast;client_port=8000-8001
                        // Transport: RTP/AVP/TCP;interleaved=0-1

                        Rtsp.Messages.RtspRequest setup_message = new Rtsp.Messages.RtspRequestSetup();
                        setup_message.RtspUri = new Uri(url + "/" + control);
                        setup_message.AddHeader("Transport: RTP/AVP/TCP;interleaved=0");
                        rtsp_client.SendMessage(setup_message);
                    }
                }
            }

            if (message.OriginalRequest != null && message.OriginalRequest is Rtsp.Messages.RtspRequestSetup)
            {
                // Got Reply to SETUP
                Console.WriteLine("Got reply from Setup. Session is " + message.Session);

                String session = message.Session; // Session value used with Play, Pause, Teardown

                Rtsp.Messages.RtspRequest play_message = new Rtsp.Messages.RtspRequestPlay();
                play_message.RtspUri = new Uri(url);
                play_message.Session = session;
//                play_message.Timeout = 65;
                rtsp_client.SendMessage(play_message);
            }

            if (message.OriginalRequest != null && message.OriginalRequest is Rtsp.Messages.RtspRequestPlay)
            {
                // Got Reply to PLAU
                Console.WriteLine("Got reply from Play  " + message.Command);
            }
        }
Esempio n. 12
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);
        }
Esempio n. 13
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);
        }