# RTCP中的NTP的时间计算方法

615人阅读 评论(0)

NTP的时间戳有MSW和LSW组成， MSW好算，以秒为单位，LSW就头痛了，查了RTP的文档，讲得很模糊，NTP(RFC1305)中只讲单位大约是200 picoseconds，但我试了用200 picoseconds为单位不行，还是闪。

1，000，000，000，000/4294967296 = 232.83064365386962890625

That's it！！

/**
* @return NTP 64-bits timestamp in host byte order.
*/
uint64_t NTPtime64 (void)
{
struct timespec ts;
#if defined (CLOCK_REALTIME)
clock_gettime (CLOCK_REALTIME, &ts);
#else
{
struct timeval tv;
gettimeofday (&tv, NULL);
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
}
#endif

/* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
t /= 1000000000;

/* There is 70 years (incl. 17 leap ones) offset to the Unix Epoch.
* No leap seconds during that period since they were not invented yet.
*/
assert (t < 0x100000000);
t |= ((70LL * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
return t;
}

live555中的处理

// ASSERT: fSink != NULL

enqueueCommonReportPrefix(RTCP_PT_SR, fSink->SSRC(),
5 /* extra words in a SR */);

// Now, add the 'sender info' for our sink

// Insert the NTP and RTP timestamps for the 'wallclock time':
struct timeval timeNow;
gettimeofday(&timeNow, NULL);

printf("mike======>change SR\n");

fOutBuf->enqueueWord(timeNow.tv_sec + 0x83AA7E80);
// NTP timestamp most-significant word (1970 epoch -> 1900 epoch)
double fractionalPart = (timeNow.tv_usec/15625.0)*0x04000000; // 2^32/10^6
fOutBuf->enqueueWord((unsigned)(fractionalPart+0.5));
// NTP timestamp least-significant word
unsigned rtpTimestamp = fSink->convertToRTPTimestamp(timeNow);
fOutBuf->enqueueWord(rtpTimestamp); // RTP ts

// Insert the packet and byte counts:
fOutBuf->enqueueWord(fSink->packetCount());
fOutBuf->enqueueWord(fSink->octetCount());

enqueueCommonReportSuffix();
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：166719次
• 积分：2648
• 等级：
• 排名：第13787名
• 原创：74篇
• 转载：182篇
• 译文：0篇
• 评论：10条
评论排行
最新评论