configure.ac: reorganize clean up document more remove cruft
[platform/upstream/gstreamer.git] / examples / helloworld / helloworld.c
1 #include <stdlib.h>
2 #include <gst/gst.h>
3
4 static void
5 event_loop (GstElement * pipe)
6 {
7   GstBus *bus;
8   GstMessage *message = NULL;
9
10   bus = gst_element_get_bus (GST_ELEMENT (pipe));
11
12   while (TRUE) {
13     message = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
14
15     g_assert (message != NULL);
16
17     switch (message->type) {
18       case GST_MESSAGE_EOS:
19         gst_message_unref (message);
20         return;
21       case GST_MESSAGE_WARNING:
22       case GST_MESSAGE_ERROR:{
23         GError *gerror;
24         gchar *debug;
25
26         gst_message_parse_error (message, &gerror, &debug);
27         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
28         gst_message_unref (message);
29         g_error_free (gerror);
30         g_free (debug);
31         return;
32       }
33       default:
34         gst_message_unref (message);
35         break;
36     }
37   }
38 }
39
40 int
41 main (int argc, char *argv[])
42 {
43   GstElement *bin, *filesrc, *decoder, *audiosink;
44
45   gst_init (&argc, &argv);
46
47   if (argc != 2) {
48     g_print ("usage: %s <mp3 file>\n", argv[0]);
49     exit (-1);
50   }
51
52   /* create a new bin to hold the elements */
53   bin = gst_pipeline_new ("pipeline");
54   g_assert (bin);
55
56   /* create a disk reader */
57   filesrc = gst_element_factory_make ("filesrc", "disk_source");
58   g_assert (filesrc);
59   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
60
61   /* now it's time to get the decoder */
62   decoder = gst_element_factory_make ("mad", "decode");
63   if (!decoder) {
64     g_print ("could not find plugin \"mad\"");
65     return -1;
66   }
67   /* and an audio sink */
68   audiosink = gst_element_factory_make ("alsasink", "play_audio");
69   g_assert (audiosink);
70
71   /* add objects to the main pipeline */
72   gst_bin_add_many (GST_BIN (bin), filesrc, decoder, audiosink, NULL);
73
74   /* link the elements */
75   gst_element_link_many (filesrc, decoder, audiosink, NULL);
76
77   /* start playing */
78   gst_element_set_state (bin, GST_STATE_PLAYING);
79
80   /* Run event loop listening for bus messages until EOS or ERROR */
81   event_loop (bin);
82
83   /* stop the bin */
84   gst_element_set_state (bin, GST_STATE_NULL);
85
86   exit (0);
87 }