Skip to content

Instantly share code, notes, and snippets.

@avazquezdev
Created November 4, 2021 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avazquezdev/3c6b393fb25caffdee9a2e052c90ff43 to your computer and use it in GitHub Desktop.
Save avazquezdev/3c6b393fb25caffdee9a2e052c90ff43 to your computer and use it in GitHub Desktop.
fallbacksrc test
/*
* Compile: gcc main.c -o main `pkg-config --cflags --libs gstreamer-1.0
*/
#include <gst/gst.h>
/* Structure to contain all our information, so we can pass it to callbacks */
typedef struct _CustomData
{
GstElement *pipeline;
GstElement *source;
GstElement *depay;
GstElement *capsFilter;
GstElement *parse;
GstElement *dec;
GstElement *convert;
GstElement *sink;
} CustomData;
/* Handler for the pad-added signal */
static void pad_added_handler (GstElement * src, GstPad * pad,
CustomData * data);
int
main (int argc, char *argv[])
{
CustomData data;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
gboolean terminate = FALSE;
GstElement *stream;
GstElement *fallbacksrc;
GstPad *videopad;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Create the elements */
data.source = gst_element_factory_make ("souphttpsrc", "source-video");
data.depay = gst_element_factory_make ("multipartdemux", "depay");
data.parse = gst_element_factory_make ("jpegparse", "parse");
data.capsFilter = gst_element_factory_make ("capsfilter", "filter");
data.dec = gst_element_factory_make ("avdec_mjpeg", "dec");
data.convert = gst_element_factory_make ("videoconvert", "convert");
data.sink = gst_element_factory_make ("autovideosink", "sink");
/*Config source video*/
g_object_set (data.source,"is-live", TRUE, NULL);
g_object_set (data.source,"location", "http://192.168.1.23:8080/?action=stream", NULL);
/*Configure caps*/
GstCaps *caps = gst_caps_from_string("image/jpeg,framerate=0/1");
g_object_set(G_OBJECT(data.capsFilter), "caps", caps, NULL);
gst_caps_unref(caps);
/* Create the empty pipeline */
data.pipeline = gst_pipeline_new ("test-pipeline");
if (!data.pipeline || !data.source || !data.depay ||
!data.parse || !data.capsFilter || !data.dec || !data.convert ||
!data.sink) {
g_printerr ("Not all elements could be created.\n");
return -1;
}
/* Build the pipeline. Note that we are NOT linking the source at this
* point. We will do it later. */
stream = gst_bin_new ("stream");
gst_bin_add_many (GST_BIN (stream), data.source, data.depay, data.capsFilter, data.parse, data.dec, NULL);
gst_element_link_many(data.source, data.depay, NULL);
if (!gst_element_link_many (data.capsFilter, data.parse, data.dec, NULL)) {
g_printerr ("Elements stream could not be linked.\n");
gst_object_unref (data.pipeline);
return -1;
}
/*Configure ghost pad*/
videopad = gst_element_get_static_pad(data.dec, "src");
gst_element_add_pad(stream, gst_ghost_pad_new("src", videopad));
/*Configure fallback*/
fallbacksrc = gst_element_factory_make ("fallbacksrc", "fallback-source");
g_object_set (G_OBJECT (fallbacksrc), "fallback-uri", "file:/home/no-video.jpg", NULL);
g_object_set (G_OBJECT (fallbacksrc), "enable-audio", FALSE, NULL);
g_object_set (G_OBJECT (fallbacksrc), "restart-on-eos", TRUE, NULL);
g_object_set (G_OBJECT (fallbacksrc), "source", G_OBJECT (stream), NULL);
gst_bin_add_many (GST_BIN (data.pipeline), fallbacksrc, data.convert, data.sink, NULL);
if (!gst_element_link_many (data.convert, data.sink, NULL)) {
g_printerr ("Elements could not be linked.\n");
gst_object_unref (data.pipeline);
return -1;
}
/* Connect to the pad-added signal */
g_signal_connect (data.depay, "pad-added", G_CALLBACK (pad_added_handler),
&data);
g_signal_connect (fallbacksrc, "pad-added", G_CALLBACK (pad_added_handler),
&data);
/* Start playing */
ret = gst_element_set_state (data.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 (data.pipeline);
return -1;
}
/* Listen to the bus */
bus = gst_element_get_bus (data.pipeline);
do {
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
GST_MESSAGE_STATE_CHANGED | 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);
terminate = TRUE;
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
terminate = TRUE;
break;
case GST_MESSAGE_STATE_CHANGED:
/* We are only interested in state-changed messages from the pipeline */
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed (msg, &old_state, &new_state,
&pending_state);
g_print ("Pipeline state changed from %s to %s:\n",
gst_element_state_get_name (old_state),
gst_element_state_get_name (new_state));
}
break;
default:
/* We should not reach here */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
} while (!terminate);
/* Free resources */
gst_object_unref (bus);
gst_element_set_state (data.pipeline, GST_STATE_NULL);
gst_object_unref (data.pipeline);
return 0;
}
/* This function will be called by the pad-added signal */
static void
pad_added_handler (GstElement * src, GstPad * new_pad, CustomData * data)
{
GstPad *sink_pad = NULL;
GstPadLinkReturn ret;
GstCaps *new_pad_caps = NULL;
GstStructure *new_pad_struct = NULL;
const gchar *new_pad_type = NULL;
if (strcmp(GST_PAD_NAME (new_pad),"video") == 0)
sink_pad = gst_element_get_static_pad (data->convert, "sink");
else
sink_pad = gst_element_get_static_pad (data->capsFilter, "sink");
g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad),
GST_ELEMENT_NAME (src));
/* If our converter is already linked, we have nothing to do here */
if (gst_pad_is_linked (sink_pad)) {
g_print ("We are already linked. Ignoring.\n");
goto exit;
}
/* Check the new pad's type */
new_pad_caps = gst_pad_get_current_caps (new_pad);
if (new_pad_caps != NULL) {
new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
new_pad_type = gst_structure_get_name (new_pad_struct);
}
else
new_pad_type = GST_PAD_NAME (new_pad);
/* Attempt the link */
ret = gst_pad_link (new_pad, sink_pad);
if (GST_PAD_LINK_FAILED (ret)) {
g_print ("Type is '%s' but link failed.\n", new_pad_type);
} else {
g_print ("Link succeeded (type '%s').\n", new_pad_type);
}
exit:
/* Unreference the new pad's caps, if we got them */
if (new_pad_caps != NULL)
gst_caps_unref (new_pad_caps);
/* Unreference the sink pad */
gst_object_unref (sink_pad);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment