7#include <QtCore/qloggingcategory.h>
8#include <QtCore/qthread.h>
9#include <QtCore/private/qfactorycacheregistration_p.h>
10#include <QtCore/private/qsystemerror_p.h>
11#include <QtGui/qguiapplication.h>
12#include <QtGui/qwindow.h>
13#include <QtGui/qpa/qplatformscreen_p.h>
14#include <QtMultimedia/qabstractvideobuffer.h>
15#include <QtMultimedia/qvideoframe.h>
16#include <QtMultimedia/private/qcapturablewindow_p.h>
17#include <QtMultimedia/private/qmultimediautils_p.h>
18#include <QtMultimedia/private/qvideoframe_p.h>
21#include <winrt/base.h>
26template <
typename Async>
27auto wait_for(Async
const& async, Windows::Foundation::TimeSpan
const& timeout);
29#include <winrt/Windows.Foundation.Collections.h>
30#include <winrt/Windows.Graphics.Capture.h>
31#include <winrt/Windows.Graphics.DirectX.h>
32#include <winrt/Windows.Graphics.DirectX.Direct3D11.h>
33#include <Windows.Graphics.Capture.h>
34#include <Windows.Graphics.Capture.Interop.h>
35#include <windows.graphics.directx.direct3d11.interop.h>
39#include <lowlevelmonitorconfigurationapi.h>
40#include <physicalmonitorenumerationapi.h>
43#include <system_error>
47using namespace Qt::StringLiterals;
49using namespace winrt::Windows::Graphics::Capture;
50using namespace winrt::Windows::Graphics::DirectX;
51using namespace winrt::Windows::Graphics::DirectX::Direct3D11;
52using namespace Windows::Graphics::DirectX::Direct3D11;
54using winrt::check_hresult;
60Q_LOGGING_CATEGORY(qLcWindowCaptureUwp,
"qt.multimedia.ffmpeg.windowcapture.uwp");
62winrt::Windows::Graphics::SizeInt32 getWindowSize(HWND hwnd)
65 ::GetWindowRect(hwnd, &windowRect);
67 return { windowRect.right - windowRect.left, windowRect.bottom - windowRect.top };
70QSize asQSize(winrt::Windows::Graphics::SizeInt32 size)
72 return { size.Width, size.Height };
75struct MultithreadedApartment
77 MultithreadedApartment(
const MultithreadedApartment &) =
delete;
78 MultithreadedApartment &operator=(
const MultithreadedApartment &) =
delete;
80 MultithreadedApartment() { winrt::init_apartment(); }
81 ~MultithreadedApartment() { winrt::uninit_apartment(); }
84class QUwpTextureVideoBuffer :
public QAbstractVideoBuffer
87 QUwpTextureVideoBuffer(com_ptr<IDXGISurface> &&surface) : m_surface(surface) { }
89 ~QUwpTextureVideoBuffer() override { Q_ASSERT(m_mapMode == QVideoFrame::NotMapped); }
91 MapData map(QVideoFrame::MapMode mode) override
93 if (m_mapMode != QVideoFrame::NotMapped)
96 if (mode == QVideoFrame::ReadOnly) {
97 DXGI_MAPPED_RECT rect = {};
98 HRESULT hr = m_surface->Map(&rect, DXGI_MAP_READ);
100 DXGI_SURFACE_DESC desc = {};
101 hr = m_surface->GetDesc(&desc);
105 md.bytesPerLine[0] = rect.Pitch;
106 md.data[0] = rect.pBits;
107 md.dataSize[0] = rect.Pitch * desc.Height;
109 m_mapMode = QVideoFrame::ReadOnly;
113 qCDebug(qLcWindowCaptureUwp)
114 <<
"Failed to map DXGI surface" << QSystemError::windowsComString(hr);
122 void unmap() override
124 if (m_mapMode == QVideoFrame::NotMapped)
127 const HRESULT hr = m_surface->Unmap();
129 qCDebug(qLcWindowCaptureUwp)
130 <<
"Failed to unmap surface" << QSystemError::windowsComString(hr);
132 m_mapMode = QVideoFrame::NotMapped;
135 QVideoFrameFormat format()
const override {
return {}; }
138 QVideoFrame::MapMode m_mapMode = QVideoFrame::NotMapped;
139 com_ptr<IDXGISurface> m_surface;
144 WindowGrabber() =
default;
146 WindowGrabber(IDXGIAdapter1 *adapter, HWND hwnd)
147 : m_captureWindow{ hwnd }, m_frameSize{ getWindowSize(hwnd) }
149 check_hresult(D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN,
nullptr, 0,
nullptr, 0,
150 D3D11_SDK_VERSION, m_device.put(),
nullptr,
nullptr));
152 const auto captureItem = createCaptureItem(hwnd);
154 m_framePool = Direct3D11CaptureFramePool::CreateFreeThreaded(
155 getCaptureDevice(m_device), m_pixelFormat, 1,
158 m_session = m_framePool.CreateCaptureSession(captureItem);
161 if (
const auto session2 = m_session.try_as<IGraphicsCaptureSession2>())
162 session2.IsCursorCaptureEnabled(
true);
165 if (
const auto session3 = m_session.try_as<IGraphicsCaptureSession3>())
166 session3.IsBorderRequired(
false);
168 m_session.StartCapture();
177 com_ptr<IDXGISurface> tryGetFrame()
179 const Direct3D11CaptureFrame frame = m_framePool.TryGetNextFrame();
186 if (!IsWindow(m_captureWindow))
187 throw std::runtime_error(
"Window was closed");
194 if (m_frameSize != frame.ContentSize()) {
195 m_frameSize = frame.ContentSize();
196 m_framePool.Recreate(getCaptureDevice(m_device), m_pixelFormat, 1, frame.ContentSize());
200 return copyTexture(m_device, frame.Surface());
204 static GraphicsCaptureItem createCaptureItem(HWND hwnd)
206 const auto factory = winrt::get_activation_factory<GraphicsCaptureItem>();
207 const auto interop = factory.as<IGraphicsCaptureItemInterop>();
209 GraphicsCaptureItem item = {
nullptr };
210 winrt::hresult status = S_OK;
215 constexpr int maxRetry = 10;
216 constexpr std::chrono::milliseconds retryDelay{ 100 };
217 for (
int retryNum = 0; retryNum < maxRetry; ++retryNum) {
219 status = interop->CreateForWindow(hwnd, winrt::guid_of<GraphicsCaptureItem>(),
220 winrt::put_abi(item));
222 if (status != E_INVALIDARG)
225 qCWarning(qLcWindowCaptureUwp)
226 <<
"Failed to create capture item:"
227 << QString::fromStdWString(winrt::hresult_error(status).message().c_str())
228 <<
"Retry number" << retryNum;
230 if (retryNum + 1 < maxRetry)
231 QThread::sleep(retryDelay);
235 check_hresult(status);
240 static IDirect3DDevice getCaptureDevice(
const com_ptr<ID3D11Device> &d3dDevice)
242 const auto dxgiDevice = d3dDevice.as<IDXGIDevice>();
244 com_ptr<IInspectable> device;
245 check_hresult(CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.get(), device.put()));
247 return device.as<IDirect3DDevice>();
250 static com_ptr<IDXGISurface> copyTexture(
const com_ptr<ID3D11Device> &device,
251 const IDirect3DSurface &capturedTexture)
253 const auto dxgiInterop{ capturedTexture.as<IDirect3DDxgiInterfaceAccess>() };
257 com_ptr<IDXGISurface> dxgiSurface;
258 check_hresult(dxgiInterop->GetInterface(guid_of<IDXGISurface>(), dxgiSurface.put_void()));
260 DXGI_SURFACE_DESC desc = {};
261 check_hresult(dxgiSurface->GetDesc(&desc));
263 D3D11_TEXTURE2D_DESC texDesc = {};
264 texDesc.Width = desc.Width;
265 texDesc.Height = desc.Height;
266 texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
267 texDesc.Usage = D3D11_USAGE_STAGING;
268 texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
269 texDesc.MiscFlags = 0;
270 texDesc.BindFlags = 0;
271 texDesc.ArraySize = 1;
272 texDesc.MipLevels = 1;
273 texDesc.SampleDesc = { 1, 0 };
275 com_ptr<ID3D11Texture2D> texture;
276 check_hresult(device->CreateTexture2D(&texDesc,
nullptr, texture.put()));
278 com_ptr<ID3D11DeviceContext> ctx;
279 device->GetImmediateContext(ctx.put());
280 ctx->CopyResource(texture.get(), dxgiSurface.as<ID3D11Resource>().get());
282 return texture.as<IDXGISurface>();
285 MultithreadedApartment m_comApartment{};
286 HWND m_captureWindow{};
287 winrt::Windows::Graphics::SizeInt32 m_frameSize{};
288 com_ptr<ID3D11Device> m_device;
289 Direct3D11CaptureFramePool m_framePool{
nullptr };
290 GraphicsCaptureSession m_session{
nullptr };
291 const DirectXPixelFormat m_pixelFormat = DirectXPixelFormat::R8G8B8A8UIntNormalized;
299 Grabber(QFFmpegWindowCaptureUwp &capture, HWND hwnd)
304 const HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL);
305 m_adapter = getAdapter(monitor);
307 const qreal refreshRate = getMonitorRefreshRateHz(monitor);
309 m_format.setStreamFrameRate(refreshRate);
310 setFrameRate(refreshRate);
312 addFrameCallback(capture, &QFFmpegWindowCaptureUwp::newVideoFrame);
313 connect(
this, &Grabber::errorUpdated, &capture, &QFFmpegWindowCaptureUwp::updateError);
324 if (!m_adapter || !IsWindow(m_hwnd))
328 m_windowGrabber = std::make_unique<WindowGrabber>(m_adapter.get(), m_hwnd);
331 }
catch (
const winrt::hresult_error &err) {
333 const QString message = QLatin1String(
"Unable to capture window: ")
334 + QString::fromWCharArray(err.message().c_str());
336 updateError(InternalError, message);
343 m_windowGrabber =
nullptr;
349 com_ptr<IDXGISurface> texture = m_windowGrabber->tryGetFrame();
353 const QSize size = getTextureSize(texture);
355 m_format.setFrameSize(size);
357 return QVideoFramePrivate::createFrame(
358 std::make_unique<QUwpTextureVideoBuffer>(std::move(texture)), m_format);
360 }
catch (
const winrt::hresult_error &err) {
362 const QString message = QLatin1String(
"Window capture failed: ")
363 + QString::fromWCharArray(err.message().c_str());
365 updateError(InternalError, message);
366 }
catch (
const std::runtime_error& e) {
367 updateError(CaptureFailed, QString::fromLatin1(e.what()));
374 static com_ptr<IDXGIAdapter1> getAdapter(HMONITOR handle)
376 com_ptr<IDXGIFactory1> factory;
377 check_hresult(CreateDXGIFactory1(guid_of<IDXGIFactory1>(), factory.put_void()));
379 com_ptr<IDXGIAdapter1> adapter;
380 for (quint32 i = 0; factory->EnumAdapters1(i, adapter.put()) == S_OK; adapter =
nullptr, i++) {
381 com_ptr<IDXGIOutput> output;
382 for (quint32 j = 0; adapter->EnumOutputs(j, output.put()) == S_OK; output =
nullptr, j++) {
383 DXGI_OUTPUT_DESC desc = {};
384 HRESULT hr = output->GetDesc(&desc);
385 if (hr == S_OK && desc.Monitor == handle)
392 static QSize getTextureSize(
const com_ptr<IDXGISurface> &surf)
397 DXGI_SURFACE_DESC desc;
398 check_hresult(surf->GetDesc(&desc));
400 return {
static_cast<
int>(desc.Width),
static_cast<
int>(desc.Height) };
403 static qreal getMonitorRefreshRateHz(HMONITOR handle)
406 if (GetNumberOfPhysicalMonitorsFromHMONITOR(handle, &count)) {
407 std::vector<PHYSICAL_MONITOR> monitors{ count };
408 if (GetPhysicalMonitorsFromHMONITOR(handle, count, monitors.data())) {
409 for (
const auto &monitor : std::as_const(monitors)) {
410 MC_TIMING_REPORT screenTiming = {};
411 if (GetTimingReport(monitor.hPhysicalMonitor, &screenTiming)) {
415 return static_cast<qreal>(screenTiming.dwVerticalFrequencyInHZ) / 100.0;
420 return DefaultScreenCaptureFrameRate;
424 com_ptr<IDXGIAdapter1> m_adapter{};
425 std::unique_ptr<WindowGrabber> m_windowGrabber;
426 QVideoFrameFormat m_format;
429QFFmpegWindowCaptureUwp::QFFmpegWindowCaptureUwp() : QPlatformSurfaceCapture(WindowSource{})
431 qCDebug(qLcWindowCaptureUwp) <<
"Creating UWP screen capture";
434QFFmpegWindowCaptureUwp::~QFFmpegWindowCaptureUwp() =
default;
439 return u"Invalid window handle"_s;
441 if (hwnd == GetShellWindow())
442 return u"Cannot capture the shell window"_s;
444 wchar_t className[MAX_PATH] = {};
445 GetClassName(hwnd, className, MAX_PATH);
446 if (QString::fromWCharArray(className).length() == 0)
447 return u"Cannot capture windows without a class name"_s;
449 if (!IsWindowVisible(hwnd))
450 return u"Cannot capture invisible windows"_s;
452 if (GetAncestor(hwnd, GA_ROOT) != hwnd)
453 return u"Can only capture root windows"_s;
455 const LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
456 if (style & WS_DISABLED)
457 return u"Cannot capture disabled windows"_s;
459 const LONG_PTR exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
460 if (exStyle & WS_EX_TOOLWINDOW)
461 return u"No tooltips"_s;
463 DWORD cloaked = FALSE;
464 const HRESULT hr = DwmGetWindowAttribute(hwnd, DWMWA_CLOAKED, &cloaked,
sizeof(cloaked));
465 if (SUCCEEDED(hr) && cloaked == DWM_CLOAKED_SHELL)
466 return u"Cannot capture cloaked windows"_s;
471bool QFFmpegWindowCaptureUwp::setActiveInternal(
bool active)
473 if (
static_cast<
bool>(m_grabber) == active)
481 const auto window = source<WindowSource>();
482 const auto handle = QCapturableWindowPrivate::handle(window);
484 const auto hwnd =
reinterpret_cast<HWND>(handle ? handle->id : 0);
485 if (
const QString error = isCapturableWindow(hwnd); !error.isEmpty()) {
486 updateError(InternalError, error);
490 m_grabber = std::make_unique<Grabber>(*
this, hwnd);
496bool QFFmpegWindowCaptureUwp::isSupported()
498 return GraphicsCaptureSession::IsSupported();
501QVideoFrameFormat QFFmpegWindowCaptureUwp::frameFormat()
const
504 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)