Skip to main content

FrameRate

Struct FrameRate 

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

Frame rate as a rational number.

Fields§

§numerator: u32§denominator: u32

Implementations§

Source§

impl FrameRate

Source

pub fn as_f64(&self) -> f64

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

Trait Implementations§

Source§

impl Clone for FrameRate

Source§

fn clone(&self) -> FrameRate

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 FrameRate

Source§

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

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

impl Hash for FrameRate

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 FrameRate

Source§

fn eq(&self, other: &FrameRate) -> 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 FrameRate

Source§

impl Eq for FrameRate

Source§

impl StructuralPartialEq for FrameRate

Auto Trait Implementations§

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,