Through this sample code, you can learn:
This sample shows how to generate a simple pipeline equivalent to:
gst-launch-1.0 videotestsrc pattern=18 ! autovideosink
Besides the establishment of the pipeline, this example also illustrate how to handle the 2 return messages: GST_MESSAGE_ERROR and GST_MESSAGE_EOS. More messages return could be found HERE. See the GstMessageType of Enumerations section. Based on the requirement to add the message if needed in advance.
There are seven main steps to form the application of generate-a-basic-pipeline.c:
• Step 1: Initialize GStreamer
• Step 2: Create the elements
• Step 3: Create the empty pipeline and then build it
• Step 4: Modify the source's properties
• Step 5: Start playing the pipeline
• Step 6: Message handling
• Step 7: Free resources
Here, we will take generate-a-basic-pipeline.c for explanation.
In this step, it is required and must to initialize the GStreamer by init for the application.
/* Initialize GStreamer */
gst_init (&argc, &argv);
Create the elements by factory.
/* Create the elements */
source = gst_element_factory_make ("videotestsrc", "source");
sink = gst_element_factory_make ("autovideoconvert", "sink");
Generate a pipe line in order to put all element in it and then playing it later.
/* Create the empty pipeline */
pipeline = gst_pipeline_new ("test-pipeline");
/* Build the pipeline */
gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
This step is optional if you are going to set some properties to each element. In ex_application.c, we required to set properties, pattern, for switch the pattern images we are going to use:
/* Modify the source's properties */
g_object_set (source, "pattern", 18, NULL);
Playing the pipeline we created:
/* Start playing */
ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
This step makes sure the message be thrown by the pipeline we created through the GstBus. This message handling could customize based on different event thrown.
/* 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:
/* Handle other messages, otherwise the default case will called. */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
Finally, release all used GStreamer instances:
/* Free resources */
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
Go to the folder of the binary and run binary in terminal or cmd:
$ ./generate-a-basic-pipeline
and you will see the moving ball windows displayed:
