6#include <QtMultimedia/private/qpipewire_instance_p.h>
7#include <QtMultimedia/private/qpipewire_videoformat_support_p.h>
8#include <QtMultimedia/private/qmemoryvideobuffer_p.h>
9#include <QtMultimedia/private/qpipewire_async_support_p.h>
10#include <QtMultimedia/private/qvideoframe_p.h>
11#include <QtGui/private/qdesktopunixservices_p.h>
12#include <QtGui/private/qguiapplication_p.h>
13#include <QtGui/qguiapplication.h>
14#include <QtGui/qpa/qplatformintegration.h>
15#include <QtCore/qloggingcategory.h>
16#include <QtCore/quuid.h>
17#include <QtCore/qvariantmap.h>
18#include <QtDBus/qdbusconnection.h>
19#include <QtDBus/qdbusinterface.h>
20#include <QtDBus/qdbusmessage.h>
21#include <QtDBus/qdbusreply.h>
22#include <QtDBus/qdbusunixfiledescriptor.h>
24#include <spa/debug/types.h>
25#include <spa/param/video/format-utils.h>
26#include <spa/param/video/type-info.h>
32QT_WARNING_DISABLE_CLANG(
"-Wunused-value")
36using namespace Qt::StringLiterals;
37using namespace std::chrono_literals;
38using namespace std::string_view_literals;
47 : QSurfaceCaptureGrabber(CreateGrabbingThread),
48 m_pwInstance(std::move(instance)),
49 m_requestTokenPrefix(QUuid::createUuid().toString(QUuid::Id128))
51 Q_ASSERT(m_pwInstance);
53 addFrameCallback(&capture, &QPipeWireCapture::newVideoFrame);
54 connect(
this, &QSurfaceCaptureGrabber::errorUpdated, &capture, &QPipeWireCapture::updateError);
64 return std::move(m_currentFrame);
69 QSurfaceCaptureGrabber::initializeGrabbingContext();
70 if (m_state == NoState)
76 unsubscribeFromRequestResponse();
77 m_operationState = NoOperation;
79 if (m_state != NoState)
81 QSurfaceCaptureGrabber::finalizeGrabbingContext();
86 return m_videoFrameFormat;
91 qCDebug(qLcPipeWireCapture) << Q_FUNC_INFO <<
"result=" << result <<
"map=" << map;
94 unsubscribeFromRequestResponse();
97 m_operationState = NoOperation;
98 qWarning() <<
"Failed to capture screen via pipewire, perhaps because user cancelled the "
103 switch (m_operationState) {
105 selectSources(QDBusObjectPath{ map[u"session_handle"_s].toString() });
111 updateStreams(map[u"streams"_s].value<QDBusArgument>());
112 openPipeWireRemote();
113 m_operationState = NoOperation;
116 case OpenPipeWireRemote:
117 m_operationState = NoOperation;
126 return u"qtmm_%1_%2"_s.arg(m_requestTokenPrefix).arg(++m_tokenCounter);
131 Q_ASSERT(m_pendingRequestPath.isEmpty());
133 QDBusConnection bus = QDBusConnection::sessionBus();
135 QString sender = bus.baseService();
136 if (sender.startsWith(u':'))
138 sender.replace(u'.', u'_');
140 QString path = u"/org/freedesktop/portal/desktop/request/%1/%2"_s.arg(sender, handleToken);
142 const bool ok = bus.connect(u"org.freedesktop.portal.Desktop"_s, path,
143 u"org.freedesktop.portal.Request"_s, u"Response"_s,
this,
144 SLOT(gotRequestResponse(uint, QVariantMap)));
146 updateError(QPlatformSurfaceCapture::Error::InternalError,
147 u"Failed to subscribe to org.freedesktop.portal.Request response for "_s
152 m_pendingRequestPath = std::move(path);
158 if (m_pendingRequestPath.isEmpty())
161 QDBusConnection::sessionBus().disconnect(u"org.freedesktop.portal.Desktop"_s,
162 m_pendingRequestPath,
163 u"org.freedesktop.portal.Request"_s, u"Response"_s,
164 this, SLOT(gotRequestResponse(uint, QVariantMap)));
166 m_pendingRequestPath.clear();
171 Q_ASSERT(m_pwInstance->hasScreenCastPortal());
173 m_operationState = NoOperation;
175 if (!m_screenCastInterface) {
176 m_screenCastInterface = std::make_unique<QDBusInterface>(
177 u"org.freedesktop.portal.Desktop"_s, u"/org/freedesktop/portal/desktop"_s,
178 u"org.freedesktop.portal.ScreenCast"_s, QDBusConnection::sessionBus());
185 if (!m_screenCastInterface)
188 const QString handleToken = allocateToken();
189 if (!subscribeToRequestResponse(handleToken))
193 { u"handle_token"_s, handleToken },
194 { u"session_handle_token"_s, allocateToken() },
196 QDBusMessage reply = m_screenCastInterface->call(u"CreateSession"_s, options);
197 if (!reply.errorMessage().isEmpty()) {
198 unsubscribeFromRequestResponse();
199 updateError(QPlatformSurfaceCapture::Error::InternalError,
200 u"Failed to create session for org.freedesktop.portal.ScreenCast. Error: "_s
201 + reply.errorName() + u": "_s + reply.errorMessage());
205 m_operationState = CreateSession;
210 if (!m_screenCastInterface)
213 m_sessionHandle = sessionHandle;
214 m_sessionInterface = std::make_unique<QDBusInterface>(
215 u"org.freedesktop.portal.Desktop"_s, sessionHandle.path(),
216 u"org.freedesktop.portal.Session"_s, QDBusConnection::sessionBus());
217 m_sessionInterface->connection().connect(
218 u"org.freedesktop.portal.Desktop"_s, sessionHandle.path(),
219 u"org.freedesktop.portal.Session"_s, u"Closed"_s,
this, SLOT(sessionClosed()));
221 const QString handleToken = allocateToken();
222 if (!subscribeToRequestResponse(handleToken))
226 { u"handle_token"_s, handleToken },
227 { u"types"_s, (uint)1 },
228 { u"multiple"_s,
false },
229 { u"cursor_mode"_s, (uint)1 },
230 { u"persist_mode"_s, (uint)0 },
232 QDBusMessage reply = m_screenCastInterface->call(u"SelectSources"_s, sessionHandle, options);
233 if (!reply.errorMessage().isEmpty()) {
234 unsubscribeFromRequestResponse();
235 updateError(QPlatformSurfaceCapture::Error::InternalError,
236 u"Failed to select sources for org.freedesktop.portal.ScreenCast. Error: "_s
237 + reply.errorName() + u": "_s + reply.errorMessage());
241 m_operationState = SelectSources;
246 if (!m_screenCastInterface)
249 const QString handleToken = allocateToken();
250 if (!subscribeToRequestResponse(handleToken))
254 { u"handle_token"_s, handleToken },
257 auto *unixServices =
dynamic_cast<QDesktopUnixServices *>(QGuiApplicationPrivate::platformIntegration()->services());
258 const QString parentWindow = QGuiApplication::focusWindow() && unixServices
259 ? unixServices->portalWindowIdentifier(QGuiApplication::focusWindow())
261 QDBusMessage reply = m_screenCastInterface->call(
"Start"_L1, QDBusObjectPath(m_sessionHandle),
262 parentWindow, options);
263 if (!reply.errorMessage().isEmpty()) {
264 unsubscribeFromRequestResponse();
265 updateError(QPlatformSurfaceCapture::Error::InternalError,
266 u"Failed to start stream for org.freedesktop.portal.ScreenCast. Error: "_s
267 + reply.errorName() + u": "_s + reply.errorMessage());
271 m_operationState = StartStream;
278 streamsInfo.beginStructure();
279 streamsInfo.beginArray();
281 while (!streamsInfo.atEnd()) {
283 streamsInfo >> nodeId;
284 QMap<QString, QVariant> properties;
285 streamsInfo >> properties;
289 if (properties.contains(u"position"_s)) {
290 const auto position = properties[u"position"_s].value<QDBusArgument>();
291 position.beginStructure();
294 position.endStructure();
299 if (properties.contains(u"size"_s)) {
300 const auto size = properties[u"size"_s].value<QDBusArgument>();
301 size.beginStructure();
308 if (properties.contains(u"source_type"_s))
309 sourceType = properties[u"source_type"_s].toUInt();
311 StreamInfo streamInfo;
312 streamInfo.nodeId = nodeId;
313 streamInfo.sourceType = sourceType;
314 streamInfo.rect = {x, y, width, height};
315 m_streams << streamInfo;
318 streamsInfo.endArray();
319 streamsInfo.endStructure();
325 if (!m_screenCastInterface)
329 QDBusReply<QDBusUnixFileDescriptor> reply =
330 m_screenCastInterface->call(u"OpenPipeWireRemote"_s, m_sessionHandle, options);
331 if (!reply.isValid()) {
333 QPlatformSurfaceCapture::Error::InternalError,
334 u"Failed to open pipewire remote for org.freedesktop.portal.ScreenCast. Error: name="_s
335 + reply.error().name() + u", message="_s + reply.error().message());
339 bool ok =
open(reply.value()
);
340 qCDebug(qLcPipeWireCapture) <<
"open() result=" << ok;
342 updateError(QPlatformSurfaceCapture::Error::InternalError,
343 u"Failed to open pipewire remote file descriptor"_s);
347 m_operationState = OpenPipeWireRemote;
352 if (m_streams.isEmpty())
355 Q_ASSERT(m_pwInstance->hasScreenCastPortal());
357 std::unique_lock pwLock = m_pwInstance->eventLoopLock();
358 auto connection = m_pwInstance->connectToPortal(portalFd.takeFileDescriptor());
362 updateError(QPlatformSurfaceCapture::Error::InternalError,
363 u"QPipeWireCaptureHelper failed at pw_context_connect_fd()."_s);
366 m_core = std::move(*connection);
368 m_registry = PwRegistryHandle{
369 pw_core_get_registry(m_core.get(), PW_VERSION_REGISTRY, 0),
374 updateError(QPlatformSurfaceCapture::Error::InternalError,
375 u"QPipeWireCaptureHelper failed at pw_core_get_registry()."_s);
379 struct RegistryCallbackContext
384 spa_hook registryListener = {};
385 const pw_registry_events registryEvents{
386 .version = PW_VERSION_REGISTRY_EVENTS,
388 [](
void *data, uint32_t , uint32_t ,
const char *type,
389 uint32_t ,
const spa_dict *props) {
390 if (type != std::string_view(PW_TYPE_INTERFACE_Node))
393 const char *media_class = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS);
397 if (media_class !=
"Stream/Output/Video"sv && media_class !=
"Video/Source"sv)
399 auto *registryContext =
reinterpret_cast<RegistryCallbackContext *>(data);
400 registryContext->hasSource =
true;
402 .global_remove =
nullptr,
405 RegistryCallbackContext registryContext;
406 pw_registry_add_listener(m_registry.get(), ®istryListener, ®istryEvents,
409 CoreEventDoneListener doneListener;
410 bool initDone =
false;
411 doneListener.asyncWait(m_core.get(), [&] {
413 m_pwInstance->pwEventLoop().signal(
false);
417 if (m_pwInstance->pwEventLoop().wait_for(2s) != 0)
421 spa_hook_remove(®istryListener);
423 if (registryContext.hasSource)
426 return initDone && registryContext.hasSource;
431 static const pw_stream_events streamEvents = {
432 .version = PW_VERSION_STREAM_EVENTS,
433 .destroy = [](
void *data) {
436 .state_changed = [](
void *data, pw_stream_state old, pw_stream_state state,
const char *error) {
439 .control_info = [](
void *data, uint32_t id,
const struct pw_stream_control *control) {
444 .io_changed = [](
void *data, uint32_t id,
void *area, uint32_t size) {
450 .param_changed = [](
void *data, uint32_t id,
const struct spa_pod *param) {
453 .add_buffer = [](
void *data,
struct pw_buffer *buffer) {
457 .remove_buffer = [](
void *data,
struct pw_buffer *buffer) {
461 .process = [](
void *data) {
464 .drained = [](
void *data) {
467#if PW_VERSION_STREAM_EVENTS
>= 1
468 .command = [](
void *data,
const struct spa_command *command) {
473#if PW_VERSION_STREAM_EVENTS
>= 2
474 .trigger_done = [](
void *data) {
482 auto streamInfo = m_streams[0];
483 const struct std::array<spa_dict_item, 3> items{
484 SPA_DICT_ITEM_INIT(PW_KEY_MEDIA_TYPE,
"Video"),
485 SPA_DICT_ITEM_INIT(PW_KEY_MEDIA_CATEGORY,
"Capture"),
486 SPA_DICT_ITEM_INIT(PW_KEY_MEDIA_ROLE,
"Screen"),
488 const struct spa_dict info = SPA_DICT_INIT(items.data(), items.size());
489 auto *props = pw_properties_new_dict(&info);
491 std::unique_lock locker = m_pwInstance->eventLoopLock();
493 m_stream = PwStreamHandle{
494 pw_stream_new(m_core.get(),
"video-capture", props),
499 updateError(QPlatformSurfaceCapture::Error::InternalError,
500 u"QPipeWireCaptureHelper failed at pw_stream_new()."_s);
504 m_streamListener = {};
505 pw_stream_add_listener(m_stream.get(), &m_streamListener, &streamEvents,
this);
509 QT_WARNING_DISABLE_GCC(
"-Wmissing-field-initializers")
510 QT_WARNING_DISABLE_CLANG(
"-Wmissing-field-initializers")
512 std::array<uint8_t, 4096> buffer;
513 struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(buffer.data(),
sizeof(buffer));
514 struct spa_rectangle defsize = SPA_RECTANGLE(quint32(streamInfo.rect.width()), quint32(streamInfo.rect.height()));
515 struct spa_rectangle maxsize = SPA_RECTANGLE(4096, 4096);
516 struct spa_rectangle minsize = SPA_RECTANGLE(1,1);
519 struct spa_fraction maxrate = SPA_FRACTION(1000, 1);
520 struct spa_fraction minrate = SPA_FRACTION(0, 1);
521 auto rate = rateFromFps(frameRate());
523 std::array<
const struct spa_pod *, 1> params{
static_cast<
const spa_pod*>(spa_pod_builder_add_object(
525 SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
526 SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
527 SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
528 SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(6,
529 SPA_VIDEO_FORMAT_RGB,
530 SPA_VIDEO_FORMAT_BGR,
531 SPA_VIDEO_FORMAT_RGBA,
532 SPA_VIDEO_FORMAT_BGRA,
533 SPA_VIDEO_FORMAT_RGBx,
534 SPA_VIDEO_FORMAT_BGRx),
535 SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(
536 &defsize, &minsize, &maxsize),
537 SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(
538 &rate.frac, &minrate, &maxrate),
539 SPA_FORMAT_VIDEO_maxFramerate, SPA_POD_Fraction(&rate.frac))
543 const int connectErr = pw_stream_connect(
544 m_stream.get(), PW_DIRECTION_INPUT, streamInfo.nodeId,
545 static_cast<pw_stream_flags>(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS),
547 if (connectErr != 0) {
550 updateError(QPlatformSurfaceCapture::Error::InternalError,
551 u"QPipeWireCaptureHelper failed at pw_stream_connect()."_s);
560 std::unique_lock locker(m_pwInstance->eventLoopLock());
561 m_ignoreStateChange =
true;
562 pw_stream_disconnect(m_stream.get());
564 m_ignoreStateChange =
false;
566 if (waitForStreamEnd) {
567 while (m_streamState > PW_STREAM_STATE_UNCONNECTED && !m_err) {
568 if (m_pwInstance->pwEventLoop().wait_for(1s) != 0)
580 m_pwInstance->pwEventLoop().signal(
false);
588 m_streamState = state;
591 case PW_STREAM_STATE_UNCONNECTED:
592 signalLoop(
false,
true);
594 case PW_STREAM_STATE_PAUSED:
595 signalLoop(
false,
false);
597 case PW_STREAM_STATE_STREAMING:
598 signalLoop(
false,
false);
606 struct pw_buffer *pwBuffer = pw_stream_dequeue_buffer(m_stream.get());
608 updateError(QPlatformSurfaceCapture::Error::InternalError,
609 u"Out of buffers in pipewire stream dequeue."_s);
613 struct spa_buffer *buf = pwBuffer->buffer;
614 void *sdata = buf->datas[0].data;
618 int sstride = buf->datas[0].chunk->stride;
620 sstride =
int(buf->datas[0].chunk->size / m_size.height());
621 qsizetype size = buf->datas[0].chunk->size;
623 if (m_videoFrameFormat.frameSize() != m_size || m_videoFrameFormat.pixelFormat() != m_pixelFormat)
624 m_videoFrameFormat = QVideoFrameFormat(m_size, m_pixelFormat);
626 m_currentFrame = QVideoFramePrivate::createFrame(
627 std::make_unique<QMemoryVideoBuffer>(QByteArray(
static_cast<
const char *>(sdata), size), sstride),
629 qCDebug(qLcPipeWireCaptureMore) <<
"got a frame of size " << buf->datas[0].chunk->size;
631 pw_stream_queue_buffer(m_stream.get(), pwBuffer);
633 signalLoop(
true,
false);
638 Q_ASSERT(m_pwInstance->hasScreenCastPortal());
641 destroyStream(
false);
643 m_pwInstance->runWithEventLoopLock([&] {
648 unsubscribeFromRequestResponse();
656 if (m_sessionHandle.path().isEmpty())
659 if (m_sessionInterface) {
660 m_sessionInterface->connection().disconnect(
661 u"org.freedesktop.portal.Desktop"_s, m_sessionHandle.path(),
662 u"org.freedesktop.portal.Session"_s, u"Closed"_s,
this, SLOT(sessionClosed()));
663 m_sessionInterface->asyncCall(u"Close"_s);
664 m_sessionInterface.reset();
667 m_sessionHandle = {};
672 if (m_sessionHandle.path().isEmpty())
675 qCWarning(qLcPipeWireCapture) <<
"org.freedesktop.portal.Session was closed externally";
676 m_sessionHandle = {};
677 m_sessionInterface.reset();
678 updateError(QPlatformSurfaceCapture::Error::InternalError,
679 u"PipeWire screen capture session was closed"_s);
685 if (param ==
nullptr || id != SPA_PARAM_Format)
688 if (spa_format_parse(param, &m_format.media_type, &m_format.media_subtype) < 0)
691 if (m_format.media_type != SPA_MEDIA_TYPE_video
692 || m_format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
695 if (spa_format_video_raw_parse(param, &m_format.info.raw) < 0)
698 qCDebug(qLcPipeWireCapture) <<
"got video format:";
699 qCDebug(qLcPipeWireCapture) <<
" format: " << m_format.info.raw.format
700 <<
" (" << spa_debug_type_find_name(spa_type_video_format, m_format.info.raw.format) <<
")";
701 qCDebug(qLcPipeWireCapture) <<
" size: " << toQSize(m_format.info.raw.size);
702 qCDebug(qLcPipeWireCapture) <<
" framerate: " << m_format.info.raw.framerate.num
703 <<
" / " << m_format.info.raw.framerate.denom;
705 m_size = toQSize(m_format.info.raw.size);
706 m_pixelFormat = toQtPixelFormat(m_format.info.raw.format);
707 qCDebug(qLcPipeWireCapture) <<
"m_pixelFormat=" << m_pixelFormat;
void finalizeGrabbingContext() override
bool open(QDBusUnixFileDescriptor)
QVideoFrameFormat frameFormat() const
void initializeGrabbingContext() override
QVideoFrame grabFrame() override
~QPipeWireCaptureHelper() override
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)