Link error implementing CAPS in pipeline in C

I’m trying to replicate a gst-launch-1.0 pipeline in a c program without success.
Environment: Jetson AGX ORIN developer kit using a Logitech HD Pro Webcam C920 Camera

gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=100 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! nvvidconv ! nvegltransform ! nveglglessink

This works without issue. Frame rate is good.

In my c program I used 2 functions to set caps:
gst_caps_new_simple to define the capabilities I need, and the
gst_element_link_filtered to link the caps to the source element and the conversion element.

I created a flag (cap_mode) to turn the caps code off and on to see how the code reacts.

When the caps code is off, it executes like the gst-launch-1.0 pipeline if I had removed the
“! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1”. This reacts as I expected.

When I have the caps code on, I get my manually built error message:
“Fail to link filtered videosrc - caps - vidconv”

Anyone have an idea how to code this in C correctly? Thanks!

This is my code:

#include <gst/gst.h>

int main (int argc, char *argv)
{
int cap_mode = 0; // 1 - USE CAPS, 0 - NO CAPS

GstElement *pipeline, *videosrc, *vidconv, *transform, *sink;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;

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

/* Create the elements */
videosrc = gst_element_factory_make (“v4l2src”, “videosrc”);
vidconv = gst_element_factory_make (“nvvidconv”, “vidconv”);
transform = gst_element_factory_make (“nvegltransform”, “transform”);
sink = gst_element_factory_make (“nveglglessink”, “sink”);

/* Modify the videosrc’s properties */
g_object_set (G_OBJECT (videosrc), “device”, “/dev/video0”, “num-buffers”, 100, NULL);

// -------------------------------------------------- SET capabilities -----------------------------------------
if(cap_mode)
{
GstCaps *caps = gst_caps_new_simple
(
“video/x-raw”,
“format”, G_TYPE_STRING, “YUY2”,
“width”, G_TYPE_INT, 640,
“height”, G_TYPE_INT, 480,
“framerate”, GST_TYPE_FRACTION, 30, 1,
NULL
);

if (!gst_element_link_filtered(videosrc, vidconv, caps)) 
{
    g_printerr("Fail to link filtered videosrc - caps - vidconv\n");
    return -1;
}

}
// --------------------------------------------------END SET capabilities -----------------------------------------

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

if (!pipeline || !videosrc || !vidconv || !transform || !sink)
{
g_printerr (“Not all elements could be created.\n”);
return -1;
}

/* Build the pipeline */
gst_bin_add_many (GST_BIN (pipeline), videosrc, vidconv, transform, sink, NULL);

if (gst_element_link_many (videosrc, vidconv, transform, sink, NULL) != TRUE)
{
g_printerr (“Elements could not be linked.\n”);
gst_object_unref (pipeline);
return -1;
}

/* 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, 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;
}

Hi,
Generally we call gst_parse_launch() to initialize the pipeline. There are C samples in the posts:
Camera liveview cannot show up again after repeated open/close the liveview for several times - #23 by DaneLLL
Getting Error for nvv4l2h264enc in application compilation - #7 by DaneLLL
GStreamer freeze when using qtmux and NVIDIA-accelerated h264/h265 encoding - #7 by DaneLLL

Please check the samples and try gst_parse_launch()

I was able to convert my C program in-line with your C++ examples.

Camera Video displays in a window on my screen. The only issue I have now is I get an GST_MESSAGE_EOS after about 4 seconds. I will research, I may need a loop, I guess? Anyone know why I’d appreciate a hint/clue. Continued here with new post.

I did see in a stackoverflow discussion where a person indicated, using gst_parse_launch works on static pipelines. However, if the pipeline needs to be dynamic, such with mux elements, pads are required, and a different approach is required. So, maybe my original issue should be investigated. I use only static pipelines so I will stick with gst_parse_launch and go this route for now.

This is my code:

#include <gst/gst.h>

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

  const gchar *pipeline_description;
  GError *error = NULL;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);
  
  /* Create the pipeline */
  pipeline_description = "v4l2src device=/dev/video0 num-buffers=100 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! nvvidconv ! nvegltransform ! nveglglessink";

  gst_pipeline = gst_parse_launch(pipeline_description, &error);

  if(!gst_pipeline){
    g_print("Failed to parse launch: %s\n", error->message);
    return -1;
  }
  if(error) g_error_free(error);
  
  /* Start playing */
  ret = gst_element_set_state (gst_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 (gst_pipeline);
    return -1;
  }

  /* Wait until error or EOS */
  bus = gst_element_get_bus (gst_pipeline);
  msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, 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 (gst_pipeline, GST_STATE_NULL);
  gst_object_unref (gst_pipeline);
  return 0;
}

@DaneLLL Thanks for your assistants. You may close now.

I was able to find information about my initial issue using a “caps” property on a v4l2src element. v4l2src does not have a “caps” property so one must use a “filter” element to set these capabilities manually. Please note, I also show how to display video in a GTK3 window. The “sink” element/object was needed to set in the gst_video_overlay_set_window_handle.

Once last bit of knowledge I just discovered. One can use a gst_element_factory_make to get to the nveglglessink sink object even though the method of using a gst_parse_launch function is employed:
sink = gst_element_factory_make (“nveglglessink”, NULL); g_assert(sink);
Hope this helps you.

My code:

#include <gst/gst.h>
#include <gst/video/videooverlay.h>
#include <gtk/gtkx.h>
#include <glib.h>


int main (int argc, char **argv)
{
  GdkWindow *video_window_xwindow;
  GtkWidget *window, *video_window;
  gulong embed_xid;
  GstStateChangeReturn sret;

  gst_init (&argc, &argv);
  gtk_init (&argc, &argv);

  GstElement *pipeline,*videosrc,*filter,*vidconv,*transform,*sink;

  pipeline = gst_pipeline_new ("xvoverlay");
  videosrc = gst_element_factory_make ("v4l2src", NULL); g_assert(videosrc);
  filter   = gst_element_factory_make ("capsfilter",NULL); g_assert(filter);

  //Set CAPS
  g_object_set (G_OBJECT (videosrc), "device", "/dev/video0", NULL);
  GstCaps * vidxcaps = gst_caps_from_string("video/x-raw,format=YUY2,width=640,height=480,framerate=30/1");
  g_object_set (filter, "caps", vidxcaps, NULL);

  /* Create remaining elements */
  
  vidconv = gst_element_factory_make ("nvvidconv", NULL); g_assert(vidconv);
  transform = gst_element_factory_make ("nvegltransform", NULL); g_assert(transform);
  sink = gst_element_factory_make ("nveglglessink", NULL); g_assert(sink);

  //ADD
  gst_bin_add_many (GST_BIN (pipeline), videosrc, filter, vidconv, transform, sink, NULL);

  //LINK
  g_assert(gst_element_link (videosrc, filter));
  g_assert(gst_element_link (filter, vidconv));   // <<<<<< Filter placed between 
  g_assert(gst_element_link (vidconv, transform));
  g_assert(gst_element_link (transform, sink));
 
  /* prepare the ui */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);   

  g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (gtk_window_close), (gpointer) pipeline);
  gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
  gtk_window_set_title (GTK_WINDOW (window), "GstVideoOverlay Gtk+ demo");

  video_window = gtk_drawing_area_new ();
  gtk_container_add (GTK_CONTAINER (window), video_window);
  gtk_container_set_border_width (GTK_CONTAINER (window), 2);

  gtk_widget_show_all (window);

  video_window_xwindow = gtk_widget_get_window (video_window);
  embed_xid = GDK_WINDOW_XID (video_window_xwindow);
  gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), embed_xid);

  /* run the pipeline */
  sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  if (sret == GST_STATE_CHANGE_FAILURE)
    gst_element_set_state (pipeline, GST_STATE_NULL);
  else
    gtk_main ();

  gst_object_unref (pipeline);
  return 0;
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.