utils_frame_queue.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #ifndef AV_UTILS_FRAME_QUEUE_H
  2. #define AV_UTILS_FRAME_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. #include <map>
  12. #include <shared_mutex>
  13. #include <vector>
  14. extern "C" {
  15. #include <libavutil/frame.h>
  16. #include <libavutil/avutil.h>
  17. }
  18. namespace av {
  19. namespace utils {
  20. using namespace av;
  21. // 帧队列项
  22. struct FrameQueueItem {
  23. AVFrame* frame = 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 isKeyFrame = false; // 是否关键帧
  29. std::chrono::steady_clock::time_point enqueueTime; // 入队时间
  30. FrameQueueItem() {
  31. enqueueTime = std::chrono::steady_clock::now();
  32. }
  33. FrameQueueItem(AVFrame* f, int stream = -1) : frame(f), streamIndex(stream) {
  34. if (frame) {
  35. pts = frame->pts;
  36. dts = frame->pkt_dts;
  37. isKeyFrame = (frame->key_frame == 1);
  38. }
  39. enqueueTime = std::chrono::steady_clock::now();
  40. }
  41. ~FrameQueueItem() {
  42. if (frame) {
  43. av_frame_free(&frame);
  44. }
  45. }
  46. // 移动构造函数
  47. FrameQueueItem(FrameQueueItem&& other) noexcept
  48. : frame(other.frame), pts(other.pts), dts(other.dts),
  49. duration(other.duration), streamIndex(other.streamIndex),
  50. isKeyFrame(other.isKeyFrame), enqueueTime(other.enqueueTime) {
  51. other.frame = nullptr;
  52. }
  53. // 移动赋值操作符
  54. FrameQueueItem& operator=(FrameQueueItem&& other) noexcept {
  55. if (this != &other) {
  56. if (frame) {
  57. av_frame_free(&frame);
  58. }
  59. frame = other.frame;
  60. pts = other.pts;
  61. dts = other.dts;
  62. duration = other.duration;
  63. streamIndex = other.streamIndex;
  64. isKeyFrame = other.isKeyFrame;
  65. enqueueTime = other.enqueueTime;
  66. other.frame = nullptr;
  67. }
  68. return *this;
  69. }
  70. // 禁用拷贝
  71. FrameQueueItem(const FrameQueueItem&) = delete;
  72. FrameQueueItem& operator=(const FrameQueueItem&) = delete;
  73. };
  74. // 帧队列统计信息
  75. struct FrameQueueStats {
  76. size_t currentSize = 0; // 当前队列大小
  77. size_t maxSize = 0; // 最大队列大小
  78. uint64_t totalEnqueued = 0; // 总入队数量
  79. uint64_t totalDequeued = 0; // 总出队数量
  80. uint64_t totalDropped = 0; // 总丢弃数量
  81. double averageWaitTime = 0.0; // 平均等待时间(毫秒)
  82. double maxWaitTime = 0.0; // 最大等待时间(毫秒)
  83. std::chrono::steady_clock::time_point lastUpdateTime;
  84. FrameQueueStats() {
  85. lastUpdateTime = std::chrono::steady_clock::now();
  86. }
  87. };
  88. // 帧队列配置
  89. struct FrameQueueConfig {
  90. size_t maxSize = 100; // 最大队列大小
  91. bool dropOnFull = true; // 队列满时是否丢弃新帧
  92. bool dropOldest = true; // 丢弃最旧的帧(false则丢弃最新的)
  93. int timeoutMs = 1000; // 超时时间(毫秒)
  94. bool enableStats = true; // 启用统计信息
  95. // 丢帧策略
  96. bool enableFrameDrop = false; // 启用智能丢帧
  97. double maxLatency = 100.0; // 最大延迟(毫秒)
  98. int dropRatio = 2; // 丢帧比例(每N帧丢1帧)
  99. };
  100. // 帧队列类
  101. class FrameQueue {
  102. public:
  103. explicit FrameQueue(const FrameQueueConfig& config = FrameQueueConfig());
  104. ~FrameQueue();
  105. // 基本操作
  106. ErrorCode enqueue(std::unique_ptr<FrameQueueItem> item);
  107. std::unique_ptr<FrameQueueItem> dequeue();
  108. std::unique_ptr<FrameQueueItem> dequeue(int timeoutMs);
  109. // 便捷方法
  110. ErrorCode enqueue(AVFrame* frame, int streamIndex = -1);
  111. AVFrame* dequeueFrame();
  112. AVFrame* dequeueFrame(int timeoutMs);
  113. // 兼容性方法 (push/pop 别名)
  114. ErrorCode push(AVFrame* frame, int streamIndex = -1) {
  115. return enqueue(frame, streamIndex);
  116. }
  117. AVFrame* pop() {
  118. return dequeueFrame();
  119. }
  120. AVFrame* pop(int timeoutMs) {
  121. return dequeueFrame(timeoutMs);
  122. }
  123. // 队列控制
  124. void clear();
  125. void flush();
  126. void setMaxSize(size_t maxSize);
  127. void setDropPolicy(bool dropOnFull, bool dropOldest = true);
  128. // 状态查询
  129. size_t size() const;
  130. bool empty() const;
  131. bool full() const;
  132. size_t capacity() const;
  133. // 统计信息
  134. FrameQueueStats getStats() const;
  135. void resetStats();
  136. // 帧丢弃策略
  137. void enableFrameDrop(bool enable, double maxLatency = 100.0, int dropRatio = 2);
  138. void setFrameDropCallback(std::function<bool(const FrameQueueItem&)> callback);
  139. // 阻塞控制
  140. void setBlocking(bool blocking);
  141. void wakeup();
  142. // 回调设置
  143. using EnqueueCallback = std::function<void(const FrameQueueItem&)>;
  144. using DequeueCallback = std::function<void(const FrameQueueItem&)>;
  145. using DropCallback = std::function<void(const FrameQueueItem&, const std::string&)>;
  146. void setEnqueueCallback(EnqueueCallback callback) { enqueueCallback_ = callback; }
  147. void setDequeueCallback(DequeueCallback callback) { dequeueCallback_ = callback; }
  148. void setDropCallback(DropCallback callback) { dropCallback_ = callback; }
  149. protected:
  150. // 内部方法
  151. bool shouldDropFrame(const FrameQueueItem& item) const;
  152. void dropOldestFrame();
  153. void dropNewestFrame();
  154. void updateStats(const FrameQueueItem& item, bool isEnqueue);
  155. double calculateWaitTime(const FrameQueueItem& item) const;
  156. // 智能丢帧
  157. bool shouldDropByLatency(const FrameQueueItem& item) const;
  158. bool shouldDropByRatio() const;
  159. private:
  160. FrameQueueConfig config_;
  161. // 队列数据
  162. std::queue<std::unique_ptr<FrameQueueItem>> queue_;
  163. mutable std::mutex queueMutex_;
  164. std::condition_variable notEmpty_;
  165. std::condition_variable notFull_;
  166. // 状态控制
  167. std::atomic<bool> blocking_{true};
  168. std::atomic<bool> shutdown_{false};
  169. // 统计信息
  170. mutable std::mutex statsMutex_;
  171. FrameQueueStats stats_;
  172. // 丢帧控制
  173. mutable std::atomic<uint64_t> frameCounter_{0};
  174. std::function<bool(const FrameQueueItem&)> frameDropCallback_;
  175. // 回调函数
  176. EnqueueCallback enqueueCallback_;
  177. DequeueCallback dequeueCallback_;
  178. DropCallback dropCallback_;
  179. };
  180. // 多流帧队列
  181. class MultiStreamFrameQueue {
  182. public:
  183. explicit MultiStreamFrameQueue(const FrameQueueConfig& config = FrameQueueConfig());
  184. ~MultiStreamFrameQueue();
  185. // 流管理
  186. ErrorCode addStream(int streamIndex, const FrameQueueConfig& config = FrameQueueConfig());
  187. ErrorCode removeStream(int streamIndex);
  188. bool hasStream(int streamIndex) const;
  189. std::vector<int> getStreamIndices() const;
  190. // 帧操作
  191. ErrorCode enqueue(AVFrame* frame, int streamIndex);
  192. AVFrame* dequeue(int streamIndex);
  193. AVFrame* dequeue(int streamIndex, int timeoutMs);
  194. // 批量操作
  195. ErrorCode enqueueToAll(AVFrame* frame);
  196. std::vector<AVFrame*> dequeueFromAll();
  197. // 队列控制
  198. void clear();
  199. void clearStream(int streamIndex);
  200. void flush();
  201. void flushStream(int streamIndex);
  202. // 状态查询
  203. size_t size() const;
  204. size_t size(int streamIndex) const;
  205. bool empty() const;
  206. bool empty(int streamIndex) const;
  207. // 统计信息
  208. std::map<int, FrameQueueStats> getAllStats() const;
  209. FrameQueueStats getStats(int streamIndex) const;
  210. void resetStats();
  211. void resetStats(int streamIndex);
  212. // 同步控制
  213. void setBlocking(bool blocking);
  214. void wakeupAll();
  215. private:
  216. FrameQueueConfig defaultConfig_;
  217. std::map<int, std::unique_ptr<FrameQueue>> streamQueues_;
  218. mutable std::shared_mutex streamsMutex_;
  219. };
  220. // 帧队列工厂
  221. class FrameQueueFactory {
  222. public:
  223. // 创建标准帧队列
  224. static std::unique_ptr<FrameQueue> createStandardQueue(size_t maxSize = 100);
  225. // 创建低延迟队列
  226. static std::unique_ptr<FrameQueue> createLowLatencyQueue(size_t maxSize = 10);
  227. // 创建高容量队列
  228. static std::unique_ptr<FrameQueue> createHighCapacityQueue(size_t maxSize = 1000);
  229. // 创建实时队列(启用智能丢帧)
  230. static std::unique_ptr<FrameQueue> createRealtimeQueue(size_t maxSize = 50, double maxLatency = 50.0);
  231. // 创建多流队列
  232. static std::unique_ptr<MultiStreamFrameQueue> createMultiStreamQueue(const FrameQueueConfig& config = FrameQueueConfig());
  233. };
  234. } // namespace utils
  235. } // namespace av
  236. #endif // AV_UTILS_FRAME_QUEUE_H