提交 0323649f authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Merge pull request #130 from versatica/v2_rtcp

V2 RTCP handling
...@@ -10,11 +10,6 @@ ...@@ -10,11 +10,6 @@
#include "RTC/ProducerListener.hpp" #include "RTC/ProducerListener.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
#include "RTC/Transport.hpp" #include "RTC/Transport.hpp"
// TODO: Let's see.
// #include "RTC/RTCP/Feedback.hpp"
// #include "RTC/RTCP/ReceiverReport.hpp"
// #include "RTC/RTCP/Sdes.hpp"
// #include "RTC/RTCP/SenderReport.hpp"
#include "handles/Timer.hpp" #include "handles/Timer.hpp"
#include <json/json.h> #include <json/json.h>
#include <unordered_map> #include <unordered_map>
...@@ -61,6 +56,7 @@ namespace RTC ...@@ -61,6 +56,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::Transport::Listener. */ /* Pure virtual methods inherited from RTC::Transport::Listener. */
public: public:
void OnTransportClosed(RTC::Transport* transport) override; void OnTransportClosed(RTC::Transport* transport) override;
void OnTransportReceiveRtcpFeedback(RTC::Transport* transport, RTC::RTCP::FeedbackPsPacket* packet, RTC::Consumer* consumer) override;
/* Pure virtual methods inherited from RTC::ProducerListener. */ /* Pure virtual methods inherited from RTC::ProducerListener. */
public: public:
......
...@@ -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,6 +22,7 @@ ...@@ -21,6 +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 "handles/Timer.hpp"
#include <json/json.h> #include <json/json.h>
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
...@@ -40,13 +42,15 @@ namespace RTC ...@@ -40,13 +42,15 @@ 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
{ {
public: public:
virtual void OnTransportClosed(RTC::Transport* transport) = 0; virtual void OnTransportClosed(RTC::Transport* transport) = 0;
virtual void OnTransportReceiveRtcpFeedback(RTC::Transport* transport, RTC::RTCP::FeedbackPsPacket* packet, RTC::Consumer* consumer) = 0;
}; };
public: public:
...@@ -78,14 +82,15 @@ namespace RTC ...@@ -78,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. */
...@@ -154,6 +159,10 @@ namespace RTC ...@@ -154,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 };
...@@ -170,6 +179,7 @@ namespace RTC ...@@ -170,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;
......
...@@ -1277,6 +1277,19 @@ namespace RTC ...@@ -1277,6 +1277,19 @@ namespace RTC
this->transports.erase(transport->transportId); this->transports.erase(transport->transportId);
} }
void Router::OnTransportReceiveRtcpFeedback(RTC::Transport* transport, RTC::RTCP::FeedbackPsPacket* packet, RTC::Consumer* consumer)
{
MS_TRACE();
MS_ASSERT(
this->mapConsumerProducer.find(consumer) != this->mapConsumerProducer.end(),
"Consumer not present in mapConsumerProducer");
auto* producer = this->mapConsumerProducer[consumer];
producer->ReceiveRtcpFeedback(packet);
}
void Router::OnProducerClosed(RTC::Producer* producer) void Router::OnProducerClosed(RTC::Producer* producer)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -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()
...@@ -670,19 +679,245 @@ namespace RTC ...@@ -670,19 +679,245 @@ namespace RTC
break; break;
} }
// TODO: Handle tons of RTCP types. case RTCP::Type::PSFB:
{
auto* feedback = dynamic_cast<RTCP::FeedbackPsPacket*>(packet);
switch (feedback->GetMessageType())
{
case RTCP::FeedbackPs::MessageType::PLI:
case RTCP::FeedbackPs::MessageType::FIR:
{
auto* consumer = GetConsumer(feedback->GetMediaSsrc());
if (consumer == nullptr)
{
MS_WARN_TAG(
rtcp,
"no Consumer found for received %s Feedback packet "
"[sender ssrc:%" PRIu32 ", media ssrc:%" PRIu32 "]",
RTCP::FeedbackPsPacket::MessageType2String(feedback->GetMessageType()).c_str(),
feedback->GetMediaSsrc(),
feedback->GetMediaSsrc());
break;
}
consumer->RequestFullFrame();
break;
}
case RTCP::FeedbackPs::MessageType::AFB:
{
auto* afb = dynamic_cast<RTCP::FeedbackPsAfbPacket*>(feedback);
// Ignore REMB requests.
if (afb->GetApplication() == RTCP::FeedbackPsAfbPacket::Application::REMB)
break;
}
// [[fallthrough]]; (C++17)
case RTCP::FeedbackPs::MessageType::SLI:
case RTCP::FeedbackPs::MessageType::RPSI:
{
auto* consumer = GetConsumer(feedback->GetMediaSsrc());
if (consumer == nullptr)
{
MS_WARN_TAG(
rtcp,
"no Consumer found for received %s Feedback packet "
"[sender ssrc:%" PRIu32 ", media ssrc:%" PRIu32 "]",
RTCP::FeedbackPsPacket::MessageType2String(feedback->GetMessageType()).c_str(),
feedback->GetMediaSsrc(),
feedback->GetMediaSsrc());
break;
}
listener->OnTransportReceiveRtcpFeedback(this, feedback, consumer);
break;
}
default:
{
MS_WARN_TAG(
rtcp,
"ignoring unsupported %s Feedback packet "
"[sender ssrc:%" PRIu32 ", media ssrc:%" PRIu32 "]",
RTCP::FeedbackPsPacket::MessageType2String(feedback->GetMessageType()).c_str(),
feedback->GetMediaSsrc(),
feedback->GetMediaSsrc());
break;
}
}
break;
}
case RTCP::Type::RTPFB:
{
auto* feedback = dynamic_cast<RTCP::FeedbackRtpPacket*>(packet);
auto* consumer = GetConsumer(feedback->GetMediaSsrc());
if (consumer == nullptr)
{
MS_WARN_TAG(
rtcp,
"no Consumer found for received NACK Feedback packet "
"[sender ssrc:%" PRIu32 ", media ssrc:%" PRIu32 "]",
feedback->GetMediaSsrc(),
feedback->GetMediaSsrc());
}
switch (feedback->GetMessageType())
{
case RTCP::FeedbackRtp::MessageType::NACK:
{
auto* nackPacket = dynamic_cast<RTC::RTCP::FeedbackRtpNackPacket*>(packet);
consumer->ReceiveNack(nackPacket);
}
break;
default:
{
MS_WARN_TAG(
rtcp,
"ignoring unsupported %s Feedback packet "
"[sender ssrc:%" PRIu32 ", media ssrc:%" PRIu32 "]",
RTCP::FeedbackRtpPacket::MessageType2String(feedback->GetMessageType()).c_str(),
feedback->GetMediaSsrc(),
feedback->GetMediaSsrc());
break;
}
}
break;
}
case RTCP::Type::SR:
{
auto* sr = dynamic_cast<RTCP::SenderReportPacket*>(packet);
auto it = sr->Begin();
// Even if Sender Report packet can only contain one report..
for (; it != sr->End(); ++it)
{
auto& report = (*it);
// Get the producer associated to the SSRC indicated in the report.
auto* producer = this->rtpListener.GetProducer(report->GetSsrc());
if (producer == nullptr)
{
MS_WARN_TAG(
rtcp,
"no Producer found for received Sender Report [ssrc:%" PRIu32 "]",
report->GetSsrc());
}
else
producer->ReceiveRtcpSenderReport(report);
}
break;
}
case RTCP::Type::SDES:
{
auto* sdes = dynamic_cast<RTCP::SdesPacket*>(packet);
auto it = sdes->Begin();
for (; it != sdes->End(); ++it)
{
auto& chunk = (*it);
// Get the producer associated to the SSRC indicated in the report.
auto* producer = this->rtpListener.GetProducer(chunk->GetSsrc());
if (producer == nullptr)
{
MS_WARN_TAG(
rtcp, "no Producer for received SDES chunk [ssrc:%" PRIu32 "]", chunk->GetSsrc());
}
}
break;
}
case RTCP::Type::BYE:
{
MS_DEBUG_TAG(rtcp, "ignoring received RTCP BYE");
break;
}
default: default:
{ {
// TODO: Uncomment when RTCP handling above is completed. MS_WARN_TAG(
// MS_WARN_TAG( rtcp,
// rtcp, "unhandled RTCP type received [type:%" PRIu8 "]",
// "unhandled RTCP type received [type:%" PRIu8 "]", static_cast<uint8_t>(packet->GetType()));
// static_cast<uint8_t>(packet->GetType()));
} }
} }
} }
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();
...@@ -1324,4 +1559,40 @@ namespace RTC ...@@ -1324,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 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论