utils_frame_queue.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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::base;
  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. // 队列控制
  114. void clear();
  115. void flush();
  116. void setMaxSize(size_t maxSize);
  117. void setDropPolicy(bool dropOnFull, bool dropOldest = true);
  118. // 状态查询
  119. size_t size() const;
  120. bool empty() const;
  121. bool full() const;
  122. size_t capacity() const;
  123. // 统计信息
  124. FrameQueueStats getStats() const;
  125. void resetStats();
  126. // 帧丢弃策略
  127. void enableFrameDrop(bool enable, double maxLatency = 100.0, int dropRatio = 2);
  128. void setFrameDropCallback(std::function<bool(const FrameQueueItem&)> callback);
  129. // 阻塞控制
  130. void setBlocking(bool blocking);
  131. void wakeup();
  132. // 回调设置
  133. using EnqueueCallback = std::function<void(const FrameQueueItem&)>;
  134. using DequeueCallback = std::function<void(const FrameQueueItem&)>;
  135. using DropCallback = std::function<void(const FrameQueueItem&, const std::string&)>;
  136. void setEnqueueCallback(EnqueueCallback callback) { enqueueCallback_ = callback; }
  137. void setDequeueCallback(DequeueCallback callback) { dequeueCallback_ = callback; }
  138. void setDropCallback(DropCallback callback) { dropCallback_ = callback; }
  139. protected:
  140. // 内部方法
  141. bool shouldDropFrame(const FrameQueueItem& item) const;
  142. void dropOldestFrame();
  143. void dropNewestFrame();
  144. void updateStats(const FrameQueueItem& item, bool isEnqueue);
  145. double calculateWaitTime(const FrameQueueItem& item) const;
  146. // 智能丢帧
  147. bool shouldDropByLatency(const FrameQueueItem& item) const;
  148. bool shouldDropByRatio() const;
  149. private:
  150. FrameQueueConfig config_;
  151. // 队列数据
  152. std::queue<std::unique_ptr<FrameQueueItem>> queue_;
  153. mutable std::mutex queueMutex_;
  154. std::condition_variable notEmpty_;
  155. std::condition_variable notFull_;
  156. // 状态控制
  157. std::atomic<bool> blocking_{true};
  158. std::atomic<bool> shutdown_{false};
  159. // 统计信息
  160. mutable std::mutex statsMutex_;
  161. FrameQueueStats stats_;
  162. // 丢帧控制
  163. std::atomic<uint64_t> frameCounter_{0};
  164. std::function<bool(const FrameQueueItem&)> frameDropCallback_;
  165. // 回调函数
  166. EnqueueCallback enqueueCallback_;
  167. DequeueCallback dequeueCallback_;
  168. DropCallback dropCallback_;
  169. };
  170. // 多流帧队列
  171. class MultiStreamFrameQueue {
  172. public:
  173. explicit MultiStreamFrameQueue(const FrameQueueConfig& config = FrameQueueConfig());
  174. ~MultiStreamFrameQueue();
  175. // 流管理
  176. ErrorCode addStream(int streamIndex, const FrameQueueConfig& config = FrameQueueConfig());
  177. ErrorCode removeStream(int streamIndex);
  178. bool hasStream(int streamIndex) const;
  179. std::vector<int> getStreamIndices() const;
  180. // 帧操作
  181. ErrorCode enqueue(AVFrame* frame, int streamIndex);
  182. AVFrame* dequeue(int streamIndex);
  183. AVFrame* dequeue(int streamIndex, int timeoutMs);
  184. // 批量操作
  185. ErrorCode enqueueToAll(AVFrame* frame);
  186. std::vector<AVFrame*> dequeueFromAll();
  187. // 队列控制
  188. void clear();
  189. void clearStream(int streamIndex);
  190. void flush();
  191. void flushStream(int streamIndex);
  192. // 状态查询
  193. size_t size() const;
  194. size_t size(int streamIndex) const;
  195. bool empty() const;
  196. bool empty(int streamIndex) const;
  197. // 统计信息
  198. std::map<int, FrameQueueStats> getAllStats() const;
  199. FrameQueueStats getStats(int streamIndex) const;
  200. void resetStats();
  201. void resetStats(int streamIndex);
  202. // 同步控制
  203. void setBlocking(bool blocking);
  204. void wakeupAll();
  205. private:
  206. FrameQueueConfig defaultConfig_;
  207. std::map<int, std::unique_ptr<FrameQueue>> streamQueues_;
  208. mutable std::shared_mutex streamsMutex_;
  209. };
  210. // 帧队列工厂
  211. class FrameQueueFactory {
  212. public:
  213. // 创建标准帧队列
  214. static std::unique_ptr<FrameQueue> createStandardQueue(size_t maxSize = 100);
  215. // 创建低延迟队列
  216. static std::unique_ptr<FrameQueue> createLowLatencyQueue(size_t maxSize = 10);
  217. // 创建高容量队列
  218. static std::unique_ptr<FrameQueue> createHighCapacityQueue(size_t maxSize = 1000);
  219. // 创建实时队列(启用智能丢帧)
  220. static std::unique_ptr<FrameQueue> createRealtimeQueue(size_t maxSize = 50, double maxLatency = 50.0);
  221. // 创建多流队列
  222. static std::unique_ptr<MultiStreamFrameQueue> createMultiStreamQueue(const FrameQueueConfig& config = FrameQueueConfig());
  223. };
  224. } // namespace utils
  225. } // namespace av
  226. #endif // AV_UTILS_FRAME_QUEUE_H