提交 11ff2b90 authored 作者: José Luis Millán's avatar José Luis Millán 提交者: Iñaki Baz Castillo

RTCP: Packet and CompoundPacket: (#48)

- Rename GetRaw() to GetData().
- Make Serialize() always use an externally provided buffer.
- Use a static buffer for serializing RTCP packets.
上级 7d196bd7
...@@ -42,6 +42,9 @@ namespace RTC ...@@ -42,6 +42,9 @@ namespace RTC
virtual void onPeerRtcpCompleted(RTC::Peer* peer) = 0; virtual void onPeerRtcpCompleted(RTC::Peer* peer) = 0;
}; };
private:
static uint8_t rtcpBuffer[];
public: public:
Peer(Listener* listener, Channel::Notifier* notifier, uint32_t peerId, std::string& peerName); Peer(Listener* listener, Channel::Notifier* notifier, uint32_t peerId, std::string& peerName);
virtual ~Peer(); virtual ~Peer();
......
...@@ -12,9 +12,7 @@ namespace RTC { namespace RTCP ...@@ -12,9 +12,7 @@ namespace RTC { namespace RTCP
class CompoundPacket class CompoundPacket
{ {
public: public:
~CompoundPacket(); const uint8_t* GetData();
uint8_t* GetRaw();
size_t GetSize(); size_t GetSize();
size_t GetSenderReportCount(); size_t GetSenderReportCount();
size_t GetReceiverReportCount(); size_t GetReceiverReportCount();
...@@ -22,10 +20,10 @@ namespace RTC { namespace RTCP ...@@ -22,10 +20,10 @@ namespace RTC { namespace RTCP
void AddSenderReport(SenderReport* report); void AddSenderReport(SenderReport* report);
void AddReceiverReport(ReceiverReport* report); void AddReceiverReport(ReceiverReport* report);
void AddSdesChunk(SdesChunk* chunk); void AddSdesChunk(SdesChunk* chunk);
void Serialize(); void Serialize(uint8_t* buffer);
private: private:
uint8_t* raw = nullptr; uint8_t* header = nullptr;
size_t size = 0; size_t size = 0;
SenderReportPacket senderReportPacket; SenderReportPacket senderReportPacket;
ReceiverReportPacket receiverReportPacket; ReceiverReportPacket receiverReportPacket;
...@@ -35,9 +33,9 @@ namespace RTC { namespace RTCP ...@@ -35,9 +33,9 @@ namespace RTC { namespace RTCP
/* Inline methods. */ /* Inline methods. */
inline inline
uint8_t* CompoundPacket::GetRaw() const uint8_t* CompoundPacket::GetData()
{ {
return this->raw; return this->header;
} }
inline inline
......
...@@ -33,18 +33,18 @@ namespace RTC { namespace RTCP ...@@ -33,18 +33,18 @@ namespace RTC { namespace RTCP
uint32_t GetMediaSsrc(); uint32_t GetMediaSsrc();
void SetMediaSsrc(uint32_t ssrc); void SetMediaSsrc(uint32_t ssrc);
protected:
explicit FeedbackPacket(CommonHeader* commonHeader);
FeedbackPacket(typename T::MessageType type, uint32_t sender_ssrc, uint32_t media_ssrc);
virtual ~FeedbackPacket();
/* Pure virtual methods inherited from Packet. */ /* Pure virtual methods inherited from Packet. */
protected: public:
virtual void Dump() override; virtual void Dump() override;
virtual size_t Serialize(uint8_t* buffer) override; virtual size_t Serialize(uint8_t* buffer) override;
virtual size_t GetCount() override; virtual size_t GetCount() override;
virtual size_t GetSize() override; virtual size_t GetSize() override;
protected:
explicit FeedbackPacket(CommonHeader* commonHeader);
FeedbackPacket(typename T::MessageType type, uint32_t sender_ssrc, uint32_t media_ssrc);
virtual ~FeedbackPacket();
private: private:
Header* header = nullptr; Header* header = nullptr;
uint8_t* raw = nullptr; uint8_t* raw = nullptr;
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <map> #include <map>
#include <string> #include <string>
#define MS_RTCP_BUFFER_SIZE 65536
namespace RTC { namespace RTCP namespace RTC { namespace RTCP
{ {
enum class Type : uint8_t enum class Type : uint8_t
...@@ -65,8 +67,7 @@ namespace RTC { namespace RTCP ...@@ -65,8 +67,7 @@ namespace RTC { namespace RTCP
void SetNext(Packet* packet); void SetNext(Packet* packet);
Packet* GetNext(); Packet* GetNext();
Type GetType(); Type GetType();
size_t Serialize(); const uint8_t* GetData();
uint8_t* GetRaw();
public: public:
virtual void Dump() = 0; virtual void Dump() = 0;
...@@ -77,7 +78,7 @@ namespace RTC { namespace RTCP ...@@ -77,7 +78,7 @@ namespace RTC { namespace RTCP
private: private:
Type type; Type type;
Packet* next = nullptr; Packet* next = nullptr;
uint8_t* raw = nullptr; CommonHeader* header = nullptr;
}; };
/* Inline static methods. */ /* Inline static methods. */
...@@ -102,6 +103,15 @@ namespace RTC { namespace RTCP ...@@ -102,6 +103,15 @@ namespace RTC { namespace RTCP
/* Inline instance methods. */ /* Inline instance methods. */
inline
Packet::Packet(Type type)
:type(type)
{}
inline
Packet::~Packet()
{}
inline inline
Packet* Packet::GetNext() Packet* Packet::GetNext()
{ {
...@@ -114,19 +124,6 @@ namespace RTC { namespace RTCP ...@@ -114,19 +124,6 @@ namespace RTC { namespace RTCP
this->next = packet; this->next = packet;
} }
inline
size_t Packet::Serialize()
{
if (this->raw)
delete this->raw;
size_t size = this->GetSize();
this->raw = new uint8_t[size];
return this->Serialize(this->raw);
}
inline inline
Type Packet::GetType() Type Packet::GetType()
{ {
...@@ -140,9 +137,9 @@ namespace RTC { namespace RTCP ...@@ -140,9 +137,9 @@ namespace RTC { namespace RTCP
} }
inline inline
uint8_t* Packet::GetRaw() const uint8_t* Packet::GetData()
{ {
return this->raw; return (uint8_t*)this->header;
} }
}} }}
......
...@@ -34,6 +34,9 @@ namespace RTC ...@@ -34,6 +34,9 @@ namespace RTC
virtual void onRtpReceiverClosed(RtpReceiver* rtpReceiver) = 0; virtual void onRtpReceiverClosed(RtpReceiver* rtpReceiver) = 0;
}; };
private:
static uint8_t rtcpBuffer[];
public: public:
RtpReceiver(Listener* listener, Channel::Notifier* notifier, uint32_t rtpReceiverId, RTC::Media::Kind kind); RtpReceiver(Listener* listener, Channel::Notifier* notifier, uint32_t rtpReceiverId, RTC::Media::Kind kind);
virtual ~RtpReceiver(); virtual ~RtpReceiver();
......
...@@ -51,7 +51,7 @@ namespace RTC ...@@ -51,7 +51,7 @@ namespace RTC
void RemoveRtpReceiver(RTC::RtpReceiver* rtpReceiver); void RemoveRtpReceiver(RTC::RtpReceiver* rtpReceiver);
void SendRtpPacket(RTC::RtpPacket* packet); void SendRtpPacket(RTC::RtpPacket* packet);
void SendRtcpPacket(RTC::RTCP::Packet* packet); void SendRtcpPacket(RTC::RTCP::Packet* packet);
void SendRtcpPacket(RTC::RTCP::CompoundPacket* packet); void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet);
RTC::RtpReceiver* GetRtpReceiver(uint32_t ssrc); RTC::RtpReceiver* GetRtpReceiver(uint32_t ssrc);
private: private:
......
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
namespace RTC namespace RTC
{ {
/* Class variables. */
uint8_t Peer::rtcpBuffer[MS_RTCP_BUFFER_SIZE];
/* Instance methods. */ /* Instance methods. */
Peer::Peer(Listener* listener, Channel::Notifier* notifier, uint32_t peerId, std::string& peerName) : Peer::Peer(Listener* listener, Channel::Notifier* notifier, uint32_t peerId, std::string& peerName) :
...@@ -550,7 +554,17 @@ namespace RTC ...@@ -550,7 +554,17 @@ namespace RTC
} }
if (packet.GetSenderReportCount() || packet.GetReceiverReportCount()) if (packet.GetSenderReportCount() || packet.GetReceiverReportCount())
transport->SendRtcpPacket(&packet); {
// 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);
}
} }
} }
......
...@@ -8,16 +8,12 @@ namespace RTC { namespace RTCP ...@@ -8,16 +8,12 @@ namespace RTC { namespace RTCP
{ {
/* Instance methods. */ /* Instance methods. */
CompoundPacket::~CompoundPacket() void CompoundPacket::Serialize(uint8_t* data)
{
if (this->raw)
delete this->raw;
}
void CompoundPacket::Serialize()
{ {
MS_TRACE(); MS_TRACE();
this->header = data;
// Calculate the total required size for the entire message. // Calculate the total required size for the entire message.
if (this->senderReportPacket.GetCount()) if (this->senderReportPacket.GetCount())
{ {
...@@ -36,21 +32,18 @@ namespace RTC { namespace RTCP ...@@ -36,21 +32,18 @@ namespace RTC { namespace RTCP
if (this->sdesPacket.GetCount()) if (this->sdesPacket.GetCount())
this->size += this->sdesPacket.GetSize(); this->size += this->sdesPacket.GetSize();
// Allocate it.
this->raw = new uint8_t[this->size];
// Fill it. // Fill it.
size_t offset = 0; size_t offset = 0;
if (this->senderReportPacket.GetCount()) if (this->senderReportPacket.GetCount())
{ {
this->senderReportPacket.Serialize(this->raw); this->senderReportPacket.Serialize(this->header);
offset = this->senderReportPacket.GetSize(); offset = this->senderReportPacket.GetSize();
if (this->receiverReportPacket.GetCount()) if (this->receiverReportPacket.GetCount())
{ {
// Fix header length. // Fix header length.
Packet::CommonHeader* header = (Packet::CommonHeader*)this->raw; Packet::CommonHeader* header = (Packet::CommonHeader*)this->header;
header->length += (sizeof(ReceiverReport::Header) * this->receiverReportPacket.GetCount()) / 4; header->length += (sizeof(ReceiverReport::Header) * this->receiverReportPacket.GetCount()) / 4;
ReceiverReportPacket::Iterator it = this->receiverReportPacket.Begin(); ReceiverReportPacket::Iterator it = this->receiverReportPacket.Begin();
...@@ -58,20 +51,20 @@ namespace RTC { namespace RTCP ...@@ -58,20 +51,20 @@ namespace RTC { namespace RTCP
{ {
ReceiverReport* report = (*it); ReceiverReport* report = (*it);
report->Serialize(this->raw + offset); report->Serialize(this->header + offset);
offset += sizeof(ReceiverReport::Header); offset += sizeof(ReceiverReport::Header);
} }
} }
} }
else else
{ {
this->receiverReportPacket.Serialize(this->raw); this->receiverReportPacket.Serialize(this->header);
offset = this->receiverReportPacket.GetSize(); offset = this->receiverReportPacket.GetSize();
} }
if (this->sdesPacket.GetCount()) if (this->sdesPacket.GetCount())
{ {
this->sdesPacket.Serialize(this->raw + offset); this->sdesPacket.Serialize(this->header + offset);
offset += this->sdesPacket.GetSize(); offset += this->sdesPacket.GetSize();
} }
} }
......
...@@ -151,29 +151,20 @@ namespace RTC { namespace RTCP ...@@ -151,29 +151,20 @@ namespace RTC { namespace RTCP
/* Instance methods. */ /* Instance methods. */
Packet::Packet(Type type)
:type(type)
{}
Packet::~Packet()
{
if (this->raw)
delete raw;
}
size_t Packet::Serialize(uint8_t* buffer) size_t Packet::Serialize(uint8_t* buffer)
{ {
MS_TRACE(); MS_TRACE();
this->header = (CommonHeader*)buffer;
size_t length = (this->GetSize() / 4) - 1; size_t length = (this->GetSize() / 4) - 1;
CommonHeader* header = (Packet::CommonHeader*)buffer;
// Fill the common header. // Fill the common header.
header->version = 2; this->header->version = 2;
header->padding = 0; this->header->padding = 0;
header->count = (uint8_t)this->GetCount(); this->header->count = (uint8_t)this->GetCount();
header->packet_type = (uint8_t) this->type; this->header->packet_type = (uint8_t)this->type;
header->length = htons(length); this->header->length = htons(length);
return sizeof(CommonHeader); return sizeof(CommonHeader);
} }
......
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
namespace RTC namespace RTC
{ {
/* Class variables. */
uint8_t RtpReceiver::rtcpBuffer[MS_RTCP_BUFFER_SIZE];
/* Instance methods. */ /* Instance methods. */
RtpReceiver::RtpReceiver(Listener* listener, Channel::Notifier* notifier, uint32_t rtpReceiverId, RTC::Media::Kind kind) : RtpReceiver::RtpReceiver(Listener* listener, Channel::Notifier* notifier, uint32_t rtpReceiverId, RTC::Media::Kind kind) :
...@@ -302,7 +306,17 @@ namespace RTC ...@@ -302,7 +306,17 @@ namespace RTC
MS_TRACE(); MS_TRACE();
if (this->transport) if (this->transport)
{
// 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(RtpReceiver::rtcpBuffer);
this->transport->SendRtcpPacket(packet); this->transport->SendRtcpPacket(packet);
}
}; };
void RtpReceiver::ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet) void RtpReceiver::ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet)
...@@ -310,6 +324,16 @@ namespace RTC ...@@ -310,6 +324,16 @@ namespace RTC
MS_TRACE(); MS_TRACE();
if (this->transport) if (this->transport)
{
// 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(RtpReceiver::rtcpBuffer);
this->transport->SendRtcpPacket(packet); this->transport->SendRtcpPacket(packet);
}
}; };
} }
...@@ -530,8 +530,8 @@ namespace RTC ...@@ -530,8 +530,8 @@ namespace RTC
return; return;
} }
size_t len = packet->Serialize(); const uint8_t* data = packet->GetData();
const uint8_t* data = packet->GetRaw(); size_t len = packet->GetSize();
if (!this->srtpSendSession->EncryptRtcp(&data, &len)) if (!this->srtpSendSession->EncryptRtcp(&data, &len))
return; return;
...@@ -539,7 +539,7 @@ namespace RTC ...@@ -539,7 +539,7 @@ namespace RTC
this->selectedTuple->Send(data, len); this->selectedTuple->Send(data, len);
} }
void Transport::SendRtcpPacket(RTC::RTCP::CompoundPacket* packet) void Transport::SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
...@@ -555,8 +555,7 @@ namespace RTC ...@@ -555,8 +555,7 @@ namespace RTC
return; return;
} }
packet->Serialize(); const uint8_t* data = packet->GetData();
const uint8_t* data = packet->GetRaw();
size_t len = packet->GetSize(); size_t len = packet->GetSize();
if (!this->srtpSendSession->EncryptRtcp(&data, &len)) if (!this->srtpSendSession->EncryptRtcp(&data, &len))
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论