网络上有很多介绍RTSP的文章,但是看后还是印象不深刻,这里就记录一下读学习笔记吧。
1.协议介绍
RTSP协议以CS的方式工作,它是一个应用层的多媒体播放控制协议,用来使用户在播放从互联网下载的实时数据时能够对其进行控制,如暂停、继续、快进、快退等
RTSP的语法和操作与HTTP相似了,所有请求和响应都是基于ASCII文本。不同的是RTSP是有状态的协议,而HTTP是无状态的。RTSP记录客户端的状态,如是播放还是暂停等。RTSP没有规定音视频的压缩方法,这个都是开发人自己选择的,只要客户端和服务端能够统一好就行。
详细的内容请点击:https://tools.ietf.org/html/rfc2326
2.可用方法
下面词条引用自:https://baike.baidu.com/item/RTSP/1276768?fr=aladdin
方法 | 方向 | 对象 | 要求 | 含义 |
---|---|---|---|---|
DESCRIBE | C->S | P,S | 推荐 | 检查演示或媒体对象的描述,也允许使用接收头指定用户理解的描述格式。DESCRIBE的答复-响应组成媒体RTSP初始阶段 |
ANNOUNCE | C->SS->C | P,S | 可选 | 当从用户发往服务器时,ANNOUNCE将请求URL识别的演示或媒体对象描述发送给服务器;反之,ANNOUNCE实时更新连接描述。如新媒体流加入演示,整个演示描述再次发送,而不仅仅是附加组件,使组件能被删除 |
GET_PARAMETER | C->SS->C | P,S | 可选 | GET_PARAMETER请求检查URL指定的演示与媒体的参数值。没有实体体时,GET_PARAMETER也许能用来测试用户与服务器的连通情况 |
OPTIONS | C->SS->C | P,S | 要求 | 可在任意时刻发出OPTIONS请求,如用户打算尝试非标准请求,并不影响服务器状态 |
PAUSE | C->S | P,S | 推荐 | PAUSE请求引起流发送临时中断。如请求URL命名一个流,仅回放和记录被停止;如请求URL命名一个演示或流组,演示或组中所有当前活动的流发送都停止。恢复回放或记录后,必须维持同步。在SETUP消息中连接头超时参数所指定时段期间被暂停后,尽管服务器可能关闭连接并释放资源,但服务器资源会被预订 |
PLAY | C->S | P,S | 要求 | PLAY告诉服务器以SETUP指定的机制开始发送数据;直到一些SETUP请求被成功响应,客户端才可发布PLAY请求。PLAY请求将正常播放时间设置在所指定范围的起始处,发送流数据直到范围的结束处。PLAY请求可排成队列,服务器将PLAY请求排成队列,顺序执行 |
RECORD | C->S | P,S | 可选 | 该方法根据演示描述初始化媒体数据记录范围,时标反映开始和结束时间;如没有给出时间范围,使用演示描述提供的开始和结束时间。如连接已经启动,立即开始记录,服务器数据请求URL或其他URL决定是否存储记录的数据;如服务器没有使用URL请求,响应应为201(创建),并包含描述请求状态和参考新资源的实体与位置头。支持现场演示记录的媒体服务器必须支持时钟范围格式,smpte格式没有意义 |
REDIRECT | S->C | P,S | 可选 | 重定向请求通知客户端连接到另一服务器地址。它包含强制头地址,指示客户端发布URL请求;也可能包括参数范围,以指明重定向何时生效。若客户端要继续发送或接收URL媒体,客户端必须对当前连接发送TEARDOWN请求,而对指定主执新连接发送SETUP请求 |
SETUP | C->S | S | 要求 | 对URL的SETUP请求指定用于流媒体的传输机制。客户端对正播放的流发布一个SETUP请求,以改变服务器允许的传输参数。如不允许这样做,响应错误为"455 Method Not Valid In This State”。为了透过防火墙,客户端必须指明传输参数,即使对这些参数没有影响 |
SET_PARAMETER | C->SS->C | P,S | 可选 | 这个方法请求设置演示或URL指定流的参数值。请求仅应包含单个参数,允许客户端决定某个特殊请求为何失败。如请求包含多个参数,所有参数可成功设置,服务器必须只对该请求起作用。服务器必须允许参数可重复设置成同一值,但不让改变参数值。注意:媒体流传输参数必须用SETUP命令设置。将设置传输参数限制为SETUP有利于防火墙。将参数划分成规则排列形式,结果有更多有意义的错误指示 |
TEARDOWN | C->S | P,S | 要求 | TEARDOWN请求停止给定URL流发送,释放相关资源。如URL是此演示URL,任何RTSP连接标识不再有效。除非全部传输参数是连接描述定义的,SETUP请求必须在连接可再次播放前发布 |
注:P----演示,S----流,C----用户端,S----服务器端
3.工作流程
下面这个是根据本人抓包分画出的工作流程,这是简单的连接、播放、停止。不过其它的请求也类似,待用的时候查官方想协议查看即可。
- 6步骤会返回一个session id给客户端进程
- 9过程开始传送音视频数据,协议可以运行在UDP、RTP、TCP。在播放过程中可以随时暂停。
注意:OPTIONS 请求用于返回服务端支持的 RTSP方法列表 。也可以定时发送这个请求来保活相关的 RTSP 会话。
4.实例分析
从网上找到一个可以使用rtsp协议播放的大熊兔视频,链接为:rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov
使用ffplay播放该视频 ffplay.exe rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov
,效果如下所示:
这里使用ffplay播放文件时打印出如下日志,可以看到一开始想使用UDP协议的,但是超时,使用TCP传输。
[udp @ 00000219cd8506c0] 'circular_buffer_size' option was set but it is not supported on this build (pthread support is required)
[udp @ 00000219cd846d40] 'circular_buffer_size' option was set but it is not supported on this build (pthread support is required)
[udp @ 00000219cd875c80] 'circular_buffer_size' option was set but it is not supported on this build (pthread support is required)
[udp @ 00000219cd8457c0] 'circular_buffer_size' option was set but it is not supported on this build (pthread support is required)
[rtsp @ 00000219cd84d940] UDP timeout, retrying with TCP 0B f=0/0
Input #0, rtsp, from 'rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov':
使用wireshark抓包后获取如下日志,这里把日志分开来分析,并详细分析关键字段的意思。
<1> OPTION
客户可在任意时刻发出OPTIONS请求,OPTION发出后服务器会返回支持的方法
OPTIONS rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov RTSP/1.0
CSeq: 1
User-Agent: Lavf58.20.100
RTSP/1.0 200 OK
CSeq: 1
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, OPTIONS, ANNOUNCE, RECORD, GET_PARAMETER
Supported: play.basic, con.persistent
上面的Supported: play.basic, con.persistent
支持持续的播放服务。
<2> DESCRIBE
DESCRIBE方法根据URL来检索服务器的描述信息,可以根据accept头部来指定描述信息的格式(下面例子就是这样),请求发出后服务器返回对应的格式的描述信息,供客户端使用。需要注意的是服务器返回的数据必须包含所有媒体资源的初始化信息。
1)SDP(Session Description Protocol)
由于这里返回的SDP的数据包,下面简单介绍一下SDP常用字段意思。更详细的可以看官方文档 https://tools.ietf.org/html/rfc4566
Session description
v= (protocol version)
o= (originator and session identifier)
s= (session name)
i=* (session information)
u=* (URI of description)
e=* (email address)
p=* (phone number)
c=* (connection information -- not required if included in
all media)
b=* (zero or more bandwidth information lines)
One or more time descriptions ("t=" and "r=" lines; see below)
z=* (time zone adjustments)
k=* (encryption key)
a=* (zero or more session attribute lines) //其它的session属性行
Zero or more media descriptions
Time description
t= (time the session is active)
r=* (zero or more repeat times)
Media description, if present
m= (media name and transport address)
i=* (media title)
c=* (connection information -- optional if included at
session level)
b=* (zero or more bandwidth information lines)
k=* (encryption key)
a=* (zero or more media attribute lines)
2)结合上面SDP数据包格式分析下面实时抓包的数据。
DESCRIBE rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov RTSP/1.0
Accept: application/sdp //客户端请求时指定sdp格式
CSeq: 2
User-Agent: Lavf58.20.100
RTSP/1.0 200 OK
CSeq: 2
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Expires: Sun, 17 Feb 2019 05:55:05 UTC
Content-Length: 590 //内容长度有590个字节
Content-Base: rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/
Date: Sun, 17 Feb 2019 05:55:05 UTC
Content-Type: application/sdp //返回的数据类型sdp
Session: 1372529946;timeout=60
//下面是协议的内容
v=0
o=- 1372529946 1372529946 IN IP4 184.72.239.149
s=BigBuckBunny_175k.mov
c=IN IP4 184.72.239.149 //连接信息,这里是服务器的ip
t=0 0
a=sdplang:en
a=range:npt=0- 596.458 //数据源的长度是596.458s
a=control:*
m=audio 0 RTP/AVP 96 //m的意思就是media stream ,这里是audio stream使用RTP传输,内容类型是96,96的意思看下面的Payload types
a=rtpmap:96 mpeg4-generic/48000/2 //48000采样率,通道2
a=fmtp:96 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1190
a=control:trackID=1 //audio的trackID是1
m=video 0 RTP/AVP 97 //m的意思就是media stream ,这里是video stream使用RTP传输,内容类型是97,97的意思看下面的Payload types
a=rtpmap:97 H264/90000 //H264编码的码流,采样率90000
a=fmtp:97 packetization-mode=1;profile-level-id=42C01E;sprop-parameter-sets=Z0LAHtkDxWhAAAADAEAAAAwDxYuS,aMuMsg==
a=cliprect:0,0,160,240 //窗口属性
a=framesize:97 240-160 //帧格式和帧大小
a=framerate:24.0 //帧率
a=control:trackID=2 //这里video的trackID=2
下面就扩展下面2项内容,其它自行看文档查阅吧。
- trackID:服务器会为每一路流分配一个id,这里上面可以看到声音和视频均分配一个trackID。
- Payload types:就是传送数据的格式,数据在互联网传输时需要先压缩一下,传送到目的主机后在进行解码。下面是官方描述的一些格式,更详细的可以查看 https://tools.ietf.org/html/rfc1890
PT encoding audio/video clock rate channels
name (A/V) (Hz) (audio)
_______________________________________________________________
0 PCMU A 8000 1
1 1016 A 8000 1
2 G721 A 8000 1
3 GSM A 8000 1
4 unassigned A 8000 1
5 DVI4 A 8000 1
6 DVI4 A 16000 1
7 LPC A 8000 1
8 PCMA A 8000 1
9 G722 A 8000 1
10 L16 A 44100 2
11 L16 A 44100 1
12 unassigned A
13 unassigned A
14 MPA A 90000 (see text)
15 G728 A 8000 1
16--23 unassigned A
24 unassigned V
25 CelB V 90000
26 JPEG V 90000
27 unassigned V
28 nv V 90000
29 unassigned V
30 unassigned V
31 H261 V 90000
32 MPV V 90000
33 MP2T AV 90000
34--71 unassigned ?
72--76 reserved N/A N/A N/A
77--95 unassigned ?
96--127 dynamic ?
<3> SETUP
SETUP方法用于对请求指定流媒体的传输机制,包含端口,是否使用cache。
//音频流的传输机制获取
SETUP rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/trackID=1 RTSP/1.0 //traceID=1 这是音频流
Transport: RTP/AVP/UDP;unicast;client_port=26952-26953 //音频流端口号
CSeq: 3
User-Agent: Lavf58.20.100
Session: 1372529946
RTSP/1.0 200 OK
CSeq: 3
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Expires: Sun, 17 Feb 2019 05:55:05 UTC
Transport: RTP/AVP/UDP;unicast;client_port=26952-26953;source=184.72.239.149;server_port=12860-12861;ssrc=07C68CF2
//服务器返回客户端端口号已经对应的服务端口号。
Date: Sun, 17 Feb 2019 05:55:05 UTC
Session: 1372529946;timeout=60
//视频流的传输机制获取
SETUP rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/trackID=2 RTSP/1.0 //traceID=2 这是视频流
Transport: RTP/AVP/UDP;unicast;client_port=26954-26955 //视频流端口号
CSeq: 4
User-Agent: Lavf58.20.100
Session: 1372529946
RTSP/1.0 200 OK
CSeq: 4
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Expires: Sun, 17 Feb 2019 05:55:06 UTC
Transport: RTP/AVP/UDP;unicast;client_port=26954-26955;source=184.72.239.149;server_port=12868-12869;ssrc=281B60E7
//服务器返回客户端端口号已经对应的服务端口号,注意和上面音频流对比。
Date: Sun, 17 Feb 2019 05:55:06 UTC
Session: 1372529946;timeout=60
这两根据DESCRIBE返回的信息可以了解到,有2个媒体流。而且服务器已经给它们各分配了一个trackID,用于标示这个资源。由音视频请求中可以发现,音视频流请求对应不同端口,而且服务端也是对应不同端口,这里要特别注意。
<4> PLAY
上面该请求的数据已经获取了,发送PLAY方法开始播放视频。
PLAY rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/ RTSP/1.0
Range: npt=0.000-
CSeq: 5
User-Agent: Lavf58.20.100
Session: 1372529946
RTSP/1.0 200 OK
RTP-Info: url=rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/trackID=1;seq=1;rtptime=0,url=rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/trackID=2;seq=1;rtptime=0
CSeq: 5
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Range: npt=0.0-596.458
Session: 1372529946;timeout=60
- RTP-Info:在服务器响应PLAY方法时,此字段用于设置RTP特定参数。这里简单引用一下(详细查看:https://tools.ietf.org/html/rfc2326#page-55)
url:
Indicates the stream URL which for which the following RTP
parameters correspond.
seq:
Indicates the sequence number of the first packet of the
stream. This allows clients to gracefully deal with packets
when seeking. The client uses this value to differentiate
packets that originated before the seek from packets that
originated after the seek.
rtptime:
Indicates the RTP timestamp corresponding to the time value in
the Range response header. (Note: For aggregate control, a
particular stream may not actually generate a packet for the
Range time value returned or implied. Thus, there is no
guarantee that the packet with the sequence number indicated
by seq actually has the timestamp indicated by rtptime.) The
client uses this value to calculate the mapping of RTP time to
NPT.
<5> PAUSE
PAUSE方法用于通知服务器停止发送音视频流。
PAUSE rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/ RTSP/1.0
CSeq: 6
User-Agent: Lavf58.20.100
Session: 1372529946
RTSP/1.0 200 OK
CSeq: 6
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Session: 1372529946;timeout=60
这里由于在使用UDP传输时超时了,ffplay又重新选择了TCP传输,所以下面又看到了。SETUP、PLAY方法。
SETUP rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/trackID=1 RTSP/1.0
Transport: RTP/AVP/TCP;unicast;interleaved=0-1
CSeq: 7
User-Agent: Lavf58.20.100
RTSP/1.0 200 OK
CSeq: 7
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Expires: Sun, 17 Feb 2019 05:55:17 UTC
Transport: RTP/AVP/TCP;unicast;interleaved=0-1
Date: Sun, 17 Feb 2019 05:55:17 UTC
Session: 1372529946;timeout=60
SETUP rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/trackID=2 RTSP/1.0
Transport: RTP/AVP/TCP;unicast;interleaved=2-3
CSeq: 8
User-Agent: Lavf58.20.100
Session: 1372529946
RTSP/1.0 200 OK
CSeq: 8
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Expires: Sun, 17 Feb 2019 05:55:18 UTC
Transport: RTP/AVP/TCP;unicast;interleaved=2-3
Date: Sun, 17 Feb 2019 05:55:18 UTC
Session: 1372529946;timeout=60
PLAY rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/ RTSP/1.0
Range: npt=0.000-
CSeq: 9
User-Agent: Lavf58.20.100
Session: 1372529946
RTSP/1.0 200 OK
RTP-Info: url=rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/trackID=1;seq=178;rtptime=0,url=rtsp://184.72.239.149:554/vod/mp4://BigBuckBunny_175k.mov/trackID=2;seq=274;rtptime=0
CSeq: 9
Server: Wowza Streaming Engine 4.7.5.01 build21752
Cache-Control: no-cache
Range: npt=0.0-596.458
Session: 1372529946;timeout=60
5.总结
转眼间天黑了,一下午一篇文档,算是初步了解了RTSP协议的使用,但是还没进入真正的网络编程实践。接下来继续学习RTP、RTCP,百度输入文件编号即可找到文件。
协议 | 参考文档 |
---|---|
RTSP | 《RFC2326》 |
RTP | 《RFC3550》《RFC 3605》 |
RTCP | 《RFC3550》 |
SDP | 《RFC 4566》 |