7#include <QtCore/qloggingcategory.h>
8#include <QtCore/qscopeguard.h>
9#include <QtCore/qthread.h>
10#include <QtCore/private/qexpected_p.h>
11#include <QtCore/private/qfactorycacheregistration_p.h>
12#include <QtCore/private/qsystemerror_p.h>
13#include <QtGui/qguiapplication.h>
14#include <QtGui/qwindow.h>
15#include <QtGui/qpa/qplatformscreen_p.h>
16#include <QtMultimedia/qabstractvideobuffer.h>
17#include <QtMultimedia/qvideoframe.h>
18#include <QtMultimedia/private/qcapturablewindow_p.h>
19#include <QtMultimedia/private/qmultimediautils_p.h>
20#include <QtMultimedia/private/qvideoframe_p.h>
23#include <winrt/base.h>
28template <
typename Async>
29auto wait_for(Async
const& async, Windows::Foundation::TimeSpan
const& timeout);
31#include <winrt/Windows.Foundation.Collections.h>
32#include <winrt/Windows.Graphics.Capture.h>
33#include <winrt/Windows.Graphics.DirectX.h>
34#include <winrt/Windows.Graphics.DirectX.Direct3D11.h>
35#include <Windows.Graphics.Capture.h>
36#include <Windows.Graphics.Capture.Interop.h>
37#include <windows.graphics.directx.direct3d11.interop.h>
41#include <lowlevelmonitorconfigurationapi.h>
42#include <physicalmonitorenumerationapi.h>
45#include <system_error>
49using namespace Qt::StringLiterals;
51using namespace winrt::Windows::Graphics::Capture;
52using namespace winrt::Windows::Graphics::DirectX;
53using namespace winrt::Windows::Graphics::DirectX::Direct3D11;
54using namespace Windows::Graphics::DirectX::Direct3D11;
56using winrt::check_hresult;
62Q_LOGGING_CATEGORY(qLcWindowCaptureUwp,
"qt.multimedia.ffmpeg.windowcapture.uwp");
64winrt::Windows::Graphics::SizeInt32 getWindowSize(HWND hwnd)
67 ::GetWindowRect(hwnd, &windowRect);
69 return { windowRect.right - windowRect.left, windowRect.bottom - windowRect.top };
72QSize asQSize(winrt::Windows::Graphics::SizeInt32 size)
74 return { size.Width, size.Height };
77struct MultithreadedApartment
79 MultithreadedApartment(
const MultithreadedApartment &) =
delete;
80 MultithreadedApartment &operator=(
const MultithreadedApartment &) =
delete;
82 MultithreadedApartment() { winrt::init_apartment(); }
83 ~MultithreadedApartment() { winrt::uninit_apartment(); }
86class QUwpTextureVideoBuffer :
public QAbstractVideoBuffer
89 QUwpTextureVideoBuffer(com_ptr<IDXGISurface> &&surface) : m_surface(surface) { }
91 ~QUwpTextureVideoBuffer() override { Q_ASSERT(m_mapMode == QVideoFrame::NotMapped); }
93 MapData map(QVideoFrame::MapMode mode) override
95 if (m_mapMode != QVideoFrame::NotMapped)
98 if (mode == QVideoFrame::ReadOnly) {
99 DXGI_MAPPED_RECT rect = {};
100 HRESULT hr = m_surface->Map(&rect, DXGI_MAP_READ);
102 DXGI_SURFACE_DESC desc = {};
103 hr = m_surface->GetDesc(&desc);
107 md.bytesPerLine[0] = rect.Pitch;
108 md.data[0] = rect.pBits;
109 md.dataSize[0] = rect.Pitch * desc.Height;
111 m_mapMode = QVideoFrame::ReadOnly;
115 qCDebug(qLcWindowCaptureUwp)
116 <<
"Failed to map DXGI surface" << QSystemError::windowsComString(hr);
124 void unmap() override
126 if (m_mapMode == QVideoFrame::NotMapped)
129 const HRESULT hr = m_surface->Unmap();
131 qCDebug(qLcWindowCaptureUwp)
132 <<
"Failed to unmap surface" << QSystemError::windowsComString(hr);
134 m_mapMode = QVideoFrame::NotMapped;
137 QVideoFrameFormat format()
const override {
return {}; }
140 QVideoFrame::MapMode m_mapMode = QVideoFrame::NotMapped;
141 com_ptr<IDXGISurface> m_surface;
146 WindowGrabber() =
default;
148 WindowGrabber(IDXGIAdapter1 *adapter, HWND hwnd)
149 : m_captureWindow{ hwnd }, m_frameSize{ getWindowSize(hwnd) }
151 check_hresult(D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN,
nullptr, 0,
nullptr, 0,
152 D3D11_SDK_VERSION, m_device.put(),
nullptr,
nullptr));
154 const auto captureItem = createCaptureItem(hwnd);
156 m_framePool = Direct3D11CaptureFramePool::CreateFreeThreaded(
157 getCaptureDevice(m_device), m_pixelFormat, 1,
160 m_session = m_framePool.CreateCaptureSession(captureItem);
163 if (
const auto session2 = m_session.try_as<IGraphicsCaptureSession2>())
164 session2.IsCursorCaptureEnabled(
true);
167 if (
const auto session3 = m_session.try_as<IGraphicsCaptureSession3>())
168 session3.IsBorderRequired(
false);
170 m_session.StartCapture();
179 com_ptr<IDXGISurface> tryGetFrame()
181 const Direct3D11CaptureFrame frame = m_framePool.TryGetNextFrame();
188 if (!IsWindow(m_captureWindow))
189 throw std::runtime_error(
"Window was closed");
196 if (m_frameSize != frame.ContentSize()) {
197 m_frameSize = frame.ContentSize();
198 m_framePool.Recreate(getCaptureDevice(m_device), m_pixelFormat, 1, frame.ContentSize());
202 return copyTexture(m_device, frame.Surface());
206 static GraphicsCaptureItem createCaptureItem(HWND hwnd)
208 const auto factory = winrt::get_activation_factory<GraphicsCaptureItem>();
209 const auto interop = factory.as<IGraphicsCaptureItemInterop>();
211 GraphicsCaptureItem item = {
nullptr };
212 winrt::hresult status = S_OK;
217 constexpr int maxRetry = 10;
218 constexpr std::chrono::milliseconds retryDelay{ 100 };
219 for (
int retryNum = 0; retryNum < maxRetry; ++retryNum) {
221 status = interop->CreateForWindow(hwnd, winrt::guid_of<GraphicsCaptureItem>(),
222 winrt::put_abi(item));
224 if (status != E_INVALIDARG)
227 qCWarning(qLcWindowCaptureUwp)
228 <<
"Failed to create capture item:"
229 << QString::fromStdWString(winrt::hresult_error(status).message().c_str())
230 <<
"Retry number" << retryNum;
232 if (retryNum + 1 < maxRetry)
233 QThread::sleep(retryDelay);
237 check_hresult(status);
242 static IDirect3DDevice getCaptureDevice(
const com_ptr<ID3D11Device> &d3dDevice)
244 const auto dxgiDevice = d3dDevice.as<IDXGIDevice>();
246 com_ptr<IInspectable> device;
247 check_hresult(CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.get(), device.put()));
249 return device.as<IDirect3DDevice>();
252 static com_ptr<IDXGISurface> copyTexture(
const com_ptr<ID3D11Device> &device,
253 const IDirect3DSurface &capturedTexture)
255 const auto dxgiInterop{ capturedTexture.as<IDirect3DDxgiInterfaceAccess>() };
259 com_ptr<IDXGISurface> dxgiSurface;
260 check_hresult(dxgiInterop->GetInterface(guid_of<IDXGISurface>(), dxgiSurface.put_void()));
262 DXGI_SURFACE_DESC desc = {};
263 check_hresult(dxgiSurface->GetDesc(&desc));
265 D3D11_TEXTURE2D_DESC texDesc = {};
266 texDesc.Width = desc.Width;
267 texDesc.Height = desc.Height;
268 texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
269 texDesc.Usage = D3D11_USAGE_STAGING;
270 texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
271 texDesc.MiscFlags = 0;
272 texDesc.BindFlags = 0;
273 texDesc.ArraySize = 1;
274 texDesc.MipLevels = 1;
275 texDesc.SampleDesc = { 1, 0 };
277 com_ptr<ID3D11Texture2D> texture;
278 check_hresult(device->CreateTexture2D(&texDesc,
nullptr, texture.put()));
280 com_ptr<ID3D11DeviceContext> ctx;
281 device->GetImmediateContext(ctx.put());
282 ctx->CopyResource(texture.get(), dxgiSurface.as<ID3D11Resource>().get());
284 return texture.as<IDXGISurface>();
287 MultithreadedApartment m_comApartment{};
288 HWND m_captureWindow{};
289 winrt::Windows::Graphics::SizeInt32 m_frameSize{};
290 com_ptr<ID3D11Device> m_device;
291 Direct3D11CaptureFramePool m_framePool{
nullptr };
292 GraphicsCaptureSession m_session{
nullptr };
293 const DirectXPixelFormat m_pixelFormat = DirectXPixelFormat::R8G8B8A8UIntNormalized;
301 Grabber(QFFmpegWindowCaptureUwp &capture, HWND hwnd)
307 const HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL);
308 m_adapter = getAdapter(monitor);
310 addFrameCallback(&capture, &QFFmpegWindowCaptureUwp::newVideoFrame);
311 connect(
this, &Grabber::errorUpdated, &capture, &QFFmpegWindowCaptureUwp::updateError);
313 const auto refreshRate = getMonitorRefreshRateHz(monitor);
314 const auto defaultRate = refreshRate ? qMin(*refreshRate, DefaultScreenCaptureFrameRate)
315 : DefaultScreenCaptureFrameRate;
316 setFrameRate(capture.frameRate().value_or(defaultRate));
317 m_format.setStreamFrameRate(frameRate());
328 if (!m_adapter || !IsWindow(m_hwnd))
332 m_windowGrabber = std::make_unique<WindowGrabber>(m_adapter.get(), m_hwnd);
335 }
catch (
const winrt::hresult_error &err) {
337 const QString message = QLatin1String(
"Unable to capture window: ")
338 + QString::fromWCharArray(err.message().c_str());
340 updateError(InternalError, message);
347 m_windowGrabber =
nullptr;
353 com_ptr<IDXGISurface> texture = m_windowGrabber->tryGetFrame();
357 const QSize size = getTextureSize(texture);
359 m_format.setFrameSize(size);
361 if (m_format.streamFrameRate() != frameRate())
362 m_format.setStreamFrameRate(frameRate());
364 return QVideoFramePrivate::createFrame(
365 std::make_unique<QUwpTextureVideoBuffer>(std::move(texture)), m_format);
367 }
catch (
const winrt::hresult_error &err) {
369 const QString message = QLatin1String(
"Window capture failed: ")
370 + QString::fromWCharArray(err.message().c_str());
372 updateError(InternalError, message);
373 }
catch (
const std::runtime_error& e) {
374 updateError(CaptureFailed, QString::fromLatin1(e.what()));
381 static com_ptr<IDXGIAdapter1> getAdapter(HMONITOR handle)
383 com_ptr<IDXGIFactory1> factory;
384 check_hresult(CreateDXGIFactory1(guid_of<IDXGIFactory1>(), factory.put_void()));
386 com_ptr<IDXGIAdapter1> adapter;
387 for (quint32 i = 0; factory->EnumAdapters1(i, adapter.put()) == S_OK; adapter =
nullptr, i++) {
388 com_ptr<IDXGIOutput> output;
389 for (quint32 j = 0; adapter->EnumOutputs(j, output.put()) == S_OK; output =
nullptr, j++) {
390 DXGI_OUTPUT_DESC desc = {};
391 HRESULT hr = output->GetDesc(&desc);
392 if (hr == S_OK && desc.Monitor == handle)
399 static QSize getTextureSize(
const com_ptr<IDXGISurface> &surf)
404 DXGI_SURFACE_DESC desc;
405 check_hresult(surf->GetDesc(&desc));
407 return {
static_cast<
int>(desc.Width),
static_cast<
int>(desc.Height) };
410 static q23::expected<qreal, QString> getMonitorRefreshRateHz(HMONITOR handle)
413 return q23::unexpected{ u"No monitor"_s };
416 if (GetNumberOfPhysicalMonitorsFromHMONITOR(handle, &count)) {
417 std::vector<PHYSICAL_MONITOR> monitors{ count };
418 if (GetPhysicalMonitorsFromHMONITOR(handle, count, monitors.data())) {
419 const auto cleanup = qScopeGuard([count, &monitors] {
420 DestroyPhysicalMonitors(count, monitors.data());
422 for (
const auto &monitor : std::as_const(monitors)) {
423 MC_TIMING_REPORT screenTiming = {};
424 if (GetTimingReport(monitor.hPhysicalMonitor, &screenTiming)) {
428 return static_cast<qreal>(screenTiming.dwVerticalFrequencyInHZ) / 100.0;
433 return q23::unexpected{ u"Couldn't get monitor refresh rate"_s };
437 com_ptr<IDXGIAdapter1> m_adapter{};
438 std::unique_ptr<WindowGrabber> m_windowGrabber;
439 QVideoFrameFormat m_format;
442QFFmpegWindowCaptureUwp::QFFmpegWindowCaptureUwp() : QPlatformSurfaceCapture(WindowSource{})
444 qCDebug(qLcWindowCaptureUwp) <<
"Creating UWP screen capture";
447QFFmpegWindowCaptureUwp::~QFFmpegWindowCaptureUwp() =
default;
452 return u"Invalid window handle"_s;
454 if (hwnd == GetShellWindow())
455 return u"Cannot capture the shell window"_s;
457 wchar_t className[MAX_PATH] = {};
458 GetClassName(hwnd, className, MAX_PATH);
459 if (QString::fromWCharArray(className).length() == 0)
460 return u"Cannot capture windows without a class name"_s;
462 if (!IsWindowVisible(hwnd))
463 return u"Cannot capture invisible windows"_s;
465 if (GetAncestor(hwnd, GA_ROOT) != hwnd)
466 return u"Can only capture root windows"_s;
468 const LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
469 if (style & WS_DISABLED)
470 return u"Cannot capture disabled windows"_s;
472 const LONG_PTR exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
473 if (exStyle & WS_EX_TOOLWINDOW)
474 return u"No tooltips"_s;
476 DWORD cloaked = FALSE;
477 const HRESULT hr = DwmGetWindowAttribute(hwnd, DWMWA_CLOAKED, &cloaked,
sizeof(cloaked));
478 if (SUCCEEDED(hr) && cloaked == DWM_CLOAKED_SHELL)
479 return u"Cannot capture cloaked windows"_s;
484bool QFFmpegWindowCaptureUwp::setActiveInternal(
bool active)
486 if (
static_cast<
bool>(m_grabber) == active)
494 const auto window = source<WindowSource>();
495 const auto handle = QCapturableWindowPrivate::handle(window);
497 const auto hwnd =
reinterpret_cast<HWND>(handle ? handle->id : 0);
498 if (
const QString error = isCapturableWindow(hwnd); !error.isEmpty()) {
499 updateError(InternalError, error);
503 m_grabber = std::make_unique<Grabber>(*
this, hwnd);
509bool QFFmpegWindowCaptureUwp::isSupported()
511 return GraphicsCaptureSession::IsSupported();
514QVideoFrameFormat QFFmpegWindowCaptureUwp::frameFormat()
const
517 return m_grabber->frameFormat();
virtual void initializeGrabbingContext()
virtual void finalizeGrabbingContext()
QVideoFrameFormat frameFormat() const
QVideoFrame grabFrame() override
Grabber(QFFmpegWindowCaptureUwp &capture, HWND hwnd)
void finalizeGrabbingContext() override
void initializeGrabbingContext() override
auto wait_for(Async const &async, Windows::Foundation::TimeSpan const &timeout)
Windows::Foundation::AsyncStatus AsyncStatus
static QString isCapturableWindow(HWND hwnd)