Creating a C++ GStreamer project in Eclipse - MDHSRobotics/TeamWiki GitHub Wiki

Home / Tutorials

#Creating a C++ GStreamer project in Eclipse

This page lists out the steps necessary to set up your Eclipse environment for gstreamer c++ applications.

##1. Create a c++ Project

Create a C++ project in Eclipse using the standard approach. Set your active configuration to Release

##2. Add required include paths in your project's c++ build settings

The command pkg-config --cflags gstreamer-1.0 gives you the required dependencies and the order they need to be added in. The command returns the following.

-pthread -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include 

Add the following include folders in the Includes section of your C++ compiler build settings

  • /usr/include/gstreamer-1.0
  • /usr/include/glib-2.0
  • /usr/lib/x86_64-linux-gnu/glib-2.0/include

Add -pthread as another optimization flag in the Optimization section of your C++ compiler build settings

##3. Add links to required libraries in your project's c++ build settings

The command pkg-config --libs gstreamer-1.0 gives you the required dependencies and the order they need to be added in. The command returns the following.

-lgstreamer-1.0 -lgobject-2.0 -lglib-2.0 

Add the following libraries in the Libraries section of your C++ linker build settings

  • gstreamer-1.0
  • gobject-2.0
  • glib-2.0

##4. Test your set up Cut and paste the following code in your c++ program to test your set up. It should compile and run and will display a video test signal in a video player.

//============================================================================
// Name        : GStreamerApplication.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <gst/gst.h>

int main(int argc, char *argv[]) {
  GstElement *pipeline, *source, *sink;
  GstBus *bus;
  GstMessage *msg;
  GstStateChangeReturn ret;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  /* Create the elements */
  source = gst_element_factory_make ("videotestsrc", "source");
  sink = gst_element_factory_make ("autovideosink", "sink");

  /* Create the empty pipeline */
  pipeline = gst_pipeline_new ("test-pipeline");

  if (!pipeline || !source || !sink) {
    g_printerr ("Not all elements could be created.\n");
    return -1;
  }

  /* Build the pipeline */
  gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
  if (gst_element_link (source, sink) != TRUE) {
    g_printerr ("Elements could not be linked.\n");
    gst_object_unref (pipeline);
    return -1;
  }

  /* Modify the source's properties */
  g_object_set (source, "pattern", 0, NULL);

  /* Start playing */
  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    g_printerr ("Unable to set the pipeline to the playing state.\n");
    gst_object_unref (pipeline);
    return -1;
  }

  /* Wait until error or EOS */
  bus = gst_element_get_bus (pipeline);
  msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));

  /* Parse message */
  if (msg != NULL) {
    GError *err;
    gchar *debug_info;

    switch (GST_MESSAGE_TYPE (msg)) {
      case GST_MESSAGE_ERROR:
        gst_message_parse_error (msg, &err, &debug_info);
        g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
        g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
        g_clear_error (&err);
        g_free (debug_info);
        break;
      case GST_MESSAGE_EOS:
        g_print ("End-Of-Stream reached.\n");
        break;
      default:
        /* We should not reach here because we only asked for ERRORs and EOS */
        g_printerr ("Unexpected message received.\n");
        break;
    }
    gst_message_unref (msg);
  }

  /* Free resources */
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return 0;
}

⚠️ **GitHub.com Fallback** ⚠️