utils_packet_queue.h 12 KB

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