FFMPEG's RTP protocol (real-time transport protocol) 01

FFMPEG's RTP protocol (real-time transport protocol) 01

1 RTP and RTCP control protocol
1) RTP protocol function:
a standard data packet format used for real-time transmission of audio and video data on the network, such as streaming media, video conferencing, TV services, etc.

2) Why does RTP need to be used in conjunction with RTCP:
RTP can provide low-latency data transmission services, but it cannot guarantee that the data packets will still maintain the order when they arrive at the client, so RTCP must be used to complete flow control and congestion monitoring.

3) Why does RTP need a serial number?
In the OSI seven-layer model, the RTP protocol runs on the transport layer, and other underlying protocols can also work with RTP. The RTP data packet is UDP passed through the encapsulation header to form a UDP packet.
RTP itself does not contain any quality of service guarantee mechanism, so RTP cannot maintain the same transmission sequence of received data fields, nor does it verify whether the services provided by the underlying network are reliable. RTP adds a sequence number to the data packet, so that the receiver can reorder the data packet by detecting the sequence number after receiving the data packet to achieve order.

4) Why does RTP need a time stamp?
In streaming media data transmission, people usually face the same serious problem, that is, the time point of data transmission to the client is unpredictable, but the transmission of streaming media must ensure that the data can be delivered to the destination at the right time. Correct playback of data.
Therefore, in order to control the transmission of real-time data streams, the RTP protocol includes structures such as timestamps and serial numbers. The timestamp plays an important role in color correction in the process of streaming media transmission, and provides important data for the client. When sending an RTP message, the sender places the data used to record the sampling time in the data packet, called a time stamp. After the data packet reaches the receiving end through the network, the receiving end needs to extract the data end from the data packet , According to it to restore the original data sequence. RTP is only a transport layer protocol and is not responsible for synchronization. RTP leaves this part of the function to the application layer to complete, and has achieved the purpose of simplifying data processing and improving operating efficiency.
RTP data units are carried by UDP packets. Of course, it is not simply to encapsulate an RTP data unit in UDP packets, but to divide a frame of data into multiple UDP packets for transmission, which belongs to the same frame of data. The UDP packets will have exactly the same time stamp.

5) Why use RTCP:
During the RTP session, the role of the RTCP protocol is mainly to transmit exchange control information that monitors the correct rate of data transmission. During the session, the participants will send some relevant statistical information to other users, such as the number of data packets sent and the number of data packets that cannot be delivered. The sending of this information is usually carried out at the same time interval. It is periodically completed during the session.
The server will parse the data and increase or decrease the sending rate of the data accordingly. It is also possible to switch to other types of payloads. These changes will be made dynamically. RTP works in conjunction with RTCP to improve transmission efficiency by returning control information and reducing bandwidth consumption, thereby ensuring the smallest delay in data transmission.

6) The four main functions of
RTCP : RTCP mainly has four functions: congestion control, network monitoring and diagnosis of problems in the network by feeding back the transmission quality of the distributed data; because the SSRC (Synchronization Source Identification) is not static, when When network congestion occurs or when the participant’s program changes, the SSRC will be updated. We need to provide additional transport layer flags for the RTP source; adjust the sending speed of the RTCP packet to ensure that the data packet can reach the receiving end smoothly, so it needs to be based on Participant data to make adjustments; transfer session control information.

2 RTP file header format The
RTP message consists of two parts: the header and the payload. The header has a total of 12 bytes.

Insert picture description here
Version number (V): 2bit, used to mark the RTP version used.
Filling bit (P): 1bit, if this bit is set, the end of the RTP packet contains additional padding bytes.
Extension bit (X): 1bit, if this bit is set, an extension header is followed by the RTP fixed header.
CSRC counter (CC): 4bit, the number of CSRC after SSRC.
Mark bit (M): 1bit, the interpretation of this bit is in charge of the configuration file (Profile).
PayloadType: 7bit, which identifies the type of RTP payload. The details will be subscripted.
Serial number (SN): 16bit, each time an RTP packet is sent, the serial number increases by 1. The receiving end can detect packet loss and reconstruct the packet sequence accordingly.
Timestamp: 4byte, which records the sampling time of the first byte of the data in the packet. At the beginning of a session, the timestamp is initialized to an initial value. Even when there is no signal to send, the value of the timestamp will continue to increase over time. The clock frequency depends on the load data format and is described in the profile.

Synchronization source identifier (SSRC): 32 bits, the synchronization source refers to the source of the RTP packet stream. There cannot be two identical SSRC values ​​in the same RTP session. The identifier is randomly selected RFC1889 recommended MD5 random algorithm.

Contribution Source List (CSRC): 0-15 items, each with 32 bits, used to mark the source of all RTP packets that contribute to a new packet generated by an RTP mixer. These contributing SSRC identifiers are inserted into the table by the mixer. The SSRC identifiers are all listed so that the receiving end can correctly indicate the identity of the two parties in the conversation.

The 7-bit payload types are as follows:
RFC3551:
Insert picture description here

3 Code to implement RTP header format


#pragma pack(1)//1位字节对齐

//变量:数字表示位与操作,结构体字节的大小按照平时的方法计算即可
typedef struct RTP_FIXED_HEADER{
    
    
	/* byte 0 */
	unsigned char csrc_len:4;       /* expect 0 */
	unsigned char extension:1;      /* expect 1 */
	unsigned char padding:1;        /* expect 0 */
	unsigned char version:2;        /* expect 2 */
	/* byte 1 */
	unsigned char payload:7;
	unsigned char marker:1;        /* expect 1 */
	/* bytes 2, 3 */
	unsigned short seq_no;            
	/* bytes 4-7 */
	unsigned  long timestamp;        
	/* bytes 8-11 */
	unsigned long ssrc;            /* stream number is used here. */
} RTP_FIXED_HEADER;

Guess you like

Origin blog.csdn.net/weixin_44517656/article/details/108228019