1 /* GStreamer
2 *
3 * sprinkle.c: sample application to dynamically mix tones with adder
4 *
5 * Copyright (C) <2009> 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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 /*
24 * Produces a sweeping sprinkle of tones by dynamically adding and removing
25 * elements to adder.
26 *
27 * gcc `pkg-config --cflags --libs gstreamer-1.0` sprinkle2.c -osprinkle2
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <gst/gst.h>
35
36 static GstElement *pipeline, *adder;
37 static GMainLoop *loop;
38
39 typedef struct
40 {
41 GstElement *src, *fx;
42 GstPad *src_srcpad;
43 GstPad *fx_sinkpad, *fx_srcpad;
44 GstPad *adder_sinkpad;
45 gdouble freq;
46 gfloat pos;
47 } SourceInfo;
48
49 /* dynamically add the source to the pipeline and link it to a new pad on
50 * adder */
51 static SourceInfo *
add_source(gdouble freq,gfloat pos)52 add_source (gdouble freq, gfloat pos)
53 {
54 SourceInfo *info;
55
56 info = g_new0 (SourceInfo, 1);
57 info->freq = freq;
58 info->pos = pos;
59
60 /* make source with unique name */
61 info->src = gst_element_factory_make ("audiotestsrc", NULL);
62 info->fx = gst_element_factory_make ("audiopanorama", NULL);
63
64 g_object_set (info->src, "freq", freq, "volume", (gdouble) 0.35, NULL);
65 g_object_set (info->fx, "panorama", pos, NULL);
66
67 /* add to the bin */
68 gst_bin_add (GST_BIN (pipeline), info->src);
69 gst_bin_add (GST_BIN (pipeline), info->fx);
70
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");
75
76 /* get new pad from adder, adder will now wait for data on this pad */
77 info->adder_sinkpad = gst_element_request_pad_simple (adder, "sink_%u");
78
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);
82
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);
86
87 g_print ("added freq %5.0f, pos %3.1f\n", info->freq, info->pos);
88
89 return info;
90 }
91
92 /* remove the source from the pipeline after removing it from adder */
93 static void
remove_source(SourceInfo * info)94 remove_source (SourceInfo * info)
95 {
96 g_print ("remove freq %5.0f, pos %3.1f\n", info->freq, info->pos);
97
98 /* lock the state so that we can put it to NULL without the parent messing
99 * with our state */
100 gst_element_set_locked_state (info->src, TRUE);
101 gst_element_set_locked_state (info->fx, TRUE);
102
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);
111
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);
118
119 /* remove from the bin */
120 gst_bin_remove (GST_BIN (pipeline), info->src);
121 gst_bin_remove (GST_BIN (pipeline), info->fx);
122
123 /* send EOS to the sinkpad to make adder EOS when needed */
124 gst_pad_send_event (info->adder_sinkpad, gst_event_new_eos ());
125
126 /* give back the pad */
127 gst_element_release_request_pad (adder, info->adder_sinkpad);
128 gst_object_unref (info->adder_sinkpad);
129
130 g_free (info);
131 }
132
133 /* we'll keep the state of the sources in this structure. We keep 3 sources
134 * alive */
135 typedef struct
136 {
137 guint count;
138 SourceInfo *infos[3];
139 } SprinkleState;
140
141 static SprinkleState *
create_state(void)142 create_state (void)
143 {
144 SprinkleState *state;
145
146 state = g_new0 (SprinkleState, 1);
147
148 return state;
149 }
150
151 static void
free_state(SprinkleState * state)152 free_state (SprinkleState * state)
153 {
154 SourceInfo *info;
155 gint i;
156
157 for (i = 0; i < 3; i++) {
158 info = state->infos[i];
159 if (info)
160 remove_source (info);
161 }
162
163 g_free (state);
164 }
165
166 static gboolean
do_sprinkle(SprinkleState * state)167 do_sprinkle (SprinkleState * state)
168 {
169 SourceInfo *info;
170 gint i;
171
172 /* first remove the oldest info */
173 info = state->infos[2];
174
175 if (info)
176 remove_source (info);
177
178 /* move sources */
179 for (i = 2; i > 0; i--) {
180 state->infos[i] = state->infos[i - 1];
181 }
182
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));
188 state->count++;
189 } else {
190 state->infos[0] = NULL;
191 }
192 return TRUE;
193 }
194
195 static void
message_received(GstBus * bus,GstMessage * message,GstPipeline * pipeline)196 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
197 {
198 const GstStructure *s;
199
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)));
204 if (s) {
205 gchar *sstr;
206
207 sstr = gst_structure_to_string (s);
208 g_print ("%s\n", sstr);
209 g_free (sstr);
210 } else {
211 g_print ("no message details\n");
212 }
213 }
214
215 static void
eos_message_received(GstBus * bus,GstMessage * message,GstPipeline * pipeline)216 eos_message_received (GstBus * bus, GstMessage * message,
217 GstPipeline * pipeline)
218 {
219 message_received (bus, message, pipeline);
220 g_main_loop_quit (loop);
221 }
222
223 int
main(int argc,char * argv[])224 main (int argc, char *argv[])
225 {
226 GstBus *bus;
227 GstElement *filter, *convert, *sink;
228 GstCaps *caps;
229 gboolean res;
230 SprinkleState *state;
231
232 gst_init (&argc, &argv);
233
234 loop = g_main_loop_new (NULL, TRUE);
235
236 pipeline = gst_pipeline_new ("pipeline");
237
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
240 * a format */
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");
245
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);
251
252 gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
253
254 res = gst_element_link_many (adder, filter, convert, sink, NULL);
255 g_assert (res);
256
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,
261 pipeline);
262 g_signal_connect (bus, "message::warning", (GCallback) message_received,
263 pipeline);
264 g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
265 pipeline);
266
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);
270
271 /* and add the function that modifies the pipeline every 100ms */
272 state = create_state ();
273 g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
274
275 /* go to main loop */
276 g_main_loop_run (loop);
277
278 gst_element_set_state (pipeline, GST_STATE_NULL);
279
280 free_state (state);
281 gst_object_unref (bus);
282 gst_object_unref (pipeline);
283
284 return 0;
285 }
286