提交 87f183b7 authored 作者: José Luis Millán's avatar José Luis Millán

Fix #155. RTCP: rely on LibUV timestamp for time calculations

上级 dc69b20c
...@@ -63,7 +63,7 @@ namespace RTC ...@@ -63,7 +63,7 @@ namespace RTC
using Buffer = std::list<BufferItem>; using Buffer = std::list<BufferItem>;
Buffer buffer; Buffer buffer;
// Stats. // Stats.
uint32_t rtt{ 0 }; float rtt{ 0 };
// Others. // Others.
bool healthy{ true }; bool healthy{ true };
......
...@@ -262,7 +262,7 @@ namespace Utils ...@@ -262,7 +262,7 @@ namespace Utils
// Seconds from Jan 1, 1900 to Jan 1, 1970. // Seconds from Jan 1, 1900 to Jan 1, 1970.
static constexpr uint32_t UnixNtpOffset{ 0x83AA7E80 }; static constexpr uint32_t UnixNtpOffset{ 0x83AA7E80 };
// NTP fractional unit. // NTP fractional unit.
static constexpr double NtpFractionalUnit{ 1LL << 32 }; static constexpr uint64_t NtpFractionalUnit{ 1LL << 32 };
public: public:
struct Ntp struct Ntp
...@@ -271,26 +271,20 @@ namespace Utils ...@@ -271,26 +271,20 @@ namespace Utils
uint32_t fractions; uint32_t fractions;
}; };
static void CurrentTimeNtp(Ntp& ntp); static Time::Ntp TimeMs2Ntp(uint64_t ms);
static void UnixTime2Ntp(struct timeval* unixTime, Ntp& ntp);
static bool IsNewerTimestamp(uint32_t timestamp, uint32_t prevTimestamp); static bool IsNewerTimestamp(uint32_t timestamp, uint32_t prevTimestamp);
static uint32_t LatestTimestamp(uint32_t timestamp1, uint32_t timestamp2); static uint32_t LatestTimestamp(uint32_t timestamp1, uint32_t timestamp2);
}; };
inline void Time::CurrentTimeNtp(Ntp& ntp) inline Time::Ntp Time::TimeMs2Ntp(uint64_t ms)
{ {
struct timeval tv; Ntp ntp;
gettimeofday(&tv, nullptr); ntp.seconds = ms / 1000;
UnixTime2Ntp(&tv, ntp);
}
inline void Time::UnixTime2Ntp(struct timeval* unixTime, Ntp& ntp)
{
ntp.seconds = unixTime->tv_sec + UnixNtpOffset;
ntp.fractions = ntp.fractions =
static_cast<uint32_t>(static_cast<double>(unixTime->tv_usec) * NtpFractionalUnit * 1.0e-6); static_cast<uint32_t>(static_cast<double>(ms % 1000) / 1000 * Utils::Time::NtpFractionalUnit);
return ntp;
} }
inline bool Time::IsNewerTimestamp(uint32_t timestamp, uint32_t prevTimestamp) inline bool Time::IsNewerTimestamp(uint32_t timestamp, uint32_t prevTimestamp)
......
...@@ -45,7 +45,7 @@ namespace RTC ...@@ -45,7 +45,7 @@ namespace RTC
Json::Value json = RtpStream::GetStats(); Json::Value json = RtpStream::GetStats();
json[JsonStringType] = Type; json[JsonStringType] = Type;
json[JsonStringRtt] = Json::UInt{ this->rtt }; json[JsonStringRtt] = Json::UInt{ static_cast<uint32_t>(this->rtt) };
return json; return json;
} }
...@@ -71,20 +71,27 @@ namespace RTC ...@@ -71,20 +71,27 @@ namespace RTC
/* Calculate RTT. */ /* Calculate RTT. */
// Get the compact NTP representation of the current timestamp. // Get the NTP representation of the current timestamp.
Utils::Time::Ntp nowNtp{}; uint64_t now = DepLibUV::GetTime();
Utils::Time::CurrentTimeNtp(nowNtp); auto ntp = Utils::Time::TimeMs2Ntp(now);
uint32_t nowCompactNtp = (nowNtp.seconds & 0x0000FFFF) << 16;
nowCompactNtp |= (nowNtp.fractions & 0xFFFF0000) >> 16; // Get the compact NTP representation of the current timestamp.
uint32_t compactNtp = (ntp.seconds & 0x0000FFFF) << 16;
compactNtp |= (ntp.fractions & 0xFFFF0000) >> 16;
uint32_t lastSr = report->GetLastSenderReport(); uint32_t lastSr = report->GetLastSenderReport();
uint32_t dlsr = report->GetDelaySinceLastSenderReport(); uint32_t dlsr = report->GetDelaySinceLastSenderReport();
// RTT in 1/2^16 seconds.
uint32_t rtt = nowCompactNtp - dlsr - lastSr; // RTT in 1/2^16 second fractions.
uint32_t rtt = 0;
if (compactNtp > dlsr + lastSr)
rtt = compactNtp - dlsr - lastSr;
else
rtt = 0;
// RTT in milliseconds. // RTT in milliseconds.
this->rtt = ((rtt >> 16) * 1000); this->rtt = (rtt >> 16) * 1000;
this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000; this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000;
this->packetsLost = report->GetTotalLost(); this->packetsLost = report->GetTotalLost();
...@@ -284,38 +291,12 @@ namespace RTC ...@@ -284,38 +291,12 @@ namespace RTC
report->SetOctetCount(this->transmissionCounter.GetBytes()); report->SetOctetCount(this->transmissionCounter.GetBytes());
// Calculate RTP timestamp diff between now and last received RTP packet. // Calculate RTP timestamp diff between now and last received RTP packet.
auto diffMs = static_cast<uint32_t>(now - this->maxPacketMs); int32_t diffMs = static_cast<int32_t>(now - this->maxPacketMs);
uint32_t diffTs = diffMs * this->params.clockRate / 1000; int32_t diffTs = diffMs * this->params.clockRate / 1000;
// Get the NTP representation of the current timestamp.
Utils::Time::Ntp ntp{}; auto ntp = Utils::Time::TimeMs2Ntp(now);
struct timeval unixTime
{
};
uint64_t unixTimeMs;
gettimeofday(&unixTime, nullptr);
// Convert unix time to millisecods.
unixTimeMs = unixTime.tv_sec * 1000 + unixTime.tv_usec / 1000;
if (now >= this->maxPacketMs)
{
unixTimeMs += diffMs;
report->SetRtpTs(this->maxPacketTs + diffTs); report->SetRtpTs(this->maxPacketTs + diffTs);
}
else
{
unixTimeMs -= diffMs;
report->SetRtpTs(this->maxPacketTs - diffTs);
}
// Convert milliseconds to unix time.
unixTime.tv_sec = unixTimeMs / 1000;
unixTime.tv_usec = (unixTimeMs % 1000) * 1000;
// Convert unix time to NTP.
Utils::Time::UnixTime2Ntp(&unixTime, ntp);
report->SetNtpSec(ntp.seconds); report->SetNtpSec(ntp.seconds);
report->SetNtpFrac(ntp.fractions); report->SetNtpFrac(ntp.fractions);
......
...@@ -578,7 +578,7 @@ namespace RTC ...@@ -578,7 +578,7 @@ namespace RTC
if (timer == this->rtcpTimer) if (timer == this->rtcpTimer)
{ {
uint64_t interval = RTC::RTCP::MaxVideoIntervalMs; uint64_t interval = RTC::RTCP::MaxVideoIntervalMs;
uint32_t now = DepLibUV::GetTime(); uint64_t now = DepLibUV::GetTime();
SendRtcp(now); SendRtcp(now);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论