Skip to main content

ferroui_media_linux/
lib.rs

1//! System GStreamer sessions and immutable DMA-BUF ownership. No CPU preview route.
2#![cfg(target_os = "linux")]
3#![deny(unsafe_code)]
4#[allow(unsafe_code)]
5mod native;
6use ferroui_base::{
7    platform::IOptionalFeatureProvider,
8    reactive::Subscription,
9    threading::{DispatcherPriority, DispatcherTimer},
10};
11use ferroui_media::{frames::*, *};
12#[cfg(feature = "controls")]
13pub use ferroui_media_opengl::NativeGlTextureImporter;
14use ferroui_media_opengl::{NativeGlFrameProducer, ReadyGlFrame};
15use ferroui_opengl::IGlContextExternalObjectsFeature;
16pub use native::{EncodedRecording, NativeFrame, NativeRecording, NativeSession, NativeState};
17use std::{cell::RefCell, rc::Rc, task::Poll, time::Duration};
18
19fn unsupported(message: impl Into<String>) -> MediaError {
20    MediaError::new(MediaErrorKind::Unsupported, message)
21}
22pub fn poll_native<T: 'static>(
23    mut poll: impl FnMut() -> Poll<Result<T, MediaError>> + 'static,
24) -> MediaFuture<T> {
25    let mut timer: Option<Subscription> = None;
26    Box::pin(std::future::poll_fn(move |cx| {
27        timer.take();
28        match poll() {
29            Poll::Ready(v) => Poll::Ready(v),
30            Poll::Pending => {
31                let w = cx.waker().clone();
32                timer = Some(DispatcherTimer::run_once(
33                    move || w.wake_by_ref(),
34                    Duration::from_millis(16),
35                    DispatcherPriority::NORMAL,
36                ));
37                Poll::Pending
38            }
39        }
40    }))
41}
42/// Enumerates the active EGL display's actual DRM format/modifier pairs. These
43/// exact caps are used at source negotiation; implicit/unknown layouts are absent.
44pub fn admitted_caps(
45    producer: &NativeGlFrameProducer,
46    size: Option<(u32, u32, u32, u32)>,
47) -> MediaFuture<String> {
48    producer.invoke(move |context| {
49        context
50            .native_egl_display()
51            .ok_or_else(|| unsupported("Linux media needs an admitted EGL context"))?;
52        let features: &dyn IOptionalFeatureProvider = &**context;
53        let feature = features
54            .try_get::<dyn IGlContextExternalObjectsFeature>()
55            .ok_or_else(|| unsupported("EGL DMA-BUF import is unavailable"))?;
56        let mut formats = Vec::new();
57        for pair in feature.supported_dma_buf_formats().unwrap_or_default() {
58            if pair.modifier == 0x00ff_ffff_ffff_ffff {
59                continue;
60            }
61            let name = match pair.format {
62                0x3231564e => "NV12",
63                0x34325258 => "XR24",
64                0x34324258 => "XB24",
65                _ => continue,
66            };
67            if name == "NV12"
68                && context.version().type_() != ferroui_opengl::GlProfileType::OpenGLES
69            {
70                continue;
71            }
72            let value = if pair.modifier == 0 {
73                name.to_owned()
74            } else {
75                format!("{name}:0x{:016x}", pair.modifier)
76            };
77            if !formats.contains(&value) {
78                formats.push(value);
79            }
80        }
81        if formats.is_empty() {
82            return Err(unsupported(
83                "No explicitly negotiated native DMA-BUF formats",
84            ));
85        }
86        let mut caps = format!(
87            "video/x-raw(memory:DMABuf),format=DMA_DRM,drm-format=(string){{{}}}",
88            formats.join(",")
89        );
90        if let Some((w, h, n, d)) = size {
91            if w == 0 || h == 0 || w > 8192 || h > 8192 || n == 0 || d == 0 {
92                return Err(unsupported("Invalid camera format"));
93            }
94            caps.push_str(&format!(
95                ",width=(int){w},height=(int){h},framerate=(fraction){n}/{d}"
96            ));
97        }
98        Ok(caps)
99    })
100}
101/// At most one queued import/conversion. A native latest slot plus appsink's one
102/// queued sample are separate bounded producer slots; driver pools are opaque.
103pub struct LinuxFrameStream {
104    pub native: NativeSession,
105    producer: NativeGlFrameProducer,
106    source: u64,
107    generation: u64,
108    epoch: Rc<std::cell::Cell<u64>>,
109    pending: RefCell<Option<MediaFuture<Option<ReadyGlFrame>>>>,
110    ready: RefCell<Option<ReadyGlFrame>>,
111    retry: RefCell<Option<NativeFrame>>,
112}
113impl LinuxFrameStream {
114    pub fn new(
115        native: NativeSession,
116        producer: NativeGlFrameProducer,
117        source: u64,
118        generation: u64,
119    ) -> Self {
120        Self {
121            native,
122            producer,
123            source,
124            generation,
125            epoch: Rc::new(std::cell::Cell::new(1)),
126            pending: RefCell::new(None),
127            ready: RefCell::new(None),
128            retry: RefCell::new(None),
129        }
130    }
131    pub fn retire(&self, epoch: u64) {
132        self.epoch.set(epoch);
133        self.pending.borrow_mut().take();
134        self.ready.borrow_mut().take();
135        self.retry.borrow_mut().take();
136    }
137    pub fn has_pending_frame_work(&self) -> bool {
138        self.pending.borrow().is_some()
139            || self.ready.borrow().is_some()
140            || self.retry.borrow().is_some()
141    }
142    pub fn take_frame(&self) -> Result<Option<FrameLease>, MediaError> {
143        // Report pipeline/capture failure even while an import is pending or
144        // leases saturate the budget. A new native take may never become ready.
145        self.native.state()?;
146        if self.producer.budget().quarantined_reads() != 0 {
147            return Err(unsupported(
148                "GPU completion is indeterminate; retained storage is quarantined",
149            ));
150        }
151        if self.ready.borrow().is_some() {
152            let frame = self.ready.borrow().as_ref().unwrap().take_if_ready()?;
153            if frame.is_some() {
154                self.ready.borrow_mut().take();
155            }
156            return Ok(frame);
157        }
158        // Polling is driven by the presentation tick; no blocking wait on UI.
159        let mut pending = self.pending.borrow_mut();
160        if let Some(job) = pending.as_mut() {
161            let waker = std::task::Waker::noop();
162            let mut cx = std::task::Context::from_waker(waker);
163            match job.as_mut().poll(&mut cx) {
164                Poll::Pending => return Ok(None),
165                Poll::Ready(result) => {
166                    pending.take();
167                    match result {
168                        Ok(Some(frame)) => {
169                            self.retry.borrow_mut().take();
170                            self.ready.replace(Some(frame));
171                        }
172                        Ok(None) => {} // Keep one native sample until budget admission.
173                        Err(error) => {
174                            self.retry.borrow_mut().take();
175                            return Err(error);
176                        }
177                    }
178                    return Ok(None);
179                }
180            }
181        }
182        if let Some(frame) = self.native.take_frame()? {
183            if frame.info().epoch == self.epoch.get() {
184                self.retry.replace(Some(frame));
185            }
186        }
187        let Some(frame) = self.retry.borrow().clone() else {
188            return Ok(None);
189        };
190        let info = frame.info();
191        if info.epoch != self.epoch.get() {
192            return Ok(None);
193        }
194        let converted = info.format == 0x3231564e;
195        let output_bytes = u64::from(info.width) * u64::from(info.height) * 4;
196        let metadata = FrameMetadata {
197            identity: FrameIdentity {
198                source: self.source,
199                generation: self.generation,
200                sequence: info.sequence,
201            },
202            width: info.width,
203            height: info.height,
204            visible: FrameRegion {
205                x: 0,
206                y: 0,
207                width: info.width,
208                height: info.height,
209            },
210            format: FrameFormat::Rgba8,
211            color: if info.color == 1 {
212                FrameColor::Srgb
213            } else {
214                FrameColor::Rec709Full
215            },
216            alpha: FrameAlpha::Opaque,
217            rotation: FrameRotation::None,
218            mirrored: false,
219            top_left_origin: !converted,
220            timestamp: Duration::from_nanos(info.timestamp_ns),
221            device: Some(self.producer.device()),
222            device_generation: self.producer.device_generation(),
223            planes: vec![FramePlane {
224                offset: 0,
225                stride: u64::from(info.width) * 4,
226                length: output_bytes,
227            }],
228            retained_bytes: if converted {
229                info.bytes
230                    .checked_add(output_bytes)
231                    .ok_or_else(|| unsupported("Retained bytes overflow"))?
232            } else {
233                info.bytes.max(output_bytes)
234            },
235        };
236        let job = if converted {
237            let input = frame.clone();
238            let counters = self.producer.budget().counters();
239            self.producer
240                .produce_owned(metadata, frame, move |context, output| {
241                    counters.gpu_conversion(GpuTransferReason::PixelFormatConversion);
242                    input.convert(context, output)
243                })
244        } else {
245            native::import(&self.producer, metadata, frame)
246        };
247        *pending = Some(job);
248        Ok(None)
249    }
250}