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;
}