Skip to main content

ferroui_media/
lib.rs

1//! Shared media frames, events, errors and renderer-host contracts.
2//! Video playback, camera capture and QR recognition are separate crates.
3#![forbid(unsafe_code)]
4mod event;
5mod renderer;
6pub use event::{MediaEvent, MediaSubscription};
7pub use ferroui_base::rendering::composition::frames;
8pub use renderer::*;
9use std::{future::Future, pin::Pin};
10pub type MediaFuture<T> = Pin<Box<dyn Future<Output = Result<T, MediaError>>>>;
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct MediaError {
13    pub kind: MediaErrorKind,
14    pub message: String,
15}
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum MediaErrorKind {
18    Unsupported,
19    InvalidSource,
20    Permission,
21    NativeFailure,
22    DeviceLost,
23    Cancelled,
24    AutoplayDenied,
25}
26impl MediaError {
27    pub fn new(kind: MediaErrorKind, message: impl Into<String>) -> Self {
28        Self {
29            kind,
30            message: message.into(),
31        }
32    }
33    pub fn cancelled() -> Self {
34        Self::new(
35            MediaErrorKind::Cancelled,
36            "Source or operation was replaced",
37        )
38    }
39}
40impl std::fmt::Display for MediaError {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        f.write_str(&self.message)
43    }
44}
45impl std::error::Error for MediaError {}
46
47/// Allocates one process-unique framework source identity. Generation and frame
48/// sequence remain local to that source. Native clients may use this for hosts.
49pub fn allocate_source_id() -> u64 {
50    use std::sync::atomic::{AtomicU64, Ordering};
51    static NEXT: AtomicU64 = AtomicU64::new(1);
52    NEXT.fetch_update(Ordering::AcqRel, Ordering::Acquire, |v| v.checked_add(1))
53        .expect("media source identity exhausted")
54}