Sunshine master
Self-hosted game stream host for Moonlight.
pipewire.cpp
Go to the documentation of this file.
1
5// standard includes
6#include <fstream>
7
8// lib includes
9#include <gio/gio.h>
10#include <gio/gunixfdlist.h>
11#include <libdrm/drm_fourcc.h>
12#include <pipewire/pipewire.h>
13#include <spa/param/video/format-utils.h>
14#include <spa/param/video/type-info.h>
15#include <spa/pod/builder.h>
16
17// local includes
18#include "cuda.h"
19#include "graphics.h"
20#include "src/main.h"
21#include "src/platform/common.h"
22#include "src/video.h"
23#include "vaapi.h"
24#include "vulkan_encode.h"
25#include "wayland.h"
26
27#if !PW_CHECK_VERSION(1, 6, 0)
28constexpr int SPA_VIDEO_TRANSFER_SMPTE2084 = 14;
29#endif
30
31namespace {
32 // Buffer and limit constants
33 constexpr int SPA_POD_BUFFER_SIZE = 4096;
34 constexpr int MAX_PARAMS = 200;
35 constexpr int MAX_DMABUF_FORMATS = 200;
36 constexpr int MAX_DMABUF_MODIFIERS = 200;
37} // namespace
38
39using namespace std::literals;
40
41namespace pipewire {
42 struct format_map_t {
43 uint64_t fourcc;
44 int32_t pw_format;
45 };
46
47 static constexpr std::array<format_map_t, 7> format_map = {{
48 {DRM_FORMAT_XBGR2101010, SPA_VIDEO_FORMAT_xBGR_210LE},
49 {DRM_FORMAT_BGRA1010102, SPA_VIDEO_FORMAT_ARGB_210LE},
50 {DRM_FORMAT_RGBA1010102, SPA_VIDEO_FORMAT_ABGR_210LE},
51 {DRM_FORMAT_ABGR2101010, SPA_VIDEO_FORMAT_RGBA_102LE},
52 {DRM_FORMAT_ARGB2101010, SPA_VIDEO_FORMAT_BGRA_102LE},
53 {DRM_FORMAT_ARGB8888, SPA_VIDEO_FORMAT_BGRA},
54 {DRM_FORMAT_XRGB8888, SPA_VIDEO_FORMAT_BGRx},
55 }};
56
58 std::atomic<int> negotiated_width {0};
59 std::atomic<int> negotiated_height {0};
60 std::atomic<int> color_primaries {0};
61 std::atomic<int> transfer_function {0};
62 std::atomic<bool> stream_dead {false};
63 pw_stream_state previous_state;
64 pw_stream_state current_state;
65 std::string err_msg;
66 };
67
69 struct pw_stream *stream;
70 struct spa_hook stream_listener;
71 struct spa_video_info format;
72 struct pw_buffer *current_buffer;
73 uint64_t drm_format;
74 std::shared_ptr<shared_state_t> shared;
75 std::mutex frame_mutex;
76 std::condition_variable frame_cv;
77 size_t local_stride = 0;
78 bool frame_ready = false;
79 // Two distinct memory pools
80 std::vector<uint8_t> buffer_a;
81 std::vector<uint8_t> buffer_b;
82 // Points to the buffer currently owned by fill_img
83 std::vector<uint8_t> *front_buffer;
84 // Points to the buffer currently being written by on_process
85 std::vector<uint8_t> *back_buffer;
86
88 front_buffer(&buffer_a),
89 back_buffer(&buffer_b) {}
90 };
91
93 int32_t format;
94 uint64_t *modifiers;
95 int n_modifiers;
96 };
97
98 class pipewire_t {
99 public:
100 pipewire_t():
101 loop(pw_thread_loop_new("Pipewire thread", nullptr)) {
102 BOOST_LOG(debug) << "[pipewire] Start PW thread loop"sv;
103 pw_thread_loop_start(loop);
104 }
105
106 ~pipewire_t() {
107 BOOST_LOG(debug) << "[pipewire] Destroying pipewire_t"sv;
108 if (loop) {
109 BOOST_LOG(debug) << "[pipewire] Stop PW thread loop"sv;
110 pw_thread_loop_stop(loop);
111 }
112 try {
113 cleanup_stream();
114 } catch (const std::exception &e) {
115 BOOST_LOG(error) << "[pipewire] Standard exception caught in ~pipewire_t: "sv << e.what();
116 } catch (...) {
117 BOOST_LOG(error) << "[pipewire] Unknown exception caught in ~pipewire_t"sv;
118 }
119
120 pw_thread_loop_lock(loop);
121
122 if (core) {
123 BOOST_LOG(debug) << "[pipewire] Disconnect PW core"sv;
124 pw_core_disconnect(core);
125 core = nullptr;
126 }
127 if (context) {
128 BOOST_LOG(debug) << "[pipewire] Destroy PW context"sv;
129 pw_context_destroy(context);
130 context = nullptr;
131 }
132
133 pw_thread_loop_unlock(loop);
134
135 if (fd >= 0) {
136 BOOST_LOG(debug) << "[pipewire] Close pipewire_fd"sv;
137 close(fd);
138 }
139 BOOST_LOG(debug) << "[pipewire] Stop PW thread loop"sv;
140 pw_thread_loop_stop(loop);
141 BOOST_LOG(debug) << "[pipewire] Destroy PW thread loop"sv;
142 pw_thread_loop_destroy(loop);
143 }
144
145 std::mutex &frame_mutex() {
146 return stream_data.frame_mutex;
147 }
148
149 std::condition_variable &frame_cv() {
150 return stream_data.frame_cv;
151 }
152
153 bool is_frame_ready() const {
154 return stream_data.frame_ready;
155 }
156
157 void set_frame_ready(bool ready) {
158 stream_data.frame_ready = ready;
159 }
160
161 int init(const int stream_fd, const uint32_t stream_node, const uint64_t stream_object_serial, std::shared_ptr<shared_state_t> shared_state) {
162 fd = stream_fd;
163 node = stream_node;
164 object_serial = stream_object_serial;
165 stream_data.shared = std::move(shared_state);
166
167 pw_thread_loop_lock(loop);
168 BOOST_LOG(debug) << "[pipewire] Setup PW context"sv;
169 context = pw_context_new(pw_thread_loop_get_loop(loop), nullptr, 0);
170 if (context) {
171 BOOST_LOG(debug) << "[pipewire] Connect PW context to fd"sv;
172 if (fd >= 0) {
173 core = pw_context_connect_fd(context, fd, nullptr, 0);
174 } else {
175 core = pw_context_connect(context, nullptr, 0);
176 }
177 if (core) {
178 pw_core_add_listener(core, &core_listener, &core_events, nullptr);
179 } else {
180 BOOST_LOG(debug) << "[pipewire] Failed to connect to PW core. Error: "sv << errno << "(" << strerror(errno) << ")"sv;
181 return -1;
182 }
183 } else {
184 BOOST_LOG(debug) << "[pipewire] Failed to setup PW context. Error: "sv << errno << "(" << strerror(errno) << ")"sv;
185 return -1;
186 }
187
188 pw_thread_loop_unlock(loop);
189 return 0;
190 }
191
192 void cleanup_stream() {
193 BOOST_LOG(debug) << "[pipewire] Cleaning up stream"sv;
194 if (loop && stream_data.stream) {
195 pw_thread_loop_lock(loop);
196
197 // 1. Lock the frame mutex to stop fill_img
198 BOOST_LOG(debug) << "[pipewire] Stop fill_img"sv;
199 {
200 std::scoped_lock lock(stream_data.frame_mutex);
201 stream_data.frame_ready = false;
202 stream_data.current_buffer = nullptr;
203 }
204
205 if (stream_data.stream) {
206 BOOST_LOG(debug) << "[pipewire] Disconnect stream"sv;
207 pw_stream_disconnect(stream_data.stream);
208 BOOST_LOG(debug) << "[pipewire] Destroy stream"sv;
209 pw_stream_destroy(stream_data.stream);
210 stream_data.stream = nullptr;
211 }
212
213 pw_thread_loop_unlock(loop);
214 }
215 }
216
217 int ensure_stream(const platf::mem_type_e mem_type, const uint32_t width, const uint32_t height, const uint32_t refresh_rate, const struct dmabuf_format_info_t *dmabuf_infos, const int n_dmabuf_infos, const bool display_is_nvidia) {
218 pw_thread_loop_lock(loop);
219 if (!stream_data.stream) {
220 if (!core) {
221 BOOST_LOG(debug) << "[pipewire] PW core not available. Cannot ensure stream."sv;
222 pw_thread_loop_unlock(loop);
223 return -1;
224 }
225
226 struct pw_properties *props = pw_properties_new(PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture", PW_KEY_MEDIA_ROLE, "Screen", nullptr);
227#ifdef PW_KEY_TARGET_OBJECT
228 // If pipewire supports setting a PW_KEY_TARGET_OBJECT via object serial and the serial is valid (lower 32-bits not SPA_ID_INVALID, see PW_KEY_OBJECT_SERIAL docs), use it.
229 if ((object_serial & SPA_ID_INVALID) != SPA_ID_INVALID) {
230 BOOST_LOG(debug) << "[pipewire] Set PW stream target object to serial: "sv << object_serial;
231 pw_properties_setf(props, PW_KEY_TARGET_OBJECT, "%" PRIu64, object_serial);
232 node = PW_ID_ANY; // Force pw_connect_stream to connect via object serial in PW_KEY_TARGET_OBJECT with this value.
233 }
234#endif
235
236 BOOST_LOG(debug) << "[pipewire] Create PW stream"sv;
237 stream_data.stream = pw_stream_new(core, "Sunshine Video Capture", props);
238 pw_stream_add_listener(stream_data.stream, &stream_data.stream_listener, &stream_events, &stream_data);
239
240 std::array<uint8_t, SPA_POD_BUFFER_SIZE> buffer;
241 struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(buffer.data(), buffer.size());
242
243 int n_params = 0;
244 std::array<const struct spa_pod *, MAX_PARAMS> params;
245
246 // Add preferred parameters for DMA-BUF with modifiers
247 // Use DMA-BUF for VAAPI, or for CUDA when the display GPU is NVIDIA (pure NVIDIA system).
248 // On hybrid GPU systems (Intel+NVIDIA), DMA-BUFs come from the Intel GPU and cannot
249 // be imported into CUDA, so we fall back to memory buffers in that case.
250 bool use_dmabuf = n_dmabuf_infos > 0 && (mem_type == platf::mem_type_e::vaapi ||
251 mem_type == platf::mem_type_e::vulkan ||
252 (mem_type == platf::mem_type_e::cuda && display_is_nvidia));
253 if (use_dmabuf) {
254 for (int i = 0; i < n_dmabuf_infos; i++) {
255 auto format_param = build_format_parameter(&pod_builder, width, height, refresh_rate, dmabuf_infos[i].format, dmabuf_infos[i].modifiers, dmabuf_infos[i].n_modifiers);
256 params[n_params] = format_param;
257 n_params++;
258 }
259 }
260
261 // Add fallback for memptr
262 for (const auto &fmt : format_map) {
263 auto format_param = build_format_parameter(&pod_builder, width, height, refresh_rate, fmt.pw_format, nullptr, 0);
264 params[n_params] = format_param;
265 n_params++;
266 }
267 BOOST_LOG(debug) << "[pipewire] Connect PW stream - fd: "sv << fd << " node: "sv << node << " object serial: "sv << object_serial;
268 pw_stream_connect(stream_data.stream, PW_DIRECTION_INPUT, node, (enum pw_stream_flags)(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS), params.data(), n_params);
269 }
270 pw_thread_loop_unlock(loop);
271 return 0;
272 }
273
274 static void close_img_fds(egl::img_descriptor_t *img_descriptor) {
275 for (int &fd : img_descriptor->sd.fds) {
276 if (fd >= 0) {
277 close(fd);
278 fd = -1;
279 }
280 }
281 }
282
283 static void fill_img_metadata(egl::img_descriptor_t *img_descriptor, struct spa_buffer *buf) {
284 img_descriptor->frame_timestamp = std::chrono::steady_clock::now();
285
286 struct spa_meta_header *h = static_cast<struct spa_meta_header *>(
287 spa_buffer_find_meta_data(buf, SPA_META_Header, sizeof(*h))
288 );
289 if (h) {
290 img_descriptor->seq = h->seq;
291 img_descriptor->pts = h->pts;
292 }
293
294 if (buf->n_datas > 0) {
295 img_descriptor->pw_flags = buf->datas[0].chunk->flags;
296 }
297
298 struct spa_meta_region *damage = static_cast<struct spa_meta_region *>(
299 spa_buffer_find_meta_data(buf, SPA_META_VideoDamage, sizeof(*damage))
300 );
301 img_descriptor->pw_damage = (damage && damage->region.size.width > 0 && damage->region.size.height > 0) ? std::optional<bool>(true) : std::nullopt;
302 }
303
304 static void fill_img_dmabuf(egl::img_descriptor_t *img_descriptor, struct spa_buffer *buf, const stream_data_t &d) {
305 img_descriptor->sd.width = d.format.info.raw.size.width;
306 img_descriptor->sd.height = d.format.info.raw.size.height;
307 img_descriptor->sd.modifier = d.format.info.raw.modifier;
308 img_descriptor->sd.fourcc = d.drm_format;
309 for (int i = 0; i < MIN(buf->n_datas, 4); i++) {
310 img_descriptor->sd.fds[i] = dup(buf->datas[i].fd);
311 img_descriptor->sd.pitches[i] = buf->datas[i].chunk->stride;
312 img_descriptor->sd.offsets[i] = buf->datas[i].chunk->offset;
313 }
314 }
315
316 void fill_img(platf::img_t *img) {
317 pw_thread_loop_lock(loop);
318 std::scoped_lock lock(stream_data.frame_mutex);
319
320 if (stream_data.shared && stream_data.shared->stream_dead.load()) {
321 img->data = nullptr;
322 close_img_fds(static_cast<egl::img_descriptor_t *>(img));
323 pw_thread_loop_unlock(loop);
324 return;
325 }
326
327 if (!stream_data.current_buffer) {
328 img->data = nullptr;
329 pw_thread_loop_unlock(loop);
330 return;
331 }
332
333 struct spa_buffer *buf = stream_data.current_buffer->buffer;
334 if (buf->datas[0].chunk->size != 0) {
335 auto *img_descriptor = static_cast<egl::img_descriptor_t *>(img);
336 fill_img_metadata(img_descriptor, buf);
337 if (buf->datas[0].type == SPA_DATA_DmaBuf) {
338 fill_img_dmabuf(img_descriptor, buf, stream_data);
339 } else {
340 img->data = stream_data.front_buffer->data();
341 img->row_pitch = stream_data.local_stride;
342 }
343 }
344
345 pw_thread_loop_unlock(loop);
346 }
347
348 void set_negotiate_maxframerate(bool negotiate_maxframerate) {
349 negotiate_maxframerate_ = negotiate_maxframerate;
350 }
351
352 private:
353 struct pw_thread_loop *loop;
354 struct pw_context *context;
355 struct pw_core *core;
356 struct spa_hook core_listener;
357 struct stream_data_t stream_data;
358 int fd;
359 uint32_t node;
360 uint64_t object_serial;
361 bool negotiate_maxframerate_ = true;
362
363 struct spa_pod *build_format_parameter(struct spa_pod_builder *b, uint32_t width, uint32_t height, uint32_t refresh_rate, int32_t format, uint64_t *modifiers, int n_modifiers) {
364 struct spa_pod_frame object_frame;
365 struct spa_pod_frame modifier_frame;
366 std::array<struct spa_rectangle, 3> sizes;
367 std::array<struct spa_fraction, 3> framerates;
368
369 sizes[0] = SPA_RECTANGLE(width, height); // Preferred
370 sizes[1] = SPA_RECTANGLE(1, 1);
371 sizes[2] = SPA_RECTANGLE(8192, 4096);
372
373 framerates[0] = SPA_FRACTION(0, 1); // default; we only want variable rate, thus bypassing compositor pacing
374 framerates[1] = SPA_FRACTION(0, 1); // min
375 framerates[2] = SPA_FRACTION(0, 1); // max
376
377 spa_pod_builder_push_object(b, &object_frame, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
378 spa_pod_builder_add(b, SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video), 0);
379 spa_pod_builder_add(b, SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), 0);
380 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_format, SPA_POD_Id(format), 0);
381 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(&sizes[0], &sizes[1], &sizes[2]), 0);
382 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&framerates[0]), 0);
383 if (negotiate_maxframerate_) {
384 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_maxFramerate, SPA_POD_CHOICE_RANGE_Fraction(&framerates[0], &framerates[1], &framerates[2]), 0);
385 }
386
387 if (format == SPA_VIDEO_FORMAT_xBGR_210LE) {
388 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_colorPrimaries, SPA_POD_Id(SPA_VIDEO_COLOR_PRIMARIES_BT2020), 0);
389 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_transferFunction, SPA_POD_Id(SPA_VIDEO_TRANSFER_SMPTE2084), 0);
390 }
391
392 if (n_modifiers) {
393 spa_pod_builder_prop(b, SPA_FORMAT_VIDEO_modifier, SPA_POD_PROP_FLAG_MANDATORY | SPA_POD_PROP_FLAG_DONT_FIXATE);
394 spa_pod_builder_push_choice(b, &modifier_frame, SPA_CHOICE_Enum, 0);
395
396 // Preferred value, we pick the first modifier be the preferred one
397 spa_pod_builder_long(b, modifiers[0]);
398 for (uint32_t i = 0; i < n_modifiers; i++) {
399 spa_pod_builder_long(b, modifiers[i]);
400 }
401
402 spa_pod_builder_pop(b, &modifier_frame);
403 }
404
405 return static_cast<struct spa_pod *>(spa_pod_builder_pop(b, &object_frame));
406 }
407
408 static void on_core_info_cb([[maybe_unused]] void *user_data, const struct pw_core_info *pw_info) {
409 BOOST_LOG(info) << "[pipewire] Connected to pipewire version "sv << pw_info->version;
410 }
411
412 static void on_core_error_cb([[maybe_unused]] void *user_data, const uint32_t id, const int seq, [[maybe_unused]] int res, const char *message) {
413 BOOST_LOG(info) << "[pipewire] Pipewire Error, id:"sv << id << " seq:"sv << seq << " message: "sv << message;
414 }
415
416 constexpr static const struct pw_core_events core_events = {
417 .version = PW_VERSION_CORE_EVENTS,
418 .info = on_core_info_cb,
419 .error = on_core_error_cb,
420 };
421
422 static void on_stream_state_changed(void *user_data, enum pw_stream_state old, enum pw_stream_state state, const char *err_msg) {
423 BOOST_LOG(debug) << "[pipewire] PipeWire stream state: " << pw_stream_state_as_string(old)
424 << " -> " << pw_stream_state_as_string(state);
425
426 auto *d = static_cast<stream_data_t *>(user_data);
427
428 switch (state) {
429 case PW_STREAM_STATE_PAUSED:
430 if (d->shared && old == PW_STREAM_STATE_STREAMING) {
431 {
432 std::scoped_lock lock(d->frame_mutex);
433 d->frame_ready = false;
434 d->current_buffer = nullptr;
435 d->shared->stream_dead.store(true);
436 d->shared->current_state = state;
437 d->shared->previous_state = old;
438 d->shared->err_msg = "";
439 }
440 d->frame_cv.notify_all();
441 }
442 break;
443 case PW_STREAM_STATE_ERROR:
444 {
445 std::scoped_lock lock(d->frame_mutex);
446 d->shared->current_state = state;
447 d->shared->previous_state = old;
448 d->shared->err_msg = std::string(err_msg);
449 }
450 [[fallthrough]];
451 case PW_STREAM_STATE_UNCONNECTED:
452 if (d->shared) {
453 d->shared->stream_dead.store(true);
454 d->frame_cv.notify_all();
455 }
456 break;
457 default:
458 break;
459 }
460 }
461
462 static void on_process(void *user_data) {
463 const auto d = static_cast<struct stream_data_t *>(user_data);
464 struct pw_buffer *b = nullptr;
465
466 // 1. Drain the queue: Always grab the most recent buffer
467 while (struct pw_buffer *aux = pw_stream_dequeue_buffer(d->stream)) {
468 if (b) {
469 pw_stream_queue_buffer(d->stream, b); // Return the older, unused buffer
470 }
471 b = aux;
472 }
473
474 if (!b) {
475 return;
476 }
477
478 // 2. Fast Path: DMA-BUF
479 if (b->buffer->datas[0].type == SPA_DATA_DmaBuf) {
480 std::scoped_lock lock(d->frame_mutex);
481 if (d->current_buffer) {
482 pw_stream_queue_buffer(d->stream, d->current_buffer);
483 }
484 d->current_buffer = b;
485 d->frame_ready = true;
486 }
487 // 3. Optimized Path: Software/MemPtr
488 else if (b->buffer->datas[0].data != nullptr) {
489 size_t size = b->buffer->datas[0].chunk->size;
490
491 // Perform the copy to the BACK buffer while NOT holding the lock
492 if (d->back_buffer->size() < size) {
493 d->back_buffer->resize(size);
494 }
495 std::memcpy(d->back_buffer->data(), b->buffer->datas[0].data, size);
496
497 {
498 // Lock only for the pointer swap and state update
499 std::scoped_lock lock(d->frame_mutex);
500 std::swap(d->front_buffer, d->back_buffer);
501
502 d->local_stride = b->buffer->datas[0].chunk->stride;
503 d->frame_ready = true;
504 d->current_buffer = b;
505 }
506
507 // Release the PW buffer immediately after copy
508 pw_stream_queue_buffer(d->stream, b);
509 }
510
511 d->frame_cv.notify_one();
512 }
513
514 static void on_param_changed(void *user_data, uint32_t id, const struct spa_pod *param) {
515 const auto d = static_cast<struct stream_data_t *>(user_data);
516
517 d->current_buffer = nullptr;
518
519 if (param == nullptr || id != SPA_PARAM_Format) {
520 return;
521 }
522 if (spa_format_parse(param, &d->format.media_type, &d->format.media_subtype) < 0) {
523 return;
524 }
525 if (d->format.media_type != SPA_MEDIA_TYPE_video || d->format.media_subtype != SPA_MEDIA_SUBTYPE_raw) {
526 return;
527 }
528 if (spa_format_video_raw_parse(param, &d->format.info.raw) < 0) {
529 return;
530 }
531
532 BOOST_LOG(info) << "[pipewire] Video format: "sv << d->format.info.raw.format;
533 BOOST_LOG(info) << "[pipewire] Size: "sv << d->format.info.raw.size.width << "x"sv << d->format.info.raw.size.height;
534 BOOST_LOG(info) << "[pipewire] Color primaries: "sv << d->format.info.raw.color_primaries;
535 BOOST_LOG(info) << "[pipewire] Transfer function: "sv << d->format.info.raw.transfer_function;
536 if (d->format.info.raw.max_framerate.num == 0 && d->format.info.raw.max_framerate.denom == 1) {
537 BOOST_LOG(info) << "[pipewire] Framerate (from compositor): 0/1 (variable rate capture)";
538 } else {
539 BOOST_LOG(info) << "[pipewire] Framerate (from compositor): "sv << d->format.info.raw.framerate.num << "/"sv << d->format.info.raw.framerate.denom;
540 BOOST_LOG(info) << "[pipewire] Framerate (from compositor, max): "sv << d->format.info.raw.max_framerate.num << "/"sv << d->format.info.raw.max_framerate.denom;
541 }
542
543 int physical_w = d->format.info.raw.size.width;
544 int physical_h = d->format.info.raw.size.height;
545
546 if (d->shared) {
547 int old_w = d->shared->negotiated_width.load();
548 int old_h = d->shared->negotiated_height.load();
549 int old_color_primaries = d->shared->color_primaries.load();
550 int old_transfer_function = d->shared->transfer_function.load();
551
552 if (physical_w != old_w || physical_h != old_h) {
553 d->shared->negotiated_width.store(physical_w);
554 d->shared->negotiated_height.store(physical_h);
555 }
556
557 if (d->format.info.raw.color_primaries != old_color_primaries || d->format.info.raw.transfer_function != old_transfer_function) {
558 d->shared->color_primaries.store(d->format.info.raw.color_primaries);
559 d->shared->transfer_function.store(d->format.info.raw.transfer_function);
560 }
561 }
562
563 uint64_t drm_format = 0;
564 for (const auto &fmt : format_map) {
565 if (fmt.pw_format == d->format.info.raw.format) {
566 drm_format = fmt.fourcc;
567 }
568 }
569 d->drm_format = drm_format;
570
571 uint32_t buffer_types = 0;
572 if (spa_pod_find_prop(param, nullptr, SPA_FORMAT_VIDEO_modifier) != nullptr && d->drm_format) {
573 BOOST_LOG(info) << "[pipewire] using DMA-BUF buffers"sv;
574 buffer_types |= 1 << SPA_DATA_DmaBuf;
575 } else {
576 BOOST_LOG(info) << "[pipewire] using memory buffers"sv;
577 buffer_types |= 1 << SPA_DATA_MemPtr;
578 }
579
580 // Ack the buffer type and metadata
581 std::array<uint8_t, SPA_POD_BUFFER_SIZE> buffer;
582 std::array<const struct spa_pod *, 3> params;
583 int n_params = 0;
584 struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(buffer.data(), buffer.size());
585 auto buffer_param = static_cast<const struct spa_pod *>(spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers, SPA_PARAM_BUFFERS_dataType, SPA_POD_Int(buffer_types)));
586 params[n_params] = buffer_param;
587 n_params++;
588 auto meta_param = static_cast<const struct spa_pod *>(spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header), SPA_PARAM_META_size, SPA_POD_Int(sizeof(struct spa_meta_header))));
589 params[n_params] = meta_param;
590 n_params++;
591 int videoDamageRegionCount = 16;
592 auto damage_param = static_cast<const struct spa_pod *>(spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoDamage), SPA_PARAM_META_size, SPA_POD_CHOICE_RANGE_Int(sizeof(struct spa_meta_region) * videoDamageRegionCount, sizeof(struct spa_meta_region) * 1, sizeof(struct spa_meta_region) * videoDamageRegionCount)));
593 params[n_params] = damage_param;
594 n_params++;
595
596 pw_stream_update_params(d->stream, params.data(), n_params);
597 }
598
599 constexpr static const struct pw_stream_events stream_events = {
600 .version = PW_VERSION_STREAM_EVENTS,
601 .state_changed = on_stream_state_changed,
602 .param_changed = on_param_changed,
603 .process = on_process,
604 };
605 };
606
608 public:
609 static bool init_pipewire_and_check_hwdevice_type(platf::mem_type_e hwdevice_type) {
610 // Initialize pipewire to load necessary modules
611 pw_init(nullptr, nullptr);
612
613 // Check if we have a matching hwdevice_type
614 switch (hwdevice_type) {
615 using enum platf::mem_type_e;
616 case system:
617 case vaapi:
618 case cuda:
619 case vulkan:
620 return true;
621 default:
622 return false;
623 }
624 }
625
634 virtual int configure_stream(const std::string &display_name, int &out_pipewire_fd, uint32_t &out_pipewire_node, uint64_t &out_pipewire_objectserial) = 0;
635
640 // Query outputs directly using wayland wl::monitors()
641 if (logical_height <= 0 || logical_width <= 0 || env_logical_height <= 0 || env_logical_width <= 0 || env_height <= 0 || env_width <= 0) {
642 int desktop_width = 0;
643 int desktop_height = 0;
644 int desktop_logical_width = 0;
645 int desktop_logical_height = 0;
646 for (const auto &monitor : wl::monitors()) {
647 BOOST_LOG(debug) << "[pipewire] Found output: '"sv << monitor->name << "' offset: "sv << monitor->viewport.offset_x << 'x' << monitor->viewport.offset_y << " resolution: "sv << monitor->viewport.width << 'x' << monitor->viewport.height << " logical resolution: "sv << monitor->viewport.logical_width << 'x' << monitor->viewport.logical_height;
648 // If logical_width and logical_height are not valid try to update them to correct values by matching to monitor
649 // position/dimension or position/logical dimensions here since we're iterating for maximum environment size anyway
650 if ((logical_width <= 0 || logical_height <= 0) && monitor->viewport.offset_x == offset_x && monitor->viewport.offset_y == offset_y && ((monitor->viewport.width == width && monitor->viewport.height == height) || (monitor->viewport.logical_width == width && monitor->viewport.logical_height == height))) {
651 this->logical_width = monitor->viewport.logical_width;
652 this->logical_height = monitor->viewport.logical_height;
653 BOOST_LOG(debug) << "[pipewire] Set logical resolution: "sv << logical_width << 'x' << logical_height;
654 }
655 // Update desktop dimensions to setup maximum environment size over all screens
656 desktop_width = std::max(desktop_width, monitor->viewport.offset_x + monitor->viewport.width);
657 desktop_height = std::max(desktop_height, monitor->viewport.offset_y + monitor->viewport.height);
658 // Update desktop logical dimensions to setup maximum logical environment size over all screens
659 desktop_logical_width = std::max(desktop_logical_width, monitor->viewport.offset_x + monitor->viewport.logical_width);
660 desktop_logical_height = std::max(desktop_logical_height, monitor->viewport.offset_y + monitor->viewport.logical_height);
661 }
662 if (env_height <= 0 || env_width <= 0) {
663 this->env_width = desktop_width;
664 this->env_height = desktop_height;
665 BOOST_LOG(debug) << "[pipewire] Set desktop resolution: "sv << env_width << 'x' << env_height;
666 }
667 if (env_logical_height <= 0 || env_logical_width <= 0) {
668 this->env_logical_width = desktop_logical_width;
669 this->env_logical_height = desktop_logical_height;
670 BOOST_LOG(debug) << "[pipewire] Set desktop logical resolution: "sv << env_logical_width << 'x' << env_logical_height;
671 }
672 }
673 }
674
675 int init(platf::mem_type_e hwdevice_type, const std::string &display_name, const ::video::config_t &config) {
676 // calculate frame interval we should capture at
677 framerate = config.framerate;
678 if (config.framerateX100 > 0) {
679 AVRational fps_strict = ::video::framerateX100_to_rational(config.framerateX100);
680 delay = std::chrono::nanoseconds(
681 (static_cast<int64_t>(fps_strict.den) * 1'000'000'000LL) / fps_strict.num
682 );
683 BOOST_LOG(info) << "[pipewire] Requested frame rate [" << fps_strict.num << "/" << fps_strict.den << ", approx. " << av_q2d(fps_strict) << " fps]";
684 } else {
685 delay = std::chrono::nanoseconds {1s} / framerate;
686 BOOST_LOG(info) << "[pipewire] Requested frame rate [" << framerate << "fps]";
687 }
688 mem_type = hwdevice_type;
689
690 if (get_dmabuf_modifiers() < 0) {
691 return -1;
692 }
693
694 int pipewire_fd = -1;
695 auto pipewire_node = PW_ID_ANY; // Default for invalid stream from pipewire docs
696 uint64_t pipewire_object_serial = SPA_ID_INVALID; // Default for invalid stream from pipewire docs for PW_KEY_OBJECT_SERIAL
697 // Fetch stream info
698 if (configure_stream(display_name, pipewire_fd, pipewire_node, pipewire_object_serial) < 0 || (pipewire_node == PW_ID_ANY && (pipewire_object_serial & SPA_ID_INVALID) == SPA_ID_INVALID)) {
699 BOOST_LOG(error) << "[pipewire] Could not find display with name: '"sv << display_name << "'";
700 return -1;
701 }
702 BOOST_LOG(info) << "[pipewire] Streaming display '"sv << display_name << "' offset: "sv << offset_x << "x"sv << offset_y << " resolution: "sv << width << "x"sv << height;
703
704 // Verify or update display parameters for streaming to ensure absolute touch inputs work as expected
706
707 framerate = config.framerate;
708
709 if (!shared_state) {
710 shared_state = std::make_shared<shared_state_t>();
711 } else {
712 shared_state->stream_dead.store(false);
713 shared_state->negotiated_width.store(0);
714 shared_state->negotiated_height.store(0);
715 shared_state->color_primaries.store(0);
716 shared_state->transfer_function.store(0);
717 }
718
719 if (pipewire.init(pipewire_fd, pipewire_node, pipewire_object_serial, shared_state) < 0) {
720 BOOST_LOG(error) << "[pipewire] Failed to init pipewire. pipewire_t::init() failed.";
721 return -1;
722 }
723
724 // Start PipeWire now so format negotiation can proceed before capture start
725 if (pipewire.ensure_stream(mem_type, width, height, framerate, dmabuf_infos.data(), n_dmabuf_infos, display_is_nvidia) < 0) {
726 BOOST_LOG(error) << "[pipewire] Failed to ensure pipewire stream. pipewire_t::init() failed.";
727 return -1;
728 }
729
730 // Wait for pipewire negotiation to finish so we have the proper negotiated dimensions
731 int timeout_ms = 1500;
732 int negotiated_w = 0;
733 int negotiated_h = 0;
734 while (timeout_ms > 0) {
735 negotiated_w = shared_state->negotiated_width.load();
736 negotiated_h = shared_state->negotiated_height.load();
737 if (negotiated_w > 0 && negotiated_h > 0) {
738 break;
739 }
740 std::this_thread::sleep_for(std::chrono::milliseconds(10));
741 timeout_ms -= 10;
742 }
743 // Set width and height to the values negotiated by pipewire
744 if (negotiated_w > 0 && negotiated_h > 0 && (negotiated_w != width || negotiated_h != height)) {
745 width = negotiated_w;
746 height = negotiated_h;
747 BOOST_LOG(info) << "[pipewire] Using negotiated Resolution: "sv << width << "x" << height;
748
749 // Reset and update display parameters for negotiated resolution
750 env_width = 0;
751 env_height = 0;
752 logical_height = 0;
753 logical_width = 0;
754 env_logical_height = 0;
755 env_logical_width = 0;
757 }
758
759 return 0;
760 }
761
762 platf::capture_e snapshot(const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr<platf::img_t> &img_out, std::chrono::milliseconds timeout, bool show_cursor) {
763 // FIXME: show_cursor is ignored
764 auto deadline = std::chrono::steady_clock::now() + timeout;
765 int retries = 0;
766
767 while (std::chrono::steady_clock::now() < deadline) {
768 if (!wait_for_frame(deadline)) {
769 return platf::capture_e::timeout;
770 }
771
772 if (!pull_free_image_cb(img_out)) {
773 return platf::capture_e::interrupted;
774 }
775
776 auto *img_egl = static_cast<egl::img_descriptor_t *>(img_out.get());
777 img_egl->reset();
778 pipewire.fill_img(img_egl);
779
780 // Check if we got valid data (either DMA-BUF fd or memory pointer), then filter duplicates
781 if ((img_egl->sd.fds[0] >= 0 || img_egl->data != nullptr) && !is_buffer_redundant(img_egl)) {
782 // Update frame metadata
783 update_metadata(img_egl, retries);
784 return platf::capture_e::ok;
785 }
786
787 // No valid frame yet, or it was a duplicate
788 retries++;
789 }
790 return platf::capture_e::timeout;
791 }
792
793 std::shared_ptr<platf::img_t> alloc_img() override {
794 // Note: this img_t type is also used for memory buffers
795 auto img = std::make_shared<egl::img_descriptor_t>();
796
797 img->width = width;
798 img->height = height;
799 img->pixel_pitch = 4;
800 img->row_pitch = img->pixel_pitch * width;
801 img->sequence = 0;
802 img->serial = std::numeric_limits<decltype(img->serial)>::max();
803 img->data = nullptr;
804 std::fill_n(img->sd.fds, 4, -1);
805
806 return img;
807 }
808
809 virtual bool check_stream_dead(platf::capture_e &out_status) {
810 return false; // Return to default stream dead handling.
811 }
812
813 platf::capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override {
814 auto next_frame = std::chrono::steady_clock::now();
815
816 if (pipewire.ensure_stream(mem_type, width, height, framerate, dmabuf_infos.data(), n_dmabuf_infos, display_is_nvidia) < 0) {
817 BOOST_LOG(error) << "[pipewire] Failed to ensure pipewire stream. capture() failed with error.";
818 return platf::capture_e::error;
819 }
820 sleep_overshoot_logger.reset();
821
822 while (true) {
823 // Check if PipeWire signaled a dead stream
824 if (shared_state->stream_dead.exchange(false)) {
825 // Additional custom error-handling for subclasses on stream dead event
826 if (platf::capture_e status; check_stream_dead(status)) {
827 return status;
828 }
829 // Re-init the capture if the stream is dead for any other reason
830 BOOST_LOG(warning) << "[pipewire] PipeWire stream disconnected. Forcing session reset."sv;
831 return platf::capture_e::reinit;
832 }
833
834 // Advance to (or catch up with) next delay interval
835 auto now = std::chrono::steady_clock::now();
836 while (next_frame < now) {
837 next_frame += delay;
838 }
839
840 if (next_frame > now) {
841 std::this_thread::sleep_until(next_frame);
842 sleep_overshoot_logger.first_point(next_frame);
843 sleep_overshoot_logger.second_point_now_and_log();
844 }
845
846 std::shared_ptr<platf::img_t> img_out;
847 switch (const auto status = snapshot(pull_free_image_cb, img_out, 1000ms, *cursor)) {
848 case platf::capture_e::reinit:
849 case platf::capture_e::error:
850 case platf::capture_e::interrupted:
851 pipewire.frame_cv().notify_all();
852 return status;
853 case platf::capture_e::timeout:
854 if (!pull_free_image_cb(img_out)) {
855 // Detect if shutdown is pending
856 BOOST_LOG(debug) << "[pipewire] PipeWire: timeout -> interrupt nudge";
857 pipewire.frame_cv().notify_all();
858 return platf::capture_e::interrupted;
859 }
860 if (!push_captured_image_cb(std::move(img_out), false)) {
861 BOOST_LOG(debug) << "[pipewire] PipeWire: !push_captured_image_cb -> ok";
862 return platf::capture_e::ok;
863 }
864 break;
865 case platf::capture_e::ok:
866 if (!push_captured_image_cb(std::move(img_out), true)) {
867 BOOST_LOG(debug) << "[pipewire] PipeWire: !push_captured_image_cb -> ok";
868 return platf::capture_e::ok;
869 }
870 break;
871 default:
872 BOOST_LOG(error) << "[pipewire] Unrecognized capture status ["sv << std::to_underlying(status) << ']';
873 return status;
874 }
875 }
876
877 return platf::capture_e::ok;
878 }
879
880 std::unique_ptr<platf::avcodec_encode_device_t> make_avcodec_encode_device(platf::pix_fmt_e pix_fmt) override {
881#ifdef SUNSHINE_BUILD_VAAPI
882 if (mem_type == platf::mem_type_e::vaapi) {
883 return va::make_avcodec_encode_device(width, height, n_dmabuf_infos > 0);
884 }
885#endif
886
887#ifdef SUNSHINE_BUILD_VULKAN
888 if (mem_type == platf::mem_type_e::vulkan && n_dmabuf_infos > 0) {
889 return vk::make_avcodec_encode_device_vram(width, height, 0, 0);
890 }
891#endif
892
893#ifdef SUNSHINE_BUILD_CUDA
894 if (mem_type == platf::mem_type_e::cuda) {
895 if (display_is_nvidia && n_dmabuf_infos > 0) {
896 // Display GPU is NVIDIA - can use DMA-BUF directly
897 return cuda::make_avcodec_gl_encode_device(width, height, 0, 0);
898 } else {
899 // Hybrid system (Intel display + NVIDIA encode) - use memory buffer path
900 // DMA-BUFs from Intel GPU cannot be imported into CUDA
901 return cuda::make_avcodec_encode_device(width, height, false);
902 }
903 }
904#endif
905
906 return std::make_unique<platf::avcodec_encode_device_t>();
907 }
908
909 int dummy_img(platf::img_t *img) override {
910 if (!img) {
911 return -1;
912 }
913
914 img->data = new std::uint8_t[img->height * img->row_pitch];
915 std::fill_n(img->data, img->height * img->row_pitch, 0);
916 return 0;
917 }
918
919 bool is_hdr() override {
920 int color_primaries = shared_state->color_primaries.load();
921 int transfer_function = shared_state->transfer_function.load();
922
923 if (color_primaries == SPA_VIDEO_COLOR_PRIMARIES_BT2020 && transfer_function == SPA_VIDEO_TRANSFER_SMPTE2084) {
924 return true;
925 }
926
927 return false;
928 }
929
930 bool get_hdr_metadata(SS_HDR_METADATA &metadata) override {
931 int color_primaries = shared_state->color_primaries.load();
932 int transfer_function = shared_state->transfer_function.load();
933
934 if (color_primaries == SPA_VIDEO_COLOR_PRIMARIES_BT2020 && transfer_function == SPA_VIDEO_TRANSFER_SMPTE2084) {
935 // Report Rec 2020 primaries
936 metadata.displayPrimaries[0].x = 0.708f * 50000;
937 metadata.displayPrimaries[0].y = 0.292f * 50000;
938 metadata.displayPrimaries[1].x = 0.170f * 50000;
939 metadata.displayPrimaries[1].y = 0.797f * 50000;
940 metadata.displayPrimaries[2].x = 0.131f * 50000;
941 metadata.displayPrimaries[2].y = 0.046f * 50000;
942 metadata.whitePoint.x = 0.3127f * 50000;
943 metadata.whitePoint.y = 0.3290f * 50000;
944
945 // This is according to HDR10+ standards, should probably be based on actual data
946 metadata.maxDisplayLuminance = 4000;
947 metadata.minDisplayLuminance = 1;
948
949 // These are content-specific metadata parameters that this interface doesn't give us
950 metadata.maxContentLightLevel = 0;
951 metadata.maxFrameAverageLightLevel = 0;
952 metadata.maxFullFrameLuminance = 0;
953
954 return true;
955 }
956
957 return false;
958 }
959
960 private:
961 bool is_buffer_redundant(const egl::img_descriptor_t *img) {
962 // Check for corrupted frame
963 if (img->pw_flags.has_value() && (img->pw_flags.value() & SPA_CHUNK_FLAG_CORRUPTED)) {
964 return true;
965 }
966
967 // If PTS is identical, only drop if damage metadata confirms no change
968 if (img->pts.has_value() && last_pts.has_value() && img->pts.value() == last_pts.value()) {
969 return img->pw_damage.has_value() && !img->pw_damage.value();
970 }
971
972 return false;
973 }
974
975 void update_metadata(egl::img_descriptor_t *img, int retries) {
976 last_seq = img->seq;
977 last_pts = img->pts;
978 img->sequence = ++sequence;
979
980 if (retries > 0) {
981 BOOST_LOG(debug) << "[pipewire] Processed frame after " << retries << " redundant events."sv;
982 }
983 }
984
985 bool wait_for_frame(std::chrono::steady_clock::time_point deadline) {
986 std::unique_lock<std::mutex> lock(pipewire.frame_mutex());
987
988 bool success = pipewire.frame_cv().wait_until(lock, deadline, [&] {
989 return pipewire.is_frame_ready() || shared_state->stream_dead.load();
990 });
991
992 if (success) {
993 pipewire.set_frame_ready(false);
994 return true;
995 }
996 return false;
997 }
998
999 static bool pw_format_supported(uint64_t fourcc, std::array<EGLint, MAX_DMABUF_FORMATS> dmabuf_formats) {
1000 for (const auto &drm_format : dmabuf_formats) {
1001 if (drm_format == fourcc) {
1002 return true;
1003 }
1004 }
1005 return false;
1006 }
1007
1008 void query_dmabuf_formats(EGLDisplay egl_display) {
1009 EGLint num_dmabuf_formats = 0;
1010 std::array<EGLint, MAX_DMABUF_FORMATS> dmabuf_formats = {0};
1011 eglQueryDmaBufFormatsEXT(egl_display, MAX_DMABUF_FORMATS, dmabuf_formats.data(), &num_dmabuf_formats);
1012
1013 if (num_dmabuf_formats > MAX_DMABUF_FORMATS) {
1014 BOOST_LOG(warning) << "[pipewire] Some DMA-BUF formats are being ignored"sv;
1015 }
1016
1017 for (const auto &fmt : format_map) {
1018 if (n_dmabuf_infos >= MAX_DMABUF_FORMATS) {
1019 break;
1020 }
1021
1022 if (!pw_format_supported(fmt.fourcc, dmabuf_formats)) {
1023 continue;
1024 }
1025
1026 EGLint num_modifiers = 0;
1027 std::array<EGLuint64KHR, MAX_DMABUF_MODIFIERS> mods = {0};
1028 eglQueryDmaBufModifiersEXT(egl_display, fmt.fourcc, MAX_DMABUF_MODIFIERS, mods.data(), nullptr, &num_modifiers);
1029
1030 if (num_modifiers > MAX_DMABUF_MODIFIERS) {
1031 BOOST_LOG(warning) << "[pipewire] Some DMA-BUF modifiers are being ignored"sv;
1032 }
1033
1034 dmabuf_infos[n_dmabuf_infos].format = fmt.pw_format;
1035 dmabuf_infos[n_dmabuf_infos].n_modifiers = MIN(num_modifiers, MAX_DMABUF_MODIFIERS);
1036 dmabuf_infos[n_dmabuf_infos].modifiers =
1037 static_cast<uint64_t *>(g_memdup2(mods.data(), sizeof(uint64_t) * dmabuf_infos[n_dmabuf_infos].n_modifiers));
1038 ++n_dmabuf_infos;
1039 }
1040 }
1041
1042 int get_dmabuf_modifiers() {
1043 if (wl_display.init() < 0) {
1044 return -1;
1045 }
1046
1047 auto egl_display = egl::make_display(wl_display.get());
1048 if (!egl_display) {
1049 return -1;
1050 }
1051
1052 // Detect if this is a pure NVIDIA system (not hybrid Intel+NVIDIA)
1053 // On hybrid systems, the wayland compositor typically runs on Intel,
1054 // so DMA-BUFs from portal will come from Intel and cannot be imported into CUDA.
1055 // Check if Intel GPU exists - if so, assume hybrid system and disable CUDA DMA-BUF.
1056 bool has_intel_gpu = std::ifstream("/sys/class/drm/card0/device/vendor").good() ||
1057 std::ifstream("/sys/class/drm/card1/device/vendor").good();
1058 if (has_intel_gpu) {
1059 // Read vendor IDs to check for Intel (0x8086)
1060 auto check_intel = [](const std::string &path) {
1061 if (std::ifstream f(path); f.good()) {
1062 std::string vendor;
1063 f >> vendor;
1064 return vendor == "0x8086";
1065 }
1066 return false;
1067 };
1068 bool intel_present = check_intel("/sys/class/drm/card0/device/vendor") ||
1069 check_intel("/sys/class/drm/card1/device/vendor");
1070 if (intel_present) {
1071 BOOST_LOG(info) << "[pipewire] Hybrid GPU system detected (Intel + discrete) - CUDA will use memory buffers"sv;
1072 display_is_nvidia = false;
1073 } else {
1074 // No Intel GPU found, check if NVIDIA is present
1075 const char *vendor = eglQueryString(egl_display.get(), EGL_VENDOR);
1076 if (vendor && std::string_view(vendor).contains("NVIDIA")) {
1077 BOOST_LOG(info) << "[pipewire] Pure NVIDIA system - DMA-BUF will be enabled for CUDA"sv;
1078 display_is_nvidia = true;
1079 }
1080 }
1081 }
1082
1083 if (eglQueryDmaBufFormatsEXT && eglQueryDmaBufModifiersEXT) {
1084 query_dmabuf_formats(egl_display.get());
1085 }
1086
1087 return 0;
1088 }
1089
1090 platf::mem_type_e mem_type;
1091 wl::display_t wl_display;
1092 std::array<struct dmabuf_format_info_t, MAX_DMABUF_FORMATS> dmabuf_infos;
1093 int n_dmabuf_infos;
1094 bool display_is_nvidia = false; // Track if display GPU is NVIDIA
1095 std::chrono::nanoseconds delay;
1096 std::optional<std::uint64_t> last_pts {};
1097 std::optional<std::uint64_t> last_seq {};
1098 std::uint64_t sequence {};
1099 uint32_t framerate;
1100
1101 protected:
1102 // Allow subclasses to access for pipewire requirements setup and stream dead checks
1103 pipewire_t pipewire;
1104 std::shared_ptr<shared_state_t> shared_state;
1105 };
1106} // namespace pipewire
Definition graphics.h:287
Definition pipewire.cpp:607
virtual void verify_and_update_display_parameters()
Verify and update display parameters for logical dimensions, desktop dimensions and logical desktop d...
Definition pipewire.cpp:639
platf::capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override
Capture a frame.
Definition pipewire.cpp:813
virtual int configure_stream(const std::string &display_name, int &out_pipewire_fd, uint32_t &out_pipewire_node, uint64_t &out_pipewire_objectserial)=0
Configure the pipewire stream.
Definition pipewire.cpp:98
Definition common.h:468
std::function< bool(std::shared_ptr< img_t > &&img, bool frame_captured)> push_captured_image_cb_t
Callback for when a new image is ready. When display has a new image ready or a timeout occurs,...
Definition common.h:477
std::function< bool(std::shared_ptr< img_t > &img_out)> pull_free_image_cb_t
Get free image from pool. Calls must be synchronized. Blocks until there is free image in the pool or...
Definition common.h:486
Definition wayland.h:174
int init(const char *display_name=nullptr)
Initialize display. If display_name == nullptr -> display_name = std::getenv("WAYLAND_DISPLAY")
Definition wayland.cpp:51
Declarations for common platform specific utilities.
mem_type_e
Definition common.h:229
pix_fmt_e
Definition common.h:239
capture_e
Definition common.h:460
@ timeout
Timeout.
std::unique_ptr< platf::avcodec_encode_device_t > make_avcodec_gl_encode_device(int width, int height, int offset_x, int offset_y)
Create a GL->CUDA encoding device for consuming captured dmabufs.
Definition cuda.cpp:492
Definitions for CUDA implementation.
Declarations for graphics related functions.
Declarations for the main entry point for Sunshine.
Definition pipewire.cpp:92
Definition pipewire.cpp:42
Definition pipewire.cpp:57
Definition pipewire.cpp:68
Definition common.h:370
Declarations for VA-API hardware accelerated capture.
Declarations for video.
Declarations for FFmpeg Vulkan Video encoder.
Declarations for Wayland capture.