RTSP会话基本流程
RTSP交互流程:
C表示RTSP客户端,S表示RTSP服务端
① C->S: OPTION request //询问S有哪些方法可用
S->C: OPTION response //S回应信息中包括提供的所有可用方法
② C->S: DESCRIBE request //要求得到S提供的媒体初始化描述信息
S->C: DESCRIBE response //S回应媒体初始化描述信息,主要是sdp
③ C->S: SETUP request //设置会话属性,以及传输模式,提醒S建立会话
S->C: SETUP response //S建立会话,返回会话标识符及会话相关信息
④ C->S: PLAY request //C请求播放
S->C: PLAY response //S回应请求信息
S->C: 发送流媒体数据
⑤ C->S: TEARDOWN request //C请求关闭会话
S->C: TEARDOWN response //S回应请求
上述的过程是标准的RTSP流程,其中第3步和第4步是必需的。
OpenCore在执行完PLAYER_SET_DATASOURCE,prepare之后,执行PLAYER_INIT时,如果发现datasource是rtsp流,则进入rtsp模块。
OpenCore的RTSP模块实现在Pvrtsp_client_engine_node.cpp中,PVRTSPEngineNode::SendRtspDescribe()描述了连接建立过程中的状态变化过程。
需要注意的时,opencore在发出OPTION request后,并不会等着收response,而是直接发DESCRIBE request,然后才开始收OPTION response和DESCRIBE response。
Live555在RTSPServer.cpp中用RTSPServer::RTSPClientSession::incomingRequestHandler()来处理来自client端的request。
RTSP源码接收端使用样例:
1 // RtstClientTest.cpp 2 #include "stdafx.h" 3 #include "RtspRequest.h" 4 #include "Rtp.h" 5 6 RtspRequest g_RtspRequest; 7 int _tmain(int argc, _TCHAR* argv[]) 8 { 9 // 接收 10 string url = "rtsp://192.168.1.1:554/aacAudioTest"; 11 string setupName = "aacAudioTest"; 12 INT rtpPort = 8080; 13 INT rtcpPort = rtpPort + 2; 14 string sdp; 15 INT64 sess; 16 g_RtspRequest.Open(url.c_str(), "127.0.0.0", 0); 17 g_RtspRequest.RequestOptions(); 18 g_RtspRequest.RequestDescribe(&sdp); 19 g_RtspRequest.RequestSetup(setupName.c_str(), transportModeRtpUdp, rtpPort , rtcpPort , &sess); 20 g_RtspRequest.RequestPlay(); 21 Rtp* pRtp = new Rtp(); 22 pRtp->Open("127.0.0.0", rtpPort); 23 PBYTE pBuffer = new BYTE[1024*1024*10]; 24 int iRead; 25 INT payloadType; 26 WORD sequenceNumber; 27 INT32 timeStamp; 28 INT32 ssrc; 29 while(TRUE) { 30 iRead = pRtp->Read(pBuffer, 1024*1024*10, &payloadType, &sequenceNumber, &timeStamp, &ssrc); 31 if (iRead > 0) { 32 // save buff 33 } 34 35 } 36 delete pRtp; 37 g_RtspRequest.RequestPause(); 38 g_RtspRequest.RequestTeardown(); 39 g_RtspRequest.Close(); 40 delete []pBuffer; 41 42 return 0; 43 } 44 45 46 47 //
1.OPTION
目的是得到服务器提供的可用方法:
OPTIONS rtsp://192.168.20.136:5000/xxx666 RTSP/1.0
CSeq: 1 //每个消息都有序号来标记,第一个包通常是option请求消息
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10)
服务器的回应信息包括提供的一些方法,例如:
RTSP/1.0 200 OK
Server: UServer 0.9.7_rc1
Cseq: 1 //每个回应消息的cseq数值和请求消息的cseq相对应
Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, SCALE,
GET_PARAMETER //服务器提供的可用的方法
2.DESCRIBE
C向S发起DESCRIBE请求,为了得到会话描述信息(SDP):
DESCRIBE rtsp://192.168.20.136:5000/xxx666 RTSP/1.0
CSeq: 2
token:
Accept: application/sdp
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10)
服务器回应一些对此会话的描述信息(sdp):
RTSP/1.0 200 OK
Server: UServer 0.9.7_rc1
Cseq: 2
x-prev-url: rtsp://192.168.20.136:5000
x-next-url: rtsp://192.168.20.136:5000
x-Accept-Retransmit: our-retransmit
x-Accept-Dynamic-Rate: 1
Cache-Control: must-revalidate
Last-Modified: Fri, 10 Nov 2006 12:34:38 GMT
Date: Fri, 10 Nov 2006 12:34:38 GMT
Expires: Fri, 10 Nov 2006 12:34:38 GMT
Content-Base: rtsp://192.168.20.136:5000/xxx666/
Content-Length: 344
Content-Type: application/sdp
v=0 //以下都是sdp信息
o=OnewaveUServerNG 1451516402 1025358037 IN IP4 192.168.20.136
s=/xxx666
u=http:///
e=admin@
c=IN IP4 0.0.0.0
t=0 0
a=isma-compliance:1,1.0,1
a=range:npt=0-
m=video 0 RTP/AVP 96 //m表示媒体描述,下面是对会话中视频通道的媒体描述
a=rtpmap:96 MP4V-ES/90000
a=fmtp:96
profile-level-id=245;config=000001B0F5000001B509000001000000012000C888B0E0E0FA62D089028307
a=control:trackID=0//trackID=0表示视频流用的是通道0
3.SETUP
客户端提醒服务器建立会话,并确定传输模式:
SETUP rtsp://192.168.20.136:5000/xxx666/trackID=0 RTSP/1.0
CSeq: 3
Transport: RTP/AVP/TCP;unicast;interleaved=0-1
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10)
//uri中带有trackID=0,表示对该通道进行设置。Transport参数设置了传输模式,包的结构。接下来的数据包头部第二个字节位置就是interleaved,它的值是每个通道都不同的,trackID=0的interleaved值有两个0或1,0表示rtp包,1表示rtcp包,接受端根据interleaved的值来区别是哪种数据包。
服务器回应信息:
RTSP/1.0 200 OK
Server: UServer 0.9.7_rc1
Cseq: 3
Session: 6310936469860791894 //服务器回应的会话标识符
Cache-Control: no-cache
Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=6B8B4567
4.PLAY
客户端发送播放请求:
PLAY rtsp://192.168.20.136:5000/xxx666 RTSP/1.0
CSeq: 4
Session: 6310936469860791894
Range: npt=0.000- //设置播放时间的范围
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10)
服务器回应信息:
RTSP/1.0 200 OK
Server: UServer 0.9.7_rc1
Cseq: 4
Session: 6310936469860791894
Range: npt=0.000000-
RTP-Info: url=trackID=0;seq=17040;rtptime=1467265309
//seq和rtptime都是rtp包中的信息
5.TEARDOWN
客户端发起关闭请求:
TEARDOWN rtsp://192.168.20.136:5000/xxx666 RTSP/1.0
CSeq: 5
Session: 6310936469860791894
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10)
服务器回应:
RTSP/1.0 200 OK
Server: UServer 0.9.7_rc1
Cseq: 5
Session: 6310936469860791894
Connection: Close
以上方法都是交互过程中最为常用的,其它还有一些重要的方法如 get/set_parameter,pause,redirect等等
ps:
sdp的格式
v=<version>
o=<username> <session id> <version> <network type> <address type> <address>
s=<session name>
i=<session description>
u=<URI>
e=<email address>
p=<phone number>
c=<network type> <address type> <connection address>
b=<modifier>:<bandwidth-value>
t=<start time> <stop time>
r=<repeat interval> <active duration> <list of offsets from start-time>
z=<adjustment time> <offset> <adjustment time> <offset> ....
k=<method>
k=<method>:<encryption key>
a=<attribute>
a=<attribute>:<value>
m=<media> <port> <transport> <fmt list>
v = (协议版本)
o = (所有者/创建者和会话标识符)
s = (会话名称)
i = * (会话信息)
u = * (URI 描述)
e = * (Email 地址)
p = * (电话号码)
c = * (连接信息)
b = * (带宽信息)
z = * (时间区域调整)
k = * (加密密钥)
a = * (0 个或多个会话属性行)
时间描述:
t = (会话活动时间)
r = * (0或多次重复次数)
媒体描述:
m = (媒体名称和传输地址)
i = * (媒体标题)
c = * (连接信息 — 如果包含在会话层则该字段可选)
b = * (带宽信息)
k = * (加密密钥)
a = * (0 个或多个媒体属性行)
参考文章:rfc2326(rtsp);rfc2327(sdp)
RTSP点播消息流程实例(客户端:VLC, RTSP服务器:LIVE555 Media Server)
1) C(Client)-> M(Media Server)
OPTIONS rtsp://192.168.1.109/1.mpg RTSP/1.0
CSeq: 1
user-Agent: VLC media player(LIVE555 Streaming Media v2007.02.20)
1) M -> C
RTSP/1.0 200 OK
CSeq: 1
Date: wed, Feb 20 2008 07:13:24 GMT
Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE
2) C -> M
DESCRIBE rtsp://192.168.1.109/1.mpg RTSP/1.0
CSeq: 2
Accept: application/sdp
User-Agent: VLC media player(LIVE555 Streaming Media v2007.02.20)
2) M -> C
RTSP/1.0 200 OK
CSeq: 2
Date: wed, Feb 20 2008 07:13:25 GMT
Content-Base: rtsp://192.168.1.109/1.mpg/
Content-type: application/sdp
Content-length: 447
v=0
o =- 2284269756 1 IN IP4 192.168.1.109
s=MPEG-1 or 2 program Stream, streamed by the LIVE555 Media Server
i=1.mpg
t=0 0
a=tool:LIVE555 Streaming Media v2008.02.08
a=type:broadcast
a=control:*
a=range:npt=0-66.181
a=x-qt-text-nam:MPEG-1 or Program Stream, streamed by the LIVE555 Media Server
a=x-qt-text-inf:1.mpg
m=video 0 RTP/AVP 32
c=IN IP4 0.0.0.0
a=control:track1
m=audio 0 RTP/AVP 14
c=IN IP4 0.0.0.0
a=control:track2
3) C -> M
SETUP rtsp://192.168.1.109/1.mpg/track1 RTSP/1.0
CSeq: 3
Transport: RTP/AVP; unicast;client_port=1112-1113
User-Agent: VLC media player(LIVE555 Streaming Media v2007.02.20)
3) M -> C
RTSP/1.0 200 OK
CSeq: 3
Date: wed, Feb 20 2008 07:13:25 GMT
Transport: RTP/AVP;unicast;destination=192.168.1.222;source=192.168.1.109;client_port=1112-1113;server_port=6970-6971
Session: 3
4) C -> M
SETUP rtsp://192.168.1.109/1.mpg/track2 RTSP/1.0
CSeq: 4
Transport: RTP/AVP; unicast;client_port=1114-1115
Session: 3
User-Agent: VLC media player(LIVE555 Streaming Media v2007.02.20)
4) M -> C
RTSP/1.0 200 OK
CSeq: 4
Date: wed, Feb 20 2008 07:13:25 GMT
Transport: RTP/AVP;unicast;destination=192.168.1.222;source=192.168.1.109;client_port=1114-1115;server_port=6972-6973
Session: 3
5) C -> M
PLAY rtsp://192.168.1.109/1.mpg/ RTSP/1.0
CSeq: 5
Session: 3
Range: npt=0.000-
User-Agent: VLC media player(LIVE555 Streaming Media v2007.02.20)
5) M -> C
RTSP/1.0 200 OK
CSeq: 5
Range: npt=0.000-
Session: 3
RTP-Info: url=rtsp://192.168.1.109/1.mpg/track1;seq=9200;rtptime=214793785,url=rtsp://192.168.1.109/1.mpg/tr