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/qpipewire_portal_p.h>
11#include <QtMultimedia/private/qvideoframe_p.h>
12#include <QtCore/qloggingcategory.h>
13#include <QtCore/qvariantmap.h>
14#include <QtDBus/qdbusconnection.h>
15#include <QtDBus/qdbusinterface.h>
16#include <QtDBus/qdbusmessage.h>
17#include <QtDBus/qdbusreply.h>
18#include <QtDBus/qdbusunixfiledescriptor.h>
20#include <spa/debug/types.h>
21#include <spa/param/video/format-utils.h>
22#include <spa/param/video/type-info.h>
28QT_WARNING_DISABLE_CLANG(
"-Wunused-value")
32using namespace Qt::StringLiterals;
33using namespace std::chrono_literals;
34using namespace std::string_view_literals;
43 : QSurfaceCaptureGrabber(CreateGrabbingThread),
44 m_pwInstance(std::move(instance))
46 Q_ASSERT(m_pwInstance);
48 addFrameCallback(&capture, &QPipeWireCapture::newVideoFrame);
49 connect(
this, &QSurfaceCaptureGrabber::errorUpdated, &capture, &QPipeWireCapture::updateError);
59 return std::move(m_currentFrame);
64 QSurfaceCaptureGrabber::initializeGrabbingContext();
65 if (m_state == NoState)
71 unsubscribeFromRequestResponse();
72 m_operationState = NoOperation;
74 if (m_state != NoState)
76 QSurfaceCaptureGrabber::finalizeGrabbingContext();
81 return m_videoFrameFormat;
86 qCDebug(qLcPipeWireCapture) << Q_FUNC_INFO <<
"result=" << result <<
"map=" << map;
89 unsubscribeFromRequestResponse();
92 m_operationState = NoOperation;
93 qWarning() <<
"Failed to capture screen via pipewire, perhaps because user cancelled the "
98 switch (m_operationState) {
100 selectSources(QDBusObjectPath{ map[u"session_handle"_s].toString() });
106 updateStreams(map[u"streams"_s].value<QDBusArgument>());
107 openPipeWireRemote();
108 m_operationState = NoOperation;
111 case OpenPipeWireRemote:
112 m_operationState = NoOperation;
121 Q_ASSERT(m_pendingRequestPath.isEmpty());
123 QDBusConnection bus = QDBusConnection::sessionBus();
125 QString path = QtPipeWire::portalRequestPath(bus, handleToken);
127 const bool ok = bus.connect(u"org.freedesktop.portal.Desktop"_s, path,
128 u"org.freedesktop.portal.Request"_s, u"Response"_s,
this,
129 SLOT(gotRequestResponse(uint, QVariantMap)));
131 updateError(QPlatformSurfaceCapture::Error::InternalError,
132 u"Failed to subscribe to org.freedesktop.portal.Request response for "_s
137 m_pendingRequestPath = std::move(path);
143 if (m_pendingRequestPath.isEmpty())
146 QDBusConnection::sessionBus().disconnect(u"org.freedesktop.portal.Desktop"_s,
147 m_pendingRequestPath,
148 u"org.freedesktop.portal.Request"_s, u"Response"_s,
149 this, SLOT(gotRequestResponse(uint, QVariantMap)));
151 m_pendingRequestPath.clear();
156 Q_ASSERT(m_pwInstance->hasScreenCastPortal());
158 m_operationState = NoOperation;
160 if (!m_screenCastInterface) {
161 m_screenCastInterface = std::make_unique<QDBusInterface>(
162 u"org.freedesktop.portal.Desktop"_s, u"/org/freedesktop/portal/desktop"_s,
163 u"org.freedesktop.portal.ScreenCast"_s, QDBusConnection::sessionBus());
170 if (!m_screenCastInterface)
173 const QString handleToken = allocatePortalRequestToken();
174 if (!subscribeToRequestResponse(handleToken))
178 { u"handle_token"_s, handleToken },
179 { u"session_handle_token"_s, allocatePortalRequestToken() },
181 QDBusMessage reply = m_screenCastInterface->call(u"CreateSession"_s, options);
182 if (!reply.errorMessage().isEmpty()) {
183 unsubscribeFromRequestResponse();
184 updateError(QPlatformSurfaceCapture::Error::InternalError,
185 u"Failed to create session for org.freedesktop.portal.ScreenCast. Error: "_s
186 + reply.errorName() + u": "_s + reply.errorMessage());
190 m_operationState = CreateSession;
195 if (!m_screenCastInterface)
198 m_sessionHandle = sessionHandle;
199 m_sessionInterface = std::make_unique<QDBusInterface>(
200 u"org.freedesktop.portal.Desktop"_s, sessionHandle.path(),
201 u"org.freedesktop.portal.Session"_s, QDBusConnection::sessionBus());
202 m_sessionInterface->connection().connect(
203 u"org.freedesktop.portal.Desktop"_s, sessionHandle.path(),
204 u"org.freedesktop.portal.Session"_s, u"Closed"_s,
this, SLOT(sessionClosed()));
206 const QString handleToken = allocatePortalRequestToken();
207 if (!subscribeToRequestResponse(handleToken))
211 { u"handle_token"_s, handleToken },
212 { u"types"_s, (uint)1 },
213 { u"multiple"_s,
false },
214 { u"cursor_mode"_s, (uint)1 },
215 { u"persist_mode"_s, (uint)0 },
217 QDBusMessage reply = m_screenCastInterface->call(u"SelectSources"_s, sessionHandle, options);
218 if (!reply.errorMessage().isEmpty()) {
219 unsubscribeFromRequestResponse();
220 updateError(QPlatformSurfaceCapture::Error::InternalError,
221 u"Failed to select sources for org.freedesktop.portal.ScreenCast. Error: "_s
222 + reply.errorName() + u": "_s + reply.errorMessage());
226 m_operationState = SelectSources;
231 if (!m_screenCastInterface)
234 const QString handleToken = allocatePortalRequestToken();
235 if (!subscribeToRequestResponse(handleToken))
239 { u"handle_token"_s, handleToken },
242 const QString parentWindow = QtPipeWire::portalParentWindowIdentifier();
243 QDBusMessage reply = m_screenCastInterface->call(
"Start"_L1, QDBusObjectPath(m_sessionHandle),
244 parentWindow, options);
245 if (!reply.errorMessage().isEmpty()) {
246 unsubscribeFromRequestResponse();
247 updateError(QPlatformSurfaceCapture::Error::InternalError,
248 u"Failed to start stream for org.freedesktop.portal.ScreenCast. Error: "_s
249 + reply.errorName() + u": "_s + reply.errorMessage());
253 m_operationState = StartStream;
260 streamsInfo.beginStructure();
261 streamsInfo.beginArray();
263 while (!streamsInfo.atEnd()) {
265 streamsInfo >> nodeId;
266 QMap<QString, QVariant> properties;
267 streamsInfo >> properties;
271 if (properties.contains(u"position"_s)) {
272 const auto position = properties[u"position"_s].value<QDBusArgument>();
273 position.beginStructure();
276 position.endStructure();
281 if (properties.contains(u"size"_s)) {
282 const auto size = properties[u"size"_s].value<QDBusArgument>();
283 size.beginStructure();
290 if (properties.contains(u"source_type"_s))
291 sourceType = properties[u"source_type"_s].toUInt();
293 StreamInfo streamInfo;
294 streamInfo.nodeId = nodeId;
295 streamInfo.sourceType = sourceType;
296 streamInfo.rect = {x, y, width, height};
297 m_streams << streamInfo;
300 streamsInfo.endArray();
301 streamsInfo.endStructure();
307 if (!m_screenCastInterface)
311 QDBusReply<QDBusUnixFileDescriptor> reply =
312 m_screenCastInterface->call(u"OpenPipeWireRemote"_s, m_sessionHandle, options);
313 if (!reply.isValid()) {
315 QPlatformSurfaceCapture::Error::InternalError,
316 u"Failed to open pipewire remote for org.freedesktop.portal.ScreenCast. Error: name="_s
317 + reply.error().name() + u", message="_s + reply.error().message());
321 bool ok =
open(reply.value()
);
322 qCDebug(qLcPipeWireCapture) <<
"open() result=" << ok;
324 updateError(QPlatformSurfaceCapture::Error::InternalError,
325 u"Failed to open pipewire remote file descriptor"_s);
329 m_operationState = OpenPipeWireRemote;
334 if (m_streams.isEmpty())
337 Q_ASSERT(m_pwInstance->hasScreenCastPortal());
339 std::unique_lock pwLock = m_pwInstance->eventLoopLock();
340 auto connection = m_pwInstance->connectToPortal(portalFd.takeFileDescriptor());
344 updateError(QPlatformSurfaceCapture::Error::InternalError,
345 u"QPipeWireCaptureHelper failed at pw_context_connect_fd()."_s);
348 m_core = std::move(*connection);
350 m_registry = PwRegistryHandle{
351 pw_core_get_registry(m_core.get(), PW_VERSION_REGISTRY, 0),
356 updateError(QPlatformSurfaceCapture::Error::InternalError,
357 u"QPipeWireCaptureHelper failed at pw_core_get_registry()."_s);
361 struct RegistryCallbackContext
366 spa_hook registryListener = {};
367 const pw_registry_events registryEvents{
368 .version = PW_VERSION_REGISTRY_EVENTS,
370 [](
void *data, uint32_t , uint32_t ,
const char *type,
371 uint32_t ,
const spa_dict *props) {
372 if (type != std::string_view(PW_TYPE_INTERFACE_Node))
375 const char *media_class = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS);
379 if (media_class !=
"Stream/Output/Video"sv && media_class !=
"Video/Source"sv)
381 auto *registryContext =
reinterpret_cast<RegistryCallbackContext *>(data);
382 registryContext->hasSource =
true;
384 .global_remove =
nullptr,
387 RegistryCallbackContext registryContext;
388 pw_registry_add_listener(m_registry.get(), ®istryListener, ®istryEvents,
391 CoreEventDoneListener doneListener;
392 bool initDone =
false;
393 doneListener.asyncWait(m_core.get(), [&] {
395 m_pwInstance->pwEventLoop().signal(
false);
399 if (m_pwInstance->pwEventLoop().wait_for(2s) != 0)
403 spa_hook_remove(®istryListener);
405 if (registryContext.hasSource)
408 return initDone && registryContext.hasSource;
413 static const pw_stream_events streamEvents = {
414 .version = PW_VERSION_STREAM_EVENTS,
415 .destroy = [](
void *data) {
418 .state_changed = [](
void *data, pw_stream_state old, pw_stream_state state,
const char *error) {
421 .control_info = [](
void *data, uint32_t id,
const struct pw_stream_control *control) {
426 .io_changed = [](
void *data, uint32_t id,
void *area, uint32_t size) {
432 .param_changed = [](
void *data, uint32_t id,
const struct spa_pod *param) {
435 .add_buffer = [](
void *data,
struct pw_buffer *buffer) {
439 .remove_buffer = [](
void *data,
struct pw_buffer *buffer) {
443 .process = [](
void *data) {
446 .drained = [](
void *data) {
449#if PW_VERSION_STREAM_EVENTS
>= 1
450 .command = [](
void *data,
const struct spa_command *command) {
455#if PW_VERSION_STREAM_EVENTS
>= 2
456 .trigger_done = [](
void *data) {
464 auto streamInfo = m_streams[0];
465 const struct std::array<spa_dict_item, 3> items{
466 SPA_DICT_ITEM_INIT(PW_KEY_MEDIA_TYPE,
"Video"),
467 SPA_DICT_ITEM_INIT(PW_KEY_MEDIA_CATEGORY,
"Capture"),
468 SPA_DICT_ITEM_INIT(PW_KEY_MEDIA_ROLE,
"Screen"),
470 const struct spa_dict info = SPA_DICT_INIT(items.data(), items.size());
471 auto *props = pw_properties_new_dict(&info);
473 std::unique_lock locker = m_pwInstance->eventLoopLock();
475 m_stream = PwStreamHandle{
476 pw_stream_new(m_core.get(),
"video-capture", props),
481 updateError(QPlatformSurfaceCapture::Error::InternalError,
482 u"QPipeWireCaptureHelper failed at pw_stream_new()."_s);
486 m_streamListener = {};
487 pw_stream_add_listener(m_stream.get(), &m_streamListener, &streamEvents,
this);
491 QT_WARNING_DISABLE_GCC(
"-Wmissing-field-initializers")
492 QT_WARNING_DISABLE_CLANG(
"-Wmissing-field-initializers")
494 std::array<uint8_t, 4096> buffer;
495 struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(buffer.data(),
sizeof(buffer));
496 struct spa_rectangle defsize = SPA_RECTANGLE(quint32(streamInfo.rect.width()), quint32(streamInfo.rect.height()));
497 struct spa_rectangle maxsize = SPA_RECTANGLE(4096, 4096);
498 struct spa_rectangle minsize = SPA_RECTANGLE(1,1);
501 struct spa_fraction maxrate = SPA_FRACTION(1000, 1);
502 struct spa_fraction minrate = SPA_FRACTION(0, 1);
503 auto rate = rateFromFps(activeFrameRate());
505 std::array<
const struct spa_pod *, 1> params{
static_cast<
const spa_pod*>(spa_pod_builder_add_object(
507 SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
508 SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
509 SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
510 SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(6,
511 SPA_VIDEO_FORMAT_RGB,
512 SPA_VIDEO_FORMAT_BGR,
513 SPA_VIDEO_FORMAT_RGBA,
514 SPA_VIDEO_FORMAT_BGRA,
515 SPA_VIDEO_FORMAT_RGBx,
516 SPA_VIDEO_FORMAT_BGRx),
517 SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(
518 &defsize, &minsize, &maxsize),
519 SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(
520 &rate.frac, &minrate, &maxrate),
521 SPA_FORMAT_VIDEO_maxFramerate, SPA_POD_Fraction(&rate.frac))
525 const int connectErr = pw_stream_connect(
526 m_stream.get(), PW_DIRECTION_INPUT, streamInfo.nodeId,
527 static_cast<pw_stream_flags>(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS),
529 if (connectErr != 0) {
532 updateError(QPlatformSurfaceCapture::Error::InternalError,
533 u"QPipeWireCaptureHelper failed at pw_stream_connect()."_s);
542 std::unique_lock locker(m_pwInstance->eventLoopLock());
543 m_ignoreStateChange =
true;
544 pw_stream_disconnect(m_stream.get());
546 m_ignoreStateChange =
false;
548 if (waitForStreamEnd) {
549 while (m_streamState > PW_STREAM_STATE_UNCONNECTED && !m_err) {
550 if (m_pwInstance->pwEventLoop().wait_for(1s) != 0)
562 m_pwInstance->pwEventLoop().signal(
false);
570 m_streamState = state;
573 case PW_STREAM_STATE_UNCONNECTED:
574 signalLoop(
false,
true);
576 case PW_STREAM_STATE_PAUSED:
577 signalLoop(
false,
false);
579 case PW_STREAM_STATE_STREAMING:
580 signalLoop(
false,
false);
588 struct pw_buffer *pwBuffer = pw_stream_dequeue_buffer(m_stream.get());
590 updateError(QPlatformSurfaceCapture::Error::InternalError,
591 u"Out of buffers in pipewire stream dequeue."_s);
595 struct spa_buffer *buf = pwBuffer->buffer;
596 void *sdata = buf->datas[0].data;
600 int sstride = buf->datas[0].chunk->stride;
602 sstride =
int(buf->datas[0].chunk->size / m_size.height());
603 qsizetype size = buf->datas[0].chunk->size;
605 if (m_videoFrameFormat.frameSize() != m_size || m_videoFrameFormat.pixelFormat() != m_pixelFormat)
606 m_videoFrameFormat = QVideoFrameFormat(m_size, m_pixelFormat);
608 m_currentFrame = QVideoFramePrivate::createFrame(
609 std::make_unique<QMemoryVideoBuffer>(QByteArray(
static_cast<
const char *>(sdata), size), sstride),
611 qCDebug(qLcPipeWireCaptureMore) <<
"got a frame of size " << buf->datas[0].chunk->size;
613 pw_stream_queue_buffer(m_stream.get(), pwBuffer);
615 signalLoop(
true,
false);
620 Q_ASSERT(m_pwInstance->hasScreenCastPortal());
623 destroyStream(
false);
625 m_pwInstance->runWithEventLoopLock([&] {
630 unsubscribeFromRequestResponse();
638 if (m_sessionHandle.path().isEmpty())
641 if (m_sessionInterface) {
642 m_sessionInterface->connection().disconnect(
643 u"org.freedesktop.portal.Desktop"_s, m_sessionHandle.path(),
644 u"org.freedesktop.portal.Session"_s, u"Closed"_s,
this, SLOT(sessionClosed()));
645 m_sessionInterface->asyncCall(u"Close"_s);
646 m_sessionInterface.reset();
649 m_sessionHandle = {};
654 if (m_sessionHandle.path().isEmpty())
657 qCWarning(qLcPipeWireCapture) <<
"org.freedesktop.portal.Session was closed externally";
658 m_sessionHandle = {};
659 m_sessionInterface.reset();
660 updateError(QPlatformSurfaceCapture::Error::InternalError,
661 u"PipeWire screen capture session was closed"_s);
667 if (param ==
nullptr || id != SPA_PARAM_Format)
670 if (spa_format_parse(param, &m_format.media_type, &m_format.media_subtype) < 0)
673 if (m_format.media_type != SPA_MEDIA_TYPE_video
674 || m_format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
677 if (spa_format_video_raw_parse(param, &m_format.info.raw) < 0)
680 qCDebug(qLcPipeWireCapture) <<
"got video format:";
681 qCDebug(qLcPipeWireCapture) <<
" format: " << m_format.info.raw.format
682 <<
" (" << spa_debug_type_find_name(spa_type_video_format, m_format.info.raw.format) <<
")";
683 qCDebug(qLcPipeWireCapture) <<
" size: " << toQSize(m_format.info.raw.size);
684 qCDebug(qLcPipeWireCapture) <<
" framerate: " << m_format.info.raw.framerate.num
685 <<
" / " << m_format.info.raw.framerate.denom;
687 m_size = toQSize(m_format.info.raw.size);
688 m_pixelFormat = toQtPixelFormat(m_format.info.raw.format);
689 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)