utils_packet_queue.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // 兼容性方法 (push/pop 别名)
  139. ErrorCode push(AVPacket* packet, int streamIndex = -1, int priority = 0) {
  140. return enqueue(packet, streamIndex, priority);
  141. }
  142. AVPacket* pop() {
  143. return dequeuePacket();
  144. }
  145. AVPacket* pop(int timeoutMs) {
  146. return dequeuePacket(timeoutMs);
  147. }
  148. // 优先级队列操作
  149. ErrorCode enqueueWithPriority(AVPacket* packet, int priority, int streamIndex = -1);
  150. std::unique_ptr<PacketQueueItem> dequeueHighestPriority();
  151. // 队列控制
  152. void clear();
  153. void flush();
  154. void setMaxSize(size_t maxSize);
  155. void setMaxBytes(size_t maxBytes);
  156. void setDropPolicy(bool dropOnFull, bool dropOldest = true);
  157. void enablePriorityQueue(bool enable);
  158. // 状态查询
  159. size_t size() const;
  160. size_t bytes() const;
  161. bool empty() const;
  162. bool full() const;
  163. bool fullByBytes() const;
  164. size_t capacity() const;
  165. size_t capacityBytes() const;
  166. // 统计信息
  167. PacketQueueStats getStats() const;
  168. void resetStats();
  169. // 包丢弃策略
  170. void enablePacketDrop(bool enable, double maxLatency = 200.0, int dropRatio = 3);
  171. void setPacketDropCallback(std::function<bool(const PacketQueueItem&)> callback);
  172. // 阻塞控制
  173. void setBlocking(bool blocking);
  174. void wakeup();
  175. // 流过滤
  176. void setStreamFilter(const std::vector<int>& allowedStreams);
  177. void clearStreamFilter();
  178. // 回调设置
  179. using EnqueueCallback = std::function<void(const PacketQueueItem&)>;
  180. using DequeueCallback = std::function<void(const PacketQueueItem&)>;
  181. using DropCallback = std::function<void(const PacketQueueItem&, const std::string&)>;
  182. void setEnqueueCallback(EnqueueCallback callback) { enqueueCallback_ = callback; }
  183. void setDequeueCallback(DequeueCallback callback) { dequeueCallback_ = callback; }
  184. void setDropCallback(DropCallback callback) { dropCallback_ = callback; }
  185. protected:
  186. // 内部方法
  187. bool shouldDropPacket(const PacketQueueItem& item) const;
  188. void dropOldestPacket();
  189. void dropNewestPacket();
  190. void dropLargestPacket();
  191. void dropNonKeyPackets();
  192. void updateStats(const PacketQueueItem& item, bool isEnqueue);
  193. double calculateWaitTime(const PacketQueueItem& item) const;
  194. bool isStreamAllowed(int streamIndex) const;
  195. // 智能丢包
  196. bool shouldDropByLatency(const PacketQueueItem& item) const;
  197. bool shouldDropByRatio() const;
  198. bool shouldDropBySize() const;
  199. // 队列操作
  200. void enqueueToNormalQueue(std::unique_ptr<PacketQueueItem> item);
  201. void enqueueToPriorityQueue(std::unique_ptr<PacketQueueItem> item);
  202. std::unique_ptr<PacketQueueItem> dequeueFromNormalQueue();
  203. std::unique_ptr<PacketQueueItem> dequeueFromPriorityQueue();
  204. private:
  205. PacketQueueConfig config_;
  206. // 队列数据
  207. std::queue<std::unique_ptr<PacketQueueItem>> normalQueue_;
  208. std::priority_queue<std::unique_ptr<PacketQueueItem>,
  209. std::vector<std::unique_ptr<PacketQueueItem>>,
  210. PacketPriorityCompare> priorityQueue_;
  211. mutable std::mutex queueMutex_;
  212. std::condition_variable notEmpty_;
  213. std::condition_variable notFull_;
  214. // 状态控制
  215. std::atomic<bool> blocking_{true};
  216. std::atomic<bool> shutdown_{false};
  217. std::atomic<size_t> totalBytes_{0};
  218. // 统计信息
  219. mutable std::mutex statsMutex_;
  220. PacketQueueStats stats_;
  221. // 丢包控制
  222. mutable std::atomic<uint64_t> packetCounter_{0};
  223. std::function<bool(const PacketQueueItem&)> packetDropCallback_;
  224. // 流过滤
  225. std::vector<int> allowedStreams_;
  226. bool hasStreamFilter_ = false;
  227. mutable std::mutex streamFilterMutex_;
  228. // 回调函数
  229. EnqueueCallback enqueueCallback_;
  230. DequeueCallback dequeueCallback_;
  231. DropCallback dropCallback_;
  232. };
  233. // 多流包队列
  234. class MultiStreamPacketQueue {
  235. public:
  236. explicit MultiStreamPacketQueue(const PacketQueueConfig& config = PacketQueueConfig());
  237. ~MultiStreamPacketQueue();
  238. // 流管理
  239. ErrorCode addStream(int streamIndex, const PacketQueueConfig& config = PacketQueueConfig());
  240. ErrorCode removeStream(int streamIndex);
  241. bool hasStream(int streamIndex) const;
  242. std::vector<int> getStreamIndices() const;
  243. // 包操作
  244. ErrorCode enqueue(AVPacket* packet, int streamIndex);
  245. AVPacket* dequeue(int streamIndex);
  246. AVPacket* dequeue(int streamIndex, int timeoutMs);
  247. // 批量操作
  248. ErrorCode enqueueToAll(AVPacket* packet);
  249. std::vector<AVPacket*> dequeueFromAll();
  250. // 同步出队(按时间戳顺序)
  251. AVPacket* dequeueSynchronized();
  252. std::vector<AVPacket*> dequeueSynchronizedBatch(size_t maxCount = 10);
  253. // 队列控制
  254. void clear();
  255. void clearStream(int streamIndex);
  256. void flush();
  257. void flushStream(int streamIndex);
  258. // 状态查询
  259. size_t size() const;
  260. size_t size(int streamIndex) const;
  261. size_t bytes() const;
  262. size_t bytes(int streamIndex) const;
  263. bool empty() const;
  264. bool empty(int streamIndex) const;
  265. // 统计信息
  266. std::map<int, PacketQueueStats> getAllStats() const;
  267. PacketQueueStats getStats(int streamIndex) const;
  268. void resetStats();
  269. void resetStats(int streamIndex);
  270. // 同步控制
  271. void setBlocking(bool blocking);
  272. void wakeupAll();
  273. private:
  274. PacketQueueConfig defaultConfig_;
  275. std::map<int, std::unique_ptr<PacketQueue>> streamQueues_;
  276. mutable std::shared_mutex streamsMutex_;
  277. // 同步出队相关
  278. struct SyncItem {
  279. AVPacket* packet;
  280. int streamIndex;
  281. int64_t timestamp;
  282. bool operator>(const SyncItem& other) const {
  283. return timestamp > other.timestamp;
  284. }
  285. };
  286. std::priority_queue<SyncItem, std::vector<SyncItem>, std::greater<SyncItem>> syncQueue_;
  287. mutable std::mutex syncMutex_;
  288. };
  289. // 包队列工厂
  290. class PacketQueueFactory {
  291. public:
  292. // 创建标准包队列
  293. static std::unique_ptr<PacketQueue> createStandardQueue(size_t maxSize = 200);
  294. // 创建低延迟队列
  295. static std::unique_ptr<PacketQueue> createLowLatencyQueue(size_t maxSize = 50);
  296. // 创建高容量队列
  297. static std::unique_ptr<PacketQueue> createHighCapacityQueue(size_t maxSize = 2000);
  298. // 创建实时队列(启用智能丢包)
  299. static std::unique_ptr<PacketQueue> createRealtimeQueue(size_t maxSize = 100, double maxLatency = 100.0);
  300. // 创建优先级队列
  301. static std::unique_ptr<PacketQueue> createPriorityQueue(size_t maxSize = 200);
  302. // 创建多流队列
  303. static std::unique_ptr<MultiStreamPacketQueue> createMultiStreamQueue(const PacketQueueConfig& config = PacketQueueConfig());
  304. };
  305. } // namespace utils
  306. } // namespace av
  307. #endif // AV_UTILS_PACKET_QUEUE_H