capture_abstract_capturer.h 5.4 KB

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