gstreamer/gst/gstautoplug.c
Wim Taymans 5a52fd4009 Updated the autoplugger:
Original commit message from CVS:
Updated the autoplugger:
- moved all of the code out of gstpipeline.c to autoplug.c
- The autoplugger now creates a GstElement based on the given src and
sink caps. The API now is:
GstElement* gst_autoplug_caps_list (GList *srcpad, GList *sinkpad, ...);
- the typefind function is moved from gstpipeline.c to gstplay.c. Not sure
if this is right behaviour but we need at least a bin in order to run
the typedetect functions.
- fixed a bug in gstbin.c where the MANAGER flag of the bin was not cleared
when going to the NULL state.
- a bug in the videosink was fixed. It was possible that more instances
of the video widget were created, causing major errors.
- commented out most of the 'old' autoplug examples.
- added the new autoplugger to gstplay.c. There still is a bit of hacking
needed to insert a queue into the autogenerated element. This will be fixed
when the autoplugger can also create the video/audio elements.
Fixed some more extranous spaces problems in various files.
2001-02-06 20:42:28 +00:00

594 lines
16 KiB
C

/* GStreamer
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
* 2000 Wim Taymans <wtay@chello.be>
*
* gstautoplug.c: Autoplugging of pipelines
*
* 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.
*/
//#define GST_DEBUG_ENABLED
#include "gst_private.h"
#include "gstautoplug.h"
#include "gstbin.h"
static void gst_autoplug_class_init (GstAutoplugClass *klass);
static void gst_autoplug_init (GstAutoplug *autoplug);
static GList* gst_autoplug_func (gpointer src, gpointer sink,
GstAutoplugListFunction list_function,
GstAutoplugCostFunction cost_function,
gpointer data);
struct _gst_autoplug_node
{
gpointer iNode;
gpointer iPrev;
gint iDist;
};
typedef struct _gst_autoplug_node gst_autoplug_node;
static GstObjectClass *parent_class = NULL;
GtkType gst_autoplug_get_type(void) {
static GtkType autoplug_type = 0;
if (!autoplug_type) {
static const GtkTypeInfo autoplug_info = {
"GstAutoplug",
sizeof(GstElement),
sizeof(GstElementClass),
(GtkClassInitFunc)gst_autoplug_class_init,
(GtkObjectInitFunc)gst_autoplug_init,
(GtkArgSetFunc)NULL,
(GtkArgGetFunc)NULL,
(GtkClassInitFunc)NULL,
};
autoplug_type = gtk_type_unique(GST_TYPE_AUTOPLUG,&autoplug_info);
}
return autoplug_type;
}
static void
gst_autoplug_class_init(GstAutoplugClass *klass) {
parent_class = gtk_type_class(GST_TYPE_OBJECT);
}
static void gst_autoplug_init(GstAutoplug *autoplug) {
}
static gboolean
gst_autoplug_can_match (GstElementFactory *src, GstElementFactory *dest)
{
GList *srctemps, *desttemps;
srctemps = src->padtemplates;
while (srctemps) {
GstPadTemplate *srctemp = (GstPadTemplate *)srctemps->data;
desttemps = dest->padtemplates;
while (desttemps) {
GstPadTemplate *desttemp = (GstPadTemplate *)desttemps->data;
if (srctemp->direction == GST_PAD_SRC &&
desttemp->direction == GST_PAD_SINK) {
if (gst_caps_list_check_compatibility (srctemp->caps, desttemp->caps)) {
GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,
"factory \"%s\" can connect with factory \"%s\"", src->name, dest->name);
return TRUE;
}
}
desttemps = g_list_next (desttemps);
}
srctemps = g_list_next (srctemps);
}
GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,
"factory \"%s\" cannot connect with factory \"%s\"", src->name, dest->name);
return FALSE;
}
static gboolean
gst_autoplug_pads_autoplug_func (GstElement *src, GstPad *pad, GstElement *sink)
{
GList *sinkpads;
gboolean connected = FALSE;
GST_DEBUG (0,"gstpipeline: autoplug pad connect function for \"%s\" to \"%s\"\n",
GST_ELEMENT_NAME(src), GST_ELEMENT_NAME(sink));
sinkpads = gst_element_get_pad_list(sink);
while (sinkpads) {
GstPad *sinkpad = (GstPad *)sinkpads->data;
// if we have a match, connect the pads
if (gst_pad_get_direction(sinkpad) == GST_PAD_SINK &&
!GST_PAD_CONNECTED(sinkpad))
{
if (gst_caps_list_check_compatibility (gst_pad_get_caps_list(pad), gst_pad_get_caps_list(sinkpad))) {
gst_pad_connect(pad, sinkpad);
GST_DEBUG (0,"gstpipeline: autoconnect pad \"%s\" in element %s <-> ", GST_PAD_NAME (pad),
GST_ELEMENT_NAME(src));
GST_DEBUG (0,"pad \"%s\" in element %s\n", GST_PAD_NAME (sinkpad),
GST_ELEMENT_NAME(sink));
connected = TRUE;
break;
}
else {
GST_DEBUG (0,"pads incompatible %s, %s\n", GST_PAD_NAME (pad), GST_PAD_NAME (sinkpad));
}
}
sinkpads = g_list_next(sinkpads);
}
if (!connected) {
GST_DEBUG (0,"gstpipeline: no path to sinks for type\n");
}
return connected;
}
static void
gst_autoplug_pads_autoplug (GstElement *src, GstElement *sink)
{
GList *srcpads;
gboolean connected = FALSE;
srcpads = gst_element_get_pad_list(src);
while (srcpads && !connected) {
GstPad *srcpad = (GstPad *)srcpads->data;
if (gst_pad_get_direction(srcpad) == GST_PAD_SRC)
connected = gst_autoplug_pads_autoplug_func (src, srcpad, sink);
srcpads = g_list_next(srcpads);
}
if (!connected) {
GST_DEBUG (0,"gstpipeline: delaying pad connections for \"%s\" to \"%s\"\n",
GST_ELEMENT_NAME(src), GST_ELEMENT_NAME(sink));
gtk_signal_connect(GTK_OBJECT(src),"new_pad",
GTK_SIGNAL_FUNC(gst_autoplug_pads_autoplug_func), sink);
}
}
static GList*
gst_autoplug_elementfactory_get_list (gpointer data)
{
return gst_elementfactory_get_list ();
}
typedef struct {
GList *src;
GList *sink;
} caps_struct;
#define IS_CAPS(cap) (((cap) == caps->src) || (cap) == caps->sink)
static guint
gst_autoplug_caps_find_cost (gpointer src, gpointer dest, gpointer data)
{
caps_struct *caps = (caps_struct *)data;
gboolean res;
if (IS_CAPS (src) && IS_CAPS (dest)) {
res = gst_caps_list_check_compatibility ((GList *)src, (GList *)dest);
//GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"caps %d to caps %d %d", ((GstCaps *)src)->id, ((GstCaps *)dest)->id, res);
}
else if (IS_CAPS (src)) {
res = gst_elementfactory_can_sink_caps_list ((GstElementFactory *)dest, (GList *)src);
//GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"factory %s to src caps %d %d", ((GstElementFactory *)dest)->name, ((GstCaps *)src)->id, res);
}
else if (IS_CAPS (dest)) {
res = gst_elementfactory_can_src_caps_list ((GstElementFactory *)src, (GList *)dest);
//GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"factory %s to sink caps %d %d", ((GstElementFactory *)src)->name, ((GstCaps *)dest)->id, res);
}
else {
res = gst_autoplug_can_match ((GstElementFactory *)src, (GstElementFactory *)dest);
}
if (res)
return 1;
else
return GST_AUTOPLUG_MAX_COST;
}
/**
* gst_autoplug_pads:
* @srcpad: the source pad
* @sinkpad: the sink pad
*
* Perform autoplugging between the two given pads.
*
* Returns: a list of elementfactories that can connect
* the two pads
*/
GstElement*
gst_autoplug_caps_list (GList *srccaps, GList *sinkcaps, ...)
{
caps_struct caps;
va_list args;
GList *capslist;
GstElement *result = NULL, *srcelement = NULL;
GList **factories;
GList *chains = NULL;
GList *endcaps = NULL;
guint numsinks = 0, i;
gboolean have_common = FALSE;
va_start (args, sinkcaps);
capslist = sinkcaps;
/*
* We first create a list of elements that are needed
* to convert the srcpad caps to the different sinkpad caps.
* and add the list of elementfactories to a list (chains).
*/
caps.src = srccaps;
while (capslist) {
GList *elements;
caps.sink = capslist;
GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"autoplugging two caps structures");
elements = gst_autoplug_func (caps.src, caps.sink,
gst_autoplug_elementfactory_get_list,
gst_autoplug_caps_find_cost,
&caps);
if (elements) {
chains = g_list_append (chains, elements);
endcaps = g_list_append (endcaps, capslist);
numsinks++;
}
else {
}
capslist = va_arg (args, GList *);
}
va_end (args);
/*
* If no list could be found the pipeline cannot be autoplugged and
* we return a NULL element
*/
if (numsinks == 0)
return NULL;
/*
* We now have a list of lists. We will turn this into an array
* of lists, this will make it much more easy to manipulate it
* in the next steps.
*/
factories = g_new0 (GList *, numsinks);
for (i = 0; chains; i++) {
GList *elements = (GList *) chains->data;
factories[i] = elements;
chains = g_list_next (chains);
}
//FIXME, free the list
result = gst_bin_new ("autoplug_bin");
/*
* We now hav a list of lists that is probably like:
*
* !
* A -> B -> C
* !
* A -> D -> E
*
* we now try to find the common elements (A) and add them to
* the bin. We remove them from both lists too.
*/
while (factories[0]) {
GstElementFactory *factory;
GstElement *element;
// fase 3: add common elements
factory = (GstElementFactory *) (factories[0]->data);
// check to other paths for matching elements (factories)
for (i=1; i<numsinks; i++) {
if (factory != (GstElementFactory *) (factories[i]->data)) {
goto differ;
}
}
GST_DEBUG (0,"common factory \"%s\"\n", factory->name);
element = gst_elementfactory_create (factory, factory->name);
gst_bin_add (GST_BIN(result), element);
if (srcelement != NULL) {
gst_autoplug_pads_autoplug (srcelement, element);
}
// this is the first element, find a good ghostpad
else {
GList *pads;
pads = gst_element_get_pad_list (element);
while (pads) {
GstPad *pad = GST_PAD (pads->data);
if (gst_caps_list_check_compatibility (srccaps, gst_pad_get_caps_list (pad))) {
gst_element_add_ghost_pad (result, pad, "sink");
break;
}
pads = g_list_next (pads);
}
}
srcelement = element;
// advance the pointer in all lists
for (i=0; i<numsinks; i++) {
factories[i] = g_list_next (factories[i]);
}
have_common = TRUE;
}
differ:
// loop over all the sink elements
for (i = 0; i < numsinks; i++) {
GstElement *thesrcelement = srcelement;
GstElement *thebin = GST_ELEMENT(result);
gboolean use_thread;
use_thread = have_common;
while (factories[i]) {
// fase 4: add other elements...
GstElementFactory *factory;
GstElement *element;
factory = (GstElementFactory *)(factories[i]->data);
GST_DEBUG (0,"factory \"%s\"\n", factory->name);
element = gst_elementfactory_create(factory, factory->name);
// this element suggests the use of a thread, so we set one up...
if (GST_ELEMENT_IS_THREAD_SUGGESTED(element) || use_thread) {
GstElement *queue;
GList *sinkpads;
GstPad *srcpad, *sinkpad;
use_thread = FALSE;
GST_DEBUG (0,"sugest new thread for \"%s\" %08x\n", GST_ELEMENT_NAME (element), GST_FLAGS(element));
// create a new queue and add to the previous bin
queue = gst_elementfactory_make("queue", g_strconcat("queue_", GST_ELEMENT_NAME(element), NULL));
GST_DEBUG (0,"adding element \"%s\"\n", GST_ELEMENT_NAME (element));
gst_bin_add(GST_BIN(thebin), queue);
// this will be the new bin for all following elements
thebin = gst_elementfactory_make("thread", g_strconcat("thread_", GST_ELEMENT_NAME(element), NULL));
srcpad = gst_element_get_pad(queue, "src");
sinkpads = gst_element_get_pad_list(element);
while (sinkpads) {
sinkpad = (GstPad *)sinkpads->data;
// FIXME connect matching pads, not just the first one...
if (gst_pad_get_direction(sinkpad) == GST_PAD_SINK &&
!GST_PAD_CONNECTED(sinkpad)) {
GList *caps = gst_pad_get_caps_list (sinkpad);
// the queue has the type of the elements it connects
gst_pad_set_caps_list (srcpad, caps);
gst_pad_set_caps_list (gst_element_get_pad(queue, "sink"), caps);
break;
}
sinkpads = g_list_next(sinkpads);
}
gst_autoplug_pads_autoplug(thesrcelement, queue);
GST_DEBUG (0,"adding element %s\n", GST_ELEMENT_NAME (element));
gst_bin_add(GST_BIN(thebin), element);
GST_DEBUG (0,"adding element %s\n", GST_ELEMENT_NAME (thebin));
gst_bin_add(GST_BIN(result), thebin);
thesrcelement = queue;
}
// no thread needed, easy case
else {
GST_DEBUG (0,"adding element %s\n", GST_ELEMENT_NAME (element));
gst_bin_add(GST_BIN(thebin), element);
}
gst_autoplug_pads_autoplug(thesrcelement, element);
// this element is now the new source element
thesrcelement = element;
factories[i] = g_list_next(factories[i]);
}
/*
* we're at the last element in the chain,
* find a suitable pad to turn into a ghostpad
*/
{
GList *endcap = (GList *)(endcaps->data);
GList *pads = gst_element_get_pad_list (thesrcelement);
endcaps = g_list_next (endcaps);
while (pads) {
GstPad *pad = GST_PAD (pads->data);
pads = g_list_next (pads);
if (gst_caps_list_check_compatibility (gst_pad_get_caps_list (pad), endcap)) {
gst_element_add_ghost_pad (result, pad, g_strdup_printf("src_%02d", i));
break;
}
}
}
}
return result;
}
static gint
find_factory (gst_autoplug_node *rgnNodes, gpointer factory)
{
gint i=0;
while (rgnNodes[i].iNode) {
if (rgnNodes[i].iNode == factory) return i;
i++;
}
return 0;
}
static GList*
construct_path (gst_autoplug_node *rgnNodes, gpointer factory)
{
GstElementFactory *current;
GList *factories = NULL;
current = rgnNodes[find_factory(rgnNodes, factory)].iPrev;
GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"factories found in autoplugging (reversed order)");
while (current != NULL)
{
gpointer next = NULL;
next = rgnNodes[find_factory(rgnNodes, current)].iPrev;
if (next) {
factories = g_list_prepend (factories, current);
GST_INFO (GST_CAT_AUTOPLUG_ATTEMPT,"factory: \"%s\"", current->name);
}
current = next;
}
return factories;
}
static GList*
gst_autoplug_enqueue (GList *queue, gpointer iNode, gint iDist, gpointer iPrev)
{
gst_autoplug_node *node = g_malloc (sizeof (gst_autoplug_node));
node->iNode = iNode;
node->iDist = iDist;
node->iPrev = iPrev;
queue = g_list_append (queue, node);
return queue;
}
static GList*
gst_autoplug_dequeue (GList *queue, gpointer *iNode, gint *iDist, gpointer *iPrev)
{
GList *head;
gst_autoplug_node *node;
head = g_list_first (queue);
if (head) {
node = (gst_autoplug_node *)head->data;
*iNode = node->iNode;
*iPrev = node->iPrev;
*iDist = node->iDist;
head = g_list_remove (queue, node);
}
return head;
}
static GList*
gst_autoplug_func (gpointer src, gpointer sink,
GstAutoplugListFunction list_function,
GstAutoplugCostFunction cost_function,
gpointer data)
{
gst_autoplug_node *rgnNodes;
GList *queue = NULL;
gpointer iNode, iPrev;
gint iDist, i, iCost;
GList *elements = g_list_copy (list_function(data));
GList *factories;
guint num_factories;
elements = g_list_append (elements, sink);
elements = g_list_append (elements, src);
factories = elements;
num_factories = g_list_length (factories);
rgnNodes = g_new0 (gst_autoplug_node, num_factories+1);
for (i=0; i< num_factories; i++) {
gpointer fact = factories->data;
rgnNodes[i].iNode = fact;
rgnNodes[i].iPrev = NULL;
if (fact == src) {
rgnNodes[i].iDist = 0;
}
else {
rgnNodes[i].iDist = GST_AUTOPLUG_MAX_COST;
}
factories = g_list_next (factories);
}
rgnNodes[num_factories].iNode = NULL;
queue = gst_autoplug_enqueue (queue, src, 0, NULL);
while (g_list_length (queue) > 0) {
GList *factories2 = elements;
queue = gst_autoplug_dequeue (queue, &iNode, &iDist, &iPrev);
for (i=0; i< num_factories; i++) {
gpointer current = factories2->data;
iCost = cost_function (iNode, current, data);
if (iCost != GST_AUTOPLUG_MAX_COST) {
if((GST_AUTOPLUG_MAX_COST == rgnNodes[i].iDist) ||
(rgnNodes[i].iDist > (iCost + iDist))) {
rgnNodes[i].iDist = iDist + iCost;
rgnNodes[i].iPrev = iNode;
queue = gst_autoplug_enqueue (queue, current, iDist + iCost, iNode);
}
}
factories2 = g_list_next (factories2);
}
}
return construct_path (rgnNodes, sink);
}