Technology Sharing

[Audio and Video | RTSP] Detailed explanation of RTSP protocol and packet capture example analysis (detailed and not redundant)

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!!!


insert image description here

🎄一、概述

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.

insert image description here

RTP protocol: full nameReal-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 nameReal-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.


insert image description here

🎄二、RTSP 方法

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.AcceptThe 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.RangeThe 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.RangeThe 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.
    The server decides whether to store the recorded data inrequest-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.
    When the server sends a message to the client, it means that the client is notified to update the session information.
  • 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,SETUPPLAYTEARDOWNThe three commands are required in the RTSP process, and other methods are not required.ANNOUNCEGET_PARAMETERSET_PARAMETERThe 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.


insert image description here

🎄三、RTSP 的 请求报文 与 响应报文

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.

✨3.1 RTSP request message

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.
insert image description here

  • Request line: The request line contains a method, a request URI, and a protocol version, which are separated by spaces and ended with CRLF (i.e.: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.0orRTSP/2.0
    The following is an example of a complete request line:
    OPTIONS rtsp://192.168.3.225:554/wbc RTSP/1.0
    
  • Request header: The request header contains some additional information, such as: CSeq (a sequence number used to identify the request), Session ID (session identifier), Transport (transport protocol), etc. Each header field consists of a field name, a colon, and a field value, and each header field is separated by CRLF.
    The following is an example of a complete request header:
    CSeq: 2
    User-Agent: LibVLC/3.0.19 (LIVE555 Streaming Media v2016.11.28)
    
  • Request body: The request body is used to transmit additional data. The specific content of the request body depends on the RTSP method used in the request line. Note: After the request header, a blank line (CRLF) needs to be inserted to distinguish the request header from the request body. Most request messages do not have a request body.

✨3.2 RTSP response message

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.
insert image description here

  • Status line: The status line contains a protocol version, a status code, and a status text, which are separated by spaces and end with CRLF (ie: "rn").
    协议版本: 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.
    The following is an example response line:
    RTSP/1.0 200 OK
    
  • Response header: The response header contains similar information to the request header, such as CSeq (a sequence number used to identify the request), Session ID (session identifier), Transport (transport protocol), etc. The format of each response header field is the same as the request header, so it will not be repeated here.
    CSeq: 2
    Date: Wed, Feb 04 1970 03:25:10 GMT
    Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, GET_PARAMETER, SET_PARAMETER
    
  • Response body: Some RTSP responses (such as DESCRIBE) may contain a response body to transmit additional data. Note: After the response header, a blank line (CRLF) needs to be inserted toDistinguishing response headers from response bodies
    The following is an example of a complete response body.
    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
    

insert image description here

🎄四、RTSP 报文的常用字段

The response header of the RTSP message will contain some fields. The following are some commonly used fields:

  • Accept: Used to specify the entity data structure type that the client notifies the server of is acceptable to it. For example: Accept: application/sdp, and then the server returns its entity data structure type through the Content-Type field;
  • Accept-Encoding: The client informs the server of the data compression formats that it can accept, for example: Accept-Encoding: gzip, compress, br. The server will then inform the client of its choice through the Content-Encoding field.
  • Accept-Language: used by the client to inform the server of the languages ​​it can understand and its acceptance, for example: Accept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5, then the server will inform the client of its choice through the Content-Language field
  • Authorization: The client request header contains the credentials that the server uses to authenticate the user agent.
  • Bandwidth: used to describe the bandwidth value available to the client. Example: Bandwidth: 4000
  • Blocksize: This field is sent by the client to the media server to request a specific media packet size from the server. The server is free to use a smaller block size than requested. This packet size does not include lower layer headers such as IP, UDP, or RTP.
  • CSeq: Specifies the sequence number of the RTSP request response. Each RTSP request must contain a unique CSeq value so that the server can correctly identify and process the request. This sequence number increases with the request message. The server response must have a CSeq value to indicate which request it is responding to.
  • Cache-Control: Implements a caching mechanism by specifying directives. Cache directives are one-way, which means that the directives set in the request are not necessarily included in the response.
  • Conference: Notify the server not to change the conference ID of the same RTSP session
  • Connection: This field determines whether the network connection will be closed after the current transaction is completed. If the value is "keep-alive", the network connection is persistent and will not be closed, so that requests to the same server can continue to be completed on this connection or Connection: close.
  • Content-Length: This field indicates the length of the content after the double CRLF after the last header of the RTSP protocol. For example, in the server response DESCRIBE, it indicates the length of the sdp information
  • Content-Type: Tells the client the content type of the actual content returned
  • Date: Provides the date and time when the server generated the response, which helps the client determine the freshness of the response or perform time synchronization. The format of the Date field conforms to RFC 1123, for example: Sat, 06 Apr 2024 11:15:00 GMT.
  • User-Agent: This field is used to allow the other end of the network protocol to identify the application type, operating system, software developer, and version number of the user agent software that initiates the request.
  • Expires: Specifies the expiration date.
  • Rang: Used to specify a time range. SMPTE, NTP or clock time units can be used.
  • Session: The Session header field identifies an RTSP session. The Session ID is given by the server inSETUPOnce the client obtains the Session ID, it must include the Session ID in all subsequent Session operation request messages. For example: Session: 4581E0AE; timeout=65
  • Transport: The Transport header field contains a list of transport options that the client can accept, including transport protocol, address port, TTL, etc. The server also returns the specific options actually selected through this header field. For example: Transport: RTP/AVP/TCPunicast;destination=192.168.31.222;source=192.168.31.222;interleaved=0-1

insert image description here

🎄五、RTSP 流程抓包解析

Use wireshark to capture the network packets of RTSP streaming media, and you can see that the general process is as follows:
1. Client sendsOPTIONSMethod, the server responds;
2. Client sendsDESCRIBEMethod, the server responds;
3. Client sendsSETUPMethod, the server responds;
2. Client sendsPLAYMethod, the server responds;
2. Client sendsTEARDOWNMethod, the server responds;
insert image description here
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.

✨5.1、OPTION method

Get the available methods from the server:
insert image description here
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, useDatea specified date,PublicIndicates the method provided.


✨5.2、DESCRIBE method

Get from the serverrtsp://192.168.3.225:554/wbcA description of a media object, whereAcceptThe fields specify the description format:

insert image description here
The client sends the DESCRIBE method and usesCSeq To specify the request sequence number, useUser-Agent Indicate your agent.AcceptThe 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, useDatea specified date,Content-TypeIndicates that the content type is SDP,Content-LengthIndicates 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.


✨5.3, SETUP method

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;

insert image description here
The client sends the SETUP method and usesCSeq To specify the request sequence number, useUser-Agent Indicate your agent.TransportThe 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, useDatea specified date,TransportIndicate 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),SessionIndicates the session ID.

Notice
In this example, RTP is transmitted via UDP. Sometimes, RTP is transmitted via TCP.TransportThe 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/TCPIndicates that the RTP stream is transmitted via TCP. When this value appears, the message does not have the client_port field;
interleaved=0-1Indicates 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


✨5.4, PLAY method

The client actively notifies the server to start sending data using the mechanism specified by SETUP.

insert image description here
The client sends the PLAY method and usesCSeq To specify the request sequence number, useUser-Agent Indicate your agent.Sessionfield specifies the session ID,RangeThe 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; useDatespecify a date;RangeThe fields specify the start and end times of the playback;SessionThe field specifies the session ID;RTP-InfoThe 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.


✨5.5. TEARDOWN method

The client requests to stop sending the specified URL stream and release related resources.
insert image description here
The client sends the TEARDOWN method and usesCSeq To specify the request sequence number, useUser-Agent Indicate your agent.Sessionfield specifies the session ID.

The server will respond to this request using CSeq Indicates which request is being responded to; useDatespecify date.


insert image description here

🎄六、RTSP 响应错误码

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:

  • 1xx: Notice - The request has been received and is being processed
  • 2xx: Success - The request was successfully processed
  • 3xx: Redirect - Further action must be taken to complete the request
  • 4xx: Client Error - The request contained incorrect parameters or syntax that made it impossible to satisfy.
  • 5xx: Server Error - The server could not fulfill the client's request properly.

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 codeReason phraseResponse method
100ContinueAll
200SuccessAll
201CreatedRECORD
250Low on Storage SpaceRECORD
300Multiple ChoicesAll
301Moved PermanentlyAll
302Moved TemporarilyAll
303See OtherAll
305Use ProxyAll
400Bad RequestAll
401UnauthorizedAll
402Payment RequiredAll
403ForbiddenAll
404Not FoundAll
405Method Not AllowedAll
406Not AcceptableAll
407Proxy Authentication RequiredAll
408Request TimeoutAll
410GoneAll
411Length RequiredAll
412Precondition Failed DESCRIBESETUP
413Request Entity Too LargAll
414Request-URI Too LongAll
415Unsupported Media TypeAll
451Invalid parameterSETUP
452Illegal Conference IdentifierSETUP
453Not Enough BandwidthSETUP
454Session Not FoundAll
455Method Not Valid In This StateAll
456Header Field Not ValidAll
457Invalid RangePLAY
458Parameter Is Read-OnlySET_PARAMETER
459Aggregate Operation Not AllowedAll
460Only Aggregate Operation AllowedAll
461Unsupported TransportAll
462Destination UnreachableAll
500Internal Server ErrorAll
501Not ImplementedAll
502Bad GatewayAll
503Service UnavailableAll
504Gateway TimeoutAll
505RTSP Version Not SupportedAll
551Option not supportAll

insert image description here
如果文章有帮助的话,点赞👍、收藏⭐,支持一波,谢谢 😁😁😁

refer to:
Real-time Streaming Protocol—RTSP [Detailed Explanation]
Master RTSP request and response from scratch 1
Detailed explanation of RTSP streaming protocol