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 是一个控制流媒体的网络控制协议.
- 传输流数据不是它的任务, RTSP发起/终结流媒体, 它的实质就是一个远程遥控器.
- RTSP流的有状态的, 比如处于Pause状态还是Play状态, 状态的改变都是通过RTSP协议来完成的.
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服务器支持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)
客户端给服务器发送DESCRIBE查询某个文件的SDP信息时,服务器会给客户端返回该媒体文件所包含的多个媒体流信息, 并为每个媒体流分配一个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指定要为那个Track发送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.
- 每个流
PLAY
之前必须执行SETUP
操作. -
发送SETUP申请时, 客户端会指定两个端口:
一个端口接收RTP
数据;
另一个端口接收RTCP
数据(meta information).
RTP is originated and received on even port numbers, and the associated RTCP communication uses the next higher odd port number.
- 通常偶数端口用来接收RTP数据,
- 相邻的右侧奇数端口用于接收RTCP数据.
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数据
50503: 接收RTCP数据
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分别执行了一次SETUP操作, 交互双方确立了传输数据要用的端口等.
- 客户端发送两个SETUP请求,分别用于建立音频和视频的RTP接收。
- SETUP时会返回一个Session字段, 用于唯一标示这个Session.
Session: 1071388409;
后续PLAY & TEARDOWN & PAUSE等操作的时候,请求报文中都要带有该字段.
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.
- 客户端发送播放请求.
- 可指定播放的范围, 类似
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
客户端发起关闭请求.
References:
https://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol
http://www.3cx.com/pbx/rtcp/
http://blog.csdn.net/ithzhang/article/details/38613359