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