Advertisement
aircampro

gstreamer example

Jan 31st, 2023 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | Source Code | 0 0
  1. /*
  2.    GStreamer example
  3.    Compiles with : gcc `pkg-config --cflags --libs gstreamer-0.10` -o gst_example gst_example.c
  4.    
  5.    Detailed explanation and example footprint was shown here...
  6.    http://ishikawa-vision.org/~kyamagu/cgi-bin/moin.cgi/VideoTexture/
  7.  
  8. */
  9. #include <gst/gst.h>
  10. #include <glib.h>
  11.  
  12. // if you require uncomment
  13. //#define USB_CAM
  14. //#define IEEE1394
  15.  
  16. /* callback prototype */
  17. static gbooleanbus_handler(GstBus  *bus, GstMessage *msg, gpointer data);
  18.  
  19. /* main */
  20. int main(int argc, char *argv[])
  21. {
  22.   GMainLoop *loop;
  23.   GstElement *pipeline, *source, *sink;
  24.   GstBus *bus;
  25.  
  26.   /** (1) Initialisation **/
  27.   gst_init(&argc, &argv);
  28.  
  29.   /** (2) Objects instantiation **/
  30.   loop = g_main_loop_new(NULL, FALSE);
  31.  
  32.   /* Create gstreamer elements */
  33.   pipeline = gst_pipeline_new("testpipeline");
  34. #if defined(USB_CAM)
  35.   //g_object_set(v4l2sink, "device", "/dev/video1", NULL);
  36.   source   = gst_element_factory_make("v4l2src", "videosrc");        /* use usb cam */
  37. #elif defined(IEEE1394)
  38.   source   = gst_element_factory_make("dc1394src", "videosrc");     /* use IEEE1394 cam */
  39. #else
  40.   source   = gst_element_factory_make("videotestsrc", "videosrc");
  41. #endif
  42.   sink     = gst_element_factory_make("fakesink", "fakesink");
  43.   if (!pipeline || !source || !sink) {
  44.     g_printerr("One element could not be created. Exiting.\n");
  45.     return -1;
  46.   }
  47.  
  48.   /** (3) Set up the pipeline **/
  49.   /* we set the sync property of fakesink to TRUE */
  50.   g_object_set(G_OBJECT (sink), "sync", TRUE, NULL);
  51.  
  52.   /* we add a message handler */
  53.   bus = gst_pipeline_get_bus(GST_PIPELINE (pipeline));
  54.   gst_bus_add_watch(bus, bus_handler, loop);
  55.   gst_object_unref(bus);
  56.  
  57.   /* we add all elements into the pipeline */
  58.   gst_bin_add_many(GST_BIN (pipeline), source, sink, NULL);
  59.  
  60.   /* we link the elements together */
  61.   gst_element_link(source, sink);
  62.  
  63.   /** (4) Main loop **/
  64.   /* Set the pipeline to "playing" state */
  65.   gst_element_set_state(pipeline, GST_STATE_PLAYING);
  66.  
  67.   g_main_loop_run(loop);
  68.  
  69.   /* Out of the main loop, clean up nicely */
  70.   // delete the memory allocated to the main loop
  71.   g_main_loop_unref(loop);
  72.   // When you don't need the element anymore, you need to unref it using gst_object_unref ().
  73.   // This decreases the reference count for the element by 1. An element has a refcount of 1 when
  74.   // it gets created. An element gets destroyed completely when the refcount is decreased to 0.
  75.   // ref :- https://www.cnblogs.com/fellow1988/p/12892254.html
  76.   //
  77.   gst_object_unref(GST_OBJECT (source));
  78.   gst_object_unref(GST_OBJECT (sink));
  79.   // same for the pipeline memory
  80.   gst_element_set_state(pipeline, GST_STATE_NULL);
  81.   gst_object_unref(GST_OBJECT (pipeline));
  82.   gst_deinit();
  83.  
  84.   return 0;
  85. }
  86.  
  87. /* callback implementation */
  88. static gboolean bus_handler(GstBus  *bus, GstMessage *msg, gpointer data)
  89. {
  90.   GMainLoop *loop = (GMainLoop *) data;
  91.  
  92.   switch (GST_MESSAGE_TYPE (msg)) {
  93.     case GST_MESSAGE_ERROR: {
  94.       gchar  *debug;
  95.       GError *error;
  96.  
  97.       gst_message_parse_error(msg, &error, &debug);
  98.       g_free (debug);
  99.       g_printerr("Error: %s\n", error->message);
  100.       g_error_free (error);
  101.  
  102.       g_main_loop_quit(loop);
  103.       break;
  104.     }
  105.     default:
  106.       break;
  107.   }
  108.  
  109.   return TRUE;
  110. }
Tags: gstreamer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement