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>
27#if !PW_CHECK_VERSION(1, 6, 0)
31#if PW_CHECK_VERSION(0, 3, 75)
34#elifdef PW_KEY_TARGET_OBJECT
44 #define PW_KEY_TARGET_OBJECT "target.object"
49 constexpr int SPA_POD_BUFFER_SIZE = 4096;
50 constexpr int MAX_PARAMS = 200;
51 constexpr int MAX_DMABUF_FORMATS = 200;
52 constexpr int MAX_DMABUF_MODIFIERS = 200;
55using namespace std::literals;
66 static constexpr std::array<format_map_t, 7> format_map = {{
67 {DRM_FORMAT_XBGR2101010, SPA_VIDEO_FORMAT_xBGR_210LE},
68 {DRM_FORMAT_BGRA1010102, SPA_VIDEO_FORMAT_ARGB_210LE},
69 {DRM_FORMAT_RGBA1010102, SPA_VIDEO_FORMAT_ABGR_210LE},
70 {DRM_FORMAT_ABGR2101010, SPA_VIDEO_FORMAT_RGBA_102LE},
71 {DRM_FORMAT_ARGB2101010, SPA_VIDEO_FORMAT_BGRA_102LE},
72 {DRM_FORMAT_ARGB8888, SPA_VIDEO_FORMAT_BGRA},
73 {DRM_FORMAT_XRGB8888, SPA_VIDEO_FORMAT_BGRx},
93 struct stream_data_t {
99 std::shared_ptr<shared_state_t>
shared;
150 loop(pw_thread_loop_new(
"Pipewire thread",
nullptr)) {
151 BOOST_LOG(
debug) <<
"[pipewire] Start PW thread loop"sv;
152 pw_thread_loop_start(loop);
156 BOOST_LOG(
debug) <<
"[pipewire] Destroying pipewire_t"sv;
157 pw_thread_loop_lock(loop);
160 BOOST_LOG(
debug) <<
"[pipewire] Stop fill_img"sv;
162 std::scoped_lock lock(stream_data.frame_mutex);
163 stream_data.frame_ready =
false;
164 stream_data.current_buffer =
nullptr;
168 if (stream_data.stream) {
169 BOOST_LOG(
debug) <<
"[pipewire] Disconnect stream"sv;
170 pw_stream_disconnect(stream_data.stream);
171 BOOST_LOG(
debug) <<
"[pipewire] Destroy stream"sv;
172 pw_stream_destroy(stream_data.stream);
173 stream_data.stream =
nullptr;
177 BOOST_LOG(
debug) <<
"[pipewire] Disconnect PW core"sv;
178 pw_core_disconnect(core);
183 BOOST_LOG(
debug) <<
"[pipewire] Destroy PW context"sv;
184 pw_context_destroy(context);
189 BOOST_LOG(
debug) <<
"[pipewire] Close pipewire_fd"sv;
193 BOOST_LOG(
debug) <<
"[pipewire] Stop PW thread loop"sv;
194 pw_thread_loop_unlock(loop);
195 pw_thread_loop_stop(loop);
196 BOOST_LOG(
debug) <<
"[pipewire] Destroy PW thread loop"sv;
197 pw_thread_loop_destroy(loop);
206 return stream_data.frame_mutex;
215 return stream_data.frame_cv;
224 return stream_data.frame_ready;
235 AVRational negotiated_rate =
237 static_cast<int32_t
>(stream_data.format.info.raw.max_framerate.num),
238 static_cast<int32_t
>(stream_data.format.info.raw.max_framerate.denom)
240 int rate_comparison = av_cmp_q(negotiated_rate, requested_framerate);
241 bool variable_rate = negotiated_rate.num == 0 && negotiated_rate.den == 1;
242 bool pacing_required = variable_rate || rate_comparison > 0;
244 if (!variable_rate && rate_comparison < 0) {
246 <<
"[pipewire] Sunshine frame pacing: disabled (negotiated rate lower than requested rate)"sv;
248 BOOST_LOG(
info) <<
"[pipewire] Sunshine frame pacing: "sv
249 << (pacing_required ? std::format(
"enabled ({}ms)", std::chrono::duration<double, std::milli>(requested_delay).count()) :
"disabled (event-driven capture)");
252 return pacing_required;
261 stream_data.frame_ready = ready;
273 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) {
276 object_serial = stream_object_serial;
277 stream_data.shared = std::move(shared_state);
279 pw_thread_loop_lock(loop);
280 BOOST_LOG(
debug) <<
"[pipewire] Setup PW context"sv;
281 context = pw_context_new(pw_thread_loop_get_loop(loop),
nullptr, 0);
283 BOOST_LOG(
debug) <<
"[pipewire] Connect PW context to fd"sv;
285 core = pw_context_connect_fd(context, fd,
nullptr, 0);
287 core = pw_context_connect(context,
nullptr, 0);
290 pw_core_add_listener(core, &core_listener, &core_events,
nullptr);
292 BOOST_LOG(
debug) <<
"[pipewire] Failed to connect to PW core. Error: "sv << errno <<
"(" << strerror(errno) <<
")"sv;
296 BOOST_LOG(
debug) <<
"[pipewire] Failed to setup PW context. Error: "sv << errno <<
"(" << strerror(errno) <<
")"sv;
300 pw_thread_loop_unlock(loop);
317 pw_thread_loop_lock(loop);
319 if (!stream_data.stream) {
321 BOOST_LOG(
debug) <<
"[pipewire] PW core not available. Cannot ensure stream."sv;
322 pw_thread_loop_unlock(loop);
326 struct pw_properties *props = pw_properties_new(PW_KEY_MEDIA_TYPE,
"Video", PW_KEY_MEDIA_CATEGORY,
"Capture", PW_KEY_MEDIA_ROLE,
"Screen",
nullptr);
328 BOOST_LOG(
debug) <<
"[pipewire] Create PW stream"sv;
329 stream_data.stream = pw_stream_new(core,
"Sunshine Video Capture", props);
330 pw_stream_add_listener(stream_data.stream, &stream_data.stream_listener, &stream_events, &stream_data);
332 std::array<uint8_t, SPA_POD_BUFFER_SIZE> buffer;
333 struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(buffer.data(), buffer.size());
336 std::array<const struct spa_pod *, MAX_PARAMS> params;
346 for (
int i = 0; i < n_dmabuf_infos; i++) {
347 auto format_param = build_format_parameter(&pod_builder, width, height, target_framerate, dmabuf_infos[i].format, dmabuf_infos[i].modifiers, dmabuf_infos[i].n_modifiers);
348 params[n_params] = format_param;
354 for (
const auto &fmt : format_map) {
355 auto format_param = build_format_parameter(&pod_builder, width, height, target_framerate, fmt.pw_format,
nullptr, 0);
356 params[n_params] = format_param;
363 BOOST_LOG(
debug) <<
"[pipewire] Connect PW stream - fd: "sv << fd <<
" object serial: "sv << object_serial;
364 result = pw_stream_connect(stream_data.stream, PW_DIRECTION_INPUT, PW_ID_ANY, (
enum pw_stream_flags)(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS), params.data(), n_params);
374 BOOST_LOG(
debug) <<
"[pipewire] Connect PW stream - fd: "sv << fd <<
" node: "sv << node;
375 result = 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);
379 pw_thread_loop_unlock(loop);
389 for (
int &fd : img_descriptor->
sd.
fds) {
423 return {kwin, std::move(version)};
427 return {gnome, std::move(version)};
440#if !GLIB_CHECK_VERSION(2, 74, 0)
442 constexpr auto G_REGEX_DEFAULT =
static_cast<GRegexCompileFlags
>(0);
443 constexpr auto G_REGEX_MATCH_DEFAULT =
static_cast<GRegexMatchFlags
>(0);
445 auto conn = g_bus_get_sync(G_BUS_TYPE_SESSION,
nullptr,
nullptr);
446 std::vector<int> result;
452 const gchar *version_regex;
456 if (compositor_type == kwin) {
457 version_regex =
"KWin version: ([0-9]+)\\.([0-9]+)\\.([0-9]+)";
458 reply = g_dbus_connection_call_sync(
463 "supportInformation",
465 G_VARIANT_TYPE(
"(s)"),
466 G_DBUS_CALL_FLAGS_NONE,
472 version_regex =
"([0-9]+)\\.([0-9]+)(?:\\.([0-9]+))?";
473 reply = g_dbus_connection_call_sync(
477 "org.freedesktop.DBus.Properties",
479 g_variant_new(
"(ss)",
"org.gnome.Shell",
"ShellVersion"),
480 G_VARIANT_TYPE(
"(v)"),
481 G_DBUS_CALL_FLAGS_NONE,
489 g_clear_object(&conn);
493 g_autofree gchar *version_str =
nullptr;
495 if (compositor_type == kwin) {
496 g_variant_get(reply,
"(s)", &version_str);
498 GVariant *inner_variant =
nullptr;
499 g_variant_get(reply,
"(v)", &inner_variant);
501 version_str = g_variant_dup_string(inner_variant,
nullptr);
502 g_variant_unref(inner_variant);
507 g_variant_unref(reply);
508 g_clear_object(&conn);
512 if (
auto *regex = g_regex_new(version_regex, G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT,
nullptr); regex) {
513 GMatchInfo *match_info =
nullptr;
514 g_regex_match(regex, version_str, G_REGEX_MATCH_DEFAULT, &match_info);
516 if (g_match_info_matches(match_info)) {
517 g_autofree
const gchar *major = g_match_info_fetch(match_info, 1);
518 g_autofree
const gchar *minor = g_match_info_fetch(match_info, 2);
519 g_autofree
const gchar *micro = g_match_info_fetch(match_info, 3);
522 result.emplace_back(std::atoi(major));
525 result.emplace_back(std::atoi(minor));
527 if (micro && *micro !=
'\0') {
528 result.emplace_back(std::atoi(micro));
531 g_match_info_free(match_info);
532 g_regex_unref(regex);
535 g_variant_unref(reply);
536 g_clear_object(&conn);
552 bool use_pts =
false;
553 bool using_virtual_monitor =
false;
556 switch (compositor.
type) {
558 using_virtual_monitor = selected_display_name.starts_with(
"Meta-");
559 use_pts = (using_virtual_monitor && compositor.
version[0] >= 51);
568 BOOST_LOG(
info) <<
"[pipewire] Using frame_timestamp (pts) metadata source: "sv
569 << (use_pts ?
"Pipewire (via compositor)" : PROJECT_NAME);
590 bool variable_rate =
true;
593 if (compositor.
type == kwin) {
597 return variable_rate;
607 struct spa_meta_header *h =
static_cast<struct spa_meta_header *
>(
608 spa_buffer_find_meta_data(buf, SPA_META_Header,
sizeof(*h))
611 img_descriptor->
seq.reset();
612 img_descriptor->
pts.reset();
614 img_descriptor->
seq = h->seq;
616 img_descriptor->
pts = h->pts;
620 std::chrono::steady_clock::time_point(std::chrono::nanoseconds(img_descriptor->
pts.value())) :
621 std::chrono::steady_clock::now();
623 if (buf->n_datas > 0) {
624 img_descriptor->
pw_flags = buf->datas[0].chunk->flags;
627 struct spa_meta_region *damage =
static_cast<struct spa_meta_region *
>(
628 spa_buffer_find_meta_data(buf, SPA_META_VideoDamage,
sizeof(*damage))
630 img_descriptor->
pw_damage = (damage && damage->region.size.width > 0 && damage->region.size.height > 0) ? std::optional<bool>(
true) : std::nullopt;
645 for (
int i = 0; i < MIN(buf->n_datas, 4); i++) {
646 img_descriptor->
sd.
fds[i] = dup(buf->datas[i].fd);
647 img_descriptor->
sd.
pitches[i] = buf->datas[i].chunk->stride;
648 img_descriptor->
sd.
offsets[i] = buf->datas[i].chunk->offset;
658 pw_thread_loop_lock(loop);
659 std::scoped_lock lock(stream_data.frame_mutex);
661 if (stream_data.shared && stream_data.shared->stream_dead.load()) {
664 pw_thread_loop_unlock(loop);
668 if (!stream_data.current_buffer) {
670 pw_thread_loop_unlock(loop);
674 struct spa_buffer *buf = stream_data.current_buffer->buffer;
675 if (buf->datas[0].chunk->size != 0) {
678 if (buf->datas[0].type == SPA_DATA_DmaBuf) {
681 img->
data = stream_data.front_buffer->data();
682 img_descriptor->data_owned =
false;
683 img->
row_pitch = stream_data.local_stride;
686 img->
pixel_pitch = (stream_data.format.info.raw.format == SPA_VIDEO_FORMAT_NV12) ? 1 : 4;
690 pw_thread_loop_unlock(loop);
699 negotiate_maxframerate_ = negotiate_maxframerate;
706 struct pw_thread_loop *loop;
707 struct pw_context *context;
708 struct pw_core *core;
709 struct spa_hook core_listener;
713 uint64_t object_serial;
714 bool negotiate_maxframerate_ =
true;
716 struct spa_pod *build_format_parameter(
struct spa_pod_builder *b, uint32_t width, uint32_t height, AVRational target_framerate, int32_t format, uint64_t *modifiers,
int n_modifiers) {
717 struct spa_pod_frame object_frame;
718 struct spa_pod_frame modifier_frame;
719 std::array<struct spa_rectangle, 3> sizes;
720 std::array<struct spa_fraction, 3> framerates;
722 sizes[0] = SPA_RECTANGLE(width, height);
723 sizes[1] = SPA_RECTANGLE(1, 1);
724 sizes[2] = SPA_RECTANGLE(8192, 4096);
726 framerates[0] = SPA_FRACTION(uint32_t(target_framerate.num), uint32_t(target_framerate.den));
727 framerates[1] = SPA_FRACTION(0, 1);
728 framerates[2] = SPA_FRACTION(1000, 1);
730 spa_pod_builder_push_object(b, &object_frame, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
731 spa_pod_builder_add(b, SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video), 0);
732 spa_pod_builder_add(b, SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), 0);
733 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_format, SPA_POD_Id(format), 0);
734 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(&sizes[0], &sizes[1], &sizes[2]), 0);
735 if (negotiate_maxframerate_) {
737 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&framerates[1]), 0);
738 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_maxFramerate, SPA_POD_CHOICE_RANGE_Fraction(&framerates[0], &framerates[1], &framerates[2]), 0);
741 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&framerates[0]), 0);
744 if (format == SPA_VIDEO_FORMAT_xBGR_210LE) {
745 spa_pod_builder_add(b, SPA_FORMAT_VIDEO_colorPrimaries, SPA_POD_Id(SPA_VIDEO_COLOR_PRIMARIES_BT2020), 0);
750 spa_pod_builder_prop(b, SPA_FORMAT_VIDEO_modifier, SPA_POD_PROP_FLAG_MANDATORY | SPA_POD_PROP_FLAG_DONT_FIXATE);
751 spa_pod_builder_push_choice(b, &modifier_frame, SPA_CHOICE_Enum, 0);
754 spa_pod_builder_long(b, modifiers[0]);
755 for (uint32_t i = 0; i < n_modifiers; i++) {
756 spa_pod_builder_long(b, modifiers[i]);
759 spa_pod_builder_pop(b, &modifier_frame);
762 return static_cast<struct spa_pod *
>(spa_pod_builder_pop(b, &object_frame));
765 static void on_core_info_cb([[maybe_unused]]
void *user_data,
const struct pw_core_info *pw_info) {
766 BOOST_LOG(
info) <<
"[pipewire] Connected to pipewire version "sv << pw_info->version;
769 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) {
770 BOOST_LOG(
info) <<
"[pipewire] Pipewire Error, id:"sv <<
id <<
" seq:"sv << seq <<
" message: "sv << message;
773 constexpr static const struct pw_core_events core_events = {
774 .version = PW_VERSION_CORE_EVENTS,
775 .info = on_core_info_cb,
776 .error = on_core_error_cb,
779 static void on_stream_state_changed(
void *user_data,
enum pw_stream_state old,
enum pw_stream_state state,
const char *err_msg) {
780 if (err_msg !=
nullptr) {
781 BOOST_LOG(
info) <<
"[pipewire] PipeWire stream error '" << err_msg <<
"' on state: " << pw_stream_state_as_string(old)
782 <<
" -> " << pw_stream_state_as_string(state);
784 BOOST_LOG(
info) <<
"[pipewire] PipeWire stream state: " << pw_stream_state_as_string(old)
785 <<
" -> " << pw_stream_state_as_string(state);
788 auto *d =
static_cast<stream_data_t *
>(user_data);
791 case PW_STREAM_STATE_PAUSED:
792 if (d->shared && old == PW_STREAM_STATE_STREAMING) {
794 std::scoped_lock
lock(d->frame_mutex);
795 d->frame_ready =
false;
796 d->current_buffer =
nullptr;
797 d->shared->stream_dead.store(
true);
798 d->shared->current_state =
state;
799 d->shared->previous_state = old;
800 d->shared->err_msg =
"";
802 d->frame_cv.notify_all();
805 case PW_STREAM_STATE_ERROR:
807 std::scoped_lock
lock(d->frame_mutex);
808 d->shared->current_state =
state;
809 d->shared->previous_state = old;
810 d->shared->err_msg = std::string(err_msg);
813 case PW_STREAM_STATE_UNCONNECTED:
815 d->shared->stream_dead.store(
true);
816 d->frame_cv.notify_all();
824 static void on_process(
void *user_data) {
825 const auto d =
static_cast<struct stream_data_t *
>(user_data);
826 struct pw_buffer *b =
nullptr;
829 while (
struct pw_buffer *aux = pw_stream_dequeue_buffer(d->stream)) {
831 pw_stream_queue_buffer(d->stream, b);
841 if (b->buffer->datas[0].type == SPA_DATA_DmaBuf) {
842 std::scoped_lock
lock(d->frame_mutex);
843 if (d->current_buffer) {
844 pw_stream_queue_buffer(d->stream, d->current_buffer);
846 d->current_buffer = b;
847 d->frame_ready =
true;
850 else if (b->buffer->datas[0].data !=
nullptr) {
851 size_t size = b->buffer->datas[0].chunk->size;
854 if (d->back_buffer->size() < size) {
855 d->back_buffer->resize(size);
857 std::memcpy(d->back_buffer->data(), b->buffer->datas[0].data, size);
861 std::scoped_lock
lock(d->frame_mutex);
862 std::swap(d->front_buffer, d->back_buffer);
864 d->local_stride = b->buffer->datas[0].chunk->stride;
865 d->frame_ready =
true;
866 d->current_buffer = b;
870 pw_stream_queue_buffer(d->stream, b);
873 d->frame_cv.notify_one();
876 static void on_param_changed(
void *user_data, uint32_t
id,
const struct spa_pod *param) {
877 const auto d =
static_cast<struct stream_data_t *
>(user_data);
879 d->current_buffer =
nullptr;
881 if (param ==
nullptr ||
id != SPA_PARAM_Format) {
884 if (spa_format_parse(param, &d->format.media_type, &d->format.media_subtype) < 0) {
887 if (d->format.media_type != SPA_MEDIA_TYPE_video || d->format.media_subtype != SPA_MEDIA_SUBTYPE_raw) {
890 if (spa_format_video_raw_parse(param, &d->format.info.raw) < 0) {
894 BOOST_LOG(
info) <<
"[pipewire] Video format: "sv << d->format.info.raw.format;
895 BOOST_LOG(
info) <<
"[pipewire] Size: "sv << d->format.info.raw.size.width <<
"x"sv << d->format.info.raw.size.height;
896 BOOST_LOG(
info) <<
"[pipewire] Color primaries: "sv << d->format.info.raw.color_primaries;
897 BOOST_LOG(
info) <<
"[pipewire] Transfer function: "sv << d->format.info.raw.transfer_function;
898 if (d->format.info.raw.max_framerate.num == 0 && d->format.info.raw.max_framerate.denom == 1) {
899 BOOST_LOG(
info) <<
"[pipewire] Compositor negotiated frame rate: 0/1 (variable rate capture)"sv;
901 BOOST_LOG(
info) <<
"[pipewire] Compositor negotiated frame rate: "sv
902 << d->format.info.raw.framerate.num <<
"/"sv << d->format.info.raw.framerate.denom
903 <<
", max: "sv << d->format.info.raw.max_framerate.num <<
"/"sv << d->format.info.raw.max_framerate.denom;
906 int physical_w = d->format.info.raw.size.width;
907 int physical_h = d->format.info.raw.size.height;
910 int old_w = d->shared->negotiated_width.load();
911 int old_h = d->shared->negotiated_height.load();
912 int old_color_primaries = d->shared->color_primaries.load();
913 int old_transfer_function = d->shared->transfer_function.load();
915 if (physical_w != old_w || physical_h != old_h) {
916 d->shared->negotiated_width.store(physical_w);
917 d->shared->negotiated_height.store(physical_h);
920 if (d->format.info.raw.color_primaries != old_color_primaries || d->format.info.raw.transfer_function != old_transfer_function) {
921 d->shared->color_primaries.store(d->format.info.raw.color_primaries);
922 d->shared->transfer_function.store(d->format.info.raw.transfer_function);
926 uint64_t drm_format = 0;
927 for (
const auto &fmt : format_map) {
928 if (fmt.pw_format == d->format.info.raw.format) {
929 drm_format = fmt.fourcc;
932 d->drm_format = drm_format;
934 uint32_t buffer_types = 0;
935 if (spa_pod_find_prop(param,
nullptr, SPA_FORMAT_VIDEO_modifier) !=
nullptr && d->drm_format) {
936 BOOST_LOG(
info) <<
"[pipewire] using DMA-BUF buffers"sv;
937 buffer_types |= 1 << SPA_DATA_DmaBuf;
939 BOOST_LOG(
info) <<
"[pipewire] using memory buffers"sv;
940 buffer_types |= 1 << SPA_DATA_MemPtr;
944 std::array<uint8_t, SPA_POD_BUFFER_SIZE> buffer;
945 std::array<const struct spa_pod *, 3> params;
947 struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(buffer.data(), buffer.size());
948 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)));
949 params[n_params] = buffer_param;
951 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))));
952 params[n_params] = meta_param;
954 int videoDamageRegionCount = 16;
955 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)));
956 params[n_params] = damage_param;
959 pw_stream_update_params(d->stream, params.data(), n_params);
962 constexpr static const struct pw_stream_events stream_events = {
963 .version = PW_VERSION_STREAM_EVENTS,
964 .state_changed = on_stream_state_changed,
965 .param_changed = on_param_changed,
966 .process = on_process,
983 pw_init(
nullptr,
nullptr);
986 switch (hwdevice_type) {
1006 virtual int configure_stream(
const std::string &display_name,
int &out_pipewire_fd, uint32_t &out_pipewire_node, uint64_t &out_pipewire_objectserial) = 0;
1014 int desktop_width = 0;
1015 int desktop_height = 0;
1016 int desktop_logical_width = 0;
1017 int desktop_logical_height = 0;
1019 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;
1028 desktop_width = std::max(desktop_width, monitor->viewport.offset_x + monitor->viewport.width);
1029 desktop_height = std::max(desktop_height, monitor->viewport.offset_y + monitor->viewport.height);
1031 desktop_logical_width = std::max(desktop_logical_width, monitor->viewport.offset_x + monitor->viewport.logical_width);
1032 desktop_logical_height = std::max(desktop_logical_height, monitor->viewport.offset_y + monitor->viewport.logical_height);
1066 BOOST_LOG(
info) <<
"[pipewire] Requested frame rate: "sv << fps.num <<
"/"sv << fps.den <<
", approx. "sv << av_q2d(fps) <<
" fps"sv;
1067 }
else if (fps.num == 0 && fps.den == 1) {
1068 BOOST_LOG(
info) <<
"[pipewire] Requested variable frame rate (Sunshine pacing required: "sv << std::chrono::duration<double, std::milli>(delay).count() <<
"ms)"sv;
1070 BOOST_LOG(
info) <<
"[pipewire] Requested frame rate: "sv << fps.num <<
"fps"sv;
1072 this->target_framerate = fps;
1073 mem_type = hwdevice_type;
1075 if (get_dmabuf_modifiers() < 0) {
1079 int pipewire_fd = -1;
1080 auto pipewire_node = PW_ID_ANY;
1081 uint64_t pipewire_object_serial = SPA_ID_INVALID;
1083 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)) {
1084 BOOST_LOG(
error) <<
"[pipewire] Could not find display with name: '"sv << display_name <<
"'";
1087 BOOST_LOG(
info) <<
"[pipewire] Streaming display '"sv << display_name <<
"' offset: "sv <<
offset_x <<
"x"sv <<
offset_y <<
" resolution: "sv <<
width <<
"x"sv <<
height;
1106 BOOST_LOG(
error) <<
"[pipewire] Failed to init pipewire. pipewire_t::init() failed.";
1112 BOOST_LOG(
error) <<
"[pipewire] Failed to ensure pipewire stream. pipewire_t::init() failed.";
1117 int timeout_ms = 1500;
1118 int negotiated_w = 0;
1119 int negotiated_h = 0;
1120 while (timeout_ms > 0) {
1123 if (negotiated_w > 0 && negotiated_h > 0) {
1126 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1130 if (negotiated_w > 0 && negotiated_h > 0 && (negotiated_w !=
width || negotiated_h !=
height)) {
1131 width = negotiated_w;
1133 BOOST_LOG(
info) <<
"[pipewire] Using negotiated Resolution: "sv <<
width <<
"x" <<
height;
1159 auto deadline = std::chrono::steady_clock::now() + timeout;
1162 while (std::chrono::steady_clock::now() < deadline) {
1163 if (!wait_for_frame(deadline)) {
1167 if (!pull_free_image_cb(img_out)) {
1176 if ((img_egl->sd.fds[0] >= 0 || img_egl->data !=
nullptr) && !is_buffer_redundant(img_egl)) {
1178 update_metadata(img_egl, retries);
1195 auto img = std::make_shared<img_descriptor_t>();
1199 img->pixel_pitch = 4;
1200 img->row_pitch = img->pixel_pitch *
width;
1202 img->serial = std::numeric_limits<
decltype(img->serial)>::max();
1203 img->data =
nullptr;
1204 img->data_owned =
false;
1205 img->sd.fds.fill(-1);
1231 auto next_frame = std::chrono::steady_clock::now();
1234 BOOST_LOG(
error) <<
"[pipewire] Failed to ensure pipewire stream. capture() failed with error.";
1235 return capture_error;
1250 BOOST_LOG(
warning) <<
"[pipewire] PipeWire stream disconnected. Forcing session reset."sv;
1255 if (pacing_required) {
1259 std::shared_ptr<platf::img_t> img_out;
1260 switch (
const auto status =
snapshot(pull_free_image_cb, img_out, 1000ms, *cursor)) {
1267 if (!pull_free_image_cb(img_out)) {
1269 BOOST_LOG(
debug) <<
"[pipewire] PipeWire: timeout -> shutdown pending -> interrupt nudge";
1273 if (!push_captured_image_cb(std::move(img_out),
false)) {
1274 BOOST_LOG(
debug) <<
"[pipewire] PipeWire: timeout -> !push_captured_image_cb -> ok";
1279 if (!push_captured_image_cb(std::move(img_out),
true)) {
1280 BOOST_LOG(
debug) <<
"[pipewire] PipeWire: ok -> !push_captured_image_cb -> ok";
1285 BOOST_LOG(
error) <<
"[pipewire] Unrecognized capture status ["sv << std::to_underlying(status) <<
']';
1300#ifdef SUNSHINE_BUILD_VAAPI
1306#ifdef SUNSHINE_BUILD_VULKAN
1312#ifdef SUNSHINE_BUILD_CUDA
1314 if (display_is_nvidia && n_dmabuf_infos > 0) {
1325 return std::make_unique<platf::avcodec_encode_device_t>();
1339 if (img->
data ==
nullptr) {
1340 const auto w = img->
width;
1341 const auto h = img->
height;
1342 if (w > 0 && h > 0) {
1343 img->
data =
new uint8_t[
static_cast<size_t>(w) * h * 4]();
1358 int color_primaries =
shared_state->color_primaries.load();
1359 int transfer_function =
shared_state->transfer_function.load();
1375 int color_primaries =
shared_state->color_primaries.load();
1376 int transfer_function =
shared_state->transfer_function.load();
1380 metadata.displayPrimaries[0].x = 0.708f * 50000;
1381 metadata.displayPrimaries[0].y = 0.292f * 50000;
1382 metadata.displayPrimaries[1].x = 0.170f * 50000;
1383 metadata.displayPrimaries[1].y = 0.797f * 50000;
1384 metadata.displayPrimaries[2].x = 0.131f * 50000;
1385 metadata.displayPrimaries[2].y = 0.046f * 50000;
1386 metadata.whitePoint.x = 0.3127f * 50000;
1387 metadata.whitePoint.y = 0.3290f * 50000;
1390 metadata.maxDisplayLuminance = 4000;
1391 metadata.minDisplayLuminance = 1;
1394 metadata.maxContentLightLevel = 0;
1395 metadata.maxFrameAverageLightLevel = 0;
1396 metadata.maxFullFrameLuminance = 0;
1407 if (img->
pw_flags.has_value() && (img->
pw_flags.value() & SPA_CHUNK_FLAG_CORRUPTED)) {
1412 if (img->
pts.has_value() && last_pts.has_value() && img->
pts.value() == last_pts.value()) {
1419 void update_metadata(egl::img_descriptor_t *img,
int retries) {
1420 last_seq = img->
seq;
1421 last_pts = img->
pts;
1425 BOOST_LOG(
debug) <<
"[pipewire] Processed frame after " << retries <<
" redundant events."sv;
1429 bool wait_for_frame(std::chrono::steady_clock::time_point deadline) {
1443 static bool pw_format_supported(uint64_t fourcc, std::array<EGLint, MAX_DMABUF_FORMATS> dmabuf_formats) {
1444 for (
const auto &drm_format : dmabuf_formats) {
1445 if (drm_format == fourcc) {
1452 void query_dmabuf_formats(EGLDisplay egl_display) {
1453 EGLint num_dmabuf_formats = 0;
1454 std::array<EGLint, MAX_DMABUF_FORMATS> dmabuf_formats = {0};
1455 eglQueryDmaBufFormatsEXT(egl_display, MAX_DMABUF_FORMATS, dmabuf_formats.data(), &num_dmabuf_formats);
1457 if (num_dmabuf_formats > MAX_DMABUF_FORMATS) {
1458 BOOST_LOG(
warning) <<
"[pipewire] Some DMA-BUF formats are being ignored"sv;
1461 for (
const auto &fmt : format_map) {
1462 if (n_dmabuf_infos >= MAX_DMABUF_FORMATS) {
1466 if (!pw_format_supported(fmt.fourcc, dmabuf_formats)) {
1470 EGLint num_modifiers = 0;
1471 std::array<EGLuint64KHR, MAX_DMABUF_MODIFIERS> mods = {0};
1472 eglQueryDmaBufModifiersEXT(egl_display, fmt.fourcc, MAX_DMABUF_MODIFIERS, mods.data(),
nullptr, &num_modifiers);
1474 if (num_modifiers > MAX_DMABUF_MODIFIERS) {
1475 BOOST_LOG(
warning) <<
"[pipewire] Some DMA-BUF modifiers are being ignored"sv;
1478 dmabuf_infos[n_dmabuf_infos].format = fmt.pw_format;
1479 dmabuf_infos[n_dmabuf_infos].n_modifiers = MIN(num_modifiers, MAX_DMABUF_MODIFIERS);
1480 dmabuf_infos[n_dmabuf_infos].modifiers =
1481 static_cast<uint64_t *
>(g_memdup2(mods.data(),
sizeof(uint64_t) * dmabuf_infos[n_dmabuf_infos].n_modifiers));
1486 int get_dmabuf_modifiers() {
1487 if (wl_display.init() < 0) {
1500 bool has_intel_gpu = std::ifstream(
"/sys/class/drm/card0/device/vendor").good() ||
1501 std::ifstream(
"/sys/class/drm/card1/device/vendor").good();
1502 if (has_intel_gpu) {
1504 auto check_intel = [](
const std::string &path) {
1505 if (std::ifstream f(path); f.good()) {
1508 return vendor ==
"0x8086";
1512 bool intel_present = check_intel(
"/sys/class/drm/card0/device/vendor") ||
1513 check_intel(
"/sys/class/drm/card1/device/vendor");
1514 if (intel_present) {
1515 BOOST_LOG(
info) <<
"[pipewire] Hybrid GPU system detected (Intel + discrete) - CUDA will use memory buffers"sv;
1516 display_is_nvidia =
false;
1519 const char *vendor = eglQueryString(egl_display.get(), EGL_VENDOR);
1520 if (vendor && std::string_view(vendor).contains(
"NVIDIA")) {
1521 BOOST_LOG(
info) <<
"[pipewire] Pure NVIDIA system - DMA-BUF will be enabled for CUDA"sv;
1522 display_is_nvidia =
true;
1527 if (eglQueryDmaBufFormatsEXT && eglQueryDmaBufModifiersEXT) {
1528 query_dmabuf_formats(egl_display.get());
1535 wl::display_t wl_display;
1536 std::array<struct dmabuf_format_info_t, MAX_DMABUF_FORMATS> dmabuf_infos;
1538 bool display_is_nvidia =
false;
1539 std::chrono::nanoseconds delay;
1540 std::optional<std::uint64_t> last_pts {};
1541 std::optional<std::uint64_t> last_seq {};
1542 std::uint64_t sequence {};
1543 AVRational target_framerate;
Captured image descriptor shared by EGL conversion paths.
Definition graphics.h:609
std::optional< bool > pw_damage
Whether PipeWire damage tracking should be used.
Definition graphics.h:639
std::optional< uint32_t > pw_flags
PipeWire frame flags reported with the buffer.
Definition graphics.h:640
std::optional< uint64_t > seq
PipeWire frame sequence number.
Definition graphics.h:638
std::uint64_t sequence
Monotonic value used to detect when GL resources must be recreated.
Definition graphics.h:631
surface_descriptor_t sd
DMA-BUF surface descriptor for the captured image.
Definition graphics.h:628
void reset()
Reset the object to its initial empty state.
Definition graphics.h:618
std::optional< uint64_t > pts
PipeWire presentation timestamp.
Definition graphics.h:637
void reset()
Reset the object to its initial empty state.
Definition logging.h:309
Display capture backend that consumes frames from a PipeWire stream.
Definition pipewire.cpp:973
bool is_hdr() override
Report whether the active display mode is HDR.
Definition pipewire.cpp:1357
virtual void verify_and_update_display_parameters()
Verify and update display parameters for logical dimensions, desktop dimensions and logical desktop d...
Definition pipewire.cpp:1011
std::shared_ptr< platf::img_t > alloc_img() override
Allocate an image buffer compatible with this display backend.
Definition pipewire.cpp:1193
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)
Capture a display frame into the provided image object.
Definition pipewire.cpp:1157
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 PipeWire frames until capture stops or the stream needs reinitialization.
Definition pipewire.cpp:1228
bool get_hdr_metadata(SS_HDR_METADATA &metadata) override
Read HDR metadata for the active display mode.
Definition pipewire.cpp:1374
pipewire_t pipewire
Pipewire.
Definition pipewire.cpp:1547
int dummy_img(platf::img_t *img) override
Populate a fallback image when real capture data is unavailable.
Definition pipewire.cpp:1334
std::shared_ptr< shared_state_t > shared_state
Shared state.
Definition pipewire.cpp:1548
virtual bool check_stream_dead(platf::capture_e &out_status)
Check stream dead.
Definition pipewire.cpp:1216
static bool init_pipewire_and_check_hwdevice_type(platf::mem_type_e hwdevice_type)
Initialize pipewire and check hwdevice type.
Definition pipewire.cpp:981
std::unique_ptr< platf::avcodec_encode_device_t > make_avcodec_encode_device(platf::pix_fmt_e pix_fmt) override
Create AVCodec encode device.
Definition pipewire.cpp:1299
int init(platf::mem_type_e hwdevice_type, const std::string &display_name, const ::video::config_t &config)
Initialize the PipeWire display backend for a selected stream.
Definition pipewire.cpp:1055
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.
PipeWire core, context, and stream setup used for screencast capture.
Definition pipewire.cpp:147
static bool use_pipewire_pts(const compositor_version_t &compositor, const std::string &selected_display_name)
Determine if Pipewire's pts metadata is suitable for client pacing based on compositor type/version w...
Definition pipewire.cpp:547
static std::atomic prefer_pipewire_pts
Whether the session should directly passthrough Pipewire pts metadata for client pacing.
Definition pipewire.cpp:702
void fill_img(platf::img_t *img)
Copy the latest PipeWire frame into Sunshine's image buffer.
Definition pipewire.cpp:657
bool is_pacing_required(AVRational requested_framerate, std::chrono::nanoseconds requested_delay)
Check and log whether the active session will require Sunshine to perform pacing.
Definition pipewire.cpp:234
std::condition_variable & frame_cv()
Return the condition variable signaled when frame state changes.
Definition pipewire.cpp:214
static compositor_version_t get_running_compositor()
Fetch compositor type and version for processing.
Definition pipewire.cpp:419
compositor_type_e
Compositors that require version-based checks for optimal stream setup.
Definition pipewire.cpp:400
@ unknown
Unknown.
Definition pipewire.cpp:401
bool is_frame_ready() const
Check whether frame ready.
Definition pipewire.cpp:223
static std::vector< int > get_running_compositor_version(enum compositor_type_e compositor_type)
Fetch compositor version information using DBus calls.
Definition pipewire.cpp:439
static void fill_img_dmabuf(egl::img_descriptor_t *img_descriptor, struct spa_buffer *buf, const stream_data_t &d)
Populate a Sunshine image descriptor from PipeWire DMA-BUF planes.
Definition pipewire.cpp:640
void set_negotiate_maxframerate(bool negotiate_maxframerate)
Set negotiate maxframerate.
Definition pipewire.cpp:698
void set_frame_ready(bool ready)
Set frame ready.
Definition pipewire.cpp:260
int ensure_stream(const platf::mem_type_e mem_type, const uint32_t width, const uint32_t height, const AVRational target_framerate, const struct dmabuf_format_info_t *dmabuf_infos, const int n_dmabuf_infos, const bool display_is_nvidia)
Create the PipeWire stream if it is not already active.
Definition pipewire.cpp:316
static bool use_variable_rate(const compositor_version_t &compositor)
Determine if the active compositor is suited for variable rate capture based on type/version whitelis...
Definition pipewire.cpp:580
std::mutex & frame_mutex()
Return the mutex protecting PipeWire frame state.
Definition pipewire.cpp:205
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)
Initialize PipeWire core objects and optional stream negotiation.
Definition pipewire.cpp:273
static std::atomic negotiate_variable_rate
Whether the session should request variable rate (maxFrameRate = 0/1) capture.
Definition pipewire.cpp:703
static void fill_img_metadata(egl::img_descriptor_t *img_descriptor, struct spa_buffer *buf)
Copy PipeWire metadata into the Sunshine image descriptor.
Definition pipewire.cpp:606
static void close_img_fds(egl::img_descriptor_t *img_descriptor)
Close img fds.
Definition pipewire.cpp:388
Abstract display capture backend used by the streaming pipeline.
Definition common.h:694
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:703
int env_height
Height of the full capture environment in physical pixels.
Definition common.h:803
int env_logical_height
Height of the full capture environment after display scaling.
Definition common.h:805
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:712
logging::time_delta_periodic_logger sleep_overshoot_logger
Periodic logger for capture sleep overshoot measurements.
Definition common.h:813
int offset_y
Vertical capture offset in physical pixels.
Definition common.h:799
int width
Width of the captured display in physical pixels.
Definition common.h:806
int logical_height
Height of the captured display after display scaling.
Definition common.h:809
int env_width
Width of the full capture environment in physical pixels.
Definition common.h:802
int height
Height of the captured display in physical pixels.
Definition common.h:807
int offset_x
Horizontal capture offset in physical pixels.
Definition common.h:798
int env_logical_width
Width of the full capture environment after display scaling.
Definition common.h:804
int logical_width
Width of the captured display after display scaling.
Definition common.h:808
Declarations for common platform specific utilities.
mem_type_e
Enumerates supported mem type options.
Definition common.h:329
@ cuda
CUDA.
Definition common.h:333
@ vulkan
Vulkan.
Definition common.h:335
@ vaapi
VAAPI.
Definition common.h:331
pix_fmt_e
Enumerates supported pix fmt options.
Definition common.h:342
capture_e
Enumerates supported capture options.
Definition common.h:683
@ ok
Success.
Definition common.h:684
@ timeout
Timeout.
Definition common.h:686
@ interrupted
Capture was interrupted.
Definition common.h:687
std::unique_ptr< platf::avcodec_encode_device_t > make_avcodec_encode_device(int width, int height, bool vram)
Create AVCodec encode device.
Definition cuda.cpp:655
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:683
Definitions for CUDA implementation.
display_t make_display(std::variant< gbm::gbm_t::pointer, wl_display *, _XDisplay * > native_display)
Open and initialize the display connection used for capture.
Definition graphics.cpp:495
Declarations for graphics related functions.
int close(int __fd)
Release the native resource held by the RAII wrapper.
void handle_pacing(std::chrono::steady_clock::time_point &next_frame, std::chrono::nanoseconds delay, auto &logger)
Generic frame pacing logic for Linux capture methods.
Definition misc.h:152
bl::sources::severity_logger< int > debug
Follow what is happening.
Definition logging.cpp:43
bl::sources::severity_logger< int > error
Recoverable errors.
Definition logging.cpp:46
bl::sources::severity_logger< int > info
Should be informed about.
Definition logging.cpp:44
bl::sources::severity_logger< int > warning
Strange events.
Definition logging.cpp:45
Declarations for the main entry point for Sunshine.
constexpr int SPA_VIDEO_TRANSFER_SMPTE2084
Protocol or platform constant for spa video transfer smpte2084.
Definition pipewire.cpp:28
constexpr bool SUNSHINE_USE_PIPEWIRE_OBJECT_SERIAL
Whether PipeWire object serials should be used for matching.
Definition pipewire.cpp:39
#define PW_KEY_TARGET_OBJECT
Macro for PW KEY TARGET OBJECT.
Definition pipewire.cpp:44
state_e state(session_t &session)
Platform handle returned from stream setup.
Definition stream.cpp:2200
std::array< std::uint32_t, 4 > pitches
Row stride in bytes for each DMA-BUF plane.
Definition graphics.h:503
std::uint32_t fourcc
DRM fourcc pixel format for the buffer.
Definition graphics.h:501
std::array< std::uint32_t, 4 > offsets
Byte offset to the first pixel for each DMA-BUF plane.
Definition graphics.h:504
std::uint64_t modifier
DRM format modifier describing the buffer layout.
Definition graphics.h:502
std::array< int, 4 > fds
DMA-BUF file descriptors for up to four planes.
Definition graphics.h:500
int height
Frame or display height in pixels.
Definition graphics.h:499
int width
Frame or display width in pixels.
Definition graphics.h:498
Pipewire image assembled for encoding.
Definition pipewire.cpp:129
bool data_owned
Whether img->data is owned by this image and must be freed.
Definition pipewire.cpp:141
Compositor type and version information.
Definition pipewire.cpp:409
compositor_type_e type
Compositor type (gnome, kde...).
Definition pipewire.cpp:410
std::vector< int > version
Compositor version.
Definition pipewire.cpp:411
PipeWire capture state shared with callback threads.
Definition pipewire.cpp:79
std::atomic< int > negotiated_height
Height negotiated with PipeWire for the stream.
Definition pipewire.cpp:81
std::atomic< bool > stream_dead
Whether the PipeWire stream has been destroyed.
Definition pipewire.cpp:84
std::atomic< int > color_primaries
PipeWire color-primaries metadata for the stream.
Definition pipewire.cpp:82
pw_stream_state previous_state
Previous PipeWire stream state reported by callbacks.
Definition pipewire.cpp:85
std::atomic< int > transfer_function
PipeWire transfer-function metadata for the stream.
Definition pipewire.cpp:83
pw_stream_state current_state
Current PipeWire stream state reported by callbacks.
Definition pipewire.cpp:86
std::atomic< int > negotiated_width
Width negotiated with PipeWire for the stream.
Definition pipewire.cpp:80
std::string err_msg
Last PipeWire error message reported by the stream.
Definition pipewire.cpp:87
PipeWire stream handle, format, and shared state pointer.
Definition pipewire.cpp:93
struct spa_hook stream_listener
Hook registering callbacks on the PipeWire stream.
Definition pipewire.cpp:95
std::condition_variable frame_cv
Signals arrival or release of a PipeWire frame.
Definition pipewire.cpp:101
std::mutex frame_mutex
Synchronizes access to the current PipeWire frame.
Definition pipewire.cpp:100
struct spa_video_info format
Negotiated PipeWire video format.
Definition pipewire.cpp:96
std::shared_ptr< shared_state_t > shared
State shared between PipeWire callbacks and the capture backend.
Definition pipewire.cpp:99
struct pw_stream * stream
PipeWire stream handle used for screencast frames.
Definition pipewire.cpp:94
std::vector< uint8_t > buffer_a
First staging buffer used for CPU-copy PipeWire frames.
Definition pipewire.cpp:105
std::vector< uint8_t > buffer_b
Second staging buffer used for CPU-copy PipeWire frames.
Definition pipewire.cpp:106
std::vector< uint8_t > * front_buffer
Staging buffer currently readable by fill_img.
Definition pipewire.cpp:108
std::vector< uint8_t > * back_buffer
Staging buffer currently writable by PipeWire callbacks.
Definition pipewire.cpp:110
uint64_t drm_format
DRM format.
Definition pipewire.cpp:98
size_t local_stride
Local stride.
Definition pipewire.cpp:102
struct pw_buffer * current_buffer
PipeWire buffer currently exposed to the capture thread.
Definition pipewire.cpp:97
bool frame_ready
Whether a PipeWire frame is ready to consume.
Definition pipewire.cpp:103
Captured frame buffer shared between capture and encode stages.
Definition common.h:530
std::int32_t row_pitch
Bytes between consecutive image rows.
Definition common.h:543
std::uint8_t * data
Pointer to the captured image buffer.
Definition common.h:539
std::int32_t pixel_pitch
Bytes per pixel in the image buffer.
Definition common.h:542
std::int32_t height
Image height in pixels.
Definition common.h:541
std::int32_t width
Image width in pixels.
Definition common.h:540
std::optional< std::chrono::steady_clock::time_point > frame_timestamp
Capture timestamp associated with the frame.
Definition common.h:545
auto lock(const std::weak_ptr< void > &wp)
Acquire the underlying lock or keyed mutex.
Definition thread_safe.h:794
std::unique_ptr< platf::avcodec_encode_device_t > make_avcodec_encode_device(int width, int height, file_t &&card, int offset_x, int offset_y, bool vram)
Create AVCodec encode device.
Definition vaapi.cpp:798
Declarations for VA-API hardware accelerated capture.
std::chrono::nanoseconds capture_frame_interval(const config_t &config)
Capture frame interval for the requested framerate. Uses the exact fractional rate when the client pr...
Definition video.h:781
AVRational framerate_to_rational(const config_t &config)
Requested framerate as an exact rational. Uses the exact fractional rate when the client provided an ...
Definition video.h:767
std::unique_ptr< platf::avcodec_encode_device_t > make_avcodec_encode_device_vram(int w, int h, int offset_x, int offset_y)
Create a Vulkan encode device for VRAM capture.
Definition vulkan_encode.cpp:1427
Declarations for FFmpeg Vulkan Video encoder.
std::vector< std::unique_ptr< monitor_t > > monitors(const char *display_name)
Refresh the monitor list reported by the display server.
Definition wayland.cpp:687
Declarations for Wayland capture.