Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpipewire_videostream.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtMultimedia/private/qmemoryvideobuffer_p.h>
7#include <QtMultimedia/private/qpipewire_propertydict_p.h>
8#include <QtMultimedia/private/qpipewire_videoformat_support_p.h>
9#include <QtMultimedia/private/qvideoframe_p.h>
10#include <QtCore/qloggingcategory.h>
11
12#include <spa/param/video/format-utils.h>
13#include <spa/pod/builder.h>
14
15#if __has_include(<spa/param/buffers.h>)
16# include <spa/param/buffers.h>
17#else
18# include <QtMultimedia/private/qpipewire_spa_compat_p.h>
19#endif
20
22# include <QtMultimedia/private/qcaptureslotregistry_p.h>
23# include <QtMultimedia/private/qdmabufallocator_p.h>
24# include <QtMultimedia/private/qdmabufsource_p.h>
25# include <QtMultimedia/private/qdmabufvideobuffer_p.h>
26# include <QtMultimedia/private/qmaterializingvideobuffer_p.h>
27# include <QtMultimedia/private/qmultimedia_dmabuf_support_p.h>
28# include <QtMultimedia/private/qmultimedia_ranges_p.h>
29# include <QtMultimedia/private/qvideotexturehelper_p.h>
30#endif
31
32#include <algorithm>
33#include <array>
34#ifndef QT_NO_DEBUG
35# include <atomic>
36#endif
38# include <fcntl.h>
39#endif
40#include <iterator>
41#include <optional>
42
43QT_BEGIN_NAMESPACE
44
45namespace QtPipeWire {
46
47using namespace Qt::StringLiterals;
48
49Q_STATIC_LOGGING_CATEGORY(lcPipewireCameraStream, "qt.multimedia.pipewire.camera.stream")
50
51namespace {
52
53// The raw formats a camera realistically produces, offered when the caller did
54// not ask for one in particular.
55constexpr std::array cameraPixelFormats{
56 SPA_VIDEO_FORMAT_NV12, SPA_VIDEO_FORMAT_YUY2, SPA_VIDEO_FORMAT_UYVY,
57 SPA_VIDEO_FORMAT_I420, SPA_VIDEO_FORMAT_RGB, SPA_VIDEO_FORMAT_BGR,
58 SPA_VIDEO_FORMAT_RGBA, SPA_VIDEO_FORMAT_BGRA, SPA_VIDEO_FORMAT_GRAY8,
59};
60
61enum class CameraMemory : uint8_t { Auto, MemPtr, MemFd, DmaBuf };
62
63CameraMemory cameraMemoryFromEnvironment()
64{
65 const QByteArray value = qgetenv("QT_PIPEWIRE_CAMERA_MEMORY");
66 if (value.isEmpty() || value == "auto")
67 return CameraMemory::Auto;
68 if (value == "memptr")
69 return CameraMemory::MemPtr;
70 if (value == "memfd")
71 return CameraMemory::MemFd;
72 if (value == "dmabuf") {
74 return CameraMemory::DmaBuf;
75#else
76 qCWarning(lcPipewireCameraStream)
77 << "QT_PIPEWIRE_CAMERA_MEMORY=dmabuf needs PipeWire 0.3.76 or newer, using auto";
78 return CameraMemory::Auto;
79#endif
80 }
81
82 qCWarning(lcPipewireCameraStream)
83 << "unknown QT_PIPEWIRE_CAMERA_MEMORY value" << value << ", using auto";
84 return CameraMemory::Auto;
85}
86
87// The buffer type an override insists on, or nullopt when any type will do.
88[[maybe_unused]] std::optional<spa_data_type> forcedDataType(CameraMemory memory)
89{
90 switch (memory) {
91 case CameraMemory::Auto:
92 return std::nullopt;
93 case CameraMemory::MemPtr:
94 return SPA_DATA_MemPtr;
95 case CameraMemory::MemFd:
96 return SPA_DATA_MemFd;
97 case CameraMemory::DmaBuf:
98 return SPA_DATA_DmaBuf;
99 }
100 Q_UNREACHABLE_RETURN(std::nullopt);
101}
102
103// pts is in nanoseconds since an arbitrary epoch.
104void setStartTime(QVideoFrame &frame, const spa_buffer &buffer,
105 std::optional<std::chrono::microseconds> &firstFrameTime)
106{
107 using namespace std::chrono;
108
109 const auto *header = static_cast<const spa_meta_header *>(
110 spa_buffer_find_meta_data(&buffer, SPA_META_Header, sizeof(spa_meta_header)));
111 const microseconds pts = [&] {
112 if (header && header->pts > 0) {
113 // pts is in nanoseconds since an arbitrary epoch.
114 return round<microseconds>(nanoseconds{ header->pts });
115 } else {
116 return round<microseconds>(steady_clock::now().time_since_epoch());
117 }
118 }();
119
120 if (!firstFrameTime)
121 firstFrameTime = pts;
122 frame.setStartTime(qint64((pts - *firstFrameTime).count()));
123}
124
125// Copies the frame out of the buffer. Each frame is copied eagerly, so the
126// buffer can be requeued as soon as this returns.
127std::optional<QByteArray> copyFrameData(const spa_buffer &buffer,
128 [[maybe_unused]] bool dmaBufReadable)
129{
130 const spa_data &data = buffer.datas[0];
131
133 if (data.type == SPA_DATA_DmaBuf) {
134 // A single plane, since each plane would otherwise need its own stride. A
135 // compressed frame is a byte stream, so only raw frames depend on the layout.
136 if (buffer.n_datas != 1 || !dmaBufReadable) {
137 qCDebug(lcPipewireCameraStream)
138 << "dropping DMABUF frame: needs one plane in a linear modifier";
139 return std::nullopt;
140 }
141
142 if (!data.chunk || data.maxsize == 0)
143 return std::nullopt;
144 const size_t chunkOffset = data.chunk->offset % data.maxsize;
145 if (chunkOffset + data.chunk->size > data.maxsize) {
146 qCDebug(lcPipewireCameraStream) << "dropping DMABUF frame: chunk exceeds the buffer";
147 return std::nullopt;
148 }
149 return QtMultimediaPrivate::copyDmaBufData(
150 data.fd, off_t(data.mapoffset) + off_t(chunkOffset), int(data.chunk->size),
151 size_t(data.mapoffset) + data.maxsize);
152 }
153#endif
154
155 if (!data.data || !data.chunk)
156 return std::nullopt;
157 return QByteArray(static_cast<const char *>(data.data) + data.chunk->offset,
158 qsizetype(data.chunk->size));
159}
160
161} // namespace
162
164namespace {
165
166// PipeWire links at most link.max-buffers, typically 16 (libcamera nodes are limited to 4).
167constexpr std::size_t kDmaBufBufferCount = 8;
168constexpr std::size_t kDmaBufMinFreeBuffers = 2;
169constexpr std::size_t kNodeDmaBufCapacity = 32;
170
171} // namespace
172
173// Owns the DMA-BUFs the camera node fills. A frame holding a pw_buffer keeps it out of the
174// stream's queue; releasing the slot queues it back to the node.
178{
179public:
181
189 {
191 }
192
193 void recycleSlot(pw_buffer *buffer) const
194 {
195# ifndef QT_NO_DEBUG
197# endif
200 });
201 }
202
204 {
205# ifndef QT_NO_DEBUG
206 m_leased[index].store(true);
207# endif
208 }
209
210 // Points the buffer at its DMA-BUF. PipeWire re-adds a buffer it already knows when it
211 // renegotiates, so a buffer keeps the DMA-BUF it was first given.
213 {
214# ifndef QT_NO_DEBUG
215 if (pwBuffer->user_data) {
217 && "PipeWire re-added a buffer that a frame still holds");
218 }
219# endif
220 if (!pwBuffer->user_data) {
222 // One past the index, so that a null user_data means the buffer is unbound.
223 pwBuffer->user_data = reinterpret_cast<void *>(++m_bound);
224 }
225
227
231 data.fd = buf.fd();
232 data.mapoffset = 0;
234 data.data = nullptr;
235 }
236
237 static std::size_t bufferIndex(const pw_buffer *pwBuffer)
238 {
239 return reinterpret_cast<std::size_t>(pwBuffer->user_data) - 1;
240 }
241
242 std::shared_ptr<QDmaBuf> buffer(std::size_t index) const { return m_buffers[index]; }
243 int stride() const { return m_stride; }
244
245private:
247# ifndef QT_NO_DEBUG
248 mutable std::array<std::atomic<bool>, kDmaBufBufferCount> m_leased{};
249# endif
250 const int m_stride;
252 pw_stream *const m_stream;
253 std::size_t m_bound = 0;
254};
255
256// Hands the DMA-BUFs that the camera node allocated to frames. A frame holding a slot keeps its
257// buffer out of the stream's queue; releasing the slot queues it back to the node.
261{
262public:
267 {
268 }
269
270 void recycleSlot(NodeDmaBuf slot) const
271 {
273 // PipeWire frees a buffer once it is removed, so a stale one must not be queued.
274 if (m_valid)
276 });
277 }
278
279 void invalidate()
280 {
282 m_valid = false;
283 });
284 }
285
286private:
288 pw_stream *const m_stream;
289 bool m_valid = true;
290};
291
292// Wraps a DMA-BUF holding one frame in a video buffer that holds the slot until the frame is
293// released. Returns null when the format has no zero-copy layout at this stride.
294template <typename Registry>
299{
300 using namespace QtMultimediaPrivate;
301
304 if (!layout)
305 return nullptr;
306
307 // One DmaBufPlane per layout plane, all within the same DMA-BUF fd.
310 for (qsizetype plane = 0; plane < layout->size(); ++plane) {
313 buf->fd(),
317 });
318 }
319 const QSpan<const DmaBufPlane> planeSpan(planes.data(), planes.size());
320
322
325 // QDmaBufVideoBuffer args:
327}
328
329namespace {
330
331// Drops the registry, so that frames still holding its slots stop requeueing their buffers.
333{
334 if (!registry)
335 return;
337 registry.reset();
338}
339
340} // namespace
341#endif
342
356
358{
360 // Outside the loop lock: waits for releases that take it to requeue buffers.
363 if (m_nodeRegistry)
365#endif
366
367 if (!m_stream)
368 return;
369
373 m_stream = {};
374 });
375}
376
378{
380 // QTBUG-129587: libpipewire 1.2.5 grew fields we do not set
381 QT_WARNING_DISABLE_GCC("-Wmissing-field-initializers")
382 QT_WARNING_DISABLE_CLANG("-Wmissing-field-initializers")
383 // clang-format off
384 static constexpr pw_stream_events events = {
386 .state_changed = [](void *data, pw_stream_state o, pw_stream_state s, const char *err) {
387 reinterpret_cast<QPipeWireVideoStream *>(data)->onStateChanged(o, s, err);
388 },
389 .param_changed = [](void *data, uint32_t id, const spa_pod *param) {
390 reinterpret_cast<QPipeWireVideoStream *>(data)->onParamChanged(id, param);
391 },
393 .add_buffer = [](void *data, pw_buffer *buffer) {
394 reinterpret_cast<QPipeWireVideoStream *>(data)->onAddBuffer(buffer);
395 },
396 .remove_buffer = [](void *data, pw_buffer *buffer) {
397 reinterpret_cast<QPipeWireVideoStream *>(data)->removeNodeDmaBuf(buffer);
398 },
399#endif
400 .process = [](void *data) {
401 reinterpret_cast<QPipeWireVideoStream *>(data)->onProcess();
402 },
403 };
404 // clang-format on
406
407 return events;
408}
409
410namespace {
411
412// How the EnumFormat param restricts the DRM modifier to the linear layout, the only one a
413// DMABUF buffer can be read through. Optional lets a node omit the modifier altogether.
414enum class LinearModifier : uint8_t { Off, Optional, Mandatory };
415
416// The EnumFormat param for the requested pixel format and size.
417const spa_pod *buildFormatParam(std::array<uint8_t, 1024> &storage,
418 QVideoFrameFormat::PixelFormat requestedPixelFormat,
419 QSize requestedSize, qreal requestedFrameRate,
420 [[maybe_unused]] LinearModifier linearModifier)
421{
422 spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage.data(), uint32_t(storage.size()));
423
424 spa_pod_frame frame{};
425 spa_pod_builder_push_object(&builder, &frame, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
426 spa_pod_builder_add(&builder, SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video), 0);
427
428 switch (requestedPixelFormat) {
429 case QVideoFrameFormat::Format_Jpeg: {
430 spa_pod_builder_add(&builder, SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_mjpg),
431 0);
432 break;
433 }
434 default: {
435 spa_pod_builder_add(&builder, SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
436 0);
437
438 const spa_video_format requestedPixelFormatSpa = toSpaVideoFormat(requestedPixelFormat);
439 switch (requestedPixelFormatSpa) {
440 case SPA_VIDEO_FORMAT_UNKNOWN: {
441 // advertise all alternatives, letting PipeWire pick one of the camera's supported formats
442 spa_pod_builder_prop(&builder, SPA_FORMAT_VIDEO_format, 0);
443 spa_pod_frame choice{};
444 spa_pod_builder_push_choice(&builder, &choice, SPA_CHOICE_Enum, 0);
445 // The first entry is both the default and one of the alternatives.
446 spa_pod_builder_id(&builder, cameraPixelFormats.front());
447 for (spa_video_format format : cameraPixelFormats)
448 spa_pod_builder_id(&builder, format);
449 spa_pod_builder_pop(&builder, &choice);
450 break;
451 }
452
453 default: {
454 spa_pod_builder_add(&builder, SPA_FORMAT_VIDEO_format,
455 SPA_POD_Id(requestedPixelFormatSpa), 0);
456 break;
457 }
458 }
459 break;
460 }
461 }
462
463 if (!requestedSize.isEmpty()) {
464 const spa_rectangle exactSize = toSpaRectangle(requestedSize);
465 spa_pod_builder_add(&builder, SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&exactSize), 0);
466 } else {
467 constexpr spa_rectangle minSize{ .width = 1, .height = 1 };
468 constexpr spa_rectangle maxSize{ .width = 8192, .height = 8192 };
469
470 spa_pod_builder_add(&builder, SPA_FORMAT_VIDEO_size,
471 SPA_POD_CHOICE_RANGE_Rectangle(&maxSize, &minSize, &maxSize), 0);
472 }
473
474 // Cameras are treated as variable rate with the requested rate as the
475 // upper bound
476 constexpr spa_fraction minRate{ .num = 0, .denom = 1 };
477 const FrameRate rate = rateFromFps(requestedFrameRate > 0. ? requestedFrameRate : 30.);
478 spa_pod_builder_add(&builder, SPA_FORMAT_VIDEO_framerate,
479 SPA_POD_CHOICE_RANGE_Fraction(&rate.frac, &minRate, &rate.frac), 0);
480
482 if (linearModifier != LinearModifier::Off) {
483 spa_pod_frame modifierChoice{};
484 spa_pod_builder_prop(
485 &builder, SPA_FORMAT_VIDEO_modifier,
486 linearModifier == LinearModifier::Mandatory ? SPA_POD_PROP_FLAG_MANDATORY : 0);
487 spa_pod_builder_push_choice(&builder, &modifierChoice, SPA_CHOICE_Enum, 0);
488 spa_pod_builder_long(&builder, int64_t(QtMultimediaPrivate::DrmFormatModifierLinear));
489 spa_pod_builder_pop(&builder, &modifierChoice);
490 }
491#endif
492
493 return static_cast<const spa_pod *>(spa_pod_builder_pop(&builder, &frame));
494}
495
496// The Buffers param, which restricts the memory types the stream accepts.
497const spa_pod *buildBuffersParam(std::array<uint8_t, 1024> &storage, BufferDataTypes dataTypes)
498{
499 spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage.data(), uint32_t(storage.size()));
500 return static_cast<const spa_pod *>(spa_pod_builder_add_object(
501 &builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers, SPA_PARAM_BUFFERS_dataType,
502 SPA_POD_CHOICE_FLAGS_Int(uint32_t(dataTypes.to_ulong()))));
503}
504
505bool offersDmaBuf(CameraMemory memory)
506{
508 && (memory == CameraMemory::Auto || memory == CameraMemory::DmaBuf);
509}
510
512// The Buffers param for stream-allocated DMA-BUFs. The stride is the one the camera node must
513// write frames with, so it is fixed here rather than read back from each frame.
514const spa_pod *buildDmaBufBuffersParam(std::array<uint8_t, 1024> &storage, int32_t size,
515 int32_t stride)
516{
517 spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage.data(), uint32_t(storage.size()));
518 return static_cast<const spa_pod *>(spa_pod_builder_add_object(
519 &builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers, SPA_PARAM_BUFFERS_buffers,
520 SPA_POD_Int(int32_t(kDmaBufBufferCount)), SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
521 SPA_PARAM_BUFFERS_size, SPA_POD_Int(size), SPA_PARAM_BUFFERS_stride,
522 SPA_POD_Int(stride), SPA_PARAM_BUFFERS_align, SPA_POD_Int(16),
523 SPA_PARAM_BUFFERS_dataType, SPA_POD_CHOICE_FLAGS_Int(1u << SPA_DATA_DmaBuf)));
524}
525
526#endif
527
528BufferDataTypes bufferDataTypes(CameraMemory memory)
529{
530 BufferDataTypes dataTypes;
531 if (memory == CameraMemory::Auto || memory == CameraMemory::MemPtr)
532 dataTypes.set(SPA_DATA_MemPtr);
533 if (memory == CameraMemory::Auto || memory == CameraMemory::MemFd)
534 dataTypes.set(SPA_DATA_MemFd);
535 if (offersDmaBuf(memory))
536 dataTypes.set(SPA_DATA_DmaBuf);
537 return dataTypes;
538}
539
540} // namespace
541
542// Memory type negotiation with the camera node has three steps:
543//
544// 1. connect(): the stream offers the formats it accepts, and the buffer memory
545// types it can receive, including DMABUF when it is supported.
546// 2. param_changed(Format): once the server has picked a format, the stream
547// decides whether it can read DMABUF frames in that format. If it cannot, it
548// withdraws DMABUF from its offer, and the server falls back to mapped memory.
549// 3. process(): frames are now delivered by the server.
550//
551// With stream-allocated DMA-BUFs, step 1 offers only linear DMABUF and sets
552// PW_STREAM_FLAG_ALLOC_BUFFERS. Step 2 allocates the buffers for the negotiated
553// size and sends them back as the Buffers param. The node then adds them, and
554// onAddBuffer() binds each one to its DMA-BUF.
555//
557{
559
561
562 if (!m_context.isConnected())
563 return false;
564
565 const spa_dict_item items[] = {
570 };
571
574
577 };
578 if (!m_stream) {
579 qCWarning(lcPipewireCameraStream) << "pw_stream_new failed";
580 return false;
581 }
582
583 m_streamListener = {};
585
587
589
591 std::array<uint8_t, 1024> formatStorage{};
593 std::array<const spa_pod *, 3> params{};
597 bool zeroCopy = false;
598
600 // PipeWire refuses to allocate the stream's DMA-BUFs for MJPEG, so compressed streams
601 // keep the server's buffers.
606 zeroCopy = true;
607 } else {
609 << "no DMA-BUF allocator, using server buffers:" << allocator.error();
610 }
611 }
612 if (zeroCopy) {
616
617 params[paramCount++] =
620 }
621#endif
622
623 if (!zeroCopy) {
624 if (offersDmaBuf(memory)) {
628 }
629 if (memory != CameraMemory::DmaBuf) {
630 params[paramCount++] =
633 }
635 }
636
639 if (status < 0) {
641 << "pw_stream_connect failed:" << make_error_code(-status).message();
643 m_stream = {};
644 return false;
645 }
646
647 return true;
648 });
649}
650
656
658 const char *error)
659{
660 qCDebug(lcPipewireCameraStream) << "stream state" << pw_stream_state_as_string(old) << "->"
662 << (error ? QString::fromUtf8(error) : QString());
663
666 : u"The PipeWire camera stream failed"_s);
667 }
668}
669
671{
672 if (!param || id != SPA_PARAM_Format)
673 return;
674
677 return;
678
680 return;
681
682 QSize size;
686 bool fixationRequired = false;
687#endif
688
689 switch (info.media_subtype) {
692 return;
696
698 // Without a modifier, the layout is implicit: the linear layout of the buffers the
699 // stream or the node allocated.
705
707 if (fixationRequired) {
708 // Answer with our one supported modifier; triggers another
709 // param_changed with the final, fixated format.
711 << "modifier fixation requested; confirming linear support";
713 }
714#endif
715 break;
716 }
719 return;
724 m_dmaBufReadable = true;
725#endif
726 break;
727 }
728 default:
729 return;
730 }
731
734 // A DMABUF that is not linear cannot be read here. Where mapped memory is
735 // also on offer, renegotiate without DMABUF rather than dropping every frame.
736 const bool mappedOnOffer =
740 std::array<uint8_t, 1024> buffersBuffer{};
741 const spa_pod *params[] = {
743 };
745 }
746 }
747#endif
748
753 }
754#endif
755
757 << "negotiated" << pixelFormat << size << framerate.num << "/" << framerate.denom;
758
761 << "the server picked a pixel format we cannot represent, media subtype"
763 Q_EMIT errorOccurred(u"The PipeWire camera stream picked an unsupported pixel format"_s);
764 }
765
767 if (framerate.denom != 0)
769
772}
773
776{
778 QT_WARNING_DISABLE_GCC("-Wmissing-field-initializers")
779 QT_WARNING_DISABLE_CLANG("-Wmissing-field-initializers")
780
781 std::array<uint8_t, 1024> buffer{};
783
786
791
792 // Single-value choice, DONT_FIXATE not set: "of the modifiers you
793 // offered, this is the one I can import."
799 const auto *pod = static_cast<const spa_pod *>(spa_pod_builder_pop(&builder, &frame));
800
802
803 const spa_pod *params[] = { pod };
805}
806#endif
807
811{
813 if (!desc) {
814 qCWarning(lcPipewireCameraStream) << "no layout for" << pixelFormat;
815 Q_EMIT errorOccurred(u"The PipeWire camera stream picked an unsupported pixel format"_s);
816 return;
817 }
818
819 const int stride = desc->strideForWidth(size.width());
821
823 for (std::size_t index = 0; index < kDmaBufBufferCount; ++index) {
825 if (!buf) {
826 qCWarning(lcPipewireCameraStream) << "cannot allocate DMA-BUF:" << buf.error();
827 break;
828 }
830 }
832 Q_EMIT errorOccurred(u"The PipeWire camera stream could not allocate DMA-BUF buffers"_s);
833 return;
834 }
835
838
839 std::array<uint8_t, 1024> storage{};
840 const spa_pod *params[] = {
842 };
844}
845
847{
848 if (!m_dmaBufAllocator) {
850 return;
851 }
852
853 if (!m_bufferRegistry) {
854 qCWarning(lcPipewireCameraStream) << "buffer added before its DMA-BUF was allocated";
855 return;
856 }
857
860}
861
863{
866 return;
867
868 // PipeWire closes the fd when the buffer is removed, but a frame can outlive that.
869 const spa_data &data = spaBuffer.datas[0];
871 if (!fd.isValid()) {
873 << "cannot duplicate the camera's DMA-BUF, errno" << errno;
874 return;
875 }
876
878 buffer,
880 });
882}
883
885{
886 using namespace QtMultimediaPrivate;
887 const auto node = ranges::find_if(m_nodeDmaBufs, [&](const NodeDmaBuf &candidate) {
888 return candidate.pwBuffer == buffer;
889 });
890 if (node == m_nodeDmaBufs.end())
891 return;
892
895}
896
899{
900 using namespace QtMultimediaPrivate;
901
905
906 const spa_chunk &chunk = *pwBuffer->buffer->datas[0].chunk;
908
911 if (!videoBuffer) {
912 // No zero-copy format (e.g. a compressed one): wrap the DMA-BUF into a plain
913 // MaterializingVideoBuffer without zero-copy machinery.
914 const int stride = chunk.stride != 0 ? chunk.stride : m_bufferRegistry->stride();
917 };
918
920
923 }
924
927
929 return true;
930}
931
934{
935 using namespace QtMultimediaPrivate;
936
937 const auto node = ranges::find_if(m_nodeDmaBufs, [&](const NodeDmaBuf &candidate) {
938 return candidate.pwBuffer == pwBuffer;
939 });
940 if (node == m_nodeDmaBufs.cend())
941 return false;
942
944 if (!chunk || chunk->offset != 0 || qsizetype(chunk->size) > node->dmaBuf->size())
945 return false;
946
947 if (!m_nodeRegistry) {
949 return false;
952 }
953
955 if (chunk->stride > 0) {
958 }
959 if (!videoBuffer) {
960 // Only a compressed frame is a byte stream without a layout; a raw one is copied.
962 return false;
963
966 };
970 }
971
974
976 return true;
977}
978#endif
979
981{
983 if (!pwBuffer) {
984 // The producer is ahead of us; dropping is better than blocking the
985 // graph.
986 qCDebug(lcPipewireCameraStream) << "no buffer available";
987 return;
988 }
989
991
993
995 // A frame that wraps the buffer takes over its requeue, through its capture slot.
999 return;
1000 }
1001#endif
1002
1003 if (frameFormat.isValid() && buffer->n_datas > 0) {
1004 // Stream-owned buffers that cannot be wrapped are copied, as server buffers are.
1007 << "frame data type" << spa_data_type(buffer->datas[0].type)
1009
1010 const spa_chunk *chunk = buffer->datas[0].chunk;
1011
1012 // A compressed format has no fixed stride; guessing one from the
1013 // chunk size would be meaningless.
1014 int stride = chunk->stride;
1016 const int height = frameFormat.frameHeight();
1017 if (height > 0)
1018 stride = int(chunk->size / quint32(height));
1019 }
1020
1024
1026 }
1027 }
1028
1030}
1031
1032} // namespace QtPipeWire
1033
1034QT_END_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcPipewireAudioSink, "qt.multimedia.pipewire.audiosink")
#define __has_include(x)
#define QT_PIPEWIRE_HAS_DMABUF_CAMERA