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

Rtcp: local receiver report generation

上级 9621f000
......@@ -11,6 +11,7 @@ public:
static void PrintVersion();
static void RunLoop();
static uv_loop_t* GetLoop();
static uint64_t GetTime();
private:
static uv_loop_t* loop;
......@@ -24,4 +25,10 @@ uv_loop_t* DepLibUV::GetLoop()
return DepLibUV::loop;
}
inline
uint64_t DepLibUV::GetTime()
{
return uv_now(DepLibUV::loop);
}
#endif
......@@ -50,7 +50,7 @@ namespace RTC
RTC::RtpParameters* GetParameters();
void ReceiveRtpPacket(RTC::RtpPacket* packet);
void RequestRtpRetransmission(uint16_t seq, uint16_t bitmask, std::vector<RTC::RtpPacket*>& container);
void ReceiveRtcpReceiverReport(RTC::RTCP::ReceiverReport* report);
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
RTC::RTCP::ReceiverReport* GetRtcpReceiverReport();
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet);
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet);
......@@ -74,8 +74,6 @@ namespace RTC
// Others.
bool rtpRawEventEnabled = false;
bool rtpObjectEventEnabled = false;
// Receiver Report holding the RTP stats.
std::unique_ptr<RTC::RTCP::ReceiverReport> receiverReport;
};
/* Inline methods. */
......@@ -106,10 +104,13 @@ namespace RTC
}
inline
RTC::RTCP::ReceiverReport* RtpReceiver::GetRtcpReceiverReport()
void RtpReceiver::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
{
return this->receiverReport.release();
};
// TODO: This assumes a single stream for now.
if (this->rtpStream)
this->rtpStream->ReceiveRtcpSenderReport(report);
}
}
#endif
......@@ -3,6 +3,8 @@
#include "common.h"
#include "RTC/RtpPacket.h"
#include "RTC/RTCP/ReceiverReport.h"
#include "RTC/RTCP/SenderReport.h"
#include <vector>
#include <list>
......@@ -29,18 +31,24 @@ namespace RTC
bool ReceivePacket(RTC::RtpPacket* packet);
void RequestRtpRetransmission(uint16_t seq, uint16_t bitmask, std::vector<RTC::RtpPacket*>& container);
RTC::RTCP::ReceiverReport* GetRtcpReceiverReport();
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
private:
void InitSeq(uint16_t seq);
bool UpdateSeq(uint16_t seq);
void CleanBuffer();
void StorePacket(RTC::RtpPacket* packet);
void CalculateJitter(uint32_t rtpTimestamp);
void Dump();
private:
// Given as argument.
uint32_t clockRate = 0;
bool started = false; // Whether at least a RTP packet has been received.
uint32_t last_sr_timestamp = 0; // The middle 32 bits out of 64 in the NTP timestamp received in the most recent sender report.
uint64_t last_sr_received = 0; // Wallclock time representing the most recent sender report arrival.
// https://tools.ietf.org/html/rfc3550#appendix-A.1 stuff.
uint16_t max_seq = 0; // Highest seq. number seen.
uint32_t cycles = 0; // Shifted count of seq. number cycles.
......
......@@ -604,9 +604,8 @@ namespace RTC
MS_ASSERT(this->mapRtpSenderRtpReceiver.find(rtpSender) != this->mapRtpSenderRtpReceiver.end(), "RtpSender not present in the map");
auto& rtpReceiver = this->mapRtpSenderRtpReceiver[rtpSender];
rtpReceiver->ReceiveRtcpReceiverReport(report);
// TODO: Calculate real downstream stats for the given RtpSender,
// considering the ReceiverReport generated locally and this one.
}
void Room::onPeerRtcpFeedback(RTC::Peer* peer, RTC::RtpSender* rtpSender, RTC::RTCP::FeedbackPsPacket* packet)
......@@ -668,6 +667,9 @@ namespace RTC
{
MS_TRACE();
// RtpReceiver needs the sender report in order to generate it's receiver report.
rtpReceiver->ReceiveRtcpSenderReport(report);
MS_ASSERT(this->mapRtpReceiverRtpSenders.find(rtpReceiver) != this->mapRtpReceiverRtpSenders.end(), "RtpReceiver not present in the map");
auto& rtpSenders = this->mapRtpReceiverRtpSenders[rtpReceiver];
......
......@@ -323,12 +323,17 @@ namespace RTC
// SSRC values in received RTP packets to match the chosen random values.
}
void RtpReceiver::ReceiveRtcpReceiverReport(RTC::RTCP::ReceiverReport* report)
RTC::RTCP::ReceiverReport* RtpReceiver::GetRtcpReceiverReport()
{
MS_TRACE();
this->receiverReport.reset(new RTC::RTCP::ReceiverReport(report));
this->receiverReport->Serialize();
if (this->rtpStream)
{
RTC::RTCP::ReceiverReport* report = this->rtpStream->GetRtcpReceiverReport();
// TODO: This assumes a single stream for now.
report->SetSsrc(this->rtpParameters->encodings[0].ssrc);
return report;
}
else
return nullptr;
};
void RtpReceiver::ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet)
......
......@@ -4,6 +4,7 @@
// #define MS_LOG_DEV
#include "RTC/RtpStream.h"
#include "DepLibUV.h"
#include "Logger.h"
#define MIN_SEQUENTIAL 2
......@@ -62,6 +63,9 @@ namespace RTC
// Dump();
}
// Calculate Jitter
this->CalculateJitter(packet->GetTimestamp());
return true;
}
......@@ -330,6 +334,68 @@ namespace RTC
(*new_buffer_it).packet = packet->Clone(store);
}
void RtpStream::CalculateJitter(uint32_t rtpTimestamp)
{
if (!this->clockRate)
return;
int transit = DepLibUV::GetTime() - (rtpTimestamp*1000/this->clockRate);
int d = transit - this->transit;
this->transit = transit;
if (d < 0) d = -d;
this->jitter += (1./16.) * ((double)d - this->jitter);
}
RTC::RTCP::ReceiverReport* RtpStream::GetRtcpReceiverReport()
{
RTC::RTCP::ReceiverReport* report = new RTC::RTCP::ReceiverReport();
// Calculate Packets Expected and Lost
uint32_t expected = (this->cycles + this->max_seq) - this->base_seq + 1;
int32_t total_lost = expected - this->received;
report->SetTotalLost(total_lost);
// Calculate Fraction Lost
uint32_t expected_interval = expected - this->expected_prior;
this->expected_prior = expected;
uint32_t received_interval = this->received - this->received_prior;
this->received_prior = this->received;
uint32_t lost_interval = expected_interval - received_interval;
uint8_t fraction_lost;
if (expected_interval == 0 || lost_interval <= 0)
fraction_lost = 0;
else
fraction_lost = (lost_interval << 8) / expected_interval;
report->SetFractionLost(fraction_lost);
// Fill the rest of the report
report->SetLastSeq((uint32_t)this->max_seq + this->cycles);
report->SetJitter(this->jitter);
if (this->last_sr_received)
{
uint32_t dlsr = (DepLibUV::GetTime() - this->last_sr_received) / 1000 * 65536;
report->SetDelaySinceLastSenderReport(dlsr);
report->SetLastSenderReport(this->last_sr_timestamp);
}
else {
report->SetDelaySinceLastSenderReport(0);
report->SetLastSenderReport(0);
}
return report;
}
void RtpStream::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
{
this->last_sr_received = DepLibUV::GetTime();
this->last_sr_timestamp = htonl(report->GetNtpSec()) << 16;
this->last_sr_timestamp += htonl(report->GetNtpFrac()) >> 16;
}
// TODO: TMP
void RtpStream::Dump()
{
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论