Skip to content

Commit

Permalink
Move SenderBandwidthEstimationHandler to WebRtcConnection (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcague authored Oct 4, 2019
1 parent 8612391 commit d9dc184
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
2 changes: 0 additions & 2 deletions erizo/src/erizo/MediaStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -403,7 +402,6 @@ void MediaStream::initializePipeline() {
pipeline_->addFront(std::make_shared<RtcpFeedbackGenerationHandler>());
pipeline_->addFront(std::make_shared<RtpRetransmissionHandler>());
pipeline_->addFront(std::make_shared<SRPacketHandler>());
pipeline_->addFront(std::make_shared<SenderBandwidthEstimationHandler>());
pipeline_->addFront(std::make_shared<LayerDetectorHandler>());
pipeline_->addFront(std::make_shared<OutgoingStatsHandler>());
pipeline_->addFront(std::make_shared<PacketCodecParser>());
Expand Down
3 changes: 3 additions & 0 deletions erizo/src/erizo/WebRtcConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,12 @@ void WebRtcConnection::initializePipeline() {
handler_manager_ = std::make_shared<HandlerManager>(shared_from_this());
pipeline_->addService(shared_from_this());
pipeline_->addService(handler_manager_);
pipeline_->addService(stats_);

pipeline_->addFront(std::make_shared<ConnectionPacketReader>(this));

pipeline_->addFront(std::make_shared<SenderBandwidthEstimationHandler>());

pipeline_->addFront(std::make_shared<ConnectionPacketWriter>(this));
pipeline_->finalize();
pipeline_initialized_ = true;
Expand Down
42 changes: 20 additions & 22 deletions erizo/src/erizo/rtp/SenderBandwidthEstimantionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ DEFINE_LOGGER(SenderBandwidthEstimationHandler, "rtp.SenderBandwidthEstimationHa
constexpr duration SenderBandwidthEstimationHandler::kMinUpdateEstimateInterval;

SenderBandwidthEstimationHandler::SenderBandwidthEstimationHandler(std::shared_ptr<Clock> 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_},
Expand All @@ -42,11 +42,10 @@ void SenderBandwidthEstimationHandler::notifyUpdate() {
return;
}
auto pipeline = getContext()->getPipelineShared();
if (pipeline && !stream_) {
stream_ = pipeline->getService<MediaStream>().get();
processor_ = pipeline->getService<RtcpProcessor>();
if (pipeline && !connection_) {
connection_ = pipeline->getService<WebRtcConnection>().get();
}
if (!stream_) {
if (!connection_) {
return;
}
stats_ = pipeline->getService<Stats>();
Expand All @@ -69,19 +68,16 @@ void SenderBandwidthEstimationHandler::read(Context *ctx, std::shared_ptr<DataPa
chead = reinterpret_cast<RtcpHeader*>(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,
Expand All @@ -102,7 +98,7 @@ void SenderBandwidthEstimationHandler::read(Context *ctx, std::shared_ptr<DataPa
uint32_t delay = now_ms - (*value)->sr_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;
Expand All @@ -118,15 +114,16 @@ void SenderBandwidthEstimationHandler::read(Context *ctx, std::shared_ptr<DataPa
received_remb_ = true;
int64_t now_ms = ClockUtils::timePointToMs(clock_->now());
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());
}
}
}
Expand All @@ -150,8 +147,7 @@ void SenderBandwidthEstimationHandler::write(Context *ctx, std::shared_ptr<DataP
updateEstimate();
last_estimate_update_ = now;
}
} else if (chead->getPacketType() == RTCP_Sender_PT &&
chead->getSSRC() == stream_->getVideoSinkSSRC()) {
} else if (chead->getPacketType() == RTCP_Sender_PT) {
analyzeSr(chead);
}
ctx->fireWrite(std::move(packet));
Expand All @@ -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<SrDelayData>( new SrDelayData(ntp, now)));
if (sr_delay_data_.size() >= kMaxSrListSize) {
sr_delay_data_.pop_front();
Expand All @@ -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<uint64_t>(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_);
}
Expand Down
6 changes: 2 additions & 4 deletions erizo/src/erizo/rtp/SenderBandwidthEstimationHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -51,8 +50,7 @@ class SenderBandwidthEstimationHandler : public Handler,
}

private:
MediaStream* stream_;
std::shared_ptr<RtcpProcessor> processor_;
WebRtcConnection* connection_;
SenderBandwidthEstimationListener* bwe_listener_;
std::shared_ptr<Clock> clock_;
bool initialized_;
Expand Down
1 change: 1 addition & 0 deletions erizo/src/erizo/rtp/StatsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ void StatsCalculator::processRtcpPacket(std::shared_ptr<DataPacket> 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")
}
Expand Down
1 change: 1 addition & 0 deletions erizo/src/test/utils/Mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define ERIZO_SRC_TEST_UTILS_MOCKS_H_

#include <WebRtcConnection.h>
#include <MediaStream.h>
#include <pipeline/Handler.h>
#include <rtp/RtcpProcessor.h>
#include <rtp/QualityManager.h>
Expand Down

0 comments on commit d9dc184

Please sign in to comment.