#ifndef AV_CAPTURE_VIDEO_CAPTURER_H #define AV_CAPTURE_VIDEO_CAPTURER_H #include "capture_abstract_capturer.h" #include #include #include extern "C" { #include #include #include } namespace av { namespace capture { /** * 视频采集器参数 */ struct VideoCaptureParams : public CapturerParams { int width = 1920; // 视频宽度 int height = 1080; // 视频高度 int fps = 30; // 帧率 AVPixelFormat pixelFormat = AV_PIX_FMT_YUV420P; // 像素格式 bool useHardwareAccel = true; // 使用硬件加速 // 摄像头特定参数 int cameraIndex = 0; // 摄像头索引 std::string cameraFormat; // 摄像头格式 (如 "dshow", "v4l2") // 屏幕录制特定参数 int screenIndex = 0; // 屏幕索引 int offsetX = 0; // X偏移 int offsetY = 0; // Y偏移 bool captureCursor = true; // 捕获鼠标光标 // 窗口采集特定参数 std::string windowTitle; // 窗口标题 std::string windowClassName; // 窗口类名 void* windowHandle = nullptr; // 窗口句柄 (HWND on Windows) bool followWindow = true; // 跟随窗口移动 bool captureClientArea = true; // 只捕获客户区 VideoCaptureParams(CapturerType type) : CapturerParams(type, MediaType::VIDEO) {} }; /** * 设备信息 */ struct VideoDeviceInfo { std::string id; // 设备ID std::string name; // 设备名称 std::string description; // 设备描述 std::vector> supportedResolutions; // 支持的分辨率 std::vector supportedFps; // 支持的帧率 std::vector supportedFormats; // 支持的像素格式 }; /** * 视频采集器类 * 支持摄像头和屏幕录制 */ class VideoCapturer : public AbstractCapturer { public: VideoCapturer(); virtual ~VideoCapturer(); // 基础接口实现 ErrorCode initialize(const CapturerParams& params) override; ErrorCode start() override; ErrorCode stop() override; ErrorCode pause() override; ErrorCode resume() override; ErrorCode reset() override; ErrorCode close() override; // 设备信息 std::vector getAvailableDevices() const override; std::string getCurrentDevice() const override; /** * 获取详细设备信息 * @return 设备信息列表 */ std::vector getDetailedDeviceInfo() const; /** * 设置采集参数 * @param width 宽度 * @param height 高度 * @param fps 帧率 * @return 错误码 */ ErrorCode setVideoParams(int width, int height, int fps); /** * 设置像素格式 * @param format 像素格式 * @return 错误码 */ ErrorCode setPixelFormat(AVPixelFormat format); /** * 获取当前视频参数 * @return 视频参数 */ VideoCaptureParams getCurrentParams() const; /** * 视频采集器工厂 */ class VideoCaptureFactory { public: /** * 创建摄像头采集器 * @param cameraIndex 摄像头索引 * @return 采集器实例 */ static std::unique_ptr createCamera(int cameraIndex = 0); /** * 创建屏幕录制采集器 * @param screenIndex 屏幕索引 * @return 采集器实例 */ static std::unique_ptr createScreen(int screenIndex = 0); /** * 创建最佳摄像头采集器 * @return 采集器实例 */ static std::unique_ptr createBestCamera(); /** * 创建窗口采集器 * @param windowTitle 窗口标题 * @return 采集器实例 */ static std::unique_ptr createWindow(const std::string& windowTitle); /** * 创建窗口采集器(通过句柄) * @param windowHandle 窗口句柄 * @return 采集器实例 */ static std::unique_ptr createWindowByHandle(void* windowHandle); }; protected: bool validateParams(const CapturerParams& params) override; private: // 内部实现方法 ErrorCode initializeCamera(); ErrorCode initializeScreen(); ErrorCode initializeWindow(); ErrorCode openInputDevice(); ErrorCode setupVideoParams(); ErrorCode setupPixelFormatConversion(); // 采集线程 void captureThreadFunc(); ErrorCode captureFrame(); // 格式转换 AVFramePtr convertPixelFormat(const AVFramePtr& srcFrame); void cleanupConverter(); // 设备枚举 std::vector enumerateCameras() const; std::vector enumerateScreens() const; std::vector enumerateWindows() const; // 平台特定实现 #ifdef _WIN32 std::vector enumerateDirectShowDevices() const; std::vector enumerateWindowsWindows() const; ErrorCode setupDirectShowCamera(); ErrorCode setupGDIScreenCapture(); ErrorCode setupGDIWindowCapture(); #elif defined(__linux__) std::vector enumerateV4L2Devices() const; ErrorCode setupV4L2Camera(); ErrorCode setupX11ScreenCapture(); #elif defined(__APPLE__) std::vector enumerateAVFoundationDevices() const; ErrorCode setupAVFoundationCamera(); ErrorCode setupCoreGraphicsScreenCapture(); #endif // 获取平台特定的输入格式 const AVInputFormat* getPlatformInputFormat() const; std::string getPlatformDeviceName() const; private: // 采集参数 VideoCaptureParams videoParams_; // FFmpeg 相关 AVFormatContext* formatCtx_ = nullptr; AVCodecContext* codecCtx_ = nullptr; const AVCodec* codec_ = nullptr; int videoStreamIndex_ = -1; // 像素格式转换 SwsContext* swsCtx_ = nullptr; AVFramePtr convertedFrame_; bool needConversion_ = false; // 采集线程 std::thread captureThread_; std::atomic shouldStop_{false}; std::condition_variable pauseCondition_; std::mutex pauseMutex_; // 帧缓冲 std::queue frameQueue_; std::mutex queueMutex_; std::condition_variable queueCondition_; static constexpr size_t MAX_QUEUE_SIZE = 10; // 设备信息缓存 mutable std::vector cachedDevices_; mutable std::mutex deviceCacheMutex_; mutable bool devicesCached_ = false; }; } // namespace capture } // namespace av #endif // AV_CAPTURE_VIDEO_CAPTURER_H