Search code examples
c++socketsstreamudprtsp

Check if RTSP Stream has finished


first I'm gonna try to explain what exactly I want to do:

I have an IP Camera which has a circular buffer from which I want to read the data. The communication between camera and my application takes place over RTSP, which means I have a TCP connection to the camera. The camera itself is capable of streaming the data over RTP.

Now the important part is, that when the camera starts streaming it's stuff, my application shall NOT read back the data. There is some kind of passive datalogger in the network which is responsible for recording the data.

The problem is, that as long as the camera is streaming, the Socket from my application needs to be opened, otherwise the camera will stop streaming.

So lets see what I have so far:

I open a socket, which connects to the camera and then sends the needed RTSP commands OPTIONS, DESCRIBE, SETUP and then PLAY. After this my socket remains open and the camera streams.

Now here comes the difficult part:

As I already said, I can not parse all the UDP data. But how can I then determine when the stream has ended? On wireshark I see that when the stream finishes I get a RTCP message which says Sender Report Goodbye.

This is the point where I would want to close my socket, but how can I determine that the camera has finished without parsing any UDP data?


Solution

  • Okay it is possible to determine the end of the Stream.

    There are two lines of communication after the Stream has started. There is the RTP Stream, which contains all the raw data. Besides that there is some RTCP Data, which can be used to control the stream.

    The important part is, that the data is sent on different Ports. These ports are defined in the RTSP SETUP Request. Now while the camera is streaming, the data incoming on the RTP can just be ignored as intended, while you can open a socket to listen on the RTCP port. There you get some Sender Reports until the stream is finished ( with a last Sender Report Goodbye ). If you read this message you know you can close, since the stream has finished.

    Note: On some devices you may need to send some Receiver Reports as answer to theSender Reports otherwise the stream may stop, since the device thinks the client timed out.