realsense_rust/pipeline/
profile.rs1use 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#[derive(Debug)]
11pub struct PipelineProfile {
12 device: Device,
14 streams: Vec<StreamProfile>,
17}
18
19#[derive(Error, Debug)]
21pub enum PipelineProfileConstructionError {
22 #[error("Could not retrieve device from pipeline profile. Type: {0}; Reason: {1}")]
24 CouldNotRetrieveDevice(Rs2Exception, String),
25 #[error(
27 "Could not retrieve stream profile list from pipeline profile. Type: {0}; Reason: {1}"
28 )]
29 CouldNotRetrieveStreamList(Rs2Exception, String),
30 #[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 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 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 pub fn device(&self) -> &Device {
96 &self.device
97 }
98
99 pub fn streams(&self) -> &Vec<StreamProfile> {
101 &self.streams
102 }
103}