RTSP
The Real Time Streaming Protocol (RTSP) is a network control protocol designed for use in entertainment and communications systems to controlstreaming media servers.
The protocol is used for establishing and controlling media sessions between end points.
Clients of media servers issue VCR-style commands, such as play, record and pause, to facilitate real-time control of the media streaming from the server to a client (Video On Demand) or from a client to the server (Voice Recording).
The transmission of streaming data itself is not a task of RTSP. Most RTSP servers use the Real-time Transport Protocol (RTP) in conjunction with Real-time Control Protocol (RTCP) for media stream delivery.
- RTSP 是一個(gè)控制流媒體的網(wǎng)絡(luò)控制協(xié)議.
- 傳輸流數(shù)據(jù)不是它的任務(wù), RTSP發(fā)起/終結(jié)流媒體, 它的實(shí)質(zhì)就是一個(gè)遠(yuǎn)程遙控器.
- RTSP流的有狀態(tài)的, 比如處于Pause狀態(tài)還是Play狀態(tài), 狀態(tài)的改變都是通過RTSP協(xié)議來完成的.
RTSP交互過程(live555)

其中SETUP和PLAY是必須的.
OPTIONS
Opening connection to 172.16.41.75, port 554...
...remote connection opened
Sending request: OPTIONS rtsp://admin:admin123@172.16.41.75 RTSP/1.0
CSeq: 2
User-Agent: openRTSP (LIVE555 Streaming Media v2016.05.20)
Received 153 new bytes of response data.
Received a complete OPTIONS response:
RTSP/1.0 200 OK
CSeq: 2
Public: OPTIONS, DESCRIBE, PLAY, PAUSE, SETUP, TEARDOWN, SET_PARAMETER, GET_PARA
METER
Date: Tue, May 31 2016 17:03:40 GMT
可以看出目前RTSP服務(wù)器支持OPTIONS, DESCRIBE, PLAY, PAUSE, SETUP, TEARDOWN, SET_PARAMETER, GET_PARAMETER等指令.
DESCRIBE
A DESCRIBE request includes an RTSP URL (rtsp://...), and the type of reply data that can be handled.
This reply includes the presentation description, typically in Session Description Protocol (SDP) format.
Among other things, the presentation description lists the media streams controlled with the aggregate URL.
In the typical case, there is one media stream each for audio and video.
Sending request: DESCRIBE rtsp://admin:admin123@172.16.41.75 RTSP/1.0
CSeq: 3
User-Agent: openRTSP (LIVE555 Streaming Media v2016.05.20)
Accept: application/sdp
Received 997 new bytes of response data.
Received a complete DESCRIBE response:
RTSP/1.0 200 OK
CSeq: 3
Content-Type: application/sdp
Content-Base: rtsp://admin:admin123@172.16.41.75/
Content-Length: 864
v=0
o=- 1464714338008683 1464714338008683 IN IP4 172.16.41.75
s=Media Presentation
e=NONE
b=AS:5100
t=0 0
a=control:rtsp://admin:admin12345@172.16.41.75/
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
b=AS:5000
a=recvonly
a=x-dimensions:1280,720
a=control:rtsp://admin:admin123@172.16.41.75/trackID=1
a=rtpmap:96 H264/90000
a=fmtp:96 profile-level-id=420029; packetization-mode=1; sprop-parameter-sets=Z0
KAH4uVAKALdCAAAOEAACvyEIA=,aN44gA==
m=audio 0 RTP/AVP 104
c=IN IP4 0.0.0.0
b=AS:50
a=recvonly
a=control:rtsp://admin:admin123@172.16.41.75/trackID=2
a=rtpmap:104 mpeg4-generic/32000/1
a=fmtp:104 profile-level-id=15; streamtype=5; mode=AAC-hbr; config=1288;SizeLeng
th=13; IndexLength=3; IndexDeltaLength=3; Profile=1;
a=Media_header:MEDIAINFO=494D4B48010100000400010001200110007D0000007D00000000000
0000000000000000000000000;
a=appversion:1.0
Opened URL "rtsp://admin:admin123@172.16.41.75", returning a SDP description:
v=0
o=- 1464714338008683 1464714338008683 IN IP4 172.16.41.75
s=Media Presentation
e=NONE
b=AS:5100
t=0 0
a=control:rtsp://admin:admin123@172.16.41.75/
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
b=AS:5000
a=recvonly
a=x-dimensions:1280,720
a=control:rtsp://admin:admin123@172.16.41.75/trackID=1
a=rtpmap:96 H264/90000
a=fmtp:96 profile-level-id=420029; packetization-mode=1; sprop-parameter-sets=Z0
KAH4uVAKALdCAAAOEAACvyEIA=,aN44gA==
m=audio 0 RTP/AVP 104
c=IN IP4 0.0.0.0
b=AS:50
a=recvonly
a=control:rtsp://admin:admin123@172.16.41.75/trackID=2
a=rtpmap:104 mpeg4-generic/32000/1
a=fmtp:104 profile-level-id=15; streamtype=5; mode=AAC-hbr; config=1288;SizeLeng
th=13; IndexLength=3; IndexDeltaLength=3; Profile=1;
a=Media_header:MEDIAINFO=494D4B48010100000400010001200110007D0000007D00000000000
0000000000000000000000000;
a=appversion:1.0
Created receiver for "video/H264" subsession (client ports 50502-50503)
Created receiver for "audio/MPEG4-GENERIC" subsession (client ports 50504-50505)
客戶端給服務(wù)器發(fā)送DESCRIBE查詢某個(gè)文件的SDP信息時(shí),服務(wù)器會(huì)給客戶端返回該媒體文件所包含的多個(gè)媒體流信息, 并為每個(gè)媒體流分配一個(gè)TrackID。
如視頻流分配為Track1,音頻流分配為Track2。
a=control:rtsp://admin:admin123@172.16.41.75/trackID=1
a=control:rtsp://admin:admin123@172.16.41.75/trackID=2
此后客戶端必須在URL指定要為那個(gè)Track發(fā)送SETUP命令.
SETUP
A SETUP request specifies how a single media stream must be transported.
This must be done before a PLAY request is sent.
The request contains the media stream URL and a transport specifier.
This specifier typically includes a local port for receiving RTP data (audio or video), and another for RTCP data (meta information).
The server reply usually confirms the chosen parameters, and fills in the missing parts, such as the server's chosen ports.
Each media stream must be configured using SETUP before an aggregate play request may be sent.
- 每個(gè)流
PLAY之前必須執(zhí)行SETUP操作. -
發(fā)送SETUP申請(qǐng)時(shí), 客戶端會(huì)指定兩個(gè)端口:
一個(gè)端口接收RTP數(shù)據(jù);
另一個(gè)端口接收RTCP數(shù)據(jù)(meta information).
RTP is originated and received on even port numbers, and the associated RTCP communication uses the next higher odd port number.

- 通常偶數(shù)端口用來接收RTP數(shù)據(jù),
- 相鄰的右側(cè)奇數(shù)端口用于接收RTCP數(shù)據(jù).
Sending request: SETUP rtsp://admin:admin123@172.16.41.75/trackID=1 RTSP/1.0
CSeq: 4
User-Agent: openRTSP (LIVE555 Streaming Media v2016.05.20)
Transport: RTP/AVP;unicast;client_port=50502-50503
Received 204 new bytes of response data.
Received a complete SETUP response:
RTSP/1.0 200 OK
CSeq: 4
Session: 1071388409;timeout=60
Transport: RTP/AVP;unicast;client_port=50502-50503;server_port=8220-8221;ssrc=38
0d5699;mode="play"
Date: Tue, May 31 2016 17:05:44 GMT
Setup "video/H264" subsession (client ports 50502-50503)
client_port=50502-50503中
50502: 接收RTP數(shù)據(jù)
50503: 接收RTCP數(shù)據(jù)
Sending request: SETUP rtsp://admin:admin123@172.16.41.75/trackID=2 RTSP/1.0
CSeq: 5
User-Agent: openRTSP (LIVE555 Streaming Media v2016.05.20)
Transport: RTP/AVP;unicast;client_port=50504-50505
Session: 1071388409
Received 204 new bytes of response data.
Received a complete SETUP response:
RTSP/1.0 200 OK
CSeq: 5
Session: 1071388409;timeout=60
Transport: RTP/AVP;unicast;client_port=50504-50505;server_port=8222-8223;ssrc=17
bc6ecb;mode="play"
Date: Tue, May 31 2016 17:06:27 GMT
Setup "audio/MPEG4-GENERIC" subsession (client ports 50504-50505)
- 可以看出Video和Audio分別執(zhí)行了一次SETUP操作, 交互雙方確立了傳輸數(shù)據(jù)要用的端口等.
- 客戶端發(fā)送兩個(gè)SETUP請(qǐng)求,分別用于建立音頻和視頻的RTP接收。
- SETUP時(shí)會(huì)返回一個(gè)Session字段, 用于唯一標(biāo)示這個(gè)Session.
Session: 1071388409;
后續(xù)PLAY & TEARDOWN & PAUSE等操作的時(shí)候,請(qǐng)求報(bào)文中都要帶有該字段.
PLAY
A PLAY request will cause one or all media streams to be played.
Play requests can be stacked by sending multiple PLAY requests.
The URL may be the aggregate(集合) URL (to play all media streams), or a single media stream URL (to play only that stream).
A range can be specified.
If no range is specified, the stream is played from the beginning and plays to the end, or, if the stream is paused, it is resumed at the point it was paused.
- 客戶端發(fā)送播放請(qǐng)求.
- 可指定播放的范圍, 類似
HTTP的Range:.
C->S: PLAY rtsp://example.com/media.mp4 RTSP/1.0
CSeq: 4
Range: npt=5-20
Session: 12345678
S->C: RTSP/1.0 200 OK
CSeq: 4
Session: 12345678
RTP-Info: url=rtsp://example.com/media.mp4/streamid=0;seq=9810092;rtptime=3450012
TEARDOWN
A TEARDOWN request is used to terminate the session. It stops all media streams and frees all session related data on the server.
C->S: TEARDOWN rtsp://example.com/media.mp4 RTSP/1.0
CSeq: 8
Session: 12345678
S->C: RTSP/1.0 200 OK
CSeq: 8
客戶端發(fā)起關(guān)閉請(qǐng)求.
References:
https://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol
http://www.3cx.com/pbx/rtcp/
http://blog.csdn.net/ithzhang/article/details/38613359