summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/ffmpeg/qx11surfacecapture.cpp
blob: ab5ea3f480fa273f408c865d844a9f767b2c9821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qx11surfacecapture_p.h"
#include "qffmpegsurfacecapturegrabber_p.h"

#include <qvideoframe.h>
#include <qscreen.h>
#include <qwindow.h>
#include <qdebug.h>
#include <qguiapplication.h>
#include <qloggingcategory.h>

#include "private/qabstractvideobuffer_p.h"
#include "private/qcapturablewindow_p.h"
#include "private/qmemoryvideobuffer_p.h"
#include "private/qvideoframeconversionhelper_p.h"

#include <X11/Xlib.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrandr.h>

#include <optional>

QT_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(qLcX11SurfaceCapture, "qt.multimedia.ffmpeg.qx11surfacecapture");

namespace {

void destroyXImage(XImage* image) {
    XDestroyImage(image); // macro
}

template <typename T, typename D>
std::unique_ptr<T, D> makeXUptr(T* ptr, D deleter) {
   return std::unique_ptr<T, D>(ptr, deleter);
}

int screenNumberByName(Display *display, const QString &name)
{
    int size = 0;
    auto monitors = makeXUptr(
            XRRGetMonitors(display, XDefaultRootWindow(display), true, &size),
                &XRRFreeMonitors);
    const auto end = monitors.get() + size;
    auto found = std::find_if(monitors.get(), end, [&](const XRRMonitorInfo &info) {
        auto atomName = makeXUptr(XGetAtomName(display, info.name), &XFree);
        return atomName && name == QString::fromUtf8(atomName.get());
    });

    return found == end ? -1 : std::distance(monitors.get(), found);
}

QVideoFrameFormat::PixelFormat xImagePixelFormat(const XImage &image)
{
    if (image.bits_per_pixel != 32) return QVideoFrameFormat::Format_Invalid;

    if (image.red_mask == 0xff0000 &&
        image.green_mask == 0xff00 &&
        image.blue_mask == 0xff)
        return QVideoFrameFormat::Format_BGRX8888;

    if (image.red_mask == 0xff00 &&
        image.green_mask == 0xff0000 &&
        image.blue_mask == 0xff000000)
        return QVideoFrameFormat::Format_XBGR8888;

    if (image.blue_mask == 0xff0000 &&
        image.green_mask == 0xff00 &&
        image.red_mask == 0xff)
        return QVideoFrameFormat::Format_RGBX8888;

    if (image.red_mask == 0xff00 &&
        image.green_mask == 0xff0000 &&
        image.blue_mask == 0xff000000)
        return QVideoFrameFormat::Format_XRGB8888;

    return QVideoFrameFormat::Format_Invalid;
}

} // namespace

class QX11SurfaceCapture::Grabber : private QFFmpegSurfaceCaptureGrabber
{
public:
    static std::unique_ptr<Grabber> create(QX11SurfaceCapture &capture, QScreen *screen)
    {
        std::unique_ptr<Grabber> result(new Grabber(capture));
        return result->init(screen) ? std::move(result) : nullptr;
    }

    static std::unique_ptr<Grabber> create(QX11SurfaceCapture &capture, WId wid)
    {
        std::unique_ptr<Grabber> result(new Grabber(capture));
        return result->init(wid) ? std::move(result) : nullptr;
    }

    ~Grabber() override
    {
        stop();

        detachShm();
    }

    const QVideoFrameFormat &format() const { return m_format; }

private:
    Grabber(QX11SurfaceCapture &capture)
    {
        addFrameCallback(capture, &QX11SurfaceCapture::newVideoFrame);
        connect(this, &Grabber::errorUpdated, &capture, &QX11SurfaceCapture::updateError);
    }

    bool createDisplay()
    {
        if (!m_display)
            m_display.reset(XOpenDisplay(nullptr));

        if (!m_display)
            updateError(QPlatformSurfaceCapture::InternalError,
                        QLatin1String("Cannot open X11 display"));

        return m_display != nullptr;
    }

    bool init(WId wid)
    {
        if (auto screen = QGuiApplication::primaryScreen())
            setFrameRate(screen->refreshRate());

        return createDisplay() && initWithXID(static_cast<XID>(wid));
    }

    bool init(QScreen *screen)
    {
        if (!screen) {
            updateError(QPlatformSurfaceCapture::NotFound, QLatin1String("Screen Not Found"));
            return false;
        }

        if (!createDisplay())
            return false;

        auto screenNumber = screenNumberByName(m_display.get(), screen->name());

        if (screenNumber < 0)
            return false;

        setFrameRate(screen->refreshRate());

        return initWithXID(RootWindow(m_display.get(), screenNumber));
    }

    bool initWithXID(XID xid)
    {
        m_xid = xid;

        if (update()) {
            start();
            return true;
        }

        return false;
    }

    void detachShm()
    {
        if (std::exchange(m_attached, false)) {
            XShmDetach(m_display.get(), &m_shmInfo);
            shmdt(m_shmInfo.shmaddr);
            shmctl(m_shmInfo.shmid, IPC_RMID, 0);
        }
    }

    void attachShm()
    {
        Q_ASSERT(!m_attached);

        m_shmInfo.shmid =
                shmget(IPC_PRIVATE, m_xImage->bytes_per_line * m_xImage->height, IPC_CREAT | 0777);

        if (m_shmInfo.shmid == -1)
            return;

        m_shmInfo.readOnly = false;
        m_shmInfo.shmaddr = m_xImage->data = (char *)shmat(m_shmInfo.shmid, 0, 0);

        m_attached = XShmAttach(m_display.get(), &m_shmInfo);
    }

    bool update()
    {
        XWindowAttributes wndattr = {};
        if (XGetWindowAttributes(m_display.get(), m_xid, &wndattr) == 0) {
            updateError(QPlatformSurfaceCapture::CaptureFailed,
                        QLatin1String("Cannot get window attributes"));
            return false;
        }

        // TODO: if capture windows, we should adjust offsets and size if
        // the window is out of the screen borders
        // m_xOffset = ...
        // m_yOffset = ...

        // check window params for the root window as well since
        // it potentially can be changed (e.g. on VM with resizing)
        if (!m_xImage || wndattr.width != m_xImage->width || wndattr.height != m_xImage->height
            || wndattr.depth != m_xImage->depth || wndattr.visual->visualid != m_visualID) {

            qCDebug(qLcX11SurfaceCapture) << "recreate ximage: " << wndattr.width << wndattr.height
                                          << wndattr.depth << wndattr.visual->visualid;

            detachShm();
            m_xImage.reset();

            m_visualID = wndattr.visual->visualid;
            m_xImage.reset(XShmCreateImage(m_display.get(), wndattr.visual, wndattr.depth, ZPixmap,
                                           nullptr, &m_shmInfo, wndattr.width, wndattr.height));

            if (!m_xImage) {
                updateError(QPlatformSurfaceCapture::CaptureFailed,
                            QLatin1String("Cannot create image"));
                return false;
            }

            const auto pixelFormat = xImagePixelFormat(*m_xImage);

            // TODO: probably, add a converter instead
            if (pixelFormat == QVideoFrameFormat::Format_Invalid) {
                updateError(QPlatformSurfaceCapture::CaptureFailed,
                            QLatin1String("Not handled pixel format, bpp=")
                                    + QString::number(m_xImage->bits_per_pixel));
                return false;
            }

            attachShm();

            if (!m_attached) {
                updateError(QPlatformSurfaceCapture::CaptureFailed,
                            QLatin1String("Cannot attach shared memory"));
                return false;
            }

            QVideoFrameFormat format(QSize(m_xImage->width, m_xImage->height), pixelFormat);
            format.setStreamFrameRate(frameRate());
            m_format = format;
        }

        return m_attached;
    }

protected:
    QVideoFrame grabFrame() override
    {
        if (!update())
            return {};

        if (!XShmGetImage(m_display.get(), m_xid, m_xImage.get(), m_xOffset, m_yOffset,
                          AllPlanes)) {
            updateError(QPlatformSurfaceCapture::CaptureFailed,
                        QLatin1String(
                                "Cannot get ximage; the window may be out of the screen borders"));
            return {};
        }

        QByteArray data(m_xImage->bytes_per_line * m_xImage->height, Qt::Uninitialized);

        const auto pixelSrc = reinterpret_cast<const uint32_t *>(m_xImage->data);
        const auto pixelDst = reinterpret_cast<uint32_t *>(data.data());
        const auto pixelCount = data.size() / 4;
        const auto xImageAlphaVaries = false; // In known cases it doesn't vary - it's 0xff or 0xff

        qCopyPixelsWithAlphaMask(pixelDst, pixelSrc, pixelCount, m_format.pixelFormat(),
                                       xImageAlphaVaries);

        auto buffer = new QMemoryVideoBuffer(data, m_xImage->bytes_per_line);
        return QVideoFrame(buffer, m_format);
    }

private:
    std::optional<QPlatformSurfaceCapture::Error> m_prevGrabberError;
    XID m_xid = None;
    int m_xOffset = 0;
    int m_yOffset = 0;
    std::unique_ptr<Display, decltype(&XCloseDisplay)> m_display{ nullptr, &XCloseDisplay };
    std::unique_ptr<XImage, decltype(&destroyXImage)> m_xImage{ nullptr, &destroyXImage };
    XShmSegmentInfo m_shmInfo;
    bool m_attached = false;
    VisualID m_visualID = None;
    QVideoFrameFormat m_format;
};

QX11SurfaceCapture::QX11SurfaceCapture(Source initialSource)
    : QPlatformSurfaceCapture(initialSource)
{
    // For debug
    //  XSetErrorHandler([](Display *, XErrorEvent * e) {
    //      qDebug() << "error handler" << e->error_code;
    //      return 0;
    //  });
}

QX11SurfaceCapture::~QX11SurfaceCapture() = default;

QVideoFrameFormat QX11SurfaceCapture::frameFormat() const
{
    return m_grabber ? m_grabber->format() : QVideoFrameFormat{};
}

bool QX11SurfaceCapture::setActiveInternal(bool active)
{
    qCDebug(qLcX11SurfaceCapture) << "set active" << active;

    if (m_grabber)
        m_grabber.reset();
    else
        std::visit([this](auto source) { activate(source); }, source());

    return static_cast<bool>(m_grabber) == active;
}

void QX11SurfaceCapture::activate(ScreenSource screen)
{
    if (checkScreenWithError(screen))
        m_grabber = Grabber::create(*this, screen);
}

void QX11SurfaceCapture::activate(WindowSource window)
{
    auto handle = QCapturableWindowPrivate::handle(window);
    m_grabber = Grabber::create(*this, handle ? handle->id : 0);
}

bool QX11SurfaceCapture::isSupported()
{
    return qgetenv("XDG_SESSION_TYPE").compare(QLatin1String("x11"), Qt::CaseInsensitive) == 0;
}

QT_END_NAMESPACE