WgcCapturer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. return _app->StartCaptureWindow(target.hwnd, width, height);
  50. } else if (target.type == CaptureTargetType::Monitor) {
  51. auto monitors = MonitorFinder::GetList();
  52. if (target.monitorIdx < 0 || target.monitorIdx >= (int)monitors.size()) return false;
  53. auto monitorInfo = MonitorFinder::GetList()[target.monitorIdx];
  54. if (!monitorInfo.monitor) {
  55. qDebug() << "monitorInfo.monitor is nullptr!";
  56. return false;
  57. }
  58. if (width <= 0 || height <= 0) {
  59. qDebug() << "Invalid width/height:" << width << height;
  60. return false;
  61. }
  62. try {
  63. return _app->StartCaptureMonitor(monitorInfo.monitor, width, height);
  64. } catch (const winrt::hresult_error& e) {
  65. qDebug() << "WGC StartCaptureMonitor exception:"
  66. << QString::fromStdString(winrt::to_string(e.message()));
  67. return false;
  68. }
  69. }
  70. return false;
  71. #else
  72. return false;
  73. #endif
  74. }
  75. void WgcCapturer::close() {
  76. #ifdef PLATFORM_WINDOWS
  77. if (_app) _app->Close();
  78. #endif
  79. }
  80. AVFrame* WgcCapturer::getFrame() {
  81. #ifdef PLATFORM_WINDOWS
  82. return _app ? _app->GetFrame() : nullptr;
  83. #else
  84. return nullptr;
  85. #endif
  86. }
  87. void WgcCapturer::setDrawCursor(bool enable) {
  88. #ifdef PLATFORM_WINDOWS
  89. if (_app) _app->SetDrawCursor(enable);
  90. #endif
  91. }
  92. } // namespace video
  93. } // namespace avrecorder