1 #include <gst/gst.h>
2 
3 static gboolean
bus_call(GstBus * bus,GstMessage * msg,gpointer data)4 bus_call (GstBus * bus, GstMessage * msg, gpointer data)
5 {
6   GMainLoop *loop = (GMainLoop *) data;
7 
8   switch (GST_MESSAGE_TYPE (msg)) {
9     case GST_MESSAGE_EOS:{
10       g_print ("End-of-stream\n");
11       g_main_loop_quit (loop);
12       break;
13     }
14     case GST_MESSAGE_ERROR:{
15       gchar *debug;
16       GError *err;
17 
18       gst_message_parse_error (msg, &err, &debug);
19       g_printerr ("Debugging info: %s\n", (debug) ? debug : "none");
20       g_free (debug);
21 
22       g_print ("Error: %s\n", err->message);
23       g_error_free (err);
24 
25       g_main_loop_quit (loop);
26 
27       break;
28     }
29     default:
30       break;
31   }
32   return TRUE;
33 }
34 
35 gint
main(gint argc,gchar * argv[])36 main (gint argc, gchar * argv[])
37 {
38   GstElement *playbin;
39   GMainLoop *loop;
40   GstBus *bus;
41   guint bus_watch_id;
42   gchar *uri;
43 
44   gst_init (&argc, &argv);
45 
46   if (argc < 2) {
47     g_print ("usage: %s <media file or uri>\n", argv[0]);
48     return 1;
49   }
50 
51   playbin = gst_element_factory_make ("playbin", NULL);
52   if (!playbin) {
53     g_print ("'playbin' gstreamer plugin missing\n");
54     return 1;
55   }
56 
57   /* take the commandline argument and ensure that it is a uri */
58   if (gst_uri_is_valid (argv[1]))
59     uri = g_strdup (argv[1]);
60   else
61     uri = gst_filename_to_uri (argv[1], NULL);
62   g_object_set (playbin, "uri", uri, NULL);
63   g_free (uri);
64 
65   /* create an event loop and feed gstreamer bus messages to it */
66   loop = g_main_loop_new (NULL, FALSE);
67 
68   bus = gst_element_get_bus (playbin);
69   bus_watch_id = gst_bus_add_watch (bus, bus_call, loop);
70   g_object_unref (bus);
71 
72   /* start play back and listed to events */
73   gst_element_set_state (playbin, GST_STATE_PLAYING);
74   g_main_loop_run (loop);
75 
76   /* cleanup */
77   gst_element_set_state (playbin, GST_STATE_NULL);
78   g_object_unref (playbin);
79   g_source_remove (bus_watch_id);
80   g_main_loop_unref (loop);
81 
82   return 0;
83 }
84