abab47ce1ee393d32cc73b542464ed947c879df0
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / examples / dynamic / sprinkle2.c
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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-0.10` 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 *
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_get_request_pad (adder, "sink%d");
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
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 *
142 create_state (void)
143 {
144   SprinkleState *state;
145
146   state = g_new0 (SprinkleState, 1);
147
148   return state;
149 }
150
151 static void
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
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
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
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
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-int",
247       "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
248       "channels", G_TYPE_INT, 2,
249       "width", G_TYPE_INT, 16,
250       "depth", G_TYPE_INT, 16,
251       "rate", G_TYPE_INT, 44100, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
252   g_object_set (filter, "caps", caps, NULL);
253   gst_caps_unref (caps);
254
255   gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
256
257   res = gst_element_link_many (adder, filter, convert, sink, NULL);
258   g_assert (res);
259
260   /* setup message handling */
261   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
262   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
263   g_signal_connect (bus, "message::error", (GCallback) message_received,
264       pipeline);
265   g_signal_connect (bus, "message::warning", (GCallback) message_received,
266       pipeline);
267   g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
268       pipeline);
269
270   /* we set the pipeline to PLAYING, the pipeline will not yet preroll because
271    * there is no source providing data for it yet */
272   gst_element_set_state (pipeline, GST_STATE_PLAYING);
273
274   /* and add the function that modifies the pipeline every 100ms */
275   state = create_state ();
276   g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
277
278   /* go to main loop */
279   g_main_loop_run (loop);
280
281   gst_element_set_state (pipeline, GST_STATE_NULL);
282
283   free_state (state);
284   gst_object_unref (bus);
285   gst_object_unref (pipeline);
286
287   return 0;
288 }