Update theme submodule
[platform/upstream/gstreamer.git] / examples / tutorials / basic-tutorial-2.c
1 #include <gst/gst.h>
2
3 int main(int argc, char *argv[]) {
4   GstElement *pipeline, *source, *sink;
5   GstBus *bus;
6   GstMessage *msg;
7   GstStateChangeReturn ret;
8
9   /* Initialize GStreamer */
10   gst_init (&argc, &argv);
11
12   /* Create the elements */
13   source = gst_element_factory_make ("videotestsrc", "source");
14   sink = gst_element_factory_make ("autovideosink", "sink");
15
16   /* Create the empty pipeline */
17   pipeline = gst_pipeline_new ("test-pipeline");
18
19   if (!pipeline || !source || !sink) {
20     g_printerr ("Not all elements could be created.\n");
21     return -1;
22   }
23
24   /* Build the pipeline */
25   gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
26   if (gst_element_link (source, sink) != TRUE) {
27     g_printerr ("Elements could not be linked.\n");
28     gst_object_unref (pipeline);
29     return -1;
30   }
31
32   /* Modify the source's properties */
33   g_object_set (source, "pattern", 0, NULL);
34
35   /* Start playing */
36   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
37   if (ret == GST_STATE_CHANGE_FAILURE) {
38     g_printerr ("Unable to set the pipeline to the playing state.\n");
39     gst_object_unref (pipeline);
40     return -1;
41   }
42
43   /* Wait until error or EOS */
44   bus = gst_element_get_bus (pipeline);
45   msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
46
47   /* Parse message */
48   if (msg != NULL) {
49     GError *err;
50     gchar *debug_info;
51
52     switch (GST_MESSAGE_TYPE (msg)) {
53       case GST_MESSAGE_ERROR:
54         gst_message_parse_error (msg, &err, &debug_info);
55         g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
56         g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
57         g_clear_error (&err);
58         g_free (debug_info);
59         break;
60       case GST_MESSAGE_EOS:
61         g_print ("End-Of-Stream reached.\n");
62         break;
63       default:
64         /* We should not reach here because we only asked for ERRORs and EOS */
65         g_printerr ("Unexpected message received.\n");
66         break;
67     }
68     gst_message_unref (msg);
69   }
70
71   /* Free resources */
72   gst_object_unref (bus);
73   gst_element_set_state (pipeline, GST_STATE_NULL);
74   gst_object_unref (pipeline);
75   return 0;
76 }