Friday, March 11, 2011

RTSP - RTP over TCP

Normally, RTSP provide streaming over UDP. By nature, UDP is a better choice as it provides robust streaming capability for media. However, it is unlikely to use UDP for streaming over the Internet.

Some issues with UDP are

1. RTSP/RTP over UDP requires many UDP ports to be opened (each media stream requires 2 UDP port for data and control).
2. Point 1 is a real problem as routers in the Internet may not open these ports.
3. It is normal for intermediate Internet router to filter and ignore UDP packets.
4. UDP is unreliable. Media packets may be lost when travelling along the Internet

RTSP/RTP over TCP may resolve this issue.

1. RTSP/RTP will communicate via 1 port for command and data. That is the RTSP port.
2. TCP provide reliable streaming
3. It is more likely that the intermediate Internet router allow these TCP packets to go through.

Using RTSP/RTP over TCP come with a price.

1. It complicated the packetization and depacketization process due to binary interleave.
2. TCP is reliable but have overhead. It may cause delay in real time media.

So, now I will talk about how to setup RTSP/RTP over TCP

RTSP/RTP over TCP

When you use RTSP/RTP over TCP, all command and media data will be sent through the RTSP port, normally, port 554. Also, when using RTSP/RTP over TCP, the data will be sent via binary interleave format.

Below will describe the essential for using RTSP/RTP over TCP

SETUP

To use TCP communication, you need to request TCP connection during RTSP SETUP. You have to sent SETUP command with

Transport: RTP/AVP/TCP;interleaved=0-1
This will tell the server to send media data with TCP and interleave the data in channel 0 and 1. Given in the specification, data channel is even number and control channel is odd (data_ch_num + 1). So, if you data channel is 0, your control channel will be 0 + 1 = 1.

Below is an example of TCP SETUP


RTP Data

After the setup, RTP data will be sent through the TCP socket that is used for RTSP commands. The RTP data will be encapsulate in the following format

| magic number | channel number | embedded data length | data |

magic number - 1 byte value of hex 0x24
channel number - 1 byte value to denote the channel
embedded data length - 2 bytes to denote the embedded data length
data - data packet, ie RTP packet, with the total length of the embedded data length

Below is a full example of the communication exchanged


Also, as RTSP is a application protocol, it has no way to control how TCP timeout the connection. Thus, during the RTSP SETUP, a Session is given to identify the connected stream

Session = "Session" ":" session-id [ ";" "timeout" "=" delta-seconds ]
If Session is given, each subsequence RTSP command must be sent with the session so that the server can identify the stream. Also, please note that timeout is an optional value. The default value for timeout is 60 seconds. So, it is advisable to send a RTSP command to the server every 60 second to keep the TCP connection alive.

Read RFC 2326 Section 10.12 Embedded (Interleaved) Binary Data for more details

15 comments:

  1. Does it still need "keep-alive" in TCP mode?

    I found this:
    http://msdn.microsoft.com/en-us/library/cc245366%28v=prot.10%29.aspx

    However, I cannot find a clear statement in RFC2326.

    ReplyDelete
  2. My experience is Yes.

    All media server/camera I have encounter so far requires me to send a RTSP message to keepalive the RTSP session while getting RTP stream from the same socket.

    ReplyDelete
  3. Hello, in the pic, there is a "$\000", I know the $ is the magic char, but, what does the "\00" mean?

    ReplyDelete
  4. \0 is just a null character in C. 00 is ASCII representation of channel 0.

    So just imagine $\000 is a printf from C++ for 1 byte of magic and 1 byte of channel

    Just remember that you need 1 byte of magic 0x24, and 1 byte of channel number will do.

    ReplyDelete
  5. Thanks for the response, Thompson.

    ReplyDelete
  6. What would be the RTCP metric for RTP over TCP to monitor for bandwidth congestion: delay, or jitter ? And would that monitoring prevent against delay accumulation? Anyone tried to research that?

    ReplyDelete
  7. Normally, it should use the jitter on RTCP report and the server should reduce the sending rate.

    ReplyDelete
  8. You are right that UDP is a better choice as it provides robust streaming capability for media. But I never knew about its issues that you listed above.Thanks.
    pdf digital signature

    ReplyDelete
  9. In fact, RTP/UDP does not really work in the public Internet most of the time. The UDP packets will be filtered by the ISP usually.

    ReplyDelete
  10. Dear Thompson Ng,

    What open source do you use for pack RTP over TCP ?
    ccRTP or ....
    I try to use oRTP, but it seems only support UDP streaming.
    Thanks a lot.

    ReplyDelete
  11. Hi Thompson Ng,
    i am writting an rtsp application in c.After sending PLAY command to server(ipcam) it responding with success code.
    but am not able to receive the data buffers.
    Do i need to create socket again for rtp port. which sent by SETUP command. can you give some info How i can receive data buffer?
    Thanks in advance

    ReplyDelete
  12. For RTSP tunneling, you need to create 2 TCP sockets. One POST socket to send commands and one GET socket to receive data and response. You need to set up these 2 socket with a session ID so that the server can bind these 2 socket together.

    Then, you send your command via POST socket and read your response from GET socket.

    Note, both command response and data are received via GET socket. Thus, you need to check for magic byte 0x24 to identify if you are getting command response or data response

    ReplyDelete
  13. A very useful information to stream RTSP - RTP over TCP.
    Can you tell me for this what changes do we have to make in the live 555 libraries as they also stream RTSP - RTP over UDP
    and you also have the knowledge of live 555 as seen in your article http://thompsonng.blogspot.in/search/label/Live555.

    ReplyDelete
  14. Live555 comes with openRTSP that allow RTSP/RTP over TCP. http://www.live555.com/openRTSP/

    openRTSP will switch to RTSP/RTP over TCP with switch command -t

    I did not really work with it. But my advise is... Put debug point at the main.c command -t potion. Step through the code and you will able to find the relevant code

    ReplyDelete
  15. I am using Live 555 to stream the live video from camera so according to there documentationenter link description here i have implemented the createNewStreamSource() and createNewRTPSink() but the problem now i am facing is what should be the function call.

    ReplyDelete