A RTSP handling library
The source of the nuget package SharpRTSP is in branch donetcore
A C# library to build RTSP Clients, RTSP Servers and handle RTP data streams. The library has several examples.
:warning: : This library does not handle the decoding of the video or audio (eg converting H264 into a bitmap). SharpRTSP is limited to the transport layer and generates the raw data that you need to feed into a video decoder or audio decoder. Many people use FFMPEG or use Hardware Accelerated Operating System APIs to do the decoding.
This is a walkthrough of an old version of the RTSP Client Example which highlights the main way to use the library.
STEP 1 - Open TCP Socket connection to the RTSP Server
// Connect to a RTSP Server
tcp_socket = new Rtsp.RtspTcpTransport(host,port);
if (tcp_socket.Connected == false)
{
Console.WriteLine("Error - did not connect");
return;
}This opens a connection for a 'TCP' mode RTSP/RTP session where RTP packets are set in the RTSP socket.
STEP 2 - Create a RTSP Listener and attach it to the RTSP TCP Socket
// Connect a RTSP Listener to the TCP Socket 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 serverThe RTSP Listener class lets you SEND messages to the RTSP Server (see below).
The RTSP Listner class has a worker thread that listens for replies from the RTSP Server.
When replies are received the MessageReceived Event is fired.
When RTP packets are received the DataReceived Event is fired.
STEP 3 - Send Messages to the RTSP Server
The samples below show how to send messages.
Send OPTIONS with this code :
Rtsp.Messages.RtspRequest options_message = new Rtsp. Messages.RtspRequestOptions();
options_message.RtspUri = new Uri(url);
rtsp_client.SendMessage(options_message);Send DESCRIBE with this code :
// send the Describe
Rtsp.Messages.RtspRequest describe_message = new Rtsp.Messages.RtspRequestDescribe();
describe_message.RtspUri = new Uri(url);
rtsp_client.SendMessage(describe_message);
// The reply will include the SDP dataSend SETUP with this code :
// the value of 'control' comes from parsing the SDP for the desired video or audio sub-stream
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);
// The reply will include the SessionSend PLAY with this code :
// the value of 'session' comes from the reply of the SETUP command
Rtsp.Messages.RtspRequest play_message = new Rtsp.Messages.RtspRequestPlay();
play_message.RtspUri = new Uri(url);
play_message.Session = session;
rtsp_client.SendMessage(play_message);STEP 4 - Handle Replies when the MessageReceived event is fired
This example assumes the main program sends an OPTIONS Command.
It looks for a reply from the server for OPTIONS and then sends DESCRIBE.
It looks for a reply from the server for DESCRIBE and then sends SETUP (for the video stream)
It looks for a reply from the server for SETUP and then sends PLAY.
Once PLAY has been sent the video, in the form of RTP packets, will be received.
…No open issues yet, or sync has not completed.