1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Defines the profile type of streams.

use crate::{
    base::{Extrinsics, Intrinsics, MotionIntrinsics, Resolution, StreamProfileData},
    common::*,
    error::{ErrorChecker, Result as RsResult},
    kind::{Extension, Format, StreamKind},
};

/// Marker traits and types for [StreamProfile].
pub mod marker {
    use super::*;

    /// The marker traits of all kinds of StreamProfile.
    pub trait StreamProfileKind {}

    /// The marker traits of all kinds of StreamProfile except [Any](Any).
    pub trait NonAnyStreamProfileKind
    where
        Self: StreamProfileKind,
    {
        const EXTENSION: Extension;
    }

    #[derive(Debug)]
    pub struct Any;
    impl StreamProfileKind for Any {}

    #[derive(Debug)]
    pub struct Video;
    impl StreamProfileKind for Video {}
    impl NonAnyStreamProfileKind for Video {
        const EXTENSION: Extension = Extension::VideoProfile;
    }

    #[derive(Debug)]
    pub struct Motion;
    impl StreamProfileKind for Motion {}
    impl NonAnyStreamProfileKind for Motion {
        const EXTENSION: Extension = Extension::MotionProfile;
    }

    #[derive(Debug)]
    pub struct Pose;
    impl StreamProfileKind for Pose {}
    impl NonAnyStreamProfileKind for Pose {
        const EXTENSION: Extension = Extension::PoseProfile;
    }
}

/// The enumeration of extended stream profile type returned by [StreamProfile::try_extend](StreamProfile::try_extend).
#[derive(Debug)]
pub enum ExtendedStreamProfile {
    Video(StreamProfile<marker::Video>),
    Motion(StreamProfile<marker::Motion>),
    Pose(StreamProfile<marker::Pose>),
    Other(StreamProfile<marker::Any>),
}

/// The profile of stream.
#[derive(Debug)]
pub struct StreamProfile<Kind>
where
    Kind: marker::StreamProfileKind,
{
    ptr: NonNull<realsense_sys::rs2_stream_profile>,
    from_clone: bool,
    _phantom: PhantomData<Kind>,
}

impl<Kind> StreamProfile<Kind>
where
    Kind: marker::StreamProfileKind,
{
    /// Check whether the profile is default or not.
    pub fn is_default(&self) -> RsResult<bool> {
        unsafe {
            let mut checker = ErrorChecker::new();
            let val = realsense_sys::rs2_is_stream_profile_default(
                self.ptr.as_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Ok(val != 0)
        }
    }

    /// Gets the attributes of stream.
    pub fn get_data(&self) -> RsResult<StreamProfileData> {
        unsafe {
            let mut checker = ErrorChecker::new();
            let mut stream = MaybeUninit::uninit();
            let mut format = MaybeUninit::uninit();
            let mut index = MaybeUninit::uninit();
            let mut unique_id = MaybeUninit::uninit();
            let mut framerate = MaybeUninit::uninit();

            realsense_sys::rs2_get_stream_profile_data(
                self.ptr.as_ptr(),
                stream.as_mut_ptr(),
                format.as_mut_ptr(),
                index.as_mut_ptr(),
                unique_id.as_mut_ptr(),
                framerate.as_mut_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;

            let data = StreamProfileData {
                stream: StreamKind::from_u32(stream.assume_init()).unwrap(),
                format: Format::from_u32(format.assume_init()).unwrap(),
                index: index.assume_init() as usize,
                unique_id: unique_id.assume_init(),
                framerate: framerate.assume_init(),
            };
            Ok(data)
        }
    }

    // /// Sets the attributes of stream.
    // pub fn set_data(&mut self) {
    //     todo!();
    // }

    /// Gets the extrinsic parameters to another stream.
    pub fn get_extrinsics<P, K>(&self, to_stream: P) -> RsResult<Extrinsics>
    where
        P: Borrow<StreamProfile<K>>,
        K: marker::StreamProfileKind,
    {
        unsafe {
            let mut extrinsics = MaybeUninit::<realsense_sys::rs2_extrinsics>::uninit();
            let mut checker = ErrorChecker::new();
            realsense_sys::rs2_get_extrinsics(
                self.ptr.as_ptr(),
                to_stream.borrow().ptr.as_ptr(),
                extrinsics.as_mut_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Ok(Extrinsics(extrinsics.assume_init()))
        }
    }

    /// Sets the extrinsic parameters to another stream.
    pub fn set_extrinsics<P, K>(&self, to_stream: P, extrinsics: Extrinsics) -> RsResult<()>
    where
        P: Borrow<StreamProfile<K>>,
        K: marker::StreamProfileKind,
    {
        unsafe {
            let mut checker = ErrorChecker::new();
            realsense_sys::rs2_register_extrinsics(
                self.ptr.as_ptr(),
                to_stream.borrow().ptr.as_ptr(),
                *extrinsics,
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Ok(())
        }
    }

    pub(crate) unsafe fn take(self) -> (NonNull<realsense_sys::rs2_stream_profile>, bool) {
        let ptr = self.ptr;
        let from_clone = self.from_clone;
        std::mem::forget(self);
        (ptr, from_clone)
    }

    pub(crate) unsafe fn from_parts(
        ptr: NonNull<realsense_sys::rs2_stream_profile>,
        from_clone: bool,
    ) -> Self {
        Self {
            ptr,
            from_clone,
            _phantom: PhantomData,
        }
    }
}

impl StreamProfile<marker::Any> {
    /// Check if the stream is extendable to the given extension.
    pub fn is_extendable_to<Kind>(&self) -> RsResult<bool>
    where
        Kind: marker::NonAnyStreamProfileKind,
    {
        unsafe {
            let mut checker = ErrorChecker::new();
            let val = realsense_sys::rs2_stream_profile_is(
                self.ptr.as_ptr(),
                Kind::EXTENSION as realsense_sys::rs2_extension,
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Ok(val != 0)
        }
    }

    /// Extends to a specific stream profile subtype.
    pub fn try_extend_to<Kind>(self) -> RsResult<Result<StreamProfile<Kind>, Self>>
    where
        Kind: marker::NonAnyStreamProfileKind,
    {
        if self.is_extendable_to::<Kind>()? {
            let (ptr, from_clone) = unsafe { self.take() };
            let profile = StreamProfile {
                ptr,
                from_clone,
                _phantom: PhantomData,
            };
            Ok(Ok(profile))
        } else {
            Ok(Err(self))
        }
    }

    /// Extends to one of a stream profile subtype.
    pub fn try_extend(self) -> RsResult<ExtendedStreamProfile> {
        let profile_any = self;

        let profile_any = match profile_any.try_extend_to::<marker::Video>()? {
            Ok(profile) => return Ok(ExtendedStreamProfile::Video(profile)),
            Err(profile) => profile,
        };

        let profile_any = match profile_any.try_extend_to::<marker::Motion>()? {
            Ok(profile) => return Ok(ExtendedStreamProfile::Motion(profile)),
            Err(profile) => profile,
        };

        let profile_any = match profile_any.try_extend_to::<marker::Pose>()? {
            Ok(profile) => return Ok(ExtendedStreamProfile::Pose(profile)),
            Err(profile) => profile,
        };

        Ok(ExtendedStreamProfile::Other(profile_any))
    }
}

impl StreamProfile<marker::Video> {
    /// Gets the resolution of stream.
    pub fn resolution(&self) -> RsResult<Resolution> {
        let mut width = MaybeUninit::uninit();
        let mut height = MaybeUninit::uninit();
        let resolution = unsafe {
            let mut checker = ErrorChecker::new();
            realsense_sys::rs2_get_video_stream_resolution(
                self.ptr.as_ptr(),
                width.as_mut_ptr(),
                height.as_mut_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;

            Resolution {
                width: width.assume_init() as usize,
                height: height.assume_init() as usize,
            }
        };
        Ok(resolution)
    }

    /// Gets the intrinsic parameters.
    pub fn intrinsics(&self) -> RsResult<Intrinsics> {
        unsafe {
            let mut checker = ErrorChecker::new();
            let mut intrinsics = MaybeUninit::<realsense_sys::rs2_intrinsics>::uninit();
            realsense_sys::rs2_get_video_stream_intrinsics(
                self.ptr.as_ptr(),
                intrinsics.as_mut_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Ok(Intrinsics(intrinsics.assume_init()))
        }
    }
}

impl StreamProfile<marker::Motion> {
    /// Gets the motion intrinsic parameters.
    pub fn motion_intrinsics(&self) -> RsResult<MotionIntrinsics> {
        unsafe {
            let mut checker = ErrorChecker::new();
            let mut intrinsics =
                MaybeUninit::<realsense_sys::rs2_motion_device_intrinsic>::uninit();
            realsense_sys::rs2_get_motion_intrinsics(
                self.ptr.as_ptr(),
                intrinsics.as_mut_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Ok(MotionIntrinsics(intrinsics.assume_init()))
        }
    }
}

impl<Kind> Drop for StreamProfile<Kind>
where
    Kind: marker::StreamProfileKind,
{
    fn drop(&mut self) {
        unsafe {
            if self.from_clone {
                realsense_sys::rs2_delete_stream_profile(self.ptr.as_ptr());
            }
        }
    }
}

unsafe impl<Kind> Send for StreamProfile<Kind> where Kind: marker::StreamProfileKind {}