Skip to content

Commit 7e214d5

Browse files
authored
[RSDK-13311] - Simplify gstreamer state (#37)
* Simplify gstreamer state * Added nullptr check * Check buffer * Add debug log * Try catch * Fix memory leak on err case
1 parent e704c4c commit 7e214d5

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

csi_camera.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,25 @@ void CSICamera::stop_pipeline() {
218218
bus = nullptr;
219219
}
220220

221-
void CSICamera::catch_pipeline() {
221+
void CSICamera::catch_pipeline(GstMessage* msg) {
222+
if (msg == nullptr) {
223+
VIAM_SDK_LOG(debug) << "catch_pipeline called with null message";
224+
return;
225+
}
226+
222227
GError* error = nullptr;
223228
gchar* debugInfo = nullptr;
224229

225230
switch (GST_MESSAGE_TYPE(msg)) {
226-
case GST_MESSAGE_ERROR:
231+
case GST_MESSAGE_ERROR: {
227232
gst_message_parse_error(msg, &error, &debugInfo);
228233
VIAM_SDK_LOG(debug) << "Debug Info: " << debugInfo;
234+
std::string err_msg = error->message;
235+
g_error_free(error);
236+
g_free(debugInfo);
229237
stop_pipeline();
230-
throw Exception("GST pipeline error: " + std::string(error->message));
231-
break;
238+
throw Exception("GST pipeline error: " + err_msg);
239+
}
232240
case GST_MESSAGE_EOS:
233241
VIAM_SDK_LOG(debug) << "End of stream received, stopping pipeline";
234242
stop_pipeline();
@@ -259,24 +267,32 @@ void CSICamera::catch_pipeline() {
259267
std::vector<unsigned char> CSICamera::get_csi_image() {
260268
// Pull sample from appsink
261269
std::vector<unsigned char> vec;
262-
sample = gst_app_sink_pull_sample(GST_APP_SINK(appsink));
270+
GstSample* sample = gst_app_sink_pull_sample(GST_APP_SINK(appsink));
263271
if (sample != nullptr) {
264272
// Retrieve buffer from the sample
265-
buffer = gst_sample_get_buffer(sample);
273+
GstBuffer* buffer = gst_sample_get_buffer(sample);
266274

267275
// Process or handle the buffer as needed
268-
vec = buff_to_vec(buffer);
276+
if (buffer != nullptr) {
277+
vec = buff_to_vec(buffer);
278+
} else {
279+
VIAM_SDK_LOG(warn) << "Failed to get buffer from sample";
280+
}
269281

270282
// Release the sample
271283
gst_sample_unref(sample);
272284
}
273285

274286
// Check bus for messages
275-
msg = gst_bus_pop(bus);
287+
GstMessage* msg = gst_bus_pop(bus);
276288
if (msg != nullptr) {
277-
catch_pipeline();
289+
try {
290+
catch_pipeline(msg);
291+
} catch (...) {
292+
gst_message_unref(msg);
293+
throw;
294+
}
278295
gst_message_unref(msg);
279-
msg = nullptr;
280296
}
281297

282298
return vec;
@@ -303,6 +319,10 @@ std::string CSICamera::create_pipeline() const {
303319
}
304320

305321
std::vector<unsigned char> CSICamera::buff_to_vec(GstBuffer* buff) {
322+
if (buff == nullptr) {
323+
throw Exception("Cannot convert null buffer to vector");
324+
}
325+
306326
// Get the size of the buffer
307327
size_t bufferSize = gst_buffer_get_size(buff);
308328

@@ -311,9 +331,9 @@ std::vector<unsigned char> CSICamera::buff_to_vec(GstBuffer* buff) {
311331

312332
// Copy the buffer data to the vector
313333
GstMapInfo map;
314-
gst_buffer_map(buffer, &map, GST_MAP_READ);
334+
gst_buffer_map(buff, &map, GST_MAP_READ);
315335
memcpy(vec.data(), map.data, bufferSize);
316-
gst_buffer_unmap(buffer, &map);
336+
gst_buffer_unmap(buff, &map);
317337

318338
return vec;
319339
}

csi_camera.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ class CSICamera : public Camera, public Reconfigurable {
3333
// GST
3434
GstElement* pipeline = nullptr;
3535
GstBus* bus = nullptr;
36-
GstMessage* msg = nullptr;
37-
GstSample* sample = nullptr;
38-
GstBuffer* buffer = nullptr;
3936
GstElement* appsink = nullptr;
4037

4138
public:
@@ -63,15 +60,14 @@ class CSICamera : public Camera, public Reconfigurable {
6360
std::string create_pipeline() const;
6461
void wait_pipeline();
6562
void stop_pipeline();
66-
void catch_pipeline();
63+
void catch_pipeline(GstMessage* msg);
6764

6865
// Image
6966
// helpers to pull and process images from appsink
7067
std::vector<unsigned char> get_csi_image();
7168
std::vector<unsigned char> buff_to_vec(GstBuffer* buff);
7269

7370
// Getters
74-
std::string get_name() const;
7571
int get_width_px() const;
7672
int get_height_px() const;
7773
int get_frame_rate() const;

0 commit comments

Comments
 (0)