RTT计算方式
最近在看WebRTC的源码,在看RTT计算的时候,发现虽然RFC3550中规定了发送端如何计算RTT,但是网上的文章似乎都翻译的有点问题,虽然整个流程很简单,但是还是重新翻译一下,做一个记录。
RFC3550原文:
last SR timestamp (LSR): 32 bits
The middle 32 bits out of 64 in the NTP timestamp (as explained in
Section 4) received as part of the most recent RTCP sender report
(SR) packet from source SSRC_n. If no SR has been received yet,
the field is set to zero.
delay since last SR (DLSR): 32 bits
The delay, expressed in units of 1/65536 seconds, between
receiving the last SR packet from source SSRC_n and sending this
reception report block. If no SR packet has been received yet
from SSRC_n, the DLSR field is set to zero.
Let SSRC_r denote the receiver issuing this receiver report.
Source SSRC_n can compute the round-trip propagation delay to
SSRC_r by recording the time A when this reception report block is
received. It calculates the total round-trip time A-LSR using the
last SR timestamp (LSR) field, and then subtracting this field to
leave the round-trip propagation delay as (A - LSR - DLSR). This
is illustrated in Fig. 2. Times are shown in both a hexadecimal
representation of the 32-bit fields and the equivalent floating-
point decimal representation. Colons indicate a 32-bit field
divided into a 16-bit integer part and 16-bit fraction part.
This may be used as an approximate measure of distance to cluster
receivers, although some links have very asymmetric delays.
[10 Nov 1995 11:33:25.125 UTC] [10 Nov 1995 11:33:36.5 UTC]
n SR(n) A=b710:8000 (46864.500 s)
---------------------------------------------------------------->
v ^
ntp_sec =0xb44db705 v ^ dlsr=0x0005:4000 ( 5.250s)
ntp_frac=0x20000000 v ^ lsr =0xb705:2000 (46853.125s)
(3024992005.125 s) v ^
r v ^ RR(n)
---------------------------------------------------------------->
|<-DLSR->|
(5.250 s)
A 0xb710:8000 (46864.500 s)
DLSR -0x0005:4000 ( 5.250 s)
LSR -0xb705:2000 (46853.125 s)
-------------------------------
delay 0x0006:2000 ( 6.125 s)
Figure 2: Example for round-trip time computation
首先是参数定义:
最新的SR时间戳(简称LSR):32位,(接收端)从源SSRC_n(即发送端)接收到的最新的RTCP 发送端报告(简称SR)包中包含64位NTP时间戳,NTP时间戳中间的32位就是SR时间戳,LSR即是取最新的SR时间戳。如果没有收到任何SR,那么LSR置为0.
最新的SR到当前时间的延迟(简称DLSR):32位,这个延迟的单位是1/65536秒,定义是:从收到SSRC_n发送的最新SR包到回复这个报告之间的时间差值,如果没有收到任何SR,DLSR置为0.
以SSRC_r代表接收端,那么发送端SSRC_n可以通过记录时间A(时间A是SSRC_n收到报告回复的时间)计算出RTT。总的往返时间是A-LSR,然后用这个值减去延迟,最终结果也就是(A-LSR-DLSR)。
整个流程就是:
发送端将LSR放在SR中发送给接收端,接收端根据收到的SR获得LSR,并在发送SR回复的时候计算得出DLSR,然后将LSR与DLSR都放在SR回复中发送给发送端,发送端收到SR回复之后,根据接收时间A和SR回复中的LSR与DLSR来计算RTT。
PS:本文中的接收端和发送端针对的是视频传输,也就是视频接收端和视频发送端。
希望这篇文章能够帮助大家理解WebRTC中关于RTT计算的相关代码