3 * codec-select.c: sample application to dynamically select a codec
5 * Copyright (C) <2008> Wim Taymans <wim dot taymans at gmail dot com>
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.
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.
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.
24 * This example sets up a pipeline to 'encode' an audiotestsrc into 3 different
25 * formats. The format can be selected dynamically at runtime.
27 * Each of the encoders require the audio in a specific different format.
29 * This example uses identity as the encoder and enforces the caps on identity
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.
39 /* Create an encoder element.
40 * We make a bin containing:
42 * audioresample ! <enccaps> ! identity
44 * The sinkpad of audioresample and source pad of identity are ghosted on the
48 make_encoder (const GstCaps * caps)
51 GstElement *audioresample;
52 GstElement *capsfilter;
56 /* create result bin */
57 result = gst_bin_new (NULL);
61 audioresample = gst_element_factory_make ("audioresample", NULL);
62 g_assert (audioresample);
64 capsfilter = gst_element_factory_make ("capsfilter", NULL);
65 g_assert (capsfilter);
66 g_object_set (capsfilter, "caps", caps, NULL);
68 identity = gst_element_factory_make ("identity", NULL);
70 g_object_set (identity, "silent", TRUE, NULL);
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);
78 gst_element_link_pads (audioresample, "src", capsfilter, "sink");
79 gst_element_link_pads (capsfilter, "src", identity, "sink");
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);
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);
96 * audiotestsrc ! <audiocaps> ! output-selector ! [enc1 .. enc3] ! input-selector
97 * select-all = true ! fakesink
99 * <audiocaps> makes sure we only produce one format from the audiotestsrc.
101 * Each encX element consists of:
103 * audioresample ! <enccaps> ! identity !
105 * This way we can simply switch encoders without having to renegotiate.
111 GstElement *audiotestsrc;
112 GstElement *audiocaps;
113 GstElement *outputselect;
114 GstElement *inputselect;
117 GstCaps *capslist[3];
120 /* create result pipeline */
121 result = gst_pipeline_new (NULL);
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);
129 audiocaps = gst_element_factory_make ("capsfilter", NULL);
130 g_assert (audiocaps);
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);
138 outputselect = gst_element_factory_make ("output-selector", "select");
139 g_assert (outputselect);
141 inputselect = gst_element_factory_make ("input-selector", NULL);
142 g_assert (inputselect);
143 g_object_set (inputselect, "select-all", TRUE, NULL);
145 sink = gst_element_factory_make ("fakesink", NULL);
146 g_object_set (sink, "sync", TRUE, NULL);
147 g_object_set (sink, "silent", TRUE, NULL);
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);
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");
165 ("audio/x-raw-int,signed=true,width=16,depth=16,rate=48000,channels=1");
168 ("audio/x-raw-int,signed=true,width=16,depth=16,rate=16000,channels=1");
171 ("audio/x-raw-int,signed=true,width=16,depth=16,rate=8000,channels=1");
173 /* create encoder elements */
174 for (i = 0; i < 3; i++) {
176 GstPad *srcpad, *sinkpad;
178 encoder = make_encoder (capslist[i]);
181 gst_bin_add (GST_BIN (result), encoder);
183 srcpad = gst_element_get_request_pad (outputselect, "src_%u");
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);
189 srcpad = gst_element_get_static_pad (encoder, "src");
190 sinkpad = gst_element_get_request_pad (inputselect, "sink_%u");
191 gst_pad_link (srcpad, sinkpad);
192 gst_object_unref (srcpad);
193 gst_object_unref (sinkpad);
200 do_switch (GstElement * pipeline)
207 rand = g_random_int_range (0, 3);
209 g_print ("switching to %d\n", rand);
211 /* find the selector */
212 select = gst_bin_get_by_name (GST_BIN (pipeline), "select");
214 /* get the named pad */
215 name = g_strdup_printf ("src_%u", rand);
216 pad = gst_element_get_static_pad (select, name);
219 /* set the active pad */
220 g_object_set (select, "active-pad", pad, NULL);
226 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
228 GstElement *sender = (GstElement *) GST_MESSAGE_SRC (message);
229 const gchar *name = gst_element_get_name (sender);
230 GMainLoop *loop = (GMainLoop *) data;
232 g_print ("Got %s message from %s\n", GST_MESSAGE_TYPE_NAME (message), name);
234 switch (GST_MESSAGE_TYPE (message)) {
236 case GST_MESSAGE_ERROR:{
240 gst_message_parse_error (message, &err, &debug);
241 g_print ("Error: %s (%s)\n", err->message, debug);
245 g_main_loop_quit (loop);
248 case GST_MESSAGE_EOS:
250 g_main_loop_quit (loop);
253 /* unhandled message */
261 main (gint argc, gchar * argv[])
263 GstElement *pipeline;
268 gst_init (&argc, &argv);
269 loop = g_main_loop_new (NULL, FALSE);
272 pipeline = make_pipeline ();
274 g_signal_connect (pipeline, "deep_notify",
275 G_CALLBACK (gst_object_default_deep_notify), NULL);
277 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
278 gst_bus_add_watch (bus, my_bus_callback, loop);
279 gst_object_unref (bus);
281 g_print ("Starting pipeline\n");
283 gst_element_set_state (pipeline, GST_STATE_PLAYING);
285 /* add a timeout to cycle between the formats */
286 g_timeout_add (1000, (GSourceFunc) do_switch, pipeline);
289 g_main_loop_run (loop);
291 g_print ("Nulling pipeline\n");
294 gst_element_set_state (pipeline, GST_STATE_NULL);
295 gst_object_unref (pipeline);