Unverified 提交 56eda745 authored 作者: José Luis Millán's avatar José Luis Millán 提交者: GitHub

RTC Logger (#1048)

RtcLogger: RtpPacket
上级 feaaf202
#ifndef MS_RTC_RTC_LOGGER_HPP
#define MS_RTC_RTC_LOGGER_HPP
#include "common.hpp"
#include <absl/container/flat_hash_map.h>
namespace RTC
{
namespace RtcLogger
{
class RtpPacket
{
public:
enum class DropReason : uint8_t
{
NONE = 0,
PRODUCER_NOT_FOUND,
RECV_RTP_STREAM_NOT_FOUND,
RECV_RTP_STREAM_DISCARDED,
CONSUMER_INACTIVE,
INVALID_TARGET_LAYER,
UNSUPPORTED_PAYLOAD_TYPE,
NOT_A_KEYFRAME,
SPATIAL_LAYER_MISMATCH,
TOO_HIGH_TIMESTAMP_EXTRA_NEEDED,
PACKET_PREVIOUS_TO_SPATIAL_LAYER_SWITCH,
DROPPED_BY_CODEC,
SEND_RTP_STREAM_DISCARDED,
};
static absl::flat_hash_map<DropReason, std::string> dropReason2String;
RtpPacket() = default;
~RtpPacket() = default;
void Sent();
void Dropped(DropReason dropReason);
private:
void Log() const;
void Clear();
public:
uint64_t timestamp;
std::string recvTransportId{};
std::string sendTransportId{};
std::string routerId{};
std::string producerId{};
std::string consumerId{};
uint32_t recvRtpTimestamp;
uint32_t sendRtpTimestamp;
uint16_t recvSeqNumber;
uint16_t sendSeqNumber;
bool dropped;
DropReason dropReason{ DropReason::NONE };
};
}; // namespace RtcLogger
} // namespace RTC
#endif
......@@ -4,6 +4,7 @@
#include "common.hpp"
#include "Utils.hpp"
#include "RTC/Codecs/PayloadDescriptorHandler.hpp"
#include "RTC/RtcLogger.hpp"
#include <absl/container/flat_hash_map.h>
#include <array>
#include <nlohmann/json.hpp>
......@@ -606,6 +607,9 @@ namespace RTC
void ShiftPayload(size_t payloadOffset, size_t shift, bool expand = true);
public:
RtcLogger::RtpPacket logger;
private:
void ParseExtensions();
......
......@@ -38,6 +38,12 @@ if get_option('ms_log_file_line')
]
endif
if get_option('ms_rtc_logger_rtp')
cpp_args += [
'-DMS_RTC_LOGGER_RTP',
]
endif
common_sources = [
'src/lib.cpp',
'src/DepLibSRTP.cpp',
......@@ -85,6 +91,7 @@ common_sources = [
'src/RTC/Producer.cpp',
'src/RTC/RateCalculator.cpp',
'src/RTC/Router.cpp',
'src/RTC/RtcLogger.cpp',
'src/RTC/RtpListener.cpp',
'src/RTC/RtpObserver.cpp',
'src/RTC/RtpPacket.cpp',
......
option('ms_log_trace', type : 'boolean', value : false, description : 'When enabled, logs the current method/function if current log level is "debug"')
option('ms_log_file_line', type : 'boolean', value : false, description : 'When enabled, all the logging macros print more verbose information, including current file and line')
option('ms_rtc_logger_rtp', type : 'boolean', value : false, description : 'When enabled, prints a line with information for each RTP packet')
......@@ -201,8 +201,14 @@ namespace RTC
{
MS_TRACE();
packet->logger.consumerId = this->id;
if (!IsActive())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::CONSUMER_INACTIVE);
return;
}
auto payloadType = packet->GetPayloadType();
......@@ -212,6 +218,8 @@ namespace RTC
{
MS_DEBUG_DEV("payload type not supported [payloadType:%" PRIu8 "]", payloadType);
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::UNSUPPORTED_PAYLOAD_TYPE);
return;
}
......@@ -223,7 +231,11 @@ namespace RTC
// If we need to sync, support key frames and this is not a key frame, ignore
// the packet.
if (syncRequired && this->keyFrameSupported && !packet->IsKeyFrame())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::NOT_A_KEYFRAME);
return;
}
// Whether this is the first packet after re-sync.
const bool isSyncPacket = syncRequired;
......@@ -252,6 +264,9 @@ namespace RTC
packet->SetSsrc(ssrc);
packet->SetSequenceNumber(seq);
packet->logger.sendRtpTimestamp = packet->GetTimestamp();
packet->logger.sendSeqNumber = seq;
if (isSyncPacket)
{
MS_DEBUG_TAG(
......
......@@ -672,6 +672,8 @@ namespace RTC
{
MS_TRACE();
packet->logger.producerId = this->id;
// Reset current packet.
this->currentRtpPacket = nullptr;
......@@ -684,6 +686,8 @@ namespace RTC
{
MS_WARN_TAG(rtp, "no stream found for received packet [ssrc:%" PRIu32 "]", packet->GetSsrc());
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::RECV_RTP_STREAM_NOT_FOUND);
return ReceiveRtpPacketResult::DISCARDED;
}
......@@ -705,6 +709,8 @@ namespace RTC
if (this->mapSsrcRtpStream.size() > numRtpStreamsBefore)
NotifyNewRtpStream(rtpStream);
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::RECV_RTP_STREAM_DISCARDED);
return result;
}
}
......@@ -716,7 +722,11 @@ namespace RTC
// Process the packet.
if (!rtpStream->ReceiveRtxPacket(packet))
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::RECV_RTP_STREAM_NOT_FOUND);
return result;
}
}
// Should not happen.
else
......
......@@ -706,6 +706,8 @@ namespace RTC
{
MS_TRACE();
packet->logger.routerId = this->id;
auto& consumers = this->mapProducerConsumers.at(producer);
if (!consumers.empty())
......
#define MS_CLASS "RTC::RtcLogger"
// #define MS_LOG_DEV_LEVEL 3
#include "RTC/RtcLogger.hpp"
#include "Logger.hpp"
namespace RTC
{
namespace RtcLogger
{
// clang-format off
absl::flat_hash_map<RtpPacket::DropReason, std::string> RtpPacket::dropReason2String = {
{ RtpPacket::DropReason::NONE, "None" },
{ RtpPacket::DropReason::PRODUCER_NOT_FOUND, "ProducerNotFound" },
{ RtpPacket::DropReason::RECV_RTP_STREAM_NOT_FOUND, "RecvRtpStreamNotFound" },
{ RtpPacket::DropReason::RECV_RTP_STREAM_DISCARDED, "RecvRtpStreamDiscarded" },
{ RtpPacket::DropReason::CONSUMER_INACTIVE, "ConsumerInactive" },
{ RtpPacket::DropReason::INVALID_TARGET_LAYER, "InvalidTargetLayer" },
{ RtpPacket::DropReason::UNSUPPORTED_PAYLOAD_TYPE, "UnsupportedPayloadType" },
{ RtpPacket::DropReason::NOT_A_KEYFRAME, "NotAKeyframe" },
{ RtpPacket::DropReason::SPATIAL_LAYER_MISMATCH, "SpatialLayerMismatch" },
{ RtpPacket::DropReason::TOO_HIGH_TIMESTAMP_EXTRA_NEEDED, "TooHighTimestampExtraNeeded" },
{ RtpPacket::DropReason::PACKET_PREVIOUS_TO_SPATIAL_LAYER_SWITCH, "PacketPreviousToSpatialLayerSwitch" },
{ RtpPacket::DropReason::DROPPED_BY_CODEC, "DroppedByCodec" },
{ RtpPacket::DropReason::SEND_RTP_STREAM_DISCARDED, "SendRtpStreamDiscarded" },
};
// clang-format on
void RtpPacket::Sent()
{
MS_TRACE();
this->dropped = false;
Log();
Clear();
}
void RtpPacket::Dropped(DropReason dropReason)
{
MS_TRACE();
this->dropped = true;
this->dropReason = dropReason;
Log();
Clear();
}
void RtpPacket::Log() const
{
#ifdef MS_RTC_LOGGER_RTP
MS_TRACE();
std::cout << "{";
std::cout << "\"timestamp\": " << this->timestamp;
if (!this->recvTransportId.empty())
{
std::cout << ", \"recvTransportId\": \"" << this->recvTransportId << "\"";
}
if (!this->sendTransportId.empty())
{
std::cout << ", \"sendTransportId\": \"" << this->sendTransportId << "\"";
}
if (!this->routerId.empty())
{
std::cout << ", \"routerId\": \"" << this->routerId << "\"";
}
if (!this->producerId.empty())
{
std::cout << ", \"producerId\": \"" << this->producerId << "\"";
}
if (!this->consumerId.empty())
{
std::cout << ", \"consumerId\": \"" << this->consumerId << "\"";
}
std::cout << ", \"recvRtpTimestamp\": " << this->recvRtpTimestamp;
std::cout << ", \"sendRtpTimestamp\": " << this->sendRtpTimestamp;
std::cout << ", \"recvSeqNumber\": " << this->recvSeqNumber;
std::cout << ", \"sendSeqNumber\": " << this->sendSeqNumber;
std::cout << ", \"dropped\": " << (this->dropped ? "true" : "false");
std::cout << ", \"dropReason\": '" << dropReason2String[this->dropReason] << "'";
std::cout << "}" << std::endl;
#endif
}
void RtpPacket::Clear()
{
MS_TRACE();
this->sendTransportId = {};
this->routerId = {};
this->producerId = {};
this->sendSeqNumber = { 0 };
this->dropped = { false };
this->dropReason = { DropReason::NONE };
}
} // namespace RtcLogger
} // namespace RTC
......@@ -2,6 +2,7 @@
// #define MS_LOG_DEV_LEVEL 3
#include "RTC/RtpPacket.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp"
#include <cstring> // std::memcpy(), std::memmove(), std::memset()
#include <iterator> // std::ostream_iterator
......@@ -139,6 +140,14 @@ namespace RTC
// Parse RFC 5285 header extension.
ParseExtensions();
// Avoid retrieving the time if RTC logger is disabled.
#ifdef MS_RTC_LOGGER_RTP
// Initialize logger.
this->logger.timestamp = DepLibUV::GetTimeMs();
this->logger.recvRtpTimestamp = this->GetTimestamp();
this->logger.recvSeqNumber = this->GetSequenceNumber();
#endif
}
RtpPacket::~RtpPacket()
......
......@@ -265,8 +265,14 @@ namespace RTC
{
MS_TRACE();
packet->logger.consumerId = this->id;
if (!IsActive())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::CONSUMER_INACTIVE);
return;
}
auto payloadType = packet->GetPayloadType();
......@@ -276,6 +282,8 @@ namespace RTC
{
MS_DEBUG_DEV("payload type not supported [payloadType:%" PRIu8 "]", payloadType);
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::UNSUPPORTED_PAYLOAD_TYPE);
return;
}
......@@ -292,13 +300,19 @@ namespace RTC
this->rtpSeqManager.Drop(packet->GetSequenceNumber());
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::DROPPED_BY_CODEC);
return;
}
// If we need to sync, support key frames and this is not a key frame, ignore
// the packet.
if (this->syncRequired && this->keyFrameSupported && !packet->IsKeyFrame())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::NOT_A_KEYFRAME);
return;
}
// Whether this is the first packet after re-sync.
const bool isSyncPacket = this->syncRequired;
......@@ -327,6 +341,9 @@ namespace RTC
packet->SetSsrc(this->rtpParameters.encodings[0].ssrc);
packet->SetSequenceNumber(seq);
packet->logger.sendRtpTimestamp = packet->GetTimestamp();
packet->logger.sendSeqNumber = seq;
if (isSyncPacket)
{
MS_DEBUG_TAG(
......
......@@ -664,11 +664,21 @@ namespace RTC
{
MS_TRACE();
packet->logger.consumerId = this->id;
if (!IsActive())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::CONSUMER_INACTIVE);
return;
}
if (this->targetTemporalLayer == -1)
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::INVALID_TARGET_LAYER);
return;
}
auto payloadType = packet->GetPayloadType();
......@@ -678,6 +688,8 @@ namespace RTC
{
MS_DEBUG_DEV("payload type not supported [payloadType:%" PRIu8 "]", payloadType);
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::UNSUPPORTED_PAYLOAD_TYPE);
return;
}
......@@ -690,7 +702,11 @@ namespace RTC
{
// Ignore if not a key frame.
if (!packet->IsKeyFrame())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::NOT_A_KEYFRAME);
return;
}
shouldSwitchCurrentSpatialLayer = true;
......@@ -702,12 +718,18 @@ namespace RTC
// drop it.
else if (spatialLayer != this->currentSpatialLayer)
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::SPATIAL_LAYER_MISMATCH);
return;
}
// If we need to sync and this is not a key frame, ignore the packet.
if (this->syncRequired && !packet->IsKeyFrame())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::NOT_A_KEYFRAME);
return;
}
// Whether this is the first packet after re-sync.
const bool isSyncPacket = this->syncRequired;
......@@ -814,6 +836,8 @@ namespace RTC
this->syncRequired = false;
this->spatialLayerToSync = -1;
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::TOO_HIGH_TIMESTAMP_EXTRA_NEEDED);
return;
}
......@@ -854,6 +878,9 @@ namespace RTC
if (SeqManager<uint16_t>::IsSeqLowerThan(
packet->GetSequenceNumber(), this->snReferenceSpatialLayer))
{
packet->logger.Dropped(
RtcLogger::RtpPacket::DropReason::PACKET_PREVIOUS_TO_SPATIAL_LAYER_SWITCH);
return;
}
else if (SeqManager<uint16_t>::IsSeqHigherThan(
......@@ -898,6 +925,8 @@ namespace RTC
{
this->rtpSeqManager.Drop(packet->GetSequenceNumber());
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::DROPPED_BY_CODEC);
return;
}
......@@ -921,6 +950,9 @@ namespace RTC
packet->SetSequenceNumber(seq);
packet->SetTimestamp(timestamp);
packet->logger.sendRtpTimestamp = timestamp;
packet->logger.sendSeqNumber = seq;
if (isSyncPacket)
{
MS_DEBUG_TAG(
......@@ -959,6 +991,8 @@ namespace RTC
origSsrc,
origSeq,
origTimestamp);
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::SEND_RTP_STREAM_DISCARDED);
}
// Restore packet fields.
......
......@@ -593,8 +593,14 @@ namespace RTC
{
MS_TRACE();
packet->logger.consumerId = this->id;
if (!IsActive())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::CONSUMER_INACTIVE);
return;
}
// clang-format off
if (
......@@ -603,6 +609,8 @@ namespace RTC
)
// clang-format on
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::INVALID_TARGET_LAYER);
return;
}
......@@ -614,12 +622,18 @@ namespace RTC
{
MS_DEBUG_DEV("payload type not supported [payloadType:%" PRIu8 "]", payloadType);
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::UNSUPPORTED_PAYLOAD_TYPE);
return;
}
// If we need to sync and this is not a key frame, ignore the packet.
if (this->syncRequired && !packet->IsKeyFrame())
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::NOT_A_KEYFRAME);
return;
}
// Whether this is the first packet after re-sync.
const bool isSyncPacket = this->syncRequired;
......@@ -646,6 +660,8 @@ namespace RTC
{
this->rtpSeqManager.Drop(packet->GetSequenceNumber());
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::DROPPED_BY_CODEC);
return;
}
......@@ -673,6 +689,9 @@ namespace RTC
packet->SetSsrc(this->rtpParameters.encodings[0].ssrc);
packet->SetSequenceNumber(seq);
packet->logger.sendRtpTimestamp = packet->GetTimestamp();
packet->logger.sendSeqNumber = seq;
if (marker)
{
packet->SetMarker(true);
......
......@@ -1692,6 +1692,8 @@ namespace RTC
{
MS_TRACE();
packet->logger.recvTransportId = this->id;
// Apply the Transport RTP header extension ids so the RTP listener can use them.
packet->SetMidExtensionId(this->recvRtpHeaderExtensionIds.mid);
packet->SetRidExtensionId(this->recvRtpHeaderExtensionIds.rid);
......@@ -1712,6 +1714,8 @@ namespace RTC
if (!producer)
{
packet->logger.Dropped(RtcLogger::RtpPacket::DropReason::PRODUCER_NOT_FOUND);
MS_WARN_TAG(
rtp,
"no suitable Producer for received RTP packet [ssrc:%" PRIu32 ", payloadType:%" PRIu8 "]",
......@@ -2663,6 +2667,9 @@ namespace RTC
{
MS_TRACE();
packet->logger.sendTransportId = this->id;
packet->logger.Sent();
// Update abs-send-time if present.
packet->UpdateAbsSendTime(DepLibUV::GetTimeMs());
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论