提交 32bdc24e authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Initial work in RtpStream

上级 5a157b6d
......@@ -4,30 +4,28 @@
#include "common.h"
#include "RTC/RtpPacket.h"
#include <vector>
#define MS_RTP_PACKET_MAX_SIZE 65536
#include <list>
namespace RTC
{
class RtpBuffer
{
public:
struct Item
private:
struct StorageItem
{
uint8_t raw[MS_RTP_PACKET_MAX_SIZE];
RTC::RtpPacket* packet = nullptr;
uint8_t store[65536];
};
public:
RtpBuffer(size_t capability);
virtual ~RtpBuffer();
RtpBuffer(size_t size);
~RtpBuffer();
void Add(RTC::RtpPacket* packet);
bool Add(RTC::RtpPacket* packet);
private:
std::vector<Item> items;
size_t pos = 0;
bool full = false;
std::vector<StorageItem> storage;
typedef std::list<RTC::RtpPacket*> Packets;
Packets packets;
};
}
......
......@@ -4,7 +4,7 @@
#include "common.h"
#include "RTC/RtpDictionaries.h"
#include "RTC/RtpPacket.h"
#include "RTC/RtpBuffer.h"
#include "RTC/RtpStream.h"
#include "RTC/RTCP/ReceiverReport.h"
#include "RTC/RTCP/Feedback.h"
#include "Channel/Request.h"
......@@ -66,7 +66,7 @@ namespace RTC
RTC::Transport* transport = nullptr;
// Allocated by this.
RTC::RtpParameters* rtpParameters = nullptr;
RTC::RtpBuffer* rtpBuffer = nullptr;
RTC::RtpStream* rtpStream = nullptr;
// Others.
bool rtpRawEventEnabled = false;
bool rtpObjectEventEnabled = false;
......
#ifndef MS_RTC_RTP_STREAM_H
#define MS_RTC_RTP_STREAM_H
#include "common.h"
#include "RTC/RtpPacket.h"
#include <vector>
#include <list>
namespace RTC
{
class RtpStream
{
private:
struct StorageItem
{
uint8_t store[65536];
};
public:
RtpStream(size_t bufferSize);
~RtpStream();
bool AddPacket(RTC::RtpPacket* packet);
private:
std::vector<StorageItem> storage;
typedef std::list<RTC::RtpPacket*> Packets;
Packets packets;
};
}
#endif
......@@ -36,6 +36,7 @@
'src/RTC/RtpPacket.cpp',
'src/RTC/RtpReceiver.cpp',
'src/RTC/RtpSender.cpp',
'src/RTC/RtpStream.cpp',
'src/RTC/SrtpSession.cpp',
'src/RTC/StunMessage.cpp',
'src/RTC/TcpConnection.cpp',
......@@ -113,6 +114,7 @@
'include/RTC/RtpPacket.h',
'include/RTC/RtpReceiver.h',
'include/RTC/RtpSender.h',
'include/RTC/RtpStream.h',
'include/RTC/SrtpSession.h',
'include/RTC/StunMessage.h',
'include/RTC/TcpConnection.h',
......
// TODO: https://tools.ietf.org/html/rfc3550#appendix-A.1
#define MS_CLASS "RTC::RtpBuffer"
#include "RTC/RtpBuffer.h"
......@@ -7,8 +9,8 @@ namespace RTC
{
/* Instance methods. */
RtpBuffer::RtpBuffer(size_t capability) :
items(capability)
RtpBuffer::RtpBuffer(size_t size) :
storage(size)
{
MS_TRACE();
}
......@@ -18,28 +20,103 @@ namespace RTC
MS_TRACE();
// Delete cloned packets.
for (auto& item : this->items)
for (auto& packet : this->packets)
{
if (item.packet)
delete item.packet;
delete packet;
}
}
void RtpBuffer::Add(RTC::RtpPacket* packet)
bool RtpBuffer::Add(RTC::RtpPacket* packet)
{
MS_TRACE();
Item* item = std::addressof(this->items[this->pos]);
MS_WARN("BEGIN | seq_number: %" PRIu16, packet->GetSequenceNumber());
// If empty do it easy.
if (this->packets.size() == 0)
{
MS_WARN(" empty packet list");
auto store = this->storage[0].store;
auto cloned_packet = packet->Clone(store);
this->packets.push_back(cloned_packet);
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber());
return true;
}
// Otherwise, do the stuff.
Packets::iterator new_it;
uint8_t* store = nullptr;
auto it_r = this->packets.rbegin();
for (; it_r != this->packets.rend(); it_r++)
{
auto current_packet = *it_r;
if (packet->GetSequenceNumber() > current_packet->GetSequenceNumber())
{
// Get a forward iterator pointing to the same element.
auto it = it_r.base();
new_it = this->packets.insert(it, nullptr);
// Exit the loop.
break;
}
}
// If the packet was older than anything in the storate, just ignore it.
if (it_r == this->packets.rend())
{
// TODO: Wrong, this will drop all the received packets once sequence_number
// reaches 65535
MS_WARN(" packet is older than anything in the storage, ignoring it");
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber());
return false;
}
// Delete previous RTP packet (if any).
if (item->packet)
delete item->packet;
MS_WARN(" packets.size:%zu, storage.size:%zu", this->packets.size(), this->storage.size());
// Clone the given RTP packet and store it.
item->packet = packet->Clone(item->raw);
// If the storage is not full use the next free area.
if (this->packets.size() - 1 < this->storage.size())
{
MS_WARN(" storage not full");
// Store points to the next free mem area.
store = this->storage[this->packets.size() - 1].store;
}
// Otherwise remove the first packet list entry and replace its storage area.
else
{
auto first_packet = *(this->packets.begin());
MS_WARN(" storage full, deleting first list packet with seq_number: %" PRIu16, first_packet->GetSequenceNumber());
// Store points to the store used by the first packet.
store = (uint8_t*)first_packet->GetRaw();
// Free the first packet.
delete first_packet;
// Remove the first element in the list.
this->packets.pop_front();
}
// Update the new list entry so it points to the cloned packed.
*new_it = packet->Clone(store);
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber());
MS_WARN("<DUMP>");
for (auto& packet : this->packets)
{
MS_WARN(" - seq_number: %" PRIu16, packet->GetSequenceNumber());
}
MS_WARN("</DUMP>\n");
this->pos = (this->pos + 1) % this->items.size();
if (!this->full && this->pos == 0)
this->full = true;
return true;
}
}
......@@ -6,8 +6,6 @@
#include "MediaSoupError.h"
#include "Logger.h"
#define RTP_BUFFER_SIZE 100
namespace RTC
{
/* Instance methods. */
......@@ -20,8 +18,8 @@ namespace RTC
{
MS_TRACE();
// Create the RtpBuffer instance.
this->rtpBuffer = new RTC::RtpBuffer(RTP_BUFFER_SIZE);
// Create the RtpStream instance.
this->rtpStream = new RTC::RtpStream(50);
}
RtpReceiver::~RtpReceiver()
......@@ -40,8 +38,8 @@ namespace RTC
if (this->rtpParameters)
delete this->rtpParameters;
if (this->rtpBuffer)
delete this->rtpBuffer;
if (this->rtpStream)
delete this->rtpStream;
// Notify.
event_data[k_class] = "RtpReceiver";
......@@ -217,6 +215,20 @@ namespace RTC
// TODO: Check if stopped, etc (not yet done)
// Store in the buffer
// TODO: Must check what kind of packet we are storing, right?
// TODO: RtpStream.AddPacket() should return true if the packet is valid and
// false if it must be ignored.
// TODO: testing for just audio (remove)
if (this->kind == RTC::Media::Kind::AUDIO)
{
// TODO: Enable when properly implemented.
// if (!this->rtpStream->AddPacket(packet))
// return;
this->rtpStream->AddPacket(packet);
}
// Notify the listener.
this->listener->onRtpPacket(this, packet);
......@@ -248,10 +260,6 @@ namespace RTC
this->notifier->EmitWithBinary(this->rtpReceiverId, "rtpobject", event_data, packet->GetPayload(), packet->GetPayloadLength());
}
// Store in the buffer
// TODO: Must check what kind of packet we are storing, right?
this->rtpBuffer->Add(packet);
}
void RtpReceiver::FillRtpParameters()
......
// TODO: https://tools.ietf.org/html/rfc3550#appendix-A.1
#define MS_CLASS "RTC::RtpStream"
#include "RTC/RtpStream.h"
#include "Logger.h"
namespace RTC
{
/* Instance methods. */
RtpStream::RtpStream(size_t bufferSize) :
storage(bufferSize)
{
MS_TRACE();
}
RtpStream::~RtpStream()
{
MS_TRACE();
// Delete cloned packets.
for (auto& packet : this->packets)
{
delete packet;
}
}
bool RtpStream::AddPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
MS_WARN("BEGIN | seq_number: %" PRIu16, packet->GetSequenceNumber());
// If empty do it easy.
if (this->packets.size() == 0)
{
MS_WARN(" empty packet list");
auto store = this->storage[0].store;
auto cloned_packet = packet->Clone(store);
this->packets.push_back(cloned_packet);
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber());
return true;
}
// Otherwise, do the stuff.
Packets::iterator new_it;
uint8_t* store = nullptr;
auto it_r = this->packets.rbegin();
for (; it_r != this->packets.rend(); it_r++)
{
auto current_packet = *it_r;
if (packet->GetSequenceNumber() > current_packet->GetSequenceNumber())
{
// Get a forward iterator pointing to the same element.
auto it = it_r.base();
new_it = this->packets.insert(it, nullptr);
// Exit the loop.
break;
}
}
// If the packet was older than anything in the storate, just ignore it.
if (it_r == this->packets.rend())
{
// TODO: Wrong, this will drop all the received packets once sequence_number
// reaches 65535
MS_WARN(" packet is older than anything in the storage, ignoring it");
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber());
return false;
}
MS_WARN(" packets.size:%zu, storage.size:%zu", this->packets.size(), this->storage.size());
// If the storage is not full use the next free area.
if (this->packets.size() - 1 < this->storage.size())
{
MS_WARN(" storage not full");
// Store points to the next free mem area.
store = this->storage[this->packets.size() - 1].store;
}
// Otherwise remove the first packet list entry and replace its storage area.
else
{
auto first_packet = *(this->packets.begin());
MS_WARN(" storage full, deleting first list packet with seq_number: %" PRIu16, first_packet->GetSequenceNumber());
// Store points to the store used by the first packet.
store = (uint8_t*)first_packet->GetRaw();
// Free the first packet.
delete first_packet;
// Remove the first element in the list.
this->packets.pop_front();
}
// Update the new list entry so it points to the cloned packed.
*new_it = packet->Clone(store);
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber());
MS_WARN("<DUMP>");
for (auto& packet : this->packets)
{
MS_WARN(" - seq_number: %" PRIu16, packet->GetSequenceNumber());
}
MS_WARN("</DUMP>\n");
return true;
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论