Skip to main content

FormatDescriptor

Struct FormatDescriptor 

Source
pub struct FormatDescriptor {
    pub pixel_format: PixelFormat,
    pub size: Size,
    /* private fields */
}
Expand description

Describes a supported camera format.

Fields§

§pixel_format: PixelFormat§size: Size

Implementations§

Source§

impl FormatDescriptor

Source

pub fn frame_rate_ranges(&self) -> &[FrameRateRange]

The frame rate ranges supported by this format.

Examples found in repository?
examples/capture.rs (line 52)
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 FormatDescriptor

Source§

fn clone(&self) -> FormatDescriptor

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 FormatDescriptor

Source§

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

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

impl PartialEq for FormatDescriptor

Source§

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

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,