forked from jerry73204/realsense-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
179 lines (164 loc) · 5.02 KB
/
config.rs
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
//! Configuration type for [Pipeline](crate::pipeline::Pipeline).
use crate::{
base::TryIntoCowCStr,
common::*,
error::{ErrorChecker, Result},
kind::{Format, StreamKind},
pipeline::Pipeline,
pipeline_kind::PipelineState,
pipeline_profile::PipelineProfile,
};
/// The pipeline configuration that will be consumed by [Pipeline::start()](crate::pipeline::Pipeline::start).
#[derive(Debug)]
pub struct Config {
pub(crate) ptr: NonNull<sys::rs2_config>,
}
impl Config {
/// Create an instance.
pub fn new() -> Result<Self> {
let ptr = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_create_config(checker.inner_mut_ptr());
checker.check()?;
ptr
};
let config = Self {
ptr: NonNull::new(ptr).unwrap(),
};
Ok(config)
}
/// Enable data stream with attributes.
pub fn enable_stream(
self,
stream: StreamKind,
index: usize,
width: usize,
height: usize,
format: Format,
framerate: usize,
) -> Result<Self> {
unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_config_enable_stream(
self.ptr.as_ptr(),
stream as sys::rs2_stream,
index as c_int,
width as c_int,
height as c_int,
format as sys::rs2_format,
framerate as c_int,
checker.inner_mut_ptr(),
);
checker.check()?;
};
Ok(self)
}
/// Enable all device streams explicitly.
pub fn enable_all_streams(self) -> Result<Self> {
unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_config_enable_all_stream(self.ptr.as_ptr(), checker.inner_mut_ptr());
checker.check()?;
}
Ok(self)
}
/// Enable device from a serial number.
pub fn enable_device_from_serial<'a>(self, serial: impl TryIntoCowCStr<'a>) -> Result<Self> {
let serial = serial.try_into_cow_cstr()?;
unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_config_enable_device(
self.ptr.as_ptr(),
serial.as_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
}
Ok(self)
}
/// Enable device from a file path.
pub fn enable_device_from_file<'a>(self, file: impl TryIntoCowCStr<'a>) -> Result<Self> {
let file = file.try_into_cow_cstr()?;
unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_config_enable_device_from_file(
self.ptr.as_ptr(),
file.as_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
}
Ok(self)
}
/// Disable data stream by stream index.
pub fn disable_index_stream(self, stream: StreamKind, index: usize) -> Result<Self> {
unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_config_disable_indexed_stream(
self.ptr.as_ptr(),
stream as sys::rs2_stream,
index as c_int,
checker.inner_mut_ptr(),
);
checker.check()?;
}
Ok(self)
}
/// Disable data stream by stream kind.
pub fn disable_stream(self, stream: StreamKind) -> Result<Self> {
unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_config_disable_stream(
self.ptr.as_ptr(),
stream as sys::rs2_stream,
checker.inner_mut_ptr(),
);
checker.check()?;
}
Ok(self)
}
/// Disable all device streams explicitly.
pub fn disable_all_streams(self) -> Result<Self> {
unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_config_disable_all_streams(self.ptr.as_ptr(), checker.inner_mut_ptr());
checker.check()?;
}
Ok(self)
}
/// Enable all device streams explicitly.
pub fn resolve<S>(&self, pipeline: &Pipeline<S>) -> Result<PipelineProfile>
where
S: PipelineState,
{
let profile = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_config_resolve(
self.ptr.as_ptr(),
pipeline.ptr.as_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
PipelineProfile::from_raw(ptr)
};
Ok(profile)
}
pub fn into_raw(self) -> *mut sys::rs2_config {
let ptr = self.ptr;
mem::forget(self);
ptr.as_ptr()
}
pub unsafe fn from_raw(ptr: *mut sys::rs2_config) -> Self {
Self {
ptr: NonNull::new(ptr).unwrap(),
}
}
}
impl Drop for Config {
fn drop(&mut self) {
unsafe {
sys::rs2_delete_config(self.ptr.as_ptr());
}
}
}
unsafe impl Send for Config {}