WgcCapturer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "WgcCapturer.h"
  2. #include "qdebug.h"
  3. #include "qwidget.h"
  4. #ifdef _WIN32
  5. #include "wgc/winrt.h"
  6. #include <capturer/finder.h>
  7. #include <winrt/base.h>
  8. #endif
  9. namespace {
  10. QWidget* g_widget = nullptr;
  11. winrt::Windows::System::DispatcherQueueController g_controller{nullptr};
  12. winrt::Windows::UI::Composition::Compositor g_compositor{nullptr};
  13. winrt::Windows::UI::Composition::Desktop::DesktopWindowTarget g_target{nullptr};
  14. winrt::Windows::UI::Composition::ContainerVisual g_root{nullptr};
  15. } // namespace
  16. namespace avrecorder {
  17. namespace video {
  18. WgcCapturer::WgcCapturer()
  19. : _app(nullptr)
  20. {
  21. // _app = new App;
  22. }
  23. WgcCapturer::~WgcCapturer() { close(); if (_app) { delete _app; _app = nullptr; } }
  24. #ifdef _WIN32
  25. void InitWinRTCapture()
  26. {
  27. // 初始化 COM/WinRT
  28. winrt::init_apartment(winrt::apartment_type::single_threaded);
  29. // 初始化 DispatcherQueue
  30. // 创建调度队列
  31. g_controller = CreateDispatcherQueueController();
  32. // 初始化合成器
  33. g_compositor = winrt::Windows::UI::Composition::Compositor();
  34. g_widget = new QWidget;
  35. g_target = CreateDesktopWindowTarget(g_compositor, (HWND) g_widget->winId());
  36. g_root = g_compositor.CreateContainerVisual();
  37. g_root.RelativeSizeAdjustment({1.0f, 1.0f});
  38. g_target.Root(g_root);
  39. }
  40. #endif
  41. bool WgcCapturer::open(const CaptureTarget& target, int width, int height) {
  42. #ifdef PLATFORM_WINDOWS
  43. close();
  44. if (!_app) {
  45. _app = new App;
  46. _app->Initialize(g_root);
  47. }
  48. if (target.type == CaptureTargetType::Window) {
  49. qDebug() << "WgcCapturer::open: Starting window capture for HWND:" << target.hwnd;
  50. bool result = _app->StartCaptureWindow(target.hwnd, width, height);
  51. if (!result) {
  52. qDebug() << "WgcCapturer::open: StartCaptureWindow failed for HWND:" << target.hwnd;
  53. }
  54. return result;
  55. } else if (target.type == CaptureTargetType::Monitor) {
  56. auto monitors = MonitorFinder::GetList();
  57. if (target.monitorIdx < 0 || target.monitorIdx >= (int)monitors.size()) return false;
  58. auto monitorInfo = MonitorFinder::GetList()[target.monitorIdx];
  59. if (!monitorInfo.monitor) {
  60. qDebug() << "monitorInfo.monitor is nullptr!";
  61. return false;
  62. }
  63. if (width <= 0 || height <= 0) {
  64. qDebug() << "Invalid width/height:" << width << height;
  65. return false;
  66. }
  67. try {
  68. return _app->StartCaptureMonitor(monitorInfo.monitor, width, height);
  69. } catch (const winrt::hresult_error& e) {
  70. qDebug() << "WGC StartCaptureMonitor exception:"
  71. << QString::fromStdString(winrt::to_string(e.message()));
  72. return false;
  73. }
  74. }
  75. return false;
  76. #else
  77. return false;
  78. #endif
  79. }
  80. void WgcCapturer::close() {
  81. #ifdef PLATFORM_WINDOWS
  82. qDebug() << "WgcCapturer::close() - 开始关闭WGC捕获器";
  83. if (_app) {
  84. qDebug() << "WgcCapturer::close() - 调用App::Close()";
  85. _app->Close();
  86. qDebug() << "WgcCapturer::close() - App::Close()完成";
  87. }
  88. qDebug() << "WgcCapturer::close() - WGC捕获器关闭完成";
  89. #endif
  90. }
  91. AVFrame* WgcCapturer::getFrame() {
  92. #ifdef PLATFORM_WINDOWS
  93. return _app ? _app->GetFrame() : nullptr;
  94. #else
  95. return nullptr;
  96. #endif
  97. }
  98. void WgcCapturer::setDrawCursor(bool enable) {
  99. #ifdef PLATFORM_WINDOWS
  100. if (_app) _app->SetDrawCursor(enable);
  101. #endif
  102. }
  103. } // namespace video
  104. } // namespace avrecorder