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

Transport: Improve how REMB is enabled.

上级 d1e66c9c
......@@ -13,6 +13,7 @@ namespace RTC
{
public:
virtual void OnProducerClosed(RTC::Producer* producer) = 0;
virtual void OnProducerHasRemb(RTC::Producer* producer) = 0;
virtual void OnProducerRtpParametersUpdated(RTC::Producer* producer) = 0;
virtual void OnProducerPaused(RTC::Producer* producer) = 0;
virtual void OnProducerResumed(RTC::Producer* producer) = 0;
......
......@@ -41,7 +41,8 @@ namespace RTC
{
public:
// Called when a receive channel group has a new bitrate estimate for the incoming streams.
virtual void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs, uint32_t bitrate) = 0;
virtual void OnRemoteBitrateEstimatorValue(
const std::vector<uint32_t>& ssrcs, uint32_t bitrate) = 0;
};
protected:
......
......@@ -64,6 +64,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ProducerListener. */
public:
void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerHasRemb(RTC::Producer* producer) override;
void OnProducerRtpParametersUpdated(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override;
......
......@@ -95,9 +95,8 @@ namespace RTC
void ChangeUfragPwd(std::string& usernameFragment, std::string& password);
void SendRtpPacket(RTC::RtpPacket* packet);
void SendRtcpPacket(RTC::RTCP::Packet* packet);
bool IsConnected() const;
void EnableRemb();
void SendRtcp(uint64_t now);
bool IsConnected() const;
private:
void MayRunDtlsTransport();
......@@ -154,13 +153,10 @@ namespace RTC
void OnDtlsApplicationData(
const RTC::DtlsTransport* dtlsTransport, const uint8_t* data, size_t len) override;
/* Pure virtual methods inherited from RTC::RemoteBitrateEstimator::Listener. */
public:
void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs, uint32_t bitrate) override;
/* Pure virtual methods inherited from RTC::ProducerListener. */
public:
void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerHasRemb(RTC::Producer* producer) override;
void OnProducerRtpParametersUpdated(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override;
......@@ -178,6 +174,10 @@ namespace RTC
void OnConsumerClosed(RTC::Consumer* consumer) override;
void OnConsumerKeyFrameRequired(RTC::Consumer* consumer) override;
/* Pure virtual methods inherited from RTC::RemoteBitrateEstimator::Listener. */
public:
void OnRemoteBitrateEstimatorValue(const std::vector<uint32_t>& ssrcs, uint32_t bitrate) override;
/* Pure virtual methods inherited from Timer::Listener. */
public:
void OnTimer(Timer* timer) override;
......@@ -227,14 +227,6 @@ namespace RTC
this->selectedTuple != nullptr &&
this->dtlsTransport->GetState() == RTC::DtlsTransport::DtlsState::CONNECTED);
}
inline void Transport::EnableRemb()
{
if (!this->remoteBitrateEstimator)
{
this->remoteBitrateEstimator.reset(new RTC::RemoteBitrateEstimatorAbsSendTime(this));
}
}
} // namespace RTC
#endif
......@@ -594,7 +594,12 @@ namespace RTC
// Enable REMB in the transport if requested.
if (useRemb)
this->transport->EnableRemb();
{
for (auto& listener : this->listeners)
{
listener->OnProducerHasRemb(this);
}
}
// Check RTX capabilities.
if (encoding.hasRtx && encoding.rtx.ssrc != 0u)
......
......@@ -331,7 +331,7 @@ namespace RTC
++this->totalProbesReceived;
// Make sure that a probe which updated the bitrate immediately has an
// effect by calling the onReceiveBitrateChanged callback.
// effect by calling the OnRemoteBitrateEstimatorValue callback.
if (ProcessClusters(nowMs) == ProbeResult::BITRATE_UPDATED)
updateEstimate = true;
}
......@@ -383,7 +383,7 @@ namespace RTC
if (updateEstimate)
{
this->lastUpdateMs = nowMs;
this->observer->OnReceiveBitrateChanged(ssrcs, targetBitrateBps);
this->observer->OnRemoteBitrateEstimatorValue(ssrcs, targetBitrateBps);
}
}
......
......@@ -171,7 +171,7 @@ namespace RTC
std::vector<uint32_t> ssrcs;
GetSsrcs(&ssrcs);
this->observer->OnReceiveBitrateChanged(ssrcs, targetBitrate);
this->observer->OnRemoteBitrateEstimatorValue(ssrcs, targetBitrate);
}
}
......
......@@ -1342,6 +1342,11 @@ namespace RTC
this->mapProducerAudioLevelContainer.erase(producer);
}
void Router::OnProducerHasRemb(RTC::Producer* /*producer*/)
{
// Do nothing.
}
void Router::OnProducerRtpParametersUpdated(RTC::Producer* producer)
{
MS_ASSERT(
......
......@@ -1521,68 +1521,6 @@ namespace RTC
// NOTE: No DataChannel support, si just ignore it.
}
void Transport::OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs, uint32_t bitrate)
{
MS_TRACE();
uint32_t effectiveBitrate;
uint64_t now = DepLibUV::GetTime();
// Limit bitrate if requested via API.
if (this->maxBitrate != 0u)
effectiveBitrate = std::min(bitrate, this->maxBitrate);
else
effectiveBitrate = bitrate;
if (MS_HAS_DEBUG_TAG(rbe))
{
std::ostringstream ssrcsStream;
if (!ssrcs.empty())
{
std::copy(ssrcs.begin(), ssrcs.end() - 1, std::ostream_iterator<uint32_t>(ssrcsStream, ","));
ssrcsStream << ssrcs.back();
}
MS_DEBUG_TAG(
rbe,
"sending RTCP REMB packet [estimated:%" PRIu32 "bps, effective:%" PRIu32 "bps, ssrcs:%s]",
bitrate,
effectiveBitrate,
ssrcsStream.str().c_str());
}
RTC::RTCP::FeedbackPsRembPacket packet(0, 0);
packet.SetBitrate(effectiveBitrate);
packet.SetSsrcs(ssrcs);
packet.Serialize(RTC::RTCP::Buffer);
this->SendRtcpPacket(&packet);
// Trigger a key frame for all the suitable streams if the effective max bitrate
// has decreased abruptly.
if (now - this->lastEffectiveMaxBitrateAt > EffectiveMaxBitrateCheckInterval)
{
if (
(bitrate != 0u) && (this->effectiveMaxBitrate != 0u) &&
static_cast<double>(effectiveBitrate) / static_cast<double>(this->effectiveMaxBitrate) <
EffectiveMaxBitrateThresholdBeforeKeyFrame)
{
MS_WARN_2TAGS(
rbe, rtx, "uplink effective max bitrate abruptly decreased, requesting key frames");
// Request key frame for all the Producers.
for (auto* producer : this->producers)
{
producer->RequestKeyFrame();
}
}
this->lastEffectiveMaxBitrateAt = now;
this->effectiveMaxBitrate = effectiveBitrate;
}
}
void Transport::OnProducerClosed(RTC::Producer* producer)
{
MS_TRACE();
......@@ -1594,6 +1532,15 @@ namespace RTC
this->rtpListener.RemoveProducer(producer);
}
void Transport::OnProducerHasRemb(RTC::Producer* /*producer*/)
{
// If already running, do nothing.
if (this->remoteBitrateEstimator)
return;
this->remoteBitrateEstimator.reset(new RTC::RemoteBitrateEstimatorAbsSendTime(this));
}
void Transport::OnProducerRtpParametersUpdated(RTC::Producer* producer)
{
MS_TRACE();
......@@ -1646,6 +1593,68 @@ namespace RTC
// Do nothing.
}
void Transport::OnRemoteBitrateEstimatorValue(const std::vector<uint32_t>& ssrcs, uint32_t bitrate)
{
MS_TRACE();
uint32_t effectiveBitrate;
uint64_t now = DepLibUV::GetTime();
// Limit bitrate if requested via API.
if (this->maxBitrate != 0u)
effectiveBitrate = std::min(bitrate, this->maxBitrate);
else
effectiveBitrate = bitrate;
if (MS_HAS_DEBUG_TAG(rbe))
{
std::ostringstream ssrcsStream;
if (!ssrcs.empty())
{
std::copy(ssrcs.begin(), ssrcs.end() - 1, std::ostream_iterator<uint32_t>(ssrcsStream, ","));
ssrcsStream << ssrcs.back();
}
MS_DEBUG_TAG(
rbe,
"sending RTCP REMB packet [estimated:%" PRIu32 "bps, effective:%" PRIu32 "bps, ssrcs:%s]",
bitrate,
effectiveBitrate,
ssrcsStream.str().c_str());
}
RTC::RTCP::FeedbackPsRembPacket packet(0, 0);
packet.SetBitrate(effectiveBitrate);
packet.SetSsrcs(ssrcs);
packet.Serialize(RTC::RTCP::Buffer);
this->SendRtcpPacket(&packet);
// Trigger a key frame for all the suitable streams if the effective max bitrate
// has decreased abruptly.
if (now - this->lastEffectiveMaxBitrateAt > EffectiveMaxBitrateCheckInterval)
{
if (
(bitrate != 0u) && (this->effectiveMaxBitrate != 0u) &&
static_cast<double>(effectiveBitrate) / static_cast<double>(this->effectiveMaxBitrate) <
EffectiveMaxBitrateThresholdBeforeKeyFrame)
{
MS_WARN_2TAGS(
rbe, rtx, "uplink effective max bitrate abruptly decreased, requesting key frames");
// Request key frame for all the Producers.
for (auto* producer : this->producers)
{
producer->RequestKeyFrame();
}
}
this->lastEffectiveMaxBitrateAt = now;
this->effectiveMaxBitrate = effectiveBitrate;
}
}
void Transport::OnTimer(Timer* timer)
{
if (timer == this->rtcpTimer)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论