2 * Copyright (C) 1999-2002 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000-2002 Wim Taymans <wtay@chello.be>
5 * gstsearchfuncs.c: functions needed when doing searches while
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
28 #include "gstsearchfuncs.h"
30 /* FIXME: "evil hack" alarm, we need a better way to get a category in here */
31 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_AUTOPLUG_ATTEMPT);
32 #define GST_CAT_DEFAULT GST_CAT_AUTOPLUG_ATTEMPT
34 /* function that really misses in GLib
35 * though the GLib version should take a function as argument...
38 g_list_free_list_and_elements (GList * list)
44 walk = g_list_next (walk);
50 * gst_autoplug_caps_intersect:
51 * @src: a source #GstCaps
52 * @sink: the sink #GstCaps
54 * Checks if the given caps have a non-null intersection.
56 * Returns: TRUE, if both caps intersect.
59 gst_autoplug_caps_intersect (const GstCaps * src, const GstCaps * sink)
63 /* get an intersection */
64 caps = gst_caps_intersect (src, sink);
66 /* if the caps can't link, there is no intersection */
67 if (gst_caps_is_empty (caps)) {
68 gst_caps_unref (caps);
72 /* hurrah, we can link, now remove the intersection */
73 gst_caps_unref (caps);
78 * gst_autoplug_can_connect_src:
79 * @fac: factory to connect to
82 * Checks if a factory's sink can connect to the given caps
84 * Returns: #GstPadTemplate that can connect to the given caps
87 gst_autoplug_can_connect_src (GstElementFactory * fac, const GstCaps * src)
91 templs = fac->padtemplates;
94 if ((GST_PAD_TEMPLATE_DIRECTION (templs->data) == GST_PAD_SINK) &&
95 gst_autoplug_caps_intersect (src,
96 GST_PAD_TEMPLATE_CAPS (templs->data))) {
97 return GST_PAD_TEMPLATE (templs->data);
99 templs = g_list_next (templs);
106 * gst_autoplug_can_connect_sink:
107 * @fac: factory to connect to
108 * @sink: caps to check
110 * Checks if a factory's src can connect to the given caps
112 * Returns: #GstPadTemplate that can connect to the given caps
115 gst_autoplug_can_connect_sink (GstElementFactory * fac, const GstCaps * sink)
119 templs = fac->padtemplates;
122 GstCaps *caps = GST_PAD_TEMPLATE_CAPS (templs->data);
124 if ((GST_PAD_TEMPLATE_DIRECTION (templs->data) == GST_PAD_SRC) &&
125 gst_autoplug_caps_intersect (caps, sink)) {
126 return GST_PAD_TEMPLATE (templs->data);
128 templs = g_list_next (templs);
135 gst_autoplug_can_match (GstElementFactory * src, GstElementFactory * dest)
137 GList *srctemps, *desttemps;
139 srctemps = src->padtemplates;
142 GstPadTemplate *srctemp = (GstPadTemplate *) srctemps->data;
144 desttemps = dest->padtemplates;
147 GstPadTemplate *desttemp = (GstPadTemplate *) desttemps->data;
149 if (srctemp->direction == GST_PAD_SRC &&
150 desttemp->direction == GST_PAD_SINK) {
151 if (gst_autoplug_caps_intersect (gst_pad_template_get_caps (srctemp),
152 gst_pad_template_get_caps (desttemp))) {
153 GST_DEBUG ("factory \"%s\" can connect with factory \"%s\"",
154 GST_OBJECT_NAME (src), GST_OBJECT_NAME (dest));
159 desttemps = g_list_next (desttemps);
161 srctemps = g_list_next (srctemps);
163 GST_DEBUG ("factory \"%s\" cannot connect with factory \"%s\"",
164 GST_OBJECT_NAME (src), GST_OBJECT_NAME (dest));
168 /* returns TRUE if the factory has padtemplates with the specified direction */
170 gst_autoplug_factory_has_direction (GstElementFactory * fac,
173 GList *templs = fac->padtemplates;
176 if (GST_PAD_TEMPLATE_DIRECTION (templs->data) == dir) {
179 templs = g_list_next (templs);
185 /* Decisions are based on the padtemplates.
186 * These functions return a new list so be sure to free it.
189 gst_autoplug_factories_sinks (GList * factories)
194 if (gst_autoplug_factory_has_sink (factories->data))
195 ret = g_list_prepend (ret, factories->data);
196 factories = g_list_next (factories);
202 gst_autoplug_factories_srcs (GList * factories)
207 if (gst_autoplug_factory_has_src (factories->data))
208 ret = g_list_prepend (ret, factories->data);
209 factories = g_list_next (factories);
215 gst_autoplug_factories_filters (GList * factories)
220 /* if you want it faster do src/sink check at once, don't call two functions */
221 if (gst_autoplug_factory_has_src (factories->data)
222 && gst_autoplug_factory_has_sink (factories->data))
223 ret = g_list_prepend (ret, factories->data);
224 factories = g_list_next (factories);
231 gst_autoplug_rank_compare (const GstElementFactory * a,
232 const GstElementFactory * b)
234 if (GST_PLUGIN_FEATURE (a)->rank > GST_PLUGIN_FEATURE (b)->rank)
236 return (GST_PLUGIN_FEATURE (a)->rank < GST_PLUGIN_FEATURE (b)->rank) ? 1 : 0;
239 /* returns all factories which have sinks with non-NULL caps and srcs with
240 * any caps. also only returns factories with a non-zero rank, and sorts by
244 gst_autoplug_factories_filters_with_sink_caps (GList * factories)
247 GstElementFactory *factory;
251 factory = (GstElementFactory *) factories->data;
252 templs = factory->padtemplates;
254 if (GST_PLUGIN_FEATURE (factory)->rank > 0) {
255 gboolean have_src = FALSE;
256 gboolean have_sink = FALSE;
259 if (GST_PAD_TEMPLATE_DIRECTION (templs->data) == GST_PAD_SRC) {
262 if ((GST_PAD_TEMPLATE_DIRECTION (templs->data) == GST_PAD_SINK)
263 && (GST_PAD_TEMPLATE_CAPS (templs->data) != NULL)) {
266 if (have_src && have_sink) {
267 ret = g_list_prepend (ret, factory);
270 templs = g_list_next (templs);
273 factories = g_list_next (factories);
275 return g_list_sort (ret, (GCompareFunc) gst_autoplug_rank_compare);
280 /* returns all factories which have a maximum of maxtemplates GstPadTemplates in direction dir
283 gst_autoplug_factories_at_most_templates (GList * factories,
284 GstPadDirection dir, guint maxtemplates)
290 GList *templs = ((GstElementFactory *) factories->data)->padtemplates;
293 if (GST_PAD_TEMPLATE_DIRECTION (templs->data) == dir) {
296 if (count > maxtemplates)
298 templs = g_list_next (templs);
300 if (count <= maxtemplates)
301 ret = g_list_prepend (ret, factories->data);
303 factories = g_list_next (factories);
308 /*********************************************************************
310 * SHORTEST PATH ALGORITHM
314 * @src_caps: a #GstCaps to plug from.
315 * @sink_caps: the #GstCaps to plug to.
316 * @factories: a #GList containing all allowed #GstElementFactory entries.
318 * Finds the shortest path of elements that together make up a possible
319 * connection between the source and sink caps.
321 * Returns: a #GList of #GstElementFactory items which have to be connected
322 * to get the shortest path.
325 gst_autoplug_sp (const GstCaps * srccaps, const GstCaps * sinkcaps,
328 GList *factory_nodes = NULL;
329 guint curcost = GST_AUTOPLUG_MAX_COST; /* below this cost, there is no path */
330 GstAutoplugNode *bestnode = NULL; /* best (unconnected) endpoint currently */
332 g_return_val_if_fail (srccaps != NULL, NULL);
333 g_return_val_if_fail (sinkcaps != NULL, NULL);
335 GST_INFO ("attempting to autoplug via shortest path from %"
336 GST_PTR_FORMAT " to %" GST_PTR_FORMAT, srccaps, sinkcaps);
338 /* wrap all factories as GstAutoplugNode
339 * initialize the cost */
341 GstAutoplugNode *node = g_new0 (GstAutoplugNode, 1);
344 node->fac = (GstElementFactory *) factories->data;
345 GST_DEBUG ("trying with %s", node->fac->details.longname);
346 node->templ = gst_autoplug_can_connect_src (node->fac, srccaps);
347 node->cost = (node->templ ? gst_autoplug_get_cost (node->fac)
348 : GST_AUTOPLUG_MAX_COST);
349 node->endpoint = gst_autoplug_can_connect_sink (node->fac, sinkcaps);
350 if (node->templ && node->endpoint)
351 GST_DEBUG ("%s makes connection possible", node->fac->details.longname);
353 GST_DEBUG ("direct connection with %s not possible",
354 node->fac->details.longname);
355 if ((node->endpoint != NULL) &&
356 ((bestnode == NULL) || (node->cost < bestnode->cost))) {
359 factory_nodes = g_list_prepend (factory_nodes, node);
360 /* make curcost the minimum cost of any plugin */
361 curcost = node->cost < curcost ? node->cost : curcost;
362 factories = g_list_next (factories);
365 /* check if we even have possible endpoints */
366 if (bestnode == NULL) {
367 GST_DEBUG ("no factory found that could connect to sink caps");
368 g_list_free_list_and_elements (factory_nodes);
372 /* iterate until we found the best path */
373 while (curcost < GST_AUTOPLUG_MAX_COST) {
374 GList *nodes = factory_nodes;
375 guint nextcost = GST_AUTOPLUG_MAX_COST; /* next cost to check */
377 GST_DEBUG ("iterating at current cost %d, bestnode %s at %d", curcost,
378 GST_OBJECT_NAME (bestnode->fac), bestnode->cost);
379 /* check if we already have a valid best connection to the sink */
380 if (bestnode->cost <= curcost) {
383 GST_DEBUG ("found a way to connect via %s",
384 GST_OBJECT_NAME ((GstObject *) bestnode->fac));
385 /* enter all factories into the return list */
386 ret = g_list_prepend (NULL, bestnode->fac);
387 bestnode = bestnode->prev;
388 while (bestnode != NULL) {
389 ret = g_list_prepend (ret, bestnode->fac);
390 bestnode = bestnode->prev;
392 g_list_free_list_and_elements (factory_nodes);
396 /* iterate over all factories we have
397 * if they have the current cost, calculate if this
398 * factory supplies shorter paths to other elements
401 if (((GstAutoplugNode *) nodes->data)->cost == curcost) {
402 /* now check all elements if we got a shorter path */
403 GList *sinknodes = factory_nodes;
404 GstAutoplugNode *srcnode = (GstAutoplugNode *) nodes->data;
407 GstAutoplugNode *sinknode = (GstAutoplugNode *) sinknodes->data;
408 GstPadTemplate *templ;
410 if ((sinknode->cost >
411 srcnode->cost + gst_autoplug_get_cost (sinknode->fac))
412 && (templ = gst_autoplug_can_match (srcnode->fac, sinknode->fac))) {
413 /* we got a shorter path
414 * now enter that path to that node */
415 sinknode->prev = srcnode;
416 sinknode->templ = templ;
418 srcnode->cost + gst_autoplug_get_cost (sinknode->fac);
419 /* make sure to set which cost to view next */
420 nextcost = (nextcost > sinknode->cost) ? sinknode->cost : nextcost;
421 /* did we get a new best node? */
422 if (sinknode->endpoint && (sinknode->cost < bestnode->cost)) {
426 sinknodes = g_list_next (sinknodes);
428 /* FIXME: for speed remove the item we just iterated with from the factory_nodes
429 * but don't free it yet and don't forget to free it.
432 nodes = g_list_next (nodes);
437 GST_DEBUG ("found no path from source caps to sink caps");
438 g_list_free_list_and_elements (factory_nodes);