3 * sprinkle.c: sample application to dynamically mix tones with adder
5 * Copyright (C) <2009> 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 * Produces a sweeping sprinkle of tones by dynamically adding and removing
34 static GstElement *pipeline, *adder;
35 static GMainLoop *loop;
45 /* dynamically add the source to the pipeline and link it to a new pad on
48 add_source (gdouble freq)
52 info = g_new0 (SourceInfo, 1);
55 /* make source with unique name */
56 info->element = gst_element_factory_make ("audiotestsrc", NULL);
58 g_object_set (info->element, "freq", freq, NULL);
61 gst_bin_add (GST_BIN (pipeline), info->element);
63 /* get pad from the element */
64 info->srcpad = gst_element_get_static_pad (info->element, "src");
66 /* get new pad from adder, adder will now wait for data on this pad */
67 info->sinkpad = gst_element_get_request_pad (adder, "sink%d");
69 /* link pad to adder */
70 gst_pad_link (info->srcpad, info->sinkpad);
72 /* and play the element */
73 gst_element_set_state (info->element, GST_STATE_PLAYING);
75 g_print ("added freq %f\n", info->freq);
80 /* remove the source from the pipeline after removing it from adder */
82 remove_source (SourceInfo * info)
84 g_print ("remove freq %f\n", info->freq);
86 /* lock the state so that we can put it to NULL without the parent messing
88 gst_element_set_locked_state (info->element, TRUE);
90 /* first stop the source. Remember that this might block when in the PAUSED
91 * state. Alternatively one could send EOS to the source, install an event
92 * probe and schedule a state change/unlink/release from the mainthread.
93 * Note that changing the state of a source makes it emit an EOS, which can
94 * make adder go EOS. */
95 gst_element_set_state (info->element, GST_STATE_NULL);
97 /* unlink from adder */
98 gst_pad_unlink (info->srcpad, info->sinkpad);
99 gst_object_unref (info->srcpad);
101 /* remove from the bin */
102 gst_bin_remove (GST_BIN (pipeline), info->element);
104 /* give back the pad */
105 gst_element_release_request_pad (adder, info->sinkpad);
106 gst_object_unref (info->sinkpad);
111 /* we'll keep the state of the sources in this structure. We keep 3 sources
116 SourceInfo *infos[3];
119 static SprinkleState *
122 SprinkleState *state;
124 state = g_new0 (SprinkleState, 1);
130 free_state (SprinkleState * state)
135 for (i = 0; i < 3; i++) {
136 info = state->infos[i];
138 remove_source (info);
145 do_sprinkle (SprinkleState * state)
150 /* first remove the oldest info */
151 info = state->infos[2];
154 remove_source (info);
157 for (i = 2; i > 0; i--) {
158 state->infos[i] = state->infos[i - 1];
161 /* add new source, stop adding sources after 10 rounds. */
162 if (state->count < 10) {
163 state->infos[0] = add_source ((state->count * 100) + 200);
166 state->infos[0] = NULL;
172 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
174 const GstStructure *s;
176 s = gst_message_get_structure (message);
177 g_print ("message from \"%s\" (%s): ",
178 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
179 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
183 sstr = gst_structure_to_string (s);
184 g_print ("%s\n", sstr);
187 g_print ("no message details\n");
192 eos_message_received (GstBus * bus, GstMessage * message,
193 GstPipeline * pipeline)
195 message_received (bus, message, pipeline);
196 g_main_loop_quit (loop);
200 main (int argc, char *argv[])
203 GstElement *filter, *convert, *sink;
206 SprinkleState *state;
208 gst_init (&argc, &argv);
210 loop = g_main_loop_new (NULL, TRUE);
212 pipeline = gst_pipeline_new ("pipeline");
214 /* add the fixed part to the pipeline. Remember that we need a capsfilter
215 * after adder so that multiple sources are not racing to negotiate
217 adder = gst_element_factory_make ("adder", "adder");
218 filter = gst_element_factory_make ("capsfilter", "filter");
219 convert = gst_element_factory_make ("audioconvert", "convert");
220 sink = gst_element_factory_make ("autoaudiosink", "sink");
222 caps = gst_caps_new_simple ("audio/x-raw-int",
223 "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
224 "channels", G_TYPE_INT, 1,
225 "width", G_TYPE_INT, 16,
226 "depth", G_TYPE_INT, 16,
227 "rate", G_TYPE_INT, 44100, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
228 g_object_set (filter, "caps", caps, NULL);
229 gst_caps_unref (caps);
231 gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
233 res = gst_element_link_many (adder, filter, convert, sink, NULL);
236 /* setup message handling */
237 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
238 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
239 g_signal_connect (bus, "message::error", (GCallback) message_received,
241 g_signal_connect (bus, "message::warning", (GCallback) message_received,
243 g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
246 /* we set the pipeline to PLAYING, the pipeline will not yet preroll because
247 * there is no source providing data for it yet */
248 gst_element_set_state (pipeline, GST_STATE_PLAYING);
250 /* and add the function that modifies the pipeline every 100ms */
251 state = create_state ();
252 g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
254 /* go to main loop */
255 g_main_loop_run (loop);
257 gst_element_set_state (pipeline, GST_STATE_NULL);
260 gst_object_unref (bus);
261 gst_object_unref (pipeline);