Merged from INCSCHED on 200505251!!!
[platform/upstream/gstreamer.git] / tests / old / 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/04/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
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 static GstCaps*
44 gst_play_typefind (GstBin *bin, GstElement *element)
45 {
46   GstElement *typefind;
47   GstElement *pipeline;
48   GstCaps *caps = NULL;
49
50   GST_DEBUG (0,"GstPipeline: typefind for element \"%s\"\n",
51              GST_ELEMENT_NAME(element));
52
53   pipeline = gst_pipeline_new ("autoplug_pipeline");
54  
55   typefind = gst_elementfactory_make ("typefind", "typefind");
56   g_return_val_if_fail (typefind != NULL, FALSE);
57
58   gst_pad_connect (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_disconnect (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   gboolean done;
87   
88   char buffer[20];
89   
90   GList *input_channels;                /* structure holding all the input channels */
91   
92   input_channel_t *channel_in;
93   
94   GstElement *main_bin;
95   GstElement *adder;
96   GstElement *audiosink;
97
98   GstPad *pad; /* to request pads for the adder */
99
100   gst_init(&argc,&argv);
101
102   if (argc == 1) {
103     g_print("usage: %s <filename1> <filename2> <...>\n", argv[0]);
104     exit(-1);
105   }
106   num_channels = argc - 1;
107   
108   /* set up output channel and main bin */
109   
110   /* create adder */
111   adder = gst_elementfactory_make ("adder", "adderel");
112
113   /* create an audio sink */
114   audiosink = gst_elementfactory_make ("esdsink", "play_audio");
115
116   /* create main bin */
117   main_bin = gst_pipeline_new("bin");
118
119   /* connect adder and output to bin */
120   GST_INFO (0, "main: adding adder to bin");
121   gst_bin_add (GST_BIN(main_bin), adder);
122   GST_INFO (0, "main: adding audiosink to bin");
123   gst_bin_add (GST_BIN(main_bin), audiosink);
124
125   /* connect adder and audiosink */
126
127   gst_pad_connect(gst_element_get_pad(adder,"src"),
128                   gst_element_get_pad(audiosink,"sink"));
129   
130   /* start looping */
131   input_channels = NULL;
132   
133   for (i = 1; i < argc; ++i)
134   {
135     printf ("Opening channel %d from file %s...\n", i, argv[i]);
136     channel_in = create_input_channel (i, argv[i]);
137     input_channels = g_list_append (input_channels, channel_in);
138
139     if (i > 1) gst_element_set_state (main_bin, GST_STATE_PAUSED);
140     gst_bin_add (GST_BIN(main_bin), channel_in->pipe);
141
142     /* request pads and connect to adder */
143     GST_INFO (0, "requesting pad\n");
144     pad = gst_element_request_pad_by_name (adder, "sink%d");
145     printf ("\tGot new adder sink pad %s\n", gst_pad_get_name (pad));
146     sprintf (buffer, "channel%d", i);
147     gst_pad_connect (gst_element_get_pad (channel_in->pipe, buffer), pad);
148
149     /* register a volume envelope */
150     printf ("\tregistering volume envelope...\n");
151
152     /* 
153      * this is the volenv :
154      * each song gets a slot of 5 seconds, with a 5 second fadeout
155      * at the end of that, all audio streams play simultaneously
156      * at a level ensuring no distortion
157      * example for three songs :
158      * song1 : starts at full level, plays 5 seconds, faded out at 10 seconds,
159      *             sleep until 25, fade to end level at 30
160      * song2 : starts silent, fades in at 5 seconds, full blast at 10 seconds,
161      *             full level until 15, faded out at 20, sleep until 25, fade to end at 30
162      * song3 : starts muted, fades in from 15, full at 20, until 25, fade to end level
163      */
164
165     if (i == 1)
166     {
167       /* first song gets special treatment for end style */
168       env_register_cp (channel_in->volenv,  0.0, 1.0);
169     }
170     else
171     {
172       env_register_cp (channel_in->volenv,  0.0            , 0.0000001); /* start muted */
173       env_register_cp (channel_in->volenv,  i * 10.0 - 15.0, 0.0000001); /* start fade in */
174       env_register_cp (channel_in->volenv,  i * 10.0 - 10.0, 1.0);
175     }
176     env_register_cp (channel_in->volenv,  i * 10.0 -  5.0, 1.0); /* end of full level */
177
178     if (i != num_channels)
179     {
180       env_register_cp (channel_in->volenv,  i * 10.0         , 0.0000001); /* fade to black */
181       env_register_cp (channel_in->volenv,  num_channels * 10.0 - 5.0, 0.0000001); /* start fade in */
182     }   
183     env_register_cp (channel_in->volenv,  num_channels * 10.0      , 1.0 / num_channels); /* to end level */
184
185     xmlSaveFile("mixer.xml", gst_xml_write(GST_ELEMENT(main_bin)));
186
187     /* start playing */
188     gst_element_set_state(main_bin, GST_STATE_PLAYING);
189
190     // write out the schedule
191     gst_schedule_show(GST_ELEMENT_SCHED(main_bin));
192     playing = TRUE;
193
194     j = 0;
195     //printf ("main: start iterating from 0");
196     while (playing && j < 100) 
197     {
198 //      printf ("main: iterating %d\n", j);
199       gst_bin_iterate(GST_BIN(main_bin));
200      //fprintf(stderr,"after iterate()\n");
201       ++j;
202     }
203   }
204   printf ("main: all the channels are open\n");
205   while (playing) 
206   {
207     gst_bin_iterate(GST_BIN(main_bin));
208     //fprintf(stderr,"after iterate()\n");
209   }
210   /* stop the bin */
211   gst_element_set_state(main_bin, GST_STATE_NULL);
212
213   while (input_channels)
214   {
215     destroy_input_channel (input_channels->data);
216     input_channels = g_list_next (input_channels);
217   }
218   g_list_free (input_channels);
219   
220   gst_object_destroy(GST_OBJECT(audiosink));
221
222   gst_object_destroy(GST_OBJECT(main_bin));
223
224   exit(0);
225 }
226
227 input_channel_t*
228 create_input_channel (int id, char* location)
229 {
230   /* create an input channel, reading from location
231    * return a pointer to the channel
232    * return NULL if failed
233    */
234
235   input_channel_t *channel;
236   
237   char buffer[20];              /* hold the names */
238
239   GstAutoplug *autoplug;
240   GstCaps *srccaps;
241   GstElement *new_element;  
242   GstElement *decoder;
243
244   GST_DEBUG (0, "c_i_p : creating channel with id %d for file %s\n",
245                   id, location);
246   
247   /* allocate channel */
248
249   channel = (input_channel_t *) malloc (sizeof (input_channel_t));
250   if (channel == NULL)
251   {
252     printf ("create_input_channel : could not allocate memory for channel !\n");
253     return NULL;
254   }
255
256   /* create channel */
257
258   GST_DEBUG (0, "c_i_p : creating pipeline\n");
259
260   sprintf (buffer, "pipeline%d", id);
261   channel->pipe = gst_bin_new (buffer);
262   g_assert(channel->pipe != NULL);    
263     
264   /* create elements */
265
266   GST_DEBUG(0, "c_i_p : creating disksrc\n");
267
268   sprintf (buffer, "disksrc%d", id);
269   channel->disksrc = gst_elementfactory_make ("disksrc", buffer);
270   g_assert(channel->disksrc != NULL);    
271
272   GST_DEBUG(0, "c_i_p : setting location\n");
273   gtk_object_set(GTK_OBJECT(channel->disksrc),"location", location, NULL);
274
275   /* add disksrc to the bin before autoplug */
276   gst_bin_add(GST_BIN(channel->pipe), channel->disksrc);
277
278   /* connect signal to eos of disksrc */
279   gtk_signal_connect(GTK_OBJECT(channel->disksrc),"eos",
280                      GTK_SIGNAL_FUNC(eos),NULL);
281
282
283 #ifdef DEBUG
284   printf ("DEBUG : c_i_p : creating volume envelope\n");
285 #endif
286
287   sprintf (buffer, "volenv%d", id);
288   channel->volenv = gst_elementfactory_make ("volenv", buffer);
289   g_assert(channel->volenv != NULL);    
290
291   /* autoplug the pipe */
292
293 #ifdef DEBUG
294   printf ("DEBUG : c_i_p : getting srccaps\n");
295 #endif
296
297 #ifdef WITH_BUG
298   srccaps = gst_play_typefind (GST_BIN (channel->pipe), channel->disksrc);
299 #endif
300 #ifdef WITH_BUG2
301   {
302     GstElement *pipeline;
303
304     pipeline = gst_pipeline_new ("autoplug_pipeline");
305
306     gst_bin_add (GST_BIN (pipeline), channel->pipe);
307     gst_element_set_state (pipeline, GST_STATE_PLAYING);
308     gst_element_set_state (pipeline, GST_STATE_NULL);
309     gst_bin_remove (GST_BIN (pipeline), channel->pipe);
310     
311   }
312 #endif
313
314 #ifdef AUTOPLUG
315   if (!srccaps) {
316     g_print ("could not autoplug, unknown media type...\n");
317     exit (-1);
318   }
319
320 #ifdef DEBUG
321   printf ("DEBUG : c_i_p : creating autoplug\n");
322 #endif
323
324   autoplug = gst_autoplugfactory_make ("static");
325   g_assert (autoplug != NULL);
326
327 #ifdef DEBUG
328   printf ("DEBUG : c_i_p : autoplugging\n");
329 #endif
330  
331   new_element = gst_autoplug_to_caps (autoplug, srccaps, 
332                                         gst_caps_new ("audio", "audio/raw", NULL), NULL);
333  
334   if (!new_element) {
335     g_print ("could not autoplug, no suitable codecs found...\n");
336     exit (-1);
337   }
338
339 #else
340
341   new_element = gst_bin_new ("autoplug_bin");
342
343   /* static plug, use mad plugin and assume mp3 input */
344   decoder =  gst_elementfactory_make ("mad", "mpg123");
345
346   gst_bin_add (GST_BIN (new_element), decoder);
347
348   gst_element_add_ghost_pad (new_element, 
349                   gst_element_get_pad (decoder, "sink"), "sink");
350   gst_element_add_ghost_pad (new_element, 
351                   gst_element_get_pad (decoder, "src"), "src_00");
352   
353 #endif  
354   xmlSaveFile ("mixer.gst", gst_xml_write (new_element));
355
356   gst_bin_add (GST_BIN(channel->pipe), channel->volenv);
357   gst_bin_add (GST_BIN (channel->pipe), new_element);
358   
359   gst_element_connect (channel->disksrc, "src", new_element, "sink");
360   gst_element_connect (new_element, "src_00", channel->volenv, "sink");
361   
362   /* add a ghost pad */
363   sprintf (buffer, "channel%d", id);
364   gst_element_add_ghost_pad (channel->pipe,
365                              gst_element_get_pad (channel->volenv, "src"), buffer);
366
367    
368 #ifdef DEBUG
369   printf ("DEBUG : c_i_p : end function\n");
370 #endif
371
372   return channel;
373 }
374
375 void
376 destroy_input_channel (input_channel_t *channel)
377 {
378   /* 
379    * destroy an input channel
380    */
381    
382 #ifdef DEBUG
383   printf ("DEBUG : d_i_p : start\n");
384 #endif
385
386   /* destroy elements */
387
388   gst_object_destroy (GST_OBJECT (channel->pipe));
389
390   free (channel);
391 }
392
393 void env_register_cp (GstElement *volenv, double cp_time, double cp_level)
394 {
395   char buffer[30];
396
397   sprintf (buffer, "%f:%f", cp_time, cp_level);
398   gtk_object_set(GTK_OBJECT(volenv), "controlpoint", buffer, NULL);
399
400 }
401