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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * Produces a sweeping sprinkle of tones by dynamically adding and removing
27 * gcc `pkg-config --cflags --libs gstreamer-0.10` sprinkle2.c -osprinkle2
36 static GstElement *pipeline, *adder;
37 static GMainLoop *loop;
43 GstPad *fx_sinkpad, *fx_srcpad;
44 GstPad *adder_sinkpad;
49 /* dynamically add the source to the pipeline and link it to a new pad on
52 add_source (gdouble freq, gfloat pos)
56 info = g_new0 (SourceInfo, 1);
60 /* make source with unique name */
61 info->src = gst_element_factory_make ("audiotestsrc", NULL);
62 info->fx = gst_element_factory_make ("audiopanorama", NULL);
64 g_object_set (info->src, "freq", freq, "volume", (gdouble) 0.35, NULL);
65 g_object_set (info->fx, "panorama", pos, NULL);
68 gst_bin_add (GST_BIN (pipeline), info->src);
69 gst_bin_add (GST_BIN (pipeline), info->fx);
71 /* get pads from the elements */
72 info->src_srcpad = gst_element_get_static_pad (info->src, "src");
73 info->fx_srcpad = gst_element_get_static_pad (info->fx, "src");
74 info->fx_sinkpad = gst_element_get_static_pad (info->fx, "sink");
76 /* get new pad from adder, adder will now wait for data on this pad */
77 info->adder_sinkpad = gst_element_get_request_pad (adder, "sink_%u");
79 /* link src to fx and fx to adder */
80 gst_pad_link (info->fx_srcpad, info->adder_sinkpad);
81 gst_pad_link (info->src_srcpad, info->fx_sinkpad);
83 /* and play the elements, change the state from sink to source */
84 gst_element_set_state (info->fx, GST_STATE_PLAYING);
85 gst_element_set_state (info->src, GST_STATE_PLAYING);
87 g_print ("added freq %5.0f, pos %3.1f\n", info->freq, info->pos);
92 /* remove the source from the pipeline after removing it from adder */
94 remove_source (SourceInfo * info)
96 g_print ("remove freq %5.0f, pos %3.1f\n", info->freq, info->pos);
98 /* lock the state so that we can put it to NULL without the parent messing
100 gst_element_set_locked_state (info->src, TRUE);
101 gst_element_set_locked_state (info->fx, TRUE);
103 /* first stop the source. Remember that this might block when in the PAUSED
104 * state. Alternatively one could send EOS to the source, install an event
105 * probe and schedule a state change/unlink/release from the mainthread. */
106 gst_element_set_state (info->fx, GST_STATE_NULL);
107 /* NOTE that the source emits EOS when shutting down but the EOS will not
108 * reach the adder sinkpad because the effect is in the NULL state. We will
109 * send an EOS to adder later. */
110 gst_element_set_state (info->src, GST_STATE_NULL);
112 /* unlink from adder */
113 gst_pad_unlink (info->src_srcpad, info->fx_sinkpad);
114 gst_pad_unlink (info->fx_srcpad, info->adder_sinkpad);
115 gst_object_unref (info->src_srcpad);
116 gst_object_unref (info->fx_srcpad);
117 gst_object_unref (info->fx_sinkpad);
119 /* remove from the bin */
120 gst_bin_remove (GST_BIN (pipeline), info->src);
121 gst_bin_remove (GST_BIN (pipeline), info->fx);
123 /* send EOS to the sinkpad to make adder EOS when needed */
124 gst_pad_send_event (info->adder_sinkpad, gst_event_new_eos ());
126 /* give back the pad */
127 gst_element_release_request_pad (adder, info->adder_sinkpad);
128 gst_object_unref (info->adder_sinkpad);
133 /* we'll keep the state of the sources in this structure. We keep 3 sources
138 SourceInfo *infos[3];
141 static SprinkleState *
144 SprinkleState *state;
146 state = g_new0 (SprinkleState, 1);
152 free_state (SprinkleState * state)
157 for (i = 0; i < 3; i++) {
158 info = state->infos[i];
160 remove_source (info);
167 do_sprinkle (SprinkleState * state)
172 /* first remove the oldest info */
173 info = state->infos[2];
176 remove_source (info);
179 for (i = 2; i > 0; i--) {
180 state->infos[i] = state->infos[i - 1];
183 /* add new source, stop adding sources after 10 rounds. */
184 if (state->count < 20) {
185 state->infos[0] = add_source (
186 (gdouble) ((state->count * 100) + 200),
187 ((gfloat) (state->count % 5) / 2.0 - 1.0));
190 state->infos[0] = NULL;
196 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
198 const GstStructure *s;
200 s = gst_message_get_structure (message);
201 g_print ("message from \"%s\" (%s): ",
202 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
203 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
207 sstr = gst_structure_to_string (s);
208 g_print ("%s\n", sstr);
211 g_print ("no message details\n");
216 eos_message_received (GstBus * bus, GstMessage * message,
217 GstPipeline * pipeline)
219 message_received (bus, message, pipeline);
220 g_main_loop_quit (loop);
224 main (int argc, char *argv[])
227 GstElement *filter, *convert, *sink;
230 SprinkleState *state;
232 gst_init (&argc, &argv);
234 loop = g_main_loop_new (NULL, TRUE);
236 pipeline = gst_pipeline_new ("pipeline");
238 /* add the fixed part to the pipeline. Remember that we need a capsfilter
239 * after adder so that multiple sources are not racing to negotiate
241 adder = gst_element_factory_make ("adder", "adder");
242 filter = gst_element_factory_make ("capsfilter", "filter");
243 convert = gst_element_factory_make ("audioconvert", "convert");
244 sink = gst_element_factory_make ("autoaudiosink", "sink");
246 caps = gst_caps_new_simple ("audio/x-raw",
247 "format", G_TYPE_STRING, "S16LE",
248 "channels", G_TYPE_INT, 2, "rate", G_TYPE_INT, 44100, NULL);
249 g_object_set (filter, "caps", caps, NULL);
250 gst_caps_unref (caps);
252 gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
254 res = gst_element_link_many (adder, filter, convert, sink, NULL);
257 /* setup message handling */
258 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
259 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
260 g_signal_connect (bus, "message::error", (GCallback) message_received,
262 g_signal_connect (bus, "message::warning", (GCallback) message_received,
264 g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
267 /* we set the pipeline to PLAYING, the pipeline will not yet preroll because
268 * there is no source providing data for it yet */
269 gst_element_set_state (pipeline, GST_STATE_PLAYING);
271 /* and add the function that modifies the pipeline every 100ms */
272 state = create_state ();
273 g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
275 /* go to main loop */
276 g_main_loop_run (loop);
278 gst_element_set_state (pipeline, GST_STATE_NULL);
281 gst_object_unref (bus);
282 gst_object_unref (pipeline);