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

Initial work in RtpStream

上级 5a157b6d
...@@ -4,30 +4,28 @@ ...@@ -4,30 +4,28 @@
#include "common.h" #include "common.h"
#include "RTC/RtpPacket.h" #include "RTC/RtpPacket.h"
#include <vector> #include <vector>
#include <list>
#define MS_RTP_PACKET_MAX_SIZE 65536
namespace RTC namespace RTC
{ {
class RtpBuffer class RtpBuffer
{ {
public: private:
struct Item struct StorageItem
{ {
uint8_t raw[MS_RTP_PACKET_MAX_SIZE]; uint8_t store[65536];
RTC::RtpPacket* packet = nullptr;
}; };
public: public:
RtpBuffer(size_t capability); RtpBuffer(size_t size);
virtual ~RtpBuffer(); ~RtpBuffer();
void Add(RTC::RtpPacket* packet); bool Add(RTC::RtpPacket* packet);
private: private:
std::vector<Item> items; std::vector<StorageItem> storage;
size_t pos = 0; typedef std::list<RTC::RtpPacket*> Packets;
bool full = false; Packets packets;
}; };
} }
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "common.h" #include "common.h"
#include "RTC/RtpDictionaries.h" #include "RTC/RtpDictionaries.h"
#include "RTC/RtpPacket.h" #include "RTC/RtpPacket.h"
#include "RTC/RtpBuffer.h" #include "RTC/RtpStream.h"
#include "RTC/RTCP/ReceiverReport.h" #include "RTC/RTCP/ReceiverReport.h"
#include "RTC/RTCP/Feedback.h" #include "RTC/RTCP/Feedback.h"
#include "Channel/Request.h" #include "Channel/Request.h"
...@@ -66,7 +66,7 @@ namespace RTC ...@@ -66,7 +66,7 @@ namespace RTC
RTC::Transport* transport = nullptr; RTC::Transport* transport = nullptr;
// Allocated by this. // Allocated by this.
RTC::RtpParameters* rtpParameters = nullptr; RTC::RtpParameters* rtpParameters = nullptr;
RTC::RtpBuffer* rtpBuffer = nullptr; RTC::RtpStream* rtpStream = nullptr;
// Others. // Others.
bool rtpRawEventEnabled = false; bool rtpRawEventEnabled = false;
bool rtpObjectEventEnabled = 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 @@ ...@@ -36,6 +36,7 @@
'src/RTC/RtpPacket.cpp', 'src/RTC/RtpPacket.cpp',
'src/RTC/RtpReceiver.cpp', 'src/RTC/RtpReceiver.cpp',
'src/RTC/RtpSender.cpp', 'src/RTC/RtpSender.cpp',
'src/RTC/RtpStream.cpp',
'src/RTC/SrtpSession.cpp', 'src/RTC/SrtpSession.cpp',
'src/RTC/StunMessage.cpp', 'src/RTC/StunMessage.cpp',
'src/RTC/TcpConnection.cpp', 'src/RTC/TcpConnection.cpp',
...@@ -113,6 +114,7 @@ ...@@ -113,6 +114,7 @@
'include/RTC/RtpPacket.h', 'include/RTC/RtpPacket.h',
'include/RTC/RtpReceiver.h', 'include/RTC/RtpReceiver.h',
'include/RTC/RtpSender.h', 'include/RTC/RtpSender.h',
'include/RTC/RtpStream.h',
'include/RTC/SrtpSession.h', 'include/RTC/SrtpSession.h',
'include/RTC/StunMessage.h', 'include/RTC/StunMessage.h',
'include/RTC/TcpConnection.h', 'include/RTC/TcpConnection.h',
......
// TODO: https://tools.ietf.org/html/rfc3550#appendix-A.1
#define MS_CLASS "RTC::RtpBuffer" #define MS_CLASS "RTC::RtpBuffer"
#include "RTC/RtpBuffer.h" #include "RTC/RtpBuffer.h"
...@@ -7,8 +9,8 @@ namespace RTC ...@@ -7,8 +9,8 @@ namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
RtpBuffer::RtpBuffer(size_t capability) : RtpBuffer::RtpBuffer(size_t size) :
items(capability) storage(size)
{ {
MS_TRACE(); MS_TRACE();
} }
...@@ -18,28 +20,103 @@ namespace RTC ...@@ -18,28 +20,103 @@ namespace RTC
MS_TRACE(); MS_TRACE();
// Delete cloned packets. // Delete cloned packets.
for (auto& item : this->items) for (auto& packet : this->packets)
{ {
if (item.packet) delete packet;
delete item.packet;
} }
} }
void RtpBuffer::Add(RTC::RtpPacket* packet) bool RtpBuffer::Add(RTC::RtpPacket* packet)
{ {
MS_TRACE(); 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). MS_WARN(" packets.size:%zu, storage.size:%zu", this->packets.size(), this->storage.size());
if (item->packet)
delete item->packet;
// Clone the given RTP packet and store it. // If the storage is not full use the next free area.
item->packet = packet->Clone(item->raw); 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(); return true;
if (!this->full && this->pos == 0)
this->full = true;
} }
} }
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
#include "MediaSoupError.h" #include "MediaSoupError.h"
#include "Logger.h" #include "Logger.h"
#define RTP_BUFFER_SIZE 100
namespace RTC namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
...@@ -20,8 +18,8 @@ namespace RTC ...@@ -20,8 +18,8 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Create the RtpBuffer instance. // Create the RtpStream instance.
this->rtpBuffer = new RTC::RtpBuffer(RTP_BUFFER_SIZE); this->rtpStream = new RTC::RtpStream(50);
} }
RtpReceiver::~RtpReceiver() RtpReceiver::~RtpReceiver()
...@@ -40,8 +38,8 @@ namespace RTC ...@@ -40,8 +38,8 @@ namespace RTC
if (this->rtpParameters) if (this->rtpParameters)
delete this->rtpParameters; delete this->rtpParameters;
if (this->rtpBuffer) if (this->rtpStream)
delete this->rtpBuffer; delete this->rtpStream;
// Notify. // Notify.
event_data[k_class] = "RtpReceiver"; event_data[k_class] = "RtpReceiver";
...@@ -217,6 +215,20 @@ namespace RTC ...@@ -217,6 +215,20 @@ namespace RTC
// TODO: Check if stopped, etc (not yet done) // 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. // Notify the listener.
this->listener->onRtpPacket(this, packet); this->listener->onRtpPacket(this, packet);
...@@ -248,10 +260,6 @@ namespace RTC ...@@ -248,10 +260,6 @@ namespace RTC
this->notifier->EmitWithBinary(this->rtpReceiverId, "rtpobject", event_data, packet->GetPayload(), packet->GetPayloadLength()); 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() 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 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论