Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / examples / dynamic / codec-select.c
1 /* GStreamer
2  *
3  * codec-select.c: sample application to dynamically select a codec
4  *
5  * Copyright (C) <2008> Wim Taymans <wim dot taymans at gmail dot com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /*
24  * This example sets up a pipeline to 'encode' an audiotestsrc into 3 different
25  * formats. The format can be selected dynamically at runtime.
26  *
27  * Each of the encoders require the audio in a specific different format.
28  *
29  * This example uses identity as the encoder and enforces the caps on identity
30  * with a capsfilter.
31  *
32  * This is a good example of input and output selector and how these elements
33  * preserve segment and timing information while switching between streams.
34  */
35
36 #include <string.h>
37 #include <gst/gst.h>
38
39 /* Create an encoder element.
40  * We make a bin containing:
41  *
42  * audioresample ! <enccaps> ! identity
43  *
44  * The sinkpad of audioresample and source pad of identity are ghosted on the
45  * bin.
46  */
47 static GstElement *
48 make_encoder (const GstCaps * caps)
49 {
50   GstElement *result;
51   GstElement *audioresample;
52   GstElement *capsfilter;
53   GstElement *identity;
54   GstPad *pad;
55
56   /* create result bin */
57   result = gst_bin_new (NULL);
58   g_assert (result);
59
60   /* create elements */
61   audioresample = gst_element_factory_make ("audioresample", NULL);
62   g_assert (audioresample);
63
64   capsfilter = gst_element_factory_make ("capsfilter", NULL);
65   g_assert (capsfilter);
66   g_object_set (capsfilter, "caps", caps, NULL);
67
68   identity = gst_element_factory_make ("identity", NULL);
69   g_assert (identity);
70   g_object_set (identity, "silent", TRUE, NULL);
71
72   /* add elements to result bin */
73   gst_bin_add (GST_BIN (result), audioresample);
74   gst_bin_add (GST_BIN (result), capsfilter);
75   gst_bin_add (GST_BIN (result), identity);
76
77   /* link elements */
78   gst_element_link_pads (audioresample, "src", capsfilter, "sink");
79   gst_element_link_pads (capsfilter, "src", identity, "sink");
80
81   /* ghost src and sink pads */
82   pad = gst_element_get_static_pad (audioresample, "sink");
83   gst_element_add_pad (result, gst_ghost_pad_new ("sink", pad));
84   gst_object_unref (pad);
85
86   pad = gst_element_get_static_pad (identity, "src");
87   gst_element_add_pad (result, gst_ghost_pad_new ("src", pad));
88   gst_object_unref (pad);
89
90   return result;
91 }
92
93 /*
94  * We generate:
95  *
96  * audiotestsrc ! <audiocaps> ! output-selector ! [enc1 .. enc3] ! input-selector
97  * select-all = true ! fakesink
98  *
99  * <audiocaps> makes sure we only produce one format from the audiotestsrc.
100  *
101  * Each encX element consists of:
102  *
103  *  audioresample ! <enccaps> ! identity !
104  *
105  * This way we can simply switch encoders without having to renegotiate.
106  */
107 static GstElement *
108 make_pipeline (void)
109 {
110   GstElement *result;
111   GstElement *audiotestsrc;
112   GstElement *audiocaps;
113   GstElement *outputselect;
114   GstElement *inputselect;
115   GstElement *sink;
116   GstCaps *caps;
117   GstCaps *capslist[3];
118   gint i;
119
120   /* create result pipeline */
121   result = gst_pipeline_new (NULL);
122   g_assert (result);
123
124   /* create various elements */
125   audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
126   g_object_set (audiotestsrc, "num-buffers", 1000, NULL);
127   g_assert (audiotestsrc);
128
129   audiocaps = gst_element_factory_make ("capsfilter", NULL);
130   g_assert (audiocaps);
131
132   caps =
133       gst_caps_from_string
134       ("audio/x-raw-int,signed=true,width=16,depth=16,rate=48000,channels=1");
135   g_object_set (audiocaps, "caps", caps, NULL);
136   gst_caps_unref (caps);
137
138   outputselect = gst_element_factory_make ("output-selector", "select");
139   g_assert (outputselect);
140
141   inputselect = gst_element_factory_make ("input-selector", NULL);
142   g_assert (inputselect);
143   g_object_set (inputselect, "select-all", TRUE, NULL);
144
145   sink = gst_element_factory_make ("fakesink", NULL);
146   g_object_set (sink, "sync", TRUE, NULL);
147   g_object_set (sink, "silent", TRUE, NULL);
148   g_assert (sink);
149
150   /* add elements */
151   gst_bin_add (GST_BIN (result), audiotestsrc);
152   gst_bin_add (GST_BIN (result), audiocaps);
153   gst_bin_add (GST_BIN (result), outputselect);
154   gst_bin_add (GST_BIN (result), inputselect);
155   gst_bin_add (GST_BIN (result), sink);
156
157   /* link elements */
158   gst_element_link_pads (audiotestsrc, "src", audiocaps, "sink");
159   gst_element_link_pads (audiocaps, "src", outputselect, "sink");
160   gst_element_link_pads (inputselect, "src", sink, "sink");
161
162   /* make caps */
163   capslist[0] =
164       gst_caps_from_string
165       ("audio/x-raw-int,signed=true,width=16,depth=16,rate=48000,channels=1");
166   capslist[1] =
167       gst_caps_from_string
168       ("audio/x-raw-int,signed=true,width=16,depth=16,rate=16000,channels=1");
169   capslist[2] =
170       gst_caps_from_string
171       ("audio/x-raw-int,signed=true,width=16,depth=16,rate=8000,channels=1");
172
173   /* create encoder elements */
174   for (i = 0; i < 3; i++) {
175     GstElement *encoder;
176     GstPad *srcpad, *sinkpad;
177
178     encoder = make_encoder (capslist[i]);
179     g_assert (encoder);
180
181     gst_bin_add (GST_BIN (result), encoder);
182
183     srcpad = gst_element_get_request_pad (outputselect, "src%d");
184     sinkpad = gst_element_get_static_pad (encoder, "sink");
185     gst_pad_link (srcpad, sinkpad);
186     gst_object_unref (srcpad);
187     gst_object_unref (sinkpad);
188
189     srcpad = gst_element_get_static_pad (encoder, "src");
190     sinkpad = gst_element_get_request_pad (inputselect, "sink%d");
191     gst_pad_link (srcpad, sinkpad);
192     gst_object_unref (srcpad);
193     gst_object_unref (sinkpad);
194   }
195
196   return result;
197 }
198
199 static gboolean
200 do_switch (GstElement * pipeline)
201 {
202   gint rand;
203   GstElement *select;
204   gchar *name;
205   GstPad *pad;
206
207   rand = g_random_int_range (0, 3);
208
209   g_print ("switching to %d\n", rand);
210
211   /* find the selector */
212   select = gst_bin_get_by_name (GST_BIN (pipeline), "select");
213
214   /* get the named pad */
215   name = g_strdup_printf ("src%d", rand);
216   pad = gst_element_get_static_pad (select, name);
217   g_free (name);
218
219   /* set the active pad */
220   g_object_set (select, "active-pad", pad, NULL);
221
222   return TRUE;
223 }
224
225 static gboolean
226 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
227 {
228   GstElement *sender = (GstElement *) GST_MESSAGE_SRC (message);
229   const gchar *name = gst_element_get_name (sender);
230   GMainLoop *loop = (GMainLoop *) data;
231
232   g_print ("Got %s message from %s\n", GST_MESSAGE_TYPE_NAME (message), name);
233
234   switch (GST_MESSAGE_TYPE (message)) {
235
236     case GST_MESSAGE_ERROR:{
237       GError *err;
238       gchar *debug;
239
240       gst_message_parse_error (message, &err, &debug);
241       g_print ("Error: %s (%s)\n", err->message, debug);
242       g_error_free (err);
243       g_free (debug);
244
245       g_main_loop_quit (loop);
246       break;
247     }
248     case GST_MESSAGE_EOS:
249       /* end-of-stream */
250       g_main_loop_quit (loop);
251       break;
252     default:
253       /* unhandled message */
254       break;
255   }
256
257   return TRUE;
258 }
259
260 gint
261 main (gint argc, gchar * argv[])
262 {
263   GstElement *pipeline;
264   GstBus *bus;
265   GMainLoop *loop;
266
267   /* init GStreamer */
268   gst_init (&argc, &argv);
269   loop = g_main_loop_new (NULL, FALSE);
270
271   /* set up */
272   pipeline = make_pipeline ();
273
274   g_signal_connect (pipeline, "deep_notify",
275       G_CALLBACK (gst_object_default_deep_notify), NULL);
276
277   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
278   gst_bus_add_watch (bus, my_bus_callback, loop);
279   gst_object_unref (bus);
280
281   g_print ("Starting pipeline\n");
282
283   gst_element_set_state (pipeline, GST_STATE_PLAYING);
284
285   /* add a timeout to cycle between the formats */
286   g_timeout_add (1000, (GSourceFunc) do_switch, pipeline);
287
288   /* now run */
289   g_main_loop_run (loop);
290
291   g_print ("Nulling pipeline\n");
292
293   /* also clean up */
294   gst_element_set_state (pipeline, GST_STATE_NULL);
295   gst_object_unref (pipeline);
296
297   return 0;
298 }