public void ReceiveMessageInterrupt() { string message = string.Empty; message += "PLAY rtsp://audio.example.com/audio RTSP/1."; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler <RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler <RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); // No exception should be generate. stream.Close(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(0, _receivedMessage.Count); Assert.AreEqual(0, _receivedData.Count); }
/// <summary> /// Gets the RTSP listener for destination. /// </summary> /// <param name="destinationUri">The destination URI.</param> /// <returns>An RTSP listener</returns> /// <remarks> /// This method try to get one of openned TCP listener and /// if it does not find it, it create it. /// </remarks> private RtspListener GetRtspListenerForDestination(Uri destinationUri) { Contract.Requires(destinationUri != null); RtspListener destination; string destinationName = destinationUri.Authority; if (_serverListener.ContainsKey(destinationName)) { destination = _serverListener[destinationName]; } else { destination = new RtspListener( new RtspTcpTransport(destinationUri.Host, destinationUri.Port) ); // un peu pourri mais pas d'autre idée... // pour avoir vraiment des clef avec IP.... if (_serverListener.ContainsKey(destination.RemoteAdress)) { destination = _serverListener[destination.RemoteAdress]; } else { AddListener(destination); destination.Start(); } } return(destination); }
/// <summary> /// Accepts the connection. /// </summary> private void AcceptConnection() { try { while (!_Stopping.WaitOne(0)) { // Wait for an incoming TCP Connection TcpClient oneClient = _RTSPServerListener.AcceptTcpClient(); Console.WriteLine("Connection from " + oneClient.Client.RemoteEndPoint.ToString()); // Hand the incoming TCP connection over to the RTSP classes var rtsp_socket = new RtspTcpTransport(oneClient); RtspListener newListener = new RtspListener(rtsp_socket); newListener.MessageReceived += new EventHandler <RtspChunkEventArgs>(async(s, e) => await RTSP_Message_ReceivedAsync(s, e)); //RTSPDispatcher.Instance.AddListener(newListener); // Add the RtspListener to the RTSPConnections List newListener.Start(); } } catch (SocketException error) { // _logger.Warn("Got an error listening, I have to handle the stopping which also throw an error", error); } catch (Exception error) { // _logger.Error("Got an error listening...", error); throw; } }
/// <summary> /// Accepts the connection. /// </summary> private void AcceptConnection() { try { while (!_Stopping.WaitOne(0)) { TcpClient oneClient = _RTSPServerListener.AcceptTcpClient(); Console.WriteLine("Connection from " + oneClient.Client.RemoteEndPoint.ToString()); var rtsp_socket = new RtspTcpTransport(oneClient); RtspListener newListener = new RtspListener(rtsp_socket); newListener.MessageReceived += RTSP_Message_Received; //RTSPDispatcher.Instance.AddListener(newListener); newListener.Start(); } } catch (SocketException error) { // _logger.Warn("Got an error listening, I have to handle the stopping which also throw an error", error); } catch (Exception error) { // _logger.Error("Got an error listening...", error); throw; } }
public void Connect() { _client.Start(); // Send first setup messge: OPTIONS RtspRequest optionsMessage = new RtspRequestOptions(); optionsMessage.RtspUri = new Uri(_url); _client.SendMessage(optionsMessage); }
// /// <summary> // /// Starts the listen. // /// </summary> // public async Task StartListen() // { // _RTSPServerListener.Start(); // // _Stopping = new ManualResetEvent(false); // _ListenTread = new Thread(new ThreadStart(AcceptConnection)); // _ListenTread.Start(); // // /* // // Initialise the H264 encoder // h264_encoder = new SimpleH264Encoder(h264_width, h264_height, h264_fps); // //h264_encoder = new TinyH264Encoder(); // hard coded to 192x128 // // // Start the VideoSource // video_source = new TestCard(h264_width, h264_height, h264_fps); // video_source.ReceivedYUVFrame += video_source_ReceivedYUVFrame; // */ // } public async Task ListenAsync() { rtspServerListener.Start(); try { while (!stopping) { // Wait for an incoming TCP Connection TcpClient oneClient = await rtspServerListener.AcceptTcpClientAsync(); Console.WriteLine("Connection from " + oneClient.Client.RemoteEndPoint.ToString()); // Hand the incoming TCP connection over to the RTSP classes var rtsp_socket = new RtspTcpTransport(oneClient); RtspListener newListener = new RtspListener(rtsp_socket); newListener.MessageReceived += RTSP_Message_Received; //RTSPDispatcher.Instance.AddListener(newListener); // Add the RtspListener to the RTSPConnections List lock (rtsp_list) { RTSPConnection new_connection = new RTSPConnection(); new_connection.listener = newListener; new_connection.client_hostname = newListener.RemoteAdress.Split(':')[0]; new_connection.ssrc = global_ssrc; new_connection.time_since_last_rtsp_keepalive = DateTime.UtcNow; new_connection.video_time_since_last_rtcp_keepalive = DateTime.UtcNow; rtsp_list.Add(new_connection); } newListener.Start(); await UpdateClients(); } } catch (SocketException error) { // _logger.Warn("Got an error listening, I have to handle the stopping which also throw an error", error); } catch (Exception error) { // _logger.Error("Got an error listening...", error); throw; } }
public void ReceiveOptionsMessage() { string message = string.Empty; message += "OPTIONS * RTSP/1.0\n"; message += "CSeq: 1\n"; message += "Require: implicit-play\n"; message += "Proxy-Require: gzipped-messages\n"; message += "\n"; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler <RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler <RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(1, _receivedMessage.Count); RtspChunk theMessage = _receivedMessage[0]; Assert.IsInstanceOf <RtspRequest>(theMessage); Assert.AreEqual(0, theMessage.Data.Length); Assert.AreSame(testedListener, theMessage.SourcePort); RtspRequest theRequest = theMessage as RtspRequest; Assert.AreEqual(RtspRequest.RequestType.OPTIONS, theRequest.RequestTyped); Assert.AreEqual(3, theRequest.Headers.Count); Assert.AreEqual(1, theRequest.CSeq); Assert.Contains("Require", theRequest.Headers.Keys); Assert.Contains("Proxy-Require", theRequest.Headers.Keys); Assert.AreEqual(null, theRequest.RtspUri); Assert.AreEqual(0, _receivedData.Count); }
public RtspClient(Uri uri, Credentials creds = null) { _uri = uri ?? throw new ArgumentNullException("Cannot create RTSP client from null uri"); _cseq = 0; _credentials = creds; _defaultTimeout = TimeSpan.FromSeconds(20); _callbacks = new ConcurrentDictionary <int, AsyncResponse>(); _sources = new ConcurrentDictionary <int, RtpInterleaveMediaSource>(); _connection = new RtspConnection(IPAddress.Parse(uri.Host), uri.Port == -1 ? DEFAULT_RTSP_PORT : uri.Port); _listener = new RtspListener(_connection, OnRtspChunk); _rtpQueue = new BlockingCollection <ByteBuffer>(); LOG.Info($"Created RTSP client for '{_connection.Endpoint}'"); Task.Run(() => ProcessInterleavedData()); _listener.Start(); }
/// <summary> /// Accepts the connection. /// </summary> private void AcceptConnection() { Console.WriteLine($"Now streaming via RTSP on port {portNumber}"); Console.WriteLine($"Connect with your player to rtsp://127.0.0.1:{portNumber}/"); try { while (!_Stopping.WaitOne(0)) { // Wait for an incoming TCP Connection TcpClient oneClient = _RTSPServerListener.AcceptTcpClient(); Console.WriteLine("Connection from " + oneClient.Client.RemoteEndPoint.ToString()); // Hand the incoming TCP connection over to the RTSP classes var rtsp_socket = new RtspTcpTransport(oneClient); RtspListener newListener = new RtspListener(rtsp_socket); newListener.MessageReceived += RTSP_Message_Received; //RTSPDispatcher.Instance.AddListener(newListener); // Add the RtspListener to the RTSPConnections List lock (rtsp_list) { RTSPConnection new_connection = new RTSPConnection(); new_connection.listener = newListener; new_connection.client_hostname = newListener.RemoteAdress.Split(':')[0]; new_connection.ssrc = global_ssrc; new_connection.time_since_last_rtsp_keepalive = DateTime.UtcNow; //new_connection.video_time_since_last_rtcp_keepalive = DateTime.UtcNow; rtsp_list.Add(new_connection); } newListener.Start(); } } catch (Exception error) { if (!_Stopping.WaitOne(0)) { Console.WriteLine("[AcceptConnection] Error: " + error.ToString()); } } }
public void ReceiveResponseMessage() { string message = string.Empty; message += "RTSP/1.0 551 Option not supported\n"; message += "CSeq: 302\n"; message += "Unsupported: funky-feature\n"; message += "\r\n"; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler <RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler <RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(1, _receivedMessage.Count); RtspChunk theMessage = _receivedMessage[0]; Assert.IsInstanceOf <RtspResponse>(theMessage); Assert.AreEqual(0, theMessage.Data.Length); Assert.AreSame(testedListener, theMessage.SourcePort); RtspResponse theResponse = theMessage as RtspResponse; Assert.AreEqual(551, theResponse.ReturnCode); Assert.AreEqual("Option not supported", theResponse.ReturnMessage); Assert.AreEqual(2, theResponse.Headers.Count); Assert.AreEqual(302, theResponse.CSeq); Assert.AreEqual(0, _receivedData.Count); }
public void ReceivePlayMessage() { string message = string.Empty; message += "PLAY rtsp://audio.example.com/audio RTSP/1.0\r\n"; message += "CSeq: 835\r\n"; message += "\r\n"; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler <RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler <RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(1, _receivedMessage.Count); RtspChunk theMessage = _receivedMessage[0]; Assert.IsInstanceOf <RtspRequest>(theMessage); Assert.AreEqual(0, theMessage.Data.Length); Assert.AreSame(testedListener, theMessage.SourcePort); RtspRequest theRequest = theMessage as RtspRequest; Assert.AreEqual(RtspRequest.RequestType.PLAY, theRequest.RequestTyped); Assert.AreEqual(1, theRequest.Headers.Count); Assert.AreEqual(835, theRequest.CSeq); Assert.AreEqual("rtsp://audio.example.com/audio", theRequest.RtspUri.ToString()); Assert.AreEqual(0, _receivedData.Count); }
public void ReceiveData() { Random rnd = new Random(); byte[] data = new byte[0x0234]; rnd.NextBytes(data); byte[] buffer = new byte[data.Length + 4]; buffer[0] = 0x24; // $ buffer[1] = 11; buffer[2] = 0x02; buffer[3] = 0x34; Buffer.BlockCopy(data, 0, buffer, 4, data.Length); MemoryStream stream = new MemoryStream(buffer); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler <RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler <RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(500); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(0, _receivedMessage.Count); Assert.AreEqual(1, _receivedData.Count); Assert.IsInstanceOf <RtspData>(_receivedData[0]); RtspData dataMessage = _receivedData[0] as RtspData; Assert.AreEqual(11, dataMessage.Channel); Assert.AreSame(testedListener, dataMessage.SourcePort); Assert.AreEqual(data, dataMessage.Data); }
private void Accept(object state) { while (!_stop.WaitOne(0)) { try { var client = _listener.AcceptTcpClient(); var conn = new RtspConnection(client); var listener = new RtspListener(conn, OnRtspRequest); LOG.Debug($"Accepted client connection from '{conn.RemoteAddress}'"); listener.Start(); _listeners.Add(conn.RemoteAddress, listener); } catch (Exception e) { LOG.Error(e, $"Caught exception while accepting client connection, message={e.Message}"); } } }
public void ReceiveData() { Random rnd = new Random(); byte[] data = new byte[0x0234]; rnd.NextBytes(data); byte[] buffer = new byte[data.Length + 4]; buffer[0] = 0x24; // $ buffer[1] = 11; buffer[2] = 0x02; buffer[3] = 0x34; Buffer.BlockCopy(data, 0, buffer, 4, data.Length); MemoryStream stream = new MemoryStream(buffer); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler<RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler<RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(500); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(0, _receivedMessage.Count); Assert.AreEqual(1, _receivedData.Count); Assert.IsInstanceOf<RtspData>(_receivedData[0]); RtspData dataMessage = _receivedData[0] as RtspData; Assert.AreEqual(11, dataMessage.Channel); Assert.AreSame(testedListener, dataMessage.SourcePort); Assert.AreEqual(data, dataMessage.Data); }
/// <summary> /// Accepts the connection. /// </summary> private void AcceptConnection() { try { while (!_Stopping.WaitOne(0)) { TcpClient oneClient = _RTSPServerListener.AcceptTcpClient(); RtspListener newListener = new RtspListener( new RtspTcpTransport(oneClient)); RTSPDispatcher.Instance.AddListener(newListener); newListener.Start(); } } catch (SocketException error) { _logger.Warn("Got an error listening, I have to handle the stopping which also throw an error", error); } catch (Exception error) { _logger.Error("Got an error listening...", error); throw; } }
public void ReceiveNoMessage() { string message = string.Empty; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler <RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler <RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); Assert.AreEqual(0, _receivedMessage.Count); Assert.AreEqual(0, _receivedData.Count); }
public void ReceiveNoMessage() { string message = string.Empty; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler<RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler<RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); Assert.AreEqual(0, _receivedMessage.Count); Assert.AreEqual(0, _receivedData.Count); }
public void ReceivePlayMessage() { string message = string.Empty; message += "PLAY rtsp://audio.example.com/audio RTSP/1.0\r\n"; message += "CSeq: 835\r\n"; message += "\r\n"; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler<RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler<RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(1, _receivedMessage.Count); RtspChunk theMessage = _receivedMessage[0]; Assert.IsInstanceOf<RtspRequest>(theMessage); Assert.AreEqual(0, theMessage.Data.Length); Assert.AreSame(testedListener, theMessage.SourcePort); RtspRequest theRequest = theMessage as RtspRequest; Assert.AreEqual(RtspRequest.RequestType.PLAY, theRequest.RequestTyped); Assert.AreEqual(1, theRequest.Headers.Count); Assert.AreEqual(835, theRequest.CSeq); Assert.AreEqual("rtsp://audio.example.com/audio", theRequest.RtspUri.ToString()); Assert.AreEqual(0, _receivedData.Count); }
public void ReceiveResponseMessage() { string message = string.Empty; message += "RTSP/1.0 551 Option not supported\n"; message += "CSeq: 302\n"; message += "Unsupported: funky-feature\n"; message += "\r\n"; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler<RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler<RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(1, _receivedMessage.Count); RtspChunk theMessage = _receivedMessage[0]; Assert.IsInstanceOf<RtspResponse>(theMessage); Assert.AreEqual(0, theMessage.Data.Length); Assert.AreSame(testedListener, theMessage.SourcePort); RtspResponse theResponse = theMessage as RtspResponse; Assert.AreEqual(551, theResponse.ReturnCode); Assert.AreEqual("Option not supported", theResponse.ReturnMessage); Assert.AreEqual(2, theResponse.Headers.Count); Assert.AreEqual(302, theResponse.CSeq); Assert.AreEqual(0, _receivedData.Count); }
public void ReceiveMessageInterrupt() { string message = string.Empty; message += "PLAY rtsp://audio.example.com/audio RTSP/1."; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler<RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler<RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); // No exception should be generate. stream.Close(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(0, _receivedMessage.Count); Assert.AreEqual(0, _receivedData.Count); }
public void ReceiveOptionsMessage() { string message = string.Empty; message += "OPTIONS * RTSP/1.0\n"; message += "CSeq: 1\n"; message += "Require: implicit-play\n"; message += "Proxy-Require: gzipped-messages\n"; message += "\n"; MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(message)); _mockTransport.GetStream().Returns(stream); // Setup test object. RtspListener testedListener = new RtspListener(_mockTransport); testedListener.MessageReceived += new EventHandler<RtspChunkEventArgs>(MessageReceived); testedListener.DataReceived += new EventHandler<RtspChunkEventArgs>(DataReceived); // Run testedListener.Start(); System.Threading.Thread.Sleep(100); testedListener.Stop(); // Check the transport was closed. _mockTransport.Received().Close(); //Check the message recevied Assert.AreEqual(1, _receivedMessage.Count); RtspChunk theMessage = _receivedMessage[0]; Assert.IsInstanceOf<RtspRequest>(theMessage); Assert.AreEqual(0, theMessage.Data.Length); Assert.AreSame(testedListener, theMessage.SourcePort); RtspRequest theRequest = theMessage as RtspRequest; Assert.AreEqual(RtspRequest.RequestType.OPTIONS, theRequest.RequestTyped); Assert.AreEqual(3, theRequest.Headers.Count); Assert.AreEqual(1, theRequest.CSeq); Assert.Contains("Require", theRequest.Headers.Keys); Assert.Contains("Proxy-Require", theRequest.Headers.Keys); Assert.AreEqual(null, theRequest.RtspUri); Assert.AreEqual(0, _receivedData.Count); }