orbbec_sdk/
pipeline.rs

1//! Pipeline module
2use std::time::Duration;
3
4use crate::{
5    SensorType,
6    device::Device,
7    frame::FrameSet,
8    stream::{StreamProfile, StreamProfileList},
9    sys::pipeline::{OBConfig, OBPipeline},
10};
11
12/// Pipeline Configuration
13pub struct Config {
14    inner: OBConfig,
15}
16
17impl Config {
18    /// Create a new configuration with default parameters
19    pub fn new() -> Result<Self, crate::error::OrbbecError> {
20        let config = OBConfig::new().map_err(crate::error::OrbbecError::from)?;
21
22        Ok(Config { inner: config })
23    }
24
25    /// Enable a stream with the given profile
26    /// ### Arguments
27    /// * `profile` - Stream profile to enable
28    pub fn enable_stream_with_profile<S: StreamProfile>(
29        &mut self,
30        profile: &S,
31    ) -> Result<(), crate::error::OrbbecError> {
32        self.inner
33            .enable_stream_with_profile(profile.as_ref())
34            .map_err(crate::error::OrbbecError::from)
35    }
36}
37
38/// Pipeline
39pub struct Pipeline {
40    inner: OBPipeline,
41}
42
43impl Pipeline {
44    /// Create a new pipeline for the given device
45    /// ### Arguments
46    /// * `device` - Device to create the pipeline for
47    pub fn new(device: &Device) -> Result<Self, crate::error::OrbbecError> {
48        let pipeline = OBPipeline::new(device.inner()).map_err(crate::error::OrbbecError::from)?;
49
50        Ok(Pipeline { inner: pipeline })
51    }
52
53    /// Get the device associated with the pipeline
54    pub fn get_device(&mut self) -> Result<Device, crate::error::OrbbecError> {
55        let device = self
56            .inner
57            .get_device()
58            .map_err(crate::error::OrbbecError::from)?;
59
60        Ok(Device::new(device))
61    }
62
63    /// Get a list of stream profiles supported by the specified sensor
64    /// ### Arguments
65    /// * `sensor` - Sensor type to get the stream profiles for
66    pub fn get_stream_profiles(
67        &mut self,
68        sensor: SensorType,
69    ) -> Result<StreamProfileList, crate::error::OrbbecError> {
70        let profile_list = self
71            .inner
72            .get_stream_profile_list(sensor)
73            .map(StreamProfileList::new)
74            .map_err(crate::error::OrbbecError::from)?;
75
76        Ok(profile_list)
77    }
78
79    /// Start the pipeline with the given configuration
80    /// ### Arguments
81    /// * `config` - Configuration to use for the pipeline
82    pub fn start(&mut self, config: &Config) -> Result<(), crate::error::OrbbecError> {
83        self.inner
84            .start_with_config(&config.inner)
85            .map_err(crate::error::OrbbecError::from)
86    }
87
88    /// Set if frames should be synchronized
89    /// ### Arguments
90    /// * `enable` - `true` to enable frame synchronization, `false` to disable it
91    pub fn set_frame_sync(&mut self, enable: bool) -> Result<(), crate::error::OrbbecError> {
92        let res = if enable {
93            self.inner.enable_frame_sync()
94        } else {
95            self.inner.disable_frame_sync()
96        };
97
98        res.map_err(crate::error::OrbbecError::from)
99    }
100
101    /// Wait for the next set of frames from the pipeline
102    /// ### Arguments
103    /// * `timeout` - Maximum time to wait for frames
104    pub fn wait_for_frames(
105        &mut self,
106        timeout: Duration,
107    ) -> Result<Option<FrameSet>, crate::error::OrbbecError> {
108        self.inner
109            .wait_for_frameset(timeout.as_millis() as u32)
110            .map(|frame| frame.map(FrameSet::from))
111            .map_err(crate::error::OrbbecError::from)
112    }
113}