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

Rtcp: calculate RTCP interval internally

上级 0b0ade4c
......@@ -13,6 +13,7 @@
#include "RTC/RTCP/Sdes.h"
#include "Channel/Request.h"
#include "Channel/Notifier.h"
#include "handles/Timer.h"
#include <string>
#include <unordered_map>
#include <vector>
......@@ -23,7 +24,8 @@ namespace RTC
class Peer :
public RTC::Transport::Listener,
public RTC::RtpReceiver::Listener,
public RTC::RtpSender::Listener
public RTC::RtpSender::Listener,
public Timer::Listener
{
public:
class Listener
......@@ -40,7 +42,6 @@ namespace RTC
virtual void onPeerRtcpFeedback(RTC::Peer* peer, RTC::RtpSender* rtpSender, RTC::RTCP::FeedbackRtpPacket* packet) = 0;
virtual void onPeerRtcpSenderReport(RTC::Peer* peer, RTC::RtpReceiver* rtpReceiver, RTC::RTCP::SenderReport* report) = 0;
virtual void onPeerRtcpSdesChunk(RTC::Peer* peer, RTC::RtpReceiver* rtpReceiver, RTC::RTCP::SdesChunk* chunk) = 0;
virtual void onPeerRtcpCompleted(RTC::Peer* peer) = 0;
};
private:
......@@ -64,7 +65,7 @@ namespace RTC
*/
void AddRtpSender(RTC::RtpSender* rtpSender, std::string& peerName, RTC::RtpParameters* rtpParameters);
RTC::RtpSender* GetRtpSender(uint32_t ssrc);
void SendRtcp();
void SendRtcp(uint64_t now);
private:
RTC::Transport* GetTransportFromRequest(Channel::Request* request, uint32_t* transportId = nullptr);
......@@ -87,6 +88,10 @@ namespace RTC
public:
virtual void onRtpSenderClosed(RTC::RtpSender* rtpSender) override;
/* Pure virtual methods inherited from Timer::Listener. */
public:
virtual void onTimer(Timer* timer) override;
public:
// Passed by argument.
uint32_t peerId;
......@@ -97,6 +102,7 @@ namespace RTC
Listener* listener = nullptr;
Channel::Notifier* notifier = nullptr;
// Others.
Timer* timer = nullptr;
bool hasCapabilities = false;
RTC::RtpCapabilities capabilities;
std::unordered_map<uint32_t, RTC::Transport*> transports;
......
......@@ -9,6 +9,10 @@
namespace RTC { namespace RTCP
{
// Maximum interval for regular RTCP mode.
constexpr uint16_t MAX_VIDEO_INTERVAL_MS = 1000;
constexpr uint16_t MAX_AUDIO_INTERVAL_MS = 5000;
enum class Type : uint8_t
{
FIR = 192,
......
......@@ -63,7 +63,6 @@ namespace RTC
virtual void onPeerRtcpFeedback(RTC::Peer* peer, RTC::RtpSender* rtpSender, RTC::RTCP::FeedbackRtpPacket* packet) override;
virtual void onPeerRtcpSenderReport(RTC::Peer* peer, RTC::RtpReceiver* rtpReceiver, RTC::RTCP::SenderReport* report) override;
virtual void onPeerRtcpSdesChunk(RTC::Peer* peer, RTC::RtpReceiver* rtpReceiver, RTC::RTCP::SdesChunk* chunk) override;
virtual void onPeerRtcpCompleted(RTC::Peer* peer) override;
public:
// Passed by argument.
......
......@@ -7,6 +7,7 @@
#include "RTC/RtpStreamRecv.h"
#include "RTC/RTCP/ReceiverReport.h"
#include "RTC/RTCP/Feedback.h"
#include "RTC/RTCP/CompoundPacket.h"
#include "Channel/Request.h"
#include "Channel/Notifier.h"
#include <string>
......@@ -49,7 +50,7 @@ namespace RTC
RTC::RtpParameters* GetParameters();
void ReceiveRtpPacket(RTC::RtpPacket* packet);
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
RTC::RTCP::ReceiverReport* GetRtcpReceiverReport();
void GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now);
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet);
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet);
......@@ -73,6 +74,9 @@ namespace RTC
// Others.
bool rtpRawEventEnabled = false;
bool rtpObjectEventEnabled = false;
// Timestamp when last RTCP was sent.
uint64_t lastRtcpSentTime = 0;
uint16_t maxRtcpInterval;
};
/* Inline methods. */
......
......@@ -6,9 +6,11 @@
#include "RTC/RtpStreamSend.h"
#include "RTC/RtpDictionaries.h"
#include "RTC/RtpPacket.h"
#include "RTC/RtpDataCounter.h"
#include "RTC/RTCP/SenderReport.h"
#include "RTC/RTCP/Sdes.h"
#include "RTC/RTCP/FeedbackRtpNack.h"
#include "RTC/RTCP/CompoundPacket.h"
#include "Channel/Request.h"
#include "Channel/Notifier.h"
#include <unordered_set>
......@@ -48,10 +50,10 @@ namespace RTC
RTC::RtpParameters* GetParameters();
void SendRtpPacket(RTC::RtpPacket* packet);
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
RTC::RTCP::SenderReport* GetRtcpSenderReport();
void ReceiveRtcpSdesChunk(RTC::RTCP::SdesChunk* chunk);
RTC::RTCP::SdesChunk* GetRtcpSdesChunk();
void GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now);
void ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket);
uint32_t GetTransmissionRate(uint64_t now);
private:
void RetransmitRtpPacket(RTC::RtpPacket* packet);
......@@ -77,6 +79,14 @@ namespace RTC
// Sender Report holding the RTP stats.
std::unique_ptr<RTC::RTCP::SenderReport> senderReport;
std::unique_ptr<RTC::RTCP::SdesChunk> sdesChunk;
// RTP counters.
RTC::RtpDataCounter transmitted;
// Timestamp when last RTCP was sent.
uint64_t lastRtcpSentTime = 0;
uint16_t maxRtcpInterval;
// TODO: keep track of retransmitted data too.
//RTC::RtpDataCounter retransmitted;
};
/* Inline methods. */
......@@ -113,12 +123,6 @@ namespace RTC
this->senderReport->Serialize();
};
inline
RTC::RTCP::SenderReport* RtpSender::GetRtcpSenderReport()
{
return this->senderReport.release();
};
inline
void RtpSender::ReceiveRtcpSdesChunk(RTC::RTCP::SdesChunk* chunk)
{
......@@ -127,9 +131,9 @@ namespace RTC
};
inline
RTC::RTCP::SdesChunk* RtpSender::GetRtcpSdesChunk()
uint32_t RtpSender::GetTransmissionRate(uint64_t now)
{
return this->sdesChunk.release();
return this->transmitted.GetRate(now);
};
}
......
......@@ -7,6 +7,7 @@
#include "RTC/RTCP/FeedbackRtpNack.h"
#include "MediaSoupError.h"
#include "Logger.h"
#include "DepLibUV.h"
namespace RTC
{
......@@ -23,11 +24,19 @@ namespace RTC
notifier(notifier)
{
MS_TRACE();
this->timer = new Timer(this);
// Start the RTCP timer.
this->timer->Start(uint64_t(RTC::RTCP::MAX_VIDEO_INTERVAL_MS / 2));
}
Peer::~Peer()
{
MS_TRACE();
// Close the RTCP timer.
this->timer->Close();
}
void Peer::Close()
......@@ -510,7 +519,7 @@ namespace RTC
return nullptr;
}
void Peer::SendRtcp()
void Peer::SendRtcp(uint64_t now)
{
MS_TRACE();
......@@ -521,7 +530,7 @@ namespace RTC
for (auto it = this->transports.begin(); it != this->transports.end(); ++it)
{
RTC::RTCP::CompoundPacket packet;
std::unique_ptr<RTC::RTCP::CompoundPacket> packet(new RTC::RTCP::CompoundPacket());
RTC::Transport* transport = it->second;
for (auto it = this->rtpSenders.begin(); it != this->rtpSenders.end(); ++it)
......@@ -531,24 +540,26 @@ namespace RTC
if (rtpSender->GetTransport() != transport)
continue;
RTC::RTCP::SenderReport* report = rtpSender->GetRtcpSenderReport();
rtpSender->GetRtcp(packet.get(), now);
if (report)
// Send one RTCP compound packet per sender report.
if (packet->GetSenderReportCount())
{
packet.AddSenderReport(report);
// Ensure that the RTCP packet fits into the RTCP buffer.
if (packet->GetSize() > MS_RTCP_BUFFER_SIZE)
{
MS_WARN_TAG(rtcp, "cannot send RTCP packet, size too big (%zu bytes)", packet->GetSize());
return;
}
RTC::RTCP::SdesChunk* chunk = rtpSender->GetRtcpSdesChunk();
if (chunk)
packet.AddSdesChunk(chunk);
packet->Serialize(Peer::rtcpBuffer);
transport->SendRtcpCompoundPacket(packet.get());
break;
// reset the Compound packet.
packet.reset(new RTC::RTCP::CompoundPacket());
}
}
// TMP: only send RR if SR is also being sent.
// if (packet.GetSenderReportCount() == 0)
// return;
for (auto it = this->rtpReceivers.begin(); it != this->rtpReceivers.end(); ++it)
{
RTC::RtpReceiver* rtpReceiver = it->second;
......@@ -556,21 +567,22 @@ namespace RTC
if (rtpReceiver->GetTransport() != transport)
continue;
RTC::RTCP::ReceiverReport* report = rtpReceiver->GetRtcpReceiverReport();
if (report)
packet.AddReceiverReport(report);
rtpReceiver->GetRtcp(packet.get(), now);
}
// Ensure that the RTCP packet fits into the RTCP buffer.
if (packet.GetSize() > MS_RTCP_BUFFER_SIZE)
// Send one RTCP compound with all receiver reports.
if (packet->GetReceiverReportCount())
{
MS_WARN_TAG(rtcp, "cannot send RTCP packet, size too big (%zu bytes)", packet.GetSize());
return;
}
// Ensure that the RTCP packet fits into the RTCP buffer.
if (packet->GetSize() > MS_RTCP_BUFFER_SIZE)
{
MS_WARN_TAG(rtcp, "cannot send RTCP packet, size too big (%zu bytes)", packet->GetSize());
return;
}
packet.Serialize(Peer::rtcpBuffer);
transport->SendRtcpCompoundPacket(&packet);
packet->Serialize(Peer::rtcpBuffer);
transport->SendRtcpCompoundPacket(packet.get());
}
}
}
......@@ -904,10 +916,6 @@ namespace RTC
packet = packet->GetNext();
}
// TMP: Notify the listener about an incoming RTCP processing termination.
// Used for now as a trigger to relay the processed RTCP data accordingly.
this->listener->onPeerRtcpCompleted(this);
}
void Peer::onRtpReceiverClosed(RTC::RtpReceiver* rtpReceiver)
......@@ -939,4 +947,41 @@ namespace RTC
// Notify the listener (Room) so it can remove this RtpSender from its map.
this->listener->onPeerRtpSenderClosed(this, rtpSender);
}
void Peer::onTimer(Timer* timer)
{
uint64_t interval = RTC::RTCP::MAX_VIDEO_INTERVAL_MS;
uint32_t now = DepLibUV::GetTime();
// Transmission rate in kbps.
uint32_t rate = 0;
this->SendRtcp(now);
// Recalculate next RTCP interval.
if (this->rtpSenders.size()) {
// Get the RTP sending rate.
for (auto& kv : this->rtpSenders)
{
RTC::RtpSender* rtpSender = kv.second;
rate += rtpSender->GetTransmissionRate(now) / 1000;
}
// Calculate bandwidth: 360 / transmission bandwidth in kbit/s
if (rate)
interval = 360000 / rate;
if (interval > RTC::RTCP::MAX_VIDEO_INTERVAL_MS)
interval = RTC::RTCP::MAX_VIDEO_INTERVAL_MS;
}
/* The interval between RTCP packets is varied randomly over the range
* [0.5,1.5] times the calculated interval to avoid unintended synchronization
* of all participants.
*/
interval *= static_cast<float>(Utils::Crypto::GetRandomUInt(5, 15)) / 10;
this->timer->Start(interval);
}
}
......@@ -685,18 +685,4 @@ namespace RTC
rtpSender->ReceiveRtcpSdesChunk(chunk);
}
}
void Room::onPeerRtcpCompleted(RTC::Peer* peer)
{
MS_TRACE();
// Tell all the peers but the one in the argument to generate and send their RTCP.
for (auto it = this->peers.begin(); it != this->peers.end(); ++it)
{
RTC::Peer* current_peer = it->second;
if (current_peer != peer)
current_peer->SendRtcp();
}
}
}
......@@ -22,6 +22,11 @@ namespace RTC
notifier(notifier)
{
MS_TRACE();
if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MAX_AUDIO_INTERVAL_MS;
else
this->maxRtcpInterval = RTC::RTCP::MAX_VIDEO_INTERVAL_MS;
}
RtpReceiver::~RtpReceiver()
......@@ -284,20 +289,20 @@ namespace RTC
// SSRC values in received RTP packets to match the chosen random values.
}
RTC::RTCP::ReceiverReport* RtpReceiver::GetRtcpReceiverReport()
void RtpReceiver::GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now)
{
if (this->rtpStream)
{
RTC::RTCP::ReceiverReport* report = this->rtpStream->GetRtcpReceiverReport();
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) >= this->maxRtcpInterval)
{
RTC::RTCP::ReceiverReport* report = this->rtpStream->GetRtcpReceiverReport();
// TODO: This assumes a single stream for now.
report->SetSsrc(this->rtpParameters->encodings[0].ssrc);
// TODO: This assumes a single stream for now.
report->SetSsrc(this->rtpParameters->encodings[0].ssrc);
packet->AddReceiverReport(report);
return report;
}
else
{
return nullptr;
this->lastRtcpSentTime = now;
}
}
}
......
......@@ -23,6 +23,11 @@ namespace RTC
notifier(notifier)
{
MS_TRACE();
if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MAX_AUDIO_INTERVAL_MS;
else
this->maxRtcpInterval = RTC::RTCP::MAX_VIDEO_INTERVAL_MS;
}
RtpSender::~RtpSender()
......@@ -260,8 +265,28 @@ namespace RTC
// Send the packet.
this->transport->SendRtpPacket(packet);
// Save RTP data
transmitted.Update(packet);
}
void RtpSender::GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now)
{
// TODO: Until we generate Sender Report.
if (this->senderReport)
{
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) >= this->maxRtcpInterval)
{
packet->AddSenderReport(this->senderReport.release());
if (this->sdesChunk)
packet->AddSdesChunk(this->sdesChunk.release());
this->lastRtcpSentTime = now;
}
}
};
void RtpSender::ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket)
{
MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论