ZLMediaKit/srt/PacketQueue.hpp

102 lines
2.7 KiB
C++
Raw Normal View History

2022-06-03 13:25:32 +08:00
#ifndef ZLMEDIAKIT_SRT_PACKET_QUEUE_H
#define ZLMEDIAKIT_SRT_PACKET_QUEUE_H
#include "Packet.hpp"
#include <algorithm>
#include <list>
2022-06-03 13:25:32 +08:00
#include <map>
2022-06-07 09:52:20 +08:00
#include <memory>
2022-06-03 13:25:32 +08:00
#include <tuple>
#include <utility>
2022-06-12 21:11:50 +08:00
#include <vector>
2022-06-03 13:25:32 +08:00
namespace SRT {
2022-06-03 13:25:32 +08:00
2022-06-12 21:11:50 +08:00
class PacketQueueInterface {
public:
using Ptr = std::shared_ptr<PacketQueueInterface>;
using LostPair = std::pair<uint32_t, uint32_t>;
PacketQueueInterface() = default;
virtual ~PacketQueueInterface() = default;
virtual bool inputPacket(DataPacket::Ptr pkt, std::list<DataPacket::Ptr> &out) = 0;
virtual uint32_t timeLatency() = 0;
virtual std::list<LostPair> getLostSeq() = 0;
virtual size_t getSize() = 0;
virtual size_t getExpectedSize() = 0;
virtual size_t getAvailableBufferSize() = 0;
virtual uint32_t getExpectedSeq() = 0;
virtual std::string dump() = 0;
virtual bool drop(uint32_t first, uint32_t last, std::list<DataPacket::Ptr> &out) = 0;
};
// for recv
2022-06-12 21:11:50 +08:00
class PacketQueue : public PacketQueueInterface {
2022-06-03 13:25:32 +08:00
public:
using Ptr = std::shared_ptr<PacketQueue>;
2022-06-07 09:52:20 +08:00
PacketQueue(uint32_t max_size, uint32_t init_seq, uint32_t latency);
2022-06-03 13:25:32 +08:00
~PacketQueue() = default;
2022-06-07 09:52:20 +08:00
bool inputPacket(DataPacket::Ptr pkt, std::list<DataPacket::Ptr> &out);
2022-06-07 09:52:20 +08:00
uint32_t timeLatency();
2022-06-03 13:25:32 +08:00
std::list<LostPair> getLostSeq();
size_t getSize();
size_t getExpectedSize();
size_t getAvailableBufferSize();
uint32_t getExpectedSeq();
std::string dump();
2022-06-07 09:52:20 +08:00
bool drop(uint32_t first, uint32_t last, std::list<DataPacket::Ptr> &out);
2022-06-03 13:25:32 +08:00
private:
void tryInsertPkt(DataPacket::Ptr pkt);
2022-06-07 09:52:20 +08:00
private:
2022-06-03 13:25:32 +08:00
uint32_t _pkt_cap;
2022-06-07 09:52:20 +08:00
uint32_t _pkt_latency;
2022-06-12 21:11:50 +08:00
uint32_t _pkt_expected_seq;
2022-06-07 09:52:20 +08:00
std::map<uint32_t, DataPacket::Ptr> _pkt_map;
2022-06-03 13:25:32 +08:00
};
2022-06-12 21:11:50 +08:00
class PacketRecvQueue : public PacketQueueInterface {
public:
using Ptr = std::shared_ptr<PacketRecvQueue>;
PacketRecvQueue(uint32_t max_size, uint32_t init_seq, uint32_t latency);
~PacketRecvQueue() = default;
bool inputPacket(DataPacket::Ptr pkt, std::list<DataPacket::Ptr> &out);
uint32_t timeLatency();
std::list<LostPair> getLostSeq();
size_t getSize();
size_t getExpectedSize();
size_t getAvailableBufferSize();
uint32_t getExpectedSeq();
std::string dump();
bool drop(uint32_t first, uint32_t last, std::list<DataPacket::Ptr> &out);
private:
void tryInsertPkt(DataPacket::Ptr pkt);
void insertToCycleBuf(DataPacket::Ptr pkt, uint32_t diff);
DataPacket::Ptr getFirst();
DataPacket::Ptr getLast();
private:
uint32_t _pkt_cap;
uint32_t _pkt_latency;
uint32_t _pkt_expected_seq;
std::vector<DataPacket::Ptr> _pkt_buf;
uint32_t _start = 0;
uint32_t _end = 0;
size_t _size = 0;
};
} // namespace SRT
2022-06-03 13:25:32 +08:00
#endif // ZLMEDIAKIT_SRT_PACKET_QUEUE_H