36 Grabber(QGrabWindowSurfaceCapture &capture, QScreen *screen) :
Grabber(capture, screen,
nullptr)
41 Grabber(QGrabWindowSurfaceCapture &capture, WindowUPtr window)
50 Q_ASSERT(!m_screenRemovingLocked);
57 m_waitForFormat.wait(&m_formatMutex);
62 Grabber(QGrabWindowSurfaceCapture &capture, QScreen *screen, WindowUPtr window)
63 : QFFmpegSurfaceCaptureGrabber(
64 QGuiApplication::platformName() == QLatin1String(
"eglfs")
65 ? QFFmpegSurfaceCaptureGrabber::UseCurrentThread
66 : QFFmpegSurfaceCaptureGrabber::CreateGrabbingThread),
69 m_window(std::move(window))
71 connect(
qApp, &QGuiApplication::screenRemoved,
this, &Grabber::onScreenRemoved);
72 addFrameCallback(m_capture, &QGrabWindowSurfaceCapture::newVideoFrame);
73 connect(
this, &Grabber::errorUpdated, &m_capture, &QGrabWindowSurfaceCapture::updateError);
76 void onScreenRemoved(QScreen *screen)
79
80
81
82
83
86 if (m_screenRemovingLocked) {
87 qDebug() <<
"Screen" << screen->name()
88 <<
"is removed while screen window grabbing lock is active";
91 while (m_screenRemovingLocked)
92 m_screenRemovingWc.wait(&m_screenRemovingMutex);
95 void setScreenRemovingLocked(
bool locked)
97 Q_ASSERT(locked != m_screenRemovingLocked);
101 m_screenRemovingLocked = locked;
105 m_screenRemovingWc.wakeAll();
108 void updateFormat(
const QVideoFrameFormat &format)
110 if (m_format && m_format->isValid())
118 m_waitForFormat.wakeAll();
123 setScreenRemovingLocked(
true);
124 auto screenGuard = qScopeGuard([
this] {
125 setScreenRemovingLocked(
false);
128 WId wid = m_window ? m_window->winId() : 0;
129 QScreen *screen = m_window ? m_window->screen() : m_screen ? m_screen.data() :
nullptr;
132 updateError(QPlatformSurfaceCapture::CaptureFailed, u"Screen not found"_s);
136 setFrameRate(screen->refreshRate());
138 QPixmap p = screen->grabWindow(wid);
139 auto buffer = std::make_unique<QImageVideoBuffer>(p.toImage());
140 const auto img = buffer->underlyingImage();
142 QVideoFrameFormat format(img.size(),
143 QVideoFrameFormat::pixelFormatFromImageFormat(img.format()));
144 format.setStreamFrameRate(screen->refreshRate());
145 updateFormat(format);
147 if (!format.isValid()) {
148 updateError(QPlatformSurfaceCapture::CaptureFailed,
149 u"Failed to grab the screen content"_s);
153 return QVideoFramePrivate::createFrame(std::move(buffer), std::move(format));
157 QGrabWindowSurfaceCapture &m_capture;
161 QMutex m_formatMutex;
162 QWaitCondition m_waitForFormat;
163 std::optional<QVideoFrameFormat> m_format;
165 QMutex m_screenRemovingMutex;
166 bool m_screenRemovingLocked =
false;
167 QWaitCondition m_screenRemovingWc;