Use NTP to calculate RTP timestamps
This commit is contained in:
parent
0102bb2801
commit
3195a271e9
|
@ -42,7 +42,7 @@ namespace uvg_rtp {
|
|||
uint8_t payload_;
|
||||
|
||||
uint32_t clock_rate_;
|
||||
uint32_t wc_start_;
|
||||
uint64_t wc_start_;
|
||||
uvg_rtp::clock::hrc::hrc_t wc_start_2;
|
||||
|
||||
size_t sent_pkts_;
|
||||
|
|
17
src/clock.cc
17
src/clock.cc
|
@ -6,20 +6,23 @@
|
|||
#endif
|
||||
|
||||
#include "clock.hh"
|
||||
#include <stdio.h>
|
||||
|
||||
static const uint64_t EPOCH = 2208988800ULL;
|
||||
static const uint64_t NTP_SCALE_FRAC = 4294967296ULL;
|
||||
|
||||
static inline uint32_t ntp_diff_ms(uint64_t t1, uint64_t t2)
|
||||
{
|
||||
uint32_t s_diff = (t1 >> 32) - (t2 >> 32);
|
||||
uint32_t us_diff = (((t1 & 0xffffffff) - (t2 & 0xffffffff)) * 1000000UL) >> 32;
|
||||
uint32_t s1 = (t1 >> 32) & 0xffffffff;
|
||||
uint32_t s2 = (t2 >> 32) & 0xffffffff;
|
||||
uint64_t us1 = ((t1 & 0xffffffff) * 1000000UL) / NTP_SCALE_FRAC;
|
||||
uint64_t us2 = ((t2 & 0xffffffff) * 1000000UL) / NTP_SCALE_FRAC;
|
||||
|
||||
return s_diff / 1000 + (us_diff / 1000);
|
||||
return (((s1 - s2) * 1000000) + ((us1 - us2))) / 1000;
|
||||
}
|
||||
|
||||
uint64_t uvg_rtp::clock::ntp::now()
|
||||
{
|
||||
static const uint64_t EPOCH = 2208988800ULL;
|
||||
static const uint64_t NTP_SCALE_FRAC = 4294967296ULL;
|
||||
|
||||
struct timeval tv;
|
||||
#ifdef _WIN32
|
||||
uvg_rtp::clock::gettimeofday(&tv, NULL);
|
||||
|
@ -30,7 +33,7 @@ uint64_t uvg_rtp::clock::ntp::now()
|
|||
uint64_t tv_ntp, tv_usecs;
|
||||
|
||||
tv_ntp = tv.tv_sec + EPOCH;
|
||||
tv_usecs = (NTP_SCALE_FRAC * tv.tv_usec) / 1000000UL;
|
||||
tv_usecs = (float)(NTP_SCALE_FRAC * tv.tv_usec) / (float)1000000UL;
|
||||
|
||||
return (tv_ntp << 32) | tv_usecs;
|
||||
}
|
||||
|
|
|
@ -90,10 +90,9 @@ void uvg_rtp::rtp::fill_header(uint8_t *buffer)
|
|||
|
||||
/* This is the first RTP message, get wall clock reading (t = 0)
|
||||
* and generate random RTP timestamp for this reading */
|
||||
if (wc_start_ == 0) {
|
||||
if (!wc_start_) {
|
||||
ts_ = uvg_rtp::random::generate_32();
|
||||
wc_start_2 = uvg_rtp::clock::hrc::now();
|
||||
wc_start_ = 1;
|
||||
wc_start_ = uvg_rtp::clock::ntp::now();
|
||||
}
|
||||
|
||||
buffer[0] = 2 << 6; // RTP version
|
||||
|
@ -105,10 +104,11 @@ void uvg_rtp::rtp::fill_header(uint8_t *buffer)
|
|||
if (timestamp_ == INVALID_TS) {
|
||||
*(uint32_t *)&buffer[4] = htonl(
|
||||
ts_
|
||||
+ uvg_rtp::clock::hrc::diff_now(wc_start_2)
|
||||
+ uvg_rtp::clock::ntp::diff_now(wc_start_)
|
||||
* clock_rate_
|
||||
/ 1000
|
||||
);
|
||||
|
||||
} else {
|
||||
*(uint32_t *)&buffer[4] = htonl(timestamp_);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue