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` sprinkle3.c -osprinkle3
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 *bin, *src, *fx;
42 GstPad *src_srcpad;
43 GstPad *fx_sinkpad, *fx_srcpad;
44 GstPad *adder_sinkpad;
45 GstPad *bin_srcpad;
46 gdouble freq;
47 gfloat pos;
48 } SourceInfo;
49
50 /* dynamically add the source to the pipeline and link it to a new pad on
51 * adder */
52 static SourceInfo *
add_source(gdouble freq,gfloat pos)53 add_source (gdouble freq, gfloat pos)
54 {
55 SourceInfo *info;
56
57 info = g_new0 (SourceInfo, 1);
58 info->freq = freq;
59 info->pos = pos;
60
61 /* make source with unique name */
62 info->bin = gst_element_factory_make ("bin", NULL);
63 info->src = gst_element_factory_make ("audiotestsrc", NULL);
64 info->fx = gst_element_factory_make ("audiopanorama", NULL);
65
66 g_object_set (info->src, "freq", freq, "volume", (gdouble) 0.35, NULL);
67 g_object_set (info->fx, "panorama", pos, NULL);
68
69 /* add to the bin */
70 gst_bin_add (GST_BIN (info->bin), info->src);
71 gst_bin_add (GST_BIN (info->bin), info->fx);
72
73 /* get pads from the elements */
74 info->src_srcpad = gst_element_get_static_pad (info->src, "src");
75 info->fx_srcpad = gst_element_get_static_pad (info->fx, "src");
76 info->fx_sinkpad = gst_element_get_static_pad (info->fx, "sink");
77
78 /* create and add a pad for the bin */
79 info->bin_srcpad = gst_ghost_pad_new ("src", info->fx_srcpad);
80 gst_element_add_pad (info->bin, info->bin_srcpad);
81
82 /* get new pad from adder, adder will now wait for data on this pad */
83 info->adder_sinkpad = gst_element_request_pad_simple (adder, "sink_%u");
84
85 /* link inside the bin */
86 gst_pad_link (info->src_srcpad, info->fx_sinkpad);
87
88 /* add bin to pipeline */
89 gst_bin_add (GST_BIN (pipeline), info->bin);
90
91 /* link bin to adder */
92 gst_pad_link (info->bin_srcpad, info->adder_sinkpad);
93
94 /* and play the elements */
95 gst_element_set_state (info->bin, GST_STATE_PLAYING);
96
97 g_print ("added freq %5.0f, pos %3.1f\n", info->freq, info->pos);
98
99 return info;
100 }
101
102 /* remove the source from the pipeline after removing it from adder */
103 static void
remove_source(SourceInfo * info)104 remove_source (SourceInfo * info)
105 {
106 g_print ("remove freq %5.0f, pos %3.1f\n", info->freq, info->pos);
107
108 /* lock the state so that we can put it to NULL without the parent messing
109 * with our state */
110 gst_element_set_locked_state (info->bin, TRUE);
111
112 /* first stop the source. Remember that this might block when in the PAUSED
113 * state. Alternatively one could send EOS to the source, install an event
114 * probe and schedule a state change/unlink/release from the mainthread. */
115 /* NOTE that the source inside the bin will emit EOS but it will not reach
116 * adder because the element after the source is shut down first. We will send
117 * EOS later */
118 gst_element_set_state (info->bin, GST_STATE_NULL);
119
120 /* unlink bin from adder */
121 gst_pad_unlink (info->bin_srcpad, info->adder_sinkpad);
122
123 /* release pads */
124 gst_object_unref (info->src_srcpad);
125 gst_object_unref (info->fx_srcpad);
126 gst_object_unref (info->fx_sinkpad);
127
128 /* remove from the bin */
129 gst_bin_remove (GST_BIN (pipeline), info->bin);
130
131 /* send EOS to the sinkpad to make adder EOS when needed */
132 gst_pad_send_event (info->adder_sinkpad, gst_event_new_eos ());
133
134 /* give back the pad */
135 gst_element_release_request_pad (adder, info->adder_sinkpad);
136 gst_object_unref (info->adder_sinkpad);
137
138 g_free (info);
139 }
140
141 /* we'll keep the state of the sources in this structure. We keep 3 sources
142 * alive */
143 typedef struct
144 {
145 guint count;
146 SourceInfo *infos[3];
147 } SprinkleState;
148
149 static SprinkleState *
create_state(void)150 create_state (void)
151 {
152 SprinkleState *state;
153
154 state = g_new0 (SprinkleState, 1);
155
156 return state;
157 }
158
159 static void
free_state(SprinkleState * state)160 free_state (SprinkleState * state)
161 {
162 SourceInfo *info;
163 gint i;
164
165 for (i = 0; i < 3; i++) {
166 info = state->infos[i];
167 if (info)
168 remove_source (info);
169 }
170
171 g_free (state);
172 }
173
174 static gboolean
do_sprinkle(SprinkleState * state)175 do_sprinkle (SprinkleState * state)
176 {
177 SourceInfo *info;
178 gint i;
179
180 /* first remove the oldest info */
181 info = state->infos[2];
182
183 if (info)
184 remove_source (info);
185
186 /* move sources */
187 for (i = 2; i > 0; i--) {
188 state->infos[i] = state->infos[i - 1];
189 }
190
191 /* add new source, stop adding sources after 10 rounds. */
192 if (state->count < 20) {
193 state->infos[0] = add_source (
194 (gdouble) ((state->count * 100) + 200),
195 ((gfloat) (state->count % 5) / 2.0 - 1.0));
196 state->count++;
197 } else {
198 state->infos[0] = NULL;
199 }
200
201 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
202 /*GST_DEBUG_GRAPH_SHOW_ALL, */
203 GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS | GST_DEBUG_GRAPH_SHOW_STATES,
204 "sprinkle3");
205 return TRUE;
206 }
207
208 static void
message_received(GstBus * bus,GstMessage * message,GstPipeline * pipeline)209 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
210 {
211 const GstStructure *s;
212
213 s = gst_message_get_structure (message);
214 g_print ("message from \"%s\" (%s): ",
215 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
216 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
217 if (s) {
218 gchar *sstr;
219
220 sstr = gst_structure_to_string (s);
221 g_print ("%s\n", sstr);
222 g_free (sstr);
223 } else {
224 g_print ("no message details\n");
225 }
226 }
227
228 static void
eos_message_received(GstBus * bus,GstMessage * message,GstPipeline * pipeline)229 eos_message_received (GstBus * bus, GstMessage * message,
230 GstPipeline * pipeline)
231 {
232 message_received (bus, message, pipeline);
233 g_main_loop_quit (loop);
234 }
235
236 int
main(int argc,char * argv[])237 main (int argc, char *argv[])
238 {
239 GstBus *bus;
240 GstElement *filter, *convert, *sink;
241 GstCaps *caps;
242 gboolean res;
243 SprinkleState *state;
244
245 gst_init (&argc, &argv);
246
247 loop = g_main_loop_new (NULL, TRUE);
248
249 pipeline = gst_pipeline_new ("pipeline");
250
251 /* add the fixed part to the pipeline. Remember that we need a capsfilter
252 * after adder so that multiple sources are not racing to negotiate
253 * a format */
254 adder = gst_element_factory_make ("adder", "adder");
255 filter = gst_element_factory_make ("capsfilter", "filter");
256 convert = gst_element_factory_make ("audioconvert", "convert");
257 sink = gst_element_factory_make ("autoaudiosink", "sink");
258
259 caps = gst_caps_new_simple ("audio/x-raw",
260 "format", G_TYPE_STRING, "S16LE",
261 "channels", G_TYPE_INT, 2, "rate", G_TYPE_INT, 44100, NULL);
262 g_object_set (filter, "caps", caps, NULL);
263 gst_caps_unref (caps);
264
265 gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
266
267 res = gst_element_link_many (adder, filter, convert, sink, NULL);
268 g_assert (res);
269
270 /* setup message handling */
271 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
272 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
273 g_signal_connect (bus, "message::error", (GCallback) message_received,
274 pipeline);
275 g_signal_connect (bus, "message::warning", (GCallback) message_received,
276 pipeline);
277 g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
278 pipeline);
279
280 /* we set the pipeline to PLAYING, the pipeline will not yet preroll because
281 * there is no source providing data for it yet */
282 gst_element_set_state (pipeline, GST_STATE_PLAYING);
283
284 /* and add the function that modifies the pipeline every 100ms */
285 state = create_state ();
286 g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
287
288 /* go to main loop */
289 g_main_loop_run (loop);
290
291 gst_element_set_state (pipeline, GST_STATE_NULL);
292
293 free_state (state);
294 gst_object_unref (bus);
295 gst_object_unref (pipeline);
296
297 return 0;
298 }
299