realsense_rust/pipeline/
profile.rs

1//! Defines a type which holds the device & streams associated with an `ActivePipeline`.
2
3use crate::{check_rs2_error, device::Device, kind::Rs2Exception, stream_profile::StreamProfile};
4use anyhow::Result;
5use realsense_sys as sys;
6use std::{convert::TryFrom, ptr::NonNull};
7use thiserror::Error;
8
9/// Type representing the device and streams associated with a pipeline.
10#[derive(Debug)]
11pub struct PipelineProfile {
12    /// The device that this profile is configured for.
13    device: Device,
14    /// A vector of stream profiles for streams that are emitting observations from the pipeline
15    /// that this profile corresponds to.
16    streams: Vec<StreamProfile>,
17}
18
19/// Type representing possible errors that can occur during pipeline profile construction.
20#[derive(Error, Debug)]
21pub enum PipelineProfileConstructionError {
22    /// Could not retrieve the device from the underlying pipeline profile pointer.
23    #[error("Could not retrieve device from pipeline profile. Type: {0}; Reason: {1}")]
24    CouldNotRetrieveDevice(Rs2Exception, String),
25    /// Could not retrieve the list of stream profiles from the underlying pipeline profile pointer.
26    #[error(
27        "Could not retrieve stream profile list from pipeline profile. Type: {0}; Reason: {1}"
28    )]
29    CouldNotRetrieveStreamList(Rs2Exception, String),
30    /// Could not retrieve the count of stream profiles from the underlying pipeline profile pointer.
31    #[error(
32        "Could not retrieve stream profile count from pipeline profile. Type: {0}; Reason: {1}"
33    )]
34    CouldNotRetrieveStreamCount(Rs2Exception, String),
35}
36
37impl TryFrom<NonNull<sys::rs2_pipeline_profile>> for PipelineProfile {
38    type Error = anyhow::Error;
39
40    /// Attempts to construct a `PipelineProfile` from an `rs2_pipeline_profile` pointer.
41    ///
42    /// # Errors
43    ///
44    /// Returns [`PipelineProfileConstructionError::CouldNotRetrieveDevice`] if the device
45    /// associated with the pipeline profile cannot be obtained.
46    ///
47    /// Returns [`PipelineProfileConstructionError::CouldNotRetrieveStreamList`] if the list of
48    /// streams associated with the pipeline profile cannot be obtained.
49    ///
50    /// Returns [`PipelineProfileConstructionError::CouldNotRetrieveStreamCount`] if the length of
51    /// the list of streams associated with the pipeline profile cannot be obtained.
52    fn try_from(
53        pipeline_profile_ptr: NonNull<sys::rs2_pipeline_profile>,
54    ) -> Result<Self, Self::Error> {
55        unsafe {
56            let mut err = std::ptr::null_mut::<sys::rs2_error>();
57            let device_ptr =
58                sys::rs2_pipeline_profile_get_device(pipeline_profile_ptr.as_ptr(), &mut err);
59            check_rs2_error!(
60                err,
61                PipelineProfileConstructionError::CouldNotRetrieveDevice
62            )?;
63
64            // Create the device object
65            let device = Device::from(NonNull::new(device_ptr).unwrap());
66
67            let stream_list_ptr =
68                sys::rs2_pipeline_profile_get_streams(pipeline_profile_ptr.as_ptr(), &mut err);
69            check_rs2_error!(
70                err,
71                PipelineProfileConstructionError::CouldNotRetrieveStreamList
72            )?;
73
74            let nonnull_stream_list = NonNull::new(stream_list_ptr).unwrap();
75            let len = sys::rs2_get_stream_profiles_count(nonnull_stream_list.as_ptr(), &mut err);
76            check_rs2_error!(
77                err,
78                PipelineProfileConstructionError::CouldNotRetrieveStreamCount
79            )?;
80
81            let mut streams = Vec::new();
82            for i in 0..len {
83                streams.push(StreamProfile::try_create(&nonnull_stream_list, i)?);
84            }
85
86            sys::rs2_delete_stream_profiles_list(nonnull_stream_list.as_ptr());
87            sys::rs2_delete_pipeline_profile(pipeline_profile_ptr.as_ptr());
88            Ok(Self { device, streams })
89        }
90    }
91}
92
93impl PipelineProfile {
94    /// Gets the device associated with a pipeline.
95    pub fn device(&self) -> &Device {
96        &self.device
97    }
98
99    /// Gets list of streams associated with a pipeline.
100    pub fn streams(&self) -> &Vec<StreamProfile> {
101        &self.streams
102    }
103}