capture_video_capturer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef AV_CAPTURE_VIDEO_CAPTURER_H
  2. #define AV_CAPTURE_VIDEO_CAPTURER_H
  3. #include "capture_abstract_capturer.h"
  4. #include <thread>
  5. #include <condition_variable>
  6. #include <queue>
  7. extern "C" {
  8. #include <libavformat/avformat.h>
  9. #include <libavdevice/avdevice.h>
  10. #include <libswscale/swscale.h>
  11. }
  12. namespace av {
  13. namespace capture {
  14. /**
  15. * 视频采集器参数
  16. */
  17. struct VideoCaptureParams : public CapturerParams {
  18. int width = 1920; // 视频宽度
  19. int height = 1080; // 视频高度
  20. int fps = 30; // 帧率
  21. AVPixelFormat pixelFormat = AV_PIX_FMT_YUV420P; // 像素格式
  22. bool useHardwareAccel = true; // 使用硬件加速
  23. // 摄像头特定参数
  24. int cameraIndex = 0; // 摄像头索引
  25. std::string cameraFormat; // 摄像头格式 (如 "dshow", "v4l2")
  26. // 屏幕录制特定参数
  27. int screenIndex = 0; // 屏幕索引
  28. int offsetX = 0; // X偏移
  29. int offsetY = 0; // Y偏移
  30. bool captureCursor = true; // 捕获鼠标光标
  31. // 窗口采集特定参数
  32. std::string windowTitle; // 窗口标题
  33. std::string windowClassName; // 窗口类名
  34. void* windowHandle = nullptr; // 窗口句柄 (HWND on Windows)
  35. bool followWindow = true; // 跟随窗口移动
  36. bool captureClientArea = true; // 只捕获客户区
  37. VideoCaptureParams(CapturerType type) : CapturerParams(type, MediaType::VIDEO) {}
  38. };
  39. /**
  40. * 设备信息
  41. */
  42. struct VideoDeviceInfo {
  43. std::string id; // 设备ID
  44. std::string name; // 设备名称
  45. std::string description; // 设备描述
  46. std::vector<std::pair<int, int>> supportedResolutions; // 支持的分辨率
  47. std::vector<int> supportedFps; // 支持的帧率
  48. std::vector<AVPixelFormat> supportedFormats; // 支持的像素格式
  49. };
  50. /**
  51. * 视频采集器类
  52. * 支持摄像头和屏幕录制
  53. */
  54. class VideoCapturer : public AbstractCapturer {
  55. public:
  56. VideoCapturer();
  57. virtual ~VideoCapturer();
  58. // 基础接口实现
  59. ErrorCode initialize(const CapturerParams& params) override;
  60. ErrorCode start() override;
  61. ErrorCode stop() override;
  62. ErrorCode pause() override;
  63. ErrorCode resume() override;
  64. ErrorCode reset() override;
  65. ErrorCode close() override;
  66. // 设备信息
  67. std::vector<std::string> getAvailableDevices() const override;
  68. std::string getCurrentDevice() const override;
  69. /**
  70. * 获取详细设备信息
  71. * @return 设备信息列表
  72. */
  73. std::vector<VideoDeviceInfo> getDetailedDeviceInfo() const;
  74. /**
  75. * 设置采集参数
  76. * @param width 宽度
  77. * @param height 高度
  78. * @param fps 帧率
  79. * @return 错误码
  80. */
  81. ErrorCode setVideoParams(int width, int height, int fps);
  82. /**
  83. * 设置像素格式
  84. * @param format 像素格式
  85. * @return 错误码
  86. */
  87. ErrorCode setPixelFormat(AVPixelFormat format);
  88. /**
  89. * 获取当前视频参数
  90. * @return 视频参数
  91. */
  92. VideoCaptureParams getCurrentParams() const;
  93. /**
  94. * 视频采集器工厂
  95. */
  96. class VideoCaptureFactory {
  97. public:
  98. /**
  99. * 创建摄像头采集器
  100. * @param cameraIndex 摄像头索引
  101. * @return 采集器实例
  102. */
  103. static std::unique_ptr<VideoCapturer> createCamera(int cameraIndex = 0);
  104. /**
  105. * 创建屏幕录制采集器
  106. * @param screenIndex 屏幕索引
  107. * @return 采集器实例
  108. */
  109. static std::unique_ptr<VideoCapturer> createScreen(int screenIndex = 0);
  110. /**
  111. * 创建最佳摄像头采集器
  112. * @return 采集器实例
  113. */
  114. static std::unique_ptr<VideoCapturer> createBestCamera();
  115. /**
  116. * 创建窗口采集器
  117. * @param windowTitle 窗口标题
  118. * @return 采集器实例
  119. */
  120. static std::unique_ptr<VideoCapturer> createWindow(const std::string& windowTitle);
  121. /**
  122. * 创建窗口采集器(通过句柄)
  123. * @param windowHandle 窗口句柄
  124. * @return 采集器实例
  125. */
  126. static std::unique_ptr<VideoCapturer> createWindowByHandle(void* windowHandle);
  127. };
  128. protected:
  129. bool validateParams(const CapturerParams& params) override;
  130. private:
  131. // 内部实现方法
  132. ErrorCode initializeCamera();
  133. ErrorCode initializeScreen();
  134. ErrorCode initializeWindow();
  135. ErrorCode openInputDevice();
  136. ErrorCode setupVideoParams();
  137. ErrorCode setupPixelFormatConversion();
  138. // 采集线程
  139. void captureThreadFunc();
  140. ErrorCode captureFrame();
  141. // 格式转换
  142. AVFramePtr convertPixelFormat(const AVFramePtr& srcFrame);
  143. void cleanupConverter();
  144. // 设备枚举
  145. std::vector<VideoDeviceInfo> enumerateCameras() const;
  146. std::vector<VideoDeviceInfo> enumerateScreens() const;
  147. std::vector<VideoDeviceInfo> enumerateWindows() const;
  148. // 平台特定实现
  149. #ifdef _WIN32
  150. std::vector<VideoDeviceInfo> enumerateDirectShowDevices() const;
  151. std::vector<VideoDeviceInfo> enumerateWindowsWindows() const;
  152. ErrorCode setupDirectShowCamera();
  153. ErrorCode setupGDIScreenCapture();
  154. ErrorCode setupGDIWindowCapture();
  155. #elif defined(__linux__)
  156. std::vector<VideoDeviceInfo> enumerateV4L2Devices() const;
  157. ErrorCode setupV4L2Camera();
  158. ErrorCode setupX11ScreenCapture();
  159. #elif defined(__APPLE__)
  160. std::vector<VideoDeviceInfo> enumerateAVFoundationDevices() const;
  161. ErrorCode setupAVFoundationCamera();
  162. ErrorCode setupCoreGraphicsScreenCapture();
  163. #endif
  164. // 获取平台特定的输入格式
  165. const AVInputFormat* getPlatformInputFormat() const;
  166. std::string getPlatformDeviceName() const;
  167. private:
  168. // 采集参数
  169. VideoCaptureParams videoParams_;
  170. // FFmpeg 相关
  171. AVFormatContext* formatCtx_ = nullptr;
  172. AVCodecContext* codecCtx_ = nullptr;
  173. const AVCodec* codec_ = nullptr;
  174. int videoStreamIndex_ = -1;
  175. // 像素格式转换
  176. SwsContext* swsCtx_ = nullptr;
  177. AVFramePtr convertedFrame_;
  178. bool needConversion_ = false;
  179. // 采集线程
  180. std::thread captureThread_;
  181. std::atomic<bool> shouldStop_{false};
  182. std::condition_variable pauseCondition_;
  183. std::mutex pauseMutex_;
  184. // 帧缓冲
  185. std::queue<AVFramePtr> frameQueue_;
  186. std::mutex queueMutex_;
  187. std::condition_variable queueCondition_;
  188. static constexpr size_t MAX_QUEUE_SIZE = 10;
  189. // 设备信息缓存
  190. mutable std::vector<VideoDeviceInfo> cachedDevices_;
  191. mutable std::mutex deviceCacheMutex_;
  192. mutable bool devicesCached_ = false;
  193. };
  194. } // namespace capture
  195. } // namespace av
  196. #endif // AV_CAPTURE_VIDEO_CAPTURER_H