Skip to content

Instantly share code, notes, and snippets.

@yashi
Last active May 10, 2021 02:25
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashi/276365488873665d81a3f1230afa2d6f to your computer and use it in GitHub Desktop.
Save yashi/276365488873665d81a3f1230afa2d6f to your computer and use it in GitHub Desktop.
An Example for GStreamer Dynamic Pad (Decodebin)
#include <gst/gst.h>
static gboolean bus_call (G_GNUC_UNUSED GstBus *bus, GstMessage *msg, gpointer data)
{
GMainLoop *loop = (GMainLoop *) data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error (msg, &error, &debug);
g_free (debug);
g_printerr ("Error: %s\n", error->message);
g_error_free (error);
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}
static void cb_new_pad (GstElement *element, GstPad *pad, gpointer data)
{
gchar *name;
GstElement *other = data;
name = gst_pad_get_name (pad);
g_print ("A new pad %s was created for %s\n", name, gst_element_get_name(element));
g_free (name);
g_print ("element %s will be linked to %s\n",
gst_element_get_name(element),
gst_element_get_name(other));
gst_element_link(element, other);
}
gint main (gint argc, gchar *argv[])
{
GMainLoop *loop;
GstElement *pipeline, *src, *dec, *conv, *sink;
GstBus *bus;
/* init GStreamer */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* setup */
pipeline = gst_pipeline_new ("pipeline");
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
src = gst_element_factory_make ("tcpserversrc", "src");
g_object_set (G_OBJECT (src), "host", "127.0.0.1",NULL);
g_object_set (G_OBJECT (src), "port", 5000 ,NULL);
dec = gst_element_factory_make ("decodebin", "dec");
conv = gst_element_factory_make ("audioconvert", "conv");
sink = gst_element_factory_make ("alsasink", "sink");
gst_bin_add_many (GST_BIN (pipeline), src, dec, conv, sink, NULL);
gst_element_link (src, dec);
gst_element_link (conv, sink);
/* you don't link them here */
/* gst_element_link (dec, conv); */
/* add call-back, instead */
g_signal_connect (dec, "pad-added", G_CALLBACK (cb_new_pad), conv);
/* run */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
/* cleanup */
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}
@Bhoomil-Chavda-Einfochips

cb_new_pad function have memory leaks

@yashi
Copy link
Author

yashi commented Aug 8, 2020

Wow, thanks. gst_element_get_name()s in cb_new_pad() are "transfer_full". I'll fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment