RTSP

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)

RTSP交互过程

其中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 端口示意
  • 通常偶数端口用来接收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.

  • 客户端发送播放请求.
  • 可指定播放的范围, 类似HTTPRange:.
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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容

  • RTSP Spec中文版(1-11)RTSP Spec中文版(12-16)RTSP Spec中文版(附录) 12 ...
    SniperPan阅读 1,276评论 0 3
  • RFC 2326RTSP Spec中文版(1-11)RTSP Spec中文版(12-16)RTSP Spec中文版...
    SniperPan阅读 5,486评论 3 10
  • gihub:https://github.com/wangdxh/Desert-Eagle/只实现了视频的处理。r...
    little_wang阅读 9,693评论 0 21
  • 假如生活欺骗了你,不要悲伤,不要难过。。发生的事情像梦一样,不能相信,却就这么真的发生了,真实的发生了,让我措不及...
    菲菲428阅读 243评论 0 0
  • 好好找了老爸老妈,认真与她们沟通。我走偏了、走慢了、走远了、恐惧了、畏惧了、偏执不愿接受。我两非性格不合,实乃我惰...
    一思量阅读 141评论 0 1