Unverified 提交 fc895efd authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

New RTP probation system (#304)

- Remove RtpProbator class.
- Make WebRtcTransport ask the BWE client (RembClient) whether RTP probation
  is needed.
- If so, make the simulcast/svc consumer resend current media packet via
  retransmission (RTX if supported) up to 3 times to satisfy desired probation
  bitrate.
上级 84218eba
......@@ -231,28 +231,28 @@ const supportedRtpCapabilities =
},
{
kind : 'audio',
uri : 'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time', // eslint-disable-line max-len
uri : 'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time',
preferredId : 4,
preferredEncrypt : false,
direction : 'sendrecv'
},
{
kind : 'video',
uri : 'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time', // eslint-disable-line max-len
uri : 'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time',
preferredId : 4,
preferredEncrypt : false,
direction : 'sendrecv'
},
{
kind : 'audio',
uri : 'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01', // eslint-disable-line max-len
uri : 'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01',
preferredId : 5,
preferredEncrypt : false,
direction : 'inactive' // TODO: Enable when done.
},
{
kind : 'video',
uri : 'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01', // eslint-disable-line max-len
uri : 'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01',
preferredId : 5,
preferredEncrypt : false,
direction : 'inactive' // TODO: Enable when done.
......
......@@ -164,11 +164,6 @@
"directory": "PATH/out",
"file": "PATH/src/RTC/RtpPacket.cpp"
},
{
"command": "c++ -c -DMS_LITTLE_ENDIAN -D_POSIX_C_SOURCE=200112 -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_INT16_T -DHAVE_INT32_T -DHAVE_INT8_T -DHAVE_UINT16_T -DHAVE_UINT32_T -DHAVE_UINT64_T -DHAVE_UINT8_T -DHAVE_STDINT_H -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -DCPU_CISC -IPATH/include -IPATH/deps/netstring/netstring-c -IPATH/deps/libuv/include -IPATH/deps/openssl/openssl/include -IPATH/deps/libsrtp/srtp/include -std=c++11 -Wall -Wextra -Wno-unused-parameter -m64 -g -o PATH/out/Release/obj.target/mediasoup-worker/src/RTC/RtpProbator.o PATH/src/RTC/RtpProbator.cpp",
"directory": "PATH/out",
"file": "PATH/src/RTC/RtpProbator.cpp"
},
{
"command": "c++ -c -DMS_LITTLE_ENDIAN -D_POSIX_C_SOURCE=200112 -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_INT16_T -DHAVE_INT32_T -DHAVE_INT8_T -DHAVE_UINT16_T -DHAVE_UINT32_T -DHAVE_UINT64_T -DHAVE_UINT8_T -DHAVE_STDINT_H -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -DCPU_CISC -IPATH/include -IPATH/deps/netstring/netstring-c -IPATH/deps/libuv/include -IPATH/deps/openssl/openssl/include -IPATH/deps/libsrtp/srtp/include -std=c++11 -Wall -Wextra -Wno-unused-parameter -m64 -g -o PATH/out/Release/obj.target/mediasoup-worker/src/RTC/RtpStream.o PATH/src/RTC/RtpStream.cpp",
"directory": "PATH/out",
......
......@@ -26,7 +26,8 @@ namespace RTC
{
public:
virtual void OnConsumerSendRtpPacket(RTC::Consumer* consumer, RTC::RtpPacket* packet) = 0;
virtual void OnConsumerRetransmitRtpPacket(RTC::Consumer* consumer, RTC::RtpPacket* packet) = 0;
virtual void OnConsumerRetransmitRtpPacket(
RTC::Consumer* consumer, RTC::RtpPacket* packet, bool probation = false) = 0;
virtual void OnConsumerKeyFrameRequested(RTC::Consumer* consumer, uint32_t mappedSsrc) = 0;
virtual void OnConsumerNeedBitrateChange(RTC::Consumer* consumer) = 0;
virtual void onConsumerProducerClosed(RTC::Consumer* consumer) = 0;
......@@ -69,6 +70,7 @@ namespace RTC
virtual uint32_t IncreaseLayer(uint32_t bitrate);
virtual void ApplyLayers();
virtual void SendRtpPacket(RTC::RtpPacket* packet) = 0;
virtual void SendProbationRtpPacket(uint16_t seq) = 0;
virtual std::vector<RTC::RtpStreamSend*> GetRtpStreams() = 0;
virtual void GetRtcp(
RTC::RTCP::CompoundPacket* packet, RTC::RtpStreamSend* rtpStream, uint64_t now) = 0;
......
......@@ -23,6 +23,7 @@ namespace RTC
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override;
void SendRtpPacket(RTC::RtpPacket* packet) override;
void SendProbationRtpPacket(uint16_t seq) override;
void GetRtcp(RTC::RTCP::CompoundPacket* packet, RTC::RtpStreamSend* rtpStream, uint64_t now) override;
std::vector<RTC::RtpStreamSend*> GetRtpStreams() override;
void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override;
......@@ -42,7 +43,8 @@ namespace RTC
/* Pure virtual methods inherited from RtpStreamSend::Listener. */
public:
void OnRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void OnRtpStreamRetransmitRtpPacket(RTC::RtpStreamSend* rtpStream, RTC::RtpPacket* packet) override;
void OnRtpStreamRetransmitRtpPacket(
RTC::RtpStreamSend* rtpStream, RTC::RtpPacket* packet, bool probation = false) override;
private:
// Allocated by this.
......
......@@ -28,7 +28,10 @@ namespace RTC
private:
bool IsConnected() const override;
void SendRtpPacket(
RTC::RtpPacket* packet, RTC::Consumer* consumer, bool retransmitted = false) override;
RTC::RtpPacket* packet,
RTC::Consumer* consumer,
bool retransmitted = false,
bool probation = false) override;
void SendRtcpPacket(RTC::RTCP::Packet* packet) override;
void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet) override;
void OnPacketRecv(RTC::TransportTuple* tuple, const uint8_t* data, size_t len);
......
......@@ -28,7 +28,10 @@ namespace RTC
private:
bool IsConnected() const override;
void SendRtpPacket(
RTC::RtpPacket* packet, RTC::Consumer* consumer, bool retransmitted = false) override;
RTC::RtpPacket* packet,
RTC::Consumer* consumer,
bool retransmitted = false,
bool probation = false) override;
void SendRtcpPacket(RTC::RTCP::Packet* packet) override;
void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet) override;
void OnPacketRecv(RTC::TransportTuple* tuple, const uint8_t* data, size_t len);
......
......@@ -3,12 +3,12 @@
#include "common.hpp"
#include "RTC/RTCP/FeedbackPsRemb.hpp"
#include "RTC/RtpDataCounter.hpp"
#include "RTC/RtpPacket.hpp"
#include "RTC/RtpProbator.hpp"
namespace RTC
{
class RembClient : public RTC::RtpProbator::Listener
class RembClient
{
public:
class Listener
......@@ -16,8 +16,6 @@ namespace RTC
public:
virtual void OnRembClientAvailableBitrate(
RTC::RembClient* rembClient, uint32_t availableBitrate) = 0;
virtual void OnRembClientSendProbationRtpPacket(
RTC::RembClient* rembClient, RTC::RtpPacket* packet) = 0;
};
public:
......@@ -30,27 +28,27 @@ namespace RTC
public:
void ReceiveRembFeedback(RTC::RTCP::FeedbackPsRembPacket* remb);
void SentRtpPacket(RTC::RtpPacket* packet, bool retransmitted);
void SentProbationRtpPacket(RTC::RtpPacket* packet);
uint32_t GetAvailableBitrate();
void ResecheduleNextEvent();
bool IsProbationNeeded();
private:
void CheckStatus(uint64_t now);
/* Pure virtual methods inherited from RTC::RtpProbator::Listener. */
public:
void OnRtpProbatorSendRtpPacket(RTC::RtpProbator* rtpProbator, RTC::RtpPacket* packet) override;
void CalculateProbationTargetBitrate();
private:
// Passed by argument.
Listener* listener{ nullptr };
// Allocated by this.
RTC::RtpProbator* rtpProbator{ nullptr };
// Others.
uint32_t initialAvailableBitrate{ 0 };
uint32_t minimumAvailableBitrate{ 0 };
uint64_t initialAvailableBitrateAt{ 0 };
uint32_t availableBitrate{ 0 };
uint64_t lastEventAt{ 0 };
uint32_t probationTargetBitrate{ 0 };
RTC::RtpDataCounter transmissionCounter;
RTC::RtpDataCounter probationTransmissionCounter;
};
} // namespace RTC
......
#ifndef MS_RTC_RTP_PROBATOR_HPP
#define MS_RTC_RTP_PROBATOR_HPP
#include "common.hpp"
#include "RTC/RtpDataCounter.hpp"
#include "RTC/RtpPacket.hpp"
namespace RTC
{
class RtpProbator
{
public:
class Listener
{
public:
virtual void OnRtpProbatorSendRtpPacket(RTC::RtpProbator* rtpProbator, RTC::RtpPacket* packet) = 0;
};
public:
explicit RtpProbator(RTC::RtpProbator::Listener* listener);
virtual ~RtpProbator();
public:
void UpdateAvailableBitrate(uint32_t availableBitrate);
void ReceiveRtpPacket(RTC::RtpPacket* packet, bool retransmitted);
private:
// Passed by argument.
Listener* listener{ nullptr };
// Others.
bool enabled{ false };
uint32_t probationTargetBitrate{ 0 };
RTC::RtpDataCounter transmissionCounter;
RTC::RtpDataCounter probationTransmissionCounter;
};
} // namespace RTC
#endif
......@@ -15,7 +15,7 @@ namespace RTC
{
public:
virtual void OnRtpStreamRetransmitRtpPacket(
RTC::RtpStreamSend* rtpStream, RTC::RtpPacket* packet) = 0;
RTC::RtpStreamSend* rtpStream, RTC::RtpPacket* packet, bool probation = false) = 0;
};
public:
......@@ -48,6 +48,7 @@ namespace RTC
RTC::RTCP::SdesChunk* GetRtcpSdesChunk();
void Pause() override;
void Resume() override;
void SendProbationRtpPacket(uint16_t seq);
uint32_t GetBitrate(uint64_t now) override;
uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override;
uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override;
......
......@@ -24,6 +24,7 @@ namespace RTC
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override;
void SendRtpPacket(RTC::RtpPacket* packet) override;
void SendProbationRtpPacket(uint16_t seq) override;
std::vector<RTC::RtpStreamSend*> GetRtpStreams() override;
void GetRtcp(RTC::RTCP::CompoundPacket* packet, RTC::RtpStreamSend* rtpStream, uint64_t now) override;
void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override;
......@@ -44,7 +45,8 @@ namespace RTC
/* Pure virtual methods inherited from RtpStreamSend::Listener. */
public:
void OnRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void OnRtpStreamRetransmitRtpPacket(RTC::RtpStreamSend* rtpStream, RTC::RtpPacket* packet) override;
void OnRtpStreamRetransmitRtpPacket(
RTC::RtpStreamSend* rtpStream, RTC::RtpPacket* packet, bool probation = false) override;
private:
// Allocated by this.
......
......@@ -31,6 +31,7 @@ namespace RTC
uint32_t IncreaseLayer(uint32_t bitrate) override;
void ApplyLayers() override;
void SendRtpPacket(RTC::RtpPacket* packet) override;
void SendProbationRtpPacket(uint16_t seq) override;
void GetRtcp(RTC::RTCP::CompoundPacket* packet, RTC::RtpStreamSend* rtpStream, uint64_t now) override;
std::vector<RTC::RtpStreamSend*> GetRtpStreams() override;
void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override;
......@@ -62,7 +63,8 @@ namespace RTC
/* Pure virtual methods inherited from RtpStreamSend::Listener. */
public:
void OnRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void OnRtpStreamRetransmitRtpPacket(RTC::RtpStreamSend* rtpStream, RTC::RtpPacket* packet) override;
void OnRtpStreamRetransmitRtpPacket(
RTC::RtpStreamSend* rtpStream, RTC::RtpPacket* packet, bool probation = false) override;
private:
// Allocated by this.
......
......@@ -101,7 +101,10 @@ namespace RTC
RTC::Consumer* GetConsumerByMediaSsrc(uint32_t ssrc) const;
virtual bool IsConnected() const = 0;
virtual void SendRtpPacket(
RTC::RtpPacket* packet, RTC::Consumer* consumer, bool retransmitted = false) = 0;
RTC::RtpPacket* packet,
RTC::Consumer* consumer,
bool retransmitted = false,
bool probation = false) = 0;
void SendRtcp(uint64_t now);
virtual void SendRtcpPacket(RTC::RTCP::Packet* packet) = 0;
virtual void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet) = 0;
......@@ -124,7 +127,8 @@ namespace RTC
/* Pure virtual methods inherited from RTC::Consumer::Listener. */
public:
void OnConsumerSendRtpPacket(RTC::Consumer* consumer, RTC::RtpPacket* packet) override;
void OnConsumerRetransmitRtpPacket(RTC::Consumer* consumer, RTC::RtpPacket* packet) override;
void OnConsumerRetransmitRtpPacket(
RTC::Consumer* consumer, RTC::RtpPacket* packet, bool probation) override;
void OnConsumerKeyFrameRequested(RTC::Consumer* consumer, uint32_t mappedSsrc) override;
virtual void OnConsumerNeedBitrateChange(RTC::Consumer* consumer) override = 0;
void onConsumerProducerClosed(RTC::Consumer* consumer) override;
......
......@@ -46,7 +46,10 @@ namespace RTC
bool IsConnected() const override;
void MayRunDtlsTransport();
void SendRtpPacket(
RTC::RtpPacket* packet, RTC::Consumer* consumer, bool retransmitted = false) override;
RTC::RtpPacket* packet,
RTC::Consumer* consumer,
bool retransmitted = false,
bool probation = false) override;
void SendRtcpPacket(RTC::RTCP::Packet* packet) override;
void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet) override;
void DistributeAvailableOutgoingBitrate();
......@@ -110,7 +113,6 @@ namespace RTC
/* Pure virtual methods inherited from RTC::RembClient::Listener. */
public:
void OnRembClientAvailableBitrate(RTC::RembClient* rembClient, uint32_t availableBitrate) override;
void OnRembClientSendProbationRtpPacket(RTC::RembClient* rembClient, RTC::RtpPacket* packet) override;
/* Pure virtual methods inherited from RTC::RembServer::RemoteBitrateEstimator::Listener. */
public:
......
......@@ -45,7 +45,6 @@
'src/RTC/RtpListener.cpp',
'src/RTC/RtpObserver.cpp',
'src/RTC/RtpPacket.cpp',
'src/RTC/RtpProbator.cpp',
'src/RTC/RtpStream.cpp',
'src/RTC/RtpStreamRecv.cpp',
'src/RTC/RtpStreamSend.cpp',
......@@ -144,7 +143,6 @@
'include/RTC/RtpListener.hpp',
'include/RTC/RtpObserver.hpp',
'include/RTC/RtpPacket.hpp',
'include/RTC/RtpProbator.hpp',
'include/RTC/RtpStream.hpp',
'include/RTC/RtpStreamRecv.hpp',
'include/RTC/RtpStreamSend.hpp',
......
......@@ -220,6 +220,13 @@ namespace RTC
packet->SetSequenceNumber(origSeq);
}
void PipeConsumer::SendProbationRtpPacket(uint16_t /*seq*/)
{
MS_TRACE();
MS_ABORT("should not call this method");
}
void PipeConsumer::GetRtcp(
RTC::RTCP::CompoundPacket* packet, RTC::RtpStreamSend* rtpStream, uint64_t now)
{
......@@ -493,10 +500,10 @@ namespace RTC
}
inline void PipeConsumer::OnRtpStreamRetransmitRtpPacket(
RTC::RtpStreamSend* /*rtpStream*/, RTC::RtpPacket* packet)
RTC::RtpStreamSend* /*rtpStream*/, RTC::RtpPacket* packet, bool probation)
{
MS_TRACE();
this->listener->OnConsumerRetransmitRtpPacket(this, packet);
this->listener->OnConsumerRetransmitRtpPacket(this, packet, probation);
}
} // namespace RTC
......@@ -265,7 +265,7 @@ namespace RTC
}
void PipeTransport::SendRtpPacket(
RTC::RtpPacket* packet, RTC::Consumer* /*consumer*/, bool /*retransmitted*/)
RTC::RtpPacket* packet, RTC::Consumer* /*consumer*/, bool /*retransmitted*/, bool /*probation*/)
{
MS_TRACE();
......
......@@ -452,7 +452,7 @@ namespace RTC
}
void PlainRtpTransport::SendRtpPacket(
RTC::RtpPacket* packet, RTC::Consumer* /*consumer*/, bool /*retransmitted*/)
RTC::RtpPacket* packet, RTC::Consumer* /*consumer*/, bool /*retransmitted*/, bool /*probation*/)
{
MS_TRACE();
......
......@@ -1063,9 +1063,14 @@ namespace RTC
{
// If video orientation was not yet detected or any value has changed,
// emit event.
// clang-format off
if (
!this->videoOrientationDetected || camera != this->videoOrientation.camera ||
flip != this->videoOrientation.flip || rotation != this->videoOrientation.rotation)
!this->videoOrientationDetected ||
camera != this->videoOrientation.camera ||
flip != this->videoOrientation.flip ||
rotation != this->videoOrientation.rotation
)
// clang-format on
{
this->videoOrientationDetected = true;
this->videoOrientation.camera = camera;
......
......@@ -23,15 +23,11 @@ namespace RTC
minimumAvailableBitrate(minimumAvailableBitrate)
{
MS_TRACE();
this->rtpProbator = new RTC::RtpProbator(this);
}
RembClient::~RembClient()
{
MS_TRACE();
delete this->rtpProbator;
}
void RembClient::ReceiveRembFeedback(RTC::RTCP::FeedbackPsRembPacket* remb)
......@@ -82,23 +78,32 @@ namespace RTC
notify = true;
}
// Pass available bitrate to the RtpProbator.
this->rtpProbator->UpdateAvailableBitrate(this->availableBitrate);
if (notify)
{
this->lastEventAt = now;
this->listener->OnRembClientAvailableBitrate(this, this->availableBitrate);
}
CalculateProbationTargetBitrate();
}
void RembClient::SentRtpPacket(RTC::RtpPacket* packet, bool retransmitted)
void RembClient::SentRtpPacket(RTC::RtpPacket* packet, bool /*retransmitted*/)
{
MS_TRACE();
// Pass the packet to the RtpProbator.
this->rtpProbator->ReceiveRtpPacket(packet, retransmitted);
// Increase transmission counter.
this->transmissionCounter.Update(packet);
}
void RembClient::SentProbationRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
MS_DEBUG_DEV("[seq:%" PRIu16 ", size:%zu]", packet->GetSequenceNumber(), packet->GetSize());
// Increase probation transmission counter.
this->probationTransmissionCounter.Update(packet);
}
uint32_t RembClient::GetAvailableBitrate()
......@@ -119,6 +124,19 @@ namespace RTC
this->lastEventAt = DepLibUV::GetTime();
}
bool RembClient::IsProbationNeeded()
{
MS_TRACE();
if (!this->probationTargetBitrate)
return false;
auto now = DepLibUV::GetTime();
auto probationTransmissionBitrate = this->probationTransmissionCounter.GetBitrate(now);
return (probationTransmissionBitrate <= this->probationTargetBitrate);
}
inline void RembClient::CheckStatus(uint64_t now)
{
MS_TRACE();
......@@ -130,17 +148,52 @@ namespace RTC
this->initialAvailableBitrateAt = now;
this->availableBitrate = this->initialAvailableBitrate;
// Tell the RTP probator to start probing even before receiving REMB
// feedbacks.
this->rtpProbator->UpdateAvailableBitrate(this->initialAvailableBitrate);
CalculateProbationTargetBitrate();
}
}
inline void RembClient::OnRtpProbatorSendRtpPacket(
RTC::RtpProbator* /*rtpProbator*/, RTC::RtpPacket* packet)
inline void RembClient::CalculateProbationTargetBitrate()
{
MS_TRACE();
this->listener->OnRembClientSendProbationRtpPacket(this, packet);
auto previousProbationTargetBitrate = this->probationTargetBitrate;
this->probationTargetBitrate = 0;
if (this->availableBitrate == 0)
return;
uint64_t now = DepLibUV::GetTime();
auto transmissionBitrate = this->transmissionCounter.GetBitrate(now);
auto factor =
static_cast<float>(transmissionBitrate) / static_cast<float>(this->availableBitrate);
// Just consider probation if transmission bitrate is close to available bitrate
// (without exceeding it much).
if (factor >= 0.8 && factor <= 1.2)
{
if (this->availableBitrate > transmissionBitrate)
this->probationTargetBitrate = 2 * (this->availableBitrate - transmissionBitrate);
else
this->probationTargetBitrate = 0.5 * transmissionBitrate;
}
// If there is no bitrate, set available bitrate as probation target bitrate.
else if (factor == 0)
{
this->probationTargetBitrate = this->availableBitrate;
}
if (this->probationTargetBitrate != previousProbationTargetBitrate)
{
MS_DEBUG_TAG(
bwe,
"probation %s [bitrate:%" PRIu32 ", availableBitrate:%" PRIu32
", factor:%f, probationTargetBitrate:%" PRIu32 "]",
this->probationTargetBitrate ? "enabled" : "disabled",
transmissionBitrate,
this->availableBitrate,
factor,
this->probationTargetBitrate);
}
}
} // namespace RTC
#define MS_CLASS "RTC::RtpProbator"
// #define MS_LOG_DEV
#include "RTC/RtpProbator.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp"
namespace RTC
{
/* Instance methods. */
RtpProbator::RtpProbator(RTC::RtpProbator::Listener* listener) : listener(listener)
{
MS_TRACE();
}
RtpProbator::~RtpProbator()
{
MS_TRACE();
}
void RtpProbator::UpdateAvailableBitrate(uint32_t availableBitrate)
{
MS_TRACE();
auto wasEnabled = this->enabled;
// Let's do this stateless.
this->enabled = false;
this->probationTargetBitrate = 0;
auto now = DepLibUV::GetTime();
auto transmissionBitrate = this->transmissionCounter.GetBitrate(now);
if (availableBitrate == 0)
{
if (wasEnabled)
MS_DEBUG_TAG(bwe, "probation disabled due zero available bitrate");
return;
}
// Just enable probation if transmission bitrate is close to available bitrate
// (without exceeding it).
auto factor = static_cast<float>(transmissionBitrate) / static_cast<float>(availableBitrate);
if (factor >= 0.8 && factor <= 1.2)
{
this->enabled = true;
if (availableBitrate > transmissionBitrate)
this->probationTargetBitrate = 2 * (availableBitrate - transmissionBitrate);
else
this->probationTargetBitrate = 0.5 * transmissionBitrate;
}
// If there is no bitrate, set available bitrate as probation target bitrate.
else if (factor == 0)
{
this->enabled = true;
this->probationTargetBitrate = availableBitrate;
}
if (!wasEnabled && this->enabled)
{
MS_DEBUG_TAG(
bwe,
"probation enabled [bitrate:%" PRIu32 ", availableBitrate:%" PRIu32
", factor:%f, probationTargetBitrate:%" PRIu32 "]",
transmissionBitrate,
availableBitrate,
factor,
this->probationTargetBitrate);
}
else if (wasEnabled && this->enabled)
{
MS_DEBUG_DEV(
"probation updated [bitrate:%" PRIu32 ", availableBitrate:%" PRIu32
", factor:%f, probationTargetBitrate:%" PRIu32 "]",
transmissionBitrate,
availableBitrate,
factor,
this->probationTargetBitrate);
}
else if (wasEnabled && !this->enabled)
{
MS_DEBUG_TAG(
bwe,
"probation disabled [bitrate:%" PRIu32 ", availableBitrate:%" PRIu32 ", factor:%f]",
transmissionBitrate,
availableBitrate,
factor);
}
}
void RtpProbator::ReceiveRtpPacket(RTC::RtpPacket* packet, bool retransmitted)
{
MS_TRACE();
// Increase transmission counter.
this->transmissionCounter.Update(packet);
// If probation disabled or a retransmitted packet, exit here.
if (!this->enabled || retransmitted)
return;
auto now = DepLibUV::GetTime();
auto probationTransmissionBitrate = this->probationTransmissionCounter.GetBitrate(now);
// Check whether we have to use this packet in probation.
if (probationTransmissionBitrate <= this->probationTargetBitrate)
{
MS_DEBUG_DEV(
"sending probation RTP packet [probationTargetBitrate:%" PRIu32
", probationTransmissionBitrate:%" PRIu32 "]",
this->probationTargetBitrate,
probationTransmissionBitrate);
// Increase probation transmission counter.
this->probationTransmissionCounter.Update(packet);
// Send the probation packet.
this->listener->OnRtpProbatorSendRtpPacket(this, packet);
// Update probation bitrate.
probationTransmissionBitrate = this->probationTransmissionCounter.GetBitrate(now);
}
// May send probation packet twice.
if (probationTransmissionBitrate <= this->probationTargetBitrate)
{
MS_DEBUG_DEV(
"sending probation RTP packet twice [probationTargetBitrate:%" PRIu32
", probationTransmissionBitrate:%" PRIu32 "]",
this->probationTargetBitrate,
probationTransmissionBitrate);
// Increase probation transmission counter.
this->probationTransmissionCounter.Update(packet);
// Send the probation packet.
this->listener->OnRtpProbatorSendRtpPacket(this, packet);
}
}
} // namespace RTC
......@@ -216,6 +216,43 @@ namespace RTC
MS_TRACE();
}
void RtpStreamSend::SendProbationRtpPacket(uint16_t seq)
{
MS_TRACE();
if (this->storage.empty())
return;
auto* storageItem = this->buffer[seq];
if (!storageItem)
return;
auto* packet = storageItem->packet;
// If we use RTX and the packet has not yet been resent, encode it now.
if (HasRtx())
{
// Increment RTX seq.
++this->rtxSeq;
if (!storageItem->rtxEncoded)
{
packet->RtxEncode(this->params.rtxPayloadType, this->params.rtxSsrc, this->rtxSeq);
storageItem->rtxEncoded = true;
}
else
{
packet->SetSequenceNumber(this->rtxSeq);
}
}
// Retransmit as probation packet.
static_cast<RTC::RtpStreamSend::Listener*>(this->listener)
->OnRtpStreamRetransmitRtpPacket(this, packet, true);
}
uint32_t RtpStreamSend::GetBitrate(uint64_t /*now*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/)
{
MS_ABORT("invalid method call");
......@@ -320,7 +357,6 @@ namespace RTC
if (!storageItem)
{
// TODO
MS_ASSERT(this->buffer[idx] == nullptr, "key should be NULL");
continue;
......@@ -467,13 +503,22 @@ namespace RTC
// Stored packet is valid for retransmission. Resend it.
else
{
// If we use RTX and the packet has not yet been resent, encode it
// now.
if (HasRtx() && !storageItem->rtxEncoded)
// If we use RTX and the packet has not yet been resent, encode it now.
if (HasRtx())
{
packet->RtxEncode(this->params.rtxPayloadType, this->params.rtxSsrc, ++this->rtxSeq);
storageItem->rtxEncoded = true;
// Increment RTX seq.
++this->rtxSeq;
if (!storageItem->rtxEncoded)
{
packet->RtxEncode(this->params.rtxPayloadType, this->params.rtxSsrc, this->rtxSeq);
storageItem->rtxEncoded = true;
}
else
{
packet->SetSequenceNumber(this->rtxSeq);
}
}
// Save when this packet was resent.
......
......@@ -216,6 +216,13 @@ namespace RTC
packet->SetSequenceNumber(origSeq);
}
void SimpleConsumer::SendProbationRtpPacket(uint16_t seq)
{
MS_TRACE();
this->rtpStream->SendProbationRtpPacket(seq);
}
void SimpleConsumer::GetRtcp(
RTC::RTCP::CompoundPacket* packet, RTC::RtpStreamSend* rtpStream, uint64_t now)
{
......@@ -452,10 +459,10 @@ namespace RTC
}
inline void SimpleConsumer::OnRtpStreamRetransmitRtpPacket(
RTC::RtpStreamSend* /*rtpStream*/, RTC::RtpPacket* packet)
RTC::RtpStreamSend* /*rtpStream*/, RTC::RtpPacket* packet, bool probation)
{
MS_TRACE();
this->listener->OnConsumerRetransmitRtpPacket(this, packet);
this->listener->OnConsumerRetransmitRtpPacket(this, packet, probation);
}
} // namespace RTC
......@@ -857,6 +857,13 @@ namespace RTC
packet->RestorePayload();
}
void SimulcastConsumer::SendProbationRtpPacket(uint16_t seq)
{
MS_TRACE();
this->rtpStream->SendProbationRtpPacket(seq);
}
void SimulcastConsumer::GetRtcp(
RTC::RTCP::CompoundPacket* packet, RTC::RtpStreamSend* rtpStream, uint64_t now)
{
......@@ -1320,7 +1327,7 @@ namespace RTC
// spatial layer, have Sender Report.
//
// clang-format off
bool canSwitch = (
return (
this->tsReferenceSpatialLayer == -1 ||
spatialLayer == this->tsReferenceSpatialLayer ||
(
......@@ -1329,11 +1336,6 @@ namespace RTC
)
);
// clang-format on
if (!canSwitch)
MS_DEBUG_TAG(simulcast, "cannot switch to spatialLayer:%" PRIi16, spatialLayer);
return canSwitch;
}
inline void SimulcastConsumer::EmitScore() const
......@@ -1418,10 +1420,10 @@ namespace RTC
}
inline void SimulcastConsumer::OnRtpStreamRetransmitRtpPacket(
RTC::RtpStreamSend* /*rtpStream*/, RTC::RtpPacket* packet)
RTC::RtpStreamSend* /*rtpStream*/, RTC::RtpPacket* packet, bool probation)
{
MS_TRACE();
this->listener->OnConsumerRetransmitRtpPacket(this, packet);
this->listener->OnConsumerRetransmitRtpPacket(this, packet, probation);
}
} // namespace RTC
......@@ -877,7 +877,8 @@ namespace RTC
SendRtpPacket(packet, consumer);
}
inline void Transport::OnConsumerRetransmitRtpPacket(RTC::Consumer* consumer, RTC::RtpPacket* packet)
inline void Transport::OnConsumerRetransmitRtpPacket(
RTC::Consumer* consumer, RTC::RtpPacket* packet, bool probation)
{
MS_TRACE();
......@@ -896,7 +897,7 @@ namespace RTC
}
}
SendRtpPacket(packet, consumer);
SendRtpPacket(packet, consumer, true, probation);
}
inline void Transport::OnConsumerKeyFrameRequested(RTC::Consumer* consumer, uint32_t mappedSsrc)
......
......@@ -775,7 +775,8 @@ namespace RTC
}
}
void WebRtcTransport::SendRtpPacket(RTC::RtpPacket* packet, RTC::Consumer* consumer, bool retransmitted)
void WebRtcTransport::SendRtpPacket(
RTC::RtpPacket* packet, RTC::Consumer* consumer, bool retransmitted, bool probation)
{
MS_TRACE();
......@@ -790,6 +791,7 @@ namespace RTC
return;
}
auto seq = packet->GetSequenceNumber();
const uint8_t* data = packet->GetData();
size_t len = packet->GetSize();
......@@ -812,12 +814,23 @@ namespace RTC
)
// clang-format on
{
uint8_t extenLen;
uint8_t* extenValue = packet->GetExtension(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME), extenLen);
if (extenValue && extenLen == 3)
if (!probation)
{
this->rembClient->SentRtpPacket(packet, retransmitted);
// May need to generate probation packets.
for (int count{ 1 }; count <= 3; ++count)
{
if (!this->rembClient->IsProbationNeeded())
break;
consumer->SendProbationRtpPacket(seq);
}
}
else
{
this->rembClient->SentProbationRtpPacket(packet);
}
}
}
......@@ -1604,34 +1617,6 @@ namespace RTC
DistributeAvailableOutgoingBitrate();
}
inline void WebRtcTransport::OnRembClientSendProbationRtpPacket(
RTC::RembClient* /*rembClient*/, RTC::RtpPacket* packet)
{
MS_TRACE();
if (!IsConnected())
return;
// Ensure there is sending SRTP session.
if (this->srtpSendSession == nullptr)
{
MS_WARN_DEV("ignoring RTP packet due to non sending SRTP session");
return;
}
const uint8_t* data = packet->GetData();
size_t len = packet->GetSize();
if (!this->srtpSendSession->EncryptRtp(&data, &len))
return;
this->iceSelectedTuple->Send(data, len);
// Increase send transmission.
RTC::Transport::DataSent(len);
}
inline void WebRtcTransport::OnRembServerAvailableBitrate(
const RTC::RembServer::RemoteBitrateEstimator* /*rembServer*/,
const std::vector<uint32_t>& ssrcs,
......
......@@ -17,7 +17,8 @@ SCENARIO("NACK and RTP packets retransmission", "[rtp][rtcp]")
{
}
void OnRtpStreamRetransmitRtpPacket(RtpStreamSend* /*rtpStream*/, RtpPacket* packet) override
void OnRtpStreamRetransmitRtpPacket(
RtpStreamSend* /*rtpStream*/, RtpPacket* packet, bool /*probation*/) override
{
this->retransmittedPackets.push_back(packet);
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论