Skip to main content

camera/
blocking.rs

1//! Synchronous facade backed by one shared Tokio runtime.
2//!
3//! It mirrors the owning async types without constructing a runtime per call.
4use crate::{
5    BackendPolicy, CameraResult, CapturePlan, CaptureRequest, DeviceInfo, DeviceSelector, Frame,
6    FrameReceiver, SessionState, SubscriptionOptions,
7};
8use std::{future::Future, sync::OnceLock, time::Duration};
9
10fn runtime() -> &'static tokio::runtime::Runtime {
11    static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
12    RUNTIME.get_or_init(|| {
13        tokio::runtime::Builder::new_multi_thread()
14            .enable_all()
15            .build()
16            .expect("camera blocking runtime")
17    })
18}
19
20fn run<F: Future>(future: F) -> F::Output {
21    runtime().block_on(future)
22}
23
24#[derive(Clone, Debug)]
25/// Values for `CameraSystem`.
26pub struct CameraSystem(crate::CameraSystem);
27
28impl CameraSystem {
29    /// Performs the `new` operation.
30    pub fn new() -> Self {
31        Self(crate::CameraSystem::new())
32    }
33
34    /// Performs the `synthetic` operation.
35    pub fn synthetic() -> Self {
36        Self(crate::CameraSystem::synthetic())
37    }
38
39    /// Performs the `with_policy` operation.
40    pub fn with_policy(policy: BackendPolicy) -> CameraResult<Self> {
41        Ok(Self(
42            crate::CameraSystem::builder()
43                .backend_policy(policy)
44                .build()?,
45        ))
46    }
47
48    /// Performs the `from_async` operation.
49    pub fn from_async(system: crate::CameraSystem) -> Self {
50        Self(system)
51    }
52
53    /// Performs the `devices` operation.
54    pub fn devices(&self) -> CameraResult<Vec<DeviceInfo>> {
55        self.0.devices_blocking()
56    }
57
58    /// Performs the `open` operation.
59    pub fn open(&self, selector: DeviceSelector) -> CameraResult<Device> {
60        run(self.0.open(selector)).map(Device)
61    }
62
63    /// Performs the `plan` operation.
64    pub fn plan(
65        &self,
66        selector: DeviceSelector,
67        request: CaptureRequest,
68    ) -> CameraResult<CapturePlan> {
69        run(self.0.plan(selector, request))
70    }
71}
72
73impl Default for CameraSystem {
74    fn default() -> Self {
75        Self::new()
76    }
77}
78
79#[derive(Debug)]
80/// Values for `Device`.
81pub struct Device(crate::Device);
82
83impl Device {
84    /// Performs the `info` operation.
85    pub fn info(&self) -> &DeviceInfo {
86        self.0.device()
87    }
88
89    /// Performs the `start` operation.
90    pub fn start(&mut self, request: CaptureRequest) -> CameraResult<Session> {
91        run(self.0.start(request)).map(Session)
92    }
93}
94
95#[derive(Debug)]
96/// Values for `Session`.
97pub struct Session(crate::Session);
98
99impl Session {
100    /// Performs the `state` operation.
101    pub fn state(&self) -> SessionState {
102        self.0.state()
103    }
104
105    /// Performs the `subscribe` operation.
106    pub fn subscribe(&self, options: SubscriptionOptions) -> CameraResult<FrameReceiverSync> {
107        self.0.subscribe(options).map(FrameReceiverSync)
108    }
109
110    /// Performs the `close` operation.
111    pub fn close(&self) -> CameraResult<()> {
112        run(self.0.close())
113    }
114}
115
116#[derive(Debug)]
117/// Values for `FrameReceiverSync`.
118pub struct FrameReceiverSync(FrameReceiver);
119
120impl FrameReceiverSync {
121    /// Performs the `next_frame` operation.
122    pub fn next_frame(&mut self) -> CameraResult<Frame> {
123        run(self.0.next())
124    }
125
126    /// Performs the `next_timeout` operation.
127    pub fn next_timeout(&mut self, timeout: Duration) -> CameraResult<Frame> {
128        run(self.0.next_timeout(timeout))
129    }
130
131    /// Performs the `dropped_frames` operation.
132    pub fn dropped_frames(&self) -> u64 {
133        self.0.dropped_frames()
134    }
135}