| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #ifndef AV_CAPTURE_VIDEO_CAPTURER_H
- #define AV_CAPTURE_VIDEO_CAPTURER_H
- #include "capture_abstract_capturer.h"
- #include <thread>
- #include <condition_variable>
- #include <queue>
- extern "C" {
- #include <libavformat/avformat.h>
- #include <libavdevice/avdevice.h>
- #include <libswscale/swscale.h>
- }
- 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<std::pair<int, int>> supportedResolutions; // 支持的分辨率
- std::vector<int> supportedFps; // 支持的帧率
- std::vector<AVPixelFormat> 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<std::string> getAvailableDevices() const override;
- std::string getCurrentDevice() const override;
-
- /**
- * 获取详细设备信息
- * @return 设备信息列表
- */
- std::vector<VideoDeviceInfo> 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<VideoCapturer> createCamera(int cameraIndex = 0);
-
- /**
- * 创建屏幕录制采集器
- * @param screenIndex 屏幕索引
- * @return 采集器实例
- */
- static std::unique_ptr<VideoCapturer> createScreen(int screenIndex = 0);
-
- /**
- * 创建最佳摄像头采集器
- * @return 采集器实例
- */
- static std::unique_ptr<VideoCapturer> createBestCamera();
-
- /**
- * 创建窗口采集器
- * @param windowTitle 窗口标题
- * @return 采集器实例
- */
- static std::unique_ptr<VideoCapturer> createWindow(const std::string& windowTitle);
-
- /**
- * 创建窗口采集器(通过句柄)
- * @param windowHandle 窗口句柄
- * @return 采集器实例
- */
- static std::unique_ptr<VideoCapturer> 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<VideoDeviceInfo> enumerateCameras() const;
- std::vector<VideoDeviceInfo> enumerateScreens() const;
- std::vector<VideoDeviceInfo> enumerateWindows() const;
-
- // 平台特定实现
- #ifdef _WIN32
- std::vector<VideoDeviceInfo> enumerateDirectShowDevices() const;
- std::vector<VideoDeviceInfo> enumerateWindowsWindows() const;
- ErrorCode setupDirectShowCamera();
- ErrorCode setupGDIScreenCapture();
- ErrorCode setupGDIWindowCapture();
- #elif defined(__linux__)
- std::vector<VideoDeviceInfo> enumerateV4L2Devices() const;
- ErrorCode setupV4L2Camera();
- ErrorCode setupX11ScreenCapture();
- #elif defined(__APPLE__)
- std::vector<VideoDeviceInfo> 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<bool> shouldStop_{false};
- std::condition_variable pauseCondition_;
- std::mutex pauseMutex_;
-
- // 帧缓冲
- std::queue<AVFramePtr> frameQueue_;
- std::mutex queueMutex_;
- std::condition_variable queueCondition_;
- static constexpr size_t MAX_QUEUE_SIZE = 10;
-
- // 设备信息缓存
- mutable std::vector<VideoDeviceInfo> cachedDevices_;
- mutable std::mutex deviceCacheMutex_;
- mutable bool devicesCached_ = false;
- };
- } // namespace capture
- } // namespace av
- #endif // AV_CAPTURE_VIDEO_CAPTURER_H
|