4 typedef struct _CustomData {
10 static void cb_message (GstBus *bus, GstMessage *msg, CustomData *data) {
12 switch (GST_MESSAGE_TYPE (msg)) {
13 case GST_MESSAGE_ERROR: {
17 gst_message_parse_error (msg, &err, &debug);
18 g_print ("Error: %s\n", err->message);
22 gst_element_set_state (data->pipeline, GST_STATE_READY);
23 g_main_loop_quit (data->loop);
28 gst_element_set_state (data->pipeline, GST_STATE_READY);
29 g_main_loop_quit (data->loop);
31 case GST_MESSAGE_BUFFERING: {
34 /* If the stream is live, we do not care about buffering. */
35 if (data->is_live) break;
37 gst_message_parse_buffering (msg, &percent);
38 g_print ("Buffering (%3d%%)\r", percent);
39 /* Wait until buffering is complete before start/resume playing */
41 gst_element_set_state (data->pipeline, GST_STATE_PAUSED);
43 gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
46 case GST_MESSAGE_CLOCK_LOST:
48 gst_element_set_state (data->pipeline, GST_STATE_PAUSED);
49 gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
52 /* Unhandled message */
57 int main(int argc, char *argv[]) {
60 GstStateChangeReturn ret;
64 /* Initialize GStreamer */
65 gst_init (&argc, &argv);
67 /* Initialize our data structure */
68 memset (&data, 0, sizeof (data));
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);
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);
80 } else if (ret == GST_STATE_CHANGE_NO_PREROLL) {
84 main_loop = g_main_loop_new (NULL, FALSE);
85 data.loop = main_loop;
86 data.pipeline = pipeline;
88 gst_bus_add_signal_watch (bus);
89 g_signal_connect (bus, "message", G_CALLBACK (cb_message), &data);
91 g_main_loop_run (main_loop);
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);