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

Rtcp: BYE

上级 2db944dc
#ifndef MS_RTC_RTCP_BYE_H
#define MS_RTC_RTCP_BYE_H
#include "common.h"
#include "RTC/RTCP/Packet.h"
#include <vector>
#include <string>
namespace RTC { namespace RTCP
{
class ByePacket
: public Packet
{
public:
typedef std::vector<uint32_t>::iterator Iterator;
public:
static ByePacket* Parse(const uint8_t* data, size_t len);
public:
ByePacket();
// Virtual methods inherited from Packet
void Dump() override;
size_t Serialize(uint8_t* data) override;
size_t GetCount() override;
size_t GetSize() override;
public:
void AddSsrc(uint32_t ssrc);
void SetReason(const std::string& reason);
const std::string& GetReason();
Iterator Begin();
Iterator End();
private:
std::vector<uint32_t> ssrcs;
std::string reason;
};
/* BYE Packet inline instance methods */
inline
ByePacket::ByePacket()
: Packet(Type::BYE)
{
}
inline
size_t ByePacket::GetCount()
{
return this->ssrcs.size();
}
inline
size_t ByePacket::GetSize()
{
size_t size = sizeof(Packet::CommonHeader);
size += ssrcs.size() * sizeof(uint32_t);
if (!this->reason.empty())
{
size += sizeof(uint8_t); // length field
size += this->reason.length();
}
// http://stackoverflow.com/questions/11642210/computing-padding-required-for-n-byte-alignment
// Consider pading to 32 bits (4 bytes) boundary
return (size + 3) & ~3;
}
inline
void ByePacket::AddSsrc(uint32_t ssrc)
{
this->ssrcs.push_back(ssrc);
}
inline
void ByePacket::SetReason(const std::string& reason)
{
this->reason = reason;
}
inline
const std::string& ByePacket::GetReason()
{
return this->reason;
}
inline
ByePacket::Iterator ByePacket::Begin()
{
return this->ssrcs.begin();
}
inline
ByePacket::Iterator ByePacket::End()
{
return this->ssrcs.end();
}
} } // RTP::RTCP
#endif
...@@ -61,6 +61,7 @@ ...@@ -61,6 +61,7 @@
'src/RTC/RTCP/SenderReport.cpp', 'src/RTC/RTCP/SenderReport.cpp',
'src/RTC/RTCP/ReceiverReport.cpp', 'src/RTC/RTCP/ReceiverReport.cpp',
'src/RTC/RTCP/Sdes.cpp', 'src/RTC/RTCP/Sdes.cpp',
'src/RTC/RTCP/Bye.cpp',
'src/RTC/RTCP/Feedback.cpp', 'src/RTC/RTCP/Feedback.cpp',
'src/RTC/RTCP/FeedbackPs.cpp', 'src/RTC/RTCP/FeedbackPs.cpp',
'src/RTC/RTCP/FeedbackRtp.cpp', 'src/RTC/RTCP/FeedbackRtp.cpp',
...@@ -121,6 +122,7 @@ ...@@ -121,6 +122,7 @@
'include/RTC/RTCP/SenderReport.h', 'include/RTC/RTCP/SenderReport.h',
'include/RTC/RTCP/ReceiverReport.h', 'include/RTC/RTCP/ReceiverReport.h',
'include/RTC/RTCP/Sdes.h', 'include/RTC/RTCP/Sdes.h',
'include/RTC/RTCP/Bye.h',
'include/RTC/RTCP/Feedback.h', 'include/RTC/RTCP/Feedback.h',
'include/RTC/RTCP/FeedbackItem.h', 'include/RTC/RTCP/FeedbackItem.h',
'include/RTC/RTCP/FeedbackPs.h', 'include/RTC/RTCP/FeedbackPs.h',
......
...@@ -807,6 +807,9 @@ namespace RTC ...@@ -807,6 +807,9 @@ namespace RTC
break; break;
case RTCP::Type::BYE:
break;
default: default:
MS_WARN("Unhandled RTCP type received to 'RTC::Peer': %u", (uint8_t)packet->GetType()); MS_WARN("Unhandled RTCP type received to 'RTC::Peer': %u", (uint8_t)packet->GetType());
break; break;
......
#define MS_CLASS "RTC::RTCP::Bye"
#include "RTC/RTCP/Bye.h"
#include "Utils.h"
#include "Logger.h"
#include <cstring> // std::memcmp(), std::memcpy()
namespace RTC { namespace RTCP
{
/* BYE Packet Class methods. */
ByePacket* ByePacket::Parse(const uint8_t* data, size_t len)
{
MS_TRACE_STD();
// Get the header.
Packet::CommonHeader* header = (Packet::CommonHeader*)data;
std::auto_ptr<ByePacket> packet(new ByePacket());
size_t offset = sizeof(Packet::CommonHeader);
uint8_t count = header->count;
while ((count--) && (len - offset > 0))
{
if (sizeof(uint32_t) > len-offset)
{
MS_WARN("not enough space for SSRC in RTCP Bye message");
return nullptr;
}
packet->AddSsrc(Utils::Byte::Get4Bytes(data+offset, 0));
offset += sizeof(uint32_t);
}
if (count == 0)
{
size_t length = size_t(Utils::Byte::Get1Byte(data+offset, 0));
if (sizeof(uint8_t) + length <= len-offset)
{
packet->SetReason(std::string((char*)data+offset, length));
}
}
return packet.release();
}
/* BYE Packet Instance methods. */
size_t ByePacket::Serialize(uint8_t* data)
{
MS_TRACE_STD();
size_t offset = Packet::Serialize(data);
for(auto ssrc : this->ssrcs) {
std::memcpy(data, &ssrc, sizeof(uint32_t));
offset += sizeof(uint32_t);
}
if (!this->reason.empty())
{
Utils::Byte::Set1Byte(data, offset, this->reason.length());
offset += sizeof(uint8_t); // length field
offset += this->reason.length();
}
// 32 bits padding
size_t padding = (-offset) & 3;
for (size_t i = 0; i < padding; i++)
{
data[offset+i] = 0;
}
return offset+padding;
}
void ByePacket::Dump()
{
MS_TRACE_STD();
if (!Logger::HasDebugLevel())
return;
MS_WARN("<Bye Packet>");
for (auto ssrc : this->ssrcs) {
MS_WARN("\tssrc: %u", ssrc);
}
if (!this->reason.empty())
{
MS_WARN("\treason: %s", this->reason.c_str());
}
MS_WARN("</Bye Packet>");
}
} } // RTP::RTCP
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "RTC/RTCP/ReceiverReport.h" #include "RTC/RTCP/ReceiverReport.h"
#include "RTC/RTCP/SenderReport.h" #include "RTC/RTCP/SenderReport.h"
#include "RTC/RTCP/Sdes.h" #include "RTC/RTCP/Sdes.h"
#include "RTC/RTCP/Bye.h"
#include "RTC/RTCP/Feedback.h" #include "RTC/RTCP/Feedback.h"
#include "Logger.h" #include "Logger.h"
...@@ -82,7 +83,7 @@ namespace RTCP ...@@ -82,7 +83,7 @@ namespace RTCP
break; break;
case Type::BYE: case Type::BYE:
current = nullptr; current = ByePacket::Parse(data, len);
break; break;
case Type::APP: case Type::APP:
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论