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); }
public volatile RtspStatus CurrentStatus; // Connecting, Connected etc // Constructor public RTSPClient(String url, RTP_TRANSPORT rtp_transport) { Rtsp.RtspUtils.RegisterUri(); Console.WriteLine("Connecting to " + url); this.url = url; // Use URI to extract host, port, username and password Uri uri = new Uri(this.url); if (uri.UserInfo.Length > 0) { try { username = uri.UserInfo.Split(new char[] { ':' })[0]; password = uri.UserInfo.Split(new char[] { ':' })[1]; this.url = uri.GetComponents((UriComponents.AbsoluteUri & ~UriComponents.UserInfo), UriFormat.UriEscaped); uri = new Uri(this.url); } catch { username = null; password = null; } } Rtsp_Client_StatusChanged(this, new RtspStatusEventArgs(RtspStatus.Connecting)); // 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"); Rtsp_Client_StatusChanged(this, new RtspStatusEventArgs(RtspStatus.ConnectFailed)); return; } if (rtsp_socket.Connected == false) { Console.WriteLine("Error - did not connect"); Rtsp_Client_StatusChanged(this, new RtspStatusEventArgs(RtspStatus.ConnectFailed)); return; } Rtsp_Client_StatusChanged(this, new RtspStatusEventArgs(RtspStatus.Connected)); String now = DateTime.Now.ToString("yyyyMMdd_HHmmss"); if (write_log_files == true) { String filename = "rtsp_capture_" + now + ".264"; fs = new FileStream(filename, FileMode.Create); } if (fs2 == null) { String filename2 = "rtsp_capture_" + now + ".raw"; fs2 = new StreamWriter(filename2); } // 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.StatusChanged += Rtsp_Client_StatusChanged; 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(this.url); rtsp_client.SendMessage(options_message); }
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); }