capture_abstract_capturer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #ifndef AV_CAPTURE_ABSTRACT_CAPTURER_H
  2. #define AV_CAPTURE_ABSTRACT_CAPTURER_H
  3. #include "../base/types.h"
  4. #include "../base/media_common.h"
  5. #include <memory>
  6. #include <functional>
  7. #include <mutex>
  8. #include <atomic>
  9. #include <string>
  10. #include <vector>
  11. extern "C" {
  12. #include <libavformat/avformat.h>
  13. #include <libavutil/frame.h>
  14. }
  15. namespace av {
  16. namespace capture {
  17. /**
  18. * 采集器状态
  19. */
  20. enum class CapturerState {
  21. IDLE, // 空闲
  22. INITIALIZED, // 已初始化
  23. STARTED, // 已启动
  24. STOPPED, // 已停止
  25. ERROR // 错误状态
  26. };
  27. /**
  28. * 采集器类型
  29. */
  30. enum class CapturerType {
  31. VIDEO_CAMERA, // 摄像头
  32. VIDEO_SCREEN, // 屏幕录制
  33. VIDEO_WINDOW, // 窗口采集
  34. AUDIO_MIC, // 麦克风
  35. AUDIO_SYSTEM, // 系统音频
  36. AUDIO_LOOPBACK // 音频回环
  37. };
  38. /**
  39. * 采集器参数基类
  40. */
  41. struct CapturerParams {
  42. CapturerType type;
  43. MediaType mediaType;
  44. std::string deviceId; // 设备ID
  45. std::string deviceName; // 设备名称
  46. bool autoStart = false; // 自动启动
  47. CapturerParams(CapturerType t, MediaType mt) : type(t), mediaType(mt) {}
  48. virtual ~CapturerParams() = default;
  49. };
  50. /**
  51. * 采集器统计信息
  52. */
  53. struct CapturerStats {
  54. uint64_t capturedFrames = 0; // 已采集帧数
  55. uint64_t droppedFrames = 0; // 丢弃帧数
  56. uint64_t totalBytes = 0; // 总字节数
  57. uint64_t errorCount = 0; // 错误次数
  58. double avgCaptureTime = 0.0; // 平均采集时间(ms)
  59. double fps = 0.0; // 实际帧率
  60. std::chrono::steady_clock::time_point startTime; // 开始时间
  61. };
  62. /**
  63. * 抽象采集器基类
  64. * 定义所有采集器的通用接口
  65. */
  66. class AbstractCapturer {
  67. public:
  68. /**
  69. * 帧回调函数类型
  70. */
  71. using FrameCallback = std::function<void(const AVFramePtr&)>;
  72. /**
  73. * 错误回调函数类型
  74. */
  75. using ErrorCallback = std::function<void(ErrorCode, const std::string&)>;
  76. public:
  77. AbstractCapturer();
  78. virtual ~AbstractCapturer();
  79. /**
  80. * 初始化采集器
  81. * @param params 采集器参数
  82. * @return 错误码
  83. */
  84. virtual ErrorCode initialize(const CapturerParams& params) = 0;
  85. /**
  86. * 启动采集
  87. * @return 错误码
  88. */
  89. virtual ErrorCode start() = 0;
  90. /**
  91. * 停止采集
  92. * @return 错误码
  93. */
  94. virtual ErrorCode stop() = 0;
  95. /**
  96. * 暂停采集
  97. * @return 错误码
  98. */
  99. virtual ErrorCode pause() = 0;
  100. /**
  101. * 恢复采集
  102. * @return 错误码
  103. */
  104. virtual ErrorCode resume() = 0;
  105. /**
  106. * 重置采集器
  107. * @return 错误码
  108. */
  109. virtual ErrorCode reset() = 0;
  110. /**
  111. * 关闭采集器
  112. * @return 错误码
  113. */
  114. virtual ErrorCode close() = 0;
  115. // 状态查询
  116. CapturerState getState() const { return state_; }
  117. bool isRunning() const { return state_ == CapturerState::STARTED; }
  118. bool isPaused() const { return paused_; }
  119. // 回调设置
  120. void setFrameCallback(const FrameCallback& callback) { frameCallback_ = callback; }
  121. void setErrorCallback(const ErrorCallback& callback) { errorCallback_ = callback; }
  122. // 统计信息
  123. virtual CapturerStats getStats() const;
  124. virtual void resetStats();
  125. // 设备信息
  126. virtual std::vector<std::string> getAvailableDevices() const = 0;
  127. virtual std::string getCurrentDevice() const = 0;
  128. // 参数验证
  129. virtual bool validateParams(const CapturerParams& params) = 0;
  130. protected:
  131. /**
  132. * 设置采集器状态
  133. * @param state 新状态
  134. */
  135. void setState(CapturerState state);
  136. /**
  137. * 触发帧回调
  138. * @param frame 采集到的帧
  139. */
  140. void onFrameCaptured(const AVFramePtr& frame);
  141. /**
  142. * 触发错误回调
  143. * @param error 错误码
  144. * @param message 错误消息
  145. */
  146. void onError(ErrorCode error, const std::string& message);
  147. /**
  148. * 更新统计信息
  149. * @param success 是否成功
  150. * @param captureTime 采集时间
  151. * @param dataSize 数据大小
  152. */
  153. void updateStats(bool success, double captureTime, size_t dataSize = 0);
  154. /**
  155. * 计算实际帧率
  156. */
  157. void calculateFPS();
  158. protected:
  159. // 状态管理
  160. std::atomic<CapturerState> state_{CapturerState::IDLE};
  161. std::atomic<bool> paused_{false};
  162. // 回调函数
  163. FrameCallback frameCallback_;
  164. ErrorCallback errorCallback_;
  165. // 统计信息
  166. mutable std::mutex statsMutex_;
  167. CapturerStats stats_;
  168. // 帧率计算
  169. std::chrono::steady_clock::time_point lastFrameTime_;
  170. std::chrono::steady_clock::time_point lastFpsCalcTime_;
  171. uint64_t frameCountForFps_ = 0;
  172. // 线程安全
  173. mutable std::mutex captureMutex_;
  174. };
  175. /**
  176. * 采集器工厂基类
  177. */
  178. class CapturerFactory {
  179. public:
  180. /**
  181. * 检查采集器是否支持
  182. * @param type 采集器类型
  183. * @return 是否支持
  184. */
  185. static bool isCapturerSupported(CapturerType type);
  186. /**
  187. * 获取可用的采集器类型
  188. * @return 采集器类型列表
  189. */
  190. static std::vector<CapturerType> getAvailableCapturerTypes();
  191. /**
  192. * 获取采集器类型名称
  193. * @param type 采集器类型
  194. * @return 类型名称
  195. */
  196. static std::string getCapturerTypeName(CapturerType type);
  197. };
  198. } // namespace capture
  199. } // namespace av
  200. #endif // AV_CAPTURE_ABSTRACT_CAPTURER_H