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

SimulcastConsumer: improve ts extra offset stuff by having a map of extra…

SimulcastConsumer: improve ts extra offset stuff by having a map of extra offsets indexed by input timestamps.
上级 66f6a10f
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "RTC/Consumer.hpp" #include "RTC/Consumer.hpp"
#include "RTC/RtpStreamSend.hpp" #include "RTC/RtpStreamSend.hpp"
#include "RTC/SeqManager.hpp" #include "RTC/SeqManager.hpp"
#include <map>
namespace RTC namespace RTC
{ {
...@@ -85,8 +86,8 @@ namespace RTC ...@@ -85,8 +86,8 @@ namespace RTC
std::unique_ptr<RTC::Codecs::EncodingContext> encodingContext; std::unique_ptr<RTC::Codecs::EncodingContext> encodingContext;
bool externallyManagedBitrate{ false }; bool externallyManagedBitrate{ false };
uint32_t tsOffset{ 0 }; // RTP Timestamp offset. uint32_t tsOffset{ 0 }; // RTP Timestamp offset.
uint32_t tsExtraOffset{ 0 }; std::map<uint32_t, uint32_t> tsExtraOffsets;
uint32_t lastIncreasedOriginalTs{ 0 }; uint16_t tsExtraOffetPacketCount{ 0 };
}; };
/* Inline methods. */ /* Inline methods. */
......
...@@ -703,28 +703,28 @@ namespace RTC ...@@ -703,28 +703,28 @@ namespace RTC
this->tsOffset = newTs2 - ts1; this->tsOffset = newTs2 - ts1;
} }
// Reset tsExtraOffset and lastIncreasedOriginalTs. // Reset tsExtraOffsets and tsExtraOffetPacketCount.
this->tsExtraOffset = 0; this->tsExtraOffsets.clear();
this->lastIncreasedOriginalTs = 0; this->tsExtraOffetPacketCount = 0;
// When switching to a new stream it may happen that the timestamp of this // When switching to a new stream it may happen that the timestamp of this
// keyframe is lower than the last sent. If so, apply an extra offset to // keyframe is lower than the last sent. If so, apply an extra offset to
// "fix" it gradually. // "fix" it gradually.
if (packet->GetTimestamp() - this->tsOffset <= this->rtpStream->GetMaxPacketTs()) if (packet->GetTimestamp() - this->tsOffset <= this->rtpStream->GetMaxPacketTs())
{ {
this->tsExtraOffset = auto tsExtraOffset =
this->rtpStream->GetMaxPacketTs() - packet->GetTimestamp() + this->tsOffset + 1; this->rtpStream->GetMaxPacketTs() - packet->GetTimestamp() + this->tsOffset + 1;
this->lastIncreasedOriginalTs = packet->GetTimestamp();
MS_DEBUG_TAG( this->tsExtraOffsets[packet->GetTimestamp()] = tsExtraOffset;
MS_WARN_TAG(
simulcast, simulcast,
"ts extra offset needed [ts in:%" PRIu32 ", ts out:%" PRIu32 ", ts max out:%" PRIu32 "ts extra offset needed [ts in:%" PRIu32 ", ts out:%" PRIu32 ", ts max out:%" PRIu32
", ts offset:%" PRIu32 ", ts extra offset:%" PRIu32 "]", ", ts offset:%" PRIu32 "]",
packet->GetTimestamp(), packet->GetTimestamp(),
packet->GetTimestamp() - this->tsOffset, packet->GetTimestamp() - this->tsOffset,
this->rtpStream->GetMaxPacketTs(), this->rtpStream->GetMaxPacketTs(),
this->tsOffset, this->tsOffset);
this->tsExtraOffset);
} }
if (this->encodingContext) if (this->encodingContext)
...@@ -755,44 +755,49 @@ namespace RTC ...@@ -755,44 +755,49 @@ namespace RTC
uint16_t seq; uint16_t seq;
uint32_t timestamp = packet->GetTimestamp() - this->tsOffset; uint32_t timestamp = packet->GetTimestamp() - this->tsOffset;
if (this->tsExtraOffset) if (!this->tsExtraOffsets.empty())
{ {
if (timestamp > this->rtpStream->GetMaxPacketTs()) uint32_t tsExtraOffset{ 0 };
auto it = this->tsExtraOffsets.find(packet->GetTimestamp());
if (it != this->tsExtraOffsets.end())
{ {
MS_DEBUG_TAG( tsExtraOffset = it->second;
simulcast,
"ts extra offset done [ts in:%" PRIu32 ", ts out:%" PRIu32 ", ts offset:%" PRIu32 MS_DEBUG_DEV(
"ts extra offset mapping found [ts in:%" PRIu32 ", ts out:%" PRIu32
", ts extra offset:%" PRIu32 "]", ", ts extra offset:%" PRIu32 "]",
packet->GetTimestamp(), packet->GetTimestamp(),
timestamp, timestamp,
this->tsOffset, tsExtraOffset);
this->tsExtraOffset);
this->tsExtraOffset = 0;
this->lastIncreasedOriginalTs = 0;
} }
else if (packet->GetTimestamp() > this->lastIncreasedOriginalTs) else if (timestamp < this->rtpStream->GetMaxPacketTs())
{
this->tsExtraOffset = this->rtpStream->GetMaxPacketTs() - timestamp + 1;
this->lastIncreasedOriginalTs = packet->GetTimestamp();
}
if (this->tsExtraOffset)
{ {
timestamp += this->tsExtraOffset; tsExtraOffset = this->rtpStream->GetMaxPacketTs() - timestamp + 1;
this->tsExtraOffsets[packet->GetTimestamp()] = tsExtraOffset;
MS_DEBUG_TAG( MS_DEBUG_DEV(
simulcast, "ts extra offset generated [ts in:%" PRIu32 ", ts out:%" PRIu32
"ts extra offset applied [ts in:%" PRIu32 ", ts out:%" PRIu32 ", ts offset:%" PRIu32
", ts extra offset:%" PRIu32 "]", ", ts extra offset:%" PRIu32 "]",
packet->GetTimestamp(), packet->GetTimestamp(),
timestamp, timestamp,
this->tsOffset, tsExtraOffset);
this->tsExtraOffset);
} }
else
timestamp += tsExtraOffset;
// Reset if more than N packets.
// clang-format off
if (
(tsExtraOffset && ++this->tsExtraOffetPacketCount > 200) ||
this->tsExtraOffetPacketCount > 500
)
// clang-format on
{ {
this->lastIncreasedOriginalTs = 0; MS_DEBUG_DEV("cleaning ts extra map");
this->tsExtraOffsets.clear();
this->tsExtraOffetPacketCount = 0;
} }
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论