Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

chore(recording): trim narrating comments, run x11_shm_capture in Linux CI

shubhook977135505928
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 43d79ddaa..dac701ce9 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -281,6 +281,7 @@ jobs:
cargo test --locked -p cap-utils -p cap-cli-install --lib
cargo test --locked -p cap --bin cap record::tests
cargo test --locked -p cap-recording --lib -- --test-threads=1
+ cargo test --locked -p cap-recording --test x11_shm_capture -- --test-threads=1
cargo test --locked -p cap-enc-ffmpeg
build-gpui:
diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs
index f262d0db6..58faeb796 100644
--- a/crates/recording/src/sources/screen_capture/linux.rs
+++ b/crates/recording/src/sources/screen_capture/linux.rs
@@ -71,8 +71,6 @@ enum LinuxInputConfig {
Wayland(WaylandInputConfig),
}
-// Public only so `examples/x11-capture-benchmark.rs` and
-// `tests/x11_shm_capture.rs` can drive the grabber directly; not a supported API.
#[doc(hidden)]
pub struct X11InputConfig {
pub display_name: String,
@@ -2036,9 +2034,8 @@ fn capture_x11(
/// FFmpeg (spacedrive native-deps) is built without. Capturing with `x11rb`
/// keeps us off any system FFmpeg/libavdevice and adds no new runtime
/// shared-library dependency (`x11rb` speaks the X11 protocol over a socket).
-///
-/// Public (but hidden) only for the X11 capture benchmark and integration
-/// tests; not a supported API.
+// `pub` only so the x11-capture-benchmark example and x11_shm_capture tests can
+// drive it; not a supported API.
#[doc(hidden)]
pub struct X11Grabber {
conn: Arc<RustConnection>,
@@ -2073,15 +2070,6 @@ impl X11Grabber {
Self::with_shm(config, true)
}
- /// Like [`Self::new`], but lets the caller choose whether MIT-SHM may be
- /// used. `allow_shm: false` forces the `GetImage` backend, the same as
- /// setting `CAP_X11_DISABLE_SHM=1`. `allow_shm: true` still falls back to
- /// `GetImage` when SHM is unavailable, so check [`Self::uses_shm`] to see
- /// which backend was picked.
- ///
- /// If an SHM grab later fails, that frame is retried with `GetImage`. The
- /// grabber logs one warning and stays on `GetImage` only if the retry
- /// succeeds.
pub fn with_shm(config: &X11InputConfig, allow_shm: bool) -> anyhow::Result<Self> {
ffmpeg::init().context("initialize FFmpeg")?;
@@ -2265,7 +2253,6 @@ impl X11Grabber {
})
}
- /// Whether frames are currently captured through MIT-SHM.
pub fn uses_shm(&self) -> bool {
matches!(self.backend, GrabBackend::Shm(_))
}
diff --git a/crates/recording/src/sources/screen_capture/linux_shm.rs b/crates/recording/src/sources/screen_capture/linux_shm.rs
index fbc084658..2b9220b8f 100644
--- a/crates/recording/src/sources/screen_capture/linux_shm.rs
+++ b/crates/recording/src/sources/screen_capture/linux_shm.rs
@@ -1,15 +1,6 @@
-//! MIT-SHM image transfer for X11 capture. All `unsafe` for the SysV segment
-//! lifecycle lives here behind a safe API.
-//!
-//! [`ShmSegment`] creates a private SysV segment, maps it into this process,
-//! asks the X server to attach it, then immediately marks it for removal
-//! (`IPC_RMID`). The kernel frees it once both sides detach, so a crash
-//! cannot leak it. Each `ShmGetImage` has the server write a frame into the
-//! segment, and the caller reads it after the reply arrives.
-//!
-//! `X11Grabber` decides when to use this module: it falls back to plain
-//! `GetImage` when the extension is missing, the server can't attach the
-//! segment (remote/TCP displays), or [`DISABLE_SHM_ENV`] is set.
+//! All `unsafe` for the X11 MIT-SHM segment lives here. The segment is marked
+//! `IPC_RMID` right after the server attaches it, so the kernel frees it once
+//! both sides detach, even if this process crashes.
use anyhow::{Context as _, anyhow, bail};
use std::{ptr::NonNull, sync::Arc};
@@ -19,7 +10,6 @@ use x11rb::protocol::xproto::{Drawable, ImageFormat};
use x11rb::rust_connection::RustConnection;
use x11rb::wrapper::ConnectionExt as _;
-/// Set to `1`/`true`/`yes`/`on` to force X11 capture onto `GetImage`.
pub(crate) const DISABLE_SHM_ENV: &str = "CAP_X11_DISABLE_SHM";
pub(crate) fn shm_disabled_by_env() -> bool {
@@ -33,8 +23,6 @@ fn is_truthy(value: &str) -> bool {
)
}
-/// Bytes per row of a Z-pixmap image, padded to `scanline_pad` bits as the
-/// server lays it out. `None` for invalid formats or overflow.
pub(crate) fn image_stride(width: u16, bits_per_pixel: u8, scanline_pad: u8) -> Option<usize> {
if bits_per_pixel == 0 || scanline_pad == 0 || scanline_pad % 8 != 0 {
return None;
@@ -44,7 +32,6 @@ pub(crate) fn image_stride(width: u16, bits_per_pixel: u8, scanline_pad: u8) ->
row_bits.div_ceil(8).checked_next_multiple_of(pad_bytes)
}
-/// Total bytes of a `width` x `height` Z-pixmap image; see [`image_stride`].
pub(crate) fn image_size(
width: u16,
height: u16,
@@ -54,7 +41,6 @@ pub(crate) fn image_size(
image_stride(width, bits_per_pixel, scanline_pad)?.checked_mul(usize::from(height))
}
-/// Errors unless the server offers MIT-SHM 1.1 or newer.
pub(crate) fn query_shm_support(conn: &RustConnection) -> anyhow::Result<()> {
if conn
.extension_information(shm::X11_EXTENSION_NAME)
@@ -160,9 +146,8 @@ impl Drop for SysvMapping {
}
}
-/// A shared-memory segment attached to both this process and the X server.
-/// Dropping it detaches from the server, waits for the server to let go, then
-/// unmaps it locally.
+// `Drop` detaches from the server and waits for it to confirm before the
+// `mapping` field drops and unmaps locally.
pub(crate) struct ShmSegment {
conn: Arc<RustConnection>,
seg: shm::Seg,
@@ -170,8 +155,6 @@ pub(crate) struct ShmSegment {
}
impl ShmSegment {
- /// Create a `size`-byte segment and attach it to the server on `conn`.
- /// Fails if the server can't attach it, e.g. over a remote connection.
pub(crate) fn new(conn: &Arc<RustConnection>, size: usize) -> anyhow::Result<Self> {
let mapping = SysvMapping::new(size)?;
let shmid =
@@ -194,8 +177,6 @@ impl ShmSegment {
self.mapping.size
}
- /// Have the server write a Z-pixmap of the given area into the segment,
- /// and return the bytes it wrote.
pub(crate) fn get_image(
&mut self,
drawable: Drawable,