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

RTCP: send periodic RTCP compound packets.

上级 95e74b46
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "RTC/IceServer.hpp" #include "RTC/IceServer.hpp"
#include "RTC/ProducerListener.hpp" #include "RTC/ProducerListener.hpp"
#include "RTC/RTCP/CompoundPacket.hpp" #include "RTC/RTCP/CompoundPacket.hpp"
#include "RTC/RTCP/FeedbackPsAfb.hpp"
#include "RTC/RTCP/Packet.hpp" #include "RTC/RTCP/Packet.hpp"
#include "RTC/RTCP/ReceiverReport.hpp" #include "RTC/RTCP/ReceiverReport.hpp"
#include "RTC/RemoteBitrateEstimator/RemoteBitrateEstimatorAbsSendTime.hpp" #include "RTC/RemoteBitrateEstimator/RemoteBitrateEstimatorAbsSendTime.hpp"
...@@ -21,7 +22,7 @@ ...@@ -21,7 +22,7 @@
#include "RTC/TcpServer.hpp" #include "RTC/TcpServer.hpp"
#include "RTC/TransportTuple.hpp" #include "RTC/TransportTuple.hpp"
#include "RTC/UdpSocket.hpp" #include "RTC/UdpSocket.hpp"
#include "RTC/RTCP/FeedbackPsAfb.hpp" #include "handles/Timer.hpp"
#include <json/json.h> #include <json/json.h>
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
...@@ -41,7 +42,8 @@ namespace RTC ...@@ -41,7 +42,8 @@ namespace RTC
public RTC::DtlsTransport::Listener, public RTC::DtlsTransport::Listener,
public RTC::RemoteBitrateEstimator::Listener, public RTC::RemoteBitrateEstimator::Listener,
public RTC::ProducerListener, public RTC::ProducerListener,
public RTC::ConsumerListener public RTC::ConsumerListener,
public Timer::Listener
{ {
public: public:
class Listener class Listener
...@@ -80,14 +82,15 @@ namespace RTC ...@@ -80,14 +82,15 @@ namespace RTC
void ChangeUfragPwd(std::string& usernameFragment, std::string& password); void ChangeUfragPwd(std::string& usernameFragment, std::string& password);
void SendRtpPacket(RTC::RtpPacket* packet); void SendRtpPacket(RTC::RtpPacket* packet);
void SendRtcpPacket(RTC::RTCP::Packet* packet); void SendRtcpPacket(RTC::RTCP::Packet* packet);
void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet);
bool IsConnected() const; bool IsConnected() const;
void EnableRemb(); void EnableRemb();
bool HasRemb(); bool HasRemb();
void SendRtcp(uint64_t now);
private: private:
void MayRunDtlsTransport(); void MayRunDtlsTransport();
void HandleRtcpPacket(RTC::RTCP::Packet* packet); void HandleRtcpPacket(RTC::RTCP::Packet* packet);
void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet);
RTC::Consumer* GetConsumer(uint32_t ssrc) const; RTC::Consumer* GetConsumer(uint32_t ssrc) const;
/* Private methods to unify UDP and TCP behavior. */ /* Private methods to unify UDP and TCP behavior. */
...@@ -156,6 +159,10 @@ namespace RTC ...@@ -156,6 +159,10 @@ namespace RTC
void OnConsumerClosed(RTC::Consumer* consumer) override; void OnConsumerClosed(RTC::Consumer* consumer) override;
void OnConsumerFullFrameRequired(RTC::Consumer* consumer) override; void OnConsumerFullFrameRequired(RTC::Consumer* consumer) override;
/* Pure virtual methods inherited from Timer::Listener. */
public:
void OnTimer(Timer* timer) override;
public: public:
// Passed by argument. // Passed by argument.
uint32_t transportId{ 0 }; uint32_t transportId{ 0 };
...@@ -172,6 +179,7 @@ namespace RTC ...@@ -172,6 +179,7 @@ namespace RTC
RTC::SrtpSession* srtpRecvSession{ nullptr }; RTC::SrtpSession* srtpRecvSession{ nullptr };
RTC::SrtpSession* srtpSendSession{ nullptr }; RTC::SrtpSession* srtpSendSession{ nullptr };
// Others. // Others.
Timer* timer{ nullptr };
bool allocated{ false }; bool allocated{ false };
// Others (Producers and Consumers). // Others (Producers and Consumers).
std::unordered_set<RTC::Producer*> producers; std::unordered_set<RTC::Producer*> producers;
......
...@@ -173,6 +173,12 @@ namespace RTC ...@@ -173,6 +173,12 @@ namespace RTC
// Create a DTLS agent. // Create a DTLS agent.
this->dtlsTransport = new RTC::DtlsTransport(this); this->dtlsTransport = new RTC::DtlsTransport(this);
// Create the RTCP timer.
this->timer = new Timer(this);
// Start the RTCP timer.
this->timer->Start(static_cast<uint64_t>(RTC::RTCP::MaxVideoIntervalMs / 2));
// Hack to avoid that Destroy() above attempts to delete this. // Hack to avoid that Destroy() above attempts to delete this.
this->allocated = true; this->allocated = true;
} }
...@@ -180,6 +186,9 @@ namespace RTC ...@@ -180,6 +186,9 @@ namespace RTC
Transport::~Transport() Transport::~Transport()
{ {
MS_TRACE(); MS_TRACE();
// Destroy the RTCP timer.
this->timer->Destroy();
} }
void Transport::Destroy() void Transport::Destroy()
...@@ -856,6 +865,59 @@ namespace RTC ...@@ -856,6 +865,59 @@ namespace RTC
} }
} }
void Transport::SendRtcp(uint64_t now)
{
MS_TRACE();
// - Create a CompoundPacket.
// - Request every Consumer and Producer their RTCP data.
// - Send the CompoundPacket.
std::unique_ptr<RTC::RTCP::CompoundPacket> packet(new RTC::RTCP::CompoundPacket());
for (auto& consumer : this->consumers)
{
consumer->GetRtcp(packet.get(), now);
// Send the RTCP compound packet if there is a sender report.
if (packet->GetSenderReportCount() != 0u)
{
// Ensure that the RTCP packet fits into the RTCP buffer.
if (packet->GetSize() > RTC::RTCP::BufferSize)
{
MS_WARN_TAG(rtcp, "cannot send RTCP packet, size too big (%zu bytes)", packet->GetSize());
return;
}
packet->Serialize(RTC::RTCP::Buffer);
SendRtcpCompoundPacket(packet.get());
// Reset the Compound packet.
packet.reset(new RTC::RTCP::CompoundPacket());
}
}
for (auto& producer : this->producers)
{
producer->GetRtcp(packet.get(), now);
}
// Send the RTCP compound with all receiver reports.
if (packet->GetReceiverReportCount() != 0u)
{
// Ensure that the RTCP packet fits into the RTCP buffer.
if (packet->GetSize() > RTC::RTCP::BufferSize)
{
MS_WARN_TAG(rtcp, "cannot send RTCP packet, size too big (%zu bytes)", packet->GetSize());
return;
}
packet->Serialize(RTC::RTCP::Buffer);
SendRtcpCompoundPacket(packet.get());
}
}
inline RTC::Consumer* Transport::GetConsumer(uint32_t ssrc) const inline RTC::Consumer* Transport::GetConsumer(uint32_t ssrc) const
{ {
MS_TRACE(); MS_TRACE();
...@@ -1497,4 +1559,40 @@ namespace RTC ...@@ -1497,4 +1559,40 @@ namespace RTC
{ {
// Do nothing. // Do nothing.
} }
void Transport::OnTimer(Timer* /*timer*/)
{
uint64_t interval = RTC::RTCP::MaxVideoIntervalMs;
uint32_t now = DepLibUV::GetTime();
this->SendRtcp(now);
// Recalculate next RTCP interval.
if (!this->consumers.empty())
{
// Transmission rate in kbps.
uint32_t rate = 0;
// Get the RTP sending rate.
for (auto& consumer : this->consumers)
{
rate += consumer->GetTransmissionRate(now) / 1000;
}
// Calculate bandwidth: 360 / transmission bandwidth in kbit/s
if (rate != 0u)
interval = 360000 / rate;
if (interval > RTC::RTCP::MaxVideoIntervalMs)
interval = RTC::RTCP::MaxVideoIntervalMs;
}
/*
* 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);
}
} // namespace RTC } // namespace RTC
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论