提交 e7c0fe11 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Consumer: Fix RTP sequence number when switching streams

上级 0d23c80c
......@@ -94,6 +94,7 @@ namespace RTC
RTC::RtpDataCounter retransmittedCounter;
// RTP sequence number and timestamp.
uint16_t seqNum{ 0 };
uint16_t maxSeqNum{ 0 };
uint32_t rtpTimestamp{ 0 };
uint16_t lastRecvSeqNum{ 0 };
uint32_t lastRecvRtpTimestamp{ 0 };
......
......@@ -24,7 +24,7 @@ namespace RTC
MS_TRACE();
// Initialize sequence number.
this->seqNum = static_cast<uint16_t>(Utils::Crypto::GetRandomUInt(0x00FF, 0xFFFF));
this->maxSeqNum = static_cast<uint16_t>(Utils::Crypto::GetRandomUInt(0x00FF, 0xFFFF));
// Set the RTCP report generation interval.
if (this->kind == RTC::Media::Kind::AUDIO)
......@@ -331,19 +331,38 @@ namespace RTC
// Check whether sequence number and timestamp sync is required.
if (this->syncRequired)
{
this->seqNum = this->maxRecvSeqNum + 1;
this->seqNum = ++this->maxSeqNum;
auto now = static_cast<uint32_t>(DepLibUV::GetTime());
if (now > this->rtpTimestamp)
this->rtpTimestamp = now;
this->maxRecvExtendedSeqNum = packet->GetExtendedSequenceNumber();
this->maxRecvSeqNum = packet->GetSequenceNumber();
this->syncRequired = false;
MS_DEBUG_TAG(
rtp,
"re-syncing Consumer stream [seqNum:%" PRIu16 ", maxRecvSeqNum:%" PRIu16
", maxRecvExtendedSeqNum:%" PRIu32 "]",
this->seqNum,
this->maxRecvSeqNum,
this->maxRecvExtendedSeqNum);
}
else
{
this->seqNum += packet->GetSequenceNumber() - this->lastRecvSeqNum;
this->rtpTimestamp += packet->GetTimestamp() - this->lastRecvRtpTimestamp;
// Update the max received sequence number if required.
if (packet->GetExtendedSequenceNumber() > this->maxRecvExtendedSeqNum)
{
this->maxRecvExtendedSeqNum = packet->GetExtendedSequenceNumber();
this->maxRecvSeqNum = packet->GetSequenceNumber();
this->maxSeqNum = this->seqNum;
}
}
// Save the received sequence number.
......@@ -355,13 +374,6 @@ namespace RTC
// Save real SSRC.
auto ssrc = packet->GetSsrc();
// Update the max received sequence number if required.
if (packet->GetExtendedSequenceNumber() > this->maxRecvExtendedSeqNum)
{
this->maxRecvExtendedSeqNum = packet->GetExtendedSequenceNumber();
this->maxRecvSeqNum = this->lastRecvSeqNum;
}
// Rewrite packet SSRC.
packet->SetSsrc(this->rtpParameters.encodings[0].ssrc);
......
......@@ -98,6 +98,10 @@ namespace RTC
uint16_t seq = packet->GetSequenceNumber();
uint16_t udelta = seq - this->maxSeq;
// If the new packet sequence number is greater than the max seen but not
// "so much bigger", accept it.
// NOTE: udelta also handles the case of a new cycle, this is:
// maxSeq:65536, seq:0 => udelta:1
if (udelta < MaxDropout)
{
// In order, with permissible gap.
......@@ -109,16 +113,16 @@ namespace RTC
this->maxSeq = seq;
}
// Too old packet received (older than the allowed misorder).
// Or to new packet (more than acceptable dropout).
else if (udelta <= RtpSeqMod - MaxMisorder)
{
// The sequence number made a very large jump.
// The sequence number made a very large jump. If two sequential packets
// arrive, accept the latter.
if (seq == this->badSeq)
{
/*
* Two sequential packets -- assume that the other side
* restarted without telling us so just re-sync
* (i.e., pretend this was the first packet).
*/
// Two sequential packets. Assume that the other side restarted without
// telling us so just re-sync (i.e., pretend this was the first packet).
MS_WARN_TAG(
rtp,
"too bad sequence number, re-syncing RTP [ssrc:%" PRIu32 ", seq:%" PRIu16 "]",
......@@ -140,6 +144,11 @@ namespace RTC
return false;
}
}
// Acceptable misorder.
else
{
// Do nothing.
}
this->received++;
......
......@@ -7,6 +7,10 @@
namespace RTC
{
/* Static. */
static constexpr uint32_t RtpSeqMod{ 1 << 16 };
/* Instance methods. */
RtpStreamRecv::RtpStreamRecv(Listener* listener, RTC::RtpStream::Params& params)
......@@ -133,6 +137,25 @@ namespace RTC
packet->SetExtendedSequenceNumber(
this->cycles + static_cast<uint32_t>(packet->GetSequenceNumber()));
// Set the extended sequence number into the packet.
// Ensure that the NACKed packet sequence is not higher than the one of
// the media stream.
//
// We are in the same cycle.
if (packet->GetSequenceNumber() <= this->maxSeq)
{
packet->SetExtendedSequenceNumber(
this->cycles + static_cast<uint32_t>(packet->GetSequenceNumber()));
}
// The nacket packet belongs to the previous cycle.
else
{
MS_DEBUG_TAG(rtx, "nacked packet belongs to previous seq cycle");
packet->SetExtendedSequenceNumber(
this->cycles - RtpSeqMod + static_cast<uint32_t>(packet->GetSequenceNumber()));
}
// Pass the packet to the NackGenerator.
if (this->params.useNack)
this->nackGenerator->ReceivePacket(packet);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论