RTCP包中的NTP Time 计算

转载:http://blog.csdn.net/chinabinlang/article/details/40110037     谢谢


关于 RTCP中的NTP Time计算有很多人不清楚,还好因为有很多开源的rtp库,这里可以参考ORTP库中的算法

//oRTP开源工程

uint64_t ortp_timeval_to_ntp(const struct timeval *tv)

{
uint64_t msw;
uint64_t lsw;
msw=tv->tv_sec + 0x83AA7E80; /* 0x83AA7E80 is the number of seconds from 1900 to 1970 */
lsw=(uint32_t)((double)tv->tv_usec*(double)(((uint64_t)1)<<32)*1.0e-6);
return msw<<32 | lsw; 
}

static void sender_info_init(sender_info_t *info, RtpSession *session)

{
struct timeval tv;
uint64_t ntp;
ortp_gettimeofday(&tv,NULL);
ntp=ortp_timeval_to_ntp(&tv);
info->ntp_timestamp_msw=htonl(ntp >>32);
info->ntp_timestamp_lsw=htonl(ntp & 0xFFFFFFFF);

info->rtp_timestamp=htonl(session->rtp.snd_last_ts);
info->senders_packet_count=(uint32_t) htonl((u_long) session->rtp.stats.packet_sent);
info->senders_octet_count=(uint32_t) htonl((u_long) session->rtp.sent_payload_bytes);
session->rtp.last_rtcp_packet_count=session->rtp.stats.packet_sent;
}


关于ortp_gettimeofday(&tv,NULL);这个函数可以在port.c文件中找到;

关于ortp的一些文章可以再网上找到,也可以参考这篇文章:http://blog.csdn.net/tjhon/article/details/17166627


//doubango开源工程 tsk_time.c文件,更多详细信息可以参考这个文件;

// http://en.wikipedia.org/wiki/Network_Time_Protocol
uint64_t tsk_time_ntp()
{
struct timeval tv;
gettimeofday(&tv, (struct timezone *)tsk_null);
return tsk_time_get_ntp_ms(&tv);
}


uint64_t tsk_time_get_ntp_ms(const struct timeval *tv)
{
static const unsigned long __epoch = 2208988800UL;
static const double __ntp_scale_frac = 4294967295.0;

uint64_t tv_ntp;
uint64_t tv_usecs;


if(!tv){
TSK_DEBUG_ERROR("Invalid parameter");
return 0;
}
tv_ntp = tv->tv_sec + __epoch;
#if 0 // ARM floating point calc issue (__aeabi_d2uiz)
tv_usecs = (tv->tv_usec * 1e-6) * __ntp_scale_frac;
return ((tv_ntp << 32) | (uint32_t)tv_usecs);
#else
tv_usecs = ((uint64_t)tv->tv_usec * (uint64_t)__ntp_scale_frac) / (uint64_t)1000000;
return ((((uint64_t)tv_ntp) << 32) | (uint32_t)tv_usecs);
#endif
}


转:

RTCP发送者报文中, 有一个64bits的时间戳, 分为2个32位字段msw和lsw.
msw的值简单来说, 就是从1970年开始至现在的秒值.
lsw的值简单来说, 它的单位是 232.8 picosecond


转:

在RTCP中ntp time存放在8个字节中,分为:MSW和LSW,分别占用4个字节

MSW: 单位是,不过是从1900年1月1日算起,所以使用gettimeofday后需要加上:1900-1970的时间差:

msw = (70LL * 365 + 17) * 24 * 60 * 60 + tv.tv_sec;

LSW:单位是232皮秒,其中

1s = 10^12ps,所以

lsw = (tv.tv_usec << 32) / 1000000;

关于为什么是232,别人已经解释的很清楚了~~~


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值