2024-07-08
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀
🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C 、数据结构、音视频🍭
🤣本文内容🤣:🍭介绍RTSP协议 🍭
😎金句分享😎:🍭你不能选择最好的,但最好的会来选择你——泰戈尔🍭
⏰Release time⏰: 2024-07-06 12:22:00
This article may not be forwarded without permission!!!
RTSP, full name is Time Real Time Streaming Protocol
, Real-time Streaming Protocol, is an application layer protocol in the TCP/IP protocol system, and is an IETF RFC standard submitted by Columbia University, Netscape and RealNetworks.
The official document about RTSP protocol is RFC2326, document link:RFC2326-Real Time Streaming Protocol (RTSP)。
The syntax and operation of the RTSP protocol refer to HTTP/1.1
, a text-based protocol that uses the ISO10646 character set and UTF-8 encoding; the transport layer protocol that carries RTSP isTCP
, default port554
; If it is RTSP-over-HTTP tunneling, the default TCP port is 8080; it is usually used in conjunction with the RTP/RTCP protocol, with the RTP protocol transmitting real-time streaming data and the RTCP protocol completing the transmission of data streams and control commands.
RTP protocol: full name
Real-time Transport Protocol
, Real-time Transport Protocol, was published by the IETF Multimedia Transport Working Group in RFC 1889 in 1996. The RTP protocol specifies the standard data packet format for delivering audio and video on the Internet. It is built on the UDP protocol.
RTCP protocol: full name
Real-time Transport Control Protocol
, Real-time Transport Control Protocol, is used in conjunction with RTP. RTP uses an even-numbered UDP port; RTCP uses the next port after RTP, which is an odd-numbered port. RTCP works in conjunction with RTP, with RTP performing the actual data transmission and RTCP being responsible for sending control packets to everyone in the session. Its main function is to provide feedback on the quality of service that RTP is providing.
The difference between RTSP protocol and HTTP protocol:
RTSP is stateful, and its commands are always sent in order, and one command may always need to be sent before another command. HTTP is stateless, and the connection will be disconnected after sending a command, and there is no dependency between commands.
The rtsp protocol uses port 554 and http uses port 80.
RTSP requests can be sent by both the server and the client, while HTTP requests can only be sent by the client.
Commonly used RTSP methods include: OPTIONS, DESCRIBE, SETUP, PLAY, PAUSE, TEARDOWN, ANNOUNCE, GET_PARAMETER and SET_PARAMETER. The detailed usage is as follows:
OPTIONS
: The client asks the server for methods supported by the server. It does not affect the state of the server;DESCRIBE
: The client obtains the description of the media object specified by the URL from the server.Accept
The field specifies the description format;SETUP
:The client requests the server to establish a session and prepare for transmission. The request information mainly includes the transmission protocol and the client's port number;PLAY
: The client actively notifies the server to start sending data using the mechanism specified in SETUP.Range
The field specifies the start and end time of the playback (the real-time stream Range is generallyRange: npt=0.000-
), when multiple PLAY requests arrive, the server will queue the PLAY requests and execute them sequentially, that is, it must wait until the first PLAY time is completed before continuing to process the second PLAY message.PAUSE
: The client requests the server to temporarily pause the media streaming.Range
The parameter pauses at a specified time point. You can also specify a stream to pause. For example, if you specify an audio stream to pause, the playback will be silent.RECORD
: RECORD notifies the server that the client will start recording media data according to the previous description. timestamp
The fields reflect the start and end times (in UTC). If this field is not present, the start or end time from the media description is used. If the session has already started, recording starts immediately.request-URI
If the server does not use a request-URI, the response SHOULD be 201 (Created) and contain an entity and a Location header describing the status of the request and referencing the new resource.TEARDOWN
: The client requests to stop sending the specified URL stream and release related resources.REDIRECT
: A redirect request in which the server informs the client that it must connect to another server location. It contains the mandatory header Location, which indicates that the client should issue a request to that URL. It may contain the parameter Range, which indicates when the redirection takes effect. If the client wants to continue sending or receiving media to this URI, the client must issue a TEARDOWN request for the current session and a SETUP for the new session on the specified host.ANNOUNCE
: When sent by the client to the server, it means submitting the presentation description or media object identified by the request URL to the server.GET_PARAMETER
: A GET_PARAMETER request retrieves the value of a parameter of the representation or stream specified in the URI. The content of the reply and response is left to the implementation. GET_PARAMETER without an entity-body can be used to test the liveness of a client or server ("ping").SET_PARAMETER
: This method requests setting of a parameter value for a presentation or a stream specified by a URL. The request should contain only a single parameter, allowing the client to determine why a particular request failed. If the request contains multiple parameters, all parameters may be successfully set, and the server must act only on that request. The server must allow parameters to be set to the same value repeatedly, but not to change the parameter value. NOTE: Media stream transport parameters must be set using the SETUP command. Limiting the setting of transport parameters to SETUP is helpful for firewalls.A total of 11 RTSP methods are introduced above, among which,SETUP
、PLAY
、TEARDOWN
The three commands are required in the RTSP process, and other methods are not required.ANNOUNCE
、GET_PARAMETER
、SET_PARAMETER
The three commands can be sent from the client to the server or from the server to the client, and the other commands are sent from the client to the server.
RTSP has two types of messages: request messages and response messages. Request messages refer to the request messages sent from the client to the server, and response messages refer to the responses from the server to the client.
The RTSP request message consists of three parts: request line, request header and request body. Among them, the request line is required, while the request header and request body are optional according to the specific situation.
rn
)Finish.方法
:These are the RTSP methods introduced above, including OPTIONS, DESCRIBE, SETUP, PLAY, PAUSE, TEARDOWN, etc.请求URI
: Identifies the media resource to be operated, usually in the format of rtsp://example.com/path/to/stream.协议版本
: Indicates the RTSP protocol version followed by the request, usuallyRTSP/1.0
orRTSP/2.0
。OPTIONS rtsp://192.168.3.225:554/wbc RTSP/1.0
CSeq: 2
User-Agent: LibVLC/3.0.19 (LIVE555 Streaming Media v2016.11.28)
The RTSP request message consists of three parts: status line, response header and response body. Among them, the status line is required, while the response header and response body are optional according to the specific situation.
协议版本
: Indicates the RTSP protocol version followed by the response, usually RTSP/1.0 or RTSP/2.0.状态码
: A three-digit number, such as 200, 401, 500, etc., used to indicate the processing result of the request. The first digit represents the response category: 2xx for success, 4xx for client error, and 5xx for server error.状态文本
: A brief text description that explains the specific meaning of the corresponding status code, such as OK, Unauthorized, etc.RTSP/1.0 200 OK
CSeq: 2
Date: Wed, Feb 04 1970 03:25:10 GMT
Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, GET_PARAMETER, SET_PARAMETER
v=0
o=- 8913478 1 IN IP4 192.168.3.91
s=LIVE555 Streaming Media v2016.07.19
i=1080
t=0 0
a=tool:LIVE555 Streaming Media v2016.07.19
a=type:broadcast
a=control:*
a=range:npt=0-
a=x-qt-text-nam:LIVE555 Streaming Media v2016.07.19
a=x-qt-text-inf:1080
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
b=AS:5000
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=64002A;sprop-parameter-sets=Z2QAKq2EAQwgCGEAQwgCGEAQwgCEO1A8ARPyoA==,aO48sA==
a=control:track1
m=audio 0 RTP/AVP 97
c=IN IP4 0.0.0.0
b=AS:768
a=rtpmap:97 PCMA/48000/2
a=control:track2
The response header of the RTSP message will contain some fields. The following are some commonly used fields:
SETUP
Once the client obtains the Session ID, it must include the Session ID in all subsequent Session operation request messages. For example: Session: 4581E0AE; timeout=65Use wireshark to capture the network packets of RTSP streaming media, and you can see that the general process is as follows:
1. Client sendsOPTIONS
Method, the server responds;
2. Client sendsDESCRIBE
Method, the server responds;
3. Client sendsSETUP
Method, the server responds;
2. Client sendsPLAY
Method, the server responds;
2. Client sendsTEARDOWN
Method, the server responds;
The complete flow acquisition message is as follows:
OPTIONS rtsp://192.168.3.225:554/wbc RTSP/1.0
CSeq: 2
User-Agent: LibVLC/3.0.19 (LIVE555 Streaming Media v2016.11.28)
RTSP/1.0 200 OK
CSeq: 2
Date: Wed, Jul 03 2024 14:42:11 GMT
Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, GET_PARAMETER, SET_PARAMETER
DESCRIBE rtsp://192.168.3.225:554/wbc RTSP/1.0
CSeq: 3
User-Agent: LibVLC/3.0.19 (LIVE555 Streaming Media v2016.11.28)
Accept: application/sdp
RTSP/1.0 200 OK
CSeq: 3
Date: Wed, Jul 03 2024 14:42:11 GMT
Content-Base: rtsp://192.168.3.225/wbc/
Content-Type: application/sdp
Content-Length: 472
v=0
o=- 1720014950032000 1 IN IP4 192.168.3.225
s=LIVE555 Streaming Media v2016.07.19
i=wbc
t=0 0
a=tool:LIVE555 Streaming Media v2016.07.19
a=type:broadcast
a=control:*
a=range:npt=0-
a=x-qt-text-nam:LIVE555 Streaming Media v2016.07.19
a=x-qt-text-inf:wbc
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
b=AS:5000
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=640029;sprop-parameter-sets=Z2QAKawsaoHgCJ WbgoCCgQ=,aO4xshs=
a=control:track1
SETUP rtsp://192.168.3.225/wbc/track1 RTSP/1.0
CSeq: 4
User-Agent: LibVLC/3.0.19 (LIVE555 Streaming Media v2016.11.28)
Transport: RTP/AVP;unicast;client_port=55320-55321
RTSP/1.0 200 OK
CSeq: 4
Date: Wed, Jul 03 2024 14:42:11 GMT
Transport: RTP/AVP;unicast;destination=192.168.2.180;source=192.168.3.225;client_port=55320-55321;server_port=6970-6971
Session: 4581E0AE;timeout=65
PLAY rtsp://192.168.3.225/wbc/ RTSP/1.0
CSeq: 5
User-Agent: LibVLC/3.0.19 (LIVE555 Streaming Media v2016.11.28)
Session: 4581E0AE
Range: npt=0.000-
RTSP/1.0 200 OK
CSeq: 5
Date: Wed, Jul 03 2024 14:42:11 GMT
Range: npt=0.000-
Session: 4581E0AE
RTP-Info: url=rtsp://192.168.3.225/wbc/track1;seq=7880;rtptime=3548171463
TEARDOWN rtsp://192.168.3.225/wbc/ RTSP/1.0
CSeq: 6
User-Agent: LibVLC/3.0.19 (LIVE555 Streaming Media v2016.11.28)
Session: 4581E0AE
RTSP/1.0 200 OK
CSeq: 6
Date: Wed, Jul 03 2024 14:42:19 GMT
The following will analyze the various RTSP methods and responses used in the previous message.
Get the available methods from the server:
The client sends the OPTIONS method and usesCSeq
To specify the request sequence number, useUser-Agent
Identify your agent;
The server will respond to the request usingCSeq
To indicate which request is being responded to, useDate
a specified date,Public
Indicates the method provided.
Get from the serverrtsp://192.168.3.225:554/wbc
A description of a media object, whereAccept
The fields specify the description format:
The client sends the DESCRIBE method and usesCSeq
To specify the request sequence number, useUser-Agent
Indicate your agent.Accept
The field specifies that the description format is SDP;
The server will respond to this request using CSeq
To indicate which request is being responded to, useDate
a specified date,Content-Type
Indicates that the content type is SDP,Content-Length
Indicates the content length.
Notice:
1. For some requests that require a username and password, the server will perform authentication when processing the DESCRIBE method. If the Authorization authentication information is not carried, or the authentication fails, the server will return a response with an error code of 401. When the client receives the 401 response, it needs to generate Authorization based on the known user authentication information and send describe again. If the authentication is successful, the server returns a response message carrying SDP.
2. The SDP information returned by the server will be parsed in the following article.
The client requests the server to establish a session and prepare for transmission. The request information mainly includes the transmission protocol and the client's port number;
The client sends the SETUP method and usesCSeq
To specify the request sequence number, useUser-Agent
Indicate your agent.Transport
The field specifies the acceptable transport protocol RTP/AVP and port (here the RTP port is specified as 55320 and the RTCP port is specified as 55321);
The server will respond to this request using CSeq
To indicate which request is being responded to, useDate
a specified date,Transport
Indicate the transport protocol RTP/AVP, destination address, source address, client port (55320 for RTP and 55321 for RTCP), server port (6970 for RTP and 6971 for RTCP),Session
Indicates the session ID.
Notice:
In this example, RTP is transmitted via UDP. Sometimes, RTP is transmitted via TCP.Transport
The fields vary. They may be:
客户端请求:Transport: RTP/AVP/TCP;unicast;interleaved=0-1
服务器响应:Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=24e4e500;mode="play"
RTP/AVP/TCP
Indicates that the RTP stream is transmitted via TCP. When this value appears, the message does not have the client_port field;
interleaved=0-1
Indicates streamid, indicating RTP streamid=0; RTCP streamid=1;
When the code stream is transmitted through TCP, it shares a TCP link with RTSP, so it does not need to establish a new connection. In order to distinguish between RTP, RTCP and RTSP protocols, it is necessary to add a packet header identifier. Here, the TCPHEAD header field is used. The tcphead is four bytes and the format is as follows:
| magic number | channel number | embedded data length | data |
magic number
: 1 byte, fixed at0x24
, is the character$
, indicating that the data transmitted is not the rtsp protocol;
channel number
: 1 byte, channel ID, identifies the type of stream, which is the streamid mentioned above;
embedded data length
: 2 bytes, indicating the stream length
data
: Indicates RTP/RTCP packet data
The client actively notifies the server to start sending data using the mechanism specified by SETUP.
The client sends the PLAY method and usesCSeq
To specify the request sequence number, useUser-Agent
Indicate your agent.Session
field specifies the session ID,Range
The fields specify the start and end time of the playback.
The server will respond to this request using CSeq
Indicates which request is being responded to; useDate
specify a date;Range
The fields specify the start and end times of the playback;Session
The field specifies the session ID;RTP-Info
The field describes the RTP information of the code stream to be sent, such as the seq and rtptime of the first RTP packet. The client can demultiplex based on this field.
The client requests to stop sending the specified URL stream and release related resources.
The client sends the TEARDOWN method and usesCSeq
To specify the request sequence number, useUser-Agent
Indicate your agent.Session
field specifies the session ID.
The server will respond to this request using CSeq
Indicates which request is being responded to; useDate
specify date.
The RTSP response content usually contains a 3-digit integer response code and a reason phrase. The purpose of the phrase is to give a brief text description of the status code. The client does not need to check or display the reason phrase. According to the first digit of the response code, it can be divided into the following five categories:
Of course, the RTSP error code is strongly related to the RTSP method. Some errors may only be triggered in specific methods. The detailed error code information is as follows:
error code | Reason phrase | Response method |
---|---|---|
100 | Continue | All |
200 | Success | All |
201 | Created | RECORD |
250 | Low on Storage Space | RECORD |
300 | Multiple Choices | All |
301 | Moved Permanently | All |
302 | Moved Temporarily | All |
303 | See Other | All |
305 | Use Proxy | All |
400 | Bad Request | All |
401 | Unauthorized | All |
402 | Payment Required | All |
403 | Forbidden | All |
404 | Not Found | All |
405 | Method Not Allowed | All |
406 | Not Acceptable | All |
407 | Proxy Authentication Required | All |
408 | Request Timeout | All |
410 | Gone | All |
411 | Length Required | All |
412 | Precondition Failed DESCRIBE | SETUP |
413 | Request Entity Too Larg | All |
414 | Request-URI Too Long | All |
415 | Unsupported Media Type | All |
451 | Invalid parameter | SETUP |
452 | Illegal Conference Identifier | SETUP |
453 | Not Enough Bandwidth | SETUP |
454 | Session Not Found | All |
455 | Method Not Valid In This State | All |
456 | Header Field Not Valid | All |
457 | Invalid Range | PLAY |
458 | Parameter Is Read-Only | SET_PARAMETER |
459 | Aggregate Operation Not Allowed | All |
460 | Only Aggregate Operation Allowed | All |
461 | Unsupported Transport | All |
462 | Destination Unreachable | All |
500 | Internal Server Error | All |
501 | Not Implemented | All |
502 | Bad Gateway | All |
503 | Service Unavailable | All |
504 | Gateway Timeout | All |
505 | RTSP Version Not Supported | All |
551 | Option not support | All |
如果文章有帮助的话,点赞👍、收藏⭐,支持一波,谢谢 😁😁😁
refer to:
Real-time Streaming Protocol—RTSP [Detailed Explanation]
Master RTSP request and response from scratch 1
Detailed explanation of RTSP streaming protocol