Screen sharing was slow mostly because every frame was copied and allocated several times on its way to the encoder: the PipeWire callback copied the mapped buffer into a new Vec, X11 copied the shared-memory segment, Windows copied twice, `to_i420` allocated fresh planes on one thread, and libvpx output was copied into Vec after Vec. Capture backends now hand each frame to a `FrameSink` on their own thread, with the pixels still in the capture buffer (`FrameRef`: borrowed planes with strides). `ScreenCapture::start_sink` is implemented by the test pattern (renders into one reused buffer), X11 (the MIT-SHM segment, cursor blended in place, monitor/window geometry looked up once a second instead of every frame), the portal and Windows Graphics Capture (the mapped staging texture with its row pitch); other backends get a forwarding default. `start` keeps working through a queue adapter. `FramePacer` caps frame rates by timestamps; sinks can change the cap while capturing (X11 and the test pattern follow, the portal renegotiates the frame rate). Portal: LINEAR DMA-BUFs are negotiated in addition to shared memory (mandatory modifier, fixated by us, `dataType` per buffer kind), mapped once per buffer and read between DMA_BUF_IOCTL_SYNC start/end. When reading them is slow (uncached VRAM), the stream renegotiates to shared memory. Tiled modifiers are not offered (needs a GPU import). The streaming path: - `workers::Workers`: a fork-join pool that allocates nothing per job. - `convert::Converter`: any stride, BGRx/BGRA/RGBx/RGBA (yuv's SIMD paths), I420 and NV12 into a caller's I420 frame, in row bands on all cores; handles buffers without padding after the last row. - `scale`: separable area-average downscaling (bilinear up), weights per size pair computed once, a 2x2 box fast path, row bands on the pool; `Pyramid` converts a capture once and derives every requested size from the nearest larger one, sharing equal sizes. - `pool::FramePool`: recycled `Arc<VideoFrame>`s; `handoff::Handoff`: a one-slot latest-wins atomic handoff that counts replaced frames. Codecs: `VideoEncoder::encode_with` hands out encoded frames borrowed from the encoder (libvpx's own buffer; OpenH264 into a reused buffer). libvpx: `cpu-used` adapts to the measured encode time against the frame interval (VP8 levels -10..-4, VP9 starting at 8 within 5..9), threads from all CPUs the frame size can use, VP8 token partitions and VP9 tile columns from the thread count and width, keyframes only on request, frame durations from timestamps (variable frame rate), `set_fps`, and `speed` for stats. A probe on the desktop test pattern showed VP8 speeds beyond -10 are no faster on screen content, only worse (about 3 dB PSNR). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
683 lines
21 KiB
Rust
683 lines
21 KiB
Rust
//! Wayland screen capture: the xdg-desktop-portal ScreenCast interface
|
|
//! (`ashpd`) picks the source and hands over a PipeWire remote; frames come
|
|
//! through a PipeWire video stream.
|
|
//!
|
|
//! The portal can remember the user's choice: pass the token from
|
|
//! [`PortalCapture::restore_token`] to [`PortalCapture::with_restore_token`]
|
|
//! next time (it persists until the user revokes it).
|
|
//!
|
|
//! Buffers: LINEAR DMA-BUFs (mapped and read by the CPU, with
|
|
//! `DMA_BUF_IOCTL_SYNC` around each read) are offered first, shared memory
|
|
//! (`BGRx` / `BGRA` / `RGBx` / `RGBA`) second, so compositors without
|
|
//! DMA-BUF support use shared memory. When reading DMA-BUFs turns out slow
|
|
//! (buffers in memory the CPU reads uncached, e.g. a discrete GPU's VRAM),
|
|
//! the stream switches to shared memory. Tiled DMA-BUFs would need a GPU
|
|
//! import and are not offered. Frames go to the [`FrameSink`] while the
|
|
//! buffer is dequeued; nothing is copied before conversion. The frame rate
|
|
//! follows the sink's cap: the compositor is asked for up to that rate and
|
|
//! sends frames when the screen changes.
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use ashpd::desktop::PersistMode;
|
|
use ashpd::desktop::screencast::{CursorMode, Screencast, SelectSourcesOptions, SourceType};
|
|
use pipewire as pw;
|
|
use pw::spa::buffer::DataType;
|
|
use pw::spa::param::format::{FormatProperties, MediaSubtype, MediaType};
|
|
use pw::spa::param::video::{VideoFormat, VideoInfoRaw};
|
|
use pw::spa::pod::{
|
|
ChoiceValue, Object, Pod, PodPropFlags, Property, PropertyFlags, Value, property,
|
|
};
|
|
use pw::spa::utils::{Choice, ChoiceEnum, ChoiceFlags, Fraction, Id, Rectangle, SpaTypes};
|
|
use tracing::{debug, warn};
|
|
|
|
use crate::capture::dmabuf::DmaBufMap;
|
|
use crate::capture::pw::{PwThread, pod, serialize};
|
|
use crate::capture::{
|
|
BoxFuture, CaptureOptions, CaptureSource, FrameSink, QueueSink, ScreenCapture, SourceId,
|
|
};
|
|
use crate::frame::{FrameRef, PixelsRef, PlaneRef, VideoFrame};
|
|
use crate::queue::FrameReceiver;
|
|
use crate::{Error, Result};
|
|
|
|
const BACKEND: &str = "portal";
|
|
|
|
fn portal_error(e: ashpd::Error) -> Error {
|
|
match e {
|
|
ashpd::Error::Response(ashpd::desktop::ResponseError::Cancelled)
|
|
| ashpd::Error::Portal(ashpd::PortalError::Cancelled(_)) => Error::Cancelled,
|
|
// No bus, no portal service, or no ScreenCast interface.
|
|
ashpd::Error::Zbus(_)
|
|
| ashpd::Error::Portal(ashpd::PortalError::ZBus(_))
|
|
| ashpd::Error::PortalNotFound(_) => Error::CaptureUnavailable {
|
|
backend: BACKEND,
|
|
reason: format!("the ScreenCast portal is not available: {e}"),
|
|
},
|
|
other => Error::Capture { backend: BACKEND, message: other.to_string() },
|
|
}
|
|
}
|
|
|
|
/// A portal session kept open while capturing.
|
|
struct Session {
|
|
proxy: Screencast,
|
|
session: ashpd::desktop::Session<Screencast>,
|
|
}
|
|
|
|
/// ScreenCast portal + PipeWire backend (Wayland, also works on X11 desktops
|
|
/// that run xdg-desktop-portal).
|
|
pub struct PortalCapture {
|
|
restore_token: Option<String>,
|
|
dmabuf: bool,
|
|
session: Option<Session>,
|
|
thread: Option<PwThread>,
|
|
}
|
|
|
|
impl Default for PortalCapture {
|
|
fn default() -> Self {
|
|
// VOELIN_PORTAL_DMABUF=0 offers shared memory only.
|
|
let dmabuf = std::env::var("VOELIN_PORTAL_DMABUF").map_or(true, |v| v != "0");
|
|
Self { restore_token: None, dmabuf, session: None, thread: None }
|
|
}
|
|
}
|
|
|
|
impl PortalCapture {
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// Reuse an earlier choice without asking (a token from
|
|
/// [`PortalCapture::restore_token`]).
|
|
pub fn with_restore_token(token: Option<String>) -> Self {
|
|
let mut capture = Self::default();
|
|
capture.restore_token = token;
|
|
capture
|
|
}
|
|
|
|
/// Offer LINEAR DMA-BUFs (default, unless `VOELIN_PORTAL_DMABUF=0`), or
|
|
/// shared memory only.
|
|
pub fn with_dmabuf(mut self, enabled: bool) -> Self {
|
|
self.dmabuf = enabled;
|
|
self
|
|
}
|
|
|
|
/// Token to persist after a successful start; `None` before that or if
|
|
/// the portal did not grant persistence.
|
|
pub fn restore_token(&self) -> Option<&str> {
|
|
self.restore_token.as_deref()
|
|
}
|
|
|
|
/// Whether the ScreenCast portal answers on the session bus.
|
|
pub async fn is_available() -> bool {
|
|
match Screencast::new().await {
|
|
// Reading a property needs the service to exist.
|
|
Ok(proxy) => proxy.available_source_types().await.is_ok(),
|
|
Err(_) => false,
|
|
}
|
|
}
|
|
|
|
async fn open(&mut self, options: &CaptureOptions) -> Result<(std::os::fd::OwnedFd, u32)> {
|
|
let proxy = Screencast::new().await.map_err(portal_error)?;
|
|
let session = proxy.create_session(Default::default()).await.map_err(portal_error)?;
|
|
let modes = proxy.available_cursor_modes().await.unwrap_or_default();
|
|
let cursor = if !options.cursor {
|
|
CursorMode::Hidden
|
|
} else if modes.contains(CursorMode::Embedded) {
|
|
CursorMode::Embedded
|
|
} else {
|
|
// Metadata cursors would need drawing; fall back to none.
|
|
CursorMode::Hidden
|
|
};
|
|
let select = SelectSourcesOptions::default()
|
|
.set_cursor_mode(cursor)
|
|
.set_sources(SourceType::Monitor | SourceType::Window)
|
|
.set_multiple(false)
|
|
.set_restore_token(self.restore_token.as_deref())
|
|
.set_persist_mode(PersistMode::ExplicitlyRevoked);
|
|
proxy
|
|
.select_sources(&session, select)
|
|
.await
|
|
.map_err(portal_error)?
|
|
.response()
|
|
.map_err(portal_error)?;
|
|
let streams = proxy
|
|
.start(&session, None, Default::default())
|
|
.await
|
|
.map_err(portal_error)?
|
|
.response()
|
|
.map_err(portal_error)?;
|
|
if let Some(token) = streams.restore_token() {
|
|
self.restore_token = Some(token.to_owned());
|
|
}
|
|
let stream = streams.streams().first().ok_or(Error::Cancelled)?;
|
|
let node = stream.pipe_wire_node_id();
|
|
debug!(node, size = ?stream.size(), "portal stream started");
|
|
let fd = proxy
|
|
.open_pipe_wire_remote(&session, Default::default())
|
|
.await
|
|
.map_err(portal_error)?;
|
|
self.session = Some(Session { proxy, session });
|
|
Ok((fd, node))
|
|
}
|
|
}
|
|
|
|
impl ScreenCapture for PortalCapture {
|
|
fn backend(&self) -> &'static str {
|
|
BACKEND
|
|
}
|
|
|
|
fn sources(&mut self) -> Result<Vec<CaptureSource>> {
|
|
Ok(vec![CaptureSource {
|
|
id: SourceId::Portal,
|
|
name: "Choose a screen or window".into(),
|
|
width: 0,
|
|
height: 0,
|
|
primary: true,
|
|
}])
|
|
}
|
|
|
|
/// Asks the user through the portal dialog (unless a restore token
|
|
/// applies). Needs a tokio runtime (the D-Bus connection runs on it).
|
|
fn start(
|
|
&mut self,
|
|
source: &SourceId,
|
|
options: &CaptureOptions,
|
|
) -> BoxFuture<'_, Result<FrameReceiver<VideoFrame>>> {
|
|
let (sink, rx) = QueueSink::new(options);
|
|
let started = self.start_sink(source, options, Box::new(sink));
|
|
Box::pin(async move {
|
|
started.await?;
|
|
Ok(rx)
|
|
})
|
|
}
|
|
|
|
/// The sink reads the PipeWire buffer itself (shared memory, or a
|
|
/// LINEAR DMA-BUF mapped for reading) while the buffer is dequeued.
|
|
fn start_sink(
|
|
&mut self,
|
|
source: &SourceId,
|
|
options: &CaptureOptions,
|
|
sink: Box<dyn FrameSink>,
|
|
) -> BoxFuture<'_, Result<()>> {
|
|
let source = source.clone();
|
|
let options = options.clone();
|
|
Box::pin(async move {
|
|
if source != SourceId::Portal {
|
|
return Err(Error::SourceNotFound(source));
|
|
}
|
|
self.stop();
|
|
let (fd, node) = self.open(&options).await?;
|
|
let dmabuf = self.dmabuf;
|
|
let thread =
|
|
PwThread::spawn("voelin-portal-capture", Some(fd), move |core, mainloop| {
|
|
video_stream(core, mainloop, node, dmabuf, sink)
|
|
})
|
|
.map_err(|e| Error::Capture { backend: BACKEND, message: e })?;
|
|
self.thread = Some(thread);
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
fn stop(&mut self) {
|
|
self.thread = None;
|
|
if let Some(s) = self.session.take() {
|
|
// Closing needs the async runtime; do it in the background if there
|
|
// is one, else the portal closes the session with the connection.
|
|
if let Ok(handle) = tokio::runtime::Handle::try_current() {
|
|
handle.spawn(async move {
|
|
let _ = s.session.close().await;
|
|
drop(s.proxy);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for PortalCapture {
|
|
fn drop(&mut self) {
|
|
self.stop();
|
|
}
|
|
}
|
|
|
|
/// Pixel formats we take, in order of preference (all 4 bytes per pixel;
|
|
/// `x` and `A` are ignored).
|
|
const FORMATS: [VideoFormat; 4] =
|
|
[VideoFormat::BGRx, VideoFormat::BGRA, VideoFormat::RGBx, VideoFormat::RGBA];
|
|
/// DRM_FORMAT_MOD_LINEAR: rows of pixels, readable by the CPU once mapped.
|
|
const MODIFIER_LINEAR: i64 = 0;
|
|
/// Reading DMA-BUFs slower than this (per pixel, conversion included) means
|
|
/// the buffers live in memory the CPU reads uncached (e.g. a discrete GPU's
|
|
/// VRAM); shared memory is faster then.
|
|
const SLOW_DMABUF_NS_PER_PIXEL: f64 = 6.0;
|
|
/// DMA-BUF frames timed before deciding.
|
|
const DMABUF_PROBE_FRAMES: u32 = 30;
|
|
|
|
/// What was negotiated.
|
|
#[derive(Clone, Copy, Debug)]
|
|
struct Negotiated {
|
|
format: VideoFormat,
|
|
width: u32,
|
|
height: u32,
|
|
/// DMA-BUF modifier; `None`: shared memory.
|
|
modifier: Option<i64>,
|
|
}
|
|
|
|
struct VideoState {
|
|
sink: Box<dyn FrameSink>,
|
|
format: Option<Negotiated>,
|
|
started: Instant,
|
|
/// Frame rate the offered formats ask for.
|
|
fps: u32,
|
|
/// Offer DMA-BUFs.
|
|
dmabuf: bool,
|
|
/// Mappings of the DMA-BUFs PipeWire cycles through.
|
|
maps: Vec<DmaBufMap>,
|
|
/// Time spent handing DMA-BUF frames to the sink, and how many.
|
|
dmabuf_time: Duration,
|
|
dmabuf_frames: u32,
|
|
logged: Option<bool>,
|
|
mainloop: pw::main_loop::MainLoopWeak,
|
|
}
|
|
|
|
fn video_stream(
|
|
core: &pw::core::CoreRc,
|
|
mainloop: &pw::main_loop::MainLoopRc,
|
|
node: u32,
|
|
dmabuf: bool,
|
|
sink: Box<dyn FrameSink>,
|
|
) -> std::result::Result<(pw::stream::StreamRc, pw::stream::StreamListener<VideoState>), String> {
|
|
let props = pw::properties::properties! {
|
|
*pw::keys::MEDIA_TYPE => "Video",
|
|
*pw::keys::MEDIA_CATEGORY => "Capture",
|
|
*pw::keys::MEDIA_ROLE => "Screen",
|
|
};
|
|
let stream = pw::stream::StreamRc::new(core.clone(), "voelin-screen-capture", props)
|
|
.map_err(|e| format!("PipeWire stream: {e}"))?;
|
|
let fps = sink.max_fps().max(1);
|
|
let state = VideoState {
|
|
sink,
|
|
format: None,
|
|
started: Instant::now(),
|
|
fps,
|
|
dmabuf,
|
|
maps: Vec::new(),
|
|
dmabuf_time: Duration::ZERO,
|
|
dmabuf_frames: 0,
|
|
logged: None,
|
|
mainloop: mainloop.downgrade(),
|
|
};
|
|
let listener = stream
|
|
.add_local_listener_with_user_data(state)
|
|
.state_changed(|_, state, _, new| {
|
|
if let pw::stream::StreamState::Error(e) = &new {
|
|
warn!("screen capture stream failed: {e}");
|
|
}
|
|
if matches!(
|
|
new,
|
|
pw::stream::StreamState::Error(_) | pw::stream::StreamState::Unconnected
|
|
) && let Some(mainloop) = state.mainloop.upgrade()
|
|
{
|
|
mainloop.quit();
|
|
}
|
|
})
|
|
.param_changed(|stream, state, id, param| {
|
|
let Some(param) = param else { return };
|
|
if id != pw::spa::param::ParamType::Format.as_raw() {
|
|
return;
|
|
}
|
|
if let Err(e) = format_changed(stream, state, param) {
|
|
warn!("screen capture format: {e}");
|
|
}
|
|
})
|
|
.remove_buffer(|_, state, _| {
|
|
// PipeWire is replacing its buffers: map the new ones.
|
|
state.maps.clear();
|
|
})
|
|
.process(process)
|
|
.register()
|
|
.map_err(|e| format!("PipeWire listener: {e}"))?;
|
|
|
|
let formats = enum_formats(fps, dmabuf)?;
|
|
let mut pods = formats.iter().map(|f| pod(f)).collect::<std::result::Result<Vec<_>, _>>()?;
|
|
stream
|
|
.connect(
|
|
pw::spa::utils::Direction::Input,
|
|
Some(node),
|
|
pw::stream::StreamFlags::AUTOCONNECT | pw::stream::StreamFlags::MAP_BUFFERS,
|
|
&mut pods,
|
|
)
|
|
.map_err(|e| format!("cannot connect to the screen cast node {node}: {e}"))?;
|
|
Ok((stream, listener))
|
|
}
|
|
|
|
fn size_range() -> Property {
|
|
property!(
|
|
FormatProperties::VideoSize,
|
|
Choice,
|
|
Range,
|
|
Rectangle,
|
|
Rectangle { width: 1920, height: 1080 },
|
|
Rectangle { width: 1, height: 1 },
|
|
Rectangle { width: 16384, height: 16384 }
|
|
)
|
|
}
|
|
|
|
/// Up to `fps`, as the compositor can (it sends frames on damage only).
|
|
fn framerate_range(fps: u32) -> Property {
|
|
property!(
|
|
FormatProperties::VideoFramerate,
|
|
Choice,
|
|
Range,
|
|
Fraction,
|
|
Fraction { num: fps, denom: 1 },
|
|
Fraction { num: 0, denom: 1 },
|
|
Fraction { num: fps, denom: 1 }
|
|
)
|
|
}
|
|
|
|
fn modifier_property(fixed: bool) -> Property {
|
|
let flags = pw::spa::sys::SPA_POD_PROP_FLAG_MANDATORY
|
|
| if fixed { 0 } else { pw::spa::sys::SPA_POD_PROP_FLAG_DONT_FIXATE };
|
|
let value = if fixed {
|
|
Value::Long(MODIFIER_LINEAR)
|
|
} else {
|
|
Value::Choice(ChoiceValue::Long(Choice(
|
|
ChoiceFlags::empty(),
|
|
ChoiceEnum::Enum { default: MODIFIER_LINEAR, alternatives: vec![MODIFIER_LINEAR] },
|
|
)))
|
|
};
|
|
Property {
|
|
key: FormatProperties::VideoModifier.as_raw(),
|
|
flags: PropertyFlags::from_bits_retain(flags),
|
|
value,
|
|
}
|
|
}
|
|
|
|
fn format_object(properties: Vec<Property>) -> Value {
|
|
Value::Object(Object {
|
|
type_: SpaTypes::ObjectParamFormat.as_raw(),
|
|
id: pw::spa::param::ParamType::EnumFormat.as_raw(),
|
|
properties,
|
|
})
|
|
}
|
|
|
|
/// The formats we offer: each pixel format as a LINEAR DMA-BUF (if
|
|
/// `dmabuf`), then all of them in shared memory.
|
|
fn enum_formats(fps: u32, dmabuf: bool) -> std::result::Result<Vec<Vec<u8>>, String> {
|
|
let mut formats = Vec::new();
|
|
let base = || {
|
|
vec![
|
|
property!(FormatProperties::MediaType, Id, MediaType::Video),
|
|
property!(FormatProperties::MediaSubtype, Id, MediaSubtype::Raw),
|
|
]
|
|
};
|
|
if dmabuf {
|
|
for format in FORMATS {
|
|
let mut properties = base();
|
|
properties.push(property!(FormatProperties::VideoFormat, Id, format));
|
|
properties.push(modifier_property(false));
|
|
properties.push(size_range());
|
|
properties.push(framerate_range(fps));
|
|
formats.push(serialize(format_object(properties))?);
|
|
}
|
|
}
|
|
let mut properties = base();
|
|
properties.push(property!(
|
|
FormatProperties::VideoFormat,
|
|
Choice,
|
|
Enum,
|
|
Id,
|
|
FORMATS[0],
|
|
FORMATS[0],
|
|
FORMATS[1],
|
|
FORMATS[2],
|
|
FORMATS[3]
|
|
));
|
|
properties.push(size_range());
|
|
properties.push(framerate_range(fps));
|
|
formats.push(serialize(format_object(properties))?);
|
|
Ok(formats)
|
|
}
|
|
|
|
/// Offer formats again (another frame rate, or no DMA-BUFs); PipeWire
|
|
/// renegotiates.
|
|
fn renegotiate(stream: &pw::stream::Stream, fps: u32, dmabuf: bool) {
|
|
let result = enum_formats(fps, dmabuf).and_then(|formats| {
|
|
let mut pods =
|
|
formats.iter().map(|f| pod(f)).collect::<std::result::Result<Vec<_>, _>>()?;
|
|
stream.update_params(&mut pods).map_err(|e| e.to_string())
|
|
});
|
|
if let Err(e) = result {
|
|
warn!("cannot renegotiate the screen capture format: {e}");
|
|
}
|
|
}
|
|
|
|
fn format_changed(
|
|
stream: &pw::stream::Stream,
|
|
state: &mut VideoState,
|
|
param: &Pod,
|
|
) -> std::result::Result<(), String> {
|
|
let Ok((MediaType::Video, MediaSubtype::Raw)) =
|
|
pw::spa::param::format_utils::parse_format(param)
|
|
else {
|
|
return Ok(());
|
|
};
|
|
let mut info = VideoInfoRaw::new();
|
|
info.parse(param).map_err(|e| format!("cannot parse the video format: {e}"))?;
|
|
let size = info.size();
|
|
state.maps.clear();
|
|
let modifier = param
|
|
.as_object()
|
|
.ok()
|
|
.and_then(|o| o.find_prop(Id(FormatProperties::VideoModifier.as_raw())));
|
|
let modifier = match modifier {
|
|
Some(prop) if prop.flags().contains(PodPropFlags::DONT_FIXATE) => {
|
|
// The producer left the choice to us; we only take LINEAR.
|
|
let mut properties = vec![
|
|
property!(FormatProperties::MediaType, Id, MediaType::Video),
|
|
property!(FormatProperties::MediaSubtype, Id, MediaSubtype::Raw),
|
|
property!(FormatProperties::VideoFormat, Id, info.format()),
|
|
modifier_property(true),
|
|
property!(
|
|
FormatProperties::VideoSize,
|
|
Rectangle,
|
|
Rectangle { width: size.width, height: size.height }
|
|
),
|
|
];
|
|
properties.push(framerate_range(state.fps));
|
|
let fixed = serialize(format_object(properties))?;
|
|
stream.update_params(&mut [pod(&fixed)?]).map_err(|e| e.to_string())?;
|
|
return Ok(());
|
|
}
|
|
Some(prop) => Some(prop.value().get_long().unwrap_or(MODIFIER_LINEAR)),
|
|
None => None,
|
|
};
|
|
debug!(format = ?info.format(), size.width, size.height, ?modifier, "screen capture format");
|
|
state.format = Some(Negotiated {
|
|
format: info.format(),
|
|
width: size.width,
|
|
height: size.height,
|
|
modifier,
|
|
});
|
|
let types = if modifier.is_some() {
|
|
1 << pw::spa::sys::SPA_DATA_DmaBuf
|
|
} else {
|
|
(1 << pw::spa::sys::SPA_DATA_MemPtr) | (1 << pw::spa::sys::SPA_DATA_MemFd)
|
|
};
|
|
let buffers = serialize(Value::Object(Object {
|
|
type_: SpaTypes::ObjectParamBuffers.as_raw(),
|
|
id: pw::spa::param::ParamType::Buffers.as_raw(),
|
|
properties: vec![Property {
|
|
key: pw::spa::sys::SPA_PARAM_BUFFERS_dataType,
|
|
flags: PropertyFlags::empty(),
|
|
value: Value::Int(types as i32),
|
|
}],
|
|
}))?;
|
|
stream.update_params(&mut [pod(&buffers)?]).map_err(|e| e.to_string())
|
|
}
|
|
|
|
fn process(stream: &pw::stream::Stream, state: &mut VideoState) {
|
|
// Follow the sink's frame rate: ask the compositor for another rate.
|
|
let fps = state.sink.max_fps().max(1);
|
|
if fps != state.fps {
|
|
state.fps = fps;
|
|
renegotiate(stream, fps, state.dmabuf);
|
|
}
|
|
let Some(mut buffer) = stream.dequeue_buffer() else { return };
|
|
let Some(format) = state.format else { return };
|
|
if format.modifier.is_some_and(|m| m != MODIFIER_LINEAR) {
|
|
// Tiled: the CPU cannot read it (we never offer that).
|
|
return;
|
|
}
|
|
let timestamp = state.started.elapsed();
|
|
if !state.sink.wants(timestamp) {
|
|
// Dropping the buffer queues it back.
|
|
return;
|
|
}
|
|
let Some(data) = buffer.datas_mut().first_mut() else { return };
|
|
let bgra = match format.format {
|
|
VideoFormat::BGRx | VideoFormat::BGRA => true,
|
|
VideoFormat::RGBx | VideoFormat::RGBA => false,
|
|
_ => return,
|
|
};
|
|
let chunk = data.chunk();
|
|
if chunk.size() == 0 || chunk.flags().contains(pw::spa::buffer::ChunkFlags::CORRUPTED) {
|
|
return;
|
|
}
|
|
let offset = chunk.offset() as usize;
|
|
let row = format.width as usize * 4;
|
|
let stride = usize::try_from(chunk.stride()).ok().filter(|&s| s >= row).unwrap_or(row);
|
|
let needed = offset + stride * (format.height as usize).saturating_sub(1) + row;
|
|
fn view(
|
|
bytes: &[u8],
|
|
at: (usize, usize, bool),
|
|
format: Negotiated,
|
|
t: Duration,
|
|
) -> Option<FrameRef<'_>> {
|
|
let (offset, stride, bgra) = at;
|
|
let plane = PlaneRef::new(bytes.get(offset..)?, stride);
|
|
let pixels = if bgra { PixelsRef::Bgra(plane) } else { PixelsRef::Rgba(plane) };
|
|
Some(FrameRef { width: format.width, height: format.height, timestamp: t, pixels })
|
|
}
|
|
let at = (offset, stride, bgra);
|
|
let is_dmabuf = data.type_() == DataType::DmaBuf;
|
|
if state.logged != Some(is_dmabuf) {
|
|
state.logged = Some(is_dmabuf);
|
|
debug!(dmabuf = is_dmabuf, "screen capture buffers");
|
|
}
|
|
let started = Instant::now();
|
|
let more = if is_dmabuf {
|
|
let raw = data.as_raw();
|
|
let len = (raw.mapoffset + raw.maxsize) as usize;
|
|
let fd = data.fd();
|
|
if !state.maps.iter().any(|m| m.fd() == fd && m.len() >= len) {
|
|
state.maps.retain(|m| m.fd() != fd);
|
|
match DmaBufMap::new(fd, len) {
|
|
Ok(map) => state.maps.push(map),
|
|
Err(e) => {
|
|
warn!("cannot map a screen capture DMA-BUF: {e}");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
let map = state.maps.iter().find(|m| m.fd() == fd).expect("mapped above");
|
|
let base = raw.mapoffset as usize;
|
|
map.read(|bytes| {
|
|
match bytes
|
|
.get(base..)
|
|
.filter(|b| b.len() >= needed)
|
|
.and_then(|b| view(b, at, format, timestamp))
|
|
{
|
|
Some(frame) => state.sink.frame(frame),
|
|
None => true,
|
|
}
|
|
})
|
|
} else {
|
|
match data.data().filter(|b| b.len() >= needed).and_then(|b| view(b, at, format, timestamp))
|
|
{
|
|
Some(frame) => state.sink.frame(frame),
|
|
None => true,
|
|
}
|
|
};
|
|
if is_dmabuf && state.dmabuf {
|
|
probe_dmabuf(stream, state, started.elapsed(), format);
|
|
}
|
|
if !more && let Some(mainloop) = state.mainloop.upgrade() {
|
|
// Nobody takes frames any more.
|
|
mainloop.quit();
|
|
}
|
|
}
|
|
|
|
/// Time the first DMA-BUF frames; if reading them is slow, offer shared
|
|
/// memory only.
|
|
fn probe_dmabuf(
|
|
stream: &pw::stream::Stream,
|
|
state: &mut VideoState,
|
|
took: Duration,
|
|
format: Negotiated,
|
|
) {
|
|
if state.dmabuf_frames >= DMABUF_PROBE_FRAMES {
|
|
return;
|
|
}
|
|
state.dmabuf_frames += 1;
|
|
state.dmabuf_time += took;
|
|
if state.dmabuf_frames < DMABUF_PROBE_FRAMES {
|
|
return;
|
|
}
|
|
let pixels = f64::from(format.width) * f64::from(format.height);
|
|
let per_pixel = state.dmabuf_time.as_secs_f64() * 1e9 / f64::from(DMABUF_PROBE_FRAMES) / pixels;
|
|
if per_pixel > SLOW_DMABUF_NS_PER_PIXEL {
|
|
tracing::info!(
|
|
"reading DMA-BUFs takes {per_pixel:.1} ns per pixel; switching screen capture to shared memory"
|
|
);
|
|
state.dmabuf = false;
|
|
renegotiate(stream, state.fps, false);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// Without a session bus the portal must fail cleanly, not hang or
|
|
/// panic. Skipped where a bus exists (it could open a real dialog).
|
|
#[tokio::test]
|
|
async fn unavailable_without_session_bus() {
|
|
let runtime_bus = std::env::var_os("XDG_RUNTIME_DIR")
|
|
.is_some_and(|d| std::path::Path::new(&d).join("bus").exists());
|
|
if std::env::var_os("DBUS_SESSION_BUS_ADDRESS").is_some() || runtime_bus {
|
|
eprintln!("skipped: a D-Bus session bus is configured");
|
|
return;
|
|
}
|
|
let mut capture = PortalCapture::new();
|
|
assert_eq!(capture.sources().unwrap()[0].id, SourceId::Portal);
|
|
let err = capture.start(&SourceId::Portal, &CaptureOptions::default()).await.err().unwrap();
|
|
assert!(matches!(err, Error::CaptureUnavailable { backend: "portal", .. }), "{err}");
|
|
assert!(!PortalCapture::is_available().await);
|
|
assert!(capture.restore_token().is_none());
|
|
}
|
|
|
|
/// The offered formats parse back: DMA-BUF (mandatory LINEAR modifier,
|
|
/// left to us to fixate) per pixel format, then shared memory.
|
|
#[test]
|
|
fn offered_formats() {
|
|
let formats = enum_formats(60, true).unwrap();
|
|
assert_eq!(formats.len(), FORMATS.len() + 1);
|
|
for (i, bytes) in formats.iter().enumerate() {
|
|
let object = pod(bytes).unwrap().as_object().unwrap();
|
|
let modifier = object.find_prop(Id(FormatProperties::VideoModifier.as_raw()));
|
|
if i < FORMATS.len() {
|
|
let flags = modifier.expect("a modifier").flags();
|
|
assert!(flags.contains(PodPropFlags::MANDATORY | PodPropFlags::DONT_FIXATE));
|
|
} else {
|
|
assert!(modifier.is_none(), "shared memory has no modifier");
|
|
}
|
|
}
|
|
assert_eq!(enum_formats(30, false).unwrap().len(), 1);
|
|
let fixed = serialize(format_object(vec![modifier_property(true)])).unwrap();
|
|
let object = pod(&fixed).unwrap().as_object().unwrap();
|
|
let prop = object.find_prop(Id(FormatProperties::VideoModifier.as_raw())).unwrap();
|
|
assert_eq!(prop.value().get_long().unwrap(), MODIFIER_LINEAR);
|
|
assert!(!prop.flags().contains(PodPropFlags::DONT_FIXATE));
|
|
}
|
|
}
|