webrtc code walk twenty-four (RTT delay calculation)

I. Overview

There are two ways to calculate RTT in WebRTC:
Based on the calculation of the media stream sender. Calculated by the information carried in the Sender Report (SR) and Receiver Report (RR).
Based on the calculation of the media stream receiver. The information of Receiver Reference Time Report Block and DLRR Report Block is carried through RTCP Extended ReportsRTCP (XR).
The principles of calculating RTT in the two methods are the same. Introduce the XR calculation method, because in some scenarios, one of the two endpoints only sends media data, and the other only receives media data. In this scenario, the receiving end will not send SR, which makes it impossible to calculate RTT. Therefore, the way RTCP XR is calculated at the receiving end is extended.

WebRTC obtains the RTT delay parameter in RTCPReceiver::OnPeriodicRttUpdate. When the client on this side is sending data, obtain the value calculated based on the media stream sender. When the client only receives media data, it obtains the value calculated based on the media stream receiving end.

 2. Calculation based on media stream sender 

delay Since Last SR = T22 - T21

RTT = T12 - T11 - (T22 - T21)

RTT = T12 - T11 - Delay Since Last SR

The sender needs to package the Sender Report (SR) and parse and process the Receiver Report (RR) protocol.

1. Sender Report (SR) agreement

NTP timestamp: 64 bits. Records the NTP timestamp when the SR was sent.

The wireshark example is as follows:

The RTCPSender::BuildSR code is implemented as follows: 

 

 2. Receiver Report (RR) protocol

last SR(LSR): 32 bits. The 32bit in the middle of the 64-bit NTP timestamp (the NTP timestamp refers to the absolute time, relative to the time experienced at 00:00:00 on January 1, 1900, in seconds. The complete NTP timestamp is represented by 64bits, and the left half of the 32bits represents an integer. The right half of 32bits represents the decimal, generally for the sake of compactness, the middle 32bits can be used for representation, at this time the integer and the decimal are represented by 16bits respectively). Records the NTP time of the last SR sent by the source SSRC_n, obtained from the NTP timestamp of the received SR record. Value is 0 if no SR was received.
delay since last SR (DLSR): 32 bits. In units of 1/65536 (2^16) seconds. It records the interval time from receiving the SR sent by the source SSRC_n last time to sending the RR currently. Value is 0 if no SR was received. 

The wireshark example is as follows:

  •   The media receiver encapsulates the RR packet
PlatformThread::StartThread(void * param) 
->PlatformThread::Run() 
->ProcessThreadImpl2::Run(void * obj)
->ProcessThreadImpl2::Process() 
->ModuleRtpRtcpImpl2::Process() //配置周期发送RTCP的RR报文TimeToSendRTCPReport&SendRTCP
->RTCPSender::SendRTCP
->RTCPSender::ComputeCompoundRTCPPacket
->RTCPSender::BuildRR
->RTCPSender::CreateReportBlocks

The receiving end sends RTCP RR packets periodically. last SR is the NTP timestamp of the last RTCP SR message received by the receiver.

 

  •  The media sender analyzes and calculates the RTT value
ModuleRtpRtcpImpl2::IncomingRtcpPacket
->RTCPReceiver::IncomingPacket
->RTCPReceiver::IncomingPacket
->RTCPReceiver::ParseCompoundPacket
->RTCPReceiver::HandleReceiverReport
->RTCPReceiver::HandleReportBlock

The RTCPReceiver::HandleReportBlock function obtains the current NTP time of the sender, and then extracts last_sr and delay_since_last_sr from the RR message to calculate the RTT delay time.

3. Calculation based on the media stream receiver

1、Receiver Reference Time Report Block 

 

Wireshark example:

  2、DLRR Report Block

Wireshark example:

The receiver calculates the RTT delay in the RTCPReceiver::HandleXrDlrrReportBlock function. 

 

 reference:

WebRTC Research: Round-trip Delay of Statistical Parameters - Sword Crazy

Guess you like

Origin blog.csdn.net/CrystalShaw/article/details/125934629