Git init
[framework/multimedia/gstreamer0.10.git] / tests / examples / manual / helloworld.c
1
2 /*** block  from ../../../docs/manual/basics-helloworld.xml ***/
3 #include <gst/gst.h>
4 #include <glib.h>
5
6
7 static gboolean
8 bus_call (GstBus     *bus,
9           GstMessage *msg,
10           gpointer    data)
11 {
12   GMainLoop *loop = (GMainLoop *) data;
13
14   switch (GST_MESSAGE_TYPE (msg)) {
15
16     case GST_MESSAGE_EOS:
17       g_print ("End of stream\n");
18       g_main_loop_quit (loop);
19       break;
20
21     case GST_MESSAGE_ERROR: {
22       gchar  *debug;
23       GError *error;
24
25       gst_message_parse_error (msg, &error, &debug);
26       g_free (debug);
27
28       g_printerr ("Error: %s\n", error->message);
29       g_error_free (error);
30
31       g_main_loop_quit (loop);
32       break;
33     }
34     default:
35       break;
36   }
37
38   return TRUE;
39 }
40
41
42 static void
43 on_pad_added (GstElement *element,
44               GstPad     *pad,
45               gpointer    data)
46 {
47   GstPad *sinkpad;
48   GstElement *decoder = (GstElement *) data;
49
50   /* We can now link this pad with the vorbis-decoder sink pad */
51   g_print ("Dynamic pad created, linking demuxer/decoder\n");
52
53   sinkpad = gst_element_get_static_pad (decoder, "sink");
54
55   gst_pad_link (pad, sinkpad);
56
57   gst_object_unref (sinkpad);
58 }
59
60
61
62 int
63 main (int   argc,
64       char *argv[])
65 {
66   GMainLoop *loop;
67
68   GstElement *pipeline, *source, *demuxer, *decoder, *conv, *sink;
69   GstBus *bus;
70
71   /* Initialisation */
72   gst_init (&argc, &argv);
73
74   loop = g_main_loop_new (NULL, FALSE);
75
76
77   /* Check input arguments */
78   if (argc != 2) {
79     g_printerr ("Usage: %s <Ogg/Vorbis filename>\n", argv[0]);
80     return -1;
81   }
82
83
84   /* Create gstreamer elements */
85   pipeline = gst_pipeline_new ("audio-player");
86   source   = gst_element_factory_make ("filesrc",       "file-source");
87   demuxer  = gst_element_factory_make ("oggdemux",      "ogg-demuxer");
88   decoder  = gst_element_factory_make ("vorbisdec",     "vorbis-decoder");
89   conv     = gst_element_factory_make ("audioconvert",  "converter");
90   sink     = gst_element_factory_make ("autoaudiosink", "audio-output");
91
92   if (!pipeline || !source || !demuxer || !decoder || !conv || !sink) {
93     g_printerr ("One element could not be created. Exiting.\n");
94     return -1;
95   }
96
97   /* Set up the pipeline */
98
99   /* we set the input filename to the source element */
100   g_object_set (G_OBJECT (source), "location", argv[1], NULL);
101
102   /* we add a message handler */
103   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
104   gst_bus_add_watch (bus, bus_call, loop);
105   gst_object_unref (bus);
106
107   /* we add all elements into the pipeline */
108   /* file-source | ogg-demuxer | vorbis-decoder | converter | alsa-output */
109   gst_bin_add_many (GST_BIN (pipeline),
110                     source, demuxer, decoder, conv, sink, NULL);
111
112   /* we link the elements together */
113   /* file-source -> ogg-demuxer ~> vorbis-decoder -> converter -> alsa-output */
114   gst_element_link (source, demuxer);
115   gst_element_link_many (decoder, conv, sink, NULL);
116   g_signal_connect (demuxer, "pad-added", G_CALLBACK (on_pad_added), decoder);
117
118   /* note that the demuxer will be linked to the decoder dynamically.
119      The reason is that Ogg may contain various streams (for example
120      audio and video). The source pad(s) will be created at run time,
121      by the demuxer when it detects the amount and nature of streams.
122      Therefore we connect a callback function which will be executed
123      when the "pad-added" is emitted.*/
124
125
126   /* Set the pipeline to "playing" state*/
127   g_print ("Now playing: %s\n", argv[1]);
128   gst_element_set_state (pipeline, GST_STATE_PLAYING);
129
130
131   /* Iterate */
132   g_print ("Running...\n");
133   g_main_loop_run (loop);
134
135
136   /* Out of the main loop, clean up nicely */
137   g_print ("Returned, stopping playback\n");
138   gst_element_set_state (pipeline, GST_STATE_NULL);
139
140   g_print ("Deleting pipeline\n");
141   gst_object_unref (GST_OBJECT (pipeline));
142
143   return 0;
144 }