gst-plugins-base: update translations
[platform/upstream/gstreamer.git] / subprojects / gst-docs / examples / bus_example.c
1 #include <gst/gst.h>
2
3 static GMainLoop *loop;
4
5 static gboolean
6 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
7 {
8   g_print ("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));
9
10   switch (GST_MESSAGE_TYPE (message)) {
11     case GST_MESSAGE_ERROR:{
12       GError *err;
13       gchar *debug;
14
15       gst_message_parse_error (message, &err, &debug);
16       g_print ("Error: %s\n", err->message);
17       g_error_free (err);
18       g_free (debug);
19
20       g_main_loop_quit (loop);
21       break;
22     }
23     case GST_MESSAGE_EOS:
24       /* end-of-stream */
25       g_main_loop_quit (loop);
26       break;
27     default:
28       /* unhandled message */
29       break;
30   }
31
32   /* we want to be notified again the next time there is a message
33    * on the bus, so returning TRUE (FALSE means we want to stop watching
34    * for messages on the bus and our callback should not be called again)
35    */
36   return TRUE;
37 }
38
39 gint
40 main (gint argc, gchar * argv[])
41 {
42   GstElement *pipeline;
43   GstBus *bus;
44   guint bus_watch_id;
45
46   /* init */
47   gst_init (&argc, &argv);
48
49   /* create pipeline, add handler */
50   pipeline = gst_pipeline_new ("my_pipeline");
51
52   /* adds a watch for new message on our pipeline's message bus to
53    * the default GLib main context, which is the main context that our
54    * GLib main loop is attached to below
55    */
56   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
57   bus_watch_id = gst_bus_add_watch (bus, my_bus_callback, NULL);
58   gst_object_unref (bus);
59
60   /* [...] */
61
62   /* create a mainloop that runs/iterates the default GLib main context
63    * (context NULL), in other words: makes the context check if anything
64    * it watches for has happened. When a message has been posted on the
65    * bus, the default main context will automatically call our
66    * my_bus_callback() function to notify us of that message.
67    * The main loop will be run until someone calls g_main_loop_quit()
68    */
69   loop = g_main_loop_new (NULL, FALSE);
70   g_main_loop_run (loop);
71
72   /* clean up */
73   gst_element_set_state (pipeline, GST_STATE_NULL);
74   gst_object_unref (pipeline);
75   g_source_remove (bus_watch_id);
76   g_main_loop_unref (loop);
77
78   return 0;
79 }