capture_video_capturer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. VideoCaptureParams(CapturerType type) : CapturerParams(type, MediaType::VIDEO) {}
  32. };
  33. /**
  34. * 设备信息
  35. */
  36. struct VideoDeviceInfo {
  37. std::string id; // 设备ID
  38. std::string name; // 设备名称
  39. std::string description; // 设备描述
  40. std::vector<std::pair<int, int>> supportedResolutions; // 支持的分辨率
  41. std::vector<int> supportedFps; // 支持的帧率
  42. std::vector<AVPixelFormat> supportedFormats; // 支持的像素格式
  43. };
  44. /**
  45. * 视频采集器类
  46. * 支持摄像头和屏幕录制
  47. */
  48. class VideoCapturer : public AbstractCapturer {
  49. public:
  50. VideoCapturer();
  51. virtual ~VideoCapturer();
  52. // 基础接口实现
  53. ErrorCode initialize(const CapturerParams& params) override;
  54. ErrorCode start() override;
  55. ErrorCode stop() override;
  56. ErrorCode pause() override;
  57. ErrorCode resume() override;
  58. ErrorCode reset() override;
  59. ErrorCode close() override;
  60. // 设备信息
  61. std::vector<std::string> getAvailableDevices() const override;
  62. std::string getCurrentDevice() const override;
  63. /**
  64. * 获取详细设备信息
  65. * @return 设备信息列表
  66. */
  67. std::vector<VideoDeviceInfo> getDetailedDeviceInfo() const;
  68. /**
  69. * 设置采集参数
  70. * @param width 宽度
  71. * @param height 高度
  72. * @param fps 帧率
  73. * @return 错误码
  74. */
  75. ErrorCode setVideoParams(int width, int height, int fps);
  76. /**
  77. * 设置像素格式
  78. * @param format 像素格式
  79. * @return 错误码
  80. */
  81. ErrorCode setPixelFormat(AVPixelFormat format);
  82. /**
  83. * 获取当前视频参数
  84. * @return 视频参数
  85. */
  86. VideoCaptureParams getCurrentParams() const;
  87. /**
  88. * 视频采集器工厂
  89. */
  90. class VideoCaptureFactory {
  91. public:
  92. /**
  93. * 创建摄像头采集器
  94. * @param cameraIndex 摄像头索引
  95. * @return 采集器实例
  96. */
  97. static std::unique_ptr<VideoCapturer> createCamera(int cameraIndex = 0);
  98. /**
  99. * 创建屏幕录制采集器
  100. * @param screenIndex 屏幕索引
  101. * @return 采集器实例
  102. */
  103. static std::unique_ptr<VideoCapturer> createScreen(int screenIndex = 0);
  104. /**
  105. * 创建最佳摄像头采集器
  106. * @return 采集器实例
  107. */
  108. static std::unique_ptr<VideoCapturer> createBestCamera();
  109. };
  110. protected:
  111. bool validateParams(const CapturerParams& params) override;
  112. private:
  113. // 内部实现方法
  114. ErrorCode initializeCamera();
  115. ErrorCode initializeScreen();
  116. ErrorCode openInputDevice();
  117. ErrorCode setupVideoParams();
  118. ErrorCode setupPixelFormatConversion();
  119. // 采集线程
  120. void captureThreadFunc();
  121. ErrorCode captureFrame();
  122. // 格式转换
  123. AVFramePtr convertPixelFormat(const AVFramePtr& srcFrame);
  124. void cleanupConverter();
  125. // 设备枚举
  126. std::vector<VideoDeviceInfo> enumerateCameras() const;
  127. std::vector<VideoDeviceInfo> enumerateScreens() const;
  128. // 平台特定实现
  129. #ifdef _WIN32
  130. std::vector<VideoDeviceInfo> enumerateDirectShowDevices() const;
  131. ErrorCode setupDirectShowCamera();
  132. ErrorCode setupGDIScreenCapture();
  133. #elif defined(__linux__)
  134. std::vector<VideoDeviceInfo> enumerateV4L2Devices() const;
  135. ErrorCode setupV4L2Camera();
  136. ErrorCode setupX11ScreenCapture();
  137. #elif defined(__APPLE__)
  138. std::vector<VideoDeviceInfo> enumerateAVFoundationDevices() const;
  139. ErrorCode setupAVFoundationCamera();
  140. ErrorCode setupCoreGraphicsScreenCapture();
  141. #endif
  142. // 获取平台特定的输入格式
  143. const AVInputFormat* getPlatformInputFormat() const;
  144. std::string getPlatformDeviceName() const;
  145. private:
  146. // 采集参数
  147. VideoCaptureParams videoParams_;
  148. // FFmpeg 相关
  149. AVFormatContext* formatCtx_ = nullptr;
  150. AVCodecContext* codecCtx_ = nullptr;
  151. const AVCodec* codec_ = nullptr;
  152. int videoStreamIndex_ = -1;
  153. // 像素格式转换
  154. SwsContext* swsCtx_ = nullptr;
  155. AVFramePtr convertedFrame_;
  156. bool needConversion_ = false;
  157. // 采集线程
  158. std::thread captureThread_;
  159. std::atomic<bool> shouldStop_{false};
  160. std::condition_variable pauseCondition_;
  161. std::mutex pauseMutex_;
  162. // 帧缓冲
  163. std::queue<AVFramePtr> frameQueue_;
  164. std::mutex queueMutex_;
  165. std::condition_variable queueCondition_;
  166. static constexpr size_t MAX_QUEUE_SIZE = 10;
  167. // 设备信息缓存
  168. mutable std::vector<VideoDeviceInfo> cachedDevices_;
  169. mutable std::mutex deviceCacheMutex_;
  170. mutable bool devicesCached_ = false;
  171. };
  172. } // namespace capture
  173. } // namespace av
  174. #endif // AV_CAPTURE_VIDEO_CAPTURER_H