Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make transport-cc feedback work similarly to libwebrtc #1088

Merged
merged 34 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
cfe202b
fix tcc-feedback
penguinol May 26, 2023
08d14a9
update format
penguinol May 26, 2023
601736f
Update TransportCongestionControlServer.cpp
penguinol May 26, 2023
86676f6
Update TransportCongestionControlServer.hpp
penguinol May 26, 2023
e7808c1
Update worker/src/RTC/TransportCongestionControlServer.cpp
penguinol Jun 25, 2023
186d72f
add comment
penguinol Jul 11, 2023
ecf03af
Merge branch 'fix_tcc_feedback' of https://github.com/penguinol/media…
penguinol Jul 11, 2023
1e3e9c0
Merge branch 'v3' into fix_tcc_feedback
ibc Jan 1, 2024
e3c935e
Update worker/src/RTC/TransportCongestionControlServer.cpp
penguinol Jan 2, 2024
d0c4c02
prepare for test
penguinol Jan 8, 2024
56ab0f4
Merge branch 'fix_tcc_feedback' of https://github.com/penguinol/media…
penguinol Jan 8, 2024
9e69ee0
fix tcc feedback
penguinol Jan 18, 2024
ba0b2b8
cosmetic
penguinol Jan 18, 2024
66d435a
Update TestFeedbackRtpTransport.cpp
penguinol Jan 18, 2024
3ac5a05
optimize
penguinol Jan 23, 2024
8bf5758
add test case
penguinol Jan 24, 2024
3d3a054
try fix compile error
penguinol Jan 24, 2024
9ef5cf0
fix test case
penguinol Jan 24, 2024
fd44165
Update worker/src/RTC/RTCP/FeedbackRtpTransport.cpp
penguinol Jan 30, 2024
381b889
Update worker/src/RTC/TransportCongestionControlServer.cpp
penguinol Jan 30, 2024
882e6d9
Update worker/src/RTC/TransportCongestionControlServer.cpp
penguinol Jan 30, 2024
b6a9729
Apply suggestions from code review
penguinol Jan 30, 2024
096e48f
Update TestFeedbackRtpTransport.cpp
penguinol Jan 30, 2024
e027f54
Merge branch 'versatica:v3' into fix_tcc_feedback
penguinol Jan 31, 2024
2bc46be
add baseSet and other optimize
penguinol Jan 31, 2024
5e25d10
Merge branch 'fix_tcc_feedback' of https://github.com/penguinol/media…
penguinol Jan 31, 2024
e9aeb16
Update CHANGELOG.md
penguinol Jan 31, 2024
ea3b19c
Apply suggestions from code review
penguinol Jan 31, 2024
4bd33f3
Apply suggestions from code review
penguinol Jan 31, 2024
a885f48
Update CHANGELOG.md
penguinol Feb 1, 2024
4856fa7
Update TransportCongestionControlServer.cpp
penguinol Feb 1, 2024
13871d2
Merge branch 'fix_tcc_feedback' of https://github.com/penguinol/media…
penguinol Feb 1, 2024
1137852
Update TransportCongestionControlServer.cpp
penguinol Feb 1, 2024
4b290d9
Merge branch 'v3' into fix_tcc_feedback
ibc Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### NEXT

- Make transport-cc feedback work similarly to libwebrtc ([PR #1088](https://github.com/versatica/mediasoup/pull/1088) by @penguinol).
- `TransportListenInfo`: announced ip can also be a hostname ([PR #1322](https://github.com/versatica/mediasoup/pull/1322)).

### 3.13.17
Expand Down
3 changes: 3 additions & 0 deletions worker/include/RTC/RTCP/FeedbackRtpTransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ namespace RTC
~FeedbackRtpTransportPacket() override;

public:
void SetBase(uint16_t sequenceNumber, uint64_t timestamp);
AddPacketResult AddPacket(uint16_t sequenceNumber, uint64_t timestamp, size_t maxRtcpPacketLen);
// Just for locally generated packets.
void Finish();
Expand Down Expand Up @@ -317,6 +318,8 @@ namespace RTC
void AddPendingChunks();

private:
// Whether baseSequenceNumber has been set.
bool baseSet{ false };
uint16_t baseSequenceNumber{ 0u };
// 24 bits signed integer.
int32_t referenceTime{ 0 };
Expand Down
9 changes: 8 additions & 1 deletion worker/include/RTC/TransportCongestionControlServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "RTC/RTCP/FeedbackRtpTransport.hpp"
#include "RTC/RTCP/Packet.hpp"
#include "RTC/RtpPacket.hpp"
#include "RTC/SeqManager.hpp"
#include "handles/TimerHandle.hpp"
#include <libwebrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h>
#include <deque>
Expand Down Expand Up @@ -54,10 +55,12 @@ namespace RTC
double GetPacketLoss() const;
void IncomingPacket(uint64_t nowMs, const RTC::RtpPacket* packet);
void SetMaxIncomingBitrate(uint32_t bitrate);
void FillAndSendTransportCcFeedback();
ibc marked this conversation as resolved.
Show resolved Hide resolved

private:
void SendTransportCcFeedback();
void MaySendLimitationRembFeedback();
void MayDropOldPacketArrivalTimes(uint16_t seqNum, uint64_t nowMs);
void MaySendLimitationRembFeedback(uint64_t nowMs);
void UpdatePacketLoss(double packetLoss);

/* Pure virtual methods inherited from webrtc::RemoteBitrateEstimator::Listener. */
Expand Down Expand Up @@ -89,6 +92,10 @@ namespace RTC
uint8_t unlimitedRembCounter{ 0u };
std::deque<double> packetLossHistory;
double packetLoss{ 0 };
// Whether any packet with transport wide sequence number was received.
bool transportWideSeqNumberReceived{ false };
uint16_t transportCcFeedbackWideSeqNumStart{ 0u };
std::map<uint16_t, uint64_t, RTC::SeqManager<uint16_t>::SeqLowerThan> mapPacketArrivalTimes;
};
} // namespace RTC

Expand Down
1 change: 1 addition & 0 deletions worker/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ test_sources = [
'test/src/RTC/TestSeqManager.cpp',
'test/src/RTC/TestTrendCalculator.cpp',
'test/src/RTC/TestRtpEncodingParameters.cpp',
'test/src/RTC/TestTransportCongestionControlServer.cpp',
'test/src/RTC/Codecs/TestVP8.cpp',
'test/src/RTC/Codecs/TestVP9.cpp',
'test/src/RTC/Codecs/TestH264.cpp',
Expand Down
23 changes: 12 additions & 11 deletions worker/src/RTC/RTCP/FeedbackRtpTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,25 @@ namespace RTC
return offset;
}

void FeedbackRtpTransportPacket::SetBase(uint16_t sequenceNumber, uint64_t timestamp)
{
MS_TRACE();

this->baseSet = true;
this->baseSequenceNumber = sequenceNumber;
this->referenceTime = static_cast<int32_t>((timestamp & 0x1FFFFFC0) / 64);
this->latestSequenceNumber = sequenceNumber - 1;
this->latestTimestamp = (timestamp >> 6) * 64; // IMPORTANT: Loose precision.
}

FeedbackRtpTransportPacket::AddPacketResult FeedbackRtpTransportPacket::AddPacket(
uint16_t sequenceNumber, uint64_t timestamp, size_t maxRtcpPacketLen)
{
MS_TRACE();

MS_ASSERT(baseSet, "base not set");
MS_ASSERT(!IsFull(), "packet is full");
penguinol marked this conversation as resolved.
Show resolved Hide resolved

// Let's see if we must set our base.
if (this->latestTimestamp == 0u)
{
this->baseSequenceNumber = sequenceNumber + 1;
this->referenceTime = static_cast<int32_t>((timestamp & 0x1FFFFFC0) / 64);
this->latestSequenceNumber = sequenceNumber;
this->latestTimestamp = (timestamp >> 6) * 64; // IMPORTANT: Loose precision.

return AddPacketResult::SUCCESS;
}

// If the wide sequence number of the new packet is lower than the latest seen,
// ignore it.
// NOTE: Not very spec compliant but libwebrtc does it.
Expand Down
175 changes: 116 additions & 59 deletions worker/src/RTC/TransportCongestionControlServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace RTC

static constexpr uint64_t TransportCcFeedbackSendInterval{ 100u }; // In ms.
static constexpr uint64_t LimitationRembInterval{ 1500u }; // In ms.
static constexpr uint64_t PacketArrivalTimestampWindow{ 500u }; // In ms.
static constexpr uint8_t UnlimitedRembNumPackets{ 4u };
static constexpr size_t PacketLossHistogramLength{ 24 };

Expand Down Expand Up @@ -124,61 +125,32 @@ namespace RTC
break;
}

// Update the RTCP media SSRC of the ongoing Transport-CC Feedback packet.
this->transportCcFeedbackSenderSsrc = 0u;
this->transportCcFeedbackMediaSsrc = packet->GetSsrc();

this->transportCcFeedbackPacket->SetSenderSsrc(0u);
this->transportCcFeedbackPacket->SetMediaSsrc(this->transportCcFeedbackMediaSsrc);

// Provide the feedback packet with the RTP packet info. If it fails,
// send current feedback and add the packet info to a new one.
auto result =
this->transportCcFeedbackPacket->AddPacket(wideSeqNumber, nowMs, this->maxRtcpPacketLen);

switch (result)
// Only insert the packet when receiving it for the first time.
if (!this->mapPacketArrivalTimes.try_emplace(wideSeqNumber, nowMs).second)
{
case RTC::RTCP::FeedbackRtpTransportPacket::AddPacketResult::SUCCESS:
{
// If the feedback packet is full, send it now.
if (this->transportCcFeedbackPacket->IsFull())
{
MS_DEBUG_DEV("transport-cc feedback packet is full, sending feedback now");

SendTransportCcFeedback();
}

break;
}

case RTC::RTCP::FeedbackRtpTransportPacket::AddPacketResult::MAX_SIZE_EXCEEDED:
{
// Send ongoing feedback packet and add the new packet info to the
// regenerated one.
SendTransportCcFeedback();
break;
}

this->transportCcFeedbackPacket->AddPacket(wideSeqNumber, nowMs, this->maxRtcpPacketLen);
// We may receive packets with sequence number lower than the one in previous
// tcc feedback, these packets may have been reported as lost previously,
// therefore we need to reset the start sequence num for the next tcc feedback.
if (
!this->transportWideSeqNumberReceived ||
RTC::SeqManager<uint16_t>::IsSeqLowerThan(
wideSeqNumber, this->transportCcFeedbackWideSeqNumStart))
{
this->transportCcFeedbackWideSeqNumStart = wideSeqNumber;
}

break;
}
this->transportWideSeqNumberReceived = true;

case RTC::RTCP::FeedbackRtpTransportPacket::AddPacketResult::FATAL:
{
// Create a new feedback packet.
this->transportCcFeedbackPacket.reset(new RTC::RTCP::FeedbackRtpTransportPacket(
this->transportCcFeedbackSenderSsrc, this->transportCcFeedbackMediaSsrc));
MayDropOldPacketArrivalTimes(wideSeqNumber, nowMs);

// Use current packet count.
// NOTE: Do not increment it since the previous ongoing feedback
// packet was not sent.
this->transportCcFeedbackPacket->SetFeedbackPacketCount(
this->transportCcFeedbackPacketCount);

break;
}
}
// Update the RTCP media SSRC of the ongoing Transport-CC Feedback packet.
this->transportCcFeedbackSenderSsrc = 0u;
this->transportCcFeedbackMediaSsrc = packet->GetSsrc();

MaySendLimitationRembFeedback();
MaySendLimitationRembFeedback(nowMs);

break;
}
Expand All @@ -204,6 +176,75 @@ namespace RTC
}
}

void TransportCongestionControlServer::FillAndSendTransportCcFeedback()
{
MS_TRACE();

if (!this->transportWideSeqNumberReceived)
{
return;
}

auto it = this->mapPacketArrivalTimes.lower_bound(this->transportCcFeedbackWideSeqNumStart);

if (it == this->mapPacketArrivalTimes.end())
{
return;
}

// Set base sequence num and reference time.
this->transportCcFeedbackPacket->SetBase(this->transportCcFeedbackWideSeqNumStart, it->second);

for (; it != this->mapPacketArrivalTimes.end(); ++it)
{
auto result =
this->transportCcFeedbackPacket->AddPacket(it->first, it->second, this->maxRtcpPacketLen);

switch (result)
{
case RTC::RTCP::FeedbackRtpTransportPacket::AddPacketResult::SUCCESS:
{
// If the feedback packet is full, send it now.
if (this->transportCcFeedbackPacket->IsFull())
{
MS_DEBUG_DEV("transport-cc feedback packet is full, sending feedback now");

SendTransportCcFeedback();
}

break;
}

case RTC::RTCP::FeedbackRtpTransportPacket::AddPacketResult::MAX_SIZE_EXCEEDED:
{
// This should not happen.
MS_WARN_DEV("transport-cc feedback packet is exceeded");

// Create a new feedback packet.
this->transportCcFeedbackPacket.reset(new RTC::RTCP::FeedbackRtpTransportPacket(
this->transportCcFeedbackSenderSsrc, this->transportCcFeedbackMediaSsrc));
}

case RTC::RTCP::FeedbackRtpTransportPacket::AddPacketResult::FATAL:
{
// Create a new feedback packet.
this->transportCcFeedbackPacket.reset(new RTC::RTCP::FeedbackRtpTransportPacket(
this->transportCcFeedbackSenderSsrc, this->transportCcFeedbackMediaSsrc));

// Use current packet count.
// NOTE: Do not increment it since the previous ongoing feedback
// packet was not sent.
this->transportCcFeedbackPacket->SetFeedbackPacketCount(
this->transportCcFeedbackPacketCount);

break;
}
}
}

SendTransportCcFeedback();
}

void TransportCongestionControlServer::SetMaxIncomingBitrate(uint32_t bitrate)
{
MS_TRACE();
Expand All @@ -217,7 +258,9 @@ namespace RTC
// This is to ensure that we send N REMB packets with bitrate 0 (unlimited).
this->unlimitedRembCounter = UnlimitedRembNumPackets;

MaySendLimitationRembFeedback();
auto nowMs = DepLibUV::GetTimeMs();

MaySendLimitationRembFeedback(nowMs);
}
}

Expand All @@ -233,7 +276,6 @@ namespace RTC
}

auto latestWideSeqNumber = this->transportCcFeedbackPacket->GetLatestSequenceNumber();
auto latestTimestamp = this->transportCcFeedbackPacket->GetLatestTimestamp();

// Notify the listener.
this->listener->OnTransportCongestionControlServerSendRtcpPacket(
Expand Down Expand Up @@ -262,21 +304,36 @@ namespace RTC

// Increment packet count.
this->transportCcFeedbackPacket->SetFeedbackPacketCount(++this->transportCcFeedbackPacketCount);
this->transportCcFeedbackWideSeqNumStart = latestWideSeqNumber + 1;
}

inline void TransportCongestionControlServer::MayDropOldPacketArrivalTimes(
uint16_t seqNum, uint64_t nowMs)
{
MS_TRACE();

// Pass the latest packet info (if any) as pre base for the new feedback packet.
if (latestTimestamp > 0u)
// Ignore nowMs value if it's smaller than PacketArrivalTimestampWindow in
// order to avoid negative values (should never happen) and return early if
// the condition is met.
if (nowMs >= PacketArrivalTimestampWindow)
penguinol marked this conversation as resolved.
Show resolved Hide resolved
{
this->transportCcFeedbackPacket->AddPacket(
latestWideSeqNumber, latestTimestamp, this->maxRtcpPacketLen);
auto expiryTimestamp = nowMs - PacketArrivalTimestampWindow;
auto it = this->mapPacketArrivalTimes.begin();

while (it != this->mapPacketArrivalTimes.end() &&
it->first != this->transportCcFeedbackWideSeqNumStart &&
RTC::SeqManager<uint16_t>::IsSeqLowerThan(it->first, seqNum) &&
it->second <= expiryTimestamp)
{
it = this->mapPacketArrivalTimes.erase(it);
}
}
}

inline void TransportCongestionControlServer::MaySendLimitationRembFeedback()
inline void TransportCongestionControlServer::MaySendLimitationRembFeedback(uint64_t nowMs)
{
MS_TRACE();

auto nowMs = DepLibUV::GetTimeMs();

// May fix unlimitedRembCounter.
if (this->unlimitedRembCounter > 0u && this->maxIncomingBitrate != 0u)
{
Expand Down Expand Up @@ -389,7 +446,7 @@ namespace RTC

if (timer == this->transportCcFeedbackSendPeriodicTimer)
{
SendTransportCcFeedback();
FillAndSendTransportCcFeedback();
}
}
} // namespace RTC
Loading
Loading