utils_frame_queue.h 9.6 KB

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