-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcsi_camera.cpp
More file actions
365 lines (307 loc) · 12 KB
/
csi_camera.cpp
File metadata and controls
365 lines (307 loc) · 12 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <chrono>
#include <cstring>
#include <fstream>
#include <sstream>
#include <thread>
#include "constraints.h"
#include "csi_camera.h"
using namespace viam::sdk;
CSICamera::CSICamera(const std::string name, const ProtoStruct& attrs) : Camera(std::move(name)) {
device = get_device_type();
VIAM_SDK_LOG(debug) << "Creating CSICamera with name: " << name;
VIAM_SDK_LOG(debug) << "Device type: " << device.name;
init(attrs);
}
CSICamera::~CSICamera() {
VIAM_SDK_LOG(debug) << "Destroying CSICamera";
stop_pipeline();
}
void CSICamera::init(const ProtoStruct& attrs) {
validate_attrs(attrs);
auto pipeline_args = create_pipeline();
VIAM_SDK_LOG(debug) << "pipeline_args: " << pipeline_args;
init_csi(pipeline_args);
}
void CSICamera::validate_attrs(const ProtoStruct& attrs) {
set_attr<int>(attrs, "width_px", &CSICamera::width_px, DEFAULT_INPUT_WIDTH);
set_attr<int>(attrs, "height_px", &CSICamera::height_px, DEFAULT_INPUT_HEIGHT);
set_attr<int>(attrs, "frame_rate", &CSICamera::frame_rate, DEFAULT_INPUT_FRAMERATE);
set_attr<std::string>(attrs, "video_path", &CSICamera::video_path, DEFAULT_INPUT_SENSOR);
}
template <typename T>
void CSICamera::set_attr(const ProtoStruct& attrs, const std::string& name, T CSICamera::* member, T de) {
if (attrs.count(name) == 1) {
const ProtoValue& val = attrs.at(name);
if constexpr (std::is_same<T, int>::value) {
this->*member = static_cast<int>(val.get_unchecked<double>());
} else if constexpr (std::is_same<T, std::string>::value) {
this->*member = val.get_unchecked<std::string>();
} else if constexpr (std::is_same<T, bool>::value) {
this->*member = val.get_unchecked<bool>();
}
} else {
this->*member = de; // Set the default value if the attribute is not found
}
}
void CSICamera::reconfigure(const Dependencies& deps, const ResourceConfig& cfg) {
VIAM_SDK_LOG(debug) << "Reconfiguring CSI Camera module";
stop_pipeline();
auto attrs = cfg.attributes();
init(attrs);
}
Camera::raw_image CSICamera::get_image(const std::string mime_type, const ProtoStruct& extra) {
raw_image image;
image.mime_type = DEFAULT_OUTPUT_MIMETYPE;
image.bytes = get_csi_image();
if (image.bytes.empty()) {
throw Exception("no bytes retrieved from get_csi_image");
}
return image;
}
Camera::image_collection CSICamera::get_images(std::vector<std::string> /* filter_source_names */, const ProtoStruct& /* extra */) {
// filter_source_names and extra are unused because this camera provides a single image source
// and doesn't include any extra support for get_images
ProtoStruct empty_extra;
// If image is not available, an exception will be thrown
raw_image image = get_image(DEFAULT_OUTPUT_MIMETYPE, empty_extra);
image.source_name = ""; // empty string because we don't have multiple sources to differentiate
image_collection collection;
collection.images = std::vector<raw_image>{std::move(image)};
auto now = std::chrono::system_clock::now();
auto duration_since_epoch = now.time_since_epoch();
auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration_since_epoch);
collection.metadata.captured_at = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>(nanoseconds);
return collection;
}
ProtoStruct CSICamera::do_command(const ProtoStruct& command) {
VIAM_SDK_LOG(warn) << "do_command not implemented";
return ProtoStruct{};
}
Camera::point_cloud CSICamera::get_point_cloud(const std::string mime_type, const ProtoStruct& extra) {
VIAM_SDK_LOG(warn) << "get_point_cloud not implemented";
return point_cloud{};
}
std::vector<GeometryConfig> CSICamera::get_geometries(const ProtoStruct& extra) {
VIAM_SDK_LOG(error) << "get_geometries not implemented";
return std::vector<GeometryConfig>{};
}
Camera::properties CSICamera::get_properties() {
Camera::properties p{};
p.supports_pcd = false;
p.intrinsic_parameters.width_px = width_px;
p.intrinsic_parameters.height_px = height_px;
return p;
}
void CSICamera::init_csi(const std::string pipeline_args) {
// Build gst pipeline
GError* error = nullptr;
pipeline = gst_parse_launch(pipeline_args.c_str(), &error);
if (!pipeline) {
VIAM_SDK_LOG(error) << "Failed to create the pipeline: " << error->message;
g_error_free(error);
throw Exception("Failed to create the pipeline");
}
// Fetch the appsink element
appsink = gst_bin_get_by_name(GST_BIN(pipeline), "appsink0");
if (!appsink) {
gst_object_unref(pipeline);
throw Exception("Failed to get the appsink element");
}
// Start the pipeline
if (gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
gst_object_unref(appsink);
gst_object_unref(pipeline);
throw Exception("Failed to start the pipeline");
}
// Handle async pipeline creation
wait_pipeline();
// Run the main loop
bus = gst_element_get_bus(pipeline);
if (!bus) {
gst_object_unref(appsink);
gst_object_unref(pipeline);
throw Exception("Failed to get the bus for the pipeline");
}
}
// Handles async GST state change
void CSICamera::wait_pipeline() {
GstState state, pending;
GstStateChangeReturn ret;
// Set timeout for state change
const int timeout_microseconds = GST_CHANGE_STATE_TIMEOUT * 1000000; // Convert seconds to microseconds
auto start_time = std::chrono::high_resolution_clock::now();
// Wait for state change to complete
while ((ret = gst_element_get_state(pipeline, &state, &pending, GST_GET_STATE_TIMEOUT * GST_SECOND)) == GST_STATE_CHANGE_ASYNC) {
auto current_time = std::chrono::high_resolution_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>(current_time - start_time).count();
if (elapsed_time >= timeout_microseconds) {
throw Exception("Timeout: GST pipeline state change did not complete within timeout limit");
}
// Wait for a short duration to avoid busy waiting
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if (ret == GST_STATE_CHANGE_SUCCESS) {
VIAM_SDK_LOG(debug) << "GST pipeline state change success";
} else if (ret == GST_STATE_CHANGE_FAILURE) {
VIAM_SDK_LOG(error) << "GST pipeline failed to change state";
throw Exception("GST pipeline failed to change state");
} else if (ret == GST_STATE_CHANGE_NO_PREROLL) {
VIAM_SDK_LOG(warn) << "GST pipeline changed but not enough data for preroll";
} else {
VIAM_SDK_LOG(error) << "GST pipeline failed to change state";
throw Exception("GST pipeline failed to change state");
}
}
void CSICamera::stop_pipeline() {
VIAM_SDK_LOG(debug) << "Stopping GST pipeline";
// Check if pipeline is defined
if (pipeline == nullptr) {
VIAM_SDK_LOG(error) << "Pipeline is not defined";
return;
}
// Stop the pipeline
if (gst_element_set_state(pipeline, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE) {
// Don't throw, continue cleanup
VIAM_SDK_LOG(error) << "Failed to stop the pipeline";
}
// Wait for async state change
try {
wait_pipeline();
} catch (const std::exception& e) {
// Don't throw, continue cleanup
VIAM_SDK_LOG(error) << "Exception during wait_pipeline: " << e.what();
}
// Free resources
if (appsink)
gst_object_unref(appsink);
if (pipeline)
gst_object_unref(pipeline);
if (bus)
gst_object_unref(bus);
appsink = nullptr;
pipeline = nullptr;
bus = nullptr;
}
void CSICamera::catch_pipeline(GstMessage* msg) {
if (msg == nullptr) {
VIAM_SDK_LOG(debug) << "catch_pipeline called with null message";
return;
}
GError* error = nullptr;
gchar* debugInfo = nullptr;
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_ERROR: {
gst_message_parse_error(msg, &error, &debugInfo);
VIAM_SDK_LOG(debug) << "Debug Info: " << debugInfo;
std::string err_msg = error->message;
g_error_free(error);
g_free(debugInfo);
stop_pipeline();
throw Exception("GST pipeline error: " + err_msg);
}
case GST_MESSAGE_EOS:
VIAM_SDK_LOG(debug) << "End of stream received, stopping pipeline";
stop_pipeline();
throw Exception("End of stream received, pipeline stopped");
break;
case GST_MESSAGE_WARNING:
gst_message_parse_warning(msg, &error, &debugInfo);
VIAM_SDK_LOG(warn) << "Warning: " << error->message;
VIAM_SDK_LOG(warn) << "Debug Info: " << debugInfo;
break;
case GST_MESSAGE_INFO:
gst_message_parse_info(msg, &error, &debugInfo);
VIAM_SDK_LOG(info) << "Info: " << error->message;
VIAM_SDK_LOG(info) << "Debug Info: " << debugInfo;
break;
default:
// Ignore other message types
break;
}
// Cleanup
if (error != nullptr)
g_error_free(error);
if (debugInfo != nullptr)
g_free(debugInfo);
}
std::vector<unsigned char> CSICamera::get_csi_image() {
// Pull sample from appsink
std::vector<unsigned char> vec;
GstSample* sample = gst_app_sink_pull_sample(GST_APP_SINK(appsink));
if (sample != nullptr) {
// Retrieve buffer from the sample
GstBuffer* buffer = gst_sample_get_buffer(sample);
// Process or handle the buffer as needed
if (buffer != nullptr) {
vec = buff_to_vec(buffer);
} else {
VIAM_SDK_LOG(warn) << "Failed to get buffer from sample";
}
// Release the sample
gst_sample_unref(sample);
}
// Check bus for messages
GstMessage* msg = gst_bus_pop(bus);
if (msg != nullptr) {
try {
catch_pipeline(msg);
} catch (...) {
gst_message_unref(msg);
throw;
}
gst_message_unref(msg);
}
return vec;
}
std::string CSICamera::create_pipeline() const {
const char* test_mode = std::getenv("VIAM_CSI_TEST_MODE");
if (test_mode != nullptr && std::string(test_mode) == "1") {
VIAM_SDK_LOG(warn) << "CI Test mode enabled";
return TEST_GST_PIPELINE;
}
auto device_params = get_device_params(device);
std::string input_sensor = (device.value == device_type::jetson) ? (" sensor-id=" + video_path) : "";
std::ostringstream oss;
oss << device_params.input_source << input_sensor << " ! " << device_params.input_format << ",width=" << std::to_string(width_px)
<< ",height=" << std::to_string(height_px) << ",framerate=" << std::to_string(frame_rate) << "/1 ! "
<< device_params.video_converter << " ! " << device_params.output_encoder << " ! "
<< "image/jpeg"
<< " ! appsink name=appsink0 sync=false max-buffers=1 drop=true";
return oss.str();
}
std::vector<unsigned char> CSICamera::buff_to_vec(GstBuffer* buff) {
if (buff == nullptr) {
throw Exception("Cannot convert null buffer to vector");
}
// Get the size of the buffer
size_t bufferSize = gst_buffer_get_size(buff);
// Create a vector with the same size as the buffer
std::vector<unsigned char> vec(bufferSize);
// Copy the buffer data to the vector
GstMapInfo map;
gst_buffer_map(buff, &map, GST_MAP_READ);
memcpy(vec.data(), map.data, bufferSize);
gst_buffer_unmap(buff, &map);
return vec;
}
std::string CSICamera::get_video_path() const {
return video_path;
}
int CSICamera::get_width_px() const {
return width_px;
}
int CSICamera::get_height_px() const {
return height_px;
}
int CSICamera::get_frame_rate() const {
return frame_rate;
}
GstElement* CSICamera::get_appsink() const {
return appsink;
}
GstElement* CSICamera::get_pipeline() const {
return pipeline;
}
GstBus* CSICamera::get_bus() const {
return bus;
}