@@ -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() {
259267std::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
305321std::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}
0 commit comments