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

Working on RtpStream (habemus RTP buffer)

上级 ae310629
...@@ -70,8 +70,7 @@ namespace RTC ...@@ -70,8 +70,7 @@ namespace RTC
// Others. // Others.
bool rtpRawEventEnabled = false; bool rtpRawEventEnabled = false;
bool rtpObjectEventEnabled = false; bool rtpObjectEventEnabled = false;
// Receiver Report holding the RTP stats.
// Receiver Report holding the RTP stats
std::auto_ptr<RTC::RTCP::ReceiverReport> receiverReport; std::auto_ptr<RTC::RTCP::ReceiverReport> receiverReport;
}; };
......
...@@ -16,17 +16,25 @@ namespace RTC ...@@ -16,17 +16,25 @@ namespace RTC
uint8_t store[65536]; uint8_t store[65536];
}; };
private:
struct BufferItem
{
uint32_t seq32 = 0; // RTP seq in 32 bytes plus 16 bits cycles.
RTC::RtpPacket* packet = nullptr;
};
public: public:
RtpStream(size_t bufferSize); RtpStream(size_t bufferSize);
~RtpStream(); ~RtpStream();
bool ReceivePacket(RTC::RtpPacket* packet); bool ReceivePacket(RTC::RtpPacket* packet);
// TODO: REMOVE
bool AddPacket(RTC::RtpPacket* packet);
private: private:
void InitSeq(uint16_t seq); void InitSeq(uint16_t seq);
bool UpdateSeq(uint16_t seq); bool UpdateSeq(uint16_t seq);
void CleanBuffer();
void StorePacket(RTC::RtpPacket* packet);
void Dump();
private: private:
bool started = false; // Whether at least a RTP packet has been received. bool started = false; // Whether at least a RTP packet has been received.
...@@ -43,8 +51,8 @@ namespace RTC ...@@ -43,8 +51,8 @@ namespace RTC
uint32_t jitter = 0; // Estimated jitter. uint32_t jitter = 0; // Estimated jitter.
// Others. // Others.
std::vector<StorageItem> storage; std::vector<StorageItem> storage;
typedef std::list<RTC::RtpPacket*> Packets; typedef std::list<BufferItem> Buffer;
Packets packets; Buffer buffer;
}; };
} }
......
...@@ -18,8 +18,9 @@ namespace RTC ...@@ -18,8 +18,9 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Create the RtpStream instance. // Create an RtpStream instance if video.
this->rtpStream = new RTC::RtpStream(50); if (kind == RTC::Media::Kind::VIDEO)
this->rtpStream = new RTC::RtpStream(50); // TODO: Set a proper size.
} }
RtpReceiver::~RtpReceiver() RtpReceiver::~RtpReceiver()
...@@ -219,15 +220,11 @@ namespace RTC ...@@ -219,15 +220,11 @@ namespace RTC
// TODO: Must check what kind of packet we are storing, right? // TODO: Must check what kind of packet we are storing, right?
// TODO: RtpStream.ReceivePacket() should return true if the packet is valid and // TODO: RtpStream.ReceivePacket() should return true if the packet is valid and
// false if it must be ignored. // false if it must be ignored.
// TODO: testing for just video (remove)
if (this->kind == RTC::Media::Kind::VIDEO) if (this->kind == RTC::Media::Kind::VIDEO)
{ {
// TODO: Enable when properly implemented. // TODO: Enable when properly implemented.
if (!this->rtpStream->ReceivePacket(packet)) if (!this->rtpStream->ReceivePacket(packet))
return; return;
// TODO: REMOVE
// this->rtpStream->ReceivePacket(packet);
} }
// Notify the listener. // Notify the listener.
......
...@@ -24,11 +24,8 @@ namespace RTC ...@@ -24,11 +24,8 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Delete cloned packets. // Clear buffer.
for (auto& packet : this->packets) CleanBuffer();
{
delete packet;
}
} }
bool RtpStream::ReceivePacket(RTC::RtpPacket* packet) bool RtpStream::ReceivePacket(RTC::RtpPacket* packet)
...@@ -56,6 +53,12 @@ namespace RTC ...@@ -56,6 +53,12 @@ namespace RTC
return false; return false;
} }
// Store the packet into the buffer.
StorePacket(packet);
// TODO: TMP
Dump();
return true; return true;
} }
...@@ -63,6 +66,7 @@ namespace RTC ...@@ -63,6 +66,7 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Initialize/reset RTP counters.
this->base_seq = seq; this->base_seq = seq;
this->max_seq = seq; this->max_seq = seq;
this->bad_seq = RTP_SEQ_MOD + 1; // So seq == bad_seq is false. this->bad_seq = RTP_SEQ_MOD + 1; // So seq == bad_seq is false.
...@@ -70,6 +74,9 @@ namespace RTC ...@@ -70,6 +74,9 @@ namespace RTC
this->received = 0; this->received = 0;
this->received_prior = 0; this->received_prior = 0;
this->expected_prior = 0; this->expected_prior = 0;
// Clean buffer.
CleanBuffer();
} }
bool RtpStream::UpdateSeq(uint16_t seq) bool RtpStream::UpdateSeq(uint16_t seq)
...@@ -147,98 +154,114 @@ namespace RTC ...@@ -147,98 +154,114 @@ namespace RTC
return true; return true;
} }
// TODO: REMOVE void RtpStream::CleanBuffer()
bool RtpStream::AddPacket(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
MS_WARN("BEGIN | seq_number: %" PRIu16, packet->GetSequenceNumber()); // Delete cloned packets.
for (auto& buffer_item : this->buffer)
{
delete buffer_item.packet;
}
// If empty do it easy. // Clear list.
if (this->packets.size() == 0) this->buffer.clear();
}
inline
void RtpStream::StorePacket(RTC::RtpPacket* packet)
{ {
MS_WARN(" empty packet list"); MS_TRACE();
// Sum the packet seq number and the number of 16 bits cycles.
uint32_t packet_seq32 = (uint32_t)packet->GetSequenceNumber() + this->cycles;
BufferItem buffer_item;
buffer_item.seq32 = packet_seq32;
// If empty do it easy.
if (this->buffer.size() == 0)
{
auto store = this->storage[0].store; auto store = this->storage[0].store;
auto cloned_packet = packet->Clone(store);
this->packets.push_back(cloned_packet); buffer_item.packet = packet->Clone(store);
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber()); this->buffer.push_back(buffer_item);
return true; return;
} }
// Otherwise, do the stuff. // Otherwise, do the stuff.
Packets::iterator new_it; Buffer::iterator new_buffer_it;
uint8_t* store = nullptr; uint8_t* store = nullptr;
auto it_r = this->packets.rbegin(); // Iterate the buffer in reverse order and find the proper place to store the
for (; it_r != this->packets.rend(); it_r++) // packet.
auto buffer_it_r = this->buffer.rbegin();
for (; buffer_it_r != this->buffer.rend(); buffer_it_r++)
{ {
auto current_packet = *it_r; auto current_seq32 = (*buffer_it_r).seq32;
if (packet->GetSequenceNumber() > current_packet->GetSequenceNumber()) if (packet_seq32 > current_seq32)
{ {
// Get a forward iterator pointing to the same element. // Get a forward iterator pointing to the same element.
auto it = it_r.base(); auto it = buffer_it_r.base();
new_it = this->packets.insert(it, nullptr); new_buffer_it = this->buffer.insert(it, buffer_item);
// Exit the loop. // Exit the loop.
break; break;
} }
} }
// If the packet was older than anything in the storate, just ignore it. // If the packet was older than anything in the buffer, just ignore it.
if (it_r == this->packets.rend()) // NOTE: This should never happen.
if (buffer_it_r == this->buffer.rend())
{ {
// TODO: Wrong, this will drop all the received packets once sequence_number MS_WARN("packet is older than anything in the buffer, ignoring it");
// reaches 65535
MS_WARN(" packet is older than anything in the storage, ignoring it");
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber());
return false; return;
} }
MS_WARN(" packets.size:%zu, storage.size:%zu", this->packets.size(), this->storage.size()); // If the storage is not full use its next free store.
if (this->buffer.size() - 1 < 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 = this->storage[this->buffer.size() - 1].store;
// 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. // Otherwise remove the first packet of the buffer and replace its storage area.
else else
{ {
auto first_packet = *(this->packets.begin()); auto& first_buffer_item = *(this->buffer.begin());
auto first_packet = first_buffer_item.packet;
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 points to the store used by the first packet.
store = (uint8_t*)first_packet->GetRaw(); store = (uint8_t*)first_packet->GetRaw();
// Free the first packet. // Free the first packet.
delete first_packet; delete first_packet;
// Remove the first element in the list. // Remove the first element in the list.
this->packets.pop_front(); this->buffer.pop_front();
} }
// Update the new list entry so it points to the cloned packed. // Update the new buffer item so it points to the cloned packed.
*new_it = packet->Clone(store); (*new_buffer_it).packet = packet->Clone(store);
}
MS_WARN("END | seq_number: %" PRIu16, packet->GetSequenceNumber()); // TODO: TMP
void RtpStream::Dump()
{
MS_TRACE();
MS_WARN("<DUMP>"); MS_WARN("<DUMP>");
for (auto& packet : this->packets)
MS_WARN(" [buffer.size:%zu, storage.size:%zu]", this->buffer.size(), this->storage.size());
for (auto& buffer_item : this->buffer)
{ {
MS_WARN(" - seq_number: %" PRIu16, packet->GetSequenceNumber()); auto packet = buffer_item.packet;
MS_WARN(" packet [seq:%" PRIu16 ", seq32:%" PRIu32 "]", packet->GetSequenceNumber(), buffer_item.seq32);
} }
MS_WARN("</DUMP>\n");
return true; MS_WARN("</DUMP>");
} }
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论