From d9dc1845d1f4d8220ae428187b16f11e3a150e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Cervi=C3=B1o?= Date: Fri, 4 Oct 2019 18:59:20 +0200 Subject: [PATCH] Move SenderBandwidthEstimationHandler to WebRtcConnection (#1472) --- erizo/src/erizo/MediaStream.cpp | 2 - erizo/src/erizo/WebRtcConnection.cpp | 3 ++ .../rtp/SenderBandwidthEstimantionHandler.cpp | 42 +++++++++---------- .../rtp/SenderBandwidthEstimationHandler.h | 6 +-- erizo/src/erizo/rtp/StatsHandler.cpp | 1 + erizo/src/test/utils/Mocks.h | 1 + 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/erizo/src/erizo/MediaStream.cpp b/erizo/src/erizo/MediaStream.cpp index b605111963..83fd901112 100644 --- a/erizo/src/erizo/MediaStream.cpp +++ b/erizo/src/erizo/MediaStream.cpp @@ -30,7 +30,6 @@ #include "rtp/FakeKeyframeGeneratorHandler.h" #include "rtp/StatsHandler.h" #include "rtp/SRPacketHandler.h" -#include "rtp/SenderBandwidthEstimationHandler.h" #include "rtp/LayerDetectorHandler.h" #include "rtp/LayerBitrateCalculationHandler.h" #include "rtp/QualityFilterHandler.h" @@ -403,7 +402,6 @@ void MediaStream::initializePipeline() { pipeline_->addFront(std::make_shared()); pipeline_->addFront(std::make_shared()); pipeline_->addFront(std::make_shared()); - pipeline_->addFront(std::make_shared()); pipeline_->addFront(std::make_shared()); pipeline_->addFront(std::make_shared()); pipeline_->addFront(std::make_shared()); diff --git a/erizo/src/erizo/WebRtcConnection.cpp b/erizo/src/erizo/WebRtcConnection.cpp index ea7af515b7..c8e04df116 100644 --- a/erizo/src/erizo/WebRtcConnection.cpp +++ b/erizo/src/erizo/WebRtcConnection.cpp @@ -116,9 +116,12 @@ void WebRtcConnection::initializePipeline() { handler_manager_ = std::make_shared(shared_from_this()); pipeline_->addService(shared_from_this()); pipeline_->addService(handler_manager_); + pipeline_->addService(stats_); pipeline_->addFront(std::make_shared(this)); + pipeline_->addFront(std::make_shared()); + pipeline_->addFront(std::make_shared(this)); pipeline_->finalize(); pipeline_initialized_ = true; diff --git a/erizo/src/erizo/rtp/SenderBandwidthEstimantionHandler.cpp b/erizo/src/erizo/rtp/SenderBandwidthEstimantionHandler.cpp index 08902a790e..0d876610b0 100644 --- a/erizo/src/erizo/rtp/SenderBandwidthEstimantionHandler.cpp +++ b/erizo/src/erizo/rtp/SenderBandwidthEstimantionHandler.cpp @@ -8,14 +8,14 @@ DEFINE_LOGGER(SenderBandwidthEstimationHandler, "rtp.SenderBandwidthEstimationHa constexpr duration SenderBandwidthEstimationHandler::kMinUpdateEstimateInterval; SenderBandwidthEstimationHandler::SenderBandwidthEstimationHandler(std::shared_ptr the_clock) : - stream_{nullptr}, bwe_listener_{nullptr}, clock_{the_clock}, initialized_{false}, enabled_{true}, + connection_{nullptr}, bwe_listener_{nullptr}, clock_{the_clock}, initialized_{false}, enabled_{true}, received_remb_{false}, period_packets_sent_{0}, estimated_bitrate_{0}, estimated_loss_{0}, estimated_rtt_{0}, last_estimate_update_{clock::now()}, sender_bwe_{new SendSideBandwidthEstimation()} { sender_bwe_->SetSendBitrate(kStartSendBitrate); }; SenderBandwidthEstimationHandler::SenderBandwidthEstimationHandler(const SenderBandwidthEstimationHandler&& handler) : // NOLINT - stream_{handler.stream_}, + connection_{handler.connection_}, bwe_listener_{handler.bwe_listener_}, clock_{handler.clock_}, initialized_{handler.initialized_}, @@ -42,11 +42,10 @@ void SenderBandwidthEstimationHandler::notifyUpdate() { return; } auto pipeline = getContext()->getPipelineShared(); - if (pipeline && !stream_) { - stream_ = pipeline->getService().get(); - processor_ = pipeline->getService(); + if (pipeline && !connection_) { + connection_ = pipeline->getService().get(); } - if (!stream_) { + if (!connection_) { return; } stats_ = pipeline->getService(); @@ -69,19 +68,16 @@ void SenderBandwidthEstimationHandler::read(Context *ctx, std::shared_ptr(packet_pointer); rtcp_length = (ntohs(chead->length) + 1) * 4; total_length += rtcp_length; - ELOG_DEBUG("%s ssrc %u, sourceSSRC %u, PacketType %u", stream_->toLog(), + ELOG_DEBUG("%s ssrc %u, sourceSSRC %u, PacketType %u", connection_->toLog(), chead->getSSRC(), chead->getSourceSSRC(), chead->getPacketType()); switch (chead->packettype) { case RTCP_Receiver_PT: { - if (chead->getSourceSSRC() != stream_->getVideoSinkSSRC()) { - continue; - } ELOG_DEBUG("%s, Analyzing Video RR: PacketLost %u, Ratio %u, current_block %d, blocks %d" ", sourceSSRC %u, ssrc %u", - stream_->toLog(), + connection_->toLog(), chead->getLostPackets(), chead->getFractionLost(), current_block, @@ -102,7 +98,7 @@ void SenderBandwidthEstimationHandler::read(Context *ctx, std::shared_ptrsr_send_time - delay_since_last_ms; ELOG_DEBUG("%s message: Updating Estimate with RR, fraction_lost: %u, " "delay: %u, period_packets_sent_: %u", - stream_->toLog(), chead->getFractionLost(), delay, period_packets_sent_); + connection_->toLog(), chead->getFractionLost(), delay, period_packets_sent_); sender_bwe_->UpdateReceiverBlock(chead->getFractionLost(), delay, period_packets_sent_, now_ms); period_packets_sent_ = 0; @@ -118,15 +114,16 @@ void SenderBandwidthEstimationHandler::read(Context *ctx, std::shared_ptrnow()); uint64_t remb_bitrate = chead->getBrMantis() << chead->getBrExp(); - uint64_t bitrate = estimated_bitrate_ !=0 ? estimated_bitrate_ : remb_bitrate; - uint64_t cappedBitrate = bitrate < processor_->getMaxVideoBW() ? bitrate : processor_->getMaxVideoBW(); - chead->setREMBBitRate(cappedBitrate); + uint64_t bitrate = estimated_bitrate_ != 0 ? estimated_bitrate_ : remb_bitrate; + + // We update the REMB with the latest estimation + chead->setREMBBitRate(bitrate); ELOG_DEBUG("%s message: Updating estimate REMB, bitrate: %lu, estimated_bitrate %lu, remb_bitrate %lu", - stream_->toLog(), cappedBitrate, estimated_bitrate_, remb_bitrate); + connection_->toLog(), bitrate, estimated_bitrate_, remb_bitrate); sender_bwe_->UpdateReceiverEstimate(now_ms, remb_bitrate); updateEstimate(); } else { - ELOG_DEBUG("%s message: Unsupported AFB Packet not REMB", stream_->toLog()); + ELOG_DEBUG("%s message: Unsupported AFB Packet not REMB", connection_->toLog()); } } } @@ -150,8 +147,7 @@ void SenderBandwidthEstimationHandler::write(Context *ctx, std::shared_ptrgetPacketType() == RTCP_Sender_PT && - chead->getSSRC() == stream_->getVideoSinkSSRC()) { + } else if (chead->getPacketType() == RTCP_Sender_PT) { analyzeSr(chead); } ctx->fireWrite(std::move(packet)); @@ -161,7 +157,7 @@ void SenderBandwidthEstimationHandler::analyzeSr(RtcpHeader* chead) { uint64_t now = ClockUtils::timePointToMs(clock_->now()); uint32_t ntp; ntp = chead->get32MiddleNtp(); - ELOG_DEBUG("%s message: adding incoming SR to list, ntp: %u", stream_->toLog(), ntp); + ELOG_DEBUG("%s message: adding incoming SR to list, ntp: %u", connection_->toLog(), ntp); sr_delay_data_.push_back(std::shared_ptr( new SrDelayData(ntp, now))); if (sr_delay_data_.size() >= kMaxSrListSize) { sr_delay_data_.pop_front(); @@ -171,10 +167,12 @@ void SenderBandwidthEstimationHandler::analyzeSr(RtcpHeader* chead) { void SenderBandwidthEstimationHandler::updateEstimate() { sender_bwe_->CurrentEstimate(&estimated_bitrate_, &estimated_loss_, &estimated_rtt_); - stats_->getNode()["total"].insertStat("senderBitrateEstimation", + if (stats_) { + stats_->getNode()["total"].insertStat("senderBitrateEstimation", CumulativeStat{static_cast(estimated_bitrate_)}); + } ELOG_DEBUG("%s message: estimated bitrate %d, loss %u, rtt %ld", - stream_->toLog(), estimated_bitrate_, estimated_loss_, estimated_rtt_); + connection_->toLog(), estimated_bitrate_, estimated_loss_, estimated_rtt_); if (bwe_listener_) { bwe_listener_->onBandwidthEstimate(estimated_bitrate_, estimated_loss_, estimated_rtt_); } diff --git a/erizo/src/erizo/rtp/SenderBandwidthEstimationHandler.h b/erizo/src/erizo/rtp/SenderBandwidthEstimationHandler.h index 0430d6d658..33242f3c61 100644 --- a/erizo/src/erizo/rtp/SenderBandwidthEstimationHandler.h +++ b/erizo/src/erizo/rtp/SenderBandwidthEstimationHandler.h @@ -2,8 +2,7 @@ #define ERIZO_SRC_ERIZO_RTP_SENDERBANDWIDTHESTIMATIONHANDLER_H_ #include "pipeline/Handler.h" #include "./logger.h" -#include "./MediaStream.h" -#include "./rtp/RtcpProcessor.h" +#include "./WebRtcConnection.h" #include "lib/Clock.h" #include "webrtc/modules/bitrate_controller/send_side_bandwidth_estimation.h" @@ -51,8 +50,7 @@ class SenderBandwidthEstimationHandler : public Handler, } private: - MediaStream* stream_; - std::shared_ptr processor_; + WebRtcConnection* connection_; SenderBandwidthEstimationListener* bwe_listener_; std::shared_ptr clock_; bool initialized_; diff --git a/erizo/src/erizo/rtp/StatsHandler.cpp b/erizo/src/erizo/rtp/StatsHandler.cpp index 790e463393..be493579d5 100644 --- a/erizo/src/erizo/rtp/StatsHandler.cpp +++ b/erizo/src/erizo/rtp/StatsHandler.cpp @@ -157,6 +157,7 @@ void StatsCalculator::processRtcpPacket(std::shared_ptr packet) { // ELOG_DEBUG("REMB Packet numSSRC %u mantissa %u exp %u, tot %lu bps", // chead->getREMBNumSSRC(), chead->getBrMantis(), chead->getBrExp(), bitrate); getStatsInfo()[ssrc].insertStat("bandwidth", CumulativeStat{bitrate}); + getStatsInfo()["total"].insertStat("senderBitrateEstimation", CumulativeStat{bitrate}); } else { ELOG_DEBUG("Unsupported AFB Packet not REMB") } diff --git a/erizo/src/test/utils/Mocks.h b/erizo/src/test/utils/Mocks.h index 965fef8efe..269db9a690 100644 --- a/erizo/src/test/utils/Mocks.h +++ b/erizo/src/test/utils/Mocks.h @@ -2,6 +2,7 @@ #define ERIZO_SRC_TEST_UTILS_MOCKS_H_ #include +#include #include #include #include