orbbec_sdk/
stream.rs

1//! Stream module
2use crate::{
3    Format,
4    error::OrbbecError,
5    sys::stream::{OBCameraIntrinsic, OBStreamProfile, OBStreamProfileList},
6};
7
8/// Stream profile trait
9pub trait StreamProfile: AsRef<OBStreamProfile> {}
10
11/// Camera intrinsic for a stream
12#[derive(Debug, Clone, Copy, PartialEq)]
13pub struct CameraIntrinsic {
14    /// Focal length in pixels along X axis
15    pub fx: f32,
16    /// Focal length in pixels along Y axis
17    pub fy: f32,
18    /// Optical center abscissa
19    pub cx: f32,
20    /// Optical center ordinate
21    pub cy: f32,
22    /// Image width in pixels
23    pub width: u16,
24    /// Image height in pixels
25    pub height: u16,
26}
27
28impl From<OBCameraIntrinsic> for CameraIntrinsic {
29    fn from(intrinsic: OBCameraIntrinsic) -> Self {
30        CameraIntrinsic {
31            fx: intrinsic.fx(),
32            fy: intrinsic.fy(),
33            cx: intrinsic.cx(),
34            cy: intrinsic.cy(),
35            width: intrinsic.width() as u16,
36            height: intrinsic.height() as u16,
37        }
38    }
39}
40
41/// Video Stream profile
42pub struct VideoStreamProfile {
43    inner: OBStreamProfile,
44}
45
46impl VideoStreamProfile {
47    pub(crate) fn new(inner: OBStreamProfile) -> Self {
48        VideoStreamProfile { inner }
49    }
50
51    pub(crate) fn inner(&self) -> &OBStreamProfile {
52        &self.inner
53    }
54
55    /// Get the camera intrinsic parameters for this video stream profile
56    pub fn get_intrinsic(&self) -> Result<CameraIntrinsic, OrbbecError> {
57        self.inner
58            .get_video_intrinsic()
59            .map(|intrinsic| intrinsic.into())
60            .map_err(OrbbecError::from)
61    }
62}
63
64impl AsRef<OBStreamProfile> for VideoStreamProfile {
65    fn as_ref(&self) -> &OBStreamProfile {
66        &self.inner
67    }
68}
69impl StreamProfile for VideoStreamProfile {}
70
71/// List of video stream profiles
72pub struct StreamProfileList {
73    inner: OBStreamProfileList,
74}
75
76impl StreamProfileList {
77    pub(crate) fn new(inner: OBStreamProfileList) -> Self {
78        StreamProfileList { inner }
79    }
80
81    /// Get the corresponding video stream profile through the passed parameters.
82    /// ### Arguments
83    /// * `width` - Width of the stream in pixels
84    /// * `height` - Height of the stream in pixels
85    /// * `format` - Pixel format of the stream
86    /// * `fps` - Frame rate of the stream in frames per second
87    pub fn get_video_stream_profile(
88        &self,
89        width: u16,
90        height: u16,
91        format: Format,
92        fps: u8,
93    ) -> Result<VideoStreamProfile, OrbbecError> {
94        self.inner
95            .get_video_stream_profile(width as i32, height as i32, format, fps as i32)
96            .map(VideoStreamProfile::new)
97            .map_err(OrbbecError::from)
98    }
99}