utils_packet_queue.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #ifndef AV_UTILS_PACKET_QUEUE_H
  2. #define AV_UTILS_PACKET_QUEUE_H
  3. #include "../base/types.h"
  4. #include <queue>
  5. #include <mutex>
  6. #include <shared_mutex>
  7. #include <condition_variable>
  8. #include <atomic>
  9. #include <chrono>
  10. #include <memory>
  11. #include <functional>
  12. #include <map>
  13. #include <vector>
  14. extern "C" {
  15. #include <libavcodec/packet.h>
  16. #include <libavutil/avutil.h>
  17. }
  18. namespace av {
  19. namespace utils {
  20. using namespace av;
  21. // 包队列项
  22. struct PacketQueueItem {
  23. AVPacket* packet = nullptr; // 包数据
  24. int64_t pts = AV_NOPTS_VALUE; // 显示时间戳
  25. int64_t dts = AV_NOPTS_VALUE; // 解码时间戳
  26. double duration = 0.0; // 包持续时间
  27. int streamIndex = -1; // 流索引
  28. bool isKeyPacket = false; // 是否关键包
  29. int priority = 0; // 优先级(数值越大优先级越高)
  30. std::chrono::steady_clock::time_point enqueueTime; // 入队时间
  31. PacketQueueItem() {
  32. enqueueTime = std::chrono::steady_clock::now();
  33. }
  34. PacketQueueItem(AVPacket* p, int stream = -1, int prio = 0)
  35. : packet(p), streamIndex(stream), priority(prio) {
  36. if (packet) {
  37. pts = packet->pts;
  38. dts = packet->dts;
  39. isKeyPacket = (packet->flags & AV_PKT_FLAG_KEY) != 0;
  40. }
  41. enqueueTime = std::chrono::steady_clock::now();
  42. }
  43. ~PacketQueueItem() {
  44. if (packet) {
  45. av_packet_free(&packet);
  46. }
  47. }
  48. // 移动构造函数
  49. PacketQueueItem(PacketQueueItem&& other) noexcept
  50. : packet(other.packet), pts(other.pts), dts(other.dts),
  51. duration(other.duration), streamIndex(other.streamIndex),
  52. isKeyPacket(other.isKeyPacket), priority(other.priority),
  53. enqueueTime(other.enqueueTime) {
  54. other.packet = nullptr;
  55. }
  56. // 移动赋值操作符
  57. PacketQueueItem& operator=(PacketQueueItem&& other) noexcept {
  58. if (this != &other) {
  59. if (packet) {
  60. av_packet_free(&packet);
  61. }
  62. packet = other.packet;
  63. pts = other.pts;
  64. dts = other.dts;
  65. duration = other.duration;
  66. streamIndex = other.streamIndex;
  67. isKeyPacket = other.isKeyPacket;
  68. priority = other.priority;
  69. enqueueTime = other.enqueueTime;
  70. other.packet = nullptr;
  71. }
  72. return *this;
  73. }
  74. // 禁用拷贝
  75. PacketQueueItem(const PacketQueueItem&) = delete;
  76. PacketQueueItem& operator=(const PacketQueueItem&) = delete;
  77. };
  78. // 包队列统计信息
  79. struct PacketQueueStats {
  80. size_t currentSize = 0; // 当前队列大小
  81. size_t maxSize = 0; // 最大队列大小
  82. uint64_t totalEnqueued = 0; // 总入队数量
  83. uint64_t totalDequeued = 0; // 总出队数量
  84. uint64_t totalDropped = 0; // 总丢弃数量
  85. uint64_t totalBytes = 0; // 总字节数
  86. double averageWaitTime = 0.0; // 平均等待时间(毫秒)
  87. double maxWaitTime = 0.0; // 最大等待时间(毫秒)
  88. double averagePacketSize = 0.0; // 平均包大小
  89. std::chrono::steady_clock::time_point lastUpdateTime;
  90. PacketQueueStats() {
  91. lastUpdateTime = std::chrono::steady_clock::now();
  92. }
  93. };
  94. // 包队列配置
  95. struct PacketQueueConfig {
  96. size_t maxSize = 200; // 最大队列大小
  97. size_t maxBytes = 50 * 1024 * 1024; // 最大字节数(50MB)
  98. bool dropOnFull = true; // 队列满时是否丢弃新包
  99. bool dropOldest = true; // 丢弃最旧的包(false则丢弃最新的)
  100. bool priorityQueue = false; // 是否启用优先级队列
  101. int timeoutMs = 1000; // 超时时间(毫秒)
  102. bool enableStats = true; // 启用统计信息
  103. // 丢包策略
  104. bool enablePacketDrop = false; // 启用智能丢包
  105. double maxLatency = 200.0; // 最大延迟(毫秒)
  106. int dropRatio = 3; // 丢包比例(每N包丢1包)
  107. bool dropNonKeyPackets = true; // 优先丢弃非关键包
  108. };
  109. // 优先级比较器
  110. struct PacketPriorityCompare {
  111. bool operator()(const std::unique_ptr<PacketQueueItem>& a,
  112. const std::unique_ptr<PacketQueueItem>& b) const {
  113. // 优先级高的在前
  114. if (a->priority != b->priority) {
  115. return a->priority < b->priority;
  116. }
  117. // 关键包优先
  118. if (a->isKeyPacket != b->isKeyPacket) {
  119. return !a->isKeyPacket && b->isKeyPacket;
  120. }
  121. // 时间戳小的在前
  122. return a->dts > b->dts;
  123. }
  124. };
  125. // 包队列类
  126. class PacketQueue {
  127. public:
  128. explicit PacketQueue(const PacketQueueConfig& config = PacketQueueConfig());
  129. ~PacketQueue();
  130. // 基本操作
  131. ErrorCode enqueue(std::unique_ptr<PacketQueueItem> item);
  132. std::unique_ptr<PacketQueueItem> dequeue();
  133. std::unique_ptr<PacketQueueItem> dequeue(int timeoutMs);
  134. // 便捷方法
  135. ErrorCode enqueue(AVPacket* packet, int streamIndex = -1, int priority = 0);
  136. AVPacket* dequeuePacket();
  137. AVPacket* dequeuePacket(int timeoutMs);
  138. // 优先级队列操作
  139. ErrorCode enqueueWithPriority(AVPacket* packet, int priority, int streamIndex = -1);
  140. std::unique_ptr<PacketQueueItem> dequeueHighestPriority();
  141. // 队列控制
  142. void clear();
  143. void flush();
  144. void setMaxSize(size_t maxSize);
  145. void setMaxBytes(size_t maxBytes);
  146. void setDropPolicy(bool dropOnFull, bool dropOldest = true);
  147. void enablePriorityQueue(bool enable);
  148. // 状态查询
  149. size_t size() const;
  150. size_t bytes() const;
  151. bool empty() const;
  152. bool full() const;
  153. bool fullByBytes() const;
  154. size_t capacity() const;
  155. size_t capacityBytes() const;
  156. // 统计信息
  157. PacketQueueStats getStats() const;
  158. void resetStats();
  159. // 包丢弃策略
  160. void enablePacketDrop(bool enable, double maxLatency = 200.0, int dropRatio = 3);
  161. void setPacketDropCallback(std::function<bool(const PacketQueueItem&)> callback);
  162. // 阻塞控制
  163. void setBlocking(bool blocking);
  164. void wakeup();
  165. // 流过滤
  166. void setStreamFilter(const std::vector<int>& allowedStreams);
  167. void clearStreamFilter();
  168. // 回调设置
  169. using EnqueueCallback = std::function<void(const PacketQueueItem&)>;
  170. using DequeueCallback = std::function<void(const PacketQueueItem&)>;
  171. using DropCallback = std::function<void(const PacketQueueItem&, const std::string&)>;
  172. void setEnqueueCallback(EnqueueCallback callback) { enqueueCallback_ = callback; }
  173. void setDequeueCallback(DequeueCallback callback) { dequeueCallback_ = callback; }
  174. void setDropCallback(DropCallback callback) { dropCallback_ = callback; }
  175. protected:
  176. // 内部方法
  177. bool shouldDropPacket(const PacketQueueItem& item) const;
  178. void dropOldestPacket();
  179. void dropNewestPacket();
  180. void dropLargestPacket();
  181. void dropNonKeyPackets();
  182. void updateStats(const PacketQueueItem& item, bool isEnqueue);
  183. double calculateWaitTime(const PacketQueueItem& item) const;
  184. bool isStreamAllowed(int streamIndex) const;
  185. // 智能丢包
  186. bool shouldDropByLatency(const PacketQueueItem& item) const;
  187. bool shouldDropByRatio() const;
  188. bool shouldDropBySize() const;
  189. // 队列操作
  190. void enqueueToNormalQueue(std::unique_ptr<PacketQueueItem> item);
  191. void enqueueToPriorityQueue(std::unique_ptr<PacketQueueItem> item);
  192. std::unique_ptr<PacketQueueItem> dequeueFromNormalQueue();
  193. std::unique_ptr<PacketQueueItem> dequeueFromPriorityQueue();
  194. private:
  195. PacketQueueConfig config_;
  196. // 队列数据
  197. std::queue<std::unique_ptr<PacketQueueItem>> normalQueue_;
  198. std::priority_queue<std::unique_ptr<PacketQueueItem>,
  199. std::vector<std::unique_ptr<PacketQueueItem>>,
  200. PacketPriorityCompare> priorityQueue_;
  201. mutable std::mutex queueMutex_;
  202. std::condition_variable notEmpty_;
  203. std::condition_variable notFull_;
  204. // 状态控制
  205. std::atomic<bool> blocking_{true};
  206. std::atomic<bool> shutdown_{false};
  207. std::atomic<size_t> totalBytes_{0};
  208. // 统计信息
  209. mutable std::mutex statsMutex_;
  210. PacketQueueStats stats_;
  211. // 丢包控制
  212. mutable std::atomic<uint64_t> packetCounter_{0};
  213. std::function<bool(const PacketQueueItem&)> packetDropCallback_;
  214. // 流过滤
  215. std::vector<int> allowedStreams_;
  216. bool hasStreamFilter_ = false;
  217. mutable std::mutex streamFilterMutex_;
  218. // 回调函数
  219. EnqueueCallback enqueueCallback_;
  220. DequeueCallback dequeueCallback_;
  221. DropCallback dropCallback_;
  222. };
  223. // 多流包队列
  224. class MultiStreamPacketQueue {
  225. public:
  226. explicit MultiStreamPacketQueue(const PacketQueueConfig& config = PacketQueueConfig());
  227. ~MultiStreamPacketQueue();
  228. // 流管理
  229. ErrorCode addStream(int streamIndex, const PacketQueueConfig& config = PacketQueueConfig());
  230. ErrorCode removeStream(int streamIndex);
  231. bool hasStream(int streamIndex) const;
  232. std::vector<int> getStreamIndices() const;
  233. // 包操作
  234. ErrorCode enqueue(AVPacket* packet, int streamIndex);
  235. AVPacket* dequeue(int streamIndex);
  236. AVPacket* dequeue(int streamIndex, int timeoutMs);
  237. // 批量操作
  238. ErrorCode enqueueToAll(AVPacket* packet);
  239. std::vector<AVPacket*> dequeueFromAll();
  240. // 同步出队(按时间戳顺序)
  241. AVPacket* dequeueSynchronized();
  242. std::vector<AVPacket*> dequeueSynchronizedBatch(size_t maxCount = 10);
  243. // 队列控制
  244. void clear();
  245. void clearStream(int streamIndex);
  246. void flush();
  247. void flushStream(int streamIndex);
  248. // 状态查询
  249. size_t size() const;
  250. size_t size(int streamIndex) const;
  251. size_t bytes() const;
  252. size_t bytes(int streamIndex) const;
  253. bool empty() const;
  254. bool empty(int streamIndex) const;
  255. // 统计信息
  256. std::map<int, PacketQueueStats> getAllStats() const;
  257. PacketQueueStats getStats(int streamIndex) const;
  258. void resetStats();
  259. void resetStats(int streamIndex);
  260. // 同步控制
  261. void setBlocking(bool blocking);
  262. void wakeupAll();
  263. private:
  264. PacketQueueConfig defaultConfig_;
  265. std::map<int, std::unique_ptr<PacketQueue>> streamQueues_;
  266. mutable std::shared_mutex streamsMutex_;
  267. // 同步出队相关
  268. struct SyncItem {
  269. AVPacket* packet;
  270. int streamIndex;
  271. int64_t timestamp;
  272. bool operator>(const SyncItem& other) const {
  273. return timestamp > other.timestamp;
  274. }
  275. };
  276. std::priority_queue<SyncItem, std::vector<SyncItem>, std::greater<SyncItem>> syncQueue_;
  277. mutable std::mutex syncMutex_;
  278. };
  279. // 包队列工厂
  280. class PacketQueueFactory {
  281. public:
  282. // 创建标准包队列
  283. static std::unique_ptr<PacketQueue> createStandardQueue(size_t maxSize = 200);
  284. // 创建低延迟队列
  285. static std::unique_ptr<PacketQueue> createLowLatencyQueue(size_t maxSize = 50);
  286. // 创建高容量队列
  287. static std::unique_ptr<PacketQueue> createHighCapacityQueue(size_t maxSize = 2000);
  288. // 创建实时队列(启用智能丢包)
  289. static std::unique_ptr<PacketQueue> createRealtimeQueue(size_t maxSize = 100, double maxLatency = 100.0);
  290. // 创建优先级队列
  291. static std::unique_ptr<PacketQueue> createPriorityQueue(size_t maxSize = 200);
  292. // 创建多流队列
  293. static std::unique_ptr<MultiStreamPacketQueue> createMultiStreamQueue(const PacketQueueConfig& config = PacketQueueConfig());
  294. };
  295. } // namespace utils
  296. } // namespace av
  297. #endif // AV_UTILS_PACKET_QUEUE_H