提交 6238c99e authored 作者: José Luis Millán's avatar José Luis Millán

RtpStream: move to RtpStream all common data from children classes

上级 dc1ba011
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define MS_RTC_RTP_STREAM_HPP #define MS_RTC_RTP_STREAM_HPP
#include "common.hpp" #include "common.hpp"
#include "RTC/RtpDataCounter.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
#include "handles/Timer.hpp" #include "handles/Timer.hpp"
...@@ -35,8 +36,9 @@ namespace RTC ...@@ -35,8 +36,9 @@ namespace RTC
explicit RtpStream(RTC::RtpStream::Params& params); explicit RtpStream(RTC::RtpStream::Params& params);
virtual ~RtpStream(); virtual ~RtpStream();
virtual Json::Value ToJson() = 0; virtual Json::Value ToJson();
virtual bool ReceivePacket(RTC::RtpPacket* packet); virtual bool ReceivePacket(RTC::RtpPacket* packet);
uint32_t GetBitRate();
uint32_t GetSsrc(); uint32_t GetSsrc();
uint8_t GetPayloadType(); uint8_t GetPayloadType();
const RTC::RtpCodecMimeType& GetMimeType() const; const RTC::RtpCodecMimeType& GetMimeType() const;
...@@ -68,13 +70,18 @@ namespace RTC ...@@ -68,13 +70,18 @@ namespace RTC
uint32_t cycles{ 0 }; // Shifted count of seq. number cycles. uint32_t cycles{ 0 }; // Shifted count of seq. number cycles.
uint32_t baseSeq{ 0 }; // Base seq number. uint32_t baseSeq{ 0 }; // Base seq number.
uint32_t badSeq{ 0 }; // Last 'bad' seq number + 1. uint32_t badSeq{ 0 }; // Last 'bad' seq number + 1.
uint32_t received{ 0 }; // Packets received.
uint32_t expectedPrior{ 0 }; // Packet expected at last interval. uint32_t expectedPrior{ 0 }; // Packet expected at last interval.
uint32_t receivedPrior{ 0 }; // Packet received at last interval. uint32_t receivedPrior{ 0 }; // Packet received at last interval.
// Others. // Others.
uint32_t maxTimestamp{ 0 }; // Highest timestamp seen. uint32_t maxTimestamp{ 0 }; // Highest timestamp seen.
Timer* healthCheckTimer{ nullptr }; Timer* healthCheckTimer{ nullptr };
bool healthy{ true }; bool healthy{ true };
uint32_t totalLost{ 0 };
uint8_t fractionLost{ 0 };
uint32_t jitter{ 0 };
uint32_t rtt{ 0 };
// RTP counters.
RTC::RtpDataCounter counter;
}; };
/* Inline instance methods. */ /* Inline instance methods. */
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "RTC/NackGenerator.hpp" #include "RTC/NackGenerator.hpp"
#include "RTC/RTCP/ReceiverReport.hpp" #include "RTC/RTCP/ReceiverReport.hpp"
#include "RTC/RTCP/SenderReport.hpp" #include "RTC/RTCP/SenderReport.hpp"
#include "RTC/RtpDataCounter.hpp"
#include "RTC/RtpStream.hpp" #include "RTC/RtpStream.hpp"
namespace RTC namespace RTC
...@@ -24,14 +23,12 @@ namespace RTC ...@@ -24,14 +23,12 @@ namespace RTC
RtpStreamRecv(Listener* listener, RTC::RtpStream::Params& params); RtpStreamRecv(Listener* listener, RTC::RtpStream::Params& params);
~RtpStreamRecv() override; ~RtpStreamRecv() override;
Json::Value ToJson() override;
bool ReceivePacket(RTC::RtpPacket* packet) override; bool ReceivePacket(RTC::RtpPacket* packet) override;
bool ReceiveRtxPacket(RTC::RtpPacket* packet); bool ReceiveRtxPacket(RTC::RtpPacket* packet);
RTC::RTCP::ReceiverReport* GetRtcpReceiverReport(); RTC::RTCP::ReceiverReport* GetRtcpReceiverReport();
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report); void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
void SetRtx(uint8_t payloadType, uint32_t ssrc); void SetRtx(uint8_t payloadType, uint32_t ssrc);
void RequestKeyFrame(); void RequestKeyFrame();
uint32_t GetBitRate();
private: private:
void CalculateJitter(uint32_t rtpTimestamp); void CalculateJitter(uint32_t rtpTimestamp);
...@@ -55,14 +52,11 @@ namespace RTC ...@@ -55,14 +52,11 @@ namespace RTC
uint64_t lastSrReceived{ 0 }; // Wallclock time representing the most recent sender report uint64_t lastSrReceived{ 0 }; // Wallclock time representing the most recent sender report
// arrival. // arrival.
uint32_t transit{ 0 }; // Relative trans time for prev pkt. uint32_t transit{ 0 }; // Relative trans time for prev pkt.
uint32_t jitter{ 0 }; // Estimated jitter.
std::unique_ptr<RTC::NackGenerator> nackGenerator; std::unique_ptr<RTC::NackGenerator> nackGenerator;
// RTX related. // RTX related.
bool hasRtx{ false }; bool hasRtx{ false };
uint8_t rtxPayloadType{ 0 }; uint8_t rtxPayloadType{ 0 };
uint32_t rtxSsrc{ 0 }; uint32_t rtxSsrc{ 0 };
// RTP counters.
RTC::RtpDataCounter receivedCounter;
}; };
} // namespace RTC } // namespace RTC
......
...@@ -29,13 +29,11 @@ namespace RTC ...@@ -29,13 +29,11 @@ namespace RTC
RtpStreamSend(RTC::RtpStream::Params& params, size_t bufferSize); RtpStreamSend(RTC::RtpStream::Params& params, size_t bufferSize);
~RtpStreamSend() override; ~RtpStreamSend() override;
Json::Value ToJson() override;
bool ReceivePacket(RTC::RtpPacket* packet) override; bool ReceivePacket(RTC::RtpPacket* packet) override;
void ReceiveRtcpReceiverReport(RTC::RTCP::ReceiverReport* report); void ReceiveRtcpReceiverReport(RTC::RTCP::ReceiverReport* report);
void RequestRtpRetransmission( void RequestRtpRetransmission(
uint16_t seq, uint16_t bitmask, std::vector<RTC::RtpPacket*>& container); uint16_t seq, uint16_t bitmask, std::vector<RTC::RtpPacket*>& container);
RTC::RTCP::SenderReport* GetRtcpSenderReport(uint64_t now); RTC::RTCP::SenderReport* GetRtcpSenderReport(uint64_t now);
uint32_t GetRtt() const;
void SetRtx(uint8_t payloadType, uint32_t ssrc); void SetRtx(uint8_t payloadType, uint32_t ssrc);
bool HasRtx() const; bool HasRtx() const;
void RtxEncode(RtpPacket* packet); void RtxEncode(RtpPacket* packet);
...@@ -55,10 +53,8 @@ namespace RTC ...@@ -55,10 +53,8 @@ namespace RTC
Buffer buffer; Buffer buffer;
private: private:
size_t receivedBytes{ 0 }; // Bytes received.
uint64_t lastPacketTimeMs{ 0 }; // Time (MS) when the last packet was received. uint64_t lastPacketTimeMs{ 0 }; // Time (MS) when the last packet was received.
uint32_t lastPacketRtpTimestamp{ 0 }; // RTP Timestamp of the last packet. uint32_t lastPacketRtpTimestamp{ 0 }; // RTP Timestamp of the last packet.
uint32_t rtt{ 0 }; // Round trip time.
// RTX related. // RTX related.
bool hasRtx{ false }; bool hasRtx{ false };
...@@ -67,11 +63,6 @@ namespace RTC ...@@ -67,11 +63,6 @@ namespace RTC
uint16_t rtxSeq{ 0 }; uint16_t rtxSeq{ 0 };
}; };
inline uint32_t RtpStreamSend::GetRtt() const
{
return this->rtt;
}
inline bool RtpStreamSend::HasRtx() const inline bool RtpStreamSend::HasRtx() const
{ {
return this->hasRtx; return this->hasRtx;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
// #define MS_LOG_DEV // #define MS_LOG_DEV
#include "RTC/RtpStream.hpp" #include "RTC/RtpStream.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp" #include "Logger.hpp"
namespace RTC namespace RTC
...@@ -36,6 +37,35 @@ namespace RTC ...@@ -36,6 +37,35 @@ namespace RTC
this->healthCheckTimer->Destroy(); this->healthCheckTimer->Destroy();
} }
Json::Value RtpStream::ToJson()
{
MS_TRACE();
static const Json::StaticString JsonStringBitRate{ "bitrate" };
static const Json::StaticString JsonStringJitter{ "jitter" };
static const Json::StaticString JsonStringMaxTimestamp{ "maxTimestamp" };
static const Json::StaticString JsonStringParams{ "params" };
static const Json::StaticString JsonStringPacketCount{ "packetCount" };
static const Json::StaticString JsonStringOctetCount{ "octetCount" };
static const Json::StaticString JsonStringRtt{ "rtt" };
static const Json::StaticString JsonStringFractionLost{ "fractionLost" };
static const Json::StaticString JsonStringTotalLost{ "totalLost" };
Json::Value json(Json::objectValue);
json[JsonStringBitRate] = Json::UInt{ GetBitRate() };
json[JsonStringJitter] = Json::UInt{ this->jitter };
json[JsonStringMaxTimestamp] = Json::UInt{ this->maxTimestamp };
json[JsonStringParams] = this->params.ToJson();
json[JsonStringPacketCount] = static_cast<Json::UInt>(this->counter.GetPacketCount());
json[JsonStringOctetCount] = static_cast<Json::UInt>(this->counter.GetBytes());
json[JsonStringRtt] = Json::UInt{ this->rtt };
json[JsonStringFractionLost] = Json::UInt{ this->fractionLost };
json[JsonStringTotalLost] = Json::UInt{ this->totalLost };
return json;
}
bool RtpStream::ReceivePacket(RTC::RtpPacket* packet) bool RtpStream::ReceivePacket(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
...@@ -74,6 +104,13 @@ namespace RTC ...@@ -74,6 +104,13 @@ namespace RTC
return true; return true;
} }
uint32_t RtpStream::GetBitRate()
{
uint64_t now = DepLibUV::GetTime();
return this->counter.GetRate(now);
}
void RtpStream::InitSeq(uint16_t seq) void RtpStream::InitSeq(uint16_t seq)
{ {
MS_TRACE(); MS_TRACE();
...@@ -150,7 +187,8 @@ namespace RTC ...@@ -150,7 +187,8 @@ namespace RTC
// Do nothing. // Do nothing.
} }
this->received++; // Increase counters.
this->counter.Update(packet);
return true; return true;
} }
......
...@@ -27,29 +27,6 @@ namespace RTC ...@@ -27,29 +27,6 @@ namespace RTC
MS_TRACE(); MS_TRACE();
} }
Json::Value RtpStreamRecv::ToJson()
{
MS_TRACE();
static const Json::StaticString JsonStringParams{ "params" };
static const Json::StaticString JsonStringReceived{ "received" };
static const Json::StaticString JsonStringMaxTimestamp{ "maxTimestamp" };
static const Json::StaticString JsonStringTransit{ "transit" };
static const Json::StaticString JsonStringJitter{ "jitter" };
static const Json::StaticString JsonStringBitRate{ "bitrate" };
Json::Value json(Json::objectValue);
json[JsonStringParams] = this->params.ToJson();
json[JsonStringReceived] = Json::UInt{ this->received };
json[JsonStringMaxTimestamp] = Json::UInt{ this->maxTimestamp };
json[JsonStringTransit] = Json::UInt{ this->transit };
json[JsonStringJitter] = Json::UInt{ this->jitter };
json[JsonStringBitRate] = Json::UInt{ GetBitRate() };
return json;
}
bool RtpStreamRecv::ReceivePacket(RTC::RtpPacket* packet) bool RtpStreamRecv::ReceivePacket(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
...@@ -62,8 +39,6 @@ namespace RTC ...@@ -62,8 +39,6 @@ namespace RTC
return false; return false;
} }
this->receivedCounter.Update(packet);
// Calculate Jitter. // Calculate Jitter.
CalculateJitter(packet->GetTimestamp()); CalculateJitter(packet->GetTimestamp());
...@@ -157,7 +132,7 @@ namespace RTC ...@@ -157,7 +132,7 @@ namespace RTC
// Calculate Packets Expected and Lost. // Calculate Packets Expected and Lost.
uint32_t expected = (this->cycles + this->maxSeq) - this->baseSeq + 1; uint32_t expected = (this->cycles + this->maxSeq) - this->baseSeq + 1;
int32_t totalLost = expected - this->received; this->totalLost = expected - this->counter.GetPacketCount();
report->SetTotalLost(totalLost); report->SetTotalLost(totalLost);
...@@ -166,19 +141,18 @@ namespace RTC ...@@ -166,19 +141,18 @@ namespace RTC
this->expectedPrior = expected; this->expectedPrior = expected;
uint32_t receivedInterval = this->received - this->receivedPrior; uint32_t receivedInterval = this->counter.GetPacketCount() - this->receivedPrior;
this->receivedPrior = this->received; this->receivedPrior = counter.GetPacketCount();
int32_t lostInterval = expectedInterval - receivedInterval; int32_t lostInterval = expectedInterval - receivedInterval;
uint8_t fractionLost;
if (expectedInterval == 0 || lostInterval <= 0) if (expectedInterval == 0 || lostInterval <= 0)
fractionLost = 0; this->fractionLost = 0;
else else
fractionLost = (lostInterval << 8) / expectedInterval; this->fractionLost = (lostInterval << 8) / expectedInterval;
report->SetFractionLost(fractionLost); report->SetFractionLost(this->fractionLost);
// Fill the rest of the report. // Fill the rest of the report.
report->SetLastSeq(static_cast<uint32_t>(this->maxSeq) + this->cycles); report->SetLastSeq(static_cast<uint32_t>(this->maxSeq) + this->cycles);
...@@ -227,13 +201,6 @@ namespace RTC ...@@ -227,13 +201,6 @@ namespace RTC
} }
} }
uint32_t RtpStreamRecv::GetBitRate()
{
uint64_t now = DepLibUV::GetTime();
return this->receivedCounter.GetRate(now);
}
void RtpStreamRecv::CalculateJitter(uint32_t rtpTimestamp) void RtpStreamRecv::CalculateJitter(uint32_t rtpTimestamp)
{ {
MS_TRACE(); MS_TRACE();
...@@ -261,7 +228,7 @@ namespace RTC ...@@ -261,7 +228,7 @@ namespace RTC
auto now = DepLibUV::GetTime(); auto now = DepLibUV::GetTime();
bool healthy = true; bool healthy = true;
if (this->receivedCounter.GetRate(now) == 0) if (this->counter.GetRate(now) == 0)
healthy = false; healthy = false;
// NOTE: Update the 'healthy' value after notification. // NOTE: Update the 'healthy' value after notification.
......
...@@ -31,27 +31,6 @@ namespace RTC ...@@ -31,27 +31,6 @@ namespace RTC
ClearRetransmissionBuffer(); ClearRetransmissionBuffer();
} }
Json::Value RtpStreamSend::ToJson()
{
MS_TRACE();
static const Json::StaticString JsonStringParams{ "params" };
static const Json::StaticString JsonStringReceived{ "received" };
static const Json::StaticString JsonStringMaxTimestamp{ "maxTimestamp" };
static const Json::StaticString JsonStringReceivedBytes{ "receivedBytes" };
static const Json::StaticString JsonStringRtt{ "rtt" };
Json::Value json(Json::objectValue);
json[JsonStringParams] = this->params.ToJson();
json[JsonStringReceived] = Json::UInt{ this->received };
json[JsonStringMaxTimestamp] = Json::UInt{ this->maxTimestamp };
json[JsonStringReceivedBytes] = static_cast<Json::UInt>(this->receivedBytes);
json[JsonStringRtt] = Json::UInt{ this->rtt };
return json;
}
bool RtpStreamSend::ReceivePacket(RTC::RtpPacket* packet) bool RtpStreamSend::ReceivePacket(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
...@@ -64,9 +43,6 @@ namespace RTC ...@@ -64,9 +43,6 @@ namespace RTC
if (!this->storage.empty()) if (!this->storage.empty())
StorePacket(packet); StorePacket(packet);
// Increase packet counters.
this->receivedBytes += packet->GetPayloadLength();
// Record current time and RTP timestamp. // Record current time and RTP timestamp.
this->lastPacketTimeMs = DepLibUV::GetTime(); this->lastPacketTimeMs = DepLibUV::GetTime();
this->lastPacketRtpTimestamp = packet->GetTimestamp(); this->lastPacketRtpTimestamp = packet->GetTimestamp();
...@@ -95,6 +71,10 @@ namespace RTC ...@@ -95,6 +71,10 @@ namespace RTC
// RTT in milliseconds. // RTT in milliseconds.
this->rtt = ((rtt >> 16) * 1000); this->rtt = ((rtt >> 16) * 1000);
this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000; this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000;
this->totalLost = report->GetTotalLost();
this->fractionLost = report->GetFractionLost();
this->jitter = report->GetJitter();
} }
// This method looks for the requested RTP packets and inserts them into the // This method looks for the requested RTP packets and inserts them into the
...@@ -302,13 +282,13 @@ namespace RTC ...@@ -302,13 +282,13 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (this->received == 0u) if (this->counter.GetPacketCount() == 0u)
return nullptr; return nullptr;
auto report = new RTC::RTCP::SenderReport(); auto report = new RTC::RTCP::SenderReport();
report->SetPacketCount(this->received); report->SetPacketCount(this->counter.GetPacketCount());
report->SetOctetCount(this->receivedBytes); report->SetOctetCount(this->counter.GetBytes());
Utils::Time::Ntp ntp{}; Utils::Time::Ntp ntp{};
Utils::Time::CurrentTimeNtp(ntp); Utils::Time::CurrentTimeNtp(ntp);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论