capture_video_capturer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. #include "capture_video_capturer.h"
  2. #include "../base/logger.h"
  3. #include "../base/media_common.h"
  4. #include <algorithm>
  5. #include <sstream>
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. #include <dshow.h>
  9. #pragma comment(lib, "strmiids.lib")
  10. #endif
  11. extern "C" {
  12. #include <libavformat/avformat.h>
  13. #include <libavdevice/avdevice.h>
  14. #include <libswscale/swscale.h>
  15. #include <libavutil/imgutils.h>
  16. #include <libavutil/opt.h>
  17. }
  18. namespace av {
  19. namespace capture {
  20. VideoCapturer::VideoCapturer() : videoParams_(CapturerType::VIDEO_CAMERA) {
  21. AV_LOGGER_DEBUG("创建视频采集器");
  22. // 注册设备
  23. avdevice_register_all();
  24. }
  25. VideoCapturer::~VideoCapturer() {
  26. close();
  27. AV_LOGGER_DEBUG("视频采集器已销毁");
  28. }
  29. ErrorCode VideoCapturer::initialize(const CapturerParams& params) {
  30. if (params.mediaType != MediaType::VIDEO) {
  31. AV_LOGGER_ERROR("参数媒体类型不是视频");
  32. return ErrorCode::INVALID_PARAMS;
  33. }
  34. videoParams_ = static_cast<const VideoCaptureParams&>(params);
  35. if (!validateParams(videoParams_)) {
  36. return ErrorCode::INVALID_PARAMS;
  37. }
  38. ErrorCode result = ErrorCode::SUCCESS;
  39. if (videoParams_.type == CapturerType::VIDEO_CAMERA) {
  40. result = initializeCamera();
  41. } else if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  42. result = initializeScreen();
  43. } else if (videoParams_.type == CapturerType::VIDEO_WINDOW) {
  44. result = initializeWindow();
  45. } else {
  46. AV_LOGGER_ERROR("不支持的视频采集器类型");
  47. return ErrorCode::NOT_SUPPORTED;
  48. }
  49. if (result == ErrorCode::SUCCESS) {
  50. setState(CapturerState::INITIALIZED);
  51. AV_LOGGER_INFOF("视频采集器初始化成功: {}x{}@{}fps",
  52. videoParams_.width, videoParams_.height, videoParams_.fps);
  53. }
  54. return result;
  55. }
  56. ErrorCode VideoCapturer::start() {
  57. std::lock_guard<std::mutex> lock(captureMutex_);
  58. if (getState() != CapturerState::INITIALIZED) {
  59. AV_LOGGER_ERROR("采集器状态无效,无法启动");
  60. return ErrorCode::INVALID_STATE;
  61. }
  62. shouldStop_ = false;
  63. // 启动采集线程
  64. try {
  65. captureThread_ = std::thread(&VideoCapturer::captureThreadFunc, this);
  66. setState(CapturerState::STARTED);
  67. AV_LOGGER_INFO("视频采集已启动");
  68. return ErrorCode::SUCCESS;
  69. } catch (const std::exception& e) {
  70. AV_LOGGER_ERRORF("启动采集线程失败: {}", e.what());
  71. return ErrorCode::THREAD_ERROR;
  72. }
  73. }
  74. ErrorCode VideoCapturer::stop() {
  75. std::lock_guard<std::mutex> lock(captureMutex_);
  76. if (getState() != CapturerState::STARTED) {
  77. return ErrorCode::SUCCESS;
  78. }
  79. shouldStop_ = true;
  80. // 唤醒暂停的线程
  81. {
  82. std::lock_guard<std::mutex> pauseLock(pauseMutex_);
  83. paused_ = false;
  84. pauseCondition_.notify_all();
  85. }
  86. // 等待线程结束
  87. if (captureThread_.joinable()) {
  88. captureThread_.join();
  89. }
  90. setState(CapturerState::STOPPED);
  91. AV_LOGGER_INFO("视频采集已停止");
  92. return ErrorCode::SUCCESS;
  93. }
  94. ErrorCode VideoCapturer::pause() {
  95. if (getState() != CapturerState::STARTED) {
  96. return ErrorCode::INVALID_STATE;
  97. }
  98. paused_ = true;
  99. AV_LOGGER_INFO("视频采集已暂停");
  100. return ErrorCode::SUCCESS;
  101. }
  102. ErrorCode VideoCapturer::resume() {
  103. if (getState() != CapturerState::STARTED) {
  104. return ErrorCode::INVALID_STATE;
  105. }
  106. {
  107. std::lock_guard<std::mutex> lock(pauseMutex_);
  108. paused_ = false;
  109. pauseCondition_.notify_all();
  110. }
  111. AV_LOGGER_INFO("视频采集已恢复");
  112. return ErrorCode::SUCCESS;
  113. }
  114. ErrorCode VideoCapturer::reset() {
  115. ErrorCode result = stop();
  116. if (result != ErrorCode::SUCCESS) {
  117. return result;
  118. }
  119. // 清空帧队列
  120. {
  121. std::lock_guard<std::mutex> lock(queueMutex_);
  122. while (!frameQueue_.empty()) {
  123. frameQueue_.pop();
  124. }
  125. }
  126. resetStats();
  127. setState(CapturerState::INITIALIZED);
  128. AV_LOGGER_INFO("视频采集器已重置");
  129. return ErrorCode::SUCCESS;
  130. }
  131. ErrorCode VideoCapturer::close() {
  132. stop();
  133. // 清理资源
  134. cleanupConverter();
  135. if (codecCtx_) {
  136. avcodec_free_context(&codecCtx_);
  137. codecCtx_ = nullptr;
  138. }
  139. if (formatCtx_) {
  140. avformat_close_input(&formatCtx_);
  141. formatCtx_ = nullptr;
  142. }
  143. codec_ = nullptr;
  144. videoStreamIndex_ = -1;
  145. setState(CapturerState::IDLE);
  146. AV_LOGGER_INFO("视频采集器已关闭");
  147. return ErrorCode::SUCCESS;
  148. }
  149. std::vector<std::string> VideoCapturer::getAvailableDevices() const {
  150. std::vector<std::string> devices;
  151. auto deviceInfos = getDetailedDeviceInfo();
  152. for (const auto& info : deviceInfos) {
  153. devices.push_back(info.name);
  154. }
  155. return devices;
  156. }
  157. std::string VideoCapturer::getCurrentDevice() const {
  158. return videoParams_.deviceName;
  159. }
  160. std::vector<VideoDeviceInfo> VideoCapturer::getDetailedDeviceInfo() const {
  161. std::lock_guard<std::mutex> lock(deviceCacheMutex_);
  162. if (!devicesCached_) {
  163. if (videoParams_.type == CapturerType::VIDEO_CAMERA) {
  164. cachedDevices_ = enumerateCameras();
  165. } else if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  166. cachedDevices_ = enumerateScreens();
  167. } else if (videoParams_.type == CapturerType::VIDEO_WINDOW) {
  168. cachedDevices_ = enumerateWindows();
  169. }
  170. devicesCached_ = true;
  171. }
  172. return cachedDevices_;
  173. }
  174. ErrorCode VideoCapturer::setVideoParams(int width, int height, int fps) {
  175. if (getState() == CapturerState::STARTED) {
  176. AV_LOGGER_ERROR("无法在采集过程中修改参数");
  177. return ErrorCode::INVALID_STATE;
  178. }
  179. videoParams_.width = width;
  180. videoParams_.height = height;
  181. videoParams_.fps = fps;
  182. AV_LOGGER_INFOF("视频参数已更新: {}x{}@{}fps", width, height, fps);
  183. return ErrorCode::SUCCESS;
  184. }
  185. ErrorCode VideoCapturer::setPixelFormat(AVPixelFormat format) {
  186. if (getState() == CapturerState::STARTED) {
  187. AV_LOGGER_ERROR("无法在采集过程中修改像素格式");
  188. return ErrorCode::INVALID_STATE;
  189. }
  190. videoParams_.pixelFormat = format;
  191. AV_LOGGER_INFOF("像素格式已更新: {}", av_get_pix_fmt_name(format));
  192. return ErrorCode::SUCCESS;
  193. }
  194. VideoCaptureParams VideoCapturer::getCurrentParams() const {
  195. return videoParams_;
  196. }
  197. bool VideoCapturer::validateParams(const CapturerParams& params) {
  198. const auto& videoParams = static_cast<const VideoCaptureParams&>(params);
  199. if (videoParams.width <= 0 || videoParams.height <= 0) {
  200. AV_LOGGER_ERROR("视频分辨率无效");
  201. return false;
  202. }
  203. if (videoParams.fps <= 0 || videoParams.fps > 120) {
  204. AV_LOGGER_ERROR("帧率无效");
  205. return false;
  206. }
  207. if (videoParams.type == CapturerType::VIDEO_CAMERA) {
  208. if (videoParams.cameraIndex < 0) {
  209. AV_LOGGER_ERROR("摄像头索引无效");
  210. return false;
  211. }
  212. } else if (videoParams.type == CapturerType::VIDEO_SCREEN) {
  213. if (videoParams.screenIndex < 0) {
  214. AV_LOGGER_ERROR("屏幕索引无效");
  215. return false;
  216. }
  217. } else if (videoParams.type == CapturerType::VIDEO_WINDOW) {
  218. if (videoParams.windowTitle.empty() && !videoParams.windowHandle) {
  219. AV_LOGGER_ERROR("窗口标题和窗口句柄都为空");
  220. return false;
  221. }
  222. }
  223. return true;
  224. }
  225. ErrorCode VideoCapturer::initializeCamera() {
  226. AV_LOGGER_INFOF("初始化摄像头采集器: 索引={}", videoParams_.cameraIndex);
  227. #ifdef _WIN32
  228. return setupDirectShowCamera();
  229. #elif defined(__linux__)
  230. return setupV4L2Camera();
  231. #elif defined(__APPLE__)
  232. return setupAVFoundationCamera();
  233. #else
  234. AV_LOGGER_ERROR("当前平台不支持摄像头采集");
  235. return ErrorCode::NOT_SUPPORTED;
  236. #endif
  237. }
  238. ErrorCode VideoCapturer::initializeScreen() {
  239. AV_LOGGER_INFOF("初始化屏幕录制: 索引={}", videoParams_.screenIndex);
  240. #ifdef _WIN32
  241. return setupGDIScreenCapture();
  242. #elif defined(__linux__)
  243. return setupX11ScreenCapture();
  244. #elif defined(__APPLE__)
  245. return setupCoreGraphicsScreenCapture();
  246. #else
  247. AV_LOGGER_ERROR("当前平台不支持屏幕录制");
  248. return ErrorCode::NOT_SUPPORTED;
  249. #endif
  250. }
  251. ErrorCode VideoCapturer::initializeWindow() {
  252. AV_LOGGER_INFOF("初始化窗口采集: 标题={}", videoParams_.windowTitle);
  253. #ifdef _WIN32
  254. return setupGDIWindowCapture();
  255. #else
  256. AV_LOGGER_ERROR("当前平台不支持窗口采集");
  257. return ErrorCode::NOT_SUPPORTED;
  258. #endif
  259. }
  260. ErrorCode VideoCapturer::openInputDevice() {
  261. const AVInputFormat* inputFormat = getPlatformInputFormat();
  262. if (!inputFormat) {
  263. AV_LOGGER_ERROR("获取输入格式失败");
  264. return ErrorCode::NOT_SUPPORTED;
  265. }
  266. std::string deviceName = getPlatformDeviceName();
  267. if (deviceName.empty()) {
  268. AV_LOGGER_ERROR("获取设备名称失败");
  269. return ErrorCode::DEVICE_NOT_FOUND;
  270. }
  271. AV_LOGGER_INFOF("打开输入设备: {} (格式: {})", deviceName, inputFormat->name);
  272. // 设置输入选项
  273. AVDictionary* options = nullptr;
  274. // 设置视频参数
  275. av_dict_set(&options, "video_size",
  276. (std::to_string(videoParams_.width) + "x" + std::to_string(videoParams_.height)).c_str(), 0);
  277. av_dict_set(&options, "framerate", std::to_string(videoParams_.fps).c_str(), 0);
  278. if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  279. // 屏幕录制特定选项
  280. if (videoParams_.captureCursor) {
  281. av_dict_set(&options, "draw_mouse", "1", 0);
  282. }
  283. if (videoParams_.offsetX != 0 || videoParams_.offsetY != 0) {
  284. av_dict_set(&options, "offset_x", std::to_string(videoParams_.offsetX).c_str(), 0);
  285. av_dict_set(&options, "offset_y", std::to_string(videoParams_.offsetY).c_str(), 0);
  286. }
  287. } else if (videoParams_.type == CapturerType::VIDEO_WINDOW) {
  288. // 窗口采集特定选项
  289. if (videoParams_.captureCursor) {
  290. av_dict_set(&options, "draw_mouse", "1", 0);
  291. }
  292. if (videoParams_.offsetX != 0 || videoParams_.offsetY != 0) {
  293. av_dict_set(&options, "offset_x", std::to_string(videoParams_.offsetX).c_str(), 0);
  294. av_dict_set(&options, "offset_y", std::to_string(videoParams_.offsetY).c_str(), 0);
  295. }
  296. }
  297. // 打开输入
  298. int ret = avformat_open_input(&formatCtx_, deviceName.c_str(), inputFormat, &options);
  299. av_dict_free(&options);
  300. if (ret < 0) {
  301. AV_LOGGER_ERRORF("打开输入设备失败: {} (设备: {})",
  302. ffmpeg_utils::errorToString(ret), deviceName);
  303. return static_cast<ErrorCode>(ret);
  304. }
  305. // 查找流信息
  306. ret = avformat_find_stream_info(formatCtx_, nullptr);
  307. if (ret < 0) {
  308. AV_LOGGER_ERRORF("查找流信息失败: {}", ffmpeg_utils::errorToString(ret));
  309. return static_cast<ErrorCode>(ret);
  310. }
  311. // 查找视频流
  312. videoStreamIndex_ = av_find_best_stream(formatCtx_, AVMEDIA_TYPE_VIDEO, -1, -1, &codec_, 0);
  313. if (videoStreamIndex_ < 0) {
  314. AV_LOGGER_ERROR("未找到视频流");
  315. return ErrorCode::STREAM_NOT_FOUND;
  316. }
  317. // 创建解码上下文
  318. codecCtx_ = avcodec_alloc_context3(codec_);
  319. if (!codecCtx_) {
  320. AV_LOGGER_ERROR("分配解码上下文失败");
  321. return ErrorCode::MEMORY_ALLOC_FAILED;
  322. }
  323. // 复制流参数到解码上下文
  324. ret = avcodec_parameters_to_context(codecCtx_, formatCtx_->streams[videoStreamIndex_]->codecpar);
  325. if (ret < 0) {
  326. AV_LOGGER_ERRORF("复制流参数失败: {}", ffmpeg_utils::errorToString(ret));
  327. return static_cast<ErrorCode>(ret);
  328. }
  329. // 打开解码器
  330. ret = avcodec_open2(codecCtx_, codec_, nullptr);
  331. if (ret < 0) {
  332. AV_LOGGER_ERRORF("打开解码器失败: {}", ffmpeg_utils::errorToString(ret));
  333. return static_cast<ErrorCode>(ret);
  334. }
  335. // 设置像素格式转换
  336. return setupPixelFormatConversion();
  337. }
  338. ErrorCode VideoCapturer::setupPixelFormatConversion() {
  339. AVPixelFormat srcFormat = codecCtx_->pix_fmt;
  340. AVPixelFormat dstFormat = videoParams_.pixelFormat;
  341. needConversion_ = (srcFormat != dstFormat);
  342. if (needConversion_) {
  343. AV_LOGGER_INFOF("需要像素格式转换: {} -> {}",
  344. av_get_pix_fmt_name(srcFormat),
  345. av_get_pix_fmt_name(dstFormat));
  346. swsCtx_ = sws_getContext(
  347. codecCtx_->width, codecCtx_->height, srcFormat,
  348. videoParams_.width, videoParams_.height, dstFormat,
  349. SWS_BILINEAR, nullptr, nullptr, nullptr
  350. );
  351. if (!swsCtx_) {
  352. AV_LOGGER_ERROR("创建像素格式转换上下文失败");
  353. return ErrorCode::CONVERSION_FAILED;
  354. }
  355. // 创建转换后的帧
  356. convertedFrame_ = makeAVFrame();
  357. if (!convertedFrame_) {
  358. return ErrorCode::MEMORY_ALLOC_FAILED;
  359. }
  360. convertedFrame_->format = dstFormat;
  361. convertedFrame_->width = videoParams_.width;
  362. convertedFrame_->height = videoParams_.height;
  363. int ret = av_frame_get_buffer(convertedFrame_.get(), 32);
  364. if (ret < 0) {
  365. AV_LOGGER_ERRORF("分配转换帧缓冲区失败: {}", ffmpeg_utils::errorToString(ret));
  366. return static_cast<ErrorCode>(ret);
  367. }
  368. }
  369. return ErrorCode::SUCCESS;
  370. }
  371. void VideoCapturer::captureThreadFunc() {
  372. AV_LOGGER_INFO("视频采集线程已启动");
  373. while (!shouldStop_) {
  374. // 检查暂停状态
  375. {
  376. std::unique_lock<std::mutex> lock(pauseMutex_);
  377. pauseCondition_.wait(lock, [this] { return !paused_ || shouldStop_; });
  378. }
  379. if (shouldStop_) {
  380. break;
  381. }
  382. ErrorCode result = captureFrame();
  383. if (result != ErrorCode::SUCCESS) {
  384. onError(result, "采集帧失败");
  385. // 短暂休眠后重试
  386. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  387. }
  388. }
  389. AV_LOGGER_INFO("视频采集线程已退出");
  390. }
  391. ErrorCode VideoCapturer::captureFrame() {
  392. AVPacket* packet = av_packet_alloc();
  393. if (!packet) {
  394. return ErrorCode::MEMORY_ALLOC_FAILED;
  395. }
  396. // 读取包
  397. int ret = av_read_frame(formatCtx_, packet);
  398. if (ret < 0) {
  399. if (ret == AVERROR_EOF) {
  400. AV_LOGGER_WARNING("到达文件末尾");
  401. return ErrorCode::END_OF_STREAM;
  402. } else {
  403. AV_LOGGER_ERRORF("读取帧失败: {}", ffmpeg_utils::errorToString(ret));
  404. return static_cast<ErrorCode>(ret);
  405. }
  406. }
  407. // 检查是否是视频包
  408. if (packet->stream_index != videoStreamIndex_) {
  409. av_packet_free(&packet);
  410. return ErrorCode::SUCCESS;
  411. }
  412. // 发送包到解码器
  413. ret = avcodec_send_packet(codecCtx_, packet);
  414. av_packet_free(&packet);
  415. if (ret < 0) {
  416. AV_LOGGER_ERRORF("发送包到解码器失败: {}", ffmpeg_utils::errorToString(ret));
  417. return static_cast<ErrorCode>(ret);
  418. }
  419. // 接收解码后的帧
  420. AVFramePtr frame = makeAVFrame();
  421. if (!frame) {
  422. return ErrorCode::MEMORY_ALLOC_FAILED;
  423. }
  424. ret = avcodec_receive_frame(codecCtx_, frame.get());
  425. if (ret == AVERROR(EAGAIN)) {
  426. return ErrorCode::SUCCESS; // 需要更多输入
  427. } else if (ret < 0) {
  428. AV_LOGGER_ERRORF("接收解码帧失败: {}", ffmpeg_utils::errorToString(ret));
  429. return static_cast<ErrorCode>(ret);
  430. }
  431. // 像素格式转换
  432. AVFramePtr outputFrame;
  433. if (needConversion_) {
  434. outputFrame = convertPixelFormat(frame);
  435. if (!outputFrame) {
  436. return ErrorCode::CONVERSION_FAILED;
  437. }
  438. } else {
  439. outputFrame = std::move(frame);
  440. }
  441. // 添加到队列或直接回调
  442. onFrameCaptured(outputFrame);
  443. return ErrorCode::SUCCESS;
  444. }
  445. AVFramePtr VideoCapturer::convertPixelFormat(const AVFramePtr& srcFrame) {
  446. if (!srcFrame || !swsCtx_ || !convertedFrame_) {
  447. return nullptr;
  448. }
  449. // 执行像素格式转换
  450. int ret = sws_scale(swsCtx_,
  451. srcFrame->data, srcFrame->linesize, 0, srcFrame->height,
  452. convertedFrame_->data, convertedFrame_->linesize);
  453. if (ret < 0) {
  454. AV_LOGGER_ERRORF("像素格式转换失败: {}", ffmpeg_utils::errorToString(ret));
  455. return nullptr;
  456. }
  457. // 复制时间戳等信息
  458. av_frame_copy_props(convertedFrame_.get(), srcFrame.get());
  459. // 创建新的frame并复制数据
  460. AVFramePtr outputFrame = makeAVFrame();
  461. if (!outputFrame) {
  462. return nullptr;
  463. }
  464. av_frame_ref(outputFrame.get(), convertedFrame_.get());
  465. return outputFrame;
  466. }
  467. void VideoCapturer::cleanupConverter() {
  468. if (swsCtx_) {
  469. sws_freeContext(swsCtx_);
  470. swsCtx_ = nullptr;
  471. }
  472. convertedFrame_.reset();
  473. needConversion_ = false;
  474. }
  475. std::vector<VideoDeviceInfo> VideoCapturer::enumerateCameras() const {
  476. #ifdef _WIN32
  477. return enumerateDirectShowDevices();
  478. #elif defined(__linux__)
  479. return enumerateV4L2Devices();
  480. #elif defined(__APPLE__)
  481. return enumerateAVFoundationDevices();
  482. #else
  483. return {};
  484. #endif
  485. }
  486. std::vector<VideoDeviceInfo> VideoCapturer::enumerateScreens() const {
  487. std::vector<VideoDeviceInfo> screens;
  488. // 简单的屏幕枚举实现
  489. VideoDeviceInfo screen;
  490. screen.id = "desktop";
  491. screen.name = "桌面";
  492. screen.description = "主显示器";
  493. // 添加常见分辨率
  494. screen.supportedResolutions = {
  495. {1920, 1080}, {1680, 1050}, {1600, 900}, {1440, 900},
  496. {1366, 768}, {1280, 1024}, {1280, 800}, {1024, 768}
  497. };
  498. // 添加常见帧率
  499. screen.supportedFps = {15, 24, 30, 60};
  500. // 添加支持的像素格式
  501. screen.supportedFormats = {
  502. AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_YUV420P
  503. };
  504. screens.push_back(screen);
  505. return screens;
  506. }
  507. std::vector<VideoDeviceInfo> VideoCapturer::enumerateWindows() const {
  508. #ifdef _WIN32
  509. return enumerateWindowsWindows();
  510. #else
  511. return {};
  512. #endif
  513. }
  514. const AVInputFormat* VideoCapturer::getPlatformInputFormat() const {
  515. #ifdef _WIN32
  516. if (videoParams_.type == CapturerType::VIDEO_CAMERA) {
  517. return av_find_input_format("dshow");
  518. } else if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  519. return av_find_input_format("gdigrab");
  520. } else if (videoParams_.type == CapturerType::VIDEO_WINDOW) {
  521. return av_find_input_format("gdigrab");
  522. }
  523. #elif defined(__linux__)
  524. if (videoParams_.type == CapturerType::VIDEO_CAMERA) {
  525. return av_find_input_format("v4l2");
  526. } else if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  527. return av_find_input_format("x11grab");
  528. }
  529. #elif defined(__APPLE__)
  530. if (videoParams_.type == CapturerType::VIDEO_CAMERA) {
  531. return av_find_input_format("avfoundation");
  532. } else if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  533. return av_find_input_format("avfoundation");
  534. }
  535. #endif
  536. return nullptr;
  537. }
  538. std::string VideoCapturer::getPlatformDeviceName() const {
  539. #ifdef _WIN32
  540. if (videoParams_.type == CapturerType::VIDEO_CAMERA) {
  541. if (!videoParams_.deviceName.empty()) {
  542. return "video=" + videoParams_.deviceName;
  543. } else {
  544. return "video=" + std::to_string(videoParams_.cameraIndex);
  545. }
  546. } else if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  547. return "desktop";
  548. } else if (videoParams_.type == CapturerType::VIDEO_WINDOW) {
  549. if (!videoParams_.windowTitle.empty()) {
  550. return "title=" + videoParams_.windowTitle;
  551. } else if (videoParams_.windowHandle) {
  552. return "hwnd=" + std::to_string(reinterpret_cast<uintptr_t>(videoParams_.windowHandle));
  553. } else {
  554. return "desktop"; // 回退到桌面捕获
  555. }
  556. }
  557. #elif defined(__linux__)
  558. if (videoParams_.type == CapturerType::VIDEO_CAMERA) {
  559. if (!videoParams_.deviceName.empty()) {
  560. return videoParams_.deviceName;
  561. } else {
  562. return "/dev/video" + std::to_string(videoParams_.cameraIndex);
  563. }
  564. } else if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  565. return ":0.0";
  566. }
  567. #elif defined(__APPLE__)
  568. if (videoParams_.type == CapturerType::VIDEO_CAMERA) {
  569. return std::to_string(videoParams_.cameraIndex);
  570. } else if (videoParams_.type == CapturerType::VIDEO_SCREEN) {
  571. return "Capture screen 0";
  572. }
  573. #endif
  574. return "";
  575. }
  576. #ifdef _WIN32
  577. std::vector<VideoDeviceInfo> VideoCapturer::enumerateDirectShowDevices() const {
  578. std::vector<VideoDeviceInfo> devices;
  579. // 简化的DirectShow设备枚举
  580. // 实际实现需要使用COM接口
  581. VideoDeviceInfo device;
  582. device.id = "0";
  583. device.name = "默认摄像头";
  584. device.description = "DirectShow摄像头设备";
  585. // 添加常见分辨率
  586. device.supportedResolutions = {
  587. {1920, 1080}, {1280, 720}, {960, 540}, {640, 480}, {320, 240}
  588. };
  589. // 添加常见帧率
  590. device.supportedFps = {15, 24, 30, 60};
  591. // 添加支持的像素格式
  592. device.supportedFormats = {
  593. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24
  594. };
  595. devices.push_back(device);
  596. return devices;
  597. }
  598. ErrorCode VideoCapturer::setupDirectShowCamera() {
  599. AV_LOGGER_INFO("设置DirectShow摄像头");
  600. return openInputDevice();
  601. }
  602. ErrorCode VideoCapturer::setupGDIScreenCapture() {
  603. AV_LOGGER_INFO("设置GDI屏幕录制");
  604. return openInputDevice();
  605. }
  606. std::vector<VideoDeviceInfo> VideoCapturer::enumerateWindowsWindows() const {
  607. std::vector<VideoDeviceInfo> windows;
  608. // 简化的窗口枚举实现
  609. // 实际实现需要使用EnumWindows API
  610. VideoDeviceInfo window;
  611. window.id = "notepad";
  612. window.name = "记事本";
  613. window.description = "Windows记事本窗口";
  614. // 窗口采集支持的分辨率取决于窗口大小
  615. window.supportedResolutions = {
  616. {1920, 1080}, {1280, 720}, {800, 600}, {640, 480}
  617. };
  618. // 添加常见帧率
  619. window.supportedFps = {15, 24, 30, 60};
  620. // 添加支持的像素格式
  621. window.supportedFormats = {
  622. AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_YUV420P
  623. };
  624. windows.push_back(window);
  625. return windows;
  626. }
  627. ErrorCode VideoCapturer::setupGDIWindowCapture() {
  628. AV_LOGGER_INFOF("设置GDI窗口采集: {}", videoParams_.windowTitle);
  629. return openInputDevice();
  630. }
  631. #endif
  632. // VideoCaptureFactory 实现
  633. std::unique_ptr<VideoCapturer> VideoCapturer::VideoCaptureFactory::createCamera(int cameraIndex) {
  634. auto capturer = std::make_unique<VideoCapturer>();
  635. VideoCaptureParams params(CapturerType::VIDEO_CAMERA);
  636. params.cameraIndex = cameraIndex;
  637. ErrorCode result = capturer->initialize(params);
  638. if (result != ErrorCode::SUCCESS) {
  639. AV_LOGGER_ERRORF("创建摄像头采集器失败: {}", static_cast<int>(result));
  640. return nullptr;
  641. }
  642. return capturer;
  643. }
  644. std::unique_ptr<VideoCapturer> VideoCapturer::VideoCaptureFactory::createScreen(int screenIndex) {
  645. auto capturer = std::make_unique<VideoCapturer>();
  646. VideoCaptureParams params(CapturerType::VIDEO_SCREEN);
  647. params.screenIndex = screenIndex;
  648. ErrorCode result = capturer->initialize(params);
  649. if (result != ErrorCode::SUCCESS) {
  650. AV_LOGGER_ERRORF("创建屏幕录制采集器失败: {}", static_cast<int>(result));
  651. return nullptr;
  652. }
  653. return capturer;
  654. }
  655. std::unique_ptr<VideoCapturer> VideoCapturer::VideoCaptureFactory::createBestCamera() {
  656. return createCamera(0); // 默认使用第一个摄像头
  657. }
  658. std::unique_ptr<VideoCapturer> VideoCapturer::VideoCaptureFactory::createWindow(const std::string& windowTitle) {
  659. auto capturer = std::make_unique<VideoCapturer>();
  660. VideoCaptureParams params(CapturerType::VIDEO_WINDOW);
  661. params.windowTitle = windowTitle;
  662. ErrorCode result = capturer->initialize(params);
  663. if (result != ErrorCode::SUCCESS) {
  664. AV_LOGGER_ERRORF("创建窗口采集器失败: {}", static_cast<int>(result));
  665. return nullptr;
  666. }
  667. return capturer;
  668. }
  669. std::unique_ptr<VideoCapturer> VideoCapturer::VideoCaptureFactory::createWindowByHandle(void* windowHandle) {
  670. auto capturer = std::make_unique<VideoCapturer>();
  671. VideoCaptureParams params(CapturerType::VIDEO_WINDOW);
  672. params.windowHandle = windowHandle;
  673. ErrorCode result = capturer->initialize(params);
  674. if (result != ErrorCode::SUCCESS) {
  675. AV_LOGGER_ERRORF("创建窗口采集器失败: {}", static_cast<int>(result));
  676. return nullptr;
  677. }
  678. return capturer;
  679. }
  680. } // namespace capture
  681. } // namespace av