RTSP是1996年由 RealNetworks, Netscape, 哥倫比亞大學開發,提交草案給 IETF,1998年發布為 RFC2326,2016年RTSP 2.0 發布於 RFC 7826。RTSP 是串流媒體伺服器的控制協定,可建立與控制終端設備跟伺服器之間的多媒體 session。
RTSP 協定本身看起來跟 HTTP 類似,但 HTTP 本身是 stateless,而RTSP 是 stateful,故需要追蹤 session。RTSP 是用 TCP 連線,而多媒體本身,是用 RTP 傳輸,可以用 TCP 或 UDP,常見狀況是為求傳輸速度快,使用 UDP。
節錄一個 RTSP client 取得 RTSP 的封包過程
Options
查詢 RTSP server 支援的 command
Client -> Server
Request: OPTIONS rtsp://192.168.1.11:8554/mystream RTSP/1.0\r\n CSeq: 2\r\n User-Agent: LibVLC/3.0.20 (LIVE555 Streaming Media v2016.11.28)\r\n \r\n
Server -> Client
Response: RTSP/1.0 200 OK\r\n CSeq: 2\r\n Public: DESCRIBE, ANNOUNCE, SETUP, PLAY, RECORD, PAUSE, GET_PARAMETER, TEARDOWN\r\n Server: gortsplib\r\n \r\n
Describe
查詢可處理的多媒體資料格式,server 以 SDP 方式回覆
Client -> Server
Request: DESCRIBE rtsp://192.168.1.11:8554/mystream RTSP/1.0\r\n CSeq: 3\r\n User-Agent: LibVLC/3.0.20 (LIVE555 Streaming Media v2016.11.28)\r\n Accept: application/sdp\r\n \r\n
Server -> Client
Response: RTSP/1.0 200 OK\r\n CSeq: 3\r\n Content-Base: rtsp://192.168.1.11:8554/mystream/\r\n Content-length: 560 Content-type: application/sdp Server: gortsplib\r\n \r\n Session Description Protocol Version (v): 0 Owner/Creator, Session Id (o): - 0 0 IN IP4 127.0.0.1 Session Name (s): test Connection Information (c): IN IP4 0.0.0.0 Time Description, active time (t): 0 0 Media Description, name and address (m): video 0 RTP/AVP 96 Media Attribute (a): control:rtsp://192.168.1.11:8554/mystream/trackID=0 Media Attribute (a): rtpmap:96 H264/90000 Media Attribute (a): fmtp:96 packetization-mode=1; profile-level-id=640032; sprop-parameter-sets=Z2QAMqzIUB4AiflwEQAAAwPpAAC7gA8YMZY=,aOk4XLIs Media Description, name and address (m): audio 0 RTP/AVP 97 Media Attribute (a): control:rtsp://192.168.1.11:8554/mystream/trackID=1 Media Attribute (a): rtpmap:97 mpeg4-generic/48000/2 Media Attribute (a): fmtp:97 config=1190; indexdeltalength=3; indexlength=3; mode=AAC-hbr; profile-level-id=1; sizelength=13; streamtype=5
Setup
要求 server 設定傳送某一個 media stream,setup 要在 play 之前完成
Client -> Server
Request: SETUP rtsp://192.168.1.11:8554/mystream/trackID=0 RTSP/1.0\r\n CSeq: 4\r\n User-Agent: LibVLC/3.0.20 (LIVE555 Streaming Media v2016.11.28)\r\n Transport: RTP/AVP/TCP;unicast;interleaved=0-1 \r\n
Server -> Client
Response: RTSP/1.0 200 OK\r\n CSeq: 4\r\n Server: gortsplib\r\n Session: 262553a7d5084e8fb57f9d8f485c89f4 Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=03DAD41B \r\n
Setup
client 透過 setup 跟 server 確認 stream session
Client -> Server
Request: SETUP rtsp://192.168.1.11:8554/mystream/trackID=1 RTSP/1.0\r\n CSeq: 5\r\n User-Agent: LibVLC/3.0.20 (LIVE555 Streaming Media v2016.11.28)\r\n Transport: RTP/AVP/TCP;unicast;interleaved=2-3 Session: 262553a7d5084e8fb57f9d8f485c89f4 \r\n
Server -> Client
Response: RTSP/1.0 200 OK\r\n CSeq: 5\r\n Server: gortsplib\r\n Session: 262553a7d5084e8fb57f9d8f485c89f4 Transport: RTP/AVP/TCP;unicast;interleaved=2-3;ssrc=294122AE \r\n
Play
在 setup 設定的 session 裡面,開始播放 media
Client -> Server
Request: PLAY rtsp://192.168.1.11:8554/mystream/ RTSP/1.0\r\n CSeq: 6\r\n User-Agent: LibVLC/3.0.20 (LIVE555 Streaming Media v2016.11.28)\r\n Session: 262553a7d5084e8fb57f9d8f485c89f4 Range: npt=0.000-\r\n \r\n
Server -> Client
Response: RTSP/1.0 200 OK\r\n CSeq: 6\r\n RTP-Info: url=rtsp://192.168.1.11:8554/mystream/trackID=0;seq=10453;rtptime=1024864644,url=rtsp://192.168.1.11:8554/mystream/trackID=1;seq=4824;rtptime=3779844790\r\n Server: gortsplib\r\n Session: 262553a7d5084e8fb57f9d8f485c89f4 \r\n
RTP
用 RTP 的格式,傳送 media 內容
Server -> Client
10.. .... = Version: RFC 1889 Version (2) ..0. .... = Padding: False ...0 .... = Extension: False .... 0000 = Contributing source identifiers count: 0 0... .... = Marker: False Payload type: DynamicRTP-Type-96 (96) Sequence number: 10453 Timestamp: 1024871144 Synchronization Source identifier: 0x03dad41b (64672795) Payload: 09f0
Teardown
client 通知 server 停止播放 media
Client -> Server
Request: TEARDOWN rtsp://192.168.1.11:8554/mystream/ RTSP/1.0\r\n CSeq: 7\r\n User-Agent: LibVLC/3.0.20 (LIVE555 Streaming Media v2016.11.28)\r\n Session: 262553a7d5084e8fb57f9d8f485c89f4 \r\n