Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add call to gst_ctf_close and ref counting #110

Merged
merged 2 commits into from
Oct 17, 2022

Conversation

dwalkes
Copy link
Contributor

@dwalkes dwalkes commented Jun 10, 2022

To avoid issues with empty files on exit of the pipeline, see
[1].

Note: This code only works if the pipleine gracefully exits,
including calling gst_deinit(). This probably means you
need to add a signal handler for SIGINT if your pipeline
doesn't exit normally.

1: #92
Signed-off-by: Dan Walkes [email protected]

@dwalkes
Copy link
Contributor Author

dwalkes commented Jun 10, 2022

Thanks for sharing this useful tool!

This change works for me to resolve #92 as tested with a modified version of the deepstream-test1 sample application on tegra.

One requirement is to ensure your pipeline exits gracefully and calls gst_deinit(). Here's an example patch for deepstream-test1 application which includes this.

Author: Dan Walkes <[email protected]>                                                                                      
Date:   Thu Jun 9 23:06:00 2022 -0600                                                                                                 
                                                                                                                                      
    Add signal handler for sigint                                                                                                     
                                                                                                                                      
    This is a requirement to play nicely with gst-shark                                                                               
                                                                                                                                      
diff --git a/apps/sample_apps/deepstream-test1/deepstream_test1_app.c b/apps/sample_apps/deepstream-test1/deepstream_test1_app.c      
index 150b58c..2087367 100644                                                                                                         
--- a/apps/sample_apps/deepstream-test1/deepstream_test1_app.c                                                                        
+++ b/apps/sample_apps/deepstream-test1/deepstream_test1_app.c                                                                        
@@ -27,6 +27,7 @@ gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,format=YUY2,width=1280,h                                    
 #include <glib.h>                                                                                                                    
 #include <stdio.h>                                                                                                                   
 #include "gstnvdsmeta.h"                                                                                                             
+#include <glib-unix.h>                                                                                                               
                                                                                                                                      
 #define MAX_DISPLAY_LEN 64                                                                                                           
                                                                                                                                      
@@ -142,12 +143,36 @@ bus_call (GstBus * bus, GstMessage * msg, gpointer data)                                                        
       g_main_loop_quit (loop);                                                                                                       
       break;                                                                                                                         
     }                                                                                                                                
+    case GST_MESSAGE_APPLICATION:{                                                                                                   
+      const GstStructure *s;                                                                                                         
+      s = gst_message_get_structure (msg);                                                                                           
+      if (gst_structure_has_name (s, "GstLaunchInterrupt")) {                                                                        
+        /* this application message is posted when we caught an interrupt and                                                        
+          * we need to stop the pipeline. */                                                                                         
+        g_print ("Interrupt: Stopping pipeline ...\n");                                                                              
+        g_main_loop_quit (loop);                                                                                                     
+      }                                                                                                                              
+      break;                                                                                                                         
+    }                                                                                                                                
     default:                                                                                                                         
       break;                                                                                                                         
   }                                                                                                                                  
   return TRUE;                                                                                                                       
 }                                                                                                                                    
                                                                                                                                      
+static gboolean                                                                                                                      
+intr_handler (gpointer user_data)                                                                                                    
+{                                                                                                                                    
+  GstElement *pipeline = (GstElement *) user_data;                                                                                   
+  g_print ("handling interrupt.\n");                                                                                                 
+  gst_element_post_message (GST_ELEMENT (pipeline),                                                                                  
+       gst_message_new_application (GST_OBJECT (pipeline),                                                                           
+        gst_structure_new ("GstLaunchInterrupt",                                                                                     
+            "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));                                                               
+  return G_SOURCE_REMOVE;                                                                                                            
+}                                                                                                                                    
+                                                                                                                                     
+                                                                                                                                     
 int                                                                                                                                  
 main (int argc, char *argv[])                                                                                                        
 {                                                                                                                                    
@@ -172,6 +197,7 @@ main (int argc, char *argv[])
   /* Create gstreamer elements */
   /* Create Pipeline element that will form a connection of other elements */
   pipeline = gst_pipeline_new ("dstest1-pipeline");
+  g_unix_signal_add (SIGINT, (GSourceFunc) intr_handler, pipeline);

   /* Source element for reading from the file */
   source = gst_element_factory_make ("v4l2src", "v4l2-source");
@@ -301,5 +327,6 @@ main (int argc, char *argv[])
   gst_object_unref (GST_OBJECT (pipeline));
   g_source_remove (bus_watch_id);
   g_main_loop_unref (loop);
+  gst_deinit();
   return 0;
 }

@michaelgruner michaelgruner changed the base branch from master to develop June 10, 2022 16:16
plugins/tracers/gstsharktracer.c Outdated Show resolved Hide resolved
plugins/tracers/gstsharktracer.c Outdated Show resolved Hide resolved
plugins/tracers/gstsharktracer.c Outdated Show resolved Hide resolved
plugins/tracers/gstsharktracer.c Outdated Show resolved Hide resolved
@michaelgruner
Copy link
Collaborator

Thanks for the suggestion, I think it is a clever alternative to depending on gst_deinit

To avoid issues with empty files on exit of the pipeline, see
[1].

Note: This code only works if the pipleine gracefully exits,
including calling `gst_deinit()`.  This probably means you
need to add a signal handler for SIGINT if your pipeline
doesn't exit normally.

1: RidgeRun#92
Signed-off-by: Dan Walkes <[email protected]>
@dwalkes dwalkes force-pushed the fix-corrupt-output-files branch from 7a2a8e2 to 6bb5a03 Compare June 10, 2022 16:38
@dwalkes dwalkes requested a review from michaelgruner June 16, 2022 16:44
@jsalas98 jsalas98 merged commit 11e6f12 into RidgeRun:develop Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants