orbbec_sdk/
lib.rs

1pub mod device;
2pub mod error;
3pub mod filter;
4pub mod frame;
5pub mod pipeline;
6pub mod stream;
7pub(crate) mod sys;
8
9use std::sync::atomic::AtomicBool;
10
11use sys::context::OBContext;
12
13#[doc(inline)]
14pub use crate::sys::enums::OBDeviceType as DeviceType;
15
16#[doc(inline)]
17pub use crate::sys::enums::OBFormat as Format;
18
19#[doc(inline)]
20pub use crate::sys::enums::OBSensorType as SensorType;
21
22#[doc(inline)]
23pub use crate::sys::enums::OBStreamType as StreamType;
24
25#[doc(inline)]
26pub use crate::sys::enums::OBPermissionType as PermissionType;
27
28#[doc(inline)]
29pub use crate::sys::enums::OBHoleFillMode as HoleFillMode;
30
31#[doc(inline)]
32pub use crate::sys::enums::OBConvertFormat as ConvertType;
33
34/// There can only be a single context at a time
35/// C API does not enforce this, but having multiple contexts
36/// will lead to crashes and undefined behavior
37static CONTEXT_CREATED: AtomicBool = AtomicBool::new(false);
38
39/// Context Manager
40pub struct Context {
41    inner: OBContext,
42}
43
44impl Context {
45    /// Create a new context
46    pub fn new() -> Result<Self, error::OrbbecError> {
47        if CONTEXT_CREATED
48            .compare_exchange(
49                false,
50                true,
51                std::sync::atomic::Ordering::SeqCst,
52                std::sync::atomic::Ordering::SeqCst,
53            )
54            .is_err()
55        {
56            let err_data = error::OrbbecErrorData {
57                message: "A context has already been created".to_string(),
58                function: "Context::new".to_string(),
59                args: "".to_string(),
60            };
61
62            return Err(error::OrbbecError::WrongAPICallSequence(err_data));
63        }
64
65        let ctx = OBContext::new().map_err(error::OrbbecError::from)?;
66
67        Ok(Context { inner: ctx })
68    }
69
70    /// Query the list of connected devices
71    pub fn query_device_list<'a>(&'a self) -> Result<device::DeviceList<'a>, error::OrbbecError> {
72        let list = self
73            .inner
74            .query_device_list()
75            .map_err(error::OrbbecError::from)?;
76
77        Ok(device::DeviceList::new(list, self))
78    }
79}
80
81impl Drop for Context {
82    fn drop(&mut self) {
83        CONTEXT_CREATED.store(false, std::sync::atomic::Ordering::SeqCst);
84    }
85}