ngraziano / SharpRTSP

A RTSP handling library
Other
552 stars 182 forks source link

UDP Support implementation #52

Closed REPRESSION closed 5 years ago

REPRESSION commented 5 years ago

Hi,

I am using SharpRTSP to create RTSP streams (using the example RtspCameraExample). Sadly I need UDP support too, and I am looking where to add it.

I think what is missing is an implementation of IRTSPTransport, like RTSPTCPTransport but for UDP.

In the file RtspServer.cs, we got this :

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;
        }

    }

This seems to be the code that accepts a newly connected client. From what I understood, it assumes that the client is using TCP before the RTSP message exchange.

Am I on the right way ? Do you have any additional informations that could be useful ?

Thanks

RogerHardiman commented 5 years ago

What we all call RTSP video is actually two different parts. The First Part is called "RTSP". It is always TCP. It is a very high level protocol used by the viewer to request the video and to agree how the video will be streamed. There are only a few high level commands in the protocol, one command is called the DESCRIBE command, one is called SETUP command and one is called PLAY command.

The Second Part is called "RTP". This can be TCP or UDP or Multicast. RTP carries audio or video. The way to send the video (TCP, UDP or Multicast) was negotiated between Viewer and Server at the RTSP level in the SETUP command. The UDP Ports that are going to be used were also agreed at the RTSP level.

So the code snippet above is part of the RTSP level. That will always be TCP.

You want to be looking at the part where the 'RTP Transports' are negotiated in the SETUP command. Look for the comment "Handle SETUP message"

In fact if you search for UDP in the source code it will take you to the places where I needed to add UDP support.

RogerHardiman commented 5 years ago

I have added some initial UDP support to RtspCameraExample It will stream UDP and provided the client/viewer sends the RTSP TEARDOWN message it stops streaming too.

But if the client does not send TEARDOWN (or TEARDOWN is not received) it will keep sending UDP packets forever!

RTSP Servers normally check that the RTSP session is still open (clients send OPTIONS or GET_PARAMETERS) every 30 seconds. Servers normally send RTCP messages and check for replies to confirm the viewer is still there.

You need to add these to SharpRTSP

RogerHardiman commented 5 years ago

I've now added in RTSP Keepalives - so as long as the client sends OPTIONS or GET_PARAMETERS every 60 seconds (I do a test every 70 seconds) then the UDP stream stays open. Otherwise it is closed as we assume the client is no longer there.

REPRESSION commented 5 years ago

wow didn't expected this kind of help, I just tested it and it works so far. Thanks!

RogerHardiman commented 5 years ago

Glad it worked