我开发了一个服务器和客户端应用程序,用于使用RTSP将视频帧从一端流式传输到另一端.现在,为了收集有助于我改进应用程序的统计数据,我需要测量发送帧和接收帧之间经过的时间.
目前我正在使用以下公式:
Client_Receive_Timestamp - Server_Send_Timestamp = Elapsed_Time
问题
在我看来,经过的时间大约是100-200ms.我认为原因是服务器时钟和客户端时钟不同步,差异大约为100-200ms.
题
如何准确测量两个mashines之间的经过时间?
主题Accurately measuring elapsed time between machines建议计算往返延迟.但是,我不能使用此解决方案,因为客户端不请求帧.它只是通过RTSP接收帧.
解决方法
假设
>您只想测量单程旅行的延迟
>每台机器提供一个clock of acceptable resolution
>两个时钟都可以是synchronized to acceptable accuracy
然后您可以简单地从“接收的时间戳”中减去“发送的时间戳”以获得延迟持续时间. observed error将小于两个时钟误差的总和.如果时间尺度足够小(可能小于一小时),你可以合理地忽略slew效果.
如果ntpd尚未在两台计算机上运行,并且您具有必要的权限,则可以
$sudo ntpdate -v pool.ntp.org
强制与公共可用时间服务器池同步.
然后你可以使用c 11 high_resolution_clock来计算持续时间:
/* hrc.cc */
#include <chrono>
#include <iostream>
int main(int,char**){
using std::chrono::high_resolution_clock;
// send something
high_resolution_clock::time_point start = high_resolution_clock::Now();
std::cout << "time this" << std::endl ;
// receive something
high_resolution_clock::time_point stop = high_resolution_clock::Now();
std::cout
<< "duration == "
<< std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start).count()
<< "ns"
<< std::endl
;
return 0;
}
这是上一个示例在我的系统上的样子:
$make hrc && ./hrc c++ hrc.cc -o hrc time this duration == 32010ns