Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgdiwindowcapture.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include "qvideoframe.h"
8#include "private/qcapturablewindow_p.h"
9#include "private/qmemoryvideobuffer_p.h"
10#include "private/qvideoframe_p.h"
11
12#include <qt_windows.h>
13#include <QtCore/qloggingcategory.h>
14
15Q_STATIC_LOGGING_CATEGORY(qLcGdiWindowCapture, "qt.multimedia.ffmpeg.gdiwindowcapture");
16
18
20{
21public:
22 static std::unique_ptr<Grabber> create(QGdiWindowCapture &capture, HWND hWnd)
23 {
24 auto hdcWindow = GetDC(hWnd);
25 if (!hdcWindow) {
27 QLatin1String("Cannot create a window drawing context"));
28 return nullptr;
29 }
30
31 auto hdcMem = CreateCompatibleDC(hdcWindow);
32
33 if (!hdcMem) {
35 QLatin1String("Cannot create a compatible drawing context"));
36 return nullptr;
37 }
38
39 std::unique_ptr<Grabber> result(new Grabber(capture, hWnd, hdcWindow, hdcMem));
40 if (!result->update())
41 return nullptr;
42
43 result->start();
44 return result;
45 }
46
47 ~Grabber() override
48 {
49 stop();
50
51 if (m_hBitmap)
52 DeleteObject(m_hBitmap);
53
54 if (m_hdcMem)
55 DeleteDC(m_hdcMem);
56
57 if (m_hdcWindow)
58 ReleaseDC(m_hwnd, m_hdcWindow);
59 }
60
61 QVideoFrameFormat format() const { return m_format; }
62
63private:
64 Grabber(QGdiWindowCapture &capture, HWND hWnd, HDC hdcWindow, HDC hdcMem)
65 : m_hwnd(hWnd), m_hdcWindow(hdcWindow), m_hdcMem(hdcMem)
66 {
67 if (auto rate = GetDeviceCaps(hdcWindow, VREFRESH); rate > 0)
69
72 }
73
74 bool update()
75 {
76 RECT windowRect{};
77 if (!GetWindowRect(m_hwnd, &windowRect)) {
79 QLatin1String("Cannot get window size"));
80 return false;
81 }
82
83 const QSize size{ windowRect.right - windowRect.left, windowRect.bottom - windowRect.top };
84
85 if (m_format.isValid() && size == m_format.frameSize() && m_hBitmap)
86 return true;
87
88 if (m_hBitmap)
89 DeleteObject(std::exchange(m_hBitmap, nullptr));
90
91 if (size.isEmpty()) {
92 m_format = {};
94 QLatin1String("Invalid window size"));
95 return false;
96 }
97
98 m_hBitmap = CreateCompatibleBitmap(m_hdcWindow, size.width(), size.height());
99
100 if (!m_hBitmap) {
101 m_format = {};
103 QLatin1String("Cannot create a compatible bitmap"));
104 return false;
105 }
106
108 format.setStreamFrameRate(frameRate());
109 m_format = format;
110 return true;
111 }
112
114 {
115 if (!update())
116 return {};
117
118 const auto oldBitmap = SelectObject(m_hdcMem, m_hBitmap);
119 auto deselect = qScopeGuard([&]() { SelectObject(m_hdcMem, oldBitmap); });
120
121 const auto size = m_format.frameSize();
122
123 if (!BitBlt(m_hdcMem, 0, 0, size.width(), size.height(), m_hdcWindow, 0, 0, SRCCOPY)) {
125 QLatin1String("Cannot copy image to the compatible DC"));
126 return {};
127 }
128
129 BITMAPINFO info{};
130 auto &header = info.bmiHeader;
131 header.biSize = sizeof(BITMAPINFOHEADER);
132 header.biWidth = size.width();
133 header.biHeight = -size.height(); // negative height to ensure top-down orientation
134 header.biPlanes = 1;
135 header.biBitCount = 32;
136 header.biCompression = BI_RGB;
137
138 const auto bytesPerLine = size.width() * 4;
139
140 QByteArray array(size.height() * bytesPerLine, Qt::Uninitialized);
141
142 const auto copiedHeight = GetDIBits(m_hdcMem, m_hBitmap, 0, size.height(), array.data(), &info, DIB_RGB_COLORS);
143 if (copiedHeight != size.height()) {
144 qCWarning(qLcGdiWindowCapture) << copiedHeight << "lines have been copied, expected:" << size.height();
145 // In practice, it might fail randomly first time after start. So we don't consider it as an error.
146 // TODO: investigate reasons and properly handle the error
147 // updateError(QPlatformSurfaceCapture::CaptureFailed,
148 // QLatin1String("Cannot get raw image data"));
149 return {};
150 }
151
152 if (header.biWidth != size.width() || header.biHeight != -size.height()
153 || header.biPlanes != 1 || header.biBitCount != 32 || header.biCompression != BI_RGB) {
155 QLatin1String("Output bitmap info is unexpected"));
156 return {};
157 }
158
160 std::make_unique<QMemoryVideoBuffer>(std::move(array), bytesPerLine), m_format);
161 }
162
163private:
164 HWND m_hwnd = {};
165 QVideoFrameFormat m_format;
166 HDC m_hdcWindow = {};
167 HDC m_hdcMem = {};
168 HBITMAP m_hBitmap = {};
169};
170
172
174
176{
177 return m_grabber ? m_grabber->format() : QVideoFrameFormat();
178}
179
181{
182 if (active == static_cast<bool>(m_grabber))
183 return true;
184
185 if (m_grabber) {
186 m_grabber.reset();
187 } else {
188 auto window = source<WindowSource>();
190
191 m_grabber = Grabber::create(*this, reinterpret_cast<HWND>(handle ? handle->id : 0));
192 }
193
194 return static_cast<bool>(m_grabber) == active;
195}
196
\inmodule QtCore
Definition qbytearray.h:57
static const QCapturableWindowPrivate * handle(const QCapturableWindow &window)
\inmodule QtMultimedia
void errorUpdated(QPlatformSurfaceCapture::Error error, const QString &description)
void addFrameCallback(Object &object, Method method)
void updateError(QPlatformSurfaceCapture::Error error, const QString &description={})
static std::unique_ptr< Grabber > create(QGdiWindowCapture &capture, HWND hWnd)
QVideoFrameFormat format() const
QVideoFrame grabFrame() override
QVideoFrameFormat frameFormat() const override
bool setActiveInternal(bool active) override
~QGdiWindowCapture() override
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void updateError(Error error, const QString &errorString)
void newVideoFrame(const QVideoFrame &)
\inmodule QtCore
Definition qsize.h:25
The QVideoFrameFormat class specifies the stream format of a video presentation surface.
bool isValid() const
Identifies if a video surface format has a valid pixel format and frame size.
QSize frameSize() const
Returns the dimensions of frames in a video stream.
static QVideoFrame createFrame(std::unique_ptr< Buffer > buffer, QVideoFrameFormat format)
The QVideoFrame class represents a frame of video data.
Definition qvideoframe.h:27
Combined button and popup list for selecting options.
constexpr Initialization Uninitialized
static QString header(const QString &name)
#define qCWarning(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)
GLuint64 GLenum void * handle
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum format
GLuint GLenum * rate
GLenum array
GLuint64EXT * result
[6]
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
aWidget window() -> setWindowTitle("New Window Title")
[2]
QHostInfo info
[0]