cbfc798fa1aa069a1b8b2ff0d998c1fed9d9f0be
[platform/upstream/gstreamer.git] / gst / autoplug / gststaticautoplugrender.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gststaticautoplug.c: A static Autoplugger of pipelines
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 #include "gststaticautoplugrender.h"
24
25 #include <gst/gst.h>
26
27 #define GST_AUTOPLUG_MAX_COST 999999
28
29 typedef guint   (*GstAutoplugCostFunction) (gpointer src, gpointer dest, gpointer data);
30 typedef GList*  (*GstAutoplugListFunction) (gpointer data);
31
32
33 static void             gst_static_autoplug_render_class_init   (GstStaticAutoplugRenderClass *klass);
34 static void             gst_static_autoplug_render_init (GstStaticAutoplugRender *autoplug);
35
36 static GList*           gst_autoplug_func               (gpointer src, gpointer sink,
37                                                          GstAutoplugListFunction list_function,
38                                                          GstAutoplugCostFunction cost_function,
39                                                          gpointer data);
40
41
42
43 static GstElement*      gst_static_autoplug_to_render   (GstAutoplug *autoplug, 
44                                                          GstCaps *srccaps, GstElement *target, va_list args);
45
46 static GstAutoplugClass *parent_class = NULL;
47
48 GtkType gst_static_autoplug_render_get_type(void)
49 {
50   static GtkType static_autoplug_type = 0;
51
52   if (!static_autoplug_type) {
53     static const GtkTypeInfo static_autoplug_info = {
54       "GstStaticAutoplugRender",
55       sizeof(GstElement),
56       sizeof(GstElementClass),
57       (GtkClassInitFunc)gst_static_autoplug_render_class_init,
58       (GtkObjectInitFunc)gst_static_autoplug_render_init,
59       (GtkArgSetFunc)NULL,
60       (GtkArgGetFunc)NULL,
61       (GtkClassInitFunc)NULL,
62     };
63     static_autoplug_type = gtk_type_unique (GST_TYPE_AUTOPLUG, &static_autoplug_info);
64   }
65   return static_autoplug_type;
66 }
67
68 static void
69 gst_static_autoplug_render_class_init(GstStaticAutoplugRenderClass *klass)
70 {
71   GstAutoplugClass *gstautoplug_class;
72
73   gstautoplug_class = (GstAutoplugClass*) klass;
74
75   parent_class = gtk_type_class(GST_TYPE_AUTOPLUG);
76
77   gstautoplug_class->autoplug_to_renderers = gst_static_autoplug_to_render;
78 }
79
80 static void gst_static_autoplug_render_init(GstStaticAutoplugRender *autoplug) {
81 }
82
83 static gboolean
84 plugin_init (GModule *module, GstPlugin *plugin)
85 {
86   GstAutoplugFactory *factory;
87
88   gst_plugin_set_longname (plugin, "A static autoplugger");
89
90   factory = gst_autoplugfactory_new ("staticrender",
91                   "A static autoplugger, it constructs the complete element before running it",
92                   gst_static_autoplug_render_get_type ());
93
94   if (factory != NULL) {
95      gst_plugin_add_autoplugger (plugin, factory);
96   }
97   return TRUE;
98 }
99
100 GstPluginDesc plugin_desc = {
101   GST_VERSION_MAJOR,
102   GST_VERSION_MINOR,
103   "gststaticautoplugrender",
104   plugin_init
105 };
106
107 static gboolean
108 gst_autoplug_can_match (GstElementFactory *src, GstElementFactory *dest)
109 {
110   GList *srctemps, *desttemps;
111
112   srctemps = src->padtemplates;
113
114   while (srctemps) {
115     GstPadTemplate *srctemp = (GstPadTemplate *)srctemps->data;
116
117     desttemps = dest->padtemplates;
118
119     while (desttemps) {
120       GstPadTemplate *desttemp = (GstPadTemplate *)desttemps->data;
121
122       if (srctemp->direction == GST_PAD_SRC &&
123           desttemp->direction == GST_PAD_SINK) {
124         if (gst_caps_check_compatibility (GST_PADTEMPLATE_CAPS (srctemp), GST_PADTEMPLATE_CAPS (desttemp))) {
125           GST_DEBUG (GST_CAT_AUTOPLUG_ATTEMPT,
126                           "factory \"%s\" can connect with factory \"%s\"\n", src->name, dest->name);
127           return TRUE;
128         }
129       }
130
131       desttemps = g_list_next (desttemps);
132     }
133     srctemps = g_list_next (srctemps);
134   }
135   GST_DEBUG (GST_CAT_AUTOPLUG_ATTEMPT,
136                   "factory \"%s\" cannot connect with factory \"%s\"\n", src->name, dest->name);
137   return FALSE;
138 }
139
140 static gboolean
141 gst_autoplug_pads_autoplug_func (GstElement *src, GstPad *pad, GstElement *sink)
142 {
143   GList *sinkpads;
144   gboolean connected = FALSE;
145
146   GST_DEBUG (0,"gstpipeline: autoplug pad connect function for %s %s:%s to \"%s\"\n",
147                   GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME(pad), GST_ELEMENT_NAME(sink));
148
149   sinkpads = gst_element_get_pad_list(sink);
150   while (sinkpads) {
151     GstPad *sinkpad = (GstPad *)sinkpads->data;
152
153     // if we have a match, connect the pads
154     if (gst_pad_get_direction(sinkpad) == GST_PAD_SINK &&
155         !GST_PAD_CONNECTED (pad) && !GST_PAD_CONNECTED(sinkpad))
156     {
157       GstElementState state = GST_STATE (gst_element_get_parent (src));
158
159       if (state == GST_STATE_PLAYING)
160         gst_element_set_state (GST_ELEMENT (gst_element_get_parent (src)), GST_STATE_PAUSED);
161         
162       if ((connected = gst_pad_connect (pad, sinkpad))) {
163         if (state == GST_STATE_PLAYING)
164           gst_element_set_state (GST_ELEMENT (gst_element_get_parent (src)), GST_STATE_PLAYING);
165         break;
166       }
167       else {
168         GST_DEBUG (0,"pads incompatible %s, %s\n", GST_PAD_NAME (pad), GST_PAD_NAME (sinkpad));
169       }
170       if (state == GST_STATE_PLAYING)
171         gst_element_set_state (GST_ELEMENT (gst_element_get_parent (src)), GST_STATE_PLAYING);
172     }
173     sinkpads = g_list_next(sinkpads);
174   }
175
176   if (!connected) {
177     GST_DEBUG (0,"gstpipeline: no path to sinks for type\n");
178   }
179   return connected;
180 }
181
182 static void
183 gst_autoplug_pads_autoplug (GstElement *src, GstElement *sink)
184 {
185   GList *srcpads;
186   gboolean connected = FALSE;
187
188   srcpads = gst_element_get_pad_list(src);
189
190   while (srcpads && !connected) {
191     GstPad *srcpad = (GstPad *)srcpads->data;
192
193     if (gst_pad_get_direction(srcpad) == GST_PAD_SRC) {
194       connected = gst_autoplug_pads_autoplug_func (src, srcpad, sink);
195       if (connected)
196         break;
197     }
198
199     srcpads = g_list_next(srcpads);
200   }
201
202   if (!connected) {
203     GST_DEBUG (0,"gstpipeline: delaying pad connections for \"%s\" to \"%s\"\n",
204                     GST_ELEMENT_NAME(src), GST_ELEMENT_NAME(sink));
205     gtk_signal_connect(GTK_OBJECT(src),"new_pad",
206                  GTK_SIGNAL_FUNC(gst_autoplug_pads_autoplug_func), sink);
207     gtk_signal_connect(GTK_OBJECT(src),"new_ghost_pad",
208                  GTK_SIGNAL_FUNC(gst_autoplug_pads_autoplug_func), sink);
209   }
210 }
211
212 static GList*
213 gst_autoplug_elementfactory_get_list (gpointer data)
214 {
215   return gst_elementfactory_get_list ();
216 }
217
218 typedef struct {
219   GstCaps *src;
220   GstCaps *sink;
221 } caps_struct;
222
223 #define IS_CAPS(cap) (((cap) == caps->src) || (cap) == caps->sink)
224
225 static guint
226 gst_autoplug_caps_find_cost (gpointer src, gpointer dest, gpointer data)
227 {
228   caps_struct *caps = (caps_struct *)data;
229   gboolean res;
230
231   if (IS_CAPS (src) && IS_CAPS (dest)) {
232     res = gst_caps_check_compatibility ((GstCaps *)src, (GstCaps *)dest);
233     //GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"caps %d to caps %d %d", ((GstCaps *)src)->id, ((GstCaps *)dest)->id, res);
234   }
235   else if (IS_CAPS (src)) {
236     res = gst_elementfactory_can_sink_caps ((GstElementFactory *)dest, (GstCaps *)src);
237     //GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"factory %s to src caps %d %d", ((GstElementFactory *)dest)->name, ((GstCaps *)src)->id, res);
238   }
239   else if (IS_CAPS (dest)) {
240     res = gst_elementfactory_can_src_caps ((GstElementFactory *)src, (GstCaps *)dest);
241     //GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"factory %s to sink caps %d %d", ((GstElementFactory *)src)->name, ((GstCaps *)dest)->id, res);
242   }
243   else {
244     res = gst_autoplug_can_match ((GstElementFactory *)src, (GstElementFactory *)dest);
245   }
246
247   if (res)
248     return 1;
249   else
250     return GST_AUTOPLUG_MAX_COST;
251 }
252
253 static GstElement*
254 gst_static_autoplug_to_render (GstAutoplug *autoplug, GstCaps *srccaps, GstElement *target, va_list args)
255 {
256   caps_struct caps;
257   GstElement *targetelement;
258   GstElement *result = NULL, *srcelement = NULL;
259   GList **factories;
260   GList *chains = NULL;
261   GList *endelements = NULL;
262   guint numsinks = 0, i;
263   gboolean have_common = FALSE;
264
265   targetelement = target;
266
267   /*
268    * We first create a list of elements that are needed
269    * to convert the srcpad caps to the different sinkpad caps.
270    * and add the list of elementfactories to a list (chains).
271    */
272   caps.src  = srccaps;
273
274   while (targetelement) {
275     GList *elements;
276     GstRealPad *pad;
277     GstPadTemplate *templ;
278
279     pad = GST_PAD_REALIZE (gst_element_get_pad_list (targetelement)->data);
280     templ = GST_PAD_PADTEMPLATE (pad);
281
282     if (templ)
283       caps.sink = GST_PADTEMPLATE_CAPS (templ);
284     else 
285       goto next;
286
287     GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"autoplugging two caps structures");
288
289     elements =  gst_autoplug_func (caps.src, caps.sink,
290                                    gst_autoplug_elementfactory_get_list,
291                                    gst_autoplug_caps_find_cost,
292                                    &caps);
293
294     if (elements) {
295       chains = g_list_append (chains, elements);
296       endelements = g_list_append (endelements, targetelement);
297       numsinks++;
298     }
299     else {
300     }
301 next:
302     targetelement = va_arg (args, GstElement *);
303   }
304
305   /*
306    * If no list could be found the pipeline cannot be autoplugged and
307    * we return a NULL element
308    */
309   if (numsinks == 0)
310     return NULL;
311
312   /*
313    * We now have a list of lists. We will turn this into an array
314    * of lists, this will make it much more easy to manipulate it
315    * in the next steps.
316    */
317   factories = g_new0 (GList *, numsinks);
318
319   for (i = 0; chains; i++) {
320     GList *elements = (GList *) chains->data;
321
322     factories[i] = elements;
323
324     chains = g_list_next (chains);
325   }
326   //FIXME, free the list
327
328   result = gst_bin_new ("autoplug_bin");
329
330   /*
331    * We now hav a list of lists that is probably like:
332    *
333    *  !
334    *  A -> B -> C
335    *  !
336    *  A -> D -> E
337    *
338    * we now try to find the common elements (A) and add them to
339    * the bin. We remove them from both lists too.
340    */
341   while (factories[0]) {
342     GstElementFactory *factory;
343     GstElement *element;
344
345     // fase 3: add common elements
346     factory = (GstElementFactory *) (factories[0]->data);
347
348     // check to other paths for matching elements (factories)
349     for (i=1; i<numsinks; i++) {
350       if (factory != (GstElementFactory *) (factories[i]->data)) {
351         goto differ;
352       }
353     }
354
355     GST_DEBUG (0,"common factory \"%s\"\n", factory->name);
356
357     element = gst_elementfactory_create (factory, factory->name);
358     gst_bin_add (GST_BIN(result), element);
359
360     if (srcelement != NULL) {
361       gst_autoplug_pads_autoplug (srcelement, element);
362     }
363     // this is the first element, find a good ghostpad
364     else {
365       GList *pads;
366
367       pads = gst_element_get_pad_list (element);
368
369       while (pads) {
370         GstPad *pad = GST_PAD (pads->data);
371         GstPadTemplate *templ = GST_PAD_PADTEMPLATE (pad);
372
373         if (gst_caps_check_compatibility (srccaps, GST_PADTEMPLATE_CAPS (templ))) {
374           gst_element_add_ghost_pad (result, pad, "sink");
375           break;
376         }
377
378         pads = g_list_next (pads);
379       }
380     }
381     gst_autoplug_signal_new_object (GST_AUTOPLUG (autoplug), GST_OBJECT (element));
382
383     srcelement = element;
384
385     // advance the pointer in all lists
386     for (i=0; i<numsinks; i++) {
387       factories[i] = g_list_next (factories[i]);
388     }
389
390     have_common = TRUE;
391   }
392
393 differ:
394
395   // loop over all the sink elements
396   for (i = 0; i < numsinks; i++) {
397     GstElement *thesrcelement = srcelement;
398     GstElement *thebin = GST_ELEMENT(result);
399     GstElement *sinkelement;
400     gboolean use_thread;
401
402     sinkelement = GST_ELEMENT (endelements->data);
403     endelements = g_list_next (endelements);
404
405     use_thread = have_common;
406
407     while (factories[i] || sinkelement) {
408       // fase 4: add other elements...
409       GstElementFactory *factory;
410       GstElement *element;
411
412       if (factories[i]) {
413         factory = (GstElementFactory *)(factories[i]->data);
414
415         GST_DEBUG (0,"factory \"%s\"\n", factory->name);
416         element = gst_elementfactory_create(factory, factory->name);
417       }
418       else {
419         element = sinkelement;
420         sinkelement = NULL;
421       }
422
423       // this element suggests the use of a thread, so we set one up...
424       if (GST_ELEMENT_IS_THREAD_SUGGESTED(element) || use_thread) {
425         GstElement *queue;
426         GstPad *srcpad;
427
428         use_thread = FALSE;
429
430         GST_DEBUG (0,"sugest new thread for \"%s\" %08x\n", GST_ELEMENT_NAME (element), GST_FLAGS(element));
431
432         // create a new queue and add to the previous bin
433         queue = gst_elementfactory_make("queue", g_strconcat("queue_", GST_ELEMENT_NAME(element), NULL));
434         GST_DEBUG (0,"adding element \"%s\"\n", GST_ELEMENT_NAME (element));
435
436         // this will be the new bin for all following elements
437         thebin = gst_elementfactory_make("thread", g_strconcat("thread_", GST_ELEMENT_NAME(element), NULL));
438
439         gst_bin_add(GST_BIN(thebin), queue);
440         gst_autoplug_signal_new_object (GST_AUTOPLUG (autoplug), GST_OBJECT (queue));
441
442         srcpad = gst_element_get_pad(queue, "src");
443
444         gst_autoplug_pads_autoplug(thesrcelement, queue);
445
446         GST_DEBUG (0,"adding element %s\n", GST_ELEMENT_NAME (element));
447         gst_bin_add(GST_BIN(thebin), element);
448         gst_autoplug_signal_new_object (GST_AUTOPLUG (autoplug), GST_OBJECT (element));
449         GST_DEBUG (0,"adding element %s\n", GST_ELEMENT_NAME (thebin));
450         gst_bin_add(GST_BIN(result), thebin);
451         gst_autoplug_signal_new_object (GST_AUTOPLUG (autoplug), GST_OBJECT (thebin));
452         thesrcelement = queue;
453       }
454       // no thread needed, easy case
455       else {
456         GST_DEBUG (0,"adding element %s\n", GST_ELEMENT_NAME (element));
457         gst_bin_add(GST_BIN(thebin), element);
458         gst_autoplug_signal_new_object (GST_AUTOPLUG (autoplug), GST_OBJECT (element));
459       }
460       gst_autoplug_pads_autoplug(thesrcelement, element);
461
462       // this element is now the new source element
463       thesrcelement = element;
464
465       factories[i] = g_list_next(factories[i]);
466     }
467   }
468
469   return result;
470 }
471
472 /*
473  * shortest path algorithm
474  *
475  */
476 struct _gst_autoplug_node
477 {
478   gpointer iNode;
479   gpointer iPrev;
480   gint iDist;
481 };
482
483 typedef struct _gst_autoplug_node gst_autoplug_node;
484
485 static gint
486 find_factory (gst_autoplug_node *rgnNodes, gpointer factory)
487 {
488   gint i=0;
489
490   while (rgnNodes[i].iNode) {
491     if (rgnNodes[i].iNode == factory) return i;
492     i++;
493   }
494   return 0;
495 }
496
497 static GList*
498 construct_path (gst_autoplug_node *rgnNodes, gpointer factory)
499 {
500   GstElementFactory *current;
501   GList *factories = NULL;
502
503   current = rgnNodes[find_factory(rgnNodes, factory)].iPrev;
504
505   GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"factories found in autoplugging (reversed order)");
506
507   while (current != NULL)
508   {
509     gpointer next = NULL;
510
511     next = rgnNodes[find_factory(rgnNodes, current)].iPrev;
512     if (next) {
513       factories = g_list_prepend (factories, current);
514       GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"factory: \"%s\"", current->name);
515     }
516     current = next;
517   }
518   return factories;
519 }
520
521 static GList*
522 gst_autoplug_enqueue (GList *queue, gpointer iNode, gint iDist, gpointer iPrev)
523 {
524   gst_autoplug_node *node = g_malloc (sizeof (gst_autoplug_node));
525
526   node->iNode = iNode;
527   node->iDist = iDist;
528   node->iPrev = iPrev;
529
530   queue = g_list_append (queue, node);
531
532   return queue;
533 }
534
535 static GList*
536 gst_autoplug_dequeue (GList *queue, gpointer *iNode, gint *iDist, gpointer *iPrev)
537 {
538   GList *head;
539   gst_autoplug_node *node;
540
541   head = g_list_first (queue);
542
543   if (head) {
544     node = (gst_autoplug_node *)head->data;
545     *iNode = node->iNode;
546     *iPrev = node->iPrev;
547     *iDist = node->iDist;
548     head = g_list_remove (queue, node);
549   }
550
551   return head;
552 }
553
554 static GList*
555 gst_autoplug_func (gpointer src, gpointer sink,
556                    GstAutoplugListFunction list_function,
557                    GstAutoplugCostFunction cost_function,
558                    gpointer data)
559 {
560   gst_autoplug_node *rgnNodes;
561   GList *queue = NULL;
562   gpointer iNode, iPrev;
563   gint iDist, i, iCost;
564
565   GList *elements = g_list_copy (list_function(data));
566   GList *factories;
567   guint num_factories;
568
569   elements = g_list_append (elements, sink);
570   elements = g_list_append (elements, src);
571
572   factories = elements;
573
574   num_factories = g_list_length (factories);
575
576   rgnNodes = g_new0 (gst_autoplug_node, num_factories+1);
577
578   for (i=0; i< num_factories; i++) {
579     gpointer fact = factories->data;
580
581     rgnNodes[i].iNode = fact;
582     rgnNodes[i].iPrev = NULL;
583
584     if (fact == src) {
585       rgnNodes[i].iDist = 0;
586     }
587     else {
588       rgnNodes[i].iDist = GST_AUTOPLUG_MAX_COST;
589     }
590
591     factories = g_list_next (factories);
592   }
593   rgnNodes[num_factories].iNode = NULL;
594
595   queue = gst_autoplug_enqueue (queue, src, 0, NULL);
596
597   while (g_list_length (queue) > 0) {
598     GList *factories2 = elements;
599
600     queue = gst_autoplug_dequeue (queue, &iNode, &iDist, &iPrev);
601
602     for (i=0; i< num_factories; i++) {
603       gpointer current = factories2->data;
604
605       iCost = cost_function (iNode, current, data);
606       if (iCost != GST_AUTOPLUG_MAX_COST) {
607         if ((GST_AUTOPLUG_MAX_COST == rgnNodes[i].iDist) ||
608             (rgnNodes[i].iDist > (iCost + iDist))) {
609           rgnNodes[i].iDist = iDist + iCost;
610           rgnNodes[i].iPrev = iNode;
611
612           queue = gst_autoplug_enqueue (queue, current, iDist + iCost, iNode);
613         }
614       }
615
616       factories2 = g_list_next (factories2);
617     }
618   }
619
620   return construct_path (rgnNodes, sink);
621 }
622