fix(linux): select PipeWire converters from negotiated buffers

This commit is contained in:
Dave Lane committed 2026-11-16 17:23:38 -05:00
1 parent 7cf6fbdfbb
commit 9ecb05ebfe
2 files changed
+174 -21

No files matched your search

+86 -20
View File
@@ -78,6 +78,7 @@ namespace pipewire {
* @brief PipeWire capture state shared with callback threads.
*/
struct shared_state_t {
std::atomic<uint32_t> buffer_type {SPA_ID_INVALID}; ///< Negotiated buffer type, or unknown until format negotiation finishes.
std::atomic<int> negotiated_width {0}; ///< Width negotiated with PipeWire for the stream.
std::atomic<int> negotiated_height {0}; ///< Height negotiated with PipeWire for the stream.
std::atomic<int> color_primaries {0}; ///< PipeWire color-primaries metadata for the stream.
@@ -273,6 +274,7 @@ namespace pipewire {
object_serial = stream_object_serial;
stream_data.shared = std::move(shared_state);
stream_data.shared->has_streamed.store(false);
stream_data.shared->buffer_type.store(SPA_ID_INVALID);
pw_thread_loop_lock(loop);
BOOST_LOG(debug) << "[pipewire] Setup PW context"sv;
@@ -661,8 +663,10 @@ namespace pipewire {
* @brief Copy the latest PipeWire frame into Sunshine's image buffer.
*
* @param img Image or frame object to read from or populate.
* @param buffer_type Buffer type selected for this display and its encoder.
* @return Capture status, including reinit if the buffer storage changed.
*/
void fill_img(platf::img_t *img) {
platf::capture_e fill_img(platf::img_t *img, uint32_t buffer_type) {
pw_thread_loop_lock(loop);
std::scoped_lock lock(stream_data.frame_mutex);
@@ -670,32 +674,55 @@ namespace pipewire {
img->data = nullptr;
close_img_fds(static_cast<egl::img_descriptor_t *>(img));
pw_thread_loop_unlock(loop);
return;
return platf::capture_e::timeout;
}
if (!stream_data.current_buffer) {
img->data = nullptr;
pw_thread_loop_unlock(loop);
return;
return platf::capture_e::timeout;
}
struct spa_buffer *buf = stream_data.current_buffer->buffer;
if (buf->datas[0].chunk->size != 0) {
auto status = copy_frame(img, stream_data, buffer_type);
pw_thread_loop_unlock(loop);
return status;
}
/**
* @brief Copy a frame only if its storage matches the encoder's selected path.
* @param img Destination image, reset by the caller before copying.
* @param d Frame state, protected by the PipeWire loop and frame locks in production.
* @param buffer_type Buffer type selected before encoder creation.
* @return Reinit on a storage change, otherwise the frame's capture status.
*/
static platf::capture_e copy_frame(platf::img_t *img, const stream_data_t &d, uint32_t buffer_type) {
struct spa_buffer *buf = d.current_buffer->buffer;
if (buf->n_datas == 0 || buf->datas[0].chunk->size == 0) {
return platf::capture_e::timeout;
}
const auto actual_type = buf->datas[0].type;
if (actual_type != SPA_DATA_DmaBuf && actual_type != SPA_DATA_MemPtr && actual_type != SPA_DATA_MemFd) {
return platf::capture_e::capture_error;
}
if ((actual_type == SPA_DATA_DmaBuf) != (buffer_type == SPA_DATA_DmaBuf)) {
return platf::capture_e::reinit;
}
{
auto *img_descriptor = static_cast<img_descriptor_t *>(img);
fill_img_metadata(img_descriptor, buf);
if (buf->datas[0].type == SPA_DATA_DmaBuf) {
fill_img_dmabuf(img_descriptor, buf, stream_data);
fill_img_dmabuf(img_descriptor, buf, d);
} else {
img->data = stream_data.front_buffer->data();
img->data = d.front_buffer->data();
img_descriptor->data_owned = false;
img->row_pitch = stream_data.local_stride;
img->row_pitch = d.local_stride;
// NV12 is the only 1-byte-per-pixel format delivered on the memory
// path; every other negotiated format is packed 4 bytes per pixel.
img->pixel_pitch = (stream_data.format.info.raw.format == SPA_VIDEO_FORMAT_NV12) ? 1 : 4;
img->pixel_pitch = (d.format.info.raw.format == SPA_VIDEO_FORMAT_NV12) ? 1 : 4;
}
}
pw_thread_loop_unlock(loop);
return platf::capture_e::ok;
}
/**
@@ -710,6 +737,19 @@ namespace pipewire {
inline static std::atomic prefer_pipewire_pts {false}; ///< Whether the session should directly passthrough Pipewire pts metadata for client pacing.
inline static std::atomic negotiate_variable_rate {true}; ///< Whether the session should request variable rate (maxFrameRate = 0/1) capture.
/**
* @brief Determine the buffer storage requested by the negotiated format.
* @param param Negotiated SPA format, or null while negotiation is pending.
* @param drm_format DRM format corresponding to the negotiated SPA format.
* @return DMA-BUF with a modifier and known DRM format, memory otherwise; unknown for null.
*/
static uint32_t negotiated_buffer_type(const spa_pod *param, uint64_t drm_format) {
if (!param) {
return SPA_ID_INVALID;
}
return drm_format && spa_pod_find_prop(param, nullptr, SPA_FORMAT_VIDEO_modifier) ? SPA_DATA_DmaBuf : SPA_DATA_MemPtr;
}
private:
struct pw_thread_loop *loop;
struct pw_context *context;
@@ -900,7 +940,13 @@ namespace pipewire {
d->current_buffer = nullptr;
if (param == nullptr || id != SPA_PARAM_Format) {
if (id != SPA_PARAM_Format) {
return;
}
if (d->shared) {
d->shared->buffer_type.store(SPA_ID_INVALID);
}
if (param == nullptr) {
return;
}
if (spa_format_parse(param, &d->format.media_type, &d->format.media_subtype) < 0) {
@@ -954,7 +1000,8 @@ namespace pipewire {
d->drm_format = drm_format;
uint32_t buffer_types = 0;
if (spa_pod_find_prop(param, nullptr, SPA_FORMAT_VIDEO_modifier) != nullptr && d->drm_format) {
const auto buffer_type = negotiated_buffer_type(param, d->drm_format);
if (buffer_type == SPA_DATA_DmaBuf) {
BOOST_LOG(info) << "[pipewire] using DMA-BUF buffers"sv;
buffer_types |= 1 << SPA_DATA_DmaBuf;
} else {
@@ -979,6 +1026,10 @@ namespace pipewire {
n_params++;
pw_stream_update_params(d->stream, params.data(), n_params);
if (d->shared) {
// Publish last: dimensions alone do not mean buffer negotiation is complete.
d->shared->buffer_type.store(buffer_type);
}
}
constexpr static const struct pw_stream_events stream_events = {
@@ -1123,14 +1174,22 @@ namespace pipewire {
int negotiated_w = 0;
int negotiated_h = 0;
while (timeout_ms > 0) {
buffer_type = shared_state->buffer_type.load();
negotiated_w = shared_state->negotiated_width.load();
negotiated_h = shared_state->negotiated_height.load();
if (negotiated_w > 0 && negotiated_h > 0) {
if (negotiated_w > 0 && negotiated_h > 0 && buffer_type != SPA_ID_INVALID) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
timeout_ms -= 10;
}
if (buffer_type == SPA_ID_INVALID) {
// Keep capture alive so portal startup-error handling can retry negotiation.
// Never assume DMA-BUF before negotiation; copy_frame will request reinit
// if a late frame requires a different converter.
BOOST_LOG(warning) << "[pipewire] Buffer negotiation pending; using memory conversion until capture restarts"sv;
buffer_type = SPA_DATA_MemPtr;
}
// Set width and height to the values negotiated by pipewire
if (negotiated_w > 0 && negotiated_h > 0 && (negotiated_w != width || negotiated_h != height)) {
width = negotiated_w;
@@ -1175,10 +1234,13 @@ namespace pipewire {
auto *img_egl = static_cast<egl::img_descriptor_t *>(img_out.get());
img_egl->reset();
pipewire.fill_img(img_egl);
const auto status = pipewire.fill_img(img_egl, buffer_type);
if (status == platf::capture_e::reinit || status == platf::capture_e::capture_error) {
return status;
}
// Check if we got valid data (either DMA-BUF fd or memory pointer), then filter duplicates
if ((img_egl->sd.fds[0] >= 0 || img_egl->data != nullptr) && !is_buffer_redundant(img_egl)) {
if (status == platf::capture_e::ok && (img_egl->sd.fds[0] >= 0 || img_egl->data != nullptr) && !is_buffer_redundant(img_egl)) {
// Update frame metadata
update_metadata(img_egl, retries);
return platf::capture_e::ok;
@@ -1304,24 +1366,27 @@ namespace pipewire {
std::unique_ptr<platf::avcodec_encode_device_t> make_avcodec_encode_device(platf::pix_fmt_e pix_fmt) override {
#ifdef SUNSHINE_BUILD_VAAPI
if (mem_type == platf::mem_type_e::vaapi) {
return va::make_avcodec_encode_device(width, height, n_dmabuf_infos > 0);
return va::make_avcodec_encode_device(width, height, buffer_type == SPA_DATA_DmaBuf);
}
#endif
#ifdef SUNSHINE_BUILD_VULKAN
if (mem_type == platf::mem_type_e::vulkan && n_dmabuf_infos > 0) {
if (mem_type == platf::mem_type_e::vulkan) {
if (buffer_type != SPA_DATA_DmaBuf) {
return nullptr;
}
return vk::make_avcodec_encode_device_vram(width, height, 0, 0);
}
#endif
#ifdef SUNSHINE_BUILD_CUDA
if (mem_type == platf::mem_type_e::cuda) {
if (display_is_nvidia && n_dmabuf_infos > 0) {
if (display_is_nvidia && buffer_type == SPA_DATA_DmaBuf) {
// Display GPU is NVIDIA - can use DMA-BUF directly
return cuda::make_avcodec_gl_encode_device(width, height, 0, 0);
} else {
// Hybrid system (Intel display + NVIDIA encode) - use memory buffer path
// DMA-BUFs from Intel GPU cannot be imported into CUDA
// Use memory buffers for hybrid GPUs and compositor memory fallback.
// DMA-BUFs from Intel GPU cannot be imported into CUDA.
return cuda::make_avcodec_encode_device(width, height, false);
}
}
@@ -1540,6 +1605,7 @@ namespace pipewire {
wl::display_t wl_display;
std::array<struct dmabuf_format_info_t, MAX_DMABUF_FORMATS> dmabuf_infos;
int n_dmabuf_infos;
uint32_t buffer_type = SPA_ID_INVALID; ///< Storage selected at initialization; capture restarts if frames change storage.
bool display_is_nvidia = false; // Track if display GPU is NVIDIA
std::chrono::nanoseconds delay;
std::optional<std::uint64_t> last_pts {};
+88 -1
View File
@@ -1,6 +1,6 @@
/**
* @file tests/unit/platform/linux/test_pipewire.cpp
* @brief Test PipeWire stream lifecycle handling and capture display teardown.
* @brief Test PipeWire buffer negotiation, stream lifecycle handling, and capture display teardown.
*/
#if defined(SUNSHINE_BUILD_KWIN) || defined(SUNSHINE_BUILD_PORTAL)
// test includes
@@ -215,6 +215,91 @@ namespace {
};
} // namespace
TEST(PipeWireBufferTest, NegotiationDistinguishesPendingMemoryAndDmabuf) {
EXPECT_EQ(pipewire::pipewire_t::negotiated_buffer_type(nullptr, DRM_FORMAT_XRGB8888), SPA_ID_INVALID);
pipewire::shared_state_t shared;
EXPECT_EQ(shared.buffer_type.load(), SPA_ID_INVALID);
std::array<uint8_t, 256> storage {};
spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage.data(), storage.size());
auto *memory = static_cast<spa_pod *>(spa_pod_builder_add_object(
&builder,
SPA_TYPE_OBJECT_Format,
SPA_PARAM_Format,
SPA_FORMAT_VIDEO_format,
SPA_POD_Id(SPA_VIDEO_FORMAT_BGRx)
));
// A locally importable format is not evidence that the compositor supplied DMA-BUF.
EXPECT_EQ(pipewire::pipewire_t::negotiated_buffer_type(memory, DRM_FORMAT_XRGB8888), SPA_DATA_MemPtr);
builder = SPA_POD_BUILDER_INIT(storage.data(), storage.size());
auto *dmabuf = static_cast<spa_pod *>(spa_pod_builder_add_object(
&builder,
SPA_TYPE_OBJECT_Format,
SPA_PARAM_Format,
SPA_FORMAT_VIDEO_modifier,
SPA_POD_Long(DRM_FORMAT_MOD_LINEAR)
));
EXPECT_EQ(pipewire::pipewire_t::negotiated_buffer_type(dmabuf, DRM_FORMAT_XRGB8888), SPA_DATA_DmaBuf);
EXPECT_EQ(pipewire::pipewire_t::negotiated_buffer_type(dmabuf, 0), SPA_DATA_MemPtr);
}
TEST(PipeWireBufferTest, StorageChangesAreRejectedBeforeCopyingFrame) {
pipewire::stream_data_t data {};
spa_chunk chunk {};
chunk.size = 16;
chunk.stride = 8;
spa_data plane {};
plane.chunk = &chunk;
plane.fd = -1;
spa_buffer buffer {};
buffer.n_datas = 1;
buffer.datas = &plane;
pw_buffer current {};
current.buffer = &buffer;
data.current_buffer = &current;
data.buffer_a.resize(16, 42);
data.local_stride = 8;
data.format.info.raw.format = SPA_VIDEO_FORMAT_BGRx;
for (auto type : {SPA_DATA_MemPtr, SPA_DATA_MemFd}) {
plane.type = type;
pipewire::img_descriptor_t img {};
img.sd.fds.fill(-1);
EXPECT_EQ(pipewire::pipewire_t::copy_frame(&img, data, SPA_DATA_DmaBuf), platf::capture_e::reinit);
EXPECT_EQ(img.data, nullptr);
EXPECT_EQ(img.sd.fds[0], -1);
EXPECT_EQ(pipewire::pipewire_t::copy_frame(&img, data, SPA_DATA_MemPtr), platf::capture_e::ok);
EXPECT_EQ(img.data, data.buffer_a.data());
EXPECT_EQ(img.row_pitch, 8);
EXPECT_EQ(img.pixel_pitch, 4);
EXPECT_FALSE(img.data_owned);
}
plane.type = SPA_DATA_DmaBuf;
pipewire::img_descriptor_t img {};
img.sd.fds.fill(-1);
EXPECT_EQ(pipewire::pipewire_t::copy_frame(&img, data, SPA_DATA_MemPtr), platf::capture_e::reinit);
EXPECT_EQ(img.data, nullptr);
EXPECT_EQ(img.sd.fds[0], -1);
// A new display may use DMA-BUF again; no process-wide fallback is latched.
plane.fd = open("/dev/null", O_RDONLY | O_CLOEXEC);
ASSERT_GE(plane.fd, 0);
EXPECT_EQ(pipewire::pipewire_t::copy_frame(&img, data, SPA_DATA_DmaBuf), platf::capture_e::ok);
EXPECT_GE(img.sd.fds[0], 0);
EXPECT_NE(img.sd.fds[0], plane.fd);
pipewire::pipewire_t::close_img_fds(&img);
close(plane.fd);
plane.type = SPA_DATA_Invalid;
EXPECT_EQ(pipewire::pipewire_t::copy_frame(&img, data, SPA_DATA_MemPtr), platf::capture_e::capture_error);
chunk.size = 0;
EXPECT_EQ(pipewire::pipewire_t::copy_frame(&img, data, SPA_DATA_MemPtr), platf::capture_e::timeout);
buffer.n_datas = 0;
EXPECT_EQ(pipewire::pipewire_t::copy_frame(&img, data, SPA_DATA_MemPtr), platf::capture_e::timeout);
}
TEST(PipeWireStreamStateTest, RecordsStartupUntilTheStreamIsReplaced) {
pipewire::stream_data_t data {};
data.shared = std::make_shared<pipewire::shared_state_t>();
@@ -302,9 +387,11 @@ TEST_F(PipeWireDisplayTest, NewConnectionResetsStartupHistory) {
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sockets.data()), 0);
auto state = std::make_shared<pipewire::shared_state_t>();
state->has_streamed.store(true);
state->buffer_type.store(SPA_DATA_DmaBuf);
auto display = std::make_shared<test_display_t>();
display->connect(sockets[0], state);
EXPECT_FALSE(state->has_streamed.load());
EXPECT_EQ(state->buffer_type.load(), SPA_ID_INVALID);
display.reset();
close(sockets[1]);
}