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