Skip to main content

ferroui_media_camera_linux/
lib.rs

1//! Explicit portal-authorized PipeWire capture or an opt-in direct V4L2 profile.
2//! Discovery does not open a device, request consent, or bypass portal denial.
3#![cfg(target_os = "linux")]
4#![deny(unsafe_code)]
5#[allow(unsafe_code)]
6mod native;
7use ferroui_media::{frames::*, MediaError, MediaErrorKind};
8use ferroui_media_camera::*;
9#[cfg(feature = "controls")]
10pub use ferroui_media_linux::NativeGlTextureImporter;
11use ferroui_media_linux::{
12    admitted_caps, poll_native, LinuxFrameStream, NativeRecording, NativeSession,
13};
14use ferroui_media_opengl::NativeGlFrameProducer;
15use native::PortalAccess;
16use std::{
17    cell::{Cell, RefCell},
18    os::fd::AsRawFd,
19    path::PathBuf,
20    rc::Rc,
21    sync::{
22        atomic::{AtomicBool, Ordering},
23        Arc,
24    },
25    task::Poll,
26    time::Duration,
27};
28#[derive(Clone, Debug, Default)]
29pub enum LinuxCameraProfile {
30    #[default]
31    Portal,
32    /// Explicit administrator/application-selected nodes. This never becomes a
33    /// fallback for a denied or unavailable portal. No implicit /dev scanning.
34    DirectV4l2 { devices: Vec<PathBuf> },
35}
36pub struct LinuxCameraProvider {
37    pub frames: NativeGlFrameProducer,
38    pub profile: LinuxCameraProfile,
39    permission: Rc<Cell<CameraPermission>>,
40}
41impl LinuxCameraProvider {
42    pub fn new(frames: NativeGlFrameProducer, profile: LinuxCameraProfile) -> Self {
43        Self {
44            frames,
45            profile,
46            permission: Rc::new(Cell::new(CameraPermission::NotDetermined)),
47        }
48    }
49}
50fn error(kind: CameraErrorKind, s: impl Into<String>) -> CameraError {
51    CameraError::new(kind, s)
52}
53fn native(e: MediaError) -> CameraError {
54    error(
55        match e.kind {
56            MediaErrorKind::Permission => CameraErrorKind::Denied,
57            MediaErrorKind::Unsupported => CameraErrorKind::Unsupported,
58            MediaErrorKind::Cancelled => CameraErrorKind::Cancelled,
59            MediaErrorKind::DeviceLost => CameraErrorKind::DeviceLost,
60            _ => CameraErrorKind::NativeFailure,
61        },
62        e.message,
63    )
64}
65fn device(id: String) -> CameraDevice {
66    CameraDevice {
67        id,
68        label: None,
69        facing: CameraFacing::Unknown,
70        formats: Vec::new(),
71    }
72}
73impl CameraProvider for LinuxCameraProvider {
74    fn devices(&self) -> CameraFuture<Vec<CameraDevice>> {
75        let profile = self.profile.clone();
76        let (send, receive) = std::sync::mpsc::sync_channel(1);
77        std::thread::spawn(move || {
78            let devices = match profile {
79                LinuxCameraProfile::Portal => {
80                    if PortalAccess::present() {
81                        vec![device("portal:default".into())]
82                    } else {
83                        Vec::new()
84                    }
85                }
86                LinuxCameraProfile::DirectV4l2 { devices } => devices
87                    .into_iter()
88                    .filter(|path| path.exists())
89                    .filter_map(|path| path.to_str().map(|id| device(id.to_owned())))
90                    .collect(),
91            };
92            let _ = send.send(devices);
93        });
94        Box::pin(async move {
95            poll_native(move || match receive.try_recv() {
96                Ok(v) => Poll::Ready(Ok(v)),
97                Err(std::sync::mpsc::TryRecvError::Empty) => Poll::Pending,
98                Err(_) => Poll::Ready(Err(MediaError::new(
99                    MediaErrorKind::NativeFailure,
100                    "Camera discovery worker failed",
101                ))),
102            })
103            .await
104            .map_err(native)
105        })
106    }
107    fn permission(&self) -> CameraPermission {
108        self.permission.get()
109    }
110    fn start(
111        &self,
112        options: CameraStartOptions,
113        source: u64,
114        generation: u64,
115        budget: FrameBudget,
116    ) -> CameraFuture<Rc<dyn NativeCameraSession>> {
117        let profile = self.profile.clone();
118        let frames = self.frames.with_budget(budget.clone());
119        let permission = self.permission.clone();
120        Box::pin(async move {
121            if options
122                .selection
123                .preferred_facing
124                .is_some_and(|f| f != CameraFacing::Unknown)
125                && options.selection.exact
126            {
127                return Err(error(
128                    CameraErrorKind::Unsupported,
129                    "Native facing metadata is unavailable in this profile",
130                ));
131            }
132            let size = options
133                .selection
134                .format
135                .map(|f| {
136                    // Preserve fractional rates to a bounded millisecond rational.
137                    let n = (f.frames_per_second * 1000.).round();
138                    if !n.is_finite() || n < 1. || n > u32::MAX as f64 {
139                        return Err(error(CameraErrorKind::InvalidRequest, "Invalid frame rate"));
140                    }
141                    Ok((f.width, f.height, n as u32, 1000))
142                })
143                .transpose()?;
144            let caps = admitted_caps(&frames, size).await.map_err(native)?;
145            let (path, profile_id, remote, node, id) = match profile {
146                LinuxCameraProfile::Portal => {
147                    if options
148                        .selection
149                        .device_id
150                        .as_deref()
151                        .is_some_and(|id| id != "portal:default")
152                    {
153                        return Err(error(
154                            CameraErrorKind::NoDevice,
155                            "Only the portal-authorized default selection is admitted",
156                        ));
157                    }
158                    let access = PortalAccess::begin().map_err(native)?;
159                    let result = poll_native(move || access.poll()).await;
160                    let (fd, node) = match result {
161                        Ok(v) => {
162                            permission.set(CameraPermission::Granted);
163                            v
164                        }
165                        Err(e) => {
166                            if e.kind == MediaErrorKind::Permission {
167                                permission.set(CameraPermission::Denied);
168                            }
169                            return Err(native(e));
170                        }
171                    };
172                    (String::new(), 1, Some(fd), node, format!("portal:{node}"))
173                }
174                LinuxCameraProfile::DirectV4l2 { devices } => {
175                    let selected = options
176                        .selection
177                        .device_id
178                        .as_ref()
179                        .map(PathBuf::from)
180                        .or_else(|| devices.first().cloned())
181                        .ok_or_else(|| {
182                            error(
183                                CameraErrorKind::NoDevice,
184                                "No explicitly configured V4L2 device",
185                            )
186                        })?;
187                    if !devices.contains(&selected) {
188                        return Err(error(
189                            CameraErrorKind::NoDevice,
190                            "Device is outside the explicit direct profile",
191                        ));
192                    }
193                    let text = selected
194                        .to_str()
195                        .ok_or_else(|| {
196                            error(
197                                CameraErrorKind::InvalidRequest,
198                                "Native device path must be UTF-8",
199                            )
200                        })?
201                        .to_owned();
202                    (text.clone(), 2, None, 0, text)
203                }
204            };
205            let session = NativeSession::open(
206                &path,
207                &caps,
208                profile_id,
209                remote.as_ref().map_or(-1, |fd| fd.as_raw_fd()),
210                node,
211            )
212            .map_err(native)?;
213            let mut prepared = Some(session);
214            let session = poll_native(move || match prepared.as_ref().unwrap().state() {
215                Err(e) => Poll::Ready(Err(e)),
216                Ok(s) if s.state == 2 && s.width > 0 && s.height > 0 => {
217                    Poll::Ready(Ok(prepared.take().unwrap()))
218                }
219                _ => Poll::Pending,
220            })
221            .await
222            .map_err(native)?;
223            let state = session.state().map_err(native)?;
224            if state.fps_d == 0 || state.fps_n == 0 {
225                return Err(error(
226                    CameraErrorKind::Unsupported,
227                    "Camera did not report actual negotiated frame rate",
228                ));
229            }
230            let settings = CameraSettings {
231                device: device(id),
232                format: CameraFormat {
233                    width: state.width,
234                    height: state.height,
235                    frames_per_second: state.fps_n as f64 / state.fps_d as f64,
236                },
237                native_still: false,
238                torch: false,
239                zoom: None,
240            };
241            permission.set(CameraPermission::Granted);
242            Ok(Rc::new(Camera {
243                stream: RefCell::new(Some(LinuxFrameStream::new(
244                    session, frames, source, generation,
245                ))),
246                settings,
247                budget,
248            }) as Rc<dyn NativeCameraSession>)
249        })
250    }
251}
252struct Camera {
253    stream: RefCell<Option<LinuxFrameStream>>,
254    settings: CameraSettings,
255    budget: FrameBudget,
256}
257static PHOTO_ACTIVE: AtomicBool = AtomicBool::new(false);
258struct PhotoSlot;
259impl Drop for PhotoSlot {
260    fn drop(&mut self) {
261        PHOTO_ACTIVE.store(false, Ordering::Release);
262    }
263}
264impl NativeCameraSession for Camera {
265    fn supports_video_recording(&self) -> bool {
266        self.stream
267            .borrow()
268            .as_ref()
269            .is_some_and(|s| s.native.supports_recording())
270    }
271
272    fn start_recording(&self) -> CameraFuture<Rc<dyn NativeCameraRecording>> {
273        let recording = self
274            .stream
275            .borrow()
276            .as_ref()
277            .ok_or_else(|| error(CameraErrorKind::Cancelled, "Camera stopped"))
278            .and_then(|s| s.native.record().map_err(native));
279        Box::pin(async move {
280            let recording = Rc::new(recording?);
281            let poll = recording.clone();
282            poll_native(move || poll.started()).await.map_err(native)?;
283            Ok(Rc::new(LinuxCameraRecording(recording)) as Rc<dyn NativeCameraRecording>)
284        })
285    }
286
287    fn settings(&self) -> CameraSettings {
288        let mut settings = self.settings.clone();
289        if let Some(s) = self
290            .stream
291            .borrow()
292            .as_ref()
293            .and_then(|s| s.native.state().ok())
294            .filter(|s| s.width > 0 && s.height > 0 && s.fps_d > 0 && s.fps_n > 0)
295        {
296            settings.format = CameraFormat {
297                width: s.width,
298                height: s.height,
299                frames_per_second: s.fps_n as f64 / s.fps_d as f64,
300            };
301        }
302        settings
303    }
304    fn take_frame(&self) -> Result<Option<FrameLease>, CameraError> {
305        self.stream
306            .borrow()
307            .as_ref()
308            .ok_or_else(|| error(CameraErrorKind::Cancelled, "Camera stopped"))?
309            .take_frame()
310            .map_err(native)
311    }
312    fn photo(&self) -> CameraFuture<CameraPhoto> {
313        let frame = self
314            .stream
315            .borrow()
316            .as_ref()
317            .ok_or_else(|| error(CameraErrorKind::Cancelled, "Camera stopped"))
318            .and_then(|s| {
319                s.native.take_frame().map_err(native)?.ok_or_else(|| {
320                    error(
321                        CameraErrorKind::Busy,
322                        "No new native preview sample for snapshot",
323                    )
324                })
325            });
326        let counters = self.budget.counters();
327        let prepared = frame.and_then(|frame| {
328            PHOTO_ACTIVE
329                .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
330                .map_err(|_| error(CameraErrorKind::Busy, "One native photo may be pending"))?;
331            Ok((frame, PhotoSlot))
332        });
333        let (send, receive) = std::sync::mpsc::sync_channel(1);
334        match prepared {
335            Err(e) => return Box::pin(async move { Err(e) }),
336            Ok((frame, slot)) => {
337                std::thread::spawn(move || {
338                    let _slot = slot;
339                    let info = frame.info();
340                    let result = (|| {
341                        let rgb = frame.photo_rgb(&counters).map_err(native)?;
342                        let mut encoded = Vec::new();
343                        jpeg_encoder::Encoder::new(&mut encoded, 90)
344                            .encode(
345                                &rgb,
346                                info.width as u16,
347                                info.height as u16,
348                                jpeg_encoder::ColorType::Rgb,
349                            )
350                            .map_err(|e| error(CameraErrorKind::NativeFailure, e.to_string()))?;
351                        if encoded.len() > 64 * 1024 * 1024 {
352                            return Err(error(
353                                CameraErrorKind::Unsupported,
354                                "Encoded photo exceeds 64 MiB",
355                            ));
356                        }
357                        counters
358                            .cpu_transfer(
359                                PixelTransferPurpose::Photo,
360                                encoded.len() as u64,
361                                PresentationRoute::SharedTexture,
362                            )
363                            .map_err(|e| error(CameraErrorKind::NativeFailure, e.to_string()))?;
364                        Ok(CameraPhoto {
365                            encoded: Arc::from(encoded),
366                            content_type: "image/jpeg".into(),
367                            width: info.width,
368                            height: info.height,
369                            timestamp: Duration::from_nanos(info.timestamp_ns),
370                            rotation_degrees: 0,
371                            mirrored: false,
372                            origin: PhotoOrigin::PreviewSnapshot,
373                        })
374                    })();
375                    let _ = send.send(result);
376                });
377            }
378        }
379        Box::pin(async move {
380            poll_native(move || match receive.try_recv() {
381                Ok(v) => Poll::Ready(Ok(v)),
382                Err(std::sync::mpsc::TryRecvError::Empty) => Poll::Pending,
383                Err(_) => Poll::Ready(Err(MediaError::new(
384                    MediaErrorKind::NativeFailure,
385                    "Native photo worker failed",
386                ))),
387            })
388            .await
389            .map_err(native)?
390        })
391    }
392    fn set_control(&self, _: CameraControl) -> Result<(), CameraError> {
393        Err(error(
394            CameraErrorKind::Unsupported,
395            "Camera controls are unavailable in this capture profile",
396        ))
397    }
398    fn stop(&self) {
399        self.stream.borrow_mut().take();
400    }
401}
402impl Drop for Camera {
403    fn drop(&mut self) {
404        self.stream.get_mut().take();
405    }
406}
407
408struct LinuxCameraRecording(Rc<NativeRecording>);
409impl NativeCameraRecording for LinuxCameraRecording {
410    fn finish(&self) -> CameraFuture<CameraVideo> {
411        self.0.finish();
412        let recording = self.0.clone();
413        Box::pin(async move {
414            let output = poll_native(move || recording.encoded())
415                .await
416                .map_err(native)?;
417            Ok(CameraVideo {
418                encoded: output.bytes,
419                content_type: "video/mp4".into(),
420                width: output.width,
421                height: output.height,
422                duration: output.duration,
423            })
424        })
425    }
426
427    fn cancel(&self) {
428        self.0.cancel();
429    }
430}