-
Notifications
You must be signed in to change notification settings - Fork 2
/
csi_camera.cpp
357 lines (301 loc) · 10.9 KB
/
csi_camera.cpp
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <chrono>
#include <thread>
#include "constraints.h"
#include "csi_camera.h"
using namespace viam::sdk;
CSICamera::CSICamera(const std::string name, const AttributeMap attrs) : Camera(std::move(name)) {
device = get_device_type();
std::cout << "Creating CSICamera with name: " << name << std::endl;
std::cout << "Device type: " << device.name << std::endl;
init(attrs);
}
CSICamera::~CSICamera() {
std::cout << "Destroying CSICamera" << std::endl;
stop_pipeline();
}
void CSICamera::init(const AttributeMap attrs) {
validate_attrs(attrs);
auto pipeline_args = create_pipeline();
if (debug) {
std::cout << "pipeline_args: " << pipeline_args << std::endl;
}
init_csi(pipeline_args);
}
void CSICamera::validate_attrs(const AttributeMap 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);
set_attr<bool>(attrs, "debug", &CSICamera::debug, false);
}
template <typename T>
void CSICamera::set_attr(const AttributeMap& attrs, const std::string& name, T CSICamera::* member, T de) {
if (attrs->count(name) == 1) {
std::shared_ptr<ProtoType> proto = attrs->at(name);
auto val = proto->proto_value();
if constexpr (std::is_same<T, int>::value) {
this->*member = val.number_value();
} else if constexpr (std::is_same<T, std::string>::value) {
this->*member = val.string_value();
} else if constexpr (std::is_same<T, bool>::value) {
this->*member = val.bool_value();
}
} else {
this->*member = de; // Set the default value if the attribute is not found
}
}
void CSICamera::reconfigure(const Dependencies deps, const ResourceConfig cfg) {
if (debug) {
std::cout << "Reconfiguring CSI Camera module" << std::endl;
}
stop_pipeline();
auto attrs = cfg.attributes();
init(attrs);
}
Camera::raw_image CSICamera::get_image(const std::string mime_type, const AttributeMap& extra) {
if (debug) {
std::cout << "hit get_image. expecting mime_type " << mime_type << std::endl;
}
raw_image image;
image.mime_type = DEFAULT_OUTPUT_MIMETYPE;
image.bytes = get_csi_image();
if (image.bytes.empty()) {
std::cerr << "ERROR: no bytes retrieved" << std::endl;
}
return image;
}
Camera::image_collection CSICamera::get_images() {
std::cerr << "get_images not implemented" << std::endl;
return image_collection{};
}
AttributeMap CSICamera::do_command(const AttributeMap command) {
std::cerr << "do_command not implemented" << std::endl;
return 0;
}
Camera::point_cloud CSICamera::get_point_cloud(const std::string mime_type, const AttributeMap& extra) {
std::cerr << "get_point_cloud not implemented" << std::endl;
return point_cloud{};
}
std::vector<GeometryConfig> CSICamera::get_geometries(const AttributeMap& extra) {
std::cerr << "get_geometries not implemented" << std::endl;
return std::vector<GeometryConfig>{};
}
Camera::properties CSICamera::get_properties() {
std::cerr << "get_properties not implemented" << std::endl;
return properties{};
}
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) {
std::cerr << "Failed to create the pipeline" << std::endl;
g_print("Error: %s\n", error->message);
g_error_free(error);
std::exit(EXIT_FAILURE);
}
// Print pipeline structure
if (debug) {
GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline_structure");
}
// Fetch the appsink element
appsink = gst_bin_get_by_name(GST_BIN(pipeline), "appsink0");
if (!appsink) {
std::cerr << "Failed to get the appsink element" << std::endl;
gst_object_unref(pipeline);
std::exit(EXIT_FAILURE);
}
// Start the pipeline
if (gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
std::cerr << "Failed to start the pipeline" << std::endl;
gst_object_unref(appsink);
gst_object_unref(pipeline);
std::exit(EXIT_FAILURE);
}
// Handle async pipeline creation
wait_pipeline();
// Run the main loop
bus = gst_element_get_bus(pipeline);
if (!bus) {
std::cerr << "Failed to get the bus for the pipeline" << std::endl;
gst_object_unref(appsink);
gst_object_unref(pipeline);
std::exit(EXIT_FAILURE);
}
}
// 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) {
std::cerr << "Timeout: GST pipeline state change did not complete within timeout limit" << std::endl;
std::exit(EXIT_FAILURE);
}
// Wait for a short duration to avoid busy waiting
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if (ret == GST_STATE_CHANGE_SUCCESS) {
std::cout << "GST pipeline state change success" << std::endl;
} else if (ret == GST_STATE_CHANGE_FAILURE) {
std::cerr << "GST pipeline failed to change state" << std::endl;
std::exit(EXIT_FAILURE);
} else if (ret = GST_STATE_CHANGE_NO_PREROLL) {
std::cout << "GST pipeline changed but not enough data for preroll" << std::endl;
} else {
std::cerr << "GST pipeline failed to change state" << std::endl;
std::exit(EXIT_FAILURE);
}
}
void CSICamera::stop_pipeline() {
if (debug) {
std::cout << "Stopping GST pipeline" << std::endl;
}
// Check if pipeline is defined
if (pipeline == nullptr) {
std::cout << "Pipeline is not defined" << std::endl;
return;
}
// Stop the pipeline
if (gst_element_set_state(pipeline, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE) {
std::cerr << "Failed to stop the pipeline" << std::endl;
gst_object_unref(appsink);
gst_object_unref(pipeline);
std::exit(EXIT_FAILURE);
}
// Wait for async state change
wait_pipeline();
// Free resources
gst_object_unref(appsink);
gst_object_unref(pipeline);
gst_object_unref(bus);
appsink = nullptr;
pipeline = nullptr;
bus = nullptr;
}
void CSICamera::catch_pipeline() {
GError* error = nullptr;
gchar* debugInfo = nullptr;
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error(msg, &error, &debugInfo);
std::cerr << "Error: " << error->message << std::endl;
std::cerr << "Debug Info: " << debugInfo << std::endl;
stop_pipeline();
std::exit(EXIT_FAILURE);
break;
case GST_MESSAGE_EOS:
std::cout << "End of stream received" << std::endl;
stop_pipeline();
std::exit(EXIT_SUCCESS);
break;
case GST_MESSAGE_WARNING:
gst_message_parse_warning(msg, &error, &debugInfo);
if (debug) {
std::cout << "Warning: " << error->message << std::endl;
std::cout << "Debug Info: " << debugInfo << std::endl;
}
break;
case GST_MESSAGE_INFO:
gst_message_parse_info(msg, &error, &debugInfo);
if (debug) {
std::cout << "Info: " << error->message << std::endl;
std::cout << "Debug Info: " << debugInfo << std::endl;
}
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;
sample = gst_app_sink_pull_sample(GST_APP_SINK(appsink));
if (sample != nullptr) {
// Retrieve buffer from the sample
buffer = gst_sample_get_buffer(sample);
// Process or handle the buffer as needed
vec = buff_to_vec(buffer);
// Release the sample
gst_sample_unref(sample);
}
// Check bus for messages
msg = gst_bus_pop(bus);
if (msg != nullptr) {
catch_pipeline();
gst_message_unref(msg);
msg = nullptr;
}
return vec;
}
std::string CSICamera::create_pipeline() const {
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) {
// 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(buffer, &map, GST_MAP_READ);
memcpy(vec.data(), map.data, bufferSize);
gst_buffer_unmap(buffer, &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;
}
bool CSICamera::is_debug() const {
return debug;
}
GstElement* CSICamera::get_appsink() const {
return appsink;
}
GstElement* CSICamera::get_pipeline() const {
return pipeline;
}
GstBus* CSICamera::get_bus() const {
return bus;
}