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

lipsync: Fix RTP SR generation

上级 5af523b2
......@@ -272,6 +272,7 @@ namespace Utils
};
static void CurrentTimeNtp(Ntp& ntp);
static void UnixTime2Ntp(struct timeval* unixTime, Ntp& ntp);
static bool IsNewerTimestamp(uint32_t timestamp, uint32_t prevTimestamp);
static uint32_t LatestTimestamp(uint32_t timestamp1, uint32_t timestamp2);
};
......@@ -282,9 +283,14 @@ namespace Utils
gettimeofday(&tv, nullptr);
ntp.seconds = tv.tv_sec + UnixNtpOffset;
UnixTime2Ntp(&tv, ntp);
}
inline void Time::UnixTime2Ntp(struct timeval* unixTime, Ntp& ntp)
{
ntp.seconds = unixTime->tv_sec + UnixNtpOffset;
ntp.fractions =
static_cast<uint32_t>(static_cast<double>(tv.tv_usec) * NtpFractionalUnit * 1.0e-6);
static_cast<uint32_t>(static_cast<double>(unixTime->tv_usec) * NtpFractionalUnit * 1.0e-6);
}
inline bool Time::IsNewerTimestamp(uint32_t timestamp, uint32_t prevTimestamp)
......
......@@ -110,6 +110,9 @@ namespace RTC
this->started = true;
this->maxSeq = seq - 1;
this->maxPacketTs = packet->GetTimestamp();
this->maxPacketMs = DepLibUV::GetTime();
}
// If not a valid packet ignore it.
......@@ -130,8 +133,12 @@ namespace RTC
// Update highest seen RTP timestamp.
if (SeqManager<uint32_t>::IsSeqHigherThan(packet->GetTimestamp(), this->maxPacketTs))
{
// Calculate time diff between this and previous highest RTP timestamp.
uint32_t diffTs = packet->GetTimestamp() - this->maxPacketTs;
uint32_t diffMs = diffTs * 1000 / this->params.clockRate;
this->maxPacketTs = packet->GetTimestamp();
this->maxPacketMs = DepLibUV::GetTime();
this->maxPacketMs += diffMs;
}
return true;
......@@ -157,7 +164,6 @@ namespace RTC
this->baseSeq = seq;
this->maxSeq = seq;
this->badSeq = RtpSeqMod + 1; // So seq == badSeq is false.
this->maxPacketTs = 0; // Also reset the highest seen RTP timestamp.
}
bool RtpStream::UpdateSeq(RTC::RtpPacket* packet)
......@@ -199,6 +205,9 @@ namespace RTC
packet->GetSequenceNumber());
InitSeq(seq);
this->maxPacketTs = packet->GetTimestamp();
this->maxPacketMs = DepLibUV::GetTime();
}
else
{
......
......@@ -277,21 +277,43 @@ namespace RTC
return nullptr;
auto report = new RTC::RTCP::SenderReport();
report->SetPacketCount(this->transmissionCounter.GetPacketCount());
report->SetOctetCount(this->transmissionCounter.GetBytes());
Utils::Time::Ntp ntp{};
Utils::Time::CurrentTimeNtp(ntp);
report->SetNtpSec(ntp.seconds);
report->SetNtpFrac(ntp.fractions);
// Calculate RTP timestamp diff between now and last received RTP packet.
auto diffMs = static_cast<uint32_t>(now - this->maxPacketMs);
uint32_t diffMs = static_cast<uint32_t>(now - this->maxPacketMs);
uint32_t diffTs = diffMs * this->params.clockRate / 1000;
report->SetRtpTs(this->maxPacketTs + diffTs);
Utils::Time::Ntp ntp{};
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);
}
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->SetNtpFrac(ntp.fractions);
return report;
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论