Skip to content

Commit

Permalink
Add a codec parser in front of the pipeline (#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
kekkokk authored and jcague committed Apr 3, 2018
1 parent 01a684c commit d56e03e
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 11 deletions.
1 change: 1 addition & 0 deletions erizo/src/erizo/MediaDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct DataPacket {
bool ending_of_layer_frame;
int picture_id;
std::string codec;
unsigned int clock_rate = 0;
};

class Monitor {
Expand Down
2 changes: 2 additions & 0 deletions erizo/src/erizo/MediaStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "rtp/PliPacerHandler.h"
#include "rtp/RtpPaddingGeneratorHandler.h"
#include "rtp/RtpUtils.h"
#include "rtp/PacketCodecParser.h"

namespace erizo {
DEFINE_LOGGER(MediaStream, "MediaStream");
Expand Down Expand Up @@ -205,6 +206,7 @@ void MediaStream::initializePipeline() {
pipeline_->addFront(SenderBandwidthEstimationHandler());
pipeline_->addFront(LayerDetectorHandler());
pipeline_->addFront(OutgoingStatsHandler());
pipeline_->addFront(PacketCodecParser());

pipeline_->addFront(PacketWriter(this));
pipeline_->finalize();
Expand Down
2 changes: 1 addition & 1 deletion erizo/src/erizo/NicerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ bool NicerConnection::setRemoteCandidates(const std::vector<CandidateInfo> &cand
nr_ice_peer_ctx *peer = peer_;
nr_ice_media_stream *stream = stream_;
std::shared_ptr<NicerInterface> nicer = nicer_;
async([cands, is_bundle, nicer, peer, stream, this, remote_candidates_promise]
async([cands, nicer, peer, stream, this, remote_candidates_promise]
(std::shared_ptr<NicerConnection> this_ptr) {
ELOG_DEBUG("%s message: adding remote candidates (%ld)", toLog(), cands.size());
for (const CandidateInfo &cand : cands) {
Expand Down
11 changes: 3 additions & 8 deletions erizo/src/erizo/rtp/LayerDetectorHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ void LayerDetectorHandler::disable() {
void LayerDetectorHandler::read(Context *ctx, std::shared_ptr<DataPacket> packet) {
RtcpHeader *chead = reinterpret_cast<RtcpHeader*>(packet->data);
if (!chead->isRtcp() && enabled_ && packet->type == VIDEO_PACKET) {
RtpHeader *rtp_header = reinterpret_cast<RtpHeader*>(packet->data);
RtpMap *codec = stream_->getRemoteSdpInfo()->getCodecByExternalPayloadType(rtp_header->getPayloadType());
if (codec && codec->encoding_name == "VP8") {
packet->codec = "VP8";
if (packet->codec == "VP8") {
parseLayerInfoFromVP8(packet);
} else if (codec && codec->encoding_name == "VP9") {
packet->codec = "VP9";
} else if (packet->codec == "VP9") {
parseLayerInfoFromVP9(packet);
} else if (codec && codec->encoding_name == "H264") {
packet->codec = "H264";
} else if (packet->codec == "H264") {
parseLayerInfoFromH264(packet);
}
}
Expand Down
52 changes: 52 additions & 0 deletions erizo/src/erizo/rtp/PacketCodecParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "rtp/PacketCodecParser.h"

#include "./MediaStream.h"

namespace erizo {

DEFINE_LOGGER(PacketCodecParser, "rtp.PacketCodecParser");

PacketCodecParser::PacketCodecParser() :
stream_ { nullptr }, enabled_ { true }, initialized_ { false } {
}

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

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

void PacketCodecParser::read(Context *ctx, std::shared_ptr<DataPacket> packet) {
RtcpHeader *chead = reinterpret_cast<RtcpHeader*>(packet->data);
if (!chead->isRtcp() && enabled_) {
RtpHeader *rtp_header = reinterpret_cast<RtpHeader*>(packet->data);
RtpMap *codec =
stream_->getRemoteSdpInfo()->getCodecByExternalPayloadType(
rtp_header->getPayloadType());
if (codec) {
packet->codec = codec->encoding_name;
packet->clock_rate = codec->clock_rate;
ELOG_DEBUG("Reading codec: %s, clock: %u", packet->codec.c_str(), packet->clock_rate);
}
}
ctx->fireRead(std::move(packet));
}

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

auto pipeline = getContext()->getPipelineShared();
if (!pipeline) {
return;
}

stream_ = pipeline->getService<MediaStream>().get();
if (!stream_) {
return;
}
}
} // namespace erizo
35 changes: 35 additions & 0 deletions erizo/src/erizo/rtp/PacketCodecParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef ERIZO_SRC_ERIZO_RTP_PACKETCODECPARSER_H_
#define ERIZO_SRC_ERIZO_RTP_PACKETCODECPARSER_H_

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

namespace erizo {

class MediaStream;

class PacketCodecParser: public InboundHandler {
DECLARE_LOGGER();


public:
PacketCodecParser();

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

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

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

private:
MediaStream *stream_;
bool enabled_;
bool initialized_;
};
} // namespace erizo

#endif // ERIZO_SRC_ERIZO_RTP_PACKETCODECPARSER_H_
2 changes: 1 addition & 1 deletion erizo/src/erizo/rtp/RtpPaddingRemovalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void RtpPaddingRemovalHandler::write(Context *ctx, std::shared_ptr<DataPacket> p
}
RtpUtils::forEachRRBlock(packet, [this, translator, ssrc](RtcpHeader *chead) {
if (chead->packettype == RTCP_RTP_Feedback_PT) {
RtpUtils::forEachNack(chead, [this, chead, translator, ssrc](uint16_t new_seq_num, uint16_t new_plb,
RtpUtils::forEachNack(chead, [this, translator, ssrc](uint16_t new_seq_num, uint16_t new_plb,
RtcpHeader* nack_header) {
uint16_t initial_seq_num = new_seq_num;
std::vector<uint16_t> seq_nums;
Expand Down
5 changes: 5 additions & 0 deletions erizo/src/test/rtp/LayerDetectorHandlerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <thread/Scheduler.h>
#include <rtp/LayerDetectorHandler.h>
#include <rtp/PacketCodecParser.h>
#include <rtp/RtpHeaders.h>
#include <MediaDefinitions.h>
#include <WebRtcConnection.h>
Expand All @@ -29,6 +30,7 @@ using erizo::IceConfig;
using erizo::RtpMap;
using erizo::RtpHeader;
using erizo::LayerDetectorHandler;
using erizo::PacketCodecParser;
using erizo::WebRtcConnection;
using erizo::Pipeline;
using erizo::InboundHandler;
Expand Down Expand Up @@ -71,7 +73,9 @@ class LayerDetectorHandlerVp8Test : public erizo::BaseHandlerTest,
std::vector<RtpMap>& payloads = media_stream->getRemoteSdpInfo()->getPayloadInfos();
payloads.push_back({96, "VP8"});
payloads.push_back({98, "VP9"});
codec_parser_handler = std::make_shared<PacketCodecParser>();
layer_detector_handler = std::make_shared<LayerDetectorHandler>();
pipeline->addBack(codec_parser_handler);
pipeline->addBack(layer_detector_handler);

media_stream->setVideoSourceSSRCList({kArbitrarySsrc1, kArbitrarySsrc2});
Expand All @@ -87,6 +91,7 @@ class LayerDetectorHandlerVp8Test : public erizo::BaseHandlerTest,
internalTearDown();
}

std::shared_ptr<PacketCodecParser> codec_parser_handler;
std::shared_ptr<LayerDetectorHandler> layer_detector_handler;
std::shared_ptr<DataPacket> packet;
int ssrc;
Expand Down
2 changes: 1 addition & 1 deletion erizo/src/test/rtp/RtcpNackGeneratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RtcpNackGeneratorTest :public ::testing::Test {
total_length += rtcp_length;

if (chead->packettype == RTCP_RTP_Feedback_PT) {
erizo::RtpUtils::forEachNack(chead, [chead, lost_seq_num, &found_nack](uint16_t seq_num,
erizo::RtpUtils::forEachNack(chead, [lost_seq_num, &found_nack](uint16_t seq_num,
uint16_t plb, RtcpHeader* nack_head) {
uint16_t initial_seq_num = seq_num;
if (initial_seq_num == lost_seq_num) {
Expand Down
1 change: 1 addition & 0 deletions erizo_controller/erizoAgent/log4cxx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ log4j.logger.rtp.StatsCalculator=WARN
log4j.logger.rtp.LayerDetectorHandler=WARN
log4j.logger.rtp.PliPacerHandler=WARN
log4j.logger.rtp.RtpPaddingGeneratorHandler=WARN
log4j.logger.rtp.PacketCodecParser=WARN

0 comments on commit d56e03e

Please sign in to comment.