| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #ifndef AV_CAPTURE_ABSTRACT_CAPTURER_H
- #define AV_CAPTURE_ABSTRACT_CAPTURER_H
- #include "../base/types.h"
- #include "../base/media_common.h"
- #include <memory>
- #include <functional>
- #include <mutex>
- #include <atomic>
- #include <string>
- #include <vector>
- extern "C" {
- #include <libavformat/avformat.h>
- #include <libavutil/frame.h>
- }
- namespace av {
- namespace capture {
- /**
- * 采集器状态
- */
- enum class CapturerState {
- IDLE, // 空闲
- INITIALIZED, // 已初始化
- STARTED, // 已启动
- STOPPED, // 已停止
- ERROR // 错误状态
- };
- /**
- * 采集器类型
- */
- enum class CapturerType {
- VIDEO_CAMERA, // 摄像头
- VIDEO_SCREEN, // 屏幕录制
- AUDIO_MIC, // 麦克风
- AUDIO_SYSTEM, // 系统音频
- AUDIO_LOOPBACK // 音频回环
- };
- /**
- * 采集器参数基类
- */
- struct CapturerParams {
- CapturerType type;
- MediaType mediaType;
- std::string deviceId; // 设备ID
- std::string deviceName; // 设备名称
- bool autoStart = false; // 自动启动
-
- CapturerParams(CapturerType t, MediaType mt) : type(t), mediaType(mt) {}
- virtual ~CapturerParams() = default;
- };
- /**
- * 采集器统计信息
- */
- struct CapturerStats {
- uint64_t capturedFrames = 0; // 已采集帧数
- uint64_t droppedFrames = 0; // 丢弃帧数
- uint64_t totalBytes = 0; // 总字节数
- uint64_t errorCount = 0; // 错误次数
- double avgCaptureTime = 0.0; // 平均采集时间(ms)
- double fps = 0.0; // 实际帧率
- std::chrono::steady_clock::time_point startTime; // 开始时间
- };
- /**
- * 抽象采集器基类
- * 定义所有采集器的通用接口
- */
- class AbstractCapturer {
- public:
- /**
- * 帧回调函数类型
- */
- using FrameCallback = std::function<void(const AVFramePtr&)>;
-
- /**
- * 错误回调函数类型
- */
- using ErrorCallback = std::function<void(ErrorCode, const std::string&)>;
-
- public:
- AbstractCapturer();
- virtual ~AbstractCapturer();
-
- /**
- * 初始化采集器
- * @param params 采集器参数
- * @return 错误码
- */
- virtual ErrorCode initialize(const CapturerParams& params) = 0;
-
- /**
- * 启动采集
- * @return 错误码
- */
- virtual ErrorCode start() = 0;
-
- /**
- * 停止采集
- * @return 错误码
- */
- virtual ErrorCode stop() = 0;
-
- /**
- * 暂停采集
- * @return 错误码
- */
- virtual ErrorCode pause() = 0;
-
- /**
- * 恢复采集
- * @return 错误码
- */
- virtual ErrorCode resume() = 0;
-
- /**
- * 重置采集器
- * @return 错误码
- */
- virtual ErrorCode reset() = 0;
-
- /**
- * 关闭采集器
- * @return 错误码
- */
- virtual ErrorCode close() = 0;
-
- // 状态查询
- CapturerState getState() const { return state_; }
- bool isRunning() const { return state_ == CapturerState::STARTED; }
- bool isPaused() const { return paused_; }
-
- // 回调设置
- void setFrameCallback(const FrameCallback& callback) { frameCallback_ = callback; }
- void setErrorCallback(const ErrorCallback& callback) { errorCallback_ = callback; }
-
- // 统计信息
- virtual CapturerStats getStats() const;
- virtual void resetStats();
-
- // 设备信息
- virtual std::vector<std::string> getAvailableDevices() const = 0;
- virtual std::string getCurrentDevice() const = 0;
-
- // 参数验证
- virtual bool validateParams(const CapturerParams& params) = 0;
-
- protected:
- /**
- * 设置采集器状态
- * @param state 新状态
- */
- void setState(CapturerState state);
-
- /**
- * 触发帧回调
- * @param frame 采集到的帧
- */
- void onFrameCaptured(const AVFramePtr& frame);
-
- /**
- * 触发错误回调
- * @param error 错误码
- * @param message 错误消息
- */
- void onError(ErrorCode error, const std::string& message);
-
- /**
- * 更新统计信息
- * @param success 是否成功
- * @param captureTime 采集时间
- * @param dataSize 数据大小
- */
- void updateStats(bool success, double captureTime, size_t dataSize = 0);
-
- /**
- * 计算实际帧率
- */
- void calculateFPS();
-
- protected:
- // 状态管理
- std::atomic<CapturerState> state_{CapturerState::IDLE};
- std::atomic<bool> paused_{false};
-
- // 回调函数
- FrameCallback frameCallback_;
- ErrorCallback errorCallback_;
-
- // 统计信息
- mutable std::mutex statsMutex_;
- CapturerStats stats_;
-
- // 帧率计算
- std::chrono::steady_clock::time_point lastFrameTime_;
- std::chrono::steady_clock::time_point lastFpsCalcTime_;
- uint64_t frameCountForFps_ = 0;
-
- // 线程安全
- mutable std::mutex captureMutex_;
- };
- /**
- * 采集器工厂基类
- */
- class CapturerFactory {
- public:
- /**
- * 检查采集器是否支持
- * @param type 采集器类型
- * @return 是否支持
- */
- static bool isCapturerSupported(CapturerType type);
-
- /**
- * 获取可用的采集器类型
- * @return 采集器类型列表
- */
- static std::vector<CapturerType> getAvailableCapturerTypes();
-
- /**
- * 获取采集器类型名称
- * @param type 采集器类型
- * @return 类型名称
- */
- static std::string getCapturerTypeName(CapturerType type);
- };
- } // namespace capture
- } // namespace av
- #endif // AV_CAPTURE_ABSTRACT_CAPTURER_H
|