2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstutils.c: Utility functions: gtk_get_property stuff, etc.
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.
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.
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.
26 #include "gst_private.h"
27 #include "gstghostpad.h"
29 #include "gsturitype.h"
31 #include "gst-i18n-lib.h"
36 * @mem: a pointer to the memory to dump
37 * @size: the size of the memory block to dump
39 * Dumps the memory block into a hex representation. Useful for debugging.
42 gst_util_dump_mem (const guchar * mem, guint size)
45 GString *string = g_string_sized_new (50);
46 GString *chars = g_string_sized_new (18);
50 if (g_ascii_isprint (mem[i]))
51 g_string_append_printf (chars, "%c", mem[i]);
53 g_string_append_printf (chars, ".");
55 g_string_append_printf (string, "%02x ", mem[i]);
60 if (j == 16 || i == size) {
61 g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
62 string->str, chars->str);
63 g_string_set_size (string, 0);
64 g_string_set_size (chars, 0);
68 g_string_free (string, TRUE);
69 g_string_free (chars, TRUE);
74 * gst_util_set_value_from_string:
75 * @value: the value to set
76 * @value_str: the string to get the value from
78 * Converts the string to the type of the value and
79 * sets the value with it.
82 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
85 g_return_if_fail (value != NULL);
86 g_return_if_fail (value_str != NULL);
88 GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
89 g_type_name (G_VALUE_TYPE (value)));
91 switch (G_VALUE_TYPE (value)) {
93 g_value_set_string (value, g_strdup (value_str));
99 sscanf (value_str, "%d", &i);
100 g_value_set_int (value, i);
106 sscanf (value_str, "%u", &i);
107 g_value_set_uint (value, i);
113 sscanf (value_str, "%ld", &i);
114 g_value_set_long (value, i);
120 sscanf (value_str, "%lu", &i);
121 g_value_set_ulong (value, i);
124 case G_TYPE_BOOLEAN:{
127 if (!strncmp ("true", value_str, 4))
129 g_value_set_boolean (value, i);
135 sscanf (value_str, "%c", &i);
136 g_value_set_char (value, i);
142 sscanf (value_str, "%c", &i);
143 g_value_set_uchar (value, i);
149 sscanf (value_str, "%f", &i);
150 g_value_set_float (value, i);
156 sscanf (value_str, "%g", &i);
157 g_value_set_double (value, (gdouble) i);
166 * gst_util_set_object_arg:
167 * @object: the object to set the argument of
168 * @name: the name of the argument to set
169 * @value: the string value to set
171 * Convertes the string value to the type of the objects argument and
172 * sets the argument with it.
175 gst_util_set_object_arg (GObject * object, const gchar * name,
179 GParamSpec *paramspec;
182 g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
188 GST_DEBUG ("paramspec->flags is %d, paramspec->value_type is %d",
189 paramspec->flags, (gint) paramspec->value_type);
191 if (paramspec->flags & G_PARAM_WRITABLE) {
192 switch (paramspec->value_type) {
194 g_object_set (G_OBJECT (object), name, value, NULL);
200 sscanf (value, "%d", &i);
201 g_object_set (G_OBJECT (object), name, i, NULL);
207 sscanf (value, "%u", &i);
208 g_object_set (G_OBJECT (object), name, i, NULL);
214 sscanf (value, "%ld", &i);
215 g_object_set (G_OBJECT (object), name, i, NULL);
221 sscanf (value, "%lu", &i);
222 g_object_set (G_OBJECT (object), name, i, NULL);
225 case G_TYPE_BOOLEAN:{
228 if (!g_ascii_strncasecmp ("true", value, 4))
230 g_object_set (G_OBJECT (object), name, i, NULL);
236 sscanf (value, "%c", &i);
237 g_object_set (G_OBJECT (object), name, i, NULL);
243 sscanf (value, "%c", &i);
244 g_object_set (G_OBJECT (object), name, i, NULL);
250 sscanf (value, "%f", &i);
251 g_object_set (G_OBJECT (object), name, i, NULL);
257 sscanf (value, "%g", &i);
258 g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
262 if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
265 sscanf (value, "%d", &i);
266 g_object_set (G_OBJECT (object), name, i, NULL);
267 } else if (paramspec->value_type == GST_TYPE_URI) {
268 g_object_set (G_OBJECT (object), name, value, NULL);
276 /* -----------------------------------------------------
278 * The following code will be moved out of the main
279 * gstreamer library someday.
285 string_append_indent (GString * str, gint count)
289 for (xx = 0; xx < count; xx++)
290 g_string_append_c (str, ' ');
294 * gst_print_pad_caps:
295 * @buf: the buffer to print the caps in
296 * @indent: initial indentation
297 * @pad: the pad to print the caps from
299 * Write the pad capabilities in a human readable format into
303 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
310 string_append_indent (buf, indent);
311 g_string_printf (buf, "%s:%s has no capabilities",
312 GST_DEBUG_PAD_NAME (pad));
316 s = gst_caps_to_string (caps);
317 g_string_append (buf, s);
323 * gst_print_element_args:
324 * @buf: the buffer to print the args in
325 * @indent: initial indentation
326 * @element: the element to print the args of
328 * Print the element argument in a human readable format in the given
332 gst_print_element_args (GString * buf, gint indent, GstElement * element)
335 GValue value = { 0, }; /* the important thing is that value.type = 0 */
337 GParamSpec *spec, **specs, **walk;
339 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
342 for (walk = specs; *walk; walk++) {
344 if (width < strlen (spec->name))
345 width = strlen (spec->name);
348 for (walk = specs; *walk; walk++) {
351 if (spec->flags & G_PARAM_READABLE) {
352 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
353 g_object_get_property (G_OBJECT (element), spec->name, &value);
354 str = g_strdup_value_contents (&value);
355 g_value_unset (&value);
357 str = g_strdup ("Parameter not readable.");
360 string_append_indent (buf, indent);
361 g_string_append (buf, spec->name);
362 string_append_indent (buf, 2 + width - strlen (spec->name));
363 g_string_append (buf, str);
364 g_string_append_c (buf, '\n');
373 * gst_element_create_all_pads:
374 * @element: a #GstElement to create pads for
376 * Creates a pad for each pad template that is always available.
377 * This function is only useful during object intialization of
378 * subclasses of #GstElement.
381 gst_element_create_all_pads (GstElement * element)
385 /* FIXME: lock element */
388 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
389 (G_OBJECT_GET_CLASS (element)));
392 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
394 if (padtempl->presence == GST_PAD_ALWAYS) {
397 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
399 gst_element_add_pad (element, pad);
401 padlist = padlist->next;
406 * gst_element_get_compatible_pad_template:
407 * @element: a #GstElement to get a compatible pad template for.
408 * @compattempl: the #GstPadTemplate to find a compatible template for.
410 * Retrieves a pad template from @element that is compatible with @compattempl.
411 * Pads from compatible templates can be linked together.
413 * Returns: a compatible #GstPadTemplate, or NULL if none was found. No
414 * unreferencing is necessary.
417 gst_element_get_compatible_pad_template (GstElement * element,
418 GstPadTemplate * compattempl)
420 GstPadTemplate *newtempl = NULL;
422 GstElementClass *class;
424 g_return_val_if_fail (element != NULL, NULL);
425 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
426 g_return_val_if_fail (compattempl != NULL, NULL);
428 class = GST_ELEMENT_GET_CLASS (element);
430 padlist = gst_element_class_get_pad_template_list (class);
432 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
433 "Looking for a suitable pad template in %s out of %d templates...",
434 GST_ELEMENT_NAME (element), g_list_length (padlist));
437 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
438 GstCaps *intersection;
442 * Check direction (must be opposite)
445 GST_CAT_LOG (GST_CAT_CAPS,
446 "checking pad template %s", padtempl->name_template);
447 if (padtempl->direction != compattempl->direction) {
448 GST_CAT_DEBUG (GST_CAT_CAPS,
449 "compatible direction: found %s pad template \"%s\"",
450 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
451 padtempl->name_template);
453 GST_CAT_DEBUG (GST_CAT_CAPS,
454 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
455 GST_CAT_DEBUG (GST_CAT_CAPS,
456 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
458 intersection = gst_caps_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
459 GST_PAD_TEMPLATE_CAPS (padtempl));
461 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible %" GST_PTR_FORMAT,
462 (intersection ? "" : "not "), intersection);
464 if (!gst_caps_is_empty (intersection))
466 gst_caps_unref (intersection);
471 padlist = g_list_next (padlist);
474 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
475 "Returning new pad template %p", newtempl);
477 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
483 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
486 GstPad *newpad = NULL;
487 GstElementClass *oclass;
489 oclass = GST_ELEMENT_GET_CLASS (element);
491 if (oclass->request_new_pad)
492 newpad = (oclass->request_new_pad) (element, templ, name);
495 gst_object_ref (newpad);
503 * gst_element_get_pad_from_template:
504 * @element: a #GstElement.
505 * @templ: a #GstPadTemplate belonging to @element.
507 * Gets a pad from @element described by @templ. If the presence of @templ is
508 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
511 * Returns: the #GstPad, or NULL if one could not be found or created.
514 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
517 GstPadPresence presence;
519 /* If this function is ever exported, we need check the validity of `element'
520 * and `templ', and to make sure the template actually belongs to the
523 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
527 case GST_PAD_SOMETIMES:
528 ret = gst_element_get_static_pad (element, templ->name_template);
529 if (!ret && presence == GST_PAD_ALWAYS)
531 ("Element %s has an ALWAYS template %s, but no pad of the same name",
532 GST_OBJECT_NAME (element), templ->name_template);
535 case GST_PAD_REQUEST:
536 ret = gst_element_request_pad (element, templ, NULL);
544 * gst_element_request_compatible_pad:
545 * @element: a #GstElement.
546 * @templ: the #GstPadTemplate to which the new pad should be able to link.
548 * Requests a pad from @element. The returned pad should be unlinked and
549 * compatible with @templ. Might return an existing pad, or request a new one.
551 * Returns: a #GstPad, or %NULL if one could not be found or created.
554 gst_element_request_compatible_pad (GstElement * element,
555 GstPadTemplate * templ)
557 GstPadTemplate *templ_new;
560 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
561 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
563 /* FIXME: should really loop through the templates, testing each for
564 * compatibility and pad availability. */
565 templ_new = gst_element_get_compatible_pad_template (element, templ);
567 pad = gst_element_get_pad_from_template (element, templ_new);
569 /* This can happen for non-request pads. No need to unref. */
570 if (pad && GST_PAD_PEER (pad))
577 * gst_element_get_compatible_pad:
578 * @element: a #GstElement in which the pad should be found.
579 * @pad: the #GstPad to find a compatible one for.
580 * @caps: the #GstCaps to use as a filter.
582 * Looks for an unlinked pad to which the given pad can link. It is not
583 * guaranteed that linking the pads will work, though it should work in most
586 * Returns: the #GstPad to which a link can be made, or %NULL if one cannot be
590 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
591 const GstCaps * caps)
594 GstPadTemplate *templ;
596 GstPad *foundpad = NULL;
599 /* FIXME check for caps compatibility */
601 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
602 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
604 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
605 "finding pad in %s compatible with %s:%s",
606 GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
608 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
611 /* try to get an existing unlinked pad */
612 pads = gst_element_iterate_pads (element);
616 switch (gst_iterator_next (pads, &padptr)) {
617 case GST_ITERATOR_OK:
622 current = GST_PAD (padptr);
624 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
625 GST_DEBUG_PAD_NAME (current));
627 peer = gst_pad_get_peer (current);
629 if (peer == NULL && gst_pad_can_link (pad, current)) {
631 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
632 "found existing unlinked pad %s:%s",
633 GST_DEBUG_PAD_NAME (current));
635 gst_iterator_free (pads);
639 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
641 gst_object_unref (current);
643 gst_object_unref (peer);
647 case GST_ITERATOR_DONE:
650 case GST_ITERATOR_RESYNC:
651 gst_iterator_resync (pads);
653 case GST_ITERATOR_ERROR:
654 g_assert_not_reached ();
658 gst_iterator_free (pads);
660 /* try to create a new one */
661 /* requesting is a little crazy, we need a template. Let's create one */
662 templcaps = gst_pad_get_caps (pad);
664 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
665 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
666 foundpad = gst_element_request_compatible_pad (element, templ);
667 gst_object_unref (templ);
670 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
671 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
675 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
676 "Could not find a compatible pad to link to %s:%s",
677 GST_DEBUG_PAD_NAME (pad));
682 * gst_element_state_get_name:
683 * @state: a #GstElementState to get the name of.
685 * Gets a string representing the given state.
687 * Returns: a string with the name of the state.
690 gst_element_state_get_name (GstElementState state)
693 #ifdef GST_DEBUG_COLOR
694 case GST_STATE_VOID_PENDING:
695 return "NONE_PENDING";
698 return "\033[01;34mNULL\033[00m";
700 case GST_STATE_READY:
701 return "\033[01;31mREADY\033[00m";
703 case GST_STATE_PLAYING:
704 return "\033[01;32mPLAYING\033[00m";
706 case GST_STATE_PAUSED:
707 return "\033[01;33mPAUSED\033[00m";
710 /* This is a memory leak */
711 return g_strdup_printf ("\033[01;35;41mUNKNOWN!\033[00m(%d)", state);
713 case GST_STATE_VOID_PENDING:
714 return "NONE_PENDING";
719 case GST_STATE_READY:
722 case GST_STATE_PLAYING:
725 case GST_STATE_PAUSED:
729 return g_strdup_printf ("UNKNOWN!(%d)", state);
736 * gst_element_factory_can_src_caps :
737 * @factory: factory to query
738 * @caps: the caps to check
740 * Checks if the factory can source the given capability.
742 * Returns: true if it can src the capabilities
745 gst_element_factory_can_src_caps (GstElementFactory * factory,
746 const GstCaps * caps)
750 g_return_val_if_fail (factory != NULL, FALSE);
751 g_return_val_if_fail (caps != NULL, FALSE);
753 templates = factory->staticpadtemplates;
756 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
758 if (template->direction == GST_PAD_SRC) {
759 if (gst_caps_is_always_compatible (gst_static_caps_get (&template->
763 templates = g_list_next (templates);
770 * gst_element_factory_can_sink_caps :
771 * @factory: factory to query
772 * @caps: the caps to check
774 * Checks if the factory can sink the given capability.
776 * Returns: true if it can sink the capabilities
779 gst_element_factory_can_sink_caps (GstElementFactory * factory,
780 const GstCaps * caps)
784 g_return_val_if_fail (factory != NULL, FALSE);
785 g_return_val_if_fail (caps != NULL, FALSE);
787 templates = factory->staticpadtemplates;
790 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
792 if (template->direction == GST_PAD_SINK) {
793 if (gst_caps_is_always_compatible (caps,
794 gst_static_caps_get (&template->static_caps)))
797 templates = g_list_next (templates);
804 /* if return val is true, *direct_child is a caller-owned ref on the direct
805 * child of ancestor that is part of object's ancestry */
807 object_has_ancestor (GstObject * object, GstObject * ancestor,
808 GstObject ** direct_child)
810 GstObject *child, *parent;
813 *direct_child = NULL;
815 child = gst_object_ref (object);
816 parent = gst_object_get_parent (object);
819 if (ancestor == parent) {
821 *direct_child = child;
823 gst_object_unref (child);
824 gst_object_unref (parent);
828 gst_object_unref (child);
830 parent = gst_object_get_parent (parent);
833 gst_object_unref (child);
838 /* caller owns return */
840 find_common_root (GstObject * o1, GstObject * o2)
843 GstObject *kid1, *kid2;
844 GstObject *root = NULL;
846 while (GST_OBJECT_PARENT (top))
847 top = GST_OBJECT_PARENT (top);
849 /* the itsy-bitsy spider... */
851 if (!object_has_ancestor (o2, top, &kid2))
854 root = gst_object_ref (top);
856 if (!object_has_ancestor (o1, kid2, &kid1)) {
857 gst_object_unref (kid2);
861 if (!object_has_ancestor (o2, kid1, &kid2)) {
862 gst_object_unref (kid1);
869 /* caller does not own return */
871 ghost_up (GstElement * e, GstPad * pad)
873 static gint ghost_pad_index = 0;
876 GstObject *parent = GST_OBJECT_PARENT (e);
878 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
879 gpad = gst_ghost_pad_new (name, pad);
882 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
883 g_warning ("Pad named %s already exists in element %s\n",
884 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
885 gst_object_unref ((GstObject *) gpad);
893 remove_pad (gpointer ppad, gpointer unused)
897 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
898 g_warning ("Couldn't remove pad %s from element %s",
899 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
903 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
904 GSList ** pads_created)
908 GSList *pads_created_local = NULL;
910 g_assert (pads_created);
912 e1 = GST_OBJECT_PARENT (*src);
913 e2 = GST_OBJECT_PARENT (*sink);
915 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
916 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
917 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
921 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
922 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
924 /* we need to setup some ghost pads */
925 root = find_common_root (e1, e2);
928 ("Trying to connect elements that don't share a common ancestor: %s and %s\n",
929 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
933 while (GST_OBJECT_PARENT (e1) != root) {
934 *src = ghost_up ((GstElement *) e1, *src);
937 e1 = GST_OBJECT_PARENT (*src);
938 pads_created_local = g_slist_prepend (pads_created_local, *src);
940 while (GST_OBJECT_PARENT (e2) != root) {
941 *sink = ghost_up ((GstElement *) e2, *sink);
944 e2 = GST_OBJECT_PARENT (*sink);
945 pads_created_local = g_slist_prepend (pads_created_local, *sink);
948 gst_object_unref (root);
949 *pads_created = g_slist_concat (*pads_created, pads_created_local);
953 gst_object_unref (root);
954 g_slist_foreach (pads_created_local, remove_pad, NULL);
955 g_slist_free (pads_created_local);
960 pad_link_maybe_ghosting (GstPad * src, GstPad * sink)
962 GSList *pads_created = NULL;
965 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
968 ret = (gst_pad_link (src, sink) == GST_PAD_LINK_OK);
972 g_slist_foreach (pads_created, remove_pad, NULL);
974 g_slist_free (pads_created);
980 * gst_element_link_pads:
981 * @src: a #GstElement containing the source pad.
982 * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
983 * @dest: the #GstElement containing the destination pad.
984 * @destpadname: the name of the #GstPad in destination element or NULL for any pad.
986 * Links the two named pads of the source and destination elements.
987 * Side effect is that if one of the pads has no parent, it becomes a
988 * child of the parent of the other element. If they have different
989 * parents, the link fails.
991 * Returns: TRUE if the pads could be linked, FALSE otherwise.
994 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
995 GstElement * dest, const gchar * destpadname)
997 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
998 GstPad *srcpad, *destpad;
999 GstPadTemplate *srctempl, *desttempl;
1000 GstElementClass *srcclass, *destclass;
1003 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1004 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1006 srcclass = GST_ELEMENT_GET_CLASS (src);
1007 destclass = GST_ELEMENT_GET_CLASS (dest);
1009 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1010 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1011 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1012 destpadname ? destpadname : "(any)");
1014 /* now get the pads we're trying to link and a list of all remaining pads */
1016 srcpad = gst_element_get_pad (src, srcpadname);
1018 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1019 GST_ELEMENT_NAME (src), srcpadname);
1022 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1023 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1024 GST_DEBUG_PAD_NAME (srcpad));
1025 gst_object_unref (srcpad);
1028 if (GST_PAD_PEER (srcpad) != NULL) {
1029 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
1030 GST_DEBUG_PAD_NAME (srcpad));
1031 gst_object_unref (srcpad);
1038 srcpads = GST_ELEMENT_PADS (src);
1039 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1041 gst_object_ref (srcpad);
1045 destpad = gst_element_get_pad (dest, destpadname);
1047 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1048 GST_ELEMENT_NAME (dest), destpadname);
1051 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1052 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1053 GST_DEBUG_PAD_NAME (destpad));
1054 gst_object_unref (destpad);
1057 if (GST_PAD_PEER (destpad) != NULL) {
1058 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
1059 GST_DEBUG_PAD_NAME (destpad));
1060 gst_object_unref (destpad);
1067 destpads = GST_ELEMENT_PADS (dest);
1068 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1070 gst_object_ref (destpad);
1074 if (srcpadname && destpadname) {
1077 /* two explicitly specified pads */
1078 result = pad_link_maybe_ghosting (srcpad, destpad);
1080 gst_object_unref (srcpad);
1081 gst_object_unref (destpad);
1086 /* loop through the allowed pads in the source, trying to find a
1087 * compatible destination pad */
1088 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1089 "looping through allowed src and dest pads");
1091 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1092 GST_DEBUG_PAD_NAME (srcpad));
1093 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1094 (GST_PAD_PEER (srcpad) == NULL)) {
1099 gst_object_ref (temp);
1101 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1104 if (temp && pad_link_maybe_ghosting (srcpad, temp)) {
1105 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1106 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1108 gst_object_unref (destpad);
1109 gst_object_unref (srcpad);
1110 gst_object_unref (temp);
1115 gst_object_unref (temp);
1118 /* find a better way for this mess */
1120 srcpads = g_list_next (srcpads);
1122 gst_object_unref (srcpad);
1123 srcpad = GST_PAD_CAST (srcpads->data);
1124 gst_object_ref (srcpad);
1130 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1131 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1132 gst_object_unref (srcpad);
1134 gst_object_unref (destpad);
1138 gst_object_unref (srcpad);
1142 /* loop through the existing pads in the destination */
1144 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1145 GST_DEBUG_PAD_NAME (destpad));
1146 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1147 (GST_PAD_PEER (destpad) == NULL)) {
1148 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1150 if (temp && pad_link_maybe_ghosting (temp, destpad)) {
1151 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1152 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1153 gst_object_unref (temp);
1154 gst_object_unref (destpad);
1156 gst_object_unref (srcpad);
1160 gst_object_unref (temp);
1164 destpads = g_list_next (destpads);
1166 gst_object_unref (destpad);
1167 destpad = GST_PAD_CAST (destpads->data);
1168 gst_object_ref (destpad);
1174 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1175 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1176 gst_object_unref (destpad);
1178 gst_object_unref (srcpad);
1181 gst_object_unref (destpad);
1183 gst_object_unref (srcpad);
1188 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1189 "we might have request pads on both sides, checking...");
1190 srctempls = gst_element_class_get_pad_template_list (srcclass);
1191 desttempls = gst_element_class_get_pad_template_list (destclass);
1193 if (srctempls && desttempls) {
1195 srctempl = (GstPadTemplate *) srctempls->data;
1196 if (srctempl->presence == GST_PAD_REQUEST) {
1197 for (l = desttempls; l; l = l->next) {
1198 desttempl = (GstPadTemplate *) l->data;
1199 if (desttempl->presence == GST_PAD_REQUEST &&
1200 desttempl->direction != srctempl->direction) {
1201 if (gst_caps_is_always_compatible (gst_pad_template_get_caps
1202 (srctempl), gst_pad_template_get_caps (desttempl))) {
1204 gst_element_get_request_pad (src, srctempl->name_template);
1206 gst_element_get_request_pad (dest, desttempl->name_template);
1207 if (pad_link_maybe_ghosting (srcpad, destpad)) {
1208 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1209 "linked pad %s:%s to pad %s:%s",
1210 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1211 gst_object_unref (srcpad);
1212 gst_object_unref (destpad);
1215 /* it failed, so we release the request pads */
1216 gst_element_release_request_pad (src, srcpad);
1217 gst_element_release_request_pad (dest, destpad);
1222 srctempls = srctempls->next;
1226 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1227 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1232 * gst_element_link_pads_filtered:
1233 * @src: a #GstElement containing the source pad.
1234 * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
1235 * @dest: the #GstElement containing the destination pad.
1236 * @destpadname: the name of the #GstPad in destination element or NULL for any pad.
1237 * @caps: the #GstCaps to filter the link, or #NULL for no filter.
1239 * Links the two named pads of the source and destination elements. Side effect
1240 * is that if one of the pads has no parent, it becomes a child of the parent of
1241 * the other element. If they have different parents, the link fails. If @caps
1242 * is not #NULL, makes sure that the caps of the link is a subset of @caps.
1244 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1247 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
1248 GstElement * dest, const gchar * destpadname, GstCaps * filter)
1251 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1252 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1253 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
1256 GstElement *capsfilter;
1259 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1261 GST_ERROR ("Could not make a capsfilter");
1265 parent = gst_object_get_parent (GST_OBJECT (src));
1266 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1268 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
1269 GST_ERROR ("Could not add capsfilter");
1270 gst_object_unref (capsfilter);
1271 gst_object_unref (parent);
1275 gst_object_unref (parent);
1277 g_object_set (capsfilter, "filter-caps", filter, NULL);
1279 if (gst_element_link_pads (src, srcpadname, capsfilter, "sink")
1280 && gst_element_link_pads (capsfilter, "src", dest, destpadname)) {
1283 GST_INFO ("Could not link elements");
1284 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
1285 /* will unref and unlink as appropriate */
1289 return gst_element_link_pads (src, srcpadname, dest, destpadname);
1295 * @src: a #GstElement containing the source pad.
1296 * @dest: the #GstElement containing the destination pad.
1298 * Links @src to @dest. The link must be from source to
1299 * destination; the other direction will not be tried. The function looks for
1300 * existing pads that aren't linked yet. It will request new pads if necessary.
1301 * If multiple links are possible, only one is established.
1303 * Returns: TRUE if the elements could be linked, FALSE otherwise.
1306 gst_element_link (GstElement * src, GstElement * dest)
1308 return gst_element_link_pads_filtered (src, NULL, dest, NULL, NULL);
1312 * gst_element_link_many:
1313 * @element_1: the first #GstElement in the link chain.
1314 * @element_2: the second #GstElement in the link chain.
1315 * @...: the NULL-terminated list of elements to link in order.
1317 * Chain together a series of elements. Uses gst_element_link().
1319 * Returns: TRUE on success, FALSE otherwise.
1322 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
1326 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
1327 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
1329 va_start (args, element_2);
1332 if (!gst_element_link (element_1, element_2))
1335 element_1 = element_2;
1336 element_2 = va_arg (args, GstElement *);
1345 * gst_element_unlink_pads:
1346 * @src: a #GstElement containing the source pad.
1347 * @srcpadname: the name of the #GstPad in source element.
1348 * @dest: a #GstElement containing the destination pad.
1349 * @destpadname: the name of the #GstPad in destination element.
1351 * Unlinks the two named pads of the source and destination elements.
1354 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
1355 GstElement * dest, const gchar * destpadname)
1357 GstPad *srcpad, *destpad;
1359 g_return_if_fail (src != NULL);
1360 g_return_if_fail (GST_IS_ELEMENT (src));
1361 g_return_if_fail (srcpadname != NULL);
1362 g_return_if_fail (dest != NULL);
1363 g_return_if_fail (GST_IS_ELEMENT (dest));
1364 g_return_if_fail (destpadname != NULL);
1366 /* obtain the pads requested */
1367 srcpad = gst_element_get_pad (src, srcpadname);
1368 if (srcpad == NULL) {
1369 GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
1372 destpad = gst_element_get_pad (dest, destpadname);
1373 if (srcpad == NULL) {
1374 GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
1379 /* we're satisified they can be unlinked, let's do it */
1380 gst_pad_unlink (srcpad, destpad);
1384 * gst_element_unlink_many:
1385 * @element_1: the first #GstElement in the link chain.
1386 * @element_2: the second #GstElement in the link chain.
1387 * @...: the NULL-terminated list of elements to unlink in order.
1389 * Unlinks a series of elements. Uses gst_element_unlink().
1392 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
1396 g_return_if_fail (element_1 != NULL && element_2 != NULL);
1397 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
1399 va_start (args, element_2);
1402 gst_element_unlink (element_1, element_2);
1404 element_1 = element_2;
1405 element_2 = va_arg (args, GstElement *);
1412 * gst_element_unlink:
1413 * @src: the source #GstElement to unlink.
1414 * @dest: the sink #GstElement to unlink.
1416 * Unlinks all source pads of the source element with all sink pads
1417 * of the sink element to which they are linked.
1420 gst_element_unlink (GstElement * src, GstElement * dest)
1423 gboolean done = FALSE;
1425 g_return_if_fail (GST_IS_ELEMENT (src));
1426 g_return_if_fail (GST_IS_ELEMENT (dest));
1428 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
1429 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1431 pads = gst_element_iterate_pads (src);
1435 switch (gst_iterator_next (pads, &data)) {
1436 case GST_ITERATOR_OK:
1438 GstPad *pad = GST_PAD_CAST (data);
1440 if (GST_PAD_IS_SRC (pad)) {
1441 GstPad *peerpad = gst_pad_get_peer (pad);
1443 /* see if the pad is connected and is really a pad
1446 GstElement *peerelem;
1448 peerelem = gst_pad_get_parent_element (peerpad);
1450 if (peerelem == dest) {
1451 gst_pad_unlink (pad, peerpad);
1454 gst_object_unref (peerelem);
1456 gst_object_unref (peerpad);
1459 gst_object_unref (pad);
1462 case GST_ITERATOR_RESYNC:
1463 gst_iterator_resync (pads);
1465 case GST_ITERATOR_DONE:
1469 g_assert_not_reached ();
1476 gst_element_query_position (GstElement * element, GstFormat * format,
1477 gint64 * cur, gint64 * end)
1482 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1483 g_return_val_if_fail (format != NULL, FALSE);
1485 query = gst_query_new_position (*format);
1486 ret = gst_element_query (element, query);
1489 gst_query_parse_position (query, format, cur, end);
1491 gst_query_unref (query);
1497 gst_element_query_convert (GstElement * element, GstFormat src_format,
1498 gint64 src_val, GstFormat * dest_fmt, gint64 * dest_val)
1503 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1504 g_return_val_if_fail (dest_fmt != NULL, FALSE);
1505 g_return_val_if_fail (dest_val != NULL, FALSE);
1507 if (*dest_fmt == src_format) {
1508 *dest_val = src_val;
1512 query = gst_query_new_convert (src_format, src_val, *dest_fmt);
1513 ret = gst_element_query (element, query);
1516 gst_query_parse_convert (query, NULL, NULL, dest_fmt, dest_val);
1518 gst_query_unref (query);
1526 * @srcpad: the source #GstPad to link.
1527 * @sinkpad: the sink #GstPad to link.
1529 * Checks if the source pad and the sink pad can be linked.
1530 * Both @srcpad and @sinkpad must be unlinked.
1532 * Returns: TRUE if the pads can be linked, FALSE otherwise.
1535 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1537 /* FIXME This function is gross. It's almost a direct copy of
1538 * gst_pad_link_filtered(). Any decent programmer would attempt
1539 * to merge the two functions, which I will do some day. --ds
1542 /* generic checks */
1543 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1544 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1546 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1547 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1549 /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
1550 if (GST_PAD_PEER (srcpad) != NULL) {
1551 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
1552 GST_DEBUG_PAD_NAME (srcpad));
1555 if (GST_PAD_PEER (sinkpad) != NULL) {
1556 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
1557 GST_DEBUG_PAD_NAME (sinkpad));
1560 if (!GST_PAD_IS_SRC (srcpad)) {
1561 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
1562 GST_DEBUG_PAD_NAME (srcpad));
1565 if (!GST_PAD_IS_SINK (sinkpad)) {
1566 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
1567 GST_DEBUG_PAD_NAME (sinkpad));
1570 if (GST_PAD_PARENT (srcpad) == NULL) {
1571 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
1572 GST_DEBUG_PAD_NAME (srcpad));
1575 if (GST_PAD_PARENT (sinkpad) == NULL) {
1576 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
1577 GST_DEBUG_PAD_NAME (srcpad));
1585 * gst_pad_use_fixed_caps:
1586 * @pad: the pad to use
1588 * A helper function you can use that sets the
1589 * @gst_pad_get_fixed_caps_func as the gstcaps function for the
1590 * pad. This way the function will always return the negotiated caps
1591 * or in case the pad is not negotiated, the padtemplate caps.
1593 * Use this function on a pad that, once _set_caps() has been called
1594 * on it, it cannot be renegotiated to something else.
1597 gst_pad_use_fixed_caps (GstPad * pad)
1599 gst_pad_set_getcaps_function (pad, gst_pad_get_fixed_caps_func);
1603 * gst_pad_get_fixed_caps_func:
1604 * @pad: the pad to use
1606 * A helper function you can use as a GetCaps function that
1607 * will return the currently negotiated caps or the padtemplate
1610 * Returns: The currently negotiated caps or the padtemplate.
1613 gst_pad_get_fixed_caps_func (GstPad * pad)
1617 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1619 if (GST_PAD_CAPS (pad)) {
1620 result = GST_PAD_CAPS (pad);
1622 GST_CAT_DEBUG (GST_CAT_CAPS,
1623 "using pad caps %p %" GST_PTR_FORMAT, result, result);
1625 result = gst_caps_ref (result);
1628 if (GST_PAD_PAD_TEMPLATE (pad)) {
1629 GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
1631 result = GST_PAD_TEMPLATE_CAPS (templ);
1632 GST_CAT_DEBUG (GST_CAT_CAPS,
1633 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
1636 result = gst_caps_ref (result);
1639 GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
1640 result = gst_caps_new_empty ();
1647 * gst_pad_get_parent_element:
1650 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
1651 * its parent is not an element, return NULL.
1653 * Returns: The parent of the pad. The caller has a reference on the parent, so
1654 * unref when you're finished with it.
1659 gst_pad_get_parent_element (GstPad * pad)
1663 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1665 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
1667 if (p && !GST_IS_ELEMENT (p)) {
1668 gst_object_unref (p);
1671 return GST_ELEMENT_CAST (p);
1675 * gst_flow_get_name:
1676 * @state: a #GstFlowReturn to get the name of.
1678 * Gets a string representing the given flow return.
1680 * Returns: a string with the name of the flow return.
1682 G_CONST_RETURN gchar *
1683 gst_flow_get_name (GstFlowReturn ret)
1686 case GST_FLOW_RESEND:
1687 return "need to resend buffer";
1690 /* expected failures */
1691 case GST_FLOW_NOT_LINKED:
1692 return "pad not linked";
1693 case GST_FLOW_WRONG_STATE:
1694 return "pad in wrong state";
1696 case GST_FLOW_UNEXPECTED:
1697 return "unexpected data on pad";
1698 case GST_FLOW_NOT_NEGOTIATED:
1699 return "pad not negotiated";
1700 case GST_FLOW_ERROR:
1701 return "fatal error occured";
1702 case GST_FLOW_NOT_SUPPORTED:
1703 return "unsupported function called";
1705 return "unknown error";
1710 * gst_object_default_error:
1711 * @object: a #GObject that signalled the error.
1712 * @orig: the #GstObject that initiated the error.
1713 * @error: the GError.
1714 * @debug: an additional debug information string, or NULL.
1716 * A default error function.
1718 * The default handler will simply print the error string using g_print.
1721 gst_object_default_error (GstObject * source, GError * error, gchar * debug)
1723 gchar *name = gst_object_get_path_string (source);
1725 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
1727 g_print (_("Additional debug info:\n%s\n"), debug);
1734 * @bin: the bin to add the elements to
1735 * @element_1: the first element to add to the bin
1736 * @...: additional elements to add to the bin
1738 * Adds a NULL-terminated list of elements to a bin. This function is
1739 * equivalent to calling gst_bin_add() for each member of the list.
1742 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
1746 g_return_if_fail (GST_IS_BIN (bin));
1747 g_return_if_fail (GST_IS_ELEMENT (element_1));
1749 va_start (args, element_1);
1752 gst_bin_add (bin, element_1);
1754 element_1 = va_arg (args, GstElement *);
1761 * gst_bin_remove_many:
1762 * @bin: the bin to remove the elements from
1763 * @element_1: the first element to remove from the bin
1764 * @...: NULL-terminated list of elements to remove from the bin
1766 * Remove a list of elements from a bin. This function is equivalent
1767 * to calling gst_bin_remove() with each member of the list.
1770 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
1774 g_return_if_fail (GST_IS_BIN (bin));
1775 g_return_if_fail (GST_IS_ELEMENT (element_1));
1777 va_start (args, element_1);
1780 gst_bin_remove (bin, element_1);
1782 element_1 = va_arg (args, GstElement *);
1789 get_state_func (GstElement * element, gpointer unused)
1791 GstElementStateReturn ret = GST_STATE_ASYNC;
1793 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1794 "new thread waiting on state change");
1796 /* wait indefinitely */
1797 while (ret == GST_STATE_ASYNC)
1798 ret = gst_element_get_state (element, NULL, NULL, NULL);
1800 gst_object_unref (element);
1804 * gst_bin_watch_for_state_change:
1805 * @bin: the bin to watch for state changes
1807 * Spawns a thread calling gst_element_get_state on @bin with infinite timeout.
1809 * In practice this is done because if a bin returns %GST_STATE_ASYNC from a
1810 * state change it will not commit its state until someone calls
1811 * gst_element_get_state() on it. Thus having another thread checking the bin's
1812 * state will ensure that a state-changed message gets posted on the bus
1815 * This function is admittedly a bit of a hack. Bins should always post
1816 * messages. However this behavior was broken out into this function to avoid
1817 * spawning threads when scrubbing, when the bin's state is changing quickly and
1821 gst_bin_watch_for_state_change (GstBin * bin)
1823 static GThreadPool *pool = NULL;
1824 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
1826 g_static_mutex_lock (&mutex);
1828 pool = g_thread_pool_new ((GFunc) get_state_func, NULL, -1, FALSE, NULL);
1829 g_static_mutex_unlock (&mutex);
1831 g_thread_pool_push (pool, gst_object_ref (bin), NULL);
1835 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
1836 guint arg_id, GParamFlags flags)
1838 GQuark prop_id = g_quark_from_string (prop_name);
1841 static GQuark fd_id = 0;
1842 static GQuark blocksize_id;
1843 static GQuark bytesperread_id;
1844 static GQuark dump_id;
1845 static GQuark filesize_id;
1846 static GQuark mmapsize_id;
1847 static GQuark location_id;
1848 static GQuark offset_id;
1849 static GQuark silent_id;
1850 static GQuark touch_id;
1853 fd_id = g_quark_from_static_string ("fd");
1854 blocksize_id = g_quark_from_static_string ("blocksize");
1855 bytesperread_id = g_quark_from_static_string ("bytesperread");
1856 dump_id = g_quark_from_static_string ("dump");
1857 filesize_id = g_quark_from_static_string ("filesize");
1858 mmapsize_id = g_quark_from_static_string ("mmapsize");
1859 location_id = g_quark_from_static_string ("location");
1860 offset_id = g_quark_from_static_string ("offset");
1861 silent_id = g_quark_from_static_string ("silent");
1862 touch_id = g_quark_from_static_string ("touch");
1865 if (prop_id == fd_id) {
1866 pspec = g_param_spec_int ("fd", "File-descriptor",
1867 "File-descriptor for the file being read", 0, G_MAXINT, 0, flags);
1868 } else if (prop_id == blocksize_id) {
1869 pspec = g_param_spec_ulong ("blocksize", "Block Size",
1870 "Block size to read per buffer", 0, G_MAXULONG, 4096, flags);
1872 } else if (prop_id == bytesperread_id) {
1873 pspec = g_param_spec_int ("bytesperread", "Bytes per read",
1874 "Number of bytes to read per buffer", G_MININT, G_MAXINT, 0, flags);
1876 } else if (prop_id == dump_id) {
1877 pspec = g_param_spec_boolean ("dump", "Dump",
1878 "Dump bytes to stdout", FALSE, flags);
1880 } else if (prop_id == filesize_id) {
1881 pspec = g_param_spec_int64 ("filesize", "File Size",
1882 "Size of the file being read", 0, G_MAXINT64, 0, flags);
1884 } else if (prop_id == mmapsize_id) {
1885 pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
1886 "Size in bytes of mmap()d regions", 0, G_MAXULONG, 4 * 1048576, flags);
1888 } else if (prop_id == location_id) {
1889 pspec = g_param_spec_string ("location", "File Location",
1890 "Location of the file to read", NULL, flags);
1892 } else if (prop_id == offset_id) {
1893 pspec = g_param_spec_int64 ("offset", "File Offset",
1894 "Byte offset of current read pointer", 0, G_MAXINT64, 0, flags);
1896 } else if (prop_id == silent_id) {
1897 pspec = g_param_spec_boolean ("silent", "Silent", "Don't produce events",
1900 } else if (prop_id == touch_id) {
1901 pspec = g_param_spec_boolean ("touch", "Touch read data",
1902 "Touch data to force disk read before " "push ()", TRUE, flags);
1904 g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
1905 prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
1910 g_object_class_install_property (klass, arg_id, pspec);
1915 * gst_element_class_install_std_props:
1916 * @klass: the #GstElementClass to add the properties to.
1917 * @first_name: the name of the first property.
1918 * in a NULL terminated
1919 * @...: the id and flags of the first property, followed by
1920 * further 'name', 'id', 'flags' triplets and terminated by NULL.
1922 * Adds a list of standardized properties with types to the @klass.
1923 * the id is for the property switch in your get_prop method, and
1924 * the flags determine readability / writeability.
1927 gst_element_class_install_std_props (GstElementClass * klass,
1928 const gchar * first_name, ...)
1934 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1936 va_start (args, first_name);
1941 int arg_id = va_arg (args, int);
1942 int flags = va_arg (args, int);
1944 gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id,
1947 name = va_arg (args, char *);
1956 * @buf1: a first source #GstBuffer to merge.
1957 * @buf2: the second source #GstBuffer to merge.
1959 * Create a new buffer that is the concatenation of the two source
1960 * buffers. The original source buffers will not be modified or
1961 * unref'd. Make sure you unref the source buffers if they are not used
1962 * anymore afterwards.
1964 * If the buffers point to contiguous areas of memory, the buffer
1965 * is created without copying the data.
1967 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
1970 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
1974 /* we're just a specific case of the more general gst_buffer_span() */
1975 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
1982 * @buf1: a first source #GstBuffer to join.
1983 * @buf2: the second source #GstBuffer to join.
1985 * Create a new buffer that is the concatenation of the two source
1986 * buffers, and takes ownership of the original source buffers.
1988 * If the buffers point to contiguous areas of memory, the buffer
1989 * is created without copying the data.
1991 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
1994 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
1998 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
1999 gst_buffer_unref (buf1);
2000 gst_buffer_unref (buf2);
2008 * @dest: buffer to stamp
2009 * @src: buffer to stamp from
2011 * Copies additional information (timestamps and offsets) from one buffer to
2015 gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src)
2017 g_return_if_fail (dest != NULL);
2018 g_return_if_fail (src != NULL);
2020 GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
2021 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
2022 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
2023 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
2027 intersect_caps_func (GstPad * pad, GValue * ret, GstPad * orig)
2030 GstCaps *peercaps, *existing;
2032 existing = g_value_get_pointer (ret);
2033 peercaps = gst_pad_peer_get_caps (pad);
2034 if (peercaps == NULL)
2035 peercaps = gst_caps_new_any ();
2036 g_value_set_pointer (ret, gst_caps_intersect (existing, peercaps));
2037 gst_caps_unref (existing);
2038 gst_caps_unref (peercaps);
2044 * gst_pad_proxy_getcaps:
2045 * @pad: a #GstPad to proxy.
2047 * Calls gst_pad_get_allowed_caps() for every other pad belonging to the
2048 * same element as @pad, and returns the intersection of the results.
2050 * This function is useful as a default getcaps function for an element
2051 * that can handle any stream format, but requires all its pads to have
2052 * the same caps. Two such elements are tee and aggregator.
2054 * Returns: the intersection of the other pads' allowed caps.
2057 gst_pad_proxy_getcaps (GstPad * pad)
2059 GstElement *element;
2060 GstCaps *caps, *intersected;
2062 GstIteratorResult res;
2063 GValue ret = { 0, };
2065 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2067 GST_DEBUG ("proxying getcaps for %s:%s", GST_DEBUG_PAD_NAME (pad));
2069 element = gst_pad_get_parent_element (pad);
2070 if (element == NULL)
2073 iter = gst_element_iterate_pads (element);
2075 g_value_init (&ret, G_TYPE_POINTER);
2076 g_value_set_pointer (&ret, gst_caps_new_any ());
2078 res = gst_iterator_fold (iter, (GstIteratorFoldFunction) intersect_caps_func,
2080 gst_iterator_free (iter);
2082 if (res != GST_ITERATOR_DONE) {
2083 g_warning ("Pad list changed during capsnego for element %s",
2084 GST_ELEMENT_NAME (element));
2088 caps = g_value_get_pointer (&ret);
2089 g_value_unset (&ret);
2091 intersected = gst_caps_intersect (caps, gst_pad_get_pad_template_caps (pad));
2092 gst_caps_unref (caps);
2104 link_fold_func (GstPad * pad, GValue * ret, LinkData * data)
2106 gboolean success = TRUE;
2108 if (pad != data->orig) {
2109 success = gst_pad_set_caps (pad, data->caps);
2110 g_value_set_boolean (ret, success);
2117 * gst_pad_proxy_setcaps
2118 * @pad: a #GstPad to proxy from
2119 * @caps: the #GstCaps to link with
2121 * Calls gst_pad_set_caps() for every other pad belonging to the
2122 * same element as @pad. If gst_pad_set_caps() fails on any pad,
2123 * the proxy setcaps fails. May be used only during negotiation.
2125 * Returns: TRUE if sucessful
2128 gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps)
2130 GstElement *element;
2132 GstIteratorResult res;
2133 GValue ret = { 0, };
2136 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2137 g_return_val_if_fail (caps != NULL, FALSE);
2139 GST_DEBUG ("proxying pad link for %s:%s", GST_DEBUG_PAD_NAME (pad));
2141 element = gst_pad_get_parent_element (pad);
2143 iter = gst_element_iterate_pads (element);
2145 g_value_init (&ret, G_TYPE_BOOLEAN);
2146 g_value_set_boolean (&ret, TRUE);
2150 res = gst_iterator_fold (iter, (GstIteratorFoldFunction) link_fold_func,
2152 gst_iterator_free (iter);
2154 if (res != GST_ITERATOR_DONE) {
2155 g_warning ("Pad list changed during proxy_pad_link for element %s",
2156 GST_ELEMENT_NAME (element));
2160 /* ok not to unset the gvalue */
2161 return g_value_get_boolean (&ret);
2165 * gst_pad_query_position:
2166 * @pad: a #GstPad to invoke the position query on.
2167 * @format: a pointer to the #GstFormat asked for.
2168 * On return contains the #GstFormat used.
2169 * @cur: A location in which to store the current position, or NULL.
2170 * @end: A location in which to store the end position (length), or NULL.
2172 * Queries a pad for the stream position and length.
2174 * Returns: TRUE if the query could be performed.
2177 gst_pad_query_position (GstPad * pad, GstFormat * format, gint64 * cur,
2183 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2184 g_return_val_if_fail (format != NULL, FALSE);
2186 query = gst_query_new_position (*format);
2187 ret = gst_pad_query (pad, query);
2190 gst_query_parse_position (query, format, cur, end);
2192 gst_query_unref (query);
2198 * gst_pad_query_convert:
2199 * @pad: a #GstPad to invoke the convert query on.
2200 * @src_format: a #GstFormat to convert from.
2201 * @src_val: a value to convert.
2202 * @dest_format: a pointer to the #GstFormat to convert to.
2203 * @dest_val: a pointer to the result.
2205 * Queries a pad to convert @src_val in @src_format to @dest_format.
2207 * Returns: TRUE if the query could be performed.
2210 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2211 GstFormat * dest_fmt, gint64 * dest_val)
2216 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2217 g_return_val_if_fail (dest_fmt != NULL, FALSE);
2218 g_return_val_if_fail (dest_val != NULL, FALSE);
2220 if (*dest_fmt == src_format) {
2221 *dest_val = src_val;
2225 query = gst_query_new_convert (src_format, src_val, *dest_fmt);
2226 ret = gst_pad_query (pad, query);
2229 gst_query_parse_convert (query, NULL, NULL, dest_fmt, dest_val);
2231 gst_query_unref (query);
2237 * gst_atomic_int_set:
2238 * @atomic_int: pointer to an atomic integer
2239 * @value: value to set
2241 * Unconditionally sets the atomic integer to @value.
2244 gst_atomic_int_set (gint * atomic_int, gint value)
2248 *atomic_int = value;
2249 /* read acts as a memory barrier */
2250 ignore = g_atomic_int_get (atomic_int);
2254 * gst_pad_add_data_probe:
2255 * @pad: pad to add the data probe handler to
2256 * @handler: function to call when data is passed over pad
2257 * @data: data to pass along with the handler
2259 * Connects a signal handler to the pad's have-data signal, and increases
2260 * the do_{buffer,event}_signals number on the pads so that those
2261 * signals are actually fired.
2263 * Returns: The handler id
2267 gst_pad_add_data_probe (GstPad * pad, GCallback handler, gpointer data)
2271 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2272 g_return_val_if_fail (handler != NULL, 0);
2275 sigid = g_signal_connect (pad, "have-data", handler, data);
2276 GST_PAD_DO_EVENT_SIGNALS (pad)++;
2277 GST_PAD_DO_BUFFER_SIGNALS (pad)++;
2278 GST_DEBUG ("adding data probe to pad %s:%s, now %d data, %d event probes",
2279 GST_DEBUG_PAD_NAME (pad),
2280 GST_PAD_DO_BUFFER_SIGNALS (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2287 * gst_pad_add_event_probe:
2288 * @pad: pad to add the event probe handler to
2289 * @handler: function to call when data is passed over pad
2290 * @data: data to pass along with the handler
2292 * Connects a signal handler to the pad's have-data signal, and increases
2293 * the do_event_signals number on the pads so that this signal
2294 * is actually fired.
2296 * Returns: The handler id
2300 gst_pad_add_event_probe (GstPad * pad, GCallback handler, gpointer data)
2304 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2305 g_return_val_if_fail (handler != NULL, 0);
2308 sigid = g_signal_connect (pad, "have-data", handler, data);
2309 GST_PAD_DO_EVENT_SIGNALS (pad)++;
2310 GST_DEBUG ("adding event probe to pad %s:%s, now %d probes",
2311 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2318 * gst_pad_add_buffer_probe:
2319 * @pad: pad to add the buffer probe handler to
2320 * @handler: function to call when data is passed over pad
2321 * @data: data to pass along with the handler
2323 * Connects a signal handler to the pad's have-data signal, and increases
2324 * the do_buffer_signals number on the pads so that this signal
2325 * is actually fired.
2327 * Returns: The handler id
2331 gst_pad_add_buffer_probe (GstPad * pad, GCallback handler, gpointer data)
2335 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2336 g_return_val_if_fail (handler != NULL, 0);
2339 sigid = g_signal_connect (pad, "have-data", handler, data);
2340 GST_PAD_DO_BUFFER_SIGNALS (pad)++;
2341 GST_DEBUG ("adding buffer probe to pad %s:%s, now %d probes",
2342 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_BUFFER_SIGNALS (pad));
2349 * gst_pad_remove_data_probe:
2350 * @pad: pad to remove the data probe handler from
2351 * @handler: function that was assigned to the signal
2352 * @data: data that was assigned to the signal handler
2354 * Unconnects a signal handler to the pad's have-data signal, and decreases
2355 * the do_{buffer,event}_signals number on the pads so that those
2356 * signals are actually no more fired if no signals are connected.
2360 gst_pad_remove_data_probe (GstPad * pad, GCallback handler, gpointer data)
2364 g_return_if_fail (GST_IS_PAD (pad));
2365 g_return_if_fail (handler != NULL);
2368 count = g_signal_handlers_disconnect_by_func (pad, handler, data);
2369 GST_PAD_DO_BUFFER_SIGNALS (pad) -= count;
2370 GST_PAD_DO_EVENT_SIGNALS (pad) -= count;
2372 ("removing %d data probes from pad %s:%s, now %d event, %d buffer probes",
2373 count, GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad),
2374 GST_PAD_DO_BUFFER_SIGNALS (pad));
2379 * gst_pad_remove_event_probe:
2380 * @pad: pad to remove the event probe handler from
2381 * @handler: function that was assigned to the signal
2382 * @data: data that was assigned to the signal handler
2384 * Unconnects a signal handler to the pad's have-data signal, and decreases
2385 * the do_event_signals number on the pads so that this signal is
2386 * actually no more fired if no signals are connected.
2390 gst_pad_remove_event_probe (GstPad * pad, GCallback handler, gpointer data)
2394 g_return_if_fail (GST_IS_PAD (pad));
2395 g_return_if_fail (handler != NULL);
2398 count = g_signal_handlers_disconnect_by_func (pad, handler, data);
2399 GST_PAD_DO_EVENT_SIGNALS (pad) -= count;
2400 GST_DEBUG ("removing %d event probes from pad %s:%s, now %d event probes",
2401 count, GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2406 * gst_pad_remove_buffer_probe:
2407 * @pad: pad to remove the buffer probe handler from
2408 * @handler: function that was assigned to the signal
2409 * @data: data that was assigned to the signal handler
2411 * Unconnects a signal handler to the pad's have-data signal, and decreases
2412 * the emit_buffer_signals number on the pads so that this signal is
2413 * actually no more fired if no signals are connected.
2417 gst_pad_remove_buffer_probe (GstPad * pad, GCallback handler, gpointer data)
2421 g_return_if_fail (GST_IS_PAD (pad));
2422 g_return_if_fail (handler != NULL);
2425 count = g_signal_handlers_disconnect_by_func (pad, handler, data);
2426 GST_PAD_DO_BUFFER_SIGNALS (pad) -= count;
2427 GST_DEBUG ("removing %d buffer probes from pad %s:%s, now %d buffer probes",
2428 count, GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_BUFFER_SIGNALS (pad));
2433 * gst_element_found_tags_for_pad:
2434 * @element: element for which to post taglist to bus.
2435 * @pad: pad on which to push tag-event.
2436 * @list: the taglist to post on the bus and create event from.
2438 * Posts a message to the bus that new tags were found and pushes the
2439 * tags as event. Takes ownership of the taglist.
2442 gst_element_found_tags_for_pad (GstElement * element,
2443 GstPad * pad, GstTagList * list)
2445 g_return_if_fail (element != NULL);
2446 g_return_if_fail (pad != NULL);
2447 g_return_if_fail (list != NULL);
2449 gst_pad_push_event (pad, gst_event_new_tag (gst_tag_list_copy (list)));
2450 gst_element_post_message (element,
2451 gst_message_new_tag (GST_OBJECT (element), list));
2455 push_and_ref (GstPad * pad, GstEvent * event)
2457 gst_pad_push_event (pad, gst_event_ref (event));
2461 * gst_element_found_tags:
2462 * @element: element for which we found the tags.
2463 * @list: list of tags.
2465 * Posts a message to the bus that new tags were found, and pushes an event
2466 * to all sourcepads. Takes ownership of the taglist.
2469 gst_element_found_tags (GstElement * element, GstTagList * list)
2474 g_return_if_fail (element != NULL);
2475 g_return_if_fail (list != NULL);
2477 iter = gst_element_iterate_src_pads (element);
2478 event = gst_event_new_tag (gst_tag_list_copy (list));
2479 gst_iterator_foreach (iter, (GFunc) push_and_ref, event);
2480 gst_iterator_free (iter);
2481 gst_event_unref (event);
2483 gst_element_post_message (element,
2484 gst_message_new_tag (GST_OBJECT (element), list));