diff --git a/crates/voelin-media/src/capture/dmabuf.rs b/crates/voelin-media/src/capture/dmabuf.rs index a82aa74..d8d07cb 100644 --- a/crates/voelin-media/src/capture/dmabuf.rs +++ b/crates/voelin-media/src/capture/dmabuf.rs @@ -21,6 +21,18 @@ const DMA_BUF_SYNC_END: u64 = 1 << 2; /// `DMA_BUF_IOCTL_SYNC`: `_IOW('b', 0, struct dma_buf_sync)`. const SYNC: rustix::ioctl::Opcode = rustix::ioctl::opcode::write::(b'b', 0); +/// The size of DMA-BUF `fd` in bytes (`lseek(fd, 0, SEEK_END)`, which a +/// DMA-BUF answers with its buffer object's size without moving anything). +pub(crate) fn size_of(fd: RawFd) -> Option { + if fd < 0 { + return None; + } + // SAFETY: borrowed for the call from a caller that holds it open. + let fd = unsafe { BorrowedFd::borrow_raw(fd) }; + let end = rustix::fs::seek(fd, rustix::fs::SeekFrom::End(0)).ok()?; + usize::try_from(end).ok().filter(|&size| size > 0) +} + /// A read-only mapping of a DMA-BUF. pub(crate) struct DmaBufMap { fd: RawFd, @@ -97,5 +109,7 @@ mod tests { assert_eq!(map.fd(), file.as_raw_fd()); assert_eq!(map.read(|b| b.iter().map(|&x| u64::from(x)).sum::()), 7 * 8192); assert!(DmaBufMap::new(-1, 10).is_err()); + assert_eq!(size_of(file.as_raw_fd()), Some(8192)); + assert_eq!(size_of(-1), None); } } diff --git a/crates/voelin-media/src/capture/portal.rs b/crates/voelin-media/src/capture/portal.rs index b53e5f3..71e9772 100644 --- a/crates/voelin-media/src/capture/portal.rs +++ b/crates/voelin-media/src/capture/portal.rs @@ -11,9 +11,13 @@ //! (`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 +//! the stream switches to shared memory. A sink that +//! [accepts DMA-BUFs](FrameSink::accepts_dmabuf) (a VA-API encoder, which +//! converts and encodes them on the GPU) gets them before anything is +//! mapped, and then the CPU never reads them. Tiled DMA-BUFs are not +//! offered: the modifiers the encoder's driver imports are not known here. +//! 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. @@ -31,7 +35,7 @@ use pw::spa::pod::{ use pw::spa::utils::{Choice, ChoiceEnum, ChoiceFlags, Fraction, Id, Rectangle, SpaTypes}; use tracing::{debug, warn}; -use crate::capture::dmabuf::DmaBufMap; +use crate::capture::dmabuf::{self, DmaBufMap}; use crate::capture::pw::{PwThread, pod, serialize}; use crate::capture::{ BoxFuture, CaptureOptions, CaptureSource, DRM_MOD_LINEAR, DmaBufRef, FrameSink, QueueSink, @@ -600,7 +604,12 @@ fn process(stream: &pw::stream::Stream, state: &mut VideoState) { fourcc, modifier: format.modifier.map_or(DRM_MOD_LINEAR, |m| m as u64), fd: data.fd(), - size: (raw.mapoffset + raw.maxsize) as usize, + // As for the mapping below: compositors leave `maxsize` 0 for + // a DMA-BUF, whose size is its buffer object's. + size: match (raw.mapoffset + raw.maxsize) as usize { + 0 => dmabuf::size_of(data.fd()).unwrap_or(raw.mapoffset as usize + needed), + size => size, + }, planes: [(raw.mapoffset as usize + offset, stride), (0, 0), (0, 0), (0, 0)], plane_count: 1, }; diff --git a/crates/voelin-media/src/ffmpeg/encoder.rs b/crates/voelin-media/src/ffmpeg/encoder.rs index 4a849e1..2e3a4b9 100644 --- a/crates/voelin-media/src/ffmpeg/encoder.rs +++ b/crates/voelin-media/src/ffmpeg/encoder.rs @@ -1098,7 +1098,7 @@ impl FfmpegEncoder { /// this frame's packet came out (the encoder reads them one frame deep). /// RGB buffers (`XR24`, `AR24`, `XB24`, `AB24`: what screen capture /// delivers) are converted into the session's NV12 surfaces on the GPU - /// (VA-API video processing, [`vpp`]), cropped to the coded size, and + /// (VA-API video processing), cropped to the coded size, and /// are no longer read when this returns. /// /// Anything else fails with `Error::CodecUnavailable`; callers then map