Skip to main content

Ratio

Struct Ratio 

Source
pub struct Ratio {
    pub numerator: u32,
    pub denominator: u32,
}
Expand description

A rational number (numerator / denominator).

Used to represent frame rates (e.g. 30000/1000 = 30 fps) and frame durations (e.g. 1000/30000 ≈ 0.033 s).

Fields§

§numerator: u32§denominator: u32

Implementations§

Source§

impl Ratio

Source

pub fn as_f64(&self) -> f64

Examples found in repository?
examples/capture.rs (line 55)
9fn main() {
10    #[cfg(target_os = "macos")]
11    {
12        use camera_stream::platform::macos::device::MacosCameraManager;
13
14        let manager = MacosCameraManager::default();
15
16        // Discover devices
17        let devices: Vec<_> = manager
18            .discover_devices()
19            .expect("failed to discover devices")
20            .collect();
21        println!("Found {} camera(s):", devices.len());
22        for (i, dev) in devices.iter().enumerate() {
23            println!("  [{}] {} (id: {})", i, dev.name(), dev.id());
24        }
25
26        if devices.is_empty() {
27            println!("No cameras found.");
28            return;
29        }
30
31        // Use default device
32        let device = manager
33            .default_device()
34            .expect("failed to get default device")
35            .expect("no default camera");
36
37        println!("\nUsing: {} ({})", device.name(), device.id());
38
39        // Print supported formats
40        let formats: Vec<_> = device
41            .supported_formats()
42            .expect("failed to get formats")
43            .collect();
44        println!("\nSupported formats ({} total):", formats.len());
45        for (i, f) in formats.iter().take(10).enumerate() {
46            println!(
47                "  [{}] {:?} {}x{} ({} frame rate range(s))",
48                i,
49                f.pixel_format,
50                f.size.width,
51                f.size.height,
52                f.frame_rate_ranges().len(),
53            );
54            for rr in f.frame_rate_ranges() {
55                println!("       {:.1}-{:.1} fps", rr.min.as_f64(), rr.max.as_f64(),);
56            }
57        }
58        if formats.len() > 10 {
59            println!("  ... and {} more", formats.len() - 10);
60        }
61
62        // Pick first format or a reasonable default
63        let config = if let Some(f) = formats.first() {
64            let rate =
65                f.frame_rate_ranges()
66                    .first()
67                    .map(|r| r.max)
68                    .unwrap_or(camera_stream::Ratio {
69                        numerator: 30000,
70                        denominator: 1000,
71                    });
72            camera_stream::StreamConfig {
73                pixel_format: f.pixel_format,
74                size: f.size,
75                frame_rate: rate,
76            }
77        } else {
78            println!("No supported formats found.");
79            return;
80        };
81
82        println!(
83            "\nOpening with {:?} {}x{} @ {:.1} fps",
84            config.pixel_format,
85            config.size.width,
86            config.size.height,
87            config.frame_rate.as_f64(),
88        );
89
90        let mut stream = device.open(&config).expect("failed to open stream");
91
92        let frame_count = Arc::new(AtomicU64::new(0));
93        let count_clone = frame_count.clone();
94        let target_frames: u64 = 60;
95
96        stream
97            .start(move |frame| {
98                let n = count_clone.fetch_add(1, Ordering::Relaxed) + 1;
99                let planes = frame.planes();
100                let total_bytes: usize = planes.iter().map(|p| p.data.len()).sum();
101                println!(
102                    "Frame {}: {:?} {}x{} ts={:.3}s planes={} bytes={}",
103                    n,
104                    frame.pixel_format(),
105                    frame.size().width,
106                    frame.size().height,
107                    frame.timestamp().as_secs_f64(),
108                    planes.len(),
109                    total_bytes,
110                );
111            })
112            .expect("failed to start stream");
113
114        // Wait until we've captured enough frames
115        loop {
116            std::thread::sleep(Duration::from_millis(100));
117            if frame_count.load(Ordering::Relaxed) >= target_frames {
118                break;
119            }
120        }
121
122        stream.stop().expect("failed to stop stream");
123        println!(
124            "\nDone. Captured {} frames.",
125            frame_count.load(Ordering::Relaxed)
126        );
127    }
128
129    #[cfg(not(target_os = "macos"))]
130    {
131        println!("This example only works on macOS.");
132    }
133}

Trait Implementations§

Source§

impl Clone for Ratio

Source§

fn clone(&self) -> Ratio

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ratio

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Ratio

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Ratio

Source§

fn eq(&self, other: &Ratio) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Ratio

Source§

impl Eq for Ratio

Source§

impl StructuralPartialEq for Ratio

Auto Trait Implementations§

§

impl Freeze for Ratio

§

impl RefUnwindSafe for Ratio

§

impl Send for Ratio

§

impl Sync for Ratio

§

impl Unpin for Ratio

§

impl UnwindSafe for Ratio

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> AutoreleaseSafe for T
where T: ?Sized,