3 /* Structure to contain all our information, so we can pass it to callbacks */
4 typedef struct _CustomData {
11 /* Handler for the pad-added signal */
12 static void pad_added_handler (GstElement *src, GstPad *pad, CustomData *data);
14 int main(int argc, char *argv[]) {
18 GstStateChangeReturn ret;
19 gboolean terminate = FALSE;
21 /* Initialize GStreamer */
22 gst_init (&argc, &argv);
24 /* Create the elements */
25 data.source = gst_element_factory_make ("uridecodebin", "source");
26 data.convert = gst_element_factory_make ("audioconvert", "convert");
27 data.sink = gst_element_factory_make ("autoaudiosink", "sink");
29 /* Create the empty pipeline */
30 data.pipeline = gst_pipeline_new ("test-pipeline");
32 if (!data.pipeline || !data.source || !data.convert || !data.sink) {
33 g_printerr ("Not all elements could be created.\n");
37 /* Build the pipeline. Note that we are NOT linking the source at this
38 * point. We will do it later. */
39 gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.convert , data.sink, NULL);
40 if (!gst_element_link (data.convert, data.sink)) {
41 g_printerr ("Elements could not be linked.\n");
42 gst_object_unref (data.pipeline);
46 /* Set the URI to play */
47 g_object_set (data.source, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
49 /* Connect to the pad-added signal */
50 g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);
53 ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
54 if (ret == GST_STATE_CHANGE_FAILURE) {
55 g_printerr ("Unable to set the pipeline to the playing state.\n");
56 gst_object_unref (data.pipeline);
60 /* Listen to the bus */
61 bus = gst_element_get_bus (data.pipeline);
63 msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
64 GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
71 switch (GST_MESSAGE_TYPE (msg)) {
72 case GST_MESSAGE_ERROR:
73 gst_message_parse_error (msg, &err, &debug_info);
74 g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
75 g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
81 g_print ("End-Of-Stream reached.\n");
84 case GST_MESSAGE_STATE_CHANGED:
85 /* We are only interested in state-changed messages from the pipeline */
86 if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
87 GstState old_state, new_state, pending_state;
88 gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
89 g_print ("Pipeline state changed from %s to %s:\n",
90 gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));
94 /* We should not reach here */
95 g_printerr ("Unexpected message received.\n");
98 gst_message_unref (msg);
100 } while (!terminate);
103 gst_object_unref (bus);
104 gst_element_set_state (data.pipeline, GST_STATE_NULL);
105 gst_object_unref (data.pipeline);
109 /* This function will be called by the pad-added signal */
110 static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) {
111 GstPad *sink_pad = gst_element_get_static_pad (data->convert, "sink");
112 GstPadLinkReturn ret;
113 GstCaps *new_pad_caps = NULL;
114 GstStructure *new_pad_struct = NULL;
115 const gchar *new_pad_type = NULL;
117 g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
119 /* If our converter is already linked, we have nothing to do here */
120 if (gst_pad_is_linked (sink_pad)) {
121 g_print (" We are already linked. Ignoring.\n");
125 /* Check the new pad's type */
126 new_pad_caps = gst_pad_get_current_caps (new_pad);
127 new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
128 new_pad_type = gst_structure_get_name (new_pad_struct);
129 if (!g_str_has_prefix (new_pad_type, "audio/x-raw")) {
130 g_print (" It has type '%s' which is not raw audio. Ignoring.\n", new_pad_type);
134 /* Attempt the link */
135 ret = gst_pad_link (new_pad, sink_pad);
136 if (GST_PAD_LINK_FAILED (ret)) {
137 g_print (" Type is '%s' but link failed.\n", new_pad_type);
139 g_print (" Link succeeded (type '%s').\n", new_pad_type);
143 /* Unreference the new pad's caps, if we got them */
144 if (new_pad_caps != NULL)
145 gst_caps_unref (new_pad_caps);
147 /* Unreference the sink pad */
148 gst_object_unref (sink_pad);