Update theme submodule
[platform/upstream/gstreamer.git] / tutorials / basic-tutorial-12.c
1 #include <gst/gst.h>\r
2 #include <string.h>\r
3   \r
4 typedef struct _CustomData {\r
5   gboolean is_live;\r
6   GstElement *pipeline;\r
7   GMainLoop *loop;\r
8 } CustomData;\r
9   \r
10 static void cb_message (GstBus *bus, GstMessage *msg, CustomData *data) {\r
11   \r
12   switch (GST_MESSAGE_TYPE (msg)) {\r
13     case GST_MESSAGE_ERROR: {\r
14       GError *err;\r
15       gchar *debug;\r
16       \r
17       gst_message_parse_error (msg, &err, &debug);\r
18       g_print ("Error: %s\n", err->message);\r
19       g_error_free (err);\r
20       g_free (debug);\r
21       \r
22       gst_element_set_state (data->pipeline, GST_STATE_READY);\r
23       g_main_loop_quit (data->loop);\r
24       break;\r
25     }\r
26     case GST_MESSAGE_EOS:\r
27       /* end-of-stream */\r
28       gst_element_set_state (data->pipeline, GST_STATE_READY);\r
29       g_main_loop_quit (data->loop);\r
30       break;\r
31     case GST_MESSAGE_BUFFERING: {\r
32       gint percent = 0;\r
33       \r
34       /* If the stream is live, we do not care about buffering. */\r
35       if (data->is_live) break;\r
36       \r
37       gst_message_parse_buffering (msg, &percent);\r
38       g_print ("Buffering (%3d%%)\r", percent);\r
39       /* Wait until buffering is complete before start/resume playing */\r
40       if (percent < 100)\r
41         gst_element_set_state (data->pipeline, GST_STATE_PAUSED);\r
42       else\r
43         gst_element_set_state (data->pipeline, GST_STATE_PLAYING);\r
44       break;\r
45     }\r
46     case GST_MESSAGE_CLOCK_LOST:\r
47       /* Get a new clock */\r
48       gst_element_set_state (data->pipeline, GST_STATE_PAUSED);\r
49       gst_element_set_state (data->pipeline, GST_STATE_PLAYING);\r
50       break;\r
51     default:\r
52       /* Unhandled message */\r
53       break;\r
54     }\r
55 }\r
56   \r
57 int main(int argc, char *argv[]) {\r
58   GstElement *pipeline;\r
59   GstBus *bus;\r
60   GstStateChangeReturn ret;\r
61   GMainLoop *main_loop;\r
62   CustomData data;\r
63   \r
64   /* Initialize GStreamer */\r
65   gst_init (&argc, &argv);\r
66   \r
67   /* Initialize our data structure */\r
68   memset (&data, 0, sizeof (data));\r
69   \r
70   /* Build the pipeline */\r
71   pipeline = gst_parse_launch ("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);\r
72   bus = gst_element_get_bus (pipeline);\r
73   \r
74   /* Start playing */\r
75   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);\r
76   if (ret == GST_STATE_CHANGE_FAILURE) {\r
77     g_printerr ("Unable to set the pipeline to the playing state.\n");\r
78     gst_object_unref (pipeline);\r
79     return -1;\r
80   } else if (ret == GST_STATE_CHANGE_NO_PREROLL) {\r
81     data.is_live = TRUE;\r
82   }\r
83   \r
84   main_loop = g_main_loop_new (NULL, FALSE);\r
85   data.loop = main_loop;\r
86   data.pipeline = pipeline;\r
87   \r
88   gst_bus_add_signal_watch (bus);\r
89   g_signal_connect (bus, "message", G_CALLBACK (cb_message), &data);\r
90   \r
91   g_main_loop_run (main_loop);\r
92   \r
93   /* Free resources */\r
94   g_main_loop_unref (main_loop);\r
95   gst_object_unref (bus);\r
96   gst_element_set_state (pipeline, GST_STATE_NULL);\r
97   gst_object_unref (pipeline);\r
98   return 0;\r
99 }\r