48 Q_ASSERT(!m_screenRemovingLocked);
55 m_waitForFormat.wait(&m_formatMutex);
61 : QFFmpegSurfaceCaptureGrabber(
62 QGuiApplication::platformName() == QLatin1String(
"eglfs")
63 ? QFFmpegSurfaceCaptureGrabber::UseCurrentThread
64 : QFFmpegSurfaceCaptureGrabber::CreateGrabbingThread),
67 m_window(std::move(window))
69 connect(
qApp, &QGuiApplication::screenRemoved,
this, &Grabber::onScreenRemoved);
70 addFrameCallback(m_capture, &QGrabWindowSurfaceCapture::newVideoFrame);
71 connect(
this, &Grabber::errorUpdated, &m_capture, &QGrabWindowSurfaceCapture::updateError);
74 void onScreenRemoved(QScreen *screen)
77
78
79
80
81
84 if (m_screenRemovingLocked) {
85 qDebug() <<
"Screen" << screen->name()
86 <<
"is removed while screen window grabbing lock is active";
89 while (m_screenRemovingLocked)
90 m_screenRemovingWc.wait(&m_screenRemovingMutex);
93 void setScreenRemovingLocked(
bool locked)
95 Q_ASSERT(locked != m_screenRemovingLocked);
99 m_screenRemovingLocked = locked;
103 m_screenRemovingWc.wakeAll();
106 void updateFormat(
const QVideoFrameFormat &format)
108 if (m_format && m_format->isValid())
116 m_waitForFormat.wakeAll();
121 setScreenRemovingLocked(
true);
122 auto screenGuard = qScopeGuard(std::bind(&Grabber::setScreenRemovingLocked,
this,
false));
124 WId wid = m_window ? m_window->winId() : 0;
125 QScreen *screen = m_window ? m_window->screen() : m_screen ? m_screen.data() :
nullptr;
128 updateError(QPlatformSurfaceCapture::CaptureFailed,
"Screen not found");
132 setFrameRate(screen->refreshRate());
134 QPixmap p = screen->grabWindow(wid);
135 auto buffer = std::make_unique<QImageVideoBuffer>(p.toImage());
136 const auto img = buffer->underlyingImage();
138 QVideoFrameFormat format(img.size(),
139 QVideoFrameFormat::pixelFormatFromImageFormat(img.format()));
140 format.setStreamFrameRate(screen->refreshRate());
141 updateFormat(format);
143 if (!format.isValid()) {
144 updateError(QPlatformSurfaceCapture::CaptureFailed,
145 "Failed to grab the screen content");
149 return QVideoFramePrivate::createFrame(std::move(buffer), std::move(format));
157 QMutex m_formatMutex;
158 QWaitCondition m_waitForFormat;
159 std::optional<QVideoFrameFormat> m_format;
161 QMutex m_screenRemovingMutex;
162 bool m_screenRemovingLocked =
false;
163 QWaitCondition m_screenRemovingWc;