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