6b27f5b944c82b428995f815c4407d2b47ee2821
[platform/upstream/gstreamer.git] / examples / mixer / mixer.c
1 /*
2  * mixer.c - stereo audio mixer - thomas@apestaart.org
3  * example based on helloworld
4  * demonstrates the adder plugin and the volume envelope plugin 
5  * work in progress but do try it out 
6  * 
7  * Latest change :      28/08/2001
8  *                                      trying to adapt to incsched
9  *                                      delayed start for channels > 1
10  *                                      now works by quickhacking the
11  *                                      adder plugin to set
12  *                                      GST_ELEMENT_COTHREAD_STOPPING           
13  * Version :            0.5.1
14  */
15
16 #include <stdlib.h>
17 #include <gst/gst.h>
18 #include "mixer.h"
19 #include <unistd.h>
20
21 /*#define WITH_BUG */
22 /*#define WITH_BUG2 */
23 /*#define DEBUG */
24 /*#define AUTOPLUG      * define if you want autoplugging of input channels * */
25 /* function prototypes */
26
27 input_channel_t*        create_input_channel (int id, char* location);
28 void                            destroy_input_channel (input_channel_t *pipe);
29 void                            env_register_cp (GstElement *volenv, double cp_time, double cp_level);
30
31
32 gboolean playing;
33
34
35 /* eos will be called when the src element has an end of stream */
36 void eos(GstElement *element) 
37 {
38   g_print("have eos, quitting ?\n");
39
40 /*  playing = FALSE; */
41 }
42
43 G_GNUC_UNUSED static GstCaps*
44 gst_play_type_find (GstBin *bin, GstElement *element)
45 {
46   GstElement *typefind;
47   GstElement *pipeline;
48   GstCaps *caps = NULL;
49
50   GST_DEBUG ("GstPipeline: typefind for element \"%s\"",
51              GST_ELEMENT_NAME(element));
52
53   pipeline = gst_pipeline_new ("autoplug_pipeline");
54  
55   typefind = gst_element_factory_make ("typefind", "typefind");
56   g_return_val_if_fail (typefind != NULL, FALSE);
57
58   gst_pad_link (gst_element_get_pad (element, "src"),
59                    gst_element_get_pad (typefind, "sink"));
60   gst_bin_add (bin, typefind);
61   gst_bin_add (GST_BIN (pipeline), GST_ELEMENT (bin));
62   
63   gst_element_set_state (pipeline, GST_STATE_PLAYING);
64   
65   /* push a buffer... the have_type signal handler will set the found flag */
66   gst_bin_iterate (GST_BIN (pipeline));
67   
68   gst_element_set_state (pipeline, GST_STATE_NULL);
69
70   caps = gst_pad_get_caps (gst_element_get_pad (element, "src"));
71
72   gst_pad_unlink (gst_element_get_pad (element, "src"),
73                       gst_element_get_pad (typefind, "sink"));
74   gst_bin_remove (bin, typefind);
75   gst_bin_remove (GST_BIN (pipeline), GST_ELEMENT (bin));
76   gst_object_unref (GST_OBJECT (typefind));
77   gst_object_unref (GST_OBJECT (pipeline));
78                    
79   return caps;
80 }
81
82 int main(int argc,char *argv[]) 
83 {
84   int i, j;
85   int num_channels;
86   
87   char buffer[20];
88   
89   GList *input_channels;                /* structure holding all the input channels */
90   
91   input_channel_t *channel_in;
92   
93   GstElement *main_bin;
94   GstElement *adder;
95   GstElement *audiosink;
96
97   GstPad *pad; /* to request pads for the adder */
98
99   gst_init(&argc,&argv);
100
101   if (argc == 1) {
102     g_print("usage: %s <filename1> <filename2> <...>\n", argv[0]);
103     exit(-1);
104   }
105   num_channels = argc - 1;
106   
107   /* set up output channel and main bin */
108   
109   /* create adder */
110   adder = gst_element_factory_make ("adder", "adderel");
111
112   /* create an audio sink */
113   audiosink = gst_element_factory_make ("esdsink", "play_audio");
114
115   /* create main bin */
116   main_bin = gst_pipeline_new("bin");
117
118   /* link adder and output to bin */
119   GST_INFO ( "main: adding adder to bin");
120   gst_bin_add (GST_BIN(main_bin), adder);
121   GST_INFO ( "main: adding audiosink to bin");
122   gst_bin_add (GST_BIN(main_bin), audiosink);
123
124   /* link adder and audiosink */
125
126   gst_pad_link(gst_element_get_pad(adder,"src"),
127                   gst_element_get_pad(audiosink,"sink"));
128   
129   /* start looping */
130   input_channels = NULL;
131   
132   for (i = 1; i < argc; ++i)
133   {
134     printf ("Opening channel %d from file %s...\n", i, argv[i]);
135     channel_in = create_input_channel (i, argv[i]);
136     input_channels = g_list_append (input_channels, channel_in);
137
138     if (i > 1) gst_element_set_state (main_bin, GST_STATE_PAUSED);
139     gst_bin_add (GST_BIN(main_bin), channel_in->pipe);
140
141     /* request pads and link to adder */
142     GST_INFO ( "requesting pad\n");
143     pad = gst_element_get_request_pad (adder, "sink%d");
144     printf ("\tGot new adder sink pad %s\n", gst_pad_get_name (pad));
145     sprintf (buffer, "channel%d", i);
146     gst_pad_link (gst_element_get_pad (channel_in->pipe, buffer), pad);
147
148     /* register a volume envelope */
149     printf ("\tregistering volume envelope...\n");
150
151     /* 
152      * this is the volenv :
153      * each song gets a slot of 5 seconds, with a 5 second fadeout
154      * at the end of that, all audio streams play simultaneously
155      * at a level ensuring no distortion
156      * example for three songs :
157      * song1 : starts at full level, plays 5 seconds, faded out at 10 seconds,
158      *             sleep until 25, fade to end level at 30
159      * song2 : starts silent, fades in at 5 seconds, full blast at 10 seconds,
160      *             full level until 15, faded out at 20, sleep until 25, fade to end at 30
161      * song3 : starts muted, fades in from 15, full at 20, until 25, fade to end level
162      */
163
164     if (i == 1)
165     {
166       /* first song gets special treatment for end style */
167       env_register_cp (channel_in->volenv,  0.0, 1.0);
168     }
169     else
170     {
171       env_register_cp (channel_in->volenv,  0.0            , 0.0000001); /* start muted */
172       env_register_cp (channel_in->volenv,  i * 10.0 - 15.0, 0.0000001); /* start fade in */
173       env_register_cp (channel_in->volenv,  i * 10.0 - 10.0, 1.0);
174     }
175     env_register_cp (channel_in->volenv,  i * 10.0 -  5.0, 1.0); /* end of full level */
176
177     if (i != num_channels)
178     {
179       env_register_cp (channel_in->volenv,  i * 10.0         , 0.0000001); /* fade to black */
180       env_register_cp (channel_in->volenv,  num_channels * 10.0 - 5.0, 0.0000001); /* start fade in */
181     }   
182     env_register_cp (channel_in->volenv,  num_channels * 10.0      , 1.0 / num_channels); /* to end level */
183
184 #ifndef GST_DISABLE_LOADSAVE
185     gst_xml_write_file (GST_ELEMENT (main_bin), fopen ("mixer.xml", "w"));
186 #endif
187
188     /* start playing */
189     gst_element_set_state(main_bin, GST_STATE_PLAYING);
190
191     /* write out the schedule */
192     gst_scheduler_show(GST_ELEMENT_SCHED(main_bin));
193     playing = TRUE;
194
195     j = 0;
196     /*printf ("main: start iterating from 0"); */
197     while (playing && j < 100) 
198     {
199 /*      printf ("main: iterating %d\n", j); */
200       gst_bin_iterate(GST_BIN(main_bin));
201      /*fprintf(stderr,"after iterate()\n"); */
202       ++j;
203     }
204   }
205   printf ("main: all the channels are open\n");
206   while (playing) 
207   {
208     gst_bin_iterate(GST_BIN(main_bin));
209     /*fprintf(stderr,"after iterate()\n"); */
210   }
211   /* stop the bin */
212   gst_element_set_state(main_bin, GST_STATE_NULL);
213
214   while (input_channels)
215   {
216     destroy_input_channel (input_channels->data);
217     input_channels = g_list_next (input_channels);
218   }
219   g_list_free (input_channels);
220   
221   gst_object_unref(GST_OBJECT(audiosink));
222
223   gst_object_unref(GST_OBJECT(main_bin));
224
225   exit(0);
226 }
227
228 input_channel_t*
229 create_input_channel (int id, char* location)
230 {
231   /* create an input channel, reading from location
232    * return a pointer to the channel
233    * return NULL if failed
234    */
235
236   input_channel_t *channel;
237   
238   char buffer[20];              /* hold the names */
239
240 /*  GstAutoplug *autoplug;
241   GstCaps *srccaps; */
242   GstElement *new_element;  
243   GstElement *decoder;
244
245   GST_DEBUG ( "c_i_p : creating channel with id %d for file %s",
246                   id, location);
247   
248   /* allocate channel */
249
250   channel = (input_channel_t *) malloc (sizeof (input_channel_t));
251   if (channel == NULL)
252   {
253     printf ("create_input_channel : could not allocate memory for channel !\n");
254     return NULL;
255   }
256
257   /* create channel */
258
259   GST_DEBUG ( "c_i_p : creating pipeline");
260
261   sprintf (buffer, "pipeline%d", id);
262   channel->pipe = gst_bin_new (buffer);
263   g_assert(channel->pipe != NULL);    
264     
265   /* create elements */
266
267   GST_DEBUG ( "c_i_p : creating filesrc");
268
269   sprintf (buffer, "filesrc%d", id);
270   channel->filesrc = gst_element_factory_make ("filesrc", buffer);
271   g_assert(channel->filesrc != NULL);    
272
273   GST_DEBUG ( "c_i_p : setting location");
274   g_object_set(G_OBJECT(channel->filesrc),"location", location, NULL);
275
276   /* add filesrc to the bin before autoplug */
277   gst_bin_add(GST_BIN(channel->pipe), channel->filesrc);
278
279   /* link signal to eos of filesrc */
280   g_signal_connect (G_OBJECT(channel->filesrc),"eos",
281                      G_CALLBACK(eos),NULL);
282
283
284 #ifdef DEBUG
285   printf ("DEBUG : c_i_p : creating volume envelope\n");
286 #endif
287
288   sprintf (buffer, "volenv%d", id);
289   channel->volenv = gst_element_factory_make ("volenv", buffer);
290   g_assert(channel->volenv != NULL);    
291
292   /* autoplug the pipe */
293
294 #ifdef DEBUG
295   printf ("DEBUG : c_i_p : getting srccaps\n");
296 #endif
297
298 #ifdef WITH_BUG
299   srccaps = gst_play_type_find (GST_BIN (channel->pipe), channel->filesrc);
300 #endif
301 #ifdef WITH_BUG2
302   {
303     GstElement *pipeline;
304
305     pipeline = gst_pipeline_new ("autoplug_pipeline");
306
307     gst_bin_add (GST_BIN (pipeline), channel->pipe);
308     gst_element_set_state (pipeline, GST_STATE_PLAYING);
309     gst_element_set_state (pipeline, GST_STATE_NULL);
310     gst_bin_remove (GST_BIN (pipeline), channel->pipe);
311     
312   }
313 #endif
314
315 #ifdef AUTOPLUG
316   if (!srccaps) {
317     g_print ("could not autoplug, unknown media type...\n");
318     exit (-1);
319   }
320
321 #ifdef DEBUG
322   printf ("DEBUG : c_i_p : creating autoplug\n");
323 #endif
324
325   autoplug = gst_autoplug_factory_make ("static");
326   g_assert (autoplug != NULL);
327
328 #ifdef DEBUG
329   printf ("DEBUG : c_i_p : autoplugging\n");
330 #endif
331  
332   new_element = gst_autoplug_to_caps (autoplug, srccaps, 
333                                         gst_caps_new ("audio/raw", NULL), NULL);
334  
335   if (!new_element) {
336     g_print ("could not autoplug, no suitable codecs found...\n");
337     exit (-1);
338   }
339
340 #else
341
342   new_element = gst_bin_new ("autoplug_bin");
343
344   /* static plug, use mad plugin and assume mp3 input */
345   printf ("using static plugging for input channel\n");
346   decoder =  gst_element_factory_make ("mad", "mpg123");
347   if (!decoder)
348   {
349     fprintf (stderr, "Could not get a decoder element !\n");
350     exit (1);
351   }
352   gst_bin_add (GST_BIN (new_element), decoder);
353
354   gst_element_add_ghost_pad (new_element, 
355                   gst_element_get_pad (decoder, "sink"), "sink");
356   gst_element_add_ghost_pad (new_element, 
357                   gst_element_get_pad (decoder, "src"), "src_00");
358   
359 #endif  
360 #ifndef GST_DISABLE_LOADSAVE
361   gst_xml_write_file (GST_ELEMENT (new_element), fopen ("mixer.gst", "w"));
362 #endif  
363
364   gst_bin_add (GST_BIN(channel->pipe), channel->volenv);
365   gst_bin_add (GST_BIN (channel->pipe), new_element);
366   
367   gst_element_link_pads (channel->filesrc, "src", new_element, "sink");
368   gst_element_link_pads (new_element, "src_00", channel->volenv, "sink");
369   
370   /* add a ghost pad */
371   sprintf (buffer, "channel%d", id);
372   gst_element_add_ghost_pad (channel->pipe,
373                              gst_element_get_pad (channel->volenv, "src"), buffer);
374
375    
376 #ifdef DEBUG
377   printf ("DEBUG : c_i_p : end function\n");
378 #endif
379
380   return channel;
381 }
382
383 void
384 destroy_input_channel (input_channel_t *channel)
385 {
386   /* 
387    * destroy an input channel
388    */
389    
390 #ifdef DEBUG
391   printf ("DEBUG : d_i_p : start\n");
392 #endif
393
394   /* destroy elements */
395
396   gst_object_unref (GST_OBJECT (channel->pipe));
397
398   free (channel);
399 }
400
401 void env_register_cp (GstElement *volenv, double cp_time, double cp_level)
402 {
403   char buffer[30];
404
405   sprintf (buffer, "%f:%f", cp_time, cp_level);
406   g_object_set(G_OBJECT(volenv), "controlpoint", buffer, NULL);
407
408 }