summaryrefslogtreecommitdiff
path: root/gst/autoplug/gstspider.c
blob: 2db44ba11cbb522bf65375e1449542d9c73edb58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/* GStreamer
 * Copyright (C) 2002 Erik Walthinsen <omega@cse.ogi.edu>
 *               2002 Wim Taymans <wtay@chello.be>
 *
 * gstspider.c: element to automatically link sinks and sources
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
/*
 * TODO:
 * - handle automatic removal of unneeded elements
 * - make the spider handle and send events (esp. new media)
 * - decide if we plug pads or elements, currently it's a mess
 * - allow unlinking
 * - implement proper saving/loading from xml
 * - implement a way to allow merging/splitting (aka tee)
 * - find ways to define which elements to use when plugging
 * - remove pads
 * - improve typefinding
 * - react to errors inside the pipeline
 * - implement more properties, change the current
 * - emit signals (most important: "NOT PLUGGABLE")
 * - implement something for reporting the state of the spider
 *   to allow easier debugging.
 *   (could be useful for bins in general)
 * - fix bugs
 * ...
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "gstspider.h"
#include "gstspideridentity.h"
#include "gstsearchfuncs.h"

GST_DEBUG_CATEGORY (gst_spider_debug);
#define GST_CAT_DEFAULT gst_spider_debug

/* signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_FACTORIES,
  /* FILL ME TOO */
};

/* generic templates */
GST_PAD_TEMPLATE_FACTORY (spider_src_factory,
  "src_%d",
  GST_PAD_SRC,
  GST_PAD_REQUEST,
  NULL      /* no caps */
);

/* standard GObject stuff */
static void                     gst_spider_class_init                   (GstSpiderClass *klass);
static void                     gst_spider_init                         (GstSpider *spider);
static void                     gst_spider_dispose                      (GObject *object);

/* element class functions */
static GstPad*                  gst_spider_request_new_pad              (GstElement *element, GstPadTemplate *templ, const gchar *name);
static void			gst_spider_set_property                 (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void			gst_spider_get_property			(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);

/* link functions */
static GstSpiderConnection *	gst_spider_link_new		(GstSpiderIdentity *src);
static void			gst_spider_link_destroy		(GstSpiderConnection *conn);
static void			gst_spider_link_reset		(GstSpiderConnection *conn, GstElement *to);
static void			gst_spider_link_add		(GstSpiderConnection *conn, GstElement *element);
static GstSpiderConnection *	gst_spider_link_find		(GstSpiderIdentity *src);
static GstSpiderConnection *	gst_spider_link_get		(GstSpiderIdentity *src);

/* autoplugging functions */
static GstElement *             gst_spider_find_element_to_plug      	(GstElement *src, GstElementFactory *fac, GstPadDirection dir);
static GstPadLinkReturn		gst_spider_plug				(GstSpiderConnection *conn);
static GstPadLinkReturn		gst_spider_plug_from_srcpad		(GstSpiderConnection *conn, GstPad *srcpad);
/*static GstPadLinkReturn      gst_spider_plug_peers			(GstSpider *spider, GstPad *srcpad, GstPad *sinkpad); */
static GstPadLinkReturn		gst_spider_create_and_plug		(GstSpiderConnection *conn, GList *plugpath);

/* random functions */
static gchar *			gst_spider_unused_elementname		(GstBin *bin, const gchar *startwith);

/* debugging stuff
static void			print_spider_contents			(GstSpider *spider);
static void			print_spider_link			(GstSpiderConnection *conn); */

/* === variables === */
static                          GstElementClass	*			parent_class = NULL;

/* no signals yet
static guint gst_spider_signals[LAST_SIGNAL] = { 0 };*/

/* GObject and GStreamer init functions */
GType
gst_spider_get_type(void)
{
  static GType spider_type = 0;

  if (!spider_type) {
    static const GTypeInfo spider_info = {
      sizeof(GstSpiderClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_spider_class_init,
      NULL,
      NULL,
      sizeof(GstSpider),
      0,
      (GInstanceInitFunc)gst_spider_init,
    };
    spider_type = g_type_register_static (GST_TYPE_BIN, "GstSpider", &spider_info, 0);
  }
  return spider_type;
}

static void
gst_spider_class_init (GstSpiderClass *klass)
{
  GObjectClass     *gobject_class;
  GstElementClass  *gstelement_class;

  gobject_class = (GObjectClass*) klass;
  gstelement_class = (GstElementClass*) klass;

  parent_class = g_type_class_ref(GST_TYPE_BIN);

  /* properties */
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FACTORIES,
  g_param_spec_pointer ("factories", "allowed factories", "allowed factories for autoplugging", G_PARAM_READWRITE));

  gobject_class->set_property = gst_spider_set_property;
  gobject_class->get_property = gst_spider_get_property;
  gobject_class->dispose = gst_spider_dispose;

  gst_element_class_add_pad_template (gstelement_class, GST_PAD_TEMPLATE_GET (spider_src_factory));
  
  gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_spider_request_new_pad);
}
static void 
gst_spider_init (GstSpider *spider) 
{
  /* use only elements which have sources and sinks and where the sinks have caps */
  /* FIXME: How do we handle factories that are added after the spider was constructed? */
  spider->factories = gst_autoplug_factories_filters_with_sink_caps ((GList *) 
		  gst_registry_pool_feature_list (GST_TYPE_ELEMENT_FACTORY));

  spider->links = NULL;

  spider->sink_ident = gst_spider_identity_new_sink ("sink_ident");
  gst_bin_add (GST_BIN (spider), GST_ELEMENT (spider->sink_ident));
  gst_element_add_ghost_pad (GST_ELEMENT(spider), spider->sink_ident->sink, "sink");
  
}

static void
gst_spider_dispose (GObject *object)
{
  GstSpider *spider;
  
  spider = GST_SPIDER (object);
  g_list_free (spider->factories);
  
  ((GObjectClass *) parent_class)->dispose (object);
}
static GstPad *
gst_spider_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *name)
{
  GstPad *returnpad;
  gchar *padname;
  GstSpiderIdentity *identity;
  GstSpider *spider;
  
  g_return_val_if_fail (templ != NULL, NULL);
  g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
  g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC, NULL);
  
  spider = GST_SPIDER (element);
  
  /* create an identity object, so we have a pad */
  padname = gst_spider_unused_elementname ((GstBin *)spider, "src_");
  identity = gst_spider_identity_new_src (padname);
  returnpad = identity->src;
  
  /* FIXME: use the requested name for the pad */

  gst_object_replace ((GstObject **) &returnpad->padtemplate, (GstObject *) templ);
  
  gst_bin_add (GST_BIN (element), GST_ELEMENT (identity));
  
  returnpad = gst_element_add_ghost_pad (element, returnpad, padname);
  gst_spider_link_new (identity);
  GST_DEBUG ("successuflly created requested pad %s:%s", GST_DEBUG_PAD_NAME (returnpad));
  
  return returnpad;
}

static void
gst_spider_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
  GstSpider *spider;
  GList *list;
  
  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_SPIDER (object));

  spider = GST_SPIDER (object);

  switch (prop_id) {
    case ARG_FACTORIES:
      list = (GList *) g_value_get_pointer (value);
      while (list)
      {
	g_return_if_fail (list->data != NULL);
	g_return_if_fail (GST_IS_ELEMENT_FACTORY (list->data));
	list = g_list_next (list);
      }
      g_list_free (spider->factories);
      spider->factories = (GList *) g_value_get_pointer (value);
      break;
    default:
      break;
  }
}
static void
gst_spider_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
  GstSpider *spider;

  /* it's not null if we got it, but it might not be ours */
  spider = GST_SPIDER(object);

  switch (prop_id) {
    case ARG_FACTORIES:
      g_value_set_pointer (value, spider->factories);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}
/* get a name for an element that isn't used yet */
static gchar *
gst_spider_unused_elementname (GstBin *bin, const gchar *startwith)
{
  gchar * name = g_strdup_printf ("%s%d", startwith, 0);
  guint i;
  
  for (i = 0; gst_bin_get_by_name (bin, name) != NULL; )
  {
    g_free (name);
    name = g_strdup_printf ("%s%d", startwith, ++i);
  }
  
  return name;
}
static void
gst_spider_link_sometimes (GstElement *src, GstPad *pad, GstSpiderConnection *conn)
{
  gulong signal_id = conn->signal_id;
  
  /* try to autoplug the elements */
  if (gst_spider_plug_from_srcpad (conn, pad) != GST_PAD_LINK_REFUSED) {
    GST_DEBUG ("%s:%s was autoplugged to %s:%s, removing callback", 
	       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (conn->src->sink));
    g_signal_handler_disconnect (src, signal_id);
    signal_id = 0;
  }
}
/* create a new link from those two elements */
static GstSpiderConnection *
gst_spider_link_new (GstSpiderIdentity *src)
{
  GstSpider *spider = GST_SPIDER (GST_OBJECT_PARENT (src));
  
  GstSpiderConnection *conn = g_new0 (GstSpiderConnection, 1);
  conn->src = src;
  conn->path = NULL;
  conn->current = (GstElement *) spider->sink_ident;
  spider->links = g_list_prepend (spider->links, conn);
  
  return conn;
}
static void
gst_spider_link_destroy (GstSpiderConnection *conn)
{
  GstSpider *spider = GST_SPIDER (GST_OBJECT_PARENT (conn->src));
  /* reset link to unplugged */
  gst_spider_link_reset (conn, (GstElement *) spider->sink_ident);
  g_free (conn);
}
static void
gst_spider_link_reset (GstSpiderConnection *conn, GstElement *to)
{
  GstSpider *spider = GST_SPIDER (GST_OBJECT_PARENT (conn->src));
  GST_DEBUG ("resetting link from %s to %s, currently at %s to %s", GST_ELEMENT_NAME (spider->sink_ident), 
	     GST_ELEMENT_NAME (conn->src), GST_ELEMENT_NAME (conn->current), GST_ELEMENT_NAME (to));
  while ((conn->path != NULL) && ((GstElement *) conn->path->data != to))
  {
    gst_object_unref ((GstObject *) conn->path->data);
    conn->path = g_list_delete_link (conn->path, conn->path);
  }
  if (conn->path == NULL)
  {
    conn->current = (GstElement *) spider->sink_ident;
  } else {
    conn->current = to;
  }
}
/* add an element to the link */
static void
gst_spider_link_add (GstSpiderConnection *conn, GstElement *element)
{
  gst_object_ref ((GstObject *) element);
  gst_object_sink ((GstObject *) element);
  conn->path = g_list_prepend (conn->path, element);
  conn->current = element;
}
/* find the link from those two elements */
static GstSpiderConnection *
gst_spider_link_find (GstSpiderIdentity *src)
{
  GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (src);
  GList *list = spider->links;
  
  while (list)
  {
    GstSpiderConnection *conn = (GstSpiderConnection *) list->data;
    if (conn->src == src){
      return conn;
    }
    list = g_list_next(list);
  }
  return NULL;
}
/* get a new link from those two elements
 * search first; if none is found, create a new one */
static GstSpiderConnection *
gst_spider_link_get (GstSpiderIdentity *src)
{
  GstSpiderConnection *ret;
  
  if ((ret = gst_spider_link_find (src)) != NULL)
  {
    return ret;
  }
  return gst_spider_link_new (src);
}  
void			
gst_spider_identity_plug (GstSpiderIdentity *ident)
{
  GstSpider *spider;
  const GList *padlist;
  GstPadDirection dir;
  GstSpiderConnection *conn;
  
  /* checks */
  g_return_if_fail (ident != NULL);
  g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
  spider = GST_SPIDER (GST_ELEMENT_PARENT (ident));
  g_assert (spider != NULL);
  g_assert (GST_IS_SPIDER (spider));
  
  /* return if we're already plugged */
  if (ident->plugged) return;

  /* get the direction of our ident */
  if (GST_PAD_PEER (ident->sink))
  {
    if (GST_PAD_PEER (ident->src))
    {
      /* Hey, the ident is linked on both sides */
      g_warning ("Trying to autoplug a linked element. Aborting...");
      return;
    } else {
      dir = GST_PAD_SINK;
    }
  } else {
    if (GST_PAD_PEER (ident->src))
    {
      dir = GST_PAD_SRC;
    } else {
      /* the ident isn't linked on either side */
      g_warning ("Trying to autoplug an unlinked element. Aborting...");
      return;
    }
  }

  /* now iterate all possible pads and link when needed */
  padlist = gst_element_get_pad_list (GST_ELEMENT (spider));
  while (padlist)
  {
    GstPad *otherpad;
    GstSpiderIdentity *peer;

    g_assert (GST_IS_PAD (padlist->data));
    otherpad = (GstPad *) GST_GPAD_REALPAD (padlist->data);
    peer = (GstSpiderIdentity *) GST_PAD_PARENT (otherpad);
    /* we only want to link to the other side */
    if (dir != GST_PAD_DIRECTION (otherpad))
    {
      /* we only link to plugged in elements */
      if (peer->plugged == TRUE) 
      {
        /* plug in the right direction */
        if (dir == GST_PAD_SINK)
        {
	  conn = gst_spider_link_get (peer);
        } else {
	  conn = gst_spider_link_get (ident);
        }
	if ((GstElement *) spider->sink_ident == conn->current)
	{
	  gst_spider_plug (conn);
	}
      }
    }
    padlist = g_list_next (padlist);
  }
  
  ident->plugged = TRUE; 
}
void
gst_spider_identity_unplug (GstSpiderIdentity *ident)
{
  GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (ident);
  GList *list = spider->links;
  
  while (list)
  {
    GstSpiderConnection *conn = list->data;
    GList *cur = list;
    list = g_list_next (list);
    if (conn->src == ident)
    {
      g_list_delete_link (spider->links, cur);
      gst_spider_link_destroy (conn);
    }
  }
  ident->plugged = FALSE;
}
/* links src to sink using the elementfactories in plugpath
 * plugpath will be removed afterwards */
static GstPadLinkReturn
gst_spider_create_and_plug (GstSpiderConnection *conn, GList *plugpath)
{
  GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (conn->src);
  GList *endelements = NULL, *templist = NULL;
  GstElement *element;
  
  /* exit if plugging is already done */
  if ((GstElement *) conn->src == conn->current)
    return GST_PAD_LINK_DONE;
  
  /* try to shorten the list at the end and not duplicate link code */
  if (plugpath != NULL)
  {
    templist = g_list_last (plugpath);
    element = (GstElement *) conn->src;
    while ((plugpath != NULL) && (element = gst_spider_find_element_to_plug (element, (GstElementFactory *) plugpath->data, GST_PAD_SINK)))
    {
      GList *cur = templist;
      endelements = g_list_prepend (endelements, element);
      templist = g_list_previous (templist);
      g_list_delete_link (cur, cur);    
    }
  }
  
  /* do the linking */
  while (conn->current != (GstElement *) (endelements == NULL ? conn->src : endelements->data))
  {
    /* get sink element to plug, src is conn->current */
    if (plugpath == NULL)
    {
      element = (GstElement *) (endelements == NULL ? conn->src : endelements->data);
    } else {
      element = gst_element_factory_create ((GstElementFactory *) plugpath->data, NULL);
      GST_DEBUG ("Adding element %s of type %s and syncing state with autoplugger", 
                 GST_ELEMENT_NAME (element), GST_PLUGIN_FEATURE_NAME (plugpath->data));    
      gst_bin_add (GST_BIN (spider), element);
    }
    /* insert and link new element */
    if (gst_element_link (conn->current, element)) {
      gst_element_sync_state_with_parent (element);    
    } else {
      /* check if the src has SOMETIMES templates. If so, link a callback */
      GList *templs = gst_element_get_pad_template_list (conn->current);
	 
      /* remove element that couldn't be linked, if it wasn't the endpoint */
      if (element != (GstElement *) conn->src)
        gst_bin_remove (GST_BIN (spider), element);
    
      while (templs) {
        GstPadTemplate *templ = (GstPadTemplate *) templs->data;
        if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) && (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
        {
          GST_DEBUG ("adding callback to link element %s to %s", GST_ELEMENT_NAME (conn->current), GST_ELEMENT_NAME (conn->src));
          conn->signal_id = g_signal_connect (G_OBJECT (conn->current), "new_pad", 
					      G_CALLBACK (gst_spider_link_sometimes), conn);
	  g_list_free (plugpath);
	  return GST_PAD_LINK_DELAYED;
        }
        templs = g_list_next (templs);
      }
      GST_DEBUG ("no chance to link element %s to %s", GST_ELEMENT_NAME (conn->current), GST_ELEMENT_NAME (conn->src));
      g_list_free (plugpath);
      return GST_PAD_LINK_REFUSED;
    }
    GST_DEBUG ("added element %s and attached it to element %s", GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (conn->current));
    gst_spider_link_add (conn, element);
    if (plugpath != NULL)
      plugpath = g_list_delete_link (plugpath, plugpath);
  }
  
  /* ref all elements at the end */
  while (endelements)
  {
    gst_spider_link_add (conn, endelements->data);
    endelements = g_list_delete_link (endelements, endelements);
  }
  
  return GST_PAD_LINK_DONE;
}
/* checks, if src is already linked to an element from factory fac on direction dir */
static GstElement *
gst_spider_find_element_to_plug (GstElement *src, GstElementFactory *fac, GstPadDirection dir)
{
  GList *padlist = GST_ELEMENT_PADS (src);
  
  while (padlist)
  {
    GstPad *pad = (GstPad *) GST_PAD_REALIZE (padlist->data);
    /* is the pad on the right side and is it linked? */
    if ((GST_PAD_DIRECTION (pad) == dir) && (pad = (GstPad *) (GST_RPAD_PEER (pad))))
    {
      /* is the element the pad is linked to of the right type? */
      GstElement *element = GST_PAD_PARENT (pad);
      if (GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS (element))->elementfactory == fac) {
        return element;
      }
    }
    padlist = g_list_next (padlist);
  }
  
  return NULL;
}
/* try to establish the link */
static GstPadLinkReturn
gst_spider_plug	(GstSpiderConnection *conn)
{
  GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (conn->src);
  if ((GstElement *) conn->src == conn->current)
    return GST_PAD_LINK_DONE;
  if ((GstElement *) spider->sink_ident == conn->current)
    return gst_spider_plug_from_srcpad (conn, spider->sink_ident->src);
  g_warning ("FIXME: autoplugging only possible from GstSpiderIdentity conn->sink yet (yep, that's technical)\n");
  return GST_PAD_LINK_REFUSED;
}
/* try to establish the link using this pad */
static GstPadLinkReturn
gst_spider_plug_from_srcpad (GstSpiderConnection *conn, GstPad *srcpad)
{
  GstElement *element;
  GList *plugpath;
  gboolean result = TRUE;
  GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (conn->src);
  GstElement *startelement = conn->current;

  g_assert ((GstElement *) GST_OBJECT_PARENT (srcpad) == conn->current);
  GST_DEBUG ("trying to plug from %s:%s to %s", 
	     GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (conn->src));
  
  /* find a path from src to sink */
  plugpath = gst_autoplug_sp (gst_pad_get_caps (srcpad), gst_pad_get_caps (conn->src->sink), spider->factories);
  
  /* prints out the path that was found for plugging */
  /* g_print ("found path from %s to %s:\n", GST_ELEMENT_NAME (conn->current), GST_ELEMENT_NAME (conn->src));
  templist = plugpath;
  while (templist)
  {
    g_print("%s\n", GST_OBJECT_NAME (templist->data));
    templist = g_list_next (templist);
  }*/
    
  /* if there is no way to plug: return */
  if (plugpath == NULL) {
    GST_DEBUG ("no chance to plug from %s to %s", GST_ELEMENT_NAME (conn->current), GST_ELEMENT_NAME (conn->src));
    return GST_PAD_LINK_REFUSED;
  }
  GST_DEBUG ("found a link that needs %d elements", g_list_length (plugpath));

  /* now remove non-needed elements from the beginning of the path 
   * alter src to point to the new element where we need to start 
   * plugging and alter the plugpath to represent the elements, that must be plugged
   */
  element = conn->current;
  while ((plugpath != NULL) && (element = gst_spider_find_element_to_plug (element, (GstElementFactory *) plugpath->data, GST_PAD_SRC)))
  {
    gst_spider_link_add (conn, element);
    plugpath = g_list_delete_link (plugpath, plugpath);
  }
  
  GST_DEBUG ("%d elements must be inserted to establish the link", g_list_length (plugpath));
  /* create the elements and plug them */
  result = gst_spider_create_and_plug (conn, plugpath);
  
  /* reset the "current" element */
  if (result == GST_PAD_LINK_REFUSED)
  {
     gst_spider_link_reset (conn, startelement);
  }
  
  return result;  
}

GstElementDetails gst_spider_details = {
  "Spider",
  "Generic",
  "LGPL",
  "Automatically link sinks and sources",
  VERSION,
  "Benjamin Otte <in7y118@public.uni-hamburg.de>",
  "(C) 2002",
};

static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
  GstElementFactory *factory;

  GST_DEBUG_CATEGORY_INIT (gst_spider_debug, "spider", 0, "spider autoplugging element");
  GST_DEBUG_CATEGORY_INIT (gst_spider_identity_debug, "spideridentity", 0, "spider autoplugging proxy element");

  factory = gst_element_factory_new("spider", GST_TYPE_SPIDER,
                                   &gst_spider_details);
  gst_plugin_set_longname (plugin, "Spider autoplugging elements");
  g_return_val_if_fail(factory != NULL, FALSE);

  gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (spider_src_factory));

  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

  /* add spideridentity */
  factory = gst_element_factory_new ("spideridentity", GST_TYPE_SPIDER_IDENTITY,
                                     &gst_spider_identity_details);
  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

  return TRUE;
}

GstPluginDesc plugin_desc = {
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
  "gstspider",
  plugin_init
};