2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2002 Thomas Vander Stichele <thomas@apestaart.org>
6 * gstutils.c: Utility functions: gtk_get_property stuff, etc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 * @short_description: Various utility functions
28 * When defining own plugins, use the GST_BOILERPLATE ease gobject creation.
31 #include "gst_private.h"
35 #include "gstghostpad.h"
39 #include "gst-i18n-lib.h"
44 * @mem: a pointer to the memory to dump
45 * @size: the size of the memory block to dump
47 * Dumps the memory block into a hex representation. Useful for debugging.
50 gst_util_dump_mem (const guchar * mem, guint size)
53 GString *string = g_string_sized_new (50);
54 GString *chars = g_string_sized_new (18);
58 if (g_ascii_isprint (mem[i]))
59 g_string_append_printf (chars, "%c", mem[i]);
61 g_string_append_printf (chars, ".");
63 g_string_append_printf (string, "%02x ", mem[i]);
68 if (j == 16 || i == size) {
69 g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
70 string->str, chars->str);
71 g_string_set_size (string, 0);
72 g_string_set_size (chars, 0);
76 g_string_free (string, TRUE);
77 g_string_free (chars, TRUE);
82 * gst_util_set_value_from_string:
83 * @value: the value to set
84 * @value_str: the string to get the value from
86 * Converts the string to the type of the value and
87 * sets the value with it.
90 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
94 g_return_if_fail (value != NULL);
95 g_return_if_fail (value_str != NULL);
97 GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
98 g_type_name (G_VALUE_TYPE (value)));
100 switch (G_VALUE_TYPE (value)) {
102 g_value_set_string (value, value_str);
108 sscanf_ret = sscanf (value_str, "%d", &i);
109 g_return_if_fail (sscanf_ret == 1);
110 g_value_set_int (value, i);
116 sscanf_ret = sscanf (value_str, "%u", &i);
117 g_return_if_fail (sscanf_ret == 1);
118 g_value_set_uint (value, i);
124 sscanf_ret = sscanf (value_str, "%ld", &i);
125 g_return_if_fail (sscanf_ret == 1);
126 g_value_set_long (value, i);
132 sscanf_ret = sscanf (value_str, "%lu", &i);
133 g_return_if_fail (sscanf_ret == 1);
134 g_value_set_ulong (value, i);
137 case G_TYPE_BOOLEAN:{
140 if (!g_ascii_strncasecmp ("true", value_str, 4))
142 g_value_set_boolean (value, i);
148 sscanf_ret = sscanf (value_str, "%c", &i);
149 g_return_if_fail (sscanf_ret == 1);
150 g_value_set_char (value, i);
156 sscanf_ret = sscanf (value_str, "%c", &i);
157 g_return_if_fail (sscanf_ret == 1);
158 g_value_set_uchar (value, i);
164 sscanf_ret = sscanf (value_str, "%f", &i);
165 g_return_if_fail (sscanf_ret == 1);
166 g_value_set_float (value, i);
172 sscanf_ret = sscanf (value_str, "%g", &i);
173 g_return_if_fail (sscanf_ret == 1);
174 g_value_set_double (value, (gdouble) i);
183 * gst_util_set_object_arg:
184 * @object: the object to set the argument of
185 * @name: the name of the argument to set
186 * @value: the string value to set
188 * Convertes the string value to the type of the objects argument and
189 * sets the argument with it.
192 gst_util_set_object_arg (GObject * object, const gchar * name,
198 GParamSpec *paramspec;
201 g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
207 GST_DEBUG ("paramspec->flags is %d, paramspec->value_type is %d",
208 paramspec->flags, (gint) paramspec->value_type);
210 if (paramspec->flags & G_PARAM_WRITABLE) {
211 switch (paramspec->value_type) {
213 g_object_set (G_OBJECT (object), name, value, NULL);
219 sscanf_ret = sscanf (value, "%d", &i);
220 g_return_if_fail (sscanf_ret == 1);
221 g_object_set (G_OBJECT (object), name, i, NULL);
227 sscanf_ret = sscanf (value, "%u", &i);
228 g_return_if_fail (sscanf_ret == 1);
229 g_object_set (G_OBJECT (object), name, i, NULL);
235 sscanf_ret = sscanf (value, "%ld", &i);
236 g_return_if_fail (sscanf_ret == 1);
237 g_object_set (G_OBJECT (object), name, i, NULL);
243 sscanf_ret = sscanf (value, "%lu", &i);
244 g_return_if_fail (sscanf_ret == 1);
245 g_object_set (G_OBJECT (object), name, i, NULL);
248 case G_TYPE_BOOLEAN:{
251 if (!g_ascii_strncasecmp ("true", value, 4))
253 g_object_set (G_OBJECT (object), name, i, NULL);
259 sscanf_ret = sscanf (value, "%c", &i);
260 g_return_if_fail (sscanf_ret == 1);
261 g_object_set (G_OBJECT (object), name, i, NULL);
267 sscanf_ret = sscanf (value, "%c", &i);
268 g_return_if_fail (sscanf_ret == 1);
269 g_object_set (G_OBJECT (object), name, i, NULL);
275 sscanf_ret = sscanf (value, "%f", &i);
276 g_return_if_fail (sscanf_ret == 1);
277 g_object_set (G_OBJECT (object), name, i, NULL);
283 sscanf_ret = sscanf (value, "%g", &i);
284 g_return_if_fail (sscanf_ret == 1);
285 g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
289 if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
292 sscanf_ret = sscanf (value, "%d", &i);
293 g_return_if_fail (sscanf_ret == 1);
294 g_object_set (G_OBJECT (object), name, i, NULL);
302 /* work around error C2520: conversion from unsigned __int64 to double
303 * not implemented, use signed __int64
305 * These are implemented as functions because on some platforms a 64bit int to
306 * double conversion is not defined/implemented.
310 gst_util_guint64_to_gdouble (guint64 value)
312 if (value & G_GINT64_CONSTANT (0x8000000000000000))
313 return (gdouble) ((gint64) value) + (gdouble) 18446744073709551616.;
315 return (gdouble) ((gint64) value);
319 gst_util_gdouble_to_guint64 (gdouble value)
321 if (value < (gdouble) 9223372036854775808.) /* 1 << 63 */
322 return ((guint64) ((gint64) value));
324 value -= (gdouble) 18446744073709551616.;
325 return ((guint64) ((gint64) value));
328 /* convenience struct for getting high and low uint32 parts of
335 #if G_BYTE_ORDER == G_BIG_ENDIAN
343 /* based on Hacker's Delight p152 */
345 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
347 GstUInt64 q1, q0, rhat;
348 GstUInt64 v, cmp1, cmp2;
353 /* count number of leading zeroes, we know they must be in the high
354 * part of denom since denom > G_MAXUINT32. */
355 s = v.l.high | (v.l.high >> 1);
359 s = ~(s | (s >> 16));
360 s = s - ((s >> 1) & 0x55555555);
361 s = (s & 0x33333333) + ((s >> 2) & 0x33333333);
362 s = (s + (s >> 4)) & 0x0f0f0f0f;
364 s = (s + (s >> 16)) & 0x3f;
367 /* normalize divisor and dividend */
369 c1.ll = (c1.ll << s) | (c0.l.high >> (32 - s));
373 q1.ll = c1.ll / v.l.high;
374 rhat.ll = c1.ll - q1.ll * v.l.high;
376 cmp1.l.high = rhat.l.low;
377 cmp1.l.low = c0.l.high;
378 cmp2.ll = q1.ll * v.l.low;
380 while (q1.l.high || cmp2.ll > cmp1.ll) {
385 cmp1.l.high = rhat.l.low;
388 c1.l.high = c1.l.low;
389 c1.l.low = c0.l.high;
390 c1.ll -= q1.ll * v.ll;
391 q0.ll = c1.ll / v.l.high;
392 rhat.ll = c1.ll - q0.ll * v.l.high;
394 cmp1.l.high = rhat.l.low;
395 cmp1.l.low = c0.l.low;
396 cmp2.ll = q0.ll * v.l.low;
398 while (q0.l.high || cmp2.ll > cmp1.ll) {
403 cmp1.l.high = rhat.l.low;
406 q0.l.high += q1.l.low;
412 gst_util_uint64_scale_int64 (guint64 val, guint64 num, guint64 denom)
414 GstUInt64 a0, a1, b0, b1, c0, ct, c1, result;
421 /* do 128 bits multiply
429 * -------------------
432 a0.ll = (guint64) v.l.low * n.l.low;
433 a1.ll = (guint64) v.l.low * n.l.high;
434 b0.ll = (guint64) v.l.high * n.l.low;
435 b1.ll = (guint64) v.l.high * n.l.high;
437 /* and sum together with carry into 128 bits c1, c0 */
439 ct.ll = (guint64) a0.l.high + a1.l.low + b0.l.low;
440 c0.l.high = ct.l.low;
441 c1.ll = (guint64) a1.l.high + b0.l.high + ct.l.high + b1.ll;
443 /* if high bits bigger than denom, we overflow */
447 /* shortcut for division by 1, c1.ll should be 0 because of the
448 * overflow check above. */
452 /* and 128/64 bits division, result fits 64 bits */
453 if (denom <= G_MAXUINT32) {
454 guint32 den = (guint32) denom;
456 /* easy case, (c1,c0)128/(den)32 division */
458 c1.l.high = c1.ll % den;
459 c1.l.low = c0.l.high;
460 c0.l.high = c1.ll % den;
461 result.l.high = c1.ll / den;
462 result.l.low = c0.ll / den;
464 result.ll = gst_util_div128_64 (c1, c0, denom);
475 * gst_util_uint64_scale:
476 * @val: the number to scale
477 * @num: the numerator of the scale ratio
478 * @denom: the denominator of the scale ratio
480 * Scale @val by @num / @denom, trying to avoid overflows.
482 * This function can potentially be very slow if denom > G_MAXUINT32.
484 * Returns: @val * @num / @denom, trying to avoid overflows.
485 * In the case of an overflow, this function returns G_MAXUINT64.
488 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom)
490 g_return_val_if_fail (denom != 0, G_MAXUINT64);
495 if (num == 1 && denom == 1)
498 /* if the denom is high, we need to do a 64 muldiv */
499 if (denom > G_MAXINT32)
502 /* if num and denom are low we can do a 32 bit muldiv */
503 if (num <= G_MAXINT32)
506 /* val and num are high, we need 64 muldiv */
507 if (val > G_MAXINT32)
510 /* val is low and num is high, we can swap them and do 32 muldiv */
511 return gst_util_uint64_scale_int (num, (gint) val, (gint) denom);
514 return gst_util_uint64_scale_int (val, (gint) num, (gint) denom);
517 /* to the more heavy implementations... */
518 return gst_util_uint64_scale_int64 (val, num, denom);
522 * gst_util_uint64_scale_int:
523 * @val: guint64 (such as a #GstClockTime) to scale.
524 * @num: numerator of the scale factor.
525 * @denom: denominator of the scale factor.
527 * Scale a guint64 by a factor expressed as a fraction (num/denom), avoiding
528 * overflows and loss of precision.
530 * @num and @denom must be positive integers. @denom cannot be 0.
532 * Returns: @val * @num / @denom, avoiding overflow and loss of precision.
533 * In the case of an overflow, this function returns G_MAXUINT64.
536 gst_util_uint64_scale_int (guint64 val, gint num, gint denom)
541 g_return_val_if_fail (denom > 0, G_MAXUINT64);
542 g_return_val_if_fail (num >= 0, G_MAXUINT64);
547 if (num == 1 && denom == 1)
550 if (val <= G_MAXUINT32)
552 return val * num / denom;
554 /* do 96 bits mult/div */
556 result.ll = ((guint64) low.l.low) * num;
557 high.ll = ((guint64) low.l.high) * num + (result.l.high);
559 low.ll = high.ll / denom;
560 result.l.high = high.ll % denom;
564 if (low.ll + result.l.high > G_MAXUINT32)
567 result.l.high += low.l.low;
577 /* -----------------------------------------------------
579 * The following code will be moved out of the main
580 * gstreamer library someday.
586 string_append_indent (GString * str, gint count)
590 for (xx = 0; xx < count; xx++)
591 g_string_append_c (str, ' ');
595 * gst_print_pad_caps:
596 * @buf: the buffer to print the caps in
597 * @indent: initial indentation
598 * @pad: the pad to print the caps from
600 * Write the pad capabilities in a human readable format into
604 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
611 string_append_indent (buf, indent);
612 g_string_printf (buf, "%s:%s has no capabilities",
613 GST_DEBUG_PAD_NAME (pad));
617 s = gst_caps_to_string (caps);
618 g_string_append (buf, s);
624 * gst_print_element_args:
625 * @buf: the buffer to print the args in
626 * @indent: initial indentation
627 * @element: the element to print the args of
629 * Print the element argument in a human readable format in the given
633 gst_print_element_args (GString * buf, gint indent, GstElement * element)
636 GValue value = { 0, }; /* the important thing is that value.type = 0 */
638 GParamSpec *spec, **specs, **walk;
640 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
643 for (walk = specs; *walk; walk++) {
645 if (width < strlen (spec->name))
646 width = strlen (spec->name);
649 for (walk = specs; *walk; walk++) {
652 if (spec->flags & G_PARAM_READABLE) {
653 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
654 g_object_get_property (G_OBJECT (element), spec->name, &value);
655 str = g_strdup_value_contents (&value);
656 g_value_unset (&value);
658 str = g_strdup ("Parameter not readable.");
661 string_append_indent (buf, indent);
662 g_string_append (buf, spec->name);
663 string_append_indent (buf, 2 + width - strlen (spec->name));
664 g_string_append (buf, str);
665 g_string_append_c (buf, '\n');
674 * gst_element_create_all_pads:
675 * @element: a #GstElement to create pads for
677 * Creates a pad for each pad template that is always available.
678 * This function is only useful during object intialization of
679 * subclasses of #GstElement.
682 gst_element_create_all_pads (GstElement * element)
686 /* FIXME: lock element */
689 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
690 (G_OBJECT_GET_CLASS (element)));
693 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
695 if (padtempl->presence == GST_PAD_ALWAYS) {
698 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
700 gst_element_add_pad (element, pad);
702 padlist = padlist->next;
707 * gst_element_get_compatible_pad_template:
708 * @element: a #GstElement to get a compatible pad template for.
709 * @compattempl: the #GstPadTemplate to find a compatible template for.
711 * Retrieves a pad template from @element that is compatible with @compattempl.
712 * Pads from compatible templates can be linked together.
714 * Returns: a compatible #GstPadTemplate, or NULL if none was found. No
715 * unreferencing is necessary.
718 gst_element_get_compatible_pad_template (GstElement * element,
719 GstPadTemplate * compattempl)
721 GstPadTemplate *newtempl = NULL;
723 GstElementClass *class;
725 g_return_val_if_fail (element != NULL, NULL);
726 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
727 g_return_val_if_fail (compattempl != NULL, NULL);
729 class = GST_ELEMENT_GET_CLASS (element);
731 padlist = gst_element_class_get_pad_template_list (class);
733 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
734 "Looking for a suitable pad template in %s out of %d templates...",
735 GST_ELEMENT_NAME (element), g_list_length (padlist));
738 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
739 GstCaps *intersection;
743 * Check direction (must be opposite)
746 GST_CAT_LOG (GST_CAT_CAPS,
747 "checking pad template %s", padtempl->name_template);
748 if (padtempl->direction != compattempl->direction) {
749 GST_CAT_DEBUG (GST_CAT_CAPS,
750 "compatible direction: found %s pad template \"%s\"",
751 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
752 padtempl->name_template);
754 GST_CAT_DEBUG (GST_CAT_CAPS,
755 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
756 GST_CAT_DEBUG (GST_CAT_CAPS,
757 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
759 intersection = gst_caps_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
760 GST_PAD_TEMPLATE_CAPS (padtempl));
762 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible %" GST_PTR_FORMAT,
763 (intersection ? "" : "not "), intersection);
765 if (!gst_caps_is_empty (intersection))
767 gst_caps_unref (intersection);
772 padlist = g_list_next (padlist);
775 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
776 "Returning new pad template %p", newtempl);
778 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
784 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
787 GstPad *newpad = NULL;
788 GstElementClass *oclass;
790 oclass = GST_ELEMENT_GET_CLASS (element);
792 if (oclass->request_new_pad)
793 newpad = (oclass->request_new_pad) (element, templ, name);
796 gst_object_ref (newpad);
804 * gst_element_get_pad_from_template:
805 * @element: a #GstElement.
806 * @templ: a #GstPadTemplate belonging to @element.
808 * Gets a pad from @element described by @templ. If the presence of @templ is
809 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
812 * Returns: the #GstPad, or NULL if one could not be found or created.
815 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
818 GstPadPresence presence;
820 /* If this function is ever exported, we need check the validity of `element'
821 * and `templ', and to make sure the template actually belongs to the
824 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
828 case GST_PAD_SOMETIMES:
829 ret = gst_element_get_static_pad (element, templ->name_template);
830 if (!ret && presence == GST_PAD_ALWAYS)
832 ("Element %s has an ALWAYS template %s, but no pad of the same name",
833 GST_OBJECT_NAME (element), templ->name_template);
836 case GST_PAD_REQUEST:
837 ret = gst_element_request_pad (element, templ, NULL);
845 * gst_element_request_compatible_pad:
846 * @element: a #GstElement.
847 * @templ: the #GstPadTemplate to which the new pad should be able to link.
849 * Requests a pad from @element. The returned pad should be unlinked and
850 * compatible with @templ. Might return an existing pad, or request a new one.
852 * Returns: a #GstPad, or %NULL if one could not be found or created.
855 gst_element_request_compatible_pad (GstElement * element,
856 GstPadTemplate * templ)
858 GstPadTemplate *templ_new;
861 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
862 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
864 /* FIXME: should really loop through the templates, testing each for
865 * compatibility and pad availability. */
866 templ_new = gst_element_get_compatible_pad_template (element, templ);
868 pad = gst_element_get_pad_from_template (element, templ_new);
870 /* This can happen for non-request pads. No need to unref. */
871 if (pad && GST_PAD_PEER (pad))
878 * gst_element_get_compatible_pad:
879 * @element: a #GstElement in which the pad should be found.
880 * @pad: the #GstPad to find a compatible one for.
881 * @caps: the #GstCaps to use as a filter.
883 * Looks for an unlinked pad to which the given pad can link. It is not
884 * guaranteed that linking the pads will work, though it should work in most
887 * Returns: the #GstPad to which a link can be made, or %NULL if one cannot be
891 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
892 const GstCaps * caps)
895 GstPadTemplate *templ;
897 GstPad *foundpad = NULL;
900 /* FIXME check for caps compatibility */
902 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
903 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
905 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
906 "finding pad in %s compatible with %s:%s",
907 GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
909 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
912 /* try to get an existing unlinked pad */
913 pads = gst_element_iterate_pads (element);
917 switch (gst_iterator_next (pads, &padptr)) {
918 case GST_ITERATOR_OK:
923 current = GST_PAD (padptr);
925 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
926 GST_DEBUG_PAD_NAME (current));
928 peer = gst_pad_get_peer (current);
930 if (peer == NULL && gst_pad_can_link (pad, current)) {
932 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
933 "found existing unlinked pad %s:%s",
934 GST_DEBUG_PAD_NAME (current));
936 gst_iterator_free (pads);
940 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
942 gst_object_unref (current);
944 gst_object_unref (peer);
948 case GST_ITERATOR_DONE:
951 case GST_ITERATOR_RESYNC:
952 gst_iterator_resync (pads);
954 case GST_ITERATOR_ERROR:
955 g_assert_not_reached ();
959 gst_iterator_free (pads);
961 /* try to create a new one */
962 /* requesting is a little crazy, we need a template. Let's create one */
963 templcaps = gst_pad_get_caps (pad);
965 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
966 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
967 foundpad = gst_element_request_compatible_pad (element, templ);
968 gst_object_unref (templ);
971 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
972 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
976 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
977 "Could not find a compatible pad to link to %s:%s",
978 GST_DEBUG_PAD_NAME (pad));
983 * gst_element_state_get_name:
984 * @state: a #GstState to get the name of.
986 * Gets a string representing the given state.
988 * Returns: a string with the name of the state.
991 gst_element_state_get_name (GstState state)
994 #ifdef GST_DEBUG_COLOR
995 case GST_STATE_VOID_PENDING:
996 return "VOID_PENDING";
999 return "\033[01;34mNULL\033[00m";
1001 case GST_STATE_READY:
1002 return "\033[01;31mREADY\033[00m";
1004 case GST_STATE_PLAYING:
1005 return "\033[01;32mPLAYING\033[00m";
1007 case GST_STATE_PAUSED:
1008 return "\033[01;33mPAUSED\033[00m";
1011 /* This is a memory leak */
1012 return g_strdup_printf ("\033[01;35;41mUNKNOWN!\033[00m(%d)", state);
1014 case GST_STATE_VOID_PENDING:
1015 return "VOID_PENDING";
1017 case GST_STATE_NULL:
1020 case GST_STATE_READY:
1023 case GST_STATE_PLAYING:
1026 case GST_STATE_PAUSED:
1030 /* This is a memory leak */
1031 return g_strdup_printf ("UNKNOWN!(%d)", state);
1038 * gst_element_factory_can_src_caps :
1039 * @factory: factory to query
1040 * @caps: the caps to check
1042 * Checks if the factory can source the given capability.
1044 * Returns: true if it can src the capabilities
1047 gst_element_factory_can_src_caps (GstElementFactory * factory,
1048 const GstCaps * caps)
1052 g_return_val_if_fail (factory != NULL, FALSE);
1053 g_return_val_if_fail (caps != NULL, FALSE);
1055 templates = factory->staticpadtemplates;
1058 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1060 if (template->direction == GST_PAD_SRC) {
1061 if (gst_caps_is_always_compatible (gst_static_caps_get (&template->
1062 static_caps), caps))
1065 templates = g_list_next (templates);
1072 * gst_element_factory_can_sink_caps :
1073 * @factory: factory to query
1074 * @caps: the caps to check
1076 * Checks if the factory can sink the given capability.
1078 * Returns: true if it can sink the capabilities
1081 gst_element_factory_can_sink_caps (GstElementFactory * factory,
1082 const GstCaps * caps)
1086 g_return_val_if_fail (factory != NULL, FALSE);
1087 g_return_val_if_fail (caps != NULL, FALSE);
1089 templates = factory->staticpadtemplates;
1092 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1094 if (template->direction == GST_PAD_SINK) {
1095 if (gst_caps_is_always_compatible (caps,
1096 gst_static_caps_get (&template->static_caps)))
1099 templates = g_list_next (templates);
1106 /* if return val is true, *direct_child is a caller-owned ref on the direct
1107 * child of ancestor that is part of object's ancestry */
1109 object_has_ancestor (GstObject * object, GstObject * ancestor,
1110 GstObject ** direct_child)
1112 GstObject *child, *parent;
1115 *direct_child = NULL;
1117 child = gst_object_ref (object);
1118 parent = gst_object_get_parent (object);
1121 if (ancestor == parent) {
1123 *direct_child = child;
1125 gst_object_unref (child);
1126 gst_object_unref (parent);
1130 gst_object_unref (child);
1132 parent = gst_object_get_parent (parent);
1135 gst_object_unref (child);
1140 /* caller owns return */
1142 find_common_root (GstObject * o1, GstObject * o2)
1144 GstObject *top = o1;
1145 GstObject *kid1, *kid2;
1146 GstObject *root = NULL;
1148 while (GST_OBJECT_PARENT (top))
1149 top = GST_OBJECT_PARENT (top);
1151 /* the itsy-bitsy spider... */
1153 if (!object_has_ancestor (o2, top, &kid2))
1156 root = gst_object_ref (top);
1158 if (!object_has_ancestor (o1, kid2, &kid1)) {
1159 gst_object_unref (kid2);
1163 if (!object_has_ancestor (o2, kid1, &kid2)) {
1164 gst_object_unref (kid1);
1171 /* caller does not own return */
1173 ghost_up (GstElement * e, GstPad * pad)
1175 static gint ghost_pad_index = 0;
1178 GstObject *parent = GST_OBJECT_PARENT (e);
1180 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1181 gpad = gst_ghost_pad_new (name, pad);
1184 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1185 g_warning ("Pad named %s already exists in element %s\n",
1186 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1187 gst_object_unref ((GstObject *) gpad);
1195 remove_pad (gpointer ppad, gpointer unused)
1199 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1200 g_warning ("Couldn't remove pad %s from element %s",
1201 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1205 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1206 GSList ** pads_created)
1210 GSList *pads_created_local = NULL;
1212 g_assert (pads_created);
1214 e1 = GST_OBJECT_PARENT (*src);
1215 e2 = GST_OBJECT_PARENT (*sink);
1217 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1218 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1219 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1223 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1224 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1226 /* we need to setup some ghost pads */
1227 root = find_common_root (e1, e2);
1230 ("Trying to connect elements that don't share a common ancestor: %s and %s\n",
1231 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
1235 while (GST_OBJECT_PARENT (e1) != root) {
1236 *src = ghost_up ((GstElement *) e1, *src);
1239 e1 = GST_OBJECT_PARENT (*src);
1240 pads_created_local = g_slist_prepend (pads_created_local, *src);
1242 while (GST_OBJECT_PARENT (e2) != root) {
1243 *sink = ghost_up ((GstElement *) e2, *sink);
1246 e2 = GST_OBJECT_PARENT (*sink);
1247 pads_created_local = g_slist_prepend (pads_created_local, *sink);
1250 gst_object_unref (root);
1251 *pads_created = g_slist_concat (*pads_created, pads_created_local);
1255 gst_object_unref (root);
1256 g_slist_foreach (pads_created_local, remove_pad, NULL);
1257 g_slist_free (pads_created_local);
1262 pad_link_maybe_ghosting (GstPad * src, GstPad * sink)
1264 GSList *pads_created = NULL;
1267 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1270 ret = (gst_pad_link (src, sink) == GST_PAD_LINK_OK);
1274 g_slist_foreach (pads_created, remove_pad, NULL);
1276 g_slist_free (pads_created);
1282 * gst_element_link_pads:
1283 * @src: a #GstElement containing the source pad.
1284 * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
1285 * @dest: the #GstElement containing the destination pad.
1286 * @destpadname: the name of the #GstPad in destination element,
1287 * or NULL for any pad.
1289 * Links the two named pads of the source and destination elements.
1290 * Side effect is that if one of the pads has no parent, it becomes a
1291 * child of the parent of the other element. If they have different
1292 * parents, the link fails.
1294 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1297 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
1298 GstElement * dest, const gchar * destpadname)
1300 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1301 GstPad *srcpad, *destpad;
1302 GstPadTemplate *srctempl, *desttempl;
1303 GstElementClass *srcclass, *destclass;
1306 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1307 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1309 srcclass = GST_ELEMENT_GET_CLASS (src);
1310 destclass = GST_ELEMENT_GET_CLASS (dest);
1312 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1313 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1314 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1315 destpadname ? destpadname : "(any)");
1319 /* name specified, look it up */
1320 srcpad = gst_element_get_pad (src, srcpadname);
1322 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1323 GST_ELEMENT_NAME (src), srcpadname);
1326 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1327 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1328 GST_DEBUG_PAD_NAME (srcpad));
1329 gst_object_unref (srcpad);
1332 if (GST_PAD_PEER (srcpad) != NULL) {
1333 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
1334 GST_DEBUG_PAD_NAME (srcpad));
1335 gst_object_unref (srcpad);
1341 /* no name given, get the first available pad */
1342 GST_OBJECT_LOCK (src);
1343 srcpads = GST_ELEMENT_PADS (src);
1344 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1346 gst_object_ref (srcpad);
1347 GST_OBJECT_UNLOCK (src);
1350 /* get a destination pad */
1352 /* name specified, look it up */
1353 destpad = gst_element_get_pad (dest, destpadname);
1355 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1356 GST_ELEMENT_NAME (dest), destpadname);
1359 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1360 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1361 GST_DEBUG_PAD_NAME (destpad));
1362 gst_object_unref (destpad);
1365 if (GST_PAD_PEER (destpad) != NULL) {
1366 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
1367 GST_DEBUG_PAD_NAME (destpad));
1368 gst_object_unref (destpad);
1374 /* no name given, get the first available pad */
1375 GST_OBJECT_LOCK (dest);
1376 destpads = GST_ELEMENT_PADS (dest);
1377 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1379 gst_object_ref (destpad);
1380 GST_OBJECT_UNLOCK (dest);
1383 if (srcpadname && destpadname) {
1386 /* two explicitly specified pads */
1387 result = pad_link_maybe_ghosting (srcpad, destpad);
1389 gst_object_unref (srcpad);
1390 gst_object_unref (destpad);
1396 /* loop through the allowed pads in the source, trying to find a
1397 * compatible destination pad */
1398 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1399 "looping through allowed src and dest pads");
1401 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1402 GST_DEBUG_PAD_NAME (srcpad));
1403 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1404 (GST_PAD_PEER (srcpad) == NULL)) {
1409 gst_object_ref (temp);
1411 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1414 if (temp && pad_link_maybe_ghosting (srcpad, temp)) {
1415 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1416 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1418 gst_object_unref (destpad);
1419 gst_object_unref (srcpad);
1420 gst_object_unref (temp);
1425 gst_object_unref (temp);
1428 /* find a better way for this mess */
1430 srcpads = g_list_next (srcpads);
1432 gst_object_unref (srcpad);
1433 srcpad = GST_PAD_CAST (srcpads->data);
1434 gst_object_ref (srcpad);
1440 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1441 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1443 gst_object_unref (destpad);
1447 gst_object_unref (srcpad);
1451 /* loop through the existing pads in the destination */
1453 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1454 GST_DEBUG_PAD_NAME (destpad));
1455 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1456 (GST_PAD_PEER (destpad) == NULL)) {
1457 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1459 if (temp && pad_link_maybe_ghosting (temp, destpad)) {
1460 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1461 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1462 gst_object_unref (temp);
1463 gst_object_unref (destpad);
1467 gst_object_unref (temp);
1471 destpads = g_list_next (destpads);
1473 gst_object_unref (destpad);
1474 destpad = GST_PAD_CAST (destpads->data);
1475 gst_object_ref (destpad);
1482 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1483 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1484 gst_object_unref (destpad);
1488 gst_object_unref (destpad);
1492 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1493 "we might have request pads on both sides, checking...");
1494 srctempls = gst_element_class_get_pad_template_list (srcclass);
1495 desttempls = gst_element_class_get_pad_template_list (destclass);
1497 if (srctempls && desttempls) {
1499 srctempl = (GstPadTemplate *) srctempls->data;
1500 if (srctempl->presence == GST_PAD_REQUEST) {
1501 for (l = desttempls; l; l = l->next) {
1502 desttempl = (GstPadTemplate *) l->data;
1503 if (desttempl->presence == GST_PAD_REQUEST &&
1504 desttempl->direction != srctempl->direction) {
1505 if (gst_caps_is_always_compatible (gst_pad_template_get_caps
1506 (srctempl), gst_pad_template_get_caps (desttempl))) {
1508 gst_element_get_request_pad (src, srctempl->name_template);
1510 gst_element_get_request_pad (dest, desttempl->name_template);
1511 if (pad_link_maybe_ghosting (srcpad, destpad)) {
1512 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1513 "linked pad %s:%s to pad %s:%s",
1514 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1515 gst_object_unref (srcpad);
1516 gst_object_unref (destpad);
1519 /* it failed, so we release the request pads */
1520 gst_element_release_request_pad (src, srcpad);
1521 gst_element_release_request_pad (dest, destpad);
1526 srctempls = srctempls->next;
1530 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1531 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1536 * gst_element_link_pads_filtered:
1537 * @src: a #GstElement containing the source pad.
1538 * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
1539 * @dest: the #GstElement containing the destination pad.
1540 * @destpadname: the name of the #GstPad in destination element or NULL for any pad.
1541 * @filter: the #GstCaps to filter the link, or #NULL for no filter.
1543 * Links the two named pads of the source and destination elements. Side effect
1544 * is that if one of the pads has no parent, it becomes a child of the parent of
1545 * the other element. If they have different parents, the link fails. If @caps
1546 * is not #NULL, makes sure that the caps of the link is a subset of @caps.
1548 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1551 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
1552 GstElement * dest, const gchar * destpadname, GstCaps * filter)
1555 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1556 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1557 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
1560 GstElement *capsfilter;
1562 GstState state, pending;
1564 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1566 GST_ERROR ("Could not make a capsfilter");
1570 parent = gst_object_get_parent (GST_OBJECT (src));
1571 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1573 gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
1575 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
1576 GST_ERROR ("Could not add capsfilter");
1577 gst_object_unref (capsfilter);
1578 gst_object_unref (parent);
1582 if (pending != GST_STATE_VOID_PENDING)
1585 gst_element_set_state (capsfilter, state);
1587 gst_object_unref (parent);
1589 g_object_set (capsfilter, "caps", filter, NULL);
1591 if (gst_element_link_pads (src, srcpadname, capsfilter, "sink")
1592 && gst_element_link_pads (capsfilter, "src", dest, destpadname)) {
1595 GST_INFO ("Could not link elements");
1596 gst_element_set_state (capsfilter, GST_STATE_NULL);
1597 /* this will unlink and unref as appropriate */
1598 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
1602 return gst_element_link_pads (src, srcpadname, dest, destpadname);
1608 * @src: a #GstElement containing the source pad.
1609 * @dest: the #GstElement containing the destination pad.
1611 * Links @src to @dest. The link must be from source to
1612 * destination; the other direction will not be tried. The function looks for
1613 * existing pads that aren't linked yet. It will request new pads if necessary.
1614 * If multiple links are possible, only one is established.
1616 * Make sure you have added your elements to a bin or pipeline with
1617 * gst_bin_add() before trying to link them.
1619 * Returns: TRUE if the elements could be linked, FALSE otherwise.
1622 gst_element_link (GstElement * src, GstElement * dest)
1624 return gst_element_link_pads_filtered (src, NULL, dest, NULL, NULL);
1628 * gst_element_link_many:
1629 * @element_1: the first #GstElement in the link chain.
1630 * @element_2: the second #GstElement in the link chain.
1631 * @...: the NULL-terminated list of elements to link in order.
1633 * Chain together a series of elements. Uses gst_element_link().
1634 * Make sure you have added your elements to a bin or pipeline with
1635 * gst_bin_add() before trying to link them.
1637 * Returns: TRUE on success, FALSE otherwise.
1640 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
1644 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
1645 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
1647 va_start (args, element_2);
1650 if (!gst_element_link (element_1, element_2))
1653 element_1 = element_2;
1654 element_2 = va_arg (args, GstElement *);
1663 * gst_element_link_filtered:
1664 * @src: a #GstElement containing the source pad.
1665 * @dest: the #GstElement containing the destination pad.
1666 * @filter: the #GstCaps to filter the link, or #NULL for no filter.
1668 * Links @src to @dest using the given caps as filtercaps.
1669 * The link must be from source to
1670 * destination; the other direction will not be tried. The function looks for
1671 * existing pads that aren't linked yet. It will request new pads if necessary.
1672 * If multiple links are possible, only one is established.
1674 * Make sure you have added your elements to a bin or pipeline with
1675 * gst_bin_add() before trying to link them.
1677 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1680 gst_element_link_filtered (GstElement * src, GstElement * dest,
1683 return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
1687 * gst_element_unlink_pads:
1688 * @src: a #GstElement containing the source pad.
1689 * @srcpadname: the name of the #GstPad in source element.
1690 * @dest: a #GstElement containing the destination pad.
1691 * @destpadname: the name of the #GstPad in destination element.
1693 * Unlinks the two named pads of the source and destination elements.
1696 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
1697 GstElement * dest, const gchar * destpadname)
1699 GstPad *srcpad, *destpad;
1701 g_return_if_fail (src != NULL);
1702 g_return_if_fail (GST_IS_ELEMENT (src));
1703 g_return_if_fail (srcpadname != NULL);
1704 g_return_if_fail (dest != NULL);
1705 g_return_if_fail (GST_IS_ELEMENT (dest));
1706 g_return_if_fail (destpadname != NULL);
1708 /* obtain the pads requested */
1709 srcpad = gst_element_get_pad (src, srcpadname);
1710 if (srcpad == NULL) {
1711 GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
1714 destpad = gst_element_get_pad (dest, destpadname);
1715 if (destpad == NULL) {
1716 GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
1718 gst_object_unref (srcpad);
1722 /* we're satisified they can be unlinked, let's do it */
1723 gst_pad_unlink (srcpad, destpad);
1724 gst_object_unref (srcpad);
1725 gst_object_unref (destpad);
1729 * gst_element_unlink_many:
1730 * @element_1: the first #GstElement in the link chain.
1731 * @element_2: the second #GstElement in the link chain.
1732 * @...: the NULL-terminated list of elements to unlink in order.
1734 * Unlinks a series of elements. Uses gst_element_unlink().
1737 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
1741 g_return_if_fail (element_1 != NULL && element_2 != NULL);
1742 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
1744 va_start (args, element_2);
1747 gst_element_unlink (element_1, element_2);
1749 element_1 = element_2;
1750 element_2 = va_arg (args, GstElement *);
1757 * gst_element_unlink:
1758 * @src: the source #GstElement to unlink.
1759 * @dest: the sink #GstElement to unlink.
1761 * Unlinks all source pads of the source element with all sink pads
1762 * of the sink element to which they are linked.
1765 gst_element_unlink (GstElement * src, GstElement * dest)
1768 gboolean done = FALSE;
1770 g_return_if_fail (GST_IS_ELEMENT (src));
1771 g_return_if_fail (GST_IS_ELEMENT (dest));
1773 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
1774 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1776 pads = gst_element_iterate_pads (src);
1780 switch (gst_iterator_next (pads, &data)) {
1781 case GST_ITERATOR_OK:
1783 GstPad *pad = GST_PAD_CAST (data);
1785 if (GST_PAD_IS_SRC (pad)) {
1786 GstPad *peerpad = gst_pad_get_peer (pad);
1788 /* see if the pad is connected and is really a pad
1791 GstElement *peerelem;
1793 peerelem = gst_pad_get_parent_element (peerpad);
1795 if (peerelem == dest) {
1796 gst_pad_unlink (pad, peerpad);
1799 gst_object_unref (peerelem);
1801 gst_object_unref (peerpad);
1804 gst_object_unref (pad);
1807 case GST_ITERATOR_RESYNC:
1808 gst_iterator_resync (pads);
1810 case GST_ITERATOR_DONE:
1814 g_assert_not_reached ();
1818 gst_iterator_free (pads);
1822 * gst_element_query_position:
1823 * @element: a #GstElement to invoke the position query on.
1824 * @format: a pointer to the #GstFormat asked for.
1825 * On return contains the #GstFormat used.
1826 * @cur: A location in which to store the current position, or NULL.
1828 * Queries an element for the stream position.
1830 * Returns: TRUE if the query could be performed.
1833 gst_element_query_position (GstElement * element, GstFormat * format,
1839 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1840 g_return_val_if_fail (format != NULL, FALSE);
1842 query = gst_query_new_position (*format);
1843 ret = gst_element_query (element, query);
1846 gst_query_parse_position (query, format, cur);
1848 gst_query_unref (query);
1854 * gst_element_query_duration:
1855 * @element: a #GstElement to invoke the duration query on.
1856 * @format: a pointer to the #GstFormat asked for.
1857 * On return contains the #GstFormat used.
1858 * @duration: A location in which to store the total duration, or NULL.
1860 * Queries an element for the total stream duration.
1862 * Returns: TRUE if the query could be performed.
1865 gst_element_query_duration (GstElement * element, GstFormat * format,
1871 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1872 g_return_val_if_fail (format != NULL, FALSE);
1874 query = gst_query_new_duration (*format);
1875 ret = gst_element_query (element, query);
1878 gst_query_parse_duration (query, format, duration);
1880 gst_query_unref (query);
1886 * gst_element_query_convert:
1887 * @element: a #GstElement to invoke the convert query on.
1888 * @src_format: a #GstFormat to convert from.
1889 * @src_val: a value to convert.
1890 * @dest_format: a pointer to the #GstFormat to convert to.
1891 * @dest_val: a pointer to the result.
1893 * Queries an element to convert @src_val in @src_format to @dest_format.
1895 * Returns: TRUE if the query could be performed.
1898 gst_element_query_convert (GstElement * element, GstFormat src_format,
1899 gint64 src_val, GstFormat * dest_format, gint64 * dest_val)
1904 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1905 g_return_val_if_fail (dest_format != NULL, FALSE);
1906 g_return_val_if_fail (dest_val != NULL, FALSE);
1908 if (*dest_format == src_format) {
1909 *dest_val = src_val;
1913 query = gst_query_new_convert (src_format, src_val, *dest_format);
1914 ret = gst_element_query (element, query);
1917 gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
1919 gst_query_unref (query);
1925 * gst_element_seek_simple
1926 * @element: a #GstElement to seek on
1927 * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
1928 * @seek_flags: seek options
1929 * @seek_pos: position to seek to (relative to the start); if you are doing
1930 * a seek in #GST_FORMAT_TIME this value is in nanoseconds -
1931 * multiply with #GST_SECOND to convert seconds to nanoseconds or
1932 * with #GST_MSECOND to convert milliseconds to nanoseconds.
1934 * Simple API to perform a seek on the given element, meaning it just seeks
1935 * to the given position relative to the start of the stream. For more complex
1936 * operations like segment seeks (e.g. for looping) or changing the playback
1937 * rate or seeking relative to the current position or seeking relative to
1938 * the end of the stream you should use gst_element_seek ().
1940 * Note that seeking is usually only possible in PAUSED or PLAYING state.
1942 * Returns: TRUE if the seek operation succeeded (the seek
1943 * might not always be executed instantly though)
1948 gst_element_seek_simple (GstElement * element, GstFormat format,
1949 GstSeekFlags seek_flags, gint64 seek_pos)
1951 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1952 g_return_val_if_fail (seek_pos >= 0, FALSE);
1954 return gst_element_seek (element, 1.0, format, seek_flags,
1955 GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_NONE, 0);
1960 * @srcpad: the source #GstPad to link.
1961 * @sinkpad: the sink #GstPad to link.
1963 * Checks if the source pad and the sink pad can be linked.
1964 * Both @srcpad and @sinkpad must be unlinked.
1966 * Returns: TRUE if the pads can be linked, FALSE otherwise.
1969 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1971 /* FIXME This function is gross. It's almost a direct copy of
1972 * gst_pad_link_filtered(). Any decent programmer would attempt
1973 * to merge the two functions, which I will do some day. --ds
1976 /* generic checks */
1977 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1978 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1980 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1981 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1983 /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
1984 if (GST_PAD_PEER (srcpad) != NULL) {
1985 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
1986 GST_DEBUG_PAD_NAME (srcpad));
1989 if (GST_PAD_PEER (sinkpad) != NULL) {
1990 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
1991 GST_DEBUG_PAD_NAME (sinkpad));
1994 if (!GST_PAD_IS_SRC (srcpad)) {
1995 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
1996 GST_DEBUG_PAD_NAME (srcpad));
1999 if (!GST_PAD_IS_SINK (sinkpad)) {
2000 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
2001 GST_DEBUG_PAD_NAME (sinkpad));
2004 if (GST_PAD_PARENT (srcpad) == NULL) {
2005 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
2006 GST_DEBUG_PAD_NAME (srcpad));
2009 if (GST_PAD_PARENT (sinkpad) == NULL) {
2010 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
2011 GST_DEBUG_PAD_NAME (srcpad));
2019 * gst_pad_use_fixed_caps:
2020 * @pad: the pad to use
2022 * A helper function you can use that sets the
2023 * @gst_pad_get_fixed_caps_func as the getcaps function for the
2024 * pad. This way the function will always return the negotiated caps
2025 * or in case the pad is not negotiated, the padtemplate caps.
2027 * Use this function on a pad that, once _set_caps() has been called
2028 * on it, cannot be renegotiated to something else.
2031 gst_pad_use_fixed_caps (GstPad * pad)
2033 gst_pad_set_getcaps_function (pad, gst_pad_get_fixed_caps_func);
2037 * gst_pad_get_fixed_caps_func:
2038 * @pad: the pad to use
2040 * A helper function you can use as a GetCaps function that
2041 * will return the currently negotiated caps or the padtemplate
2044 * Returns: The currently negotiated caps or the padtemplate.
2047 gst_pad_get_fixed_caps_func (GstPad * pad)
2051 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2053 GST_OBJECT_LOCK (pad);
2054 if (GST_PAD_CAPS (pad)) {
2055 result = GST_PAD_CAPS (pad);
2057 GST_CAT_DEBUG (GST_CAT_CAPS,
2058 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2060 result = gst_caps_ref (result);
2063 if (GST_PAD_PAD_TEMPLATE (pad)) {
2064 GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
2066 result = GST_PAD_TEMPLATE_CAPS (templ);
2067 GST_CAT_DEBUG (GST_CAT_CAPS,
2068 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2071 result = gst_caps_ref (result);
2074 GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
2075 result = gst_caps_new_empty ();
2078 GST_OBJECT_UNLOCK (pad);
2084 * gst_pad_get_parent_element:
2087 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2088 * its parent is not an element, return NULL.
2090 * Returns: The parent of the pad. The caller has a reference on the parent, so
2091 * unref when you're finished with it.
2096 gst_pad_get_parent_element (GstPad * pad)
2100 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2102 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2104 if (p && !GST_IS_ELEMENT (p)) {
2105 gst_object_unref (p);
2108 return GST_ELEMENT_CAST (p);
2112 * gst_object_default_error:
2113 * @source: the #GstObject that initiated the error.
2114 * @error: the GError.
2115 * @debug: an additional debug information string, or NULL.
2117 * A default error function.
2119 * The default handler will simply print the error string using g_print.
2122 gst_object_default_error (GstObject * source, GError * error, gchar * debug)
2124 gchar *name = gst_object_get_path_string (source);
2126 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
2128 g_print (_("Additional debug info:\n%s\n"), debug);
2136 * @element_1: the #GstElement element to add to the bin
2137 * @...: additional elements to add to the bin
2139 * Adds a NULL-terminated list of elements to a bin. This function is
2140 * equivalent to calling gst_bin_add() for each member of the list.
2143 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2147 g_return_if_fail (GST_IS_BIN (bin));
2148 g_return_if_fail (GST_IS_ELEMENT (element_1));
2150 va_start (args, element_1);
2153 gst_bin_add (bin, element_1);
2155 element_1 = va_arg (args, GstElement *);
2162 * gst_bin_remove_many:
2164 * @element_1: the first #GstElement to remove from the bin
2165 * @...: NULL-terminated list of elements to remove from the bin
2167 * Remove a list of elements from a bin. This function is equivalent
2168 * to calling gst_bin_remove() with each member of the list.
2171 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2175 g_return_if_fail (GST_IS_BIN (bin));
2176 g_return_if_fail (GST_IS_ELEMENT (element_1));
2178 va_start (args, element_1);
2181 gst_bin_remove (bin, element_1);
2183 element_1 = va_arg (args, GstElement *);
2190 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
2191 guint arg_id, GParamFlags flags)
2193 GQuark prop_id = g_quark_from_string (prop_name);
2196 static GQuark fd_id = 0;
2197 static GQuark blocksize_id;
2198 static GQuark bytesperread_id;
2199 static GQuark dump_id;
2200 static GQuark filesize_id;
2201 static GQuark mmapsize_id;
2202 static GQuark location_id;
2203 static GQuark offset_id;
2204 static GQuark silent_id;
2205 static GQuark touch_id;
2208 fd_id = g_quark_from_static_string ("fd");
2209 blocksize_id = g_quark_from_static_string ("blocksize");
2210 bytesperread_id = g_quark_from_static_string ("bytesperread");
2211 dump_id = g_quark_from_static_string ("dump");
2212 filesize_id = g_quark_from_static_string ("filesize");
2213 mmapsize_id = g_quark_from_static_string ("mmapsize");
2214 location_id = g_quark_from_static_string ("location");
2215 offset_id = g_quark_from_static_string ("offset");
2216 silent_id = g_quark_from_static_string ("silent");
2217 touch_id = g_quark_from_static_string ("touch");
2220 if (prop_id == fd_id) {
2221 pspec = g_param_spec_int ("fd", "File-descriptor",
2222 "File-descriptor for the file being read", 0, G_MAXINT, 0, flags);
2223 } else if (prop_id == blocksize_id) {
2224 pspec = g_param_spec_ulong ("blocksize", "Block Size",
2225 "Block size to read per buffer", 0, G_MAXULONG, 4096, flags);
2227 } else if (prop_id == bytesperread_id) {
2228 pspec = g_param_spec_int ("bytesperread", "Bytes per read",
2229 "Number of bytes to read per buffer", G_MININT, G_MAXINT, 0, flags);
2231 } else if (prop_id == dump_id) {
2232 pspec = g_param_spec_boolean ("dump", "Dump",
2233 "Dump bytes to stdout", FALSE, flags);
2235 } else if (prop_id == filesize_id) {
2236 pspec = g_param_spec_int64 ("filesize", "File Size",
2237 "Size of the file being read", 0, G_MAXINT64, 0, flags);
2239 } else if (prop_id == mmapsize_id) {
2240 pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
2241 "Size in bytes of mmap()d regions", 0, G_MAXULONG, 4 * 1048576, flags);
2243 } else if (prop_id == location_id) {
2244 pspec = g_param_spec_string ("location", "File Location",
2245 "Location of the file to read", NULL, flags);
2247 } else if (prop_id == offset_id) {
2248 pspec = g_param_spec_int64 ("offset", "File Offset",
2249 "Byte offset of current read pointer", 0, G_MAXINT64, 0, flags);
2251 } else if (prop_id == silent_id) {
2252 pspec = g_param_spec_boolean ("silent", "Silent", "Don't produce events",
2255 } else if (prop_id == touch_id) {
2256 pspec = g_param_spec_boolean ("touch", "Touch read data",
2257 "Touch data to force disk read before " "push ()", TRUE, flags);
2259 g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
2260 prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
2265 g_object_class_install_property (klass, arg_id, pspec);
2270 * gst_element_class_install_std_props:
2271 * @klass: the #GstElementClass to add the properties to.
2272 * @first_name: the name of the first property.
2273 * in a NULL terminated
2274 * @...: the id and flags of the first property, followed by
2275 * further 'name', 'id', 'flags' triplets and terminated by NULL.
2277 * Adds a list of standardized properties with types to the @klass.
2278 * the id is for the property switch in your get_prop method, and
2279 * the flags determine readability / writeability.
2282 gst_element_class_install_std_props (GstElementClass * klass,
2283 const gchar * first_name, ...)
2289 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
2291 va_start (args, first_name);
2296 int arg_id = va_arg (args, int);
2297 int flags = va_arg (args, int);
2299 gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id,
2302 name = va_arg (args, char *);
2311 * @buf1: the first source #GstBuffer to merge.
2312 * @buf2: the second source #GstBuffer to merge.
2314 * Create a new buffer that is the concatenation of the two source
2315 * buffers. The original source buffers will not be modified or
2316 * unref'd. Make sure you unref the source buffers if they are not used
2317 * anymore afterwards.
2319 * If the buffers point to contiguous areas of memory, the buffer
2320 * is created without copying the data.
2322 * Returns: the new #GstBuffer which is the concatenation of the source buffers.
2325 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
2329 /* we're just a specific case of the more general gst_buffer_span() */
2330 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
2337 * @buf1: the first source #GstBuffer.
2338 * @buf2: the second source #GstBuffer.
2340 * Create a new buffer that is the concatenation of the two source
2341 * buffers, and unrefs the original source buffers.
2343 * If the buffers point to contiguous areas of memory, the buffer
2344 * is created without copying the data.
2346 * Returns: the new #GstBuffer which is the concatenation of the source buffers.
2349 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
2353 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
2354 gst_buffer_unref (buf1);
2355 gst_buffer_unref (buf2);
2363 * @dest: buffer to stamp
2364 * @src: buffer to stamp from
2366 * Copies additional information (the timestamp, duration, and offset start
2367 * and end) from one buffer to the other.
2369 * This function does not copy any buffer flags or caps.
2372 gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src)
2374 g_return_if_fail (dest != NULL);
2375 g_return_if_fail (src != NULL);
2377 GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
2378 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
2379 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
2380 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
2384 intersect_caps_func (GstPad * pad, GValue * ret, GstPad * orig)
2387 GstCaps *peercaps, *existing;
2389 existing = g_value_get_pointer (ret);
2390 peercaps = gst_pad_peer_get_caps (pad);
2391 if (peercaps == NULL)
2392 peercaps = gst_caps_new_any ();
2393 g_value_set_pointer (ret, gst_caps_intersect (existing, peercaps));
2394 gst_caps_unref (existing);
2395 gst_caps_unref (peercaps);
2397 gst_object_unref (pad);
2402 * gst_pad_proxy_getcaps:
2403 * @pad: a #GstPad to proxy.
2405 * Calls gst_pad_get_allowed_caps() for every other pad belonging to the
2406 * same element as @pad, and returns the intersection of the results.
2408 * This function is useful as a default getcaps function for an element
2409 * that can handle any stream format, but requires all its pads to have
2410 * the same caps. Two such elements are tee and aggregator.
2412 * Returns: the intersection of the other pads' allowed caps.
2415 gst_pad_proxy_getcaps (GstPad * pad)
2417 GstElement *element;
2418 GstCaps *caps, *intersected;
2420 GstIteratorResult res;
2421 GValue ret = { 0, };
2423 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2425 GST_DEBUG ("proxying getcaps for %s:%s", GST_DEBUG_PAD_NAME (pad));
2427 element = gst_pad_get_parent_element (pad);
2428 if (element == NULL)
2431 /* value to hold the return, by default it holds ANY, the ref is taken by
2433 g_value_init (&ret, G_TYPE_POINTER);
2434 g_value_set_pointer (&ret, gst_caps_new_any ());
2436 iter = gst_element_iterate_pads (element);
2439 gst_iterator_fold (iter, (GstIteratorFoldFunction) intersect_caps_func,
2442 case GST_ITERATOR_RESYNC:
2443 /* unref any value stored */
2444 if ((caps = g_value_get_pointer (&ret)))
2445 gst_caps_unref (caps);
2446 /* need to reset the result again to ANY */
2447 g_value_set_pointer (&ret, gst_caps_new_any ());
2448 gst_iterator_resync (iter);
2450 case GST_ITERATOR_DONE:
2451 /* all pads iterated, return collected value */
2454 /* iterator returned _ERROR or premature end with _OK,
2455 * mark an error and exit */
2456 if ((caps = g_value_get_pointer (&ret)))
2457 gst_caps_unref (caps);
2458 g_value_set_pointer (&ret, NULL);
2463 gst_iterator_free (iter);
2465 gst_object_unref (element);
2467 caps = g_value_get_pointer (&ret);
2468 g_value_unset (&ret);
2470 intersected = gst_caps_intersect (caps, gst_pad_get_pad_template_caps (pad));
2471 gst_caps_unref (caps);
2478 g_warning ("Pad list returned error on element %s",
2479 GST_ELEMENT_NAME (element));
2480 gst_iterator_free (iter);
2481 gst_object_unref (element);
2493 link_fold_func (GstPad * pad, GValue * ret, LinkData * data)
2495 gboolean success = TRUE;
2497 if (pad != data->orig) {
2498 success = gst_pad_set_caps (pad, data->caps);
2499 g_value_set_boolean (ret, success);
2501 gst_object_unref (pad);
2507 * gst_pad_proxy_setcaps
2508 * @pad: a #GstPad to proxy from
2509 * @caps: the #GstCaps to link with
2511 * Calls gst_pad_set_caps() for every other pad belonging to the
2512 * same element as @pad. If gst_pad_set_caps() fails on any pad,
2513 * the proxy setcaps fails. May be used only during negotiation.
2515 * Returns: TRUE if sucessful
2518 gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps)
2520 GstElement *element;
2522 GstIteratorResult res;
2523 GValue ret = { 0, };
2526 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2527 g_return_val_if_fail (caps != NULL, FALSE);
2529 GST_DEBUG ("proxying pad link for %s:%s", GST_DEBUG_PAD_NAME (pad));
2531 element = gst_pad_get_parent_element (pad);
2532 if (element == NULL)
2535 iter = gst_element_iterate_pads (element);
2537 g_value_init (&ret, G_TYPE_BOOLEAN);
2538 g_value_set_boolean (&ret, TRUE);
2542 res = gst_iterator_fold (iter, (GstIteratorFoldFunction) link_fold_func,
2544 gst_iterator_free (iter);
2546 if (res != GST_ITERATOR_DONE)
2549 gst_object_unref (element);
2551 /* ok not to unset the gvalue */
2552 return g_value_get_boolean (&ret);
2557 g_warning ("Pad list changed during proxy_pad_link for element %s",
2558 GST_ELEMENT_NAME (element));
2559 gst_object_unref (element);
2565 * gst_pad_query_position:
2566 * @pad: a #GstPad to invoke the position query on.
2567 * @format: a pointer to the #GstFormat asked for.
2568 * On return contains the #GstFormat used.
2569 * @cur: A location in which to store the current position, or NULL.
2571 * Queries a pad for the stream position.
2573 * Returns: TRUE if the query could be performed.
2576 gst_pad_query_position (GstPad * pad, GstFormat * format, gint64 * cur)
2581 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2582 g_return_val_if_fail (format != NULL, FALSE);
2584 query = gst_query_new_position (*format);
2585 ret = gst_pad_query (pad, query);
2588 gst_query_parse_position (query, format, cur);
2590 gst_query_unref (query);
2596 * gst_pad_query_peer_position:
2597 * @pad: a #GstPad on whose peer to invoke the position query on.
2598 * Must be a sink pad.
2599 * @format: a pointer to the #GstFormat asked for.
2600 * On return contains the #GstFormat used.
2601 * @cur: A location in which to store the current position, or NULL.
2603 * Queries the peer of a given sink pad for the stream position.
2605 * Returns: TRUE if the query could be performed.
2608 gst_pad_query_peer_position (GstPad * pad, GstFormat * format, gint64 * cur)
2610 gboolean ret = FALSE;
2613 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2614 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2615 g_return_val_if_fail (format != NULL, FALSE);
2617 peer = gst_pad_get_peer (pad);
2619 ret = gst_pad_query_position (peer, format, cur);
2620 gst_object_unref (peer);
2627 * gst_pad_query_duration:
2628 * @pad: a #GstPad to invoke the duration query on.
2629 * @format: a pointer to the #GstFormat asked for.
2630 * On return contains the #GstFormat used.
2631 * @duration: A location in which to store the total duration, or NULL.
2633 * Queries a pad for the total stream duration.
2635 * Returns: TRUE if the query could be performed.
2638 gst_pad_query_duration (GstPad * pad, GstFormat * format, gint64 * duration)
2643 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2644 g_return_val_if_fail (format != NULL, FALSE);
2646 query = gst_query_new_duration (*format);
2647 ret = gst_pad_query (pad, query);
2650 gst_query_parse_duration (query, format, duration);
2652 gst_query_unref (query);
2658 * gst_pad_query_peer_duration:
2659 * @pad: a #GstPad on whose peer pad to invoke the duration query on.
2660 * Must be a sink pad.
2661 * @format: a pointer to the #GstFormat asked for.
2662 * On return contains the #GstFormat used.
2663 * @duration: A location in which to store the total duration, or NULL.
2665 * Queries the peer pad of a given sink pad for the total stream duration.
2667 * Returns: TRUE if the query could be performed.
2670 gst_pad_query_peer_duration (GstPad * pad, GstFormat * format,
2673 gboolean ret = FALSE;
2676 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2677 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2678 g_return_val_if_fail (format != NULL, FALSE);
2680 peer = gst_pad_get_peer (pad);
2682 ret = gst_pad_query_duration (peer, format, duration);
2683 gst_object_unref (peer);
2690 * gst_pad_query_convert:
2691 * @pad: a #GstPad to invoke the convert query on.
2692 * @src_format: a #GstFormat to convert from.
2693 * @src_val: a value to convert.
2694 * @dest_format: a pointer to the #GstFormat to convert to.
2695 * @dest_val: a pointer to the result.
2697 * Queries a pad to convert @src_val in @src_format to @dest_format.
2699 * Returns: TRUE if the query could be performed.
2702 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2703 GstFormat * dest_format, gint64 * dest_val)
2708 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2709 g_return_val_if_fail (src_val >= 0, FALSE);
2710 g_return_val_if_fail (dest_format != NULL, FALSE);
2711 g_return_val_if_fail (dest_val != NULL, FALSE);
2713 if (*dest_format == src_format) {
2714 *dest_val = src_val;
2718 query = gst_query_new_convert (src_format, src_val, *dest_format);
2719 ret = gst_pad_query (pad, query);
2722 gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
2724 gst_query_unref (query);
2730 * gst_pad_query_peer_convert:
2731 * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
2732 * Must be a sink pad.
2733 * @src_format: a #GstFormat to convert from.
2734 * @src_val: a value to convert.
2735 * @dest_format: a pointer to the #GstFormat to convert to.
2736 * @dest_val: a pointer to the result.
2738 * Queries the peer pad of a given sink pad to convert @src_val in @src_format
2741 * Returns: TRUE if the query could be performed.
2744 gst_pad_query_peer_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2745 GstFormat * dest_format, gint64 * dest_val)
2747 gboolean ret = FALSE;
2750 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2751 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2752 g_return_val_if_fail (src_val >= 0, FALSE);
2753 g_return_val_if_fail (dest_format != NULL, FALSE);
2754 g_return_val_if_fail (dest_val != NULL, FALSE);
2756 peer = gst_pad_get_peer (pad);
2758 ret = gst_pad_query_convert (peer, src_format, src_val, dest_format,
2760 gst_object_unref (peer);
2767 * gst_atomic_int_set:
2768 * @atomic_int: pointer to an atomic integer
2769 * @value: value to set
2771 * Unconditionally sets the atomic integer to @value.
2774 gst_atomic_int_set (gint * atomic_int, gint value)
2778 *atomic_int = value;
2779 /* read acts as a memory barrier */
2780 ignore = g_atomic_int_get (atomic_int);
2784 * gst_pad_add_data_probe:
2785 * @pad: pad to add the data probe handler to
2786 * @handler: function to call when data is passed over pad
2787 * @data: data to pass along with the handler
2789 * Adds a "data probe" to a pad. This function will be called whenever data
2790 * passes through a pad. In this case data means both events and buffers. The
2791 * probe will be called with the data as an argument, meaning @handler should
2792 * have the same callback signature as the 'have-data' signal of #GstPad.
2793 * Note that the data will have a reference count greater than 1, so it will
2794 * be immutable -- you must not change it.
2796 * For source pads, the probe will be called after the blocking function, if any
2797 * (see gst_pad_set_blocked_async()), but before looking up the peer to chain
2798 * to. For sink pads, the probe function will be called before configuring the
2799 * sink with new caps, if any, and before calling the pad's chain function.
2801 * Your data probe should return TRUE to let the data continue to flow, or FALSE
2802 * to drop it. Dropping data is rarely useful, but occasionally comes in handy
2805 * Although probes are implemented internally by connecting @handler to the
2806 * have-data signal on the pad, if you want to remove a probe it is insufficient
2807 * to only call g_signal_handler_disconnect on the returned handler id. To
2808 * remove a probe, use the appropriate function, such as
2809 * gst_pad_remove_data_probe().
2811 * Returns: The handler id.
2814 gst_pad_add_data_probe (GstPad * pad, GCallback handler, gpointer data)
2818 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2819 g_return_val_if_fail (handler != NULL, 0);
2821 GST_OBJECT_LOCK (pad);
2822 sigid = g_signal_connect (pad, "have-data", handler, data);
2823 GST_PAD_DO_EVENT_SIGNALS (pad)++;
2824 GST_PAD_DO_BUFFER_SIGNALS (pad)++;
2825 GST_DEBUG ("adding data probe to pad %s:%s, now %d data, %d event probes",
2826 GST_DEBUG_PAD_NAME (pad),
2827 GST_PAD_DO_BUFFER_SIGNALS (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2828 GST_OBJECT_UNLOCK (pad);
2834 * gst_pad_add_event_probe:
2835 * @pad: pad to add the event probe handler to
2836 * @handler: function to call when data is passed over pad
2837 * @data: data to pass along with the handler
2839 * Adds a probe that will be called for all events passing through a pad. See
2840 * gst_pad_add_data_probe() for more information.
2842 * Returns: The handler id
2845 gst_pad_add_event_probe (GstPad * pad, GCallback handler, gpointer data)
2849 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2850 g_return_val_if_fail (handler != NULL, 0);
2852 GST_OBJECT_LOCK (pad);
2853 sigid = g_signal_connect (pad, "have-data::event", handler, data);
2854 GST_PAD_DO_EVENT_SIGNALS (pad)++;
2855 GST_DEBUG ("adding event probe to pad %s:%s, now %d probes",
2856 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2857 GST_OBJECT_UNLOCK (pad);
2863 * gst_pad_add_buffer_probe:
2864 * @pad: pad to add the buffer probe handler to
2865 * @handler: function to call when data is passed over pad
2866 * @data: data to pass along with the handler
2868 * Adds a probe that will be called for all buffers passing through a pad. See
2869 * gst_pad_add_data_probe() for more information.
2871 * Returns: The handler id
2874 gst_pad_add_buffer_probe (GstPad * pad, GCallback handler, gpointer data)
2878 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2879 g_return_val_if_fail (handler != NULL, 0);
2881 GST_OBJECT_LOCK (pad);
2882 sigid = g_signal_connect (pad, "have-data::buffer", handler, data);
2883 GST_PAD_DO_BUFFER_SIGNALS (pad)++;
2884 GST_DEBUG ("adding buffer probe to pad %s:%s, now %d probes",
2885 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_BUFFER_SIGNALS (pad));
2886 GST_OBJECT_UNLOCK (pad);
2892 * gst_pad_remove_data_probe:
2893 * @pad: pad to remove the data probe handler from
2894 * @handler_id: handler id returned from gst_pad_add_data_probe
2896 * Removes a data probe from @pad.
2899 gst_pad_remove_data_probe (GstPad * pad, guint handler_id)
2901 g_return_if_fail (GST_IS_PAD (pad));
2902 g_return_if_fail (handler_id > 0);
2904 GST_OBJECT_LOCK (pad);
2905 g_signal_handler_disconnect (pad, handler_id);
2906 GST_PAD_DO_BUFFER_SIGNALS (pad)--;
2907 GST_PAD_DO_EVENT_SIGNALS (pad)--;
2909 ("removed data probe from pad %s:%s, now %d event, %d buffer probes",
2910 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad),
2911 GST_PAD_DO_BUFFER_SIGNALS (pad));
2912 GST_OBJECT_UNLOCK (pad);
2917 * gst_pad_remove_event_probe:
2918 * @pad: pad to remove the event probe handler from
2919 * @handler_id: handler id returned from gst_pad_add_event_probe
2921 * Removes an event probe from @pad.
2924 gst_pad_remove_event_probe (GstPad * pad, guint handler_id)
2926 g_return_if_fail (GST_IS_PAD (pad));
2927 g_return_if_fail (handler_id > 0);
2929 GST_OBJECT_LOCK (pad);
2930 g_signal_handler_disconnect (pad, handler_id);
2931 GST_PAD_DO_EVENT_SIGNALS (pad)--;
2932 GST_DEBUG ("removed event probe from pad %s:%s, now %d event probes",
2933 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2934 GST_OBJECT_UNLOCK (pad);
2938 * gst_pad_remove_buffer_probe:
2939 * @pad: pad to remove the buffer probe handler from
2940 * @handler_id: handler id returned from gst_pad_add_buffer_probe
2942 * Removes a buffer probe from @pad.
2945 gst_pad_remove_buffer_probe (GstPad * pad, guint handler_id)
2947 g_return_if_fail (GST_IS_PAD (pad));
2948 g_return_if_fail (handler_id > 0);
2950 GST_OBJECT_LOCK (pad);
2951 g_signal_handler_disconnect (pad, handler_id);
2952 GST_PAD_DO_BUFFER_SIGNALS (pad)--;
2953 GST_DEBUG ("removed buffer probe from pad %s:%s, now %d buffer probes",
2954 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_BUFFER_SIGNALS (pad));
2955 GST_OBJECT_UNLOCK (pad);
2960 * gst_element_found_tags_for_pad:
2961 * @element: element for which to post taglist to bus.
2962 * @pad: pad on which to push tag-event.
2963 * @list: the taglist to post on the bus and create event from.
2965 * Posts a message to the bus that new tags were found and pushes the
2966 * tags as event. Takes ownership of the @list.
2968 * This is a utility method for elements. Applications should use the
2969 * #GstTagSetter interface.
2972 gst_element_found_tags_for_pad (GstElement * element,
2973 GstPad * pad, GstTagList * list)
2975 g_return_if_fail (element != NULL);
2976 g_return_if_fail (pad != NULL);
2977 g_return_if_fail (list != NULL);
2979 gst_pad_push_event (pad, gst_event_new_tag (gst_tag_list_copy (list)));
2980 gst_element_post_message (element,
2981 gst_message_new_tag (GST_OBJECT (element), list));
2985 push_and_ref (GstPad * pad, GstEvent * event)
2987 gst_pad_push_event (pad, gst_event_ref (event));
2988 /* iterator refs pad, we unref when we are done with it */
2989 gst_object_unref (pad);
2993 * gst_element_found_tags:
2994 * @element: element for which we found the tags.
2995 * @list: list of tags.
2997 * Posts a message to the bus that new tags were found, and pushes an event
2998 * to all sourcepads. Takes ownership of the @list.
3000 * This is a utility method for elements. Applications should use the
3001 * #GstTagSetter interface.
3004 gst_element_found_tags (GstElement * element, GstTagList * list)
3009 g_return_if_fail (element != NULL);
3010 g_return_if_fail (list != NULL);
3012 iter = gst_element_iterate_src_pads (element);
3013 event = gst_event_new_tag (gst_tag_list_copy (list));
3014 gst_iterator_foreach (iter, (GFunc) push_and_ref, event);
3015 gst_iterator_free (iter);
3016 gst_event_unref (event);
3018 gst_element_post_message (element,
3019 gst_message_new_tag (GST_OBJECT (element), list));
3023 element_find_unconnected_pad (GstElement * element, GstPadDirection direction)
3026 GstPad *unconnected_pad = NULL;
3029 switch (direction) {
3031 iter = gst_element_iterate_src_pads (element);
3034 iter = gst_element_iterate_sink_pads (element);
3037 g_assert_not_reached ();
3044 switch (gst_iterator_next (iter, &pad)) {
3045 case GST_ITERATOR_OK:{
3048 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
3049 GST_DEBUG_PAD_NAME (pad));
3051 peer = gst_pad_get_peer (GST_PAD (pad));
3053 unconnected_pad = pad;
3055 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
3056 "found existing unlinked pad %s:%s",
3057 GST_DEBUG_PAD_NAME (unconnected_pad));
3059 gst_object_unref (pad);
3060 gst_object_unref (peer);
3064 case GST_ITERATOR_DONE:
3067 case GST_ITERATOR_RESYNC:
3068 gst_iterator_resync (iter);
3070 case GST_ITERATOR_ERROR:
3071 g_return_val_if_reached (NULL);
3076 gst_iterator_free (iter);
3078 return unconnected_pad;
3082 * gst_bin_find_unconnected_pad:
3083 * @bin: bin in which to look for elements with unconnected pads
3084 * @direction: whether to look for an unconnected source or sink pad
3086 * Recursively looks for elements with an unconnected pad of the given
3087 * direction within the specified bin and returns an unconnected pad
3088 * if one is found, or NULL otherwise. If a pad is found, the caller
3089 * owns a reference to it and should use gst_object_unref() on the
3090 * pad when it is not needed any longer.
3092 * Returns: unconnected pad of the given direction, or NULL.
3097 gst_bin_find_unconnected_pad (GstBin * bin, GstPadDirection direction)
3103 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3104 g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3107 iter = gst_bin_iterate_recurse (bin);
3111 switch (gst_iterator_next (iter, &element)) {
3112 case GST_ITERATOR_OK:
3113 pad = element_find_unconnected_pad (GST_ELEMENT (element), direction);
3114 gst_object_unref (element);
3118 case GST_ITERATOR_DONE:
3121 case GST_ITERATOR_RESYNC:
3122 gst_iterator_resync (iter);
3124 case GST_ITERATOR_ERROR:
3125 g_return_val_if_reached (NULL);
3130 gst_iterator_free (iter);
3135 #ifndef GST_DISABLE_PARSE
3137 * gst_parse_bin_from_description:
3138 * @bin_description: command line describing the bin
3139 * @ghost_unconnected_pads: whether to automatically create ghost pads
3140 * for unconnected source or sink pads within
3142 * @err: where to store the error message in case of an error, or NULL
3144 * This is a convenience wrapper around gst_parse_launch() to create a
3145 * #GstBin from a gst-launch-style pipeline description. See
3146 * gst_parse_launch() and the gst-launch man page for details about the
3147 * syntax. Ghost pads on the bin for unconnected source or sink pads
3148 * within the bin can automatically be created (but only a maximum of
3149 * one ghost pad for each direction will be created; if you expect
3150 * multiple unconnected source pads or multiple unconnected sink pads
3151 * and want them all ghosted, you will have to create the ghost pads
3154 * Returns: a newly-created bin, or NULL if an error occurred.
3159 gst_parse_bin_from_description (const gchar * bin_description,
3160 gboolean ghost_unconnected_pads, GError ** err)
3166 g_return_val_if_fail (bin_description != NULL, NULL);
3167 g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3169 GST_DEBUG ("Making bin from description '%s'", bin_description);
3171 /* parse the pipeline to a bin */
3172 desc = g_strdup_printf ("bin.( %s )", bin_description);
3173 bin = (GstBin *) gst_parse_launch (desc, err);
3176 if (bin == NULL || (err && *err != NULL)) {
3178 gst_object_unref (bin);
3182 /* find pads and ghost them if necessary */
3183 if (ghost_unconnected_pads) {
3184 if ((pad = gst_bin_find_unconnected_pad (bin, GST_PAD_SRC))) {
3185 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3186 gst_object_unref (pad);
3188 if ((pad = gst_bin_find_unconnected_pad (bin, GST_PAD_SINK))) {
3189 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3190 gst_object_unref (pad);
3194 return GST_ELEMENT (bin);