1use 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)]
25pub struct CameraSystem(crate::CameraSystem);
27
28impl CameraSystem {
29 pub fn new() -> Self {
31 Self(crate::CameraSystem::new())
32 }
33
34 pub fn synthetic() -> Self {
36 Self(crate::CameraSystem::synthetic())
37 }
38
39 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 pub fn from_async(system: crate::CameraSystem) -> Self {
50 Self(system)
51 }
52
53 pub fn devices(&self) -> CameraResult<Vec<DeviceInfo>> {
55 self.0.devices_blocking()
56 }
57
58 pub fn open(&self, selector: DeviceSelector) -> CameraResult<Device> {
60 run(self.0.open(selector)).map(Device)
61 }
62
63 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)]
80pub struct Device(crate::Device);
82
83impl Device {
84 pub fn info(&self) -> &DeviceInfo {
86 self.0.device()
87 }
88
89 pub fn start(&mut self, request: CaptureRequest) -> CameraResult<Session> {
91 run(self.0.start(request)).map(Session)
92 }
93}
94
95#[derive(Debug)]
96pub struct Session(crate::Session);
98
99impl Session {
100 pub fn state(&self) -> SessionState {
102 self.0.state()
103 }
104
105 pub fn subscribe(&self, options: SubscriptionOptions) -> CameraResult<FrameReceiverSync> {
107 self.0.subscribe(options).map(FrameReceiverSync)
108 }
109
110 pub fn close(&self) -> CameraResult<()> {
112 run(self.0.close())
113 }
114}
115
116#[derive(Debug)]
117pub struct FrameReceiverSync(FrameReceiver);
119
120impl FrameReceiverSync {
121 pub fn next_frame(&mut self) -> CameraResult<Frame> {
123 run(self.0.next())
124 }
125
126 pub fn next_timeout(&mut self, timeout: Duration) -> CameraResult<Frame> {
128 run(self.0.next_timeout(timeout))
129 }
130
131 pub fn dropped_frames(&self) -> u64 {
133 self.0.dropped_frames()
134 }
135}