Split out documentation into subfolders.
[platform/upstream/gstreamer.git] / examples / tutorials / basic-tutorial-3.c
1 #include <gst/gst.h>
2
3 /* Structure to contain all our information, so we can pass it to callbacks */
4 typedef struct _CustomData {
5   GstElement *pipeline;
6   GstElement *source;
7   GstElement *convert;
8   GstElement *sink;
9 } CustomData;
10
11 /* Handler for the pad-added signal */
12 static void pad_added_handler (GstElement *src, GstPad *pad, CustomData *data);
13
14 int main(int argc, char *argv[]) {
15   CustomData data;
16   GstBus *bus;
17   GstMessage *msg;
18   GstStateChangeReturn ret;
19   gboolean terminate = FALSE;
20
21   /* Initialize GStreamer */
22   gst_init (&argc, &argv);
23
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");
28
29   /* Create the empty pipeline */
30   data.pipeline = gst_pipeline_new ("test-pipeline");
31
32   if (!data.pipeline || !data.source || !data.convert || !data.sink) {
33     g_printerr ("Not all elements could be created.\n");
34     return -1;
35   }
36
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);
43     return -1;
44   }
45
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);
48
49   /* Connect to the pad-added signal */
50   g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);
51
52   /* Start playing */
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);
57     return -1;
58   }
59
60   /* Listen to the bus */
61   bus = gst_element_get_bus (data.pipeline);
62   do {
63     msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
64         GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
65
66     /* Parse message */
67     if (msg != NULL) {
68       GError *err;
69       gchar *debug_info;
70
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");
76           g_clear_error (&err);
77           g_free (debug_info);
78           terminate = TRUE;
79           break;
80         case GST_MESSAGE_EOS:
81           g_print ("End-Of-Stream reached.\n");
82           terminate = TRUE;
83           break;
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));
91           }
92           break;
93         default:
94           /* We should not reach here */
95           g_printerr ("Unexpected message received.\n");
96           break;
97       }
98       gst_message_unref (msg);
99     }
100   } while (!terminate);
101
102   /* Free resources */
103   gst_object_unref (bus);
104   gst_element_set_state (data.pipeline, GST_STATE_NULL);
105   gst_object_unref (data.pipeline);
106   return 0;
107 }
108
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;
116
117   g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
118
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");
122     goto exit;
123   }
124
125   /* Check the new pad's type */
126   new_pad_caps = gst_pad_query_caps (new_pad, NULL);
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);
131     goto exit;
132   }
133
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);
138   } else {
139     g_print ("  Link succeeded (type '%s').\n", new_pad_type);
140   }
141
142 exit:
143   /* Unreference the new pad's caps, if we got them */
144   if (new_pad_caps != NULL)
145     gst_caps_unref (new_pad_caps);
146
147   /* Unreference the sink pad */
148   gst_object_unref (sink_pad);
149 }