gstreamer/gst/gstutils.c

1303 lines
37 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
* 2000 Wim Taymans <wtay@chello.be>
*
* gstutils.c: Utility functions: gtk_get_property stuff, etc.
*
* 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.
*/
#include <stdio.h>
#include <string.h>
#include "gst_private.h"
#include "gstutils.h"
#include "gsturitype.h"
#include "gstinfo.h"
#include "gst-i18n-lib.h"
/**
* gst_util_dump_mem:
* @mem: a pointer to the memory to dump
* @size: the size of the memory block to dump
*
* Dumps the memory block into a hex representation. Useful for debugging.
*/
void
gst_util_dump_mem (const guchar * mem, guint size)
{
guint i, j;
GString *string = g_string_sized_new (50);
GString *chars = g_string_sized_new (18);
i = j = 0;
while (i < size) {
if (g_ascii_isprint (mem[i]))
g_string_append_printf (chars, "%c", mem[i]);
else
g_string_append_printf (chars, ".");
g_string_append_printf (string, "%02x ", mem[i]);
j++;
i++;
if (j == 16 || i == size) {
g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
string->str, chars->str);
g_string_set_size (string, 0);
g_string_set_size (chars, 0);
j = 0;
}
}
g_string_free (string, TRUE);
g_string_free (chars, TRUE);
}
/**
* gst_util_set_value_from_string:
* @value: the value to set
* @value_str: the string to get the value from
*
* Converts the string to the type of the value and
* sets the value with it.
*/
void
gst_util_set_value_from_string (GValue * value, const gchar * value_str)
{
g_return_if_fail (value != NULL);
g_return_if_fail (value_str != NULL);
GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
g_type_name (G_VALUE_TYPE (value)));
switch (G_VALUE_TYPE (value)) {
case G_TYPE_STRING:
g_value_set_string (value, g_strdup (value_str));
break;
case G_TYPE_ENUM:
case G_TYPE_INT:{
gint i;
sscanf (value_str, "%d", &i);
g_value_set_int (value, i);
break;
}
case G_TYPE_UINT:{
guint i;
sscanf (value_str, "%u", &i);
g_value_set_uint (value, i);
break;
}
case G_TYPE_LONG:{
glong i;
sscanf (value_str, "%ld", &i);
g_value_set_long (value, i);
break;
}
case G_TYPE_ULONG:{
gulong i;
sscanf (value_str, "%lu", &i);
g_value_set_ulong (value, i);
break;
}
case G_TYPE_BOOLEAN:{
gboolean i = FALSE;
if (!strncmp ("true", value_str, 4))
i = TRUE;
g_value_set_boolean (value, i);
break;
}
case G_TYPE_CHAR:{
gchar i;
sscanf (value_str, "%c", &i);
g_value_set_char (value, i);
break;
}
case G_TYPE_UCHAR:{
guchar i;
sscanf (value_str, "%c", &i);
g_value_set_uchar (value, i);
break;
}
case G_TYPE_FLOAT:{
gfloat i;
sscanf (value_str, "%f", &i);
g_value_set_float (value, i);
break;
}
case G_TYPE_DOUBLE:{
gfloat i;
sscanf (value_str, "%g", &i);
g_value_set_double (value, (gdouble) i);
break;
}
default:
break;
}
}
/**
* gst_util_set_object_arg:
* @object: the object to set the argument of
* @name: the name of the argument to set
* @value: the string value to set
*
* Convertes the string value to the type of the objects argument and
* sets the argument with it.
*/
void
gst_util_set_object_arg (GObject * object, const gchar * name,
const gchar * value)
{
if (name && value) {
GParamSpec *paramspec;
paramspec =
g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
if (!paramspec) {
return;
}
GST_DEBUG ("paramspec->flags is %d, paramspec->value_type is %d",
paramspec->flags, (gint) paramspec->value_type);
if (paramspec->flags & G_PARAM_WRITABLE) {
switch (paramspec->value_type) {
case G_TYPE_STRING:
g_object_set (G_OBJECT (object), name, value, NULL);
break;
case G_TYPE_ENUM:
case G_TYPE_INT:{
gint i;
sscanf (value, "%d", &i);
g_object_set (G_OBJECT (object), name, i, NULL);
break;
}
case G_TYPE_UINT:{
guint i;
sscanf (value, "%u", &i);
g_object_set (G_OBJECT (object), name, i, NULL);
break;
}
case G_TYPE_LONG:{
glong i;
sscanf (value, "%ld", &i);
g_object_set (G_OBJECT (object), name, i, NULL);
break;
}
case G_TYPE_ULONG:{
gulong i;
sscanf (value, "%lu", &i);
g_object_set (G_OBJECT (object), name, i, NULL);
break;
}
case G_TYPE_BOOLEAN:{
gboolean i = FALSE;
if (!g_ascii_strncasecmp ("true", value, 4))
i = TRUE;
g_object_set (G_OBJECT (object), name, i, NULL);
break;
}
case G_TYPE_CHAR:{
gchar i;
sscanf (value, "%c", &i);
g_object_set (G_OBJECT (object), name, i, NULL);
break;
}
case G_TYPE_UCHAR:{
guchar i;
sscanf (value, "%c", &i);
g_object_set (G_OBJECT (object), name, i, NULL);
break;
}
case G_TYPE_FLOAT:{
gfloat i;
sscanf (value, "%f", &i);
g_object_set (G_OBJECT (object), name, i, NULL);
break;
}
case G_TYPE_DOUBLE:{
gfloat i;
sscanf (value, "%g", &i);
g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
break;
}
default:
if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
gint i;
sscanf (value, "%d", &i);
g_object_set (G_OBJECT (object), name, i, NULL);
} else if (paramspec->value_type == GST_TYPE_URI) {
g_object_set (G_OBJECT (object), name, value, NULL);
}
break;
}
}
}
}
/* -----------------------------------------------------
*
* The following code will be moved out of the main
* gstreamer library someday.
*/
#include "gstpad.h"
static void
string_append_indent (GString * str, gint count)
{
gint xx;
for (xx = 0; xx < count; xx++)
g_string_append_c (str, ' ');
}
/**
* gst_print_pad_caps:
* @buf: the buffer to print the caps in
* @indent: initial indentation
* @pad: the pad to print the caps from
*
* Write the pad capabilities in a human readable format into
* the given GString.
*/
void
gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
{
GstRealPad *realpad;
GstCaps *caps;
realpad = GST_PAD_REALIZE (pad);
caps = realpad->caps;
if (!caps) {
string_append_indent (buf, indent);
g_string_printf (buf, "%s:%s has no capabilities",
GST_DEBUG_PAD_NAME (pad));
} else {
char *s;
s = gst_caps_to_string (caps);
g_string_append (buf, s);
g_free (s);
}
}
/**
* gst_print_element_args:
* @buf: the buffer to print the args in
* @indent: initial indentation
* @element: the element to print the args of
*
* Print the element argument in a human readable format in the given
* GString.
*/
void
gst_print_element_args (GString * buf, gint indent, GstElement * element)
{
guint width;
GValue value = { 0, }; /* the important thing is that value.type = 0 */
gchar *str = NULL;
GParamSpec *spec, **specs, **walk;
specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
width = 0;
for (walk = specs; *walk; walk++) {
spec = *walk;
if (width < strlen (spec->name))
width = strlen (spec->name);
}
for (walk = specs; *walk; walk++) {
spec = *walk;
if (spec->flags & G_PARAM_READABLE) {
g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
g_object_get_property (G_OBJECT (element), spec->name, &value);
str = g_strdup_value_contents (&value);
g_value_unset (&value);
} else {
str = g_strdup ("Parameter not readable.");
}
string_append_indent (buf, indent);
g_string_append (buf, spec->name);
string_append_indent (buf, 2 + width - strlen (spec->name));
g_string_append (buf, str);
g_string_append_c (buf, '\n');
g_free (str);
}
g_free (specs);
}
GstFlowReturn
gst_element_abort_preroll (GstElement * element)
{
GstFlowReturn result = GST_FLOW_OK;
/* grab state change lock */
GST_STATE_LOCK (element);
/* if we are going to PAUSED, we can abort the state change */
if (GST_STATE_TRANSITION (element) == GST_STATE_READY_TO_PAUSED) {
gst_element_abort_state (GST_ELEMENT (element));
}
GST_STATE_UNLOCK (element);
return result;
}
/* call with stream lock held */
GstFlowReturn
gst_element_finish_preroll (GstElement * element, GMutex * streamlock)
{
GstFlowReturn result = GST_FLOW_OK;
/* grab state change lock */
GST_STATE_LOCK (element);
/* if we are going to PAUSED, we can commit the state change */
if (GST_STATE_TRANSITION (element) == GST_STATE_READY_TO_PAUSED) {
gst_element_commit_state (GST_ELEMENT (element));
}
/* if we are paused we need to wait for playing to continue */
if (GST_STATE (element) == GST_STATE_PAUSED) {
GST_CAT_DEBUG (GST_CAT_STATES,
"element %s wants to finish preroll", GST_ELEMENT_NAME (element));
/* FIXME, release streaming lock? */
if (streamlock)
g_mutex_unlock (streamlock);
/* here we wait for the next state change */
do {
GST_CAT_DEBUG (GST_CAT_STATES, "waiting for next state change");
GST_STATE_WAIT (element);
}
while (GST_STATE (element) == GST_STATE_PAUSED);
if (streamlock)
g_mutex_lock (streamlock);
/* check if we got playing */
if (GST_STATE (element) != GST_STATE_PLAYING) {
/* not playing, we can't accept the buffer */
result = GST_FLOW_WRONG_STATE;
}
GST_CAT_DEBUG (GST_CAT_STATES, "done preroll");
}
GST_STATE_UNLOCK (element);
return result;
}
/**
* gst_element_get_compatible_pad_template:
* @element: a #GstElement to get a compatible pad template for.
* @compattempl: the #GstPadTemplate to find a compatible template for.
*
* Retrieves a pad template from @element that is compatible with @compattempl.
* Pads from compatible templates can be linked together.
*
* Returns: a compatible #GstPadTemplate, or NULL if none was found. No
* unreferencing is necessary.
*/
GstPadTemplate *
gst_element_get_compatible_pad_template (GstElement * element,
GstPadTemplate * compattempl)
{
GstPadTemplate *newtempl = NULL;
GList *padlist;
GstElementClass *class;
g_return_val_if_fail (element != NULL, NULL);
g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
g_return_val_if_fail (compattempl != NULL, NULL);
class = GST_ELEMENT_GET_CLASS (element);
padlist = gst_element_class_get_pad_template_list (class);
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
"Looking for a suitable pad template in %s out of %d templates...",
GST_ELEMENT_NAME (element), g_list_length (padlist));
while (padlist) {
GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
GstCaps *intersection;
/* Ignore name
* Ignore presence
* Check direction (must be opposite)
* Check caps
*/
GST_CAT_LOG (GST_CAT_CAPS,
"checking pad template %s", padtempl->name_template);
if (padtempl->direction != compattempl->direction) {
GST_CAT_DEBUG (GST_CAT_CAPS,
"compatible direction: found %s pad template \"%s\"",
padtempl->direction == GST_PAD_SRC ? "src" : "sink",
padtempl->name_template);
intersection = gst_caps_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
GST_PAD_TEMPLATE_CAPS (padtempl));
GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
(intersection ? "" : "not "));
if (!gst_caps_is_empty (intersection))
newtempl = padtempl;
gst_caps_unref (intersection);
if (newtempl)
break;
}
padlist = g_list_next (padlist);
}
if (newtempl)
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
"Returning new pad template %p", newtempl);
else
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
return newtempl;
}
static GstPad *
gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
const gchar * name)
{
GstPad *newpad = NULL;
GstElementClass *oclass;
oclass = GST_ELEMENT_GET_CLASS (element);
if (oclass->request_new_pad)
newpad = (oclass->request_new_pad) (element, templ, name);
return newpad;
}
/**
* gst_element_get_pad_from_template:
* @element: a #GstElement.
* @templ: a #GstPadTemplate belonging to @element.
*
* Gets a pad from @element described by @templ. If the presence of @templ is
* #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
* templates.
*
* Returns: the #GstPad, or NULL if one could not be found or created.
*/
static GstPad *
gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
{
GstPad *ret = NULL;
GstPadPresence presence;
/* If this function is ever exported, we need check the validity of `element'
* and `templ', and to make sure the template actually belongs to the
* element. */
presence = GST_PAD_TEMPLATE_PRESENCE (templ);
switch (presence) {
case GST_PAD_ALWAYS:
case GST_PAD_SOMETIMES:
ret = gst_element_get_static_pad (element, templ->name_template);
if (!ret && presence == GST_PAD_ALWAYS)
g_warning
("Element %s has an ALWAYS template %s, but no pad of the same name",
GST_OBJECT_NAME (element), templ->name_template);
break;
case GST_PAD_REQUEST:
ret = gst_element_request_pad (element, templ, NULL);
break;
}
return ret;
}
/**
* gst_element_request_compatible_pad:
* @element: a #GstElement.
* @templ: the #GstPadTemplate to which the new pad should be able to link.
*
* Requests a pad from @element. The returned pad should be unlinked and
* compatible with @templ. Might return an existing pad, or request a new one.
*
* Returns: a #GstPad, or %NULL if one could not be found or created.
*/
GstPad *
gst_element_request_compatible_pad (GstElement * element,
GstPadTemplate * templ)
{
GstPadTemplate *templ_new;
GstPad *pad = NULL;
g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
/* FIXME: should really loop through the templates, testing each for
* compatibility and pad availability. */
templ_new = gst_element_get_compatible_pad_template (element, templ);
if (templ_new)
pad = gst_element_get_pad_from_template (element, templ_new);
/* This can happen for non-request pads. No need to unref. */
if (pad && GST_PAD_PEER (pad))
pad = NULL;
return pad;
}
/**
* gst_element_get_compatible_pad_filtered:
* @element: a #GstElement in which the pad should be found.
* @pad: the #GstPad to find a compatible one for.
* @filtercaps: the #GstCaps to use as a filter.
*
* Looks for an unlinked pad to which the given pad can link. It is not
* guaranteed that linking the pads will work, though it should work in most
* cases.
*
* Returns: the #GstPad to which a link can be made, or %NULL if one cannot be
* found.
*/
GstPad *
gst_element_get_compatible_pad_filtered (GstElement * element, GstPad * pad,
const GstCaps * filtercaps)
{
const GList *pads;
GstPadTemplate *templ;
GstCaps *templcaps;
GstPad *foundpad = NULL;
g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
g_return_val_if_fail (GST_IS_PAD (pad), NULL);
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
"finding pad in %s compatible with %s:%s and filter %" GST_PTR_FORMAT,
GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad), filtercaps);
/* let's use the real pad */
pad = (GstPad *) GST_PAD_REALIZE (pad);
g_return_val_if_fail (pad != NULL, NULL);
g_return_val_if_fail (GST_RPAD_PEER (pad) == NULL, NULL);
/* try to get an existing unlinked pad */
pads = gst_element_get_pad_list (element);
while (pads) {
GstPad *current = GST_PAD (pads->data);
GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examing pad %s:%s",
GST_DEBUG_PAD_NAME (current));
if (GST_PAD_PEER (current) == NULL &&
gst_pad_can_link_filtered (pad, current, filtercaps)) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
"found existing unlinked pad %s:%s", GST_DEBUG_PAD_NAME (current));
return current;
}
pads = g_list_next (pads);
}
/* try to create a new one */
/* requesting is a little crazy, we need a template. Let's create one */
templcaps = GST_PAD_CAPS (pad);
if (filtercaps != NULL) {
GstCaps *temp;
temp = gst_caps_intersect (filtercaps, templcaps);
gst_caps_unref (templcaps);
templcaps = temp;
}
templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
foundpad = gst_element_request_compatible_pad (element, templ);
gst_object_unref (GST_OBJECT (templ));
if (foundpad) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
"found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
return foundpad;
}
GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
"Could not find a compatible pad to link to %s:%s",
GST_DEBUG_PAD_NAME (pad));
return NULL;
}
/**
* gst_element_get_compatible_pad:
* @element: a #GstElement in which the pad should be found.
* @pad: the #GstPad to find a compatible one for.
*
* Looks for an unlinked pad to which the given pad can link to.
* It is not guaranteed that linking the pads will work, though
* it should work in most cases.
*
* Returns: the #GstPad to which a link can be made, or %NULL if one
* could not be found.
*/
GstPad *
gst_element_get_compatible_pad (GstElement * element, GstPad * pad)
{
return gst_element_get_compatible_pad_filtered (element, pad, NULL);
}
/**
* gst_element_state_get_name:
* @state: a #GstElementState to get the name of.
*
* Gets a string representing the given state.
*
* Returns: a string with the name of the state.
*/
const gchar *
gst_element_state_get_name (GstElementState state)
{
switch (state) {
#ifdef GST_DEBUG_COLOR
case GST_STATE_VOID_PENDING:
return "NONE_PENDING";
break;
case GST_STATE_NULL:
return "\033[01;34mNULL\033[00m";
break;
case GST_STATE_READY:
return "\033[01;31mREADY\033[00m";
break;
case GST_STATE_PLAYING:
return "\033[01;32mPLAYING\033[00m";
break;
case GST_STATE_PAUSED:
return "\033[01;33mPAUSED\033[00m";
break;
default:
/* This is a memory leak */
return g_strdup_printf ("\033[01;35;41mUNKNOWN!\033[00m(%d)", state);
#else
case GST_STATE_VOID_PENDING:
return "NONE_PENDING";
break;
case GST_STATE_NULL:
return "NULL";
break;
case GST_STATE_READY:
return "READY";
break;
case GST_STATE_PLAYING:
return "PLAYING";
break;
case GST_STATE_PAUSED:
return "PAUSED";
break;
default:
return "UNKNOWN!";
#endif
}
return "";
}
/**
* gst_element_link_pads_filtered:
* @src: a #GstElement containing the source pad.
* @srcpadname: the name of the #GstPad in source element or NULL for any pad.
* @dest: the #GstElement containing the destination pad.
* @destpadname: the name of the #GstPad in destination element or NULL for any pad.
* @filtercaps: the #GstCaps to use as a filter.
*
* Links the two named pads of the source and destination elements.
* Side effect is that if one of the pads has no parent, it becomes a
* child of the parent of the other element. If they have different
* parents, the link fails.
*
* Returns: TRUE if the pads could be linked, FALSE otherwise.
*/
GstPadLinkReturn
gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
GstElement * dest, const gchar * destpadname, const GstCaps * filtercaps)
{
const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
GstPad *srcpad, *destpad;
GstPadTemplate *srctempl, *desttempl;
GstElementClass *srcclass, *destclass;
/* checks */
g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
srcclass = GST_ELEMENT_GET_CLASS (src);
destclass = GST_ELEMENT_GET_CLASS (dest);
GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
"trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
destpadname ? destpadname : "(any)");
/* now get the pads we're trying to link and a list of all remaining pads */
if (srcpadname) {
srcpad = gst_element_get_pad (src, srcpadname);
if (!srcpad) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
GST_ELEMENT_NAME (src), srcpadname);
return FALSE;
} else {
if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
GST_DEBUG_PAD_NAME (srcpad));
return FALSE;
}
if (GST_PAD_PEER (srcpad) != NULL) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
GST_DEBUG_PAD_NAME (srcpad));
return FALSE;
}
}
srcpads = NULL;
} else {
srcpads = gst_element_get_pad_list (src);
srcpad = srcpads ? (GstPad *) GST_PAD_REALIZE (srcpads->data) : NULL;
}
if (destpadname) {
destpad = gst_element_get_pad (dest, destpadname);
if (!destpad) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
GST_ELEMENT_NAME (dest), destpadname);
return FALSE;
} else {
if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
GST_DEBUG_PAD_NAME (destpad));
return FALSE;
}
if (GST_PAD_PEER (destpad) != NULL) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
GST_DEBUG_PAD_NAME (destpad));
return FALSE;
}
}
destpads = NULL;
} else {
destpads = gst_element_get_pad_list (dest);
destpad = destpads ? (GstPad *) GST_PAD_REALIZE (destpads->data) : NULL;
}
if (srcpadname && destpadname) {
/* two explicitly specified pads */
return gst_pad_link_filtered (srcpad, destpad, filtercaps);
}
if (srcpad) {
/* loop through the allowed pads in the source, trying to find a
* compatible destination pad */
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
"looping through allowed src and dest pads");
do {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
GST_DEBUG_PAD_NAME (srcpad));
if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
(GST_PAD_PEER (srcpad) == NULL)) {
GstPad *temp = destpadname ? destpad :
gst_element_get_compatible_pad_filtered (dest, srcpad,
filtercaps);
if (temp
&& gst_pad_link_filtered (srcpad, temp,
filtercaps) == GST_PAD_LINK_OK) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
return TRUE;
}
}
/* find a better way for this mess */
if (srcpads) {
srcpads = g_list_next (srcpads);
if (srcpads)
srcpad = (GstPad *) GST_PAD_REALIZE (srcpads->data);
}
} while (srcpads);
}
if (srcpadname) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
return FALSE;
}
if (destpad) {
/* loop through the existing pads in the destination */
do {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
GST_DEBUG_PAD_NAME (destpad));
if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
(GST_PAD_PEER (destpad) == NULL)) {
GstPad *temp = gst_element_get_compatible_pad_filtered (src, destpad,
filtercaps);
if (temp
&& gst_pad_link_filtered (temp, destpad,
filtercaps) == GST_PAD_LINK_OK) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
return TRUE;
}
}
if (destpads) {
destpads = g_list_next (destpads);
if (destpads)
destpad = (GstPad *) GST_PAD_REALIZE (destpads->data);
}
} while (destpads);
}
if (destpadname) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
return FALSE;
}
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
"we might have request pads on both sides, checking...");
srctempls = gst_element_class_get_pad_template_list (srcclass);
desttempls = gst_element_class_get_pad_template_list (destclass);
if (srctempls && desttempls) {
while (srctempls) {
srctempl = (GstPadTemplate *) srctempls->data;
if (srctempl->presence == GST_PAD_REQUEST) {
for (l = desttempls; l; l = l->next) {
desttempl = (GstPadTemplate *) l->data;
if (desttempl->presence == GST_PAD_REQUEST &&
desttempl->direction != srctempl->direction) {
if (gst_caps_is_always_compatible (gst_pad_template_get_caps
(srctempl), gst_pad_template_get_caps (desttempl))) {
srcpad =
gst_element_get_request_pad (src, srctempl->name_template);
destpad =
gst_element_get_request_pad (dest, desttempl->name_template);
if (gst_pad_link_filtered (srcpad, destpad,
filtercaps) == GST_PAD_LINK_OK) {
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
"linked pad %s:%s to pad %s:%s",
GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
return TRUE;
}
/* it failed, so we release the request pads */
gst_element_release_request_pad (src, srcpad);
gst_element_release_request_pad (dest, destpad);
}
}
}
}
srctempls = srctempls->next;
}
}
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
return FALSE;
}
/**
* gst_element_link_filtered:
* @src: a #GstElement containing the source pad.
* @dest: the #GstElement containing the destination pad.
* @filtercaps: the #GstCaps to use as a filter.
*
* Links @src to @dest, filtered by @filtercaps. The link must be from source to
* destination; the other direction will not be tried. The function looks for
* existing pads that aren't linked yet. It will request new pads if necessary.
* If multiple links are possible, only one is established.
*
* Returns: TRUE if the elements could be linked, FALSE otherwise.
*/
GstPadLinkReturn
gst_element_link_filtered (GstElement * src, GstElement * dest,
const GstCaps * filtercaps)
{
return gst_element_link_pads_filtered (src, NULL, dest, NULL, filtercaps);
}
/**
* gst_element_link_many:
* @element_1: the first #GstElement in the link chain.
* @element_2: the second #GstElement in the link chain.
* @...: the NULL-terminated list of elements to link in order.
*
* Chain together a series of elements. Uses gst_element_link().
*
* Returns: TRUE on success, FALSE otherwise.
*/
GstPadLinkReturn
gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
{
va_list args;
g_return_val_if_fail (element_1 != NULL && element_2 != NULL, FALSE);
g_return_val_if_fail (GST_IS_ELEMENT (element_1) &&
GST_IS_ELEMENT (element_2), FALSE);
va_start (args, element_2);
while (element_2) {
if (!gst_element_link (element_1, element_2))
return FALSE;
element_1 = element_2;
element_2 = va_arg (args, GstElement *);
}
va_end (args);
return TRUE;
}
/**
* gst_element_link:
* @src: a #GstElement containing the source pad.
* @dest: the #GstElement containing the destination pad.
*
* Links @src to @dest with no filter caps. See gst_element_link_filtered() for
* more information.
*
* Returns: TRUE if the elements could be linked, FALSE otherwise.
*/
GstPadLinkReturn
gst_element_link (GstElement * src, GstElement * dest)
{
return gst_element_link_pads_filtered (src, NULL, dest, NULL, NULL);
}
/**
* gst_element_link_pads:
* @src: a #GstElement containing the source pad.
* @srcpadname: the name of the #GstPad in the source element.
* @dest: the #GstElement containing the destination pad.
* @destpadname: the name of the #GstPad in destination element.
*
* Links the two named pads of the source and destination elements.
* Side effect is that if one of the pads has no parent, it becomes a
* child of the parent of the other element. If they have different
* parents, the link fails.
*
* Returns: TRUE if the pads could be linked, FALSE otherwise.
*/
GstPadLinkReturn
gst_element_link_pads (GstElement * src, const gchar * srcpadname,
GstElement * dest, const gchar * destpadname)
{
return gst_element_link_pads_filtered (src, srcpadname, dest, destpadname,
NULL);
}
/**
* gst_element_unlink_pads:
* @src: a #GstElement containing the source pad.
* @srcpadname: the name of the #GstPad in source element.
* @dest: a #GstElement containing the destination pad.
* @destpadname: the name of the #GstPad in destination element.
*
* Unlinks the two named pads of the source and destination elements.
*/
void
gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
GstElement * dest, const gchar * destpadname)
{
GstPad *srcpad, *destpad;
g_return_if_fail (src != NULL);
g_return_if_fail (GST_IS_ELEMENT (src));
g_return_if_fail (srcpadname != NULL);
g_return_if_fail (dest != NULL);
g_return_if_fail (GST_IS_ELEMENT (dest));
g_return_if_fail (destpadname != NULL);
/* obtain the pads requested */
srcpad = gst_element_get_pad (src, srcpadname);
if (srcpad == NULL) {
GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
return;
}
destpad = gst_element_get_pad (dest, destpadname);
if (srcpad == NULL) {
GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
destpadname);
return;
}
/* we're satisified they can be unlinked, let's do it */
gst_pad_unlink (srcpad, destpad);
}
/**
* gst_element_unlink_many:
* @element_1: the first #GstElement in the link chain.
* @element_2: the second #GstElement in the link chain.
* @...: the NULL-terminated list of elements to unlink in order.
*
* Unlinks a series of elements. Uses gst_element_unlink().
*/
void
gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
{
va_list args;
g_return_if_fail (element_1 != NULL && element_2 != NULL);
g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
va_start (args, element_2);
while (element_2) {
gst_element_unlink (element_1, element_2);
element_1 = element_2;
element_2 = va_arg (args, GstElement *);
}
va_end (args);
}
/**
* gst_element_unlink:
* @src: the source #GstElement to unlink.
* @dest: the sink #GstElement to unlink.
*
* Unlinks all source pads of the source element with all sink pads
* of the sink element to which they are linked.
*/
void
gst_element_unlink (GstElement * src, GstElement * dest)
{
const GList *srcpads;
GstPad *pad;
g_return_if_fail (GST_IS_ELEMENT (src));
g_return_if_fail (GST_IS_ELEMENT (dest));
GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
srcpads = gst_element_get_pad_list (src);
while (srcpads) {
pad = GST_PAD (srcpads->data);
/* we only care about real src pads */
if (GST_IS_REAL_PAD (pad) && GST_PAD_IS_SRC (pad)) {
GstPad *peerpad = GST_PAD_PEER (pad);
/* see if the pad is connected and is really a pad
* of dest */
if (peerpad && (GST_OBJECT_PARENT (peerpad) == (GstObject *) dest)) {
gst_pad_unlink (pad, peerpad);
}
}
srcpads = g_list_next (srcpads);
}
}
/**
* gst_pad_can_link_filtered:
* @srcpad: the source #GstPad to link.
* @sinkpad: the sink #GstPad to link.
* @filtercaps: the filter #GstCaps.
*
* Checks if the source pad and the sink pad can be linked when constrained
* by the given filter caps. Both @srcpad and @sinkpad must be unlinked.
*
* Returns: TRUE if the pads can be linked, FALSE otherwise.
*/
gboolean
gst_pad_can_link_filtered (GstPad * srcpad, GstPad * sinkpad,
const GstCaps * filtercaps)
{
GstRealPad *realsrc, *realsink;
/* FIXME This function is gross. It's almost a direct copy of
* gst_pad_link_filtered(). Any decent programmer would attempt
* to merge the two functions, which I will do some day. --ds
*/
/* generic checks */
g_return_val_if_fail (srcpad != NULL, FALSE);
g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
g_return_val_if_fail (sinkpad != NULL, FALSE);
g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
/* now we need to deal with the real/ghost stuff */
realsrc = GST_PAD_REALIZE (srcpad);
realsink = GST_PAD_REALIZE (sinkpad);
if ((GST_PAD (realsrc) != srcpad) || (GST_PAD (realsink) != sinkpad)) {
GST_CAT_INFO (GST_CAT_PADS, "*actually* linking %s:%s and %s:%s",
GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
}
/* FIXME: shouldn't we convert this to g_return_val_if_fail? */
if (GST_RPAD_PEER (realsrc) != NULL) {
GST_CAT_INFO (GST_CAT_PADS, "Real source pad %s:%s has a peer, failed",
GST_DEBUG_PAD_NAME (realsrc));
return FALSE;
}
if (GST_RPAD_PEER (realsink) != NULL) {
GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s has a peer, failed",
GST_DEBUG_PAD_NAME (realsink));
return FALSE;
}
if (GST_PAD_PARENT (realsrc) == NULL) {
GST_CAT_INFO (GST_CAT_PADS, "Real src pad %s:%s has no parent, failed",
GST_DEBUG_PAD_NAME (realsrc));
return FALSE;
}
if (GST_PAD_PARENT (realsink) == NULL) {
GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s has no parent, failed",
GST_DEBUG_PAD_NAME (realsrc));
return FALSE;
}
g_return_val_if_fail (realsrc != NULL, GST_PAD_LINK_REFUSED);
g_return_val_if_fail (realsink != NULL, GST_PAD_LINK_REFUSED);
return TRUE;
}
/**
* gst_pad_can_link:
* @srcpad: the source #GstPad to link.
* @sinkpad: the sink #GstPad to link.
*
* Checks if the source pad and the sink pad can be linked.
*
* Returns: TRUE if the pads can be linked, FALSE otherwise.
*/
gboolean
gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
{
return gst_pad_can_link_filtered (srcpad, sinkpad, NULL);
}
/**
* gst_object_default_error:
* @object: a #GObject that signalled the error.
* @orig: the #GstObject that initiated the error.
* @error: the GError.
* @debug: an additional debug information string, or NULL.
*
* A default error function.
*
* The default handler will simply print the error string using g_print.
*/
void
gst_object_default_error (GstObject * source, GError * error, gchar * debug)
{
gchar *name = gst_object_get_path_string (source);
g_print (_("ERROR: from element %s: %s\n"), name, error->message);
if (debug)
g_print (_("Additional debug info:\n%s\n"), debug);
g_free (name);
}
/**
* gst_bin_add_many:
* @bin: the bin to add the elements to
* @element_1: the first element to add to the bin
* @...: additional elements to add to the bin
*
* Adds a NULL-terminated list of elements to a bin. This function is
* equivalent to calling #gst_bin_add() for each member of the list.
*/
void
gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
{
va_list args;
g_return_if_fail (GST_IS_BIN (bin));
g_return_if_fail (GST_IS_ELEMENT (element_1));
va_start (args, element_1);
while (element_1) {
gst_bin_add (bin, element_1);
element_1 = va_arg (args, GstElement *);
}
va_end (args);
}
/**
* gst_bin_remove_many:
* @bin: the bin to remove the elements from
* @element_1: the first element to remove from the bin
* @...: NULL-terminated list of elements to remove from the bin
*
* Remove a list of elements from a bin. This function is equivalent
* to calling #gst_bin_remove with each member of the list.
*/
void
gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
{
va_list args;
g_return_if_fail (GST_IS_BIN (bin));
g_return_if_fail (GST_IS_ELEMENT (element_1));
va_start (args, element_1);
while (element_1) {
gst_bin_remove (bin, element_1);
element_1 = va_arg (args, GstElement *);
}
va_end (args);
}