Skip to content

Commit

Permalink
Add layer bitrate calculation handler + Test page (lynckia#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
lodoyun authored Mar 7, 2017
1 parent e41532e commit 233b979
Show file tree
Hide file tree
Showing 10 changed files with 742 additions and 1 deletion.
2 changes: 2 additions & 0 deletions erizo/src/erizo/WebRtcConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "rtp/SRPacketHandler.h"
#include "rtp/SenderBandwidthEstimationHandler.h"
#include "rtp/LayerDetectorHandler.h"
#include "rtp/LayerBitrateCalculationHandler.h"
#include "rtp/QualityFilterHandler.h"
#include "rtp/QualityManager.h"
#include "rtp/PliPacerHandler.h"
Expand Down Expand Up @@ -254,6 +255,7 @@ void WebRtcConnection::initializePipeline() {
pipeline_->addFront(RtcpProcessorHandler());
pipeline_->addFront(IncomingStatsHandler());
pipeline_->addFront(FecReceiverHandler());
pipeline_->addFront(LayerBitrateCalculationHandler());
pipeline_->addFront(QualityFilterHandler());
pipeline_->addFront(RtpAudioMuteHandler());
pipeline_->addFront(RtpSlideShowHandler());
Expand Down
62 changes: 62 additions & 0 deletions erizo/src/erizo/rtp/LayerBitrateCalculationHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "rtp/LayerBitrateCalculationHandler.h"

#include <vector>

#include "./WebRtcConnection.h"
#include "lib/ClockUtils.h"

namespace erizo {

DEFINE_LOGGER(LayerBitrateCalculationHandler, "rtp.LayerBitrateCalculationHandler");

LayerBitrateCalculationHandler::LayerBitrateCalculationHandler() : enabled_{true},
initialized_{false} {}

void LayerBitrateCalculationHandler::enable() {
enabled_ = true;
}

void LayerBitrateCalculationHandler::disable() {
enabled_ = false;
}

void LayerBitrateCalculationHandler::write(Context *ctx, std::shared_ptr<dataPacket> packet) {
if (!enabled_ || !initialized_) {
ctx->fireWrite(packet);
return;
}
std::for_each(packet->compatible_spatial_layers.begin(),
packet->compatible_spatial_layers.end(), [this, packet](int &layer_num){
std::string spatial_layer_name = std::to_string(layer_num);
std::for_each(packet->compatible_temporal_layers.begin(),
packet->compatible_temporal_layers.end(), [this, packet, spatial_layer_name](int &layer_num){
std::string temporal_layer_name = std::to_string(layer_num);
if (!stats_->getNode()[kQualityLayersStatsKey][spatial_layer_name].hasChild(temporal_layer_name)) {
stats_->getNode()[kQualityLayersStatsKey][spatial_layer_name].insertStat(
temporal_layer_name, MovingIntervalRateStat{kLayerRateStatIntervalSize,
kLayerRateStatIntervals, 8.});
} else {
stats_->getNode()[kQualityLayersStatsKey][spatial_layer_name][temporal_layer_name]+=packet->length;
}
});
});
ctx->fireWrite(packet);
}


void LayerBitrateCalculationHandler::notifyUpdate() {
if (initialized_) {
return;
}

auto pipeline = getContext()->getPipelineShared();
if (!pipeline) {
return;
}
stats_ = pipeline->getService<Stats>();
if (!stats_) {
return;
}
initialized_ = true;
}
} // namespace erizo
42 changes: 42 additions & 0 deletions erizo/src/erizo/rtp/LayerBitrateCalculationHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef ERIZO_SRC_ERIZO_RTP_LAYERBITRATECALCULATIONHANDLER_H_
#define ERIZO_SRC_ERIZO_RTP_LAYERBITRATECALCULATIONHANDLER_H_


#include "./logger.h"
#include "pipeline/Handler.h"
#include "./Stats.h"

namespace erizo {

class WebRtcConnection;

constexpr duration kLayerRateStatIntervalSize = std::chrono::milliseconds(100);
constexpr uint32_t kLayerRateStatIntervals = 10;

class LayerBitrateCalculationHandler: public OutboundHandler {
DECLARE_LOGGER();


public:
LayerBitrateCalculationHandler();

void enable() override;
void disable() override;

std::string getName() override {
return "layer_bitrate_calculator";
}

void write(Context *ctx, std::shared_ptr<dataPacket> packet) override;
void notifyUpdate() override;

private:
bool enabled_;
bool initialized_;
std::shared_ptr<Stats> stats_;
const std::string kQualityLayersStatsKey = "qualityLayers";
};
} // namespace erizo

#endif // ERIZO_SRC_ERIZO_RTP_LAYERBITRATECALCULATIONHANDLER_H_

6 changes: 6 additions & 0 deletions erizo/src/erizo/rtp/SenderBandwidthEstimantionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ void SenderBandwidthEstimationHandler::notifyUpdate() {
if (!connection_) {
return;
}
stats_ = pipeline->getService<Stats>();
if (!stats_) {
return;
}
initialized_ = true;
}

Expand Down Expand Up @@ -144,6 +148,8 @@ void SenderBandwidthEstimationHandler::analyzeSr(RtcpHeader* chead) {
void SenderBandwidthEstimationHandler::updateEstimate() {
sender_bwe_->CurrentEstimate(&estimated_bitrate_, &estimated_loss_,
&estimated_rtt_);
stats_->getNode()["total"].insertStat("senderBitrateEstimation",
CumulativeStat{static_cast<uint64_t>(estimated_bitrate_)});
ELOG_DEBUG("%s message: estimated bitrate %d, loss %u, rtt %lld",
connection_->toLog(), estimated_bitrate_, estimated_loss_, estimated_rtt_);
if (bwe_listener_) {
Expand Down
1 change: 1 addition & 0 deletions erizo/src/erizo/rtp/SenderBandwidthEstimationHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class SenderBandwidthEstimationHandler : public Handler,
int64_t estimated_rtt_;
std::shared_ptr<SendSideBandwidthEstimation> sender_bwe_;
std::list<std::shared_ptr<SrDelayData>> sr_delay_data_;
std::shared_ptr<Stats> stats_;

void updateEstimate();
};
Expand Down
40 changes: 40 additions & 0 deletions erizo/src/test/rtp/LayerBitrateCalculationHandlerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>


#include <rtp/LayerBitrateCalculationHandler.h>

#include "../utils/Mocks.h"
#include "../utils/Tools.h"
#include "../utils/Matchers.h"

using ::testing::_;
using ::testing::IsNull;
using ::testing::Args;
using ::testing::Return;
using erizo::AUDIO_PACKET;
using erizo::VIDEO_PACKET;
using erizo::Pipeline;
using erizo::LayerBitrateCalculationHandler;


class LayerBitrateCalculationHandlerTest : public erizo::HandlerTest {
public:
LayerBitrateCalculationHandlerTest() {}

protected:
void setHandler() {
layer_bitrate_handler = std::make_shared<LayerBitrateCalculationHandler>();
pipeline->addBack(layer_bitrate_handler);
}

std::shared_ptr<LayerBitrateCalculationHandler> layer_bitrate_handler;
};

TEST_F(LayerBitrateCalculationHandlerTest, basicBehaviourShouldWritePackets) {
auto packet = erizo::PacketTools::createDataPacket(erizo::kArbitrarySeqNumber, AUDIO_PACKET);

EXPECT_CALL(*writer.get(), write(_, _)).
With(Args<1>(erizo::RtpHasSequenceNumber(erizo::kArbitrarySeqNumber))).Times(1);
pipeline->write(packet);
}
1 change: 0 additions & 1 deletion erizo_controller/erizoClient/src/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,6 @@ Erizo.Room = function (spec) {

sendMessageSocket('getStreamStats', stream.getID(), function (result) {
if (result) {
L.Logger.info('Got stats', result);
callback(result);
}
});
Expand Down
Loading

0 comments on commit 233b979

Please sign in to comment.