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"
40 #include "gst-i18n-lib.h"
45 * @mem: a pointer to the memory to dump
46 * @size: the size of the memory block to dump
48 * Dumps the memory block into a hex representation. Useful for debugging.
51 gst_util_dump_mem (const guchar * mem, guint size)
54 GString *string = g_string_sized_new (50);
55 GString *chars = g_string_sized_new (18);
59 if (g_ascii_isprint (mem[i]))
60 g_string_append_printf (chars, "%c", mem[i]);
62 g_string_append_printf (chars, ".");
64 g_string_append_printf (string, "%02x ", mem[i]);
69 if (j == 16 || i == size) {
70 g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
71 string->str, chars->str);
72 g_string_set_size (string, 0);
73 g_string_set_size (chars, 0);
77 g_string_free (string, TRUE);
78 g_string_free (chars, TRUE);
83 * gst_util_set_value_from_string:
84 * @value: the value to set
85 * @value_str: the string to get the value from
87 * Converts the string to the type of the value and
88 * sets the value with it.
91 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
95 g_return_if_fail (value != NULL);
96 g_return_if_fail (value_str != NULL);
98 GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
99 g_type_name (G_VALUE_TYPE (value)));
101 switch (G_VALUE_TYPE (value)) {
103 g_value_set_string (value, value_str);
109 sscanf_ret = sscanf (value_str, "%d", &i);
110 g_return_if_fail (sscanf_ret == 1);
111 g_value_set_int (value, i);
117 sscanf_ret = sscanf (value_str, "%u", &i);
118 g_return_if_fail (sscanf_ret == 1);
119 g_value_set_uint (value, i);
125 sscanf_ret = sscanf (value_str, "%ld", &i);
126 g_return_if_fail (sscanf_ret == 1);
127 g_value_set_long (value, i);
133 sscanf_ret = sscanf (value_str, "%lu", &i);
134 g_return_if_fail (sscanf_ret == 1);
135 g_value_set_ulong (value, i);
138 case G_TYPE_BOOLEAN:{
141 if (!g_ascii_strncasecmp ("true", value_str, 4))
143 g_value_set_boolean (value, i);
149 sscanf_ret = sscanf (value_str, "%c", &i);
150 g_return_if_fail (sscanf_ret == 1);
151 g_value_set_char (value, i);
157 sscanf_ret = sscanf (value_str, "%c", &i);
158 g_return_if_fail (sscanf_ret == 1);
159 g_value_set_uchar (value, i);
165 sscanf_ret = sscanf (value_str, "%f", &i);
166 g_return_if_fail (sscanf_ret == 1);
167 g_value_set_float (value, i);
173 sscanf_ret = sscanf (value_str, "%g", &i);
174 g_return_if_fail (sscanf_ret == 1);
175 g_value_set_double (value, (gdouble) i);
184 * gst_util_set_object_arg:
185 * @object: the object to set the argument of
186 * @name: the name of the argument to set
187 * @value: the string value to set
189 * Convertes the string value to the type of the objects argument and
190 * sets the argument with it.
193 gst_util_set_object_arg (GObject * object, const gchar * name,
199 GParamSpec *paramspec;
202 g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
208 GST_DEBUG ("paramspec->flags is %d, paramspec->value_type is %d",
209 paramspec->flags, (gint) paramspec->value_type);
211 if (paramspec->flags & G_PARAM_WRITABLE) {
212 switch (paramspec->value_type) {
214 g_object_set (G_OBJECT (object), name, value, NULL);
220 sscanf_ret = sscanf (value, "%d", &i);
221 g_return_if_fail (sscanf_ret == 1);
222 g_object_set (G_OBJECT (object), name, i, NULL);
228 sscanf_ret = sscanf (value, "%u", &i);
229 g_return_if_fail (sscanf_ret == 1);
230 g_object_set (G_OBJECT (object), name, i, NULL);
236 sscanf_ret = sscanf (value, "%ld", &i);
237 g_return_if_fail (sscanf_ret == 1);
238 g_object_set (G_OBJECT (object), name, i, NULL);
244 sscanf_ret = sscanf (value, "%lu", &i);
245 g_return_if_fail (sscanf_ret == 1);
246 g_object_set (G_OBJECT (object), name, i, NULL);
249 case G_TYPE_BOOLEAN:{
252 if (!g_ascii_strncasecmp ("true", value, 4))
254 g_object_set (G_OBJECT (object), name, i, NULL);
260 sscanf_ret = sscanf (value, "%c", &i);
261 g_return_if_fail (sscanf_ret == 1);
262 g_object_set (G_OBJECT (object), name, i, NULL);
268 sscanf_ret = sscanf (value, "%c", &i);
269 g_return_if_fail (sscanf_ret == 1);
270 g_object_set (G_OBJECT (object), name, i, NULL);
276 sscanf_ret = sscanf (value, "%f", &i);
277 g_return_if_fail (sscanf_ret == 1);
278 g_object_set (G_OBJECT (object), name, i, NULL);
284 sscanf_ret = sscanf (value, "%g", &i);
285 g_return_if_fail (sscanf_ret == 1);
286 g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
290 if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
293 sscanf_ret = sscanf (value, "%d", &i);
294 g_return_if_fail (sscanf_ret == 1);
295 g_object_set (G_OBJECT (object), name, i, NULL);
303 /* work around error C2520: conversion from unsigned __int64 to double
304 * not implemented, use signed __int64
306 * These are implemented as functions because on some platforms a 64bit int to
307 * double conversion is not defined/implemented.
311 gst_util_guint64_to_gdouble (guint64 value)
313 if (value & G_GINT64_CONSTANT (0x8000000000000000))
314 return (gdouble) ((gint64) value) + (gdouble) 18446744073709551616.;
316 return (gdouble) ((gint64) value);
320 gst_util_gdouble_to_guint64 (gdouble value)
322 if (value < (gdouble) 9223372036854775808.) /* 1 << 63 */
323 return ((guint64) ((gint64) value));
325 value -= (gdouble) 18446744073709551616.;
326 return ((guint64) ((gint64) value));
329 /* convenience struct for getting high and low uint32 parts of
336 #if G_BYTE_ORDER == G_BIG_ENDIAN
344 /* based on Hacker's Delight p152 */
346 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
348 GstUInt64 q1, q0, rhat;
349 GstUInt64 v, cmp1, cmp2;
354 /* count number of leading zeroes, we know they must be in the high
355 * part of denom since denom > G_MAXUINT32. */
356 s = v.l.high | (v.l.high >> 1);
360 s = ~(s | (s >> 16));
361 s = s - ((s >> 1) & 0x55555555);
362 s = (s & 0x33333333) + ((s >> 2) & 0x33333333);
363 s = (s + (s >> 4)) & 0x0f0f0f0f;
365 s = (s + (s >> 16)) & 0x3f;
368 /* normalize divisor and dividend */
370 c1.ll = (c1.ll << s) | (c0.l.high >> (32 - s));
374 q1.ll = c1.ll / v.l.high;
375 rhat.ll = c1.ll - q1.ll * v.l.high;
377 cmp1.l.high = rhat.l.low;
378 cmp1.l.low = c0.l.high;
379 cmp2.ll = q1.ll * v.l.low;
381 while (q1.l.high || cmp2.ll > cmp1.ll) {
386 cmp1.l.high = rhat.l.low;
389 c1.l.high = c1.l.low;
390 c1.l.low = c0.l.high;
391 c1.ll -= q1.ll * v.ll;
392 q0.ll = c1.ll / v.l.high;
393 rhat.ll = c1.ll - q0.ll * v.l.high;
395 cmp1.l.high = rhat.l.low;
396 cmp1.l.low = c0.l.low;
397 cmp2.ll = q0.ll * v.l.low;
399 while (q0.l.high || cmp2.ll > cmp1.ll) {
404 cmp1.l.high = rhat.l.low;
407 q0.l.high += q1.l.low;
413 gst_util_uint64_scale_int64 (guint64 val, guint64 num, guint64 denom)
415 GstUInt64 a0, a1, b0, b1, c0, ct, c1, result;
422 /* do 128 bits multiply
430 * -------------------
433 a0.ll = (guint64) v.l.low * n.l.low;
434 a1.ll = (guint64) v.l.low * n.l.high;
435 b0.ll = (guint64) v.l.high * n.l.low;
436 b1.ll = (guint64) v.l.high * n.l.high;
438 /* and sum together with carry into 128 bits c1, c0 */
440 ct.ll = (guint64) a0.l.high + a1.l.low + b0.l.low;
441 c0.l.high = ct.l.low;
442 c1.ll = (guint64) a1.l.high + b0.l.high + ct.l.high + b1.ll;
444 /* if high bits bigger than denom, we overflow */
448 /* shortcut for division by 1, c1.ll should be 0 because of the
449 * overflow check above. */
453 /* and 128/64 bits division, result fits 64 bits */
454 if (denom <= G_MAXUINT32) {
455 guint32 den = (guint32) denom;
457 /* easy case, (c1,c0)128/(den)32 division */
459 c1.l.high = c1.ll % den;
460 c1.l.low = c0.l.high;
461 c0.l.high = c1.ll % den;
462 result.l.high = c1.ll / den;
463 result.l.low = c0.ll / den;
465 result.ll = gst_util_div128_64 (c1, c0, denom);
476 * gst_util_uint64_scale:
477 * @val: the number to scale
478 * @num: the numerator of the scale ratio
479 * @denom: the denominator of the scale ratio
481 * Scale @val by @num / @denom, trying to avoid overflows.
483 * This function can potentially be very slow if denom > G_MAXUINT32.
485 * Returns: @val * @num / @denom, trying to avoid overflows.
486 * In the case of an overflow, this function returns G_MAXUINT64.
489 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom)
491 g_return_val_if_fail (denom != 0, G_MAXUINT64);
496 if (num == 1 && denom == 1)
499 /* if the denom is high, we need to do a 64 muldiv */
500 if (denom > G_MAXINT32)
503 /* if num and denom are low we can do a 32 bit muldiv */
504 if (num <= G_MAXINT32)
507 /* val and num are high, we need 64 muldiv */
508 if (val > G_MAXINT32)
511 /* val is low and num is high, we can swap them and do 32 muldiv */
512 return gst_util_uint64_scale_int (num, (gint) val, (gint) denom);
515 return gst_util_uint64_scale_int (val, (gint) num, (gint) denom);
518 /* to the more heavy implementations... */
519 return gst_util_uint64_scale_int64 (val, num, denom);
523 * gst_util_uint64_scale_int:
524 * @val: guint64 (such as a #GstClockTime) to scale.
525 * @num: numerator of the scale factor.
526 * @denom: denominator of the scale factor.
528 * Scale a guint64 by a factor expressed as a fraction (num/denom), avoiding
529 * overflows and loss of precision.
531 * @num and @denom must be positive integers. @denom cannot be 0.
533 * Returns: @val * @num / @denom, avoiding overflow and loss of precision.
534 * In the case of an overflow, this function returns G_MAXUINT64.
537 gst_util_uint64_scale_int (guint64 val, gint num, gint denom)
542 g_return_val_if_fail (denom > 0, G_MAXUINT64);
543 g_return_val_if_fail (num >= 0, G_MAXUINT64);
548 if (num == 1 && denom == 1)
551 if (val <= G_MAXUINT32)
553 return val * num / denom;
555 /* do 96 bits mult/div */
557 result.ll = ((guint64) low.l.low) * num;
558 high.ll = ((guint64) low.l.high) * num + (result.l.high);
560 low.ll = high.ll / denom;
561 result.l.high = high.ll % denom;
565 if (low.ll + result.l.high > G_MAXUINT32)
568 result.l.high += low.l.low;
578 /* -----------------------------------------------------
580 * The following code will be moved out of the main
581 * gstreamer library someday.
587 string_append_indent (GString * str, gint count)
591 for (xx = 0; xx < count; xx++)
592 g_string_append_c (str, ' ');
596 * gst_print_pad_caps:
597 * @buf: the buffer to print the caps in
598 * @indent: initial indentation
599 * @pad: the pad to print the caps from
601 * Write the pad capabilities in a human readable format into
605 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
612 string_append_indent (buf, indent);
613 g_string_printf (buf, "%s:%s has no capabilities",
614 GST_DEBUG_PAD_NAME (pad));
618 s = gst_caps_to_string (caps);
619 g_string_append (buf, s);
625 * gst_print_element_args:
626 * @buf: the buffer to print the args in
627 * @indent: initial indentation
628 * @element: the element to print the args of
630 * Print the element argument in a human readable format in the given
634 gst_print_element_args (GString * buf, gint indent, GstElement * element)
637 GValue value = { 0, }; /* the important thing is that value.type = 0 */
639 GParamSpec *spec, **specs, **walk;
641 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
644 for (walk = specs; *walk; walk++) {
646 if (width < strlen (spec->name))
647 width = strlen (spec->name);
650 for (walk = specs; *walk; walk++) {
653 if (spec->flags & G_PARAM_READABLE) {
654 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
655 g_object_get_property (G_OBJECT (element), spec->name, &value);
656 str = g_strdup_value_contents (&value);
657 g_value_unset (&value);
659 str = g_strdup ("Parameter not readable.");
662 string_append_indent (buf, indent);
663 g_string_append (buf, spec->name);
664 string_append_indent (buf, 2 + width - strlen (spec->name));
665 g_string_append (buf, str);
666 g_string_append_c (buf, '\n');
675 * gst_element_create_all_pads:
676 * @element: a #GstElement to create pads for
678 * Creates a pad for each pad template that is always available.
679 * This function is only useful during object intialization of
680 * subclasses of #GstElement.
683 gst_element_create_all_pads (GstElement * element)
687 /* FIXME: lock element */
690 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
691 (G_OBJECT_GET_CLASS (element)));
694 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
696 if (padtempl->presence == GST_PAD_ALWAYS) {
699 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
701 gst_element_add_pad (element, pad);
703 padlist = padlist->next;
708 * gst_element_get_compatible_pad_template:
709 * @element: a #GstElement to get a compatible pad template for.
710 * @compattempl: the #GstPadTemplate to find a compatible template for.
712 * Retrieves a pad template from @element that is compatible with @compattempl.
713 * Pads from compatible templates can be linked together.
715 * Returns: a compatible #GstPadTemplate, or NULL if none was found. No
716 * unreferencing is necessary.
719 gst_element_get_compatible_pad_template (GstElement * element,
720 GstPadTemplate * compattempl)
722 GstPadTemplate *newtempl = NULL;
724 GstElementClass *class;
726 g_return_val_if_fail (element != NULL, NULL);
727 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
728 g_return_val_if_fail (compattempl != NULL, NULL);
730 class = GST_ELEMENT_GET_CLASS (element);
732 padlist = gst_element_class_get_pad_template_list (class);
734 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
735 "Looking for a suitable pad template in %s out of %d templates...",
736 GST_ELEMENT_NAME (element), g_list_length (padlist));
739 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
740 GstCaps *intersection;
744 * Check direction (must be opposite)
747 GST_CAT_LOG (GST_CAT_CAPS,
748 "checking pad template %s", padtempl->name_template);
749 if (padtempl->direction != compattempl->direction) {
750 GST_CAT_DEBUG (GST_CAT_CAPS,
751 "compatible direction: found %s pad template \"%s\"",
752 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
753 padtempl->name_template);
755 GST_CAT_DEBUG (GST_CAT_CAPS,
756 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
757 GST_CAT_DEBUG (GST_CAT_CAPS,
758 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
760 intersection = gst_caps_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
761 GST_PAD_TEMPLATE_CAPS (padtempl));
763 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible %" GST_PTR_FORMAT,
764 (intersection ? "" : "not "), intersection);
766 if (!gst_caps_is_empty (intersection))
768 gst_caps_unref (intersection);
773 padlist = g_list_next (padlist);
776 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
777 "Returning new pad template %p", newtempl);
779 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
785 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
788 GstPad *newpad = NULL;
789 GstElementClass *oclass;
791 oclass = GST_ELEMENT_GET_CLASS (element);
793 if (oclass->request_new_pad)
794 newpad = (oclass->request_new_pad) (element, templ, name);
797 gst_object_ref (newpad);
805 * gst_element_get_pad_from_template:
806 * @element: a #GstElement.
807 * @templ: a #GstPadTemplate belonging to @element.
809 * Gets a pad from @element described by @templ. If the presence of @templ is
810 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
813 * Returns: the #GstPad, or NULL if one could not be found or created.
816 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
819 GstPadPresence presence;
821 /* If this function is ever exported, we need check the validity of `element'
822 * and `templ', and to make sure the template actually belongs to the
825 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
829 case GST_PAD_SOMETIMES:
830 ret = gst_element_get_static_pad (element, templ->name_template);
831 if (!ret && presence == GST_PAD_ALWAYS)
833 ("Element %s has an ALWAYS template %s, but no pad of the same name",
834 GST_OBJECT_NAME (element), templ->name_template);
837 case GST_PAD_REQUEST:
838 ret = gst_element_request_pad (element, templ, NULL);
846 * gst_element_request_compatible_pad:
847 * @element: a #GstElement.
848 * @templ: the #GstPadTemplate to which the new pad should be able to link.
850 * Requests a pad from @element. The returned pad should be unlinked and
851 * compatible with @templ. Might return an existing pad, or request a new one.
853 * Returns: a #GstPad, or %NULL if one could not be found or created.
856 gst_element_request_compatible_pad (GstElement * element,
857 GstPadTemplate * templ)
859 GstPadTemplate *templ_new;
862 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
863 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
865 /* FIXME: should really loop through the templates, testing each for
866 * compatibility and pad availability. */
867 templ_new = gst_element_get_compatible_pad_template (element, templ);
869 pad = gst_element_get_pad_from_template (element, templ_new);
871 /* This can happen for non-request pads. No need to unref. */
872 if (pad && GST_PAD_PEER (pad))
879 * gst_element_get_compatible_pad:
880 * @element: a #GstElement in which the pad should be found.
881 * @pad: the #GstPad to find a compatible one for.
882 * @caps: the #GstCaps to use as a filter.
884 * Looks for an unlinked pad to which the given pad can link. It is not
885 * guaranteed that linking the pads will work, though it should work in most
888 * Returns: the #GstPad to which a link can be made, or %NULL if one cannot be
889 * found. gst_object_unref() after usage.
892 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
893 const GstCaps * caps)
896 GstPadTemplate *templ;
898 GstPad *foundpad = NULL;
901 /* FIXME check for caps compatibility */
903 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
904 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
906 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
907 "finding pad in %s compatible with %s:%s",
908 GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
910 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
913 /* try to get an existing unlinked pad */
914 pads = gst_element_iterate_pads (element);
918 switch (gst_iterator_next (pads, &padptr)) {
919 case GST_ITERATOR_OK:
924 current = GST_PAD (padptr);
926 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
927 GST_DEBUG_PAD_NAME (current));
929 peer = gst_pad_get_peer (current);
931 if (peer == NULL && gst_pad_can_link (pad, current)) {
933 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
934 "found existing unlinked pad %s:%s",
935 GST_DEBUG_PAD_NAME (current));
937 gst_iterator_free (pads);
941 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
943 gst_object_unref (current);
945 gst_object_unref (peer);
949 case GST_ITERATOR_DONE:
952 case GST_ITERATOR_RESYNC:
953 gst_iterator_resync (pads);
955 case GST_ITERATOR_ERROR:
956 g_assert_not_reached ();
960 gst_iterator_free (pads);
962 /* try to create a new one */
963 /* requesting is a little crazy, we need a template. Let's create one */
964 templcaps = gst_pad_get_caps (pad);
966 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
967 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
968 foundpad = gst_element_request_compatible_pad (element, templ);
969 gst_object_unref (templ);
972 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
973 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
977 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
978 "Could not find a compatible pad to link to %s:%s",
979 GST_DEBUG_PAD_NAME (pad));
984 * gst_element_state_get_name:
985 * @state: a #GstState to get the name of.
987 * Gets a string representing the given state.
989 * Returns: a string with the name of the state.
991 G_CONST_RETURN gchar *
992 gst_element_state_get_name (GstState state)
995 #ifdef GST_DEBUG_COLOR
996 case GST_STATE_VOID_PENDING:
997 return "VOID_PENDING";
999 return "\033[01;34mNULL\033[00m";
1000 case GST_STATE_READY:
1001 return "\033[01;31mREADY\033[00m";
1002 case GST_STATE_PLAYING:
1003 return "\033[01;32mPLAYING\033[00m";
1004 case GST_STATE_PAUSED:
1005 return "\033[01;33mPAUSED\033[00m";
1007 /* This is a memory leak */
1008 return g_strdup_printf ("\033[01;35;41mUNKNOWN!\033[00m(%d)", state);
1010 case GST_STATE_VOID_PENDING:
1011 return "VOID_PENDING";
1012 case GST_STATE_NULL:
1014 case GST_STATE_READY:
1016 case GST_STATE_PLAYING:
1018 case GST_STATE_PAUSED:
1021 /* This is a memory leak */
1022 return g_strdup_printf ("UNKNOWN!(%d)", state);
1028 * gst_element_state_change_return_get_name:
1029 * @state_ret: a #GstStateChangeReturn to get the name of.
1031 * Gets a string representing the given state change result.
1033 * Returns: a string with the name of the state change result.
1035 G_CONST_RETURN gchar *
1036 gst_element_state_change_return_get_name (GstStateChangeReturn state_ret)
1038 switch (state_ret) {
1039 #ifdef GST_DEBUG_COLOR
1040 case GST_STATE_CHANGE_FAILURE:
1041 return "\033[01;31mFAILURE\033[00m";
1042 case GST_STATE_CHANGE_SUCCESS:
1043 return "\033[01;32mSUCCESS\033[00m";
1044 case GST_STATE_CHANGE_ASYNC:
1045 return "\033[01;33mASYNC\033[00m";
1046 case GST_STATE_CHANGE_NO_PREROLL:
1047 return "\033[01;34mNO_PREROLL\033[00m";
1049 /* This is a memory leak */
1050 return g_strdup_printf ("\033[01;35;41mUNKNOWN!\033[00m(%d)", state_ret);
1052 case GST_STATE_CHANGE_FAILURE:
1054 case GST_STATE_CHANGE_SUCCESS:
1056 case GST_STATE_CHANGE_ASYNC:
1058 case GST_STATE_CHANGE_NO_PREROLL:
1059 return "NO PREROLL";
1061 /* This is a memory leak */
1062 return g_strdup_printf ("UNKNOWN!(%d)", state_ret);
1069 * gst_element_factory_can_src_caps :
1070 * @factory: factory to query
1071 * @caps: the caps to check
1073 * Checks if the factory can source the given capability.
1075 * Returns: true if it can src the capabilities
1078 gst_element_factory_can_src_caps (GstElementFactory * factory,
1079 const GstCaps * caps)
1083 g_return_val_if_fail (factory != NULL, FALSE);
1084 g_return_val_if_fail (caps != NULL, FALSE);
1086 templates = factory->staticpadtemplates;
1089 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1091 if (template->direction == GST_PAD_SRC) {
1092 if (gst_caps_is_always_compatible (gst_static_caps_get (&template->
1093 static_caps), caps))
1096 templates = g_list_next (templates);
1103 * gst_element_factory_can_sink_caps :
1104 * @factory: factory to query
1105 * @caps: the caps to check
1107 * Checks if the factory can sink the given capability.
1109 * Returns: true if it can sink the capabilities
1112 gst_element_factory_can_sink_caps (GstElementFactory * factory,
1113 const GstCaps * caps)
1117 g_return_val_if_fail (factory != NULL, FALSE);
1118 g_return_val_if_fail (caps != NULL, FALSE);
1120 templates = factory->staticpadtemplates;
1123 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1125 if (template->direction == GST_PAD_SINK) {
1126 if (gst_caps_is_always_compatible (caps,
1127 gst_static_caps_get (&template->static_caps)))
1130 templates = g_list_next (templates);
1137 /* if return val is true, *direct_child is a caller-owned ref on the direct
1138 * child of ancestor that is part of object's ancestry */
1140 object_has_ancestor (GstObject * object, GstObject * ancestor,
1141 GstObject ** direct_child)
1143 GstObject *child, *parent;
1146 *direct_child = NULL;
1148 child = gst_object_ref (object);
1149 parent = gst_object_get_parent (object);
1152 if (ancestor == parent) {
1154 *direct_child = child;
1156 gst_object_unref (child);
1157 gst_object_unref (parent);
1161 gst_object_unref (child);
1163 parent = gst_object_get_parent (parent);
1166 gst_object_unref (child);
1171 /* caller owns return */
1173 find_common_root (GstObject * o1, GstObject * o2)
1175 GstObject *top = o1;
1176 GstObject *kid1, *kid2;
1177 GstObject *root = NULL;
1179 while (GST_OBJECT_PARENT (top))
1180 top = GST_OBJECT_PARENT (top);
1182 /* the itsy-bitsy spider... */
1184 if (!object_has_ancestor (o2, top, &kid2))
1187 root = gst_object_ref (top);
1189 if (!object_has_ancestor (o1, kid2, &kid1)) {
1190 gst_object_unref (kid2);
1194 if (!object_has_ancestor (o2, kid1, &kid2)) {
1195 gst_object_unref (kid1);
1202 /* caller does not own return */
1204 ghost_up (GstElement * e, GstPad * pad)
1206 static gint ghost_pad_index = 0;
1209 GstObject *parent = GST_OBJECT_PARENT (e);
1211 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1212 gpad = gst_ghost_pad_new (name, pad);
1215 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1216 g_warning ("Pad named %s already exists in element %s\n",
1217 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1218 gst_object_unref ((GstObject *) gpad);
1226 remove_pad (gpointer ppad, gpointer unused)
1230 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1231 g_warning ("Couldn't remove pad %s from element %s",
1232 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1236 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1237 GSList ** pads_created)
1241 GSList *pads_created_local = NULL;
1243 g_assert (pads_created);
1245 e1 = GST_OBJECT_PARENT (*src);
1246 e2 = GST_OBJECT_PARENT (*sink);
1248 if (G_UNLIKELY (e1 == NULL)) {
1249 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1250 GST_PTR_FORMAT, *src);
1253 if (G_UNLIKELY (e2 == NULL)) {
1254 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1255 GST_PTR_FORMAT, *sink);
1259 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1260 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1261 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1265 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1266 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1268 /* we need to setup some ghost pads */
1269 root = find_common_root (e1, e2);
1271 g_warning ("Trying to connect elements that don't share a common "
1272 "ancestor: %s and %s", GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
1276 while (GST_OBJECT_PARENT (e1) != root) {
1277 *src = ghost_up ((GstElement *) e1, *src);
1280 e1 = GST_OBJECT_PARENT (*src);
1281 pads_created_local = g_slist_prepend (pads_created_local, *src);
1283 while (GST_OBJECT_PARENT (e2) != root) {
1284 *sink = ghost_up ((GstElement *) e2, *sink);
1287 e2 = GST_OBJECT_PARENT (*sink);
1288 pads_created_local = g_slist_prepend (pads_created_local, *sink);
1291 gst_object_unref (root);
1292 *pads_created = g_slist_concat (*pads_created, pads_created_local);
1296 gst_object_unref (root);
1297 g_slist_foreach (pads_created_local, remove_pad, NULL);
1298 g_slist_free (pads_created_local);
1303 pad_link_maybe_ghosting (GstPad * src, GstPad * sink)
1305 GSList *pads_created = NULL;
1308 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1311 ret = (gst_pad_link (src, sink) == GST_PAD_LINK_OK);
1315 g_slist_foreach (pads_created, remove_pad, NULL);
1317 g_slist_free (pads_created);
1323 * gst_element_link_pads:
1324 * @src: a #GstElement containing the source pad.
1325 * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
1326 * @dest: the #GstElement containing the destination pad.
1327 * @destpadname: the name of the #GstPad in destination element,
1328 * or NULL for any pad.
1330 * Links the two named pads of the source and destination elements.
1331 * Side effect is that if one of the pads has no parent, it becomes a
1332 * child of the parent of the other element. If they have different
1333 * parents, the link fails.
1335 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1338 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
1339 GstElement * dest, const gchar * destpadname)
1341 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1342 GstPad *srcpad, *destpad;
1343 GstPadTemplate *srctempl, *desttempl;
1344 GstElementClass *srcclass, *destclass;
1347 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1348 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1350 srcclass = GST_ELEMENT_GET_CLASS (src);
1351 destclass = GST_ELEMENT_GET_CLASS (dest);
1353 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1354 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1355 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1356 destpadname ? destpadname : "(any)");
1360 /* name specified, look it up */
1361 srcpad = gst_element_get_pad (src, srcpadname);
1363 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1364 GST_ELEMENT_NAME (src), srcpadname);
1367 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1368 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1369 GST_DEBUG_PAD_NAME (srcpad));
1370 gst_object_unref (srcpad);
1373 if (GST_PAD_PEER (srcpad) != NULL) {
1374 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
1375 GST_DEBUG_PAD_NAME (srcpad));
1376 gst_object_unref (srcpad);
1382 /* no name given, get the first available pad */
1383 GST_OBJECT_LOCK (src);
1384 srcpads = GST_ELEMENT_PADS (src);
1385 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1387 gst_object_ref (srcpad);
1388 GST_OBJECT_UNLOCK (src);
1391 /* get a destination pad */
1393 /* name specified, look it up */
1394 destpad = gst_element_get_pad (dest, destpadname);
1396 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1397 GST_ELEMENT_NAME (dest), destpadname);
1400 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1401 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1402 GST_DEBUG_PAD_NAME (destpad));
1403 gst_object_unref (destpad);
1406 if (GST_PAD_PEER (destpad) != NULL) {
1407 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
1408 GST_DEBUG_PAD_NAME (destpad));
1409 gst_object_unref (destpad);
1415 /* no name given, get the first available pad */
1416 GST_OBJECT_LOCK (dest);
1417 destpads = GST_ELEMENT_PADS (dest);
1418 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1420 gst_object_ref (destpad);
1421 GST_OBJECT_UNLOCK (dest);
1424 if (srcpadname && destpadname) {
1427 /* two explicitly specified pads */
1428 result = pad_link_maybe_ghosting (srcpad, destpad);
1430 gst_object_unref (srcpad);
1431 gst_object_unref (destpad);
1437 /* loop through the allowed pads in the source, trying to find a
1438 * compatible destination pad */
1439 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1440 "looping through allowed src and dest pads");
1442 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1443 GST_DEBUG_PAD_NAME (srcpad));
1444 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1445 (GST_PAD_PEER (srcpad) == NULL)) {
1450 gst_object_ref (temp);
1452 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1455 if (temp && pad_link_maybe_ghosting (srcpad, temp)) {
1456 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1457 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1459 gst_object_unref (destpad);
1460 gst_object_unref (srcpad);
1461 gst_object_unref (temp);
1466 gst_object_unref (temp);
1469 /* find a better way for this mess */
1471 srcpads = g_list_next (srcpads);
1473 gst_object_unref (srcpad);
1474 srcpad = GST_PAD_CAST (srcpads->data);
1475 gst_object_ref (srcpad);
1481 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1482 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1484 gst_object_unref (destpad);
1488 gst_object_unref (srcpad);
1492 /* loop through the existing pads in the destination */
1494 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1495 GST_DEBUG_PAD_NAME (destpad));
1496 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1497 (GST_PAD_PEER (destpad) == NULL)) {
1498 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1500 if (temp && pad_link_maybe_ghosting (temp, destpad)) {
1501 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1502 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1503 gst_object_unref (temp);
1504 gst_object_unref (destpad);
1508 gst_object_unref (temp);
1512 destpads = g_list_next (destpads);
1514 gst_object_unref (destpad);
1515 destpad = GST_PAD_CAST (destpads->data);
1516 gst_object_ref (destpad);
1523 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1524 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1525 gst_object_unref (destpad);
1529 gst_object_unref (destpad);
1533 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1534 "we might have request pads on both sides, checking...");
1535 srctempls = gst_element_class_get_pad_template_list (srcclass);
1536 desttempls = gst_element_class_get_pad_template_list (destclass);
1538 if (srctempls && desttempls) {
1540 srctempl = (GstPadTemplate *) srctempls->data;
1541 if (srctempl->presence == GST_PAD_REQUEST) {
1542 for (l = desttempls; l; l = l->next) {
1543 desttempl = (GstPadTemplate *) l->data;
1544 if (desttempl->presence == GST_PAD_REQUEST &&
1545 desttempl->direction != srctempl->direction) {
1546 if (gst_caps_is_always_compatible (gst_pad_template_get_caps
1547 (srctempl), gst_pad_template_get_caps (desttempl))) {
1549 gst_element_get_request_pad (src, srctempl->name_template);
1551 gst_element_get_request_pad (dest, desttempl->name_template);
1552 if (pad_link_maybe_ghosting (srcpad, destpad)) {
1553 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1554 "linked pad %s:%s to pad %s:%s",
1555 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1556 gst_object_unref (srcpad);
1557 gst_object_unref (destpad);
1560 /* it failed, so we release the request pads */
1561 gst_element_release_request_pad (src, srcpad);
1562 gst_element_release_request_pad (dest, destpad);
1567 srctempls = srctempls->next;
1571 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1572 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1577 * gst_element_link_pads_filtered:
1578 * @src: a #GstElement containing the source pad.
1579 * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
1580 * @dest: the #GstElement containing the destination pad.
1581 * @destpadname: the name of the #GstPad in destination element or NULL for any pad.
1582 * @filter: the #GstCaps to filter the link, or #NULL for no filter.
1584 * Links the two named pads of the source and destination elements. Side effect
1585 * is that if one of the pads has no parent, it becomes a child of the parent of
1586 * the other element. If they have different parents, the link fails. If @caps
1587 * is not #NULL, makes sure that the caps of the link is a subset of @caps.
1589 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1592 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
1593 GstElement * dest, const gchar * destpadname, GstCaps * filter)
1596 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1597 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1598 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
1601 GstElement *capsfilter;
1603 GstState state, pending;
1605 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1607 GST_ERROR ("Could not make a capsfilter");
1611 parent = gst_object_get_parent (GST_OBJECT (src));
1612 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1614 gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
1616 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
1617 GST_ERROR ("Could not add capsfilter");
1618 gst_object_unref (capsfilter);
1619 gst_object_unref (parent);
1623 if (pending != GST_STATE_VOID_PENDING)
1626 gst_element_set_state (capsfilter, state);
1628 gst_object_unref (parent);
1630 g_object_set (capsfilter, "caps", filter, NULL);
1632 if (gst_element_link_pads (src, srcpadname, capsfilter, "sink")
1633 && gst_element_link_pads (capsfilter, "src", dest, destpadname)) {
1636 GST_INFO ("Could not link elements");
1637 gst_element_set_state (capsfilter, GST_STATE_NULL);
1638 /* this will unlink and unref as appropriate */
1639 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
1643 return gst_element_link_pads (src, srcpadname, dest, destpadname);
1649 * @src: a #GstElement containing the source pad.
1650 * @dest: the #GstElement containing the destination pad.
1652 * Links @src to @dest. The link must be from source to
1653 * destination; the other direction will not be tried. The function looks for
1654 * existing pads that aren't linked yet. It will request new pads if necessary.
1655 * Such pads need to be released manualy when unlinking.
1656 * If multiple links are possible, only one is established.
1658 * Make sure you have added your elements to a bin or pipeline with
1659 * gst_bin_add() before trying to link them.
1661 * Returns: TRUE if the elements could be linked, FALSE otherwise.
1664 gst_element_link (GstElement * src, GstElement * dest)
1666 return gst_element_link_pads_filtered (src, NULL, dest, NULL, NULL);
1670 * gst_element_link_many:
1671 * @element_1: the first #GstElement in the link chain.
1672 * @element_2: the second #GstElement in the link chain.
1673 * @...: the NULL-terminated list of elements to link in order.
1675 * Chain together a series of elements. Uses gst_element_link().
1676 * Make sure you have added your elements to a bin or pipeline with
1677 * gst_bin_add() before trying to link them.
1679 * Returns: TRUE on success, FALSE otherwise.
1682 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
1686 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
1687 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
1689 va_start (args, element_2);
1692 if (!gst_element_link (element_1, element_2))
1695 element_1 = element_2;
1696 element_2 = va_arg (args, GstElement *);
1705 * gst_element_link_filtered:
1706 * @src: a #GstElement containing the source pad.
1707 * @dest: the #GstElement containing the destination pad.
1708 * @filter: the #GstCaps to filter the link, or #NULL for no filter.
1710 * Links @src to @dest using the given caps as filtercaps.
1711 * The link must be from source to
1712 * destination; the other direction will not be tried. The function looks for
1713 * existing pads that aren't linked yet. It will request new pads if necessary.
1714 * If multiple links are possible, only one is established.
1716 * Make sure you have added your elements to a bin or pipeline with
1717 * gst_bin_add() before trying to link them.
1719 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1722 gst_element_link_filtered (GstElement * src, GstElement * dest,
1725 return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
1729 * gst_element_unlink_pads:
1730 * @src: a #GstElement containing the source pad.
1731 * @srcpadname: the name of the #GstPad in source element.
1732 * @dest: a #GstElement containing the destination pad.
1733 * @destpadname: the name of the #GstPad in destination element.
1735 * Unlinks the two named pads of the source and destination elements.
1738 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
1739 GstElement * dest, const gchar * destpadname)
1741 GstPad *srcpad, *destpad;
1743 g_return_if_fail (src != NULL);
1744 g_return_if_fail (GST_IS_ELEMENT (src));
1745 g_return_if_fail (srcpadname != NULL);
1746 g_return_if_fail (dest != NULL);
1747 g_return_if_fail (GST_IS_ELEMENT (dest));
1748 g_return_if_fail (destpadname != NULL);
1750 /* obtain the pads requested */
1751 srcpad = gst_element_get_pad (src, srcpadname);
1752 if (srcpad == NULL) {
1753 GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
1756 destpad = gst_element_get_pad (dest, destpadname);
1757 if (destpad == NULL) {
1758 GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
1760 gst_object_unref (srcpad);
1764 /* we're satisified they can be unlinked, let's do it */
1765 gst_pad_unlink (srcpad, destpad);
1766 gst_object_unref (srcpad);
1767 gst_object_unref (destpad);
1771 * gst_element_unlink_many:
1772 * @element_1: the first #GstElement in the link chain.
1773 * @element_2: the second #GstElement in the link chain.
1774 * @...: the NULL-terminated list of elements to unlink in order.
1776 * Unlinks a series of elements. Uses gst_element_unlink().
1779 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
1783 g_return_if_fail (element_1 != NULL && element_2 != NULL);
1784 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
1786 va_start (args, element_2);
1789 gst_element_unlink (element_1, element_2);
1791 element_1 = element_2;
1792 element_2 = va_arg (args, GstElement *);
1799 * gst_element_unlink:
1800 * @src: the source #GstElement to unlink.
1801 * @dest: the sink #GstElement to unlink.
1803 * Unlinks all source pads of the source element with all sink pads
1804 * of the sink element to which they are linked.
1806 * If the link has been made using gst_element_link(), it could have created an
1807 * requestpad, which has to be released using gst_element_release_request_pad().
1810 gst_element_unlink (GstElement * src, GstElement * dest)
1813 gboolean done = FALSE;
1815 g_return_if_fail (GST_IS_ELEMENT (src));
1816 g_return_if_fail (GST_IS_ELEMENT (dest));
1818 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
1819 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1821 pads = gst_element_iterate_pads (src);
1825 switch (gst_iterator_next (pads, &data)) {
1826 case GST_ITERATOR_OK:
1828 GstPad *pad = GST_PAD_CAST (data);
1830 if (GST_PAD_IS_SRC (pad)) {
1831 GstPad *peerpad = gst_pad_get_peer (pad);
1833 /* see if the pad is connected and is really a pad
1836 GstElement *peerelem;
1838 peerelem = gst_pad_get_parent_element (peerpad);
1840 if (peerelem == dest) {
1841 gst_pad_unlink (pad, peerpad);
1844 gst_object_unref (peerelem);
1846 gst_object_unref (peerpad);
1849 gst_object_unref (pad);
1852 case GST_ITERATOR_RESYNC:
1853 gst_iterator_resync (pads);
1855 case GST_ITERATOR_DONE:
1859 g_assert_not_reached ();
1863 gst_iterator_free (pads);
1867 * gst_element_query_position:
1868 * @element: a #GstElement to invoke the position query on.
1869 * @format: a pointer to the #GstFormat asked for.
1870 * On return contains the #GstFormat used.
1871 * @cur: A location in which to store the current position, or NULL.
1873 * Queries an element for the stream position.
1875 * Returns: TRUE if the query could be performed.
1878 gst_element_query_position (GstElement * element, GstFormat * format,
1884 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1885 g_return_val_if_fail (format != NULL, FALSE);
1887 query = gst_query_new_position (*format);
1888 ret = gst_element_query (element, query);
1891 gst_query_parse_position (query, format, cur);
1893 gst_query_unref (query);
1899 * gst_element_query_duration:
1900 * @element: a #GstElement to invoke the duration query on.
1901 * @format: a pointer to the #GstFormat asked for.
1902 * On return contains the #GstFormat used.
1903 * @duration: A location in which to store the total duration, or NULL.
1905 * Queries an element for the total stream duration.
1907 * Returns: TRUE if the query could be performed.
1910 gst_element_query_duration (GstElement * element, GstFormat * format,
1916 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1917 g_return_val_if_fail (format != NULL, FALSE);
1919 query = gst_query_new_duration (*format);
1920 ret = gst_element_query (element, query);
1923 gst_query_parse_duration (query, format, duration);
1925 gst_query_unref (query);
1931 * gst_element_query_convert:
1932 * @element: a #GstElement to invoke the convert query on.
1933 * @src_format: a #GstFormat to convert from.
1934 * @src_val: a value to convert.
1935 * @dest_format: a pointer to the #GstFormat to convert to.
1936 * @dest_val: a pointer to the result.
1938 * Queries an element to convert @src_val in @src_format to @dest_format.
1940 * Returns: TRUE if the query could be performed.
1943 gst_element_query_convert (GstElement * element, GstFormat src_format,
1944 gint64 src_val, GstFormat * dest_format, gint64 * dest_val)
1949 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1950 g_return_val_if_fail (dest_format != NULL, FALSE);
1951 g_return_val_if_fail (dest_val != NULL, FALSE);
1953 if (*dest_format == src_format) {
1954 *dest_val = src_val;
1958 query = gst_query_new_convert (src_format, src_val, *dest_format);
1959 ret = gst_element_query (element, query);
1962 gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
1964 gst_query_unref (query);
1970 * gst_element_seek_simple
1971 * @element: a #GstElement to seek on
1972 * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
1973 * @seek_flags: seek options; playback applications will usually want to use
1974 * GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here
1975 * @seek_pos: position to seek to (relative to the start); if you are doing
1976 * a seek in #GST_FORMAT_TIME this value is in nanoseconds -
1977 * multiply with #GST_SECOND to convert seconds to nanoseconds or
1978 * with #GST_MSECOND to convert milliseconds to nanoseconds.
1980 * Simple API to perform a seek on the given element, meaning it just seeks
1981 * to the given position relative to the start of the stream. For more complex
1982 * operations like segment seeks (e.g. for looping) or changing the playback
1983 * rate or seeking relative to the last configured playback segment you should
1984 * use gst_element_seek().
1986 * In a completely prerolled PAUSED or PLAYING pipeline, seeking is always
1987 * guaranteed to return %TRUE on a seekable media type or %FALSE when the media
1988 * type is certainly not seekable (such as a live stream).
1990 * Some elements allow for seeking in the READY state, in this
1991 * case they will store the seek event and execute it when they are put to
1992 * PAUSED. If the element supports seek in READY, it will always return %TRUE when
1993 * it receives the event in the READY state.
1995 * Returns: %TRUE if the seek operation succeeded (the seek might not always be
1996 * executed instantly though)
2001 gst_element_seek_simple (GstElement * element, GstFormat format,
2002 GstSeekFlags seek_flags, gint64 seek_pos)
2004 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2005 g_return_val_if_fail (seek_pos >= 0, FALSE);
2007 return gst_element_seek (element, 1.0, format, seek_flags,
2008 GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_NONE, 0);
2013 * @srcpad: the source #GstPad to link.
2014 * @sinkpad: the sink #GstPad to link.
2016 * Checks if the source pad and the sink pad can be linked.
2017 * Both @srcpad and @sinkpad must be unlinked.
2019 * Returns: TRUE if the pads can be linked, FALSE otherwise.
2022 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2024 /* FIXME This function is gross. It's almost a direct copy of
2025 * gst_pad_link_filtered(). Any decent programmer would attempt
2026 * to merge the two functions, which I will do some day. --ds
2029 /* generic checks */
2030 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2031 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2033 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2034 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2036 /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
2037 if (GST_PAD_PEER (srcpad) != NULL) {
2038 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
2039 GST_DEBUG_PAD_NAME (srcpad));
2042 if (GST_PAD_PEER (sinkpad) != NULL) {
2043 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
2044 GST_DEBUG_PAD_NAME (sinkpad));
2047 if (!GST_PAD_IS_SRC (srcpad)) {
2048 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
2049 GST_DEBUG_PAD_NAME (srcpad));
2052 if (!GST_PAD_IS_SINK (sinkpad)) {
2053 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
2054 GST_DEBUG_PAD_NAME (sinkpad));
2057 if (GST_PAD_PARENT (srcpad) == NULL) {
2058 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
2059 GST_DEBUG_PAD_NAME (srcpad));
2062 if (GST_PAD_PARENT (sinkpad) == NULL) {
2063 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
2064 GST_DEBUG_PAD_NAME (srcpad));
2072 * gst_pad_use_fixed_caps:
2073 * @pad: the pad to use
2075 * A helper function you can use that sets the
2076 * @gst_pad_get_fixed_caps_func as the getcaps function for the
2077 * pad. This way the function will always return the negotiated caps
2078 * or in case the pad is not negotiated, the padtemplate caps.
2080 * Use this function on a pad that, once _set_caps() has been called
2081 * on it, cannot be renegotiated to something else.
2084 gst_pad_use_fixed_caps (GstPad * pad)
2086 gst_pad_set_getcaps_function (pad, gst_pad_get_fixed_caps_func);
2090 * gst_pad_get_fixed_caps_func:
2091 * @pad: the pad to use
2093 * A helper function you can use as a GetCaps function that
2094 * will return the currently negotiated caps or the padtemplate
2097 * Returns: The currently negotiated caps or the padtemplate.
2100 gst_pad_get_fixed_caps_func (GstPad * pad)
2104 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2106 GST_OBJECT_LOCK (pad);
2107 if (GST_PAD_CAPS (pad)) {
2108 result = GST_PAD_CAPS (pad);
2110 GST_CAT_DEBUG (GST_CAT_CAPS,
2111 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2113 result = gst_caps_ref (result);
2116 if (GST_PAD_PAD_TEMPLATE (pad)) {
2117 GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
2119 result = GST_PAD_TEMPLATE_CAPS (templ);
2120 GST_CAT_DEBUG (GST_CAT_CAPS,
2121 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2124 result = gst_caps_ref (result);
2127 GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
2128 result = gst_caps_new_empty ();
2131 GST_OBJECT_UNLOCK (pad);
2137 * gst_pad_get_parent_element:
2140 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2141 * its parent is not an element, return NULL.
2143 * Returns: The parent of the pad. The caller has a reference on the parent, so
2144 * unref when you're finished with it.
2149 gst_pad_get_parent_element (GstPad * pad)
2153 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2155 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2157 if (p && !GST_IS_ELEMENT (p)) {
2158 gst_object_unref (p);
2161 return GST_ELEMENT_CAST (p);
2165 * gst_object_default_error:
2166 * @source: the #GstObject that initiated the error.
2167 * @error: the GError.
2168 * @debug: an additional debug information string, or NULL.
2170 * A default error function.
2172 * The default handler will simply print the error string using g_print.
2175 gst_object_default_error (GstObject * source, GError * error, gchar * debug)
2177 gchar *name = gst_object_get_path_string (source);
2179 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
2181 g_print (_("Additional debug info:\n%s\n"), debug);
2189 * @element_1: the #GstElement element to add to the bin
2190 * @...: additional elements to add to the bin
2192 * Adds a NULL-terminated list of elements to a bin. This function is
2193 * equivalent to calling gst_bin_add() for each member of the list. The return
2194 * value of each gst_bin_add() is ignored.
2197 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2201 g_return_if_fail (GST_IS_BIN (bin));
2202 g_return_if_fail (GST_IS_ELEMENT (element_1));
2204 va_start (args, element_1);
2207 gst_bin_add (bin, element_1);
2209 element_1 = va_arg (args, GstElement *);
2216 * gst_bin_remove_many:
2218 * @element_1: the first #GstElement to remove from the bin
2219 * @...: NULL-terminated list of elements to remove from the bin
2221 * Remove a list of elements from a bin. This function is equivalent
2222 * to calling gst_bin_remove() with each member of the list.
2225 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2229 g_return_if_fail (GST_IS_BIN (bin));
2230 g_return_if_fail (GST_IS_ELEMENT (element_1));
2232 va_start (args, element_1);
2235 gst_bin_remove (bin, element_1);
2237 element_1 = va_arg (args, GstElement *);
2244 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
2245 guint arg_id, GParamFlags flags)
2247 GQuark prop_id = g_quark_from_string (prop_name);
2250 static GQuark fd_id = 0;
2251 static GQuark blocksize_id;
2252 static GQuark bytesperread_id;
2253 static GQuark dump_id;
2254 static GQuark filesize_id;
2255 static GQuark mmapsize_id;
2256 static GQuark location_id;
2257 static GQuark offset_id;
2258 static GQuark silent_id;
2259 static GQuark touch_id;
2262 fd_id = g_quark_from_static_string ("fd");
2263 blocksize_id = g_quark_from_static_string ("blocksize");
2264 bytesperread_id = g_quark_from_static_string ("bytesperread");
2265 dump_id = g_quark_from_static_string ("dump");
2266 filesize_id = g_quark_from_static_string ("filesize");
2267 mmapsize_id = g_quark_from_static_string ("mmapsize");
2268 location_id = g_quark_from_static_string ("location");
2269 offset_id = g_quark_from_static_string ("offset");
2270 silent_id = g_quark_from_static_string ("silent");
2271 touch_id = g_quark_from_static_string ("touch");
2274 if (prop_id == fd_id) {
2275 pspec = g_param_spec_int ("fd", "File-descriptor",
2276 "File-descriptor for the file being read", 0, G_MAXINT, 0, flags);
2277 } else if (prop_id == blocksize_id) {
2278 pspec = g_param_spec_ulong ("blocksize", "Block Size",
2279 "Block size to read per buffer", 0, G_MAXULONG, 4096, flags);
2281 } else if (prop_id == bytesperread_id) {
2282 pspec = g_param_spec_int ("bytesperread", "Bytes per read",
2283 "Number of bytes to read per buffer", G_MININT, G_MAXINT, 0, flags);
2285 } else if (prop_id == dump_id) {
2286 pspec = g_param_spec_boolean ("dump", "Dump",
2287 "Dump bytes to stdout", FALSE, flags);
2289 } else if (prop_id == filesize_id) {
2290 pspec = g_param_spec_int64 ("filesize", "File Size",
2291 "Size of the file being read", 0, G_MAXINT64, 0, flags);
2293 } else if (prop_id == mmapsize_id) {
2294 pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
2295 "Size in bytes of mmap()d regions", 0, G_MAXULONG, 4 * 1048576, flags);
2297 } else if (prop_id == location_id) {
2298 pspec = g_param_spec_string ("location", "File Location",
2299 "Location of the file to read", NULL, flags);
2301 } else if (prop_id == offset_id) {
2302 pspec = g_param_spec_int64 ("offset", "File Offset",
2303 "Byte offset of current read pointer", 0, G_MAXINT64, 0, flags);
2305 } else if (prop_id == silent_id) {
2306 pspec = g_param_spec_boolean ("silent", "Silent", "Don't produce events",
2309 } else if (prop_id == touch_id) {
2310 pspec = g_param_spec_boolean ("touch", "Touch read data",
2311 "Touch data to force disk read before " "push ()", TRUE, flags);
2313 g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
2314 prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
2319 g_object_class_install_property (klass, arg_id, pspec);
2324 * gst_element_class_install_std_props:
2325 * @klass: the #GstElementClass to add the properties to.
2326 * @first_name: the name of the first property.
2327 * in a NULL terminated
2328 * @...: the id and flags of the first property, followed by
2329 * further 'name', 'id', 'flags' triplets and terminated by NULL.
2331 * Adds a list of standardized properties with types to the @klass.
2332 * the id is for the property switch in your get_prop method, and
2333 * the flags determine readability / writeability.
2336 gst_element_class_install_std_props (GstElementClass * klass,
2337 const gchar * first_name, ...)
2343 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
2345 va_start (args, first_name);
2350 int arg_id = va_arg (args, int);
2351 int flags = va_arg (args, int);
2353 gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id,
2356 name = va_arg (args, char *);
2365 * @buf1: the first source #GstBuffer to merge.
2366 * @buf2: the second source #GstBuffer to merge.
2368 * Create a new buffer that is the concatenation of the two source
2369 * buffers. The original source buffers will not be modified or
2370 * unref'd. Make sure you unref the source buffers if they are not used
2371 * anymore afterwards.
2373 * If the buffers point to contiguous areas of memory, the buffer
2374 * is created without copying the data.
2376 * Returns: the new #GstBuffer which is the concatenation of the source buffers.
2379 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
2383 /* we're just a specific case of the more general gst_buffer_span() */
2384 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
2391 * @buf1: the first source #GstBuffer.
2392 * @buf2: the second source #GstBuffer.
2394 * Create a new buffer that is the concatenation of the two source
2395 * buffers, and unrefs the original source buffers.
2397 * If the buffers point to contiguous areas of memory, the buffer
2398 * is created without copying the data.
2400 * Returns: the new #GstBuffer which is the concatenation of the source buffers.
2403 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
2407 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
2408 gst_buffer_unref (buf1);
2409 gst_buffer_unref (buf2);
2417 * @dest: buffer to stamp
2418 * @src: buffer to stamp from
2420 * Copies additional information (the timestamp, duration, and offset start
2421 * and end) from one buffer to the other.
2423 * This function does not copy any buffer flags or caps and is equivalent to
2424 * gst_buffer_copy_metadata(@dest, @src, GST_BUFFER_COPY_TIMESTAMPS).
2426 * Deprecated: use gst_buffer_copy_metadata() instead, it provides more
2429 #ifndef GST_REMOVE_DEPRECATED
2431 gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src)
2433 gst_buffer_copy_metadata (dest, src, GST_BUFFER_COPY_TIMESTAMPS);
2438 intersect_caps_func (GstPad * pad, GValue * ret, GstPad * orig)
2441 GstCaps *peercaps, *existing;
2443 existing = g_value_get_pointer (ret);
2444 peercaps = gst_pad_peer_get_caps (pad);
2445 if (peercaps == NULL)
2446 peercaps = gst_caps_new_any ();
2447 g_value_set_pointer (ret, gst_caps_intersect (existing, peercaps));
2448 gst_caps_unref (existing);
2449 gst_caps_unref (peercaps);
2451 gst_object_unref (pad);
2456 * gst_pad_proxy_getcaps:
2457 * @pad: a #GstPad to proxy.
2459 * Calls gst_pad_get_allowed_caps() for every other pad belonging to the
2460 * same element as @pad, and returns the intersection of the results.
2462 * This function is useful as a default getcaps function for an element
2463 * that can handle any stream format, but requires all its pads to have
2464 * the same caps. Two such elements are tee and aggregator.
2466 * Returns: the intersection of the other pads' allowed caps.
2469 gst_pad_proxy_getcaps (GstPad * pad)
2471 GstElement *element;
2472 GstCaps *caps, *intersected;
2474 GstIteratorResult res;
2475 GValue ret = { 0, };
2477 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2479 GST_DEBUG ("proxying getcaps for %s:%s", GST_DEBUG_PAD_NAME (pad));
2481 element = gst_pad_get_parent_element (pad);
2482 if (element == NULL)
2485 /* value to hold the return, by default it holds ANY, the ref is taken by
2487 g_value_init (&ret, G_TYPE_POINTER);
2488 g_value_set_pointer (&ret, gst_caps_new_any ());
2490 iter = gst_element_iterate_pads (element);
2493 gst_iterator_fold (iter, (GstIteratorFoldFunction) intersect_caps_func,
2496 case GST_ITERATOR_RESYNC:
2497 /* unref any value stored */
2498 if ((caps = g_value_get_pointer (&ret)))
2499 gst_caps_unref (caps);
2500 /* need to reset the result again to ANY */
2501 g_value_set_pointer (&ret, gst_caps_new_any ());
2502 gst_iterator_resync (iter);
2504 case GST_ITERATOR_DONE:
2505 /* all pads iterated, return collected value */
2508 /* iterator returned _ERROR or premature end with _OK,
2509 * mark an error and exit */
2510 if ((caps = g_value_get_pointer (&ret)))
2511 gst_caps_unref (caps);
2512 g_value_set_pointer (&ret, NULL);
2517 gst_iterator_free (iter);
2519 gst_object_unref (element);
2521 caps = g_value_get_pointer (&ret);
2522 g_value_unset (&ret);
2524 intersected = gst_caps_intersect (caps, gst_pad_get_pad_template_caps (pad));
2525 gst_caps_unref (caps);
2532 g_warning ("Pad list returned error on element %s",
2533 GST_ELEMENT_NAME (element));
2534 gst_iterator_free (iter);
2535 gst_object_unref (element);
2547 link_fold_func (GstPad * pad, GValue * ret, LinkData * data)
2549 gboolean success = TRUE;
2551 if (pad != data->orig) {
2552 success = gst_pad_set_caps (pad, data->caps);
2553 g_value_set_boolean (ret, success);
2555 gst_object_unref (pad);
2561 * gst_pad_proxy_setcaps
2562 * @pad: a #GstPad to proxy from
2563 * @caps: the #GstCaps to link with
2565 * Calls gst_pad_set_caps() for every other pad belonging to the
2566 * same element as @pad. If gst_pad_set_caps() fails on any pad,
2567 * the proxy setcaps fails. May be used only during negotiation.
2569 * Returns: TRUE if sucessful
2572 gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps)
2574 GstElement *element;
2576 GstIteratorResult res;
2577 GValue ret = { 0, };
2580 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2581 g_return_val_if_fail (caps != NULL, FALSE);
2583 GST_DEBUG ("proxying pad link for %s:%s", GST_DEBUG_PAD_NAME (pad));
2585 element = gst_pad_get_parent_element (pad);
2586 if (element == NULL)
2589 iter = gst_element_iterate_pads (element);
2591 g_value_init (&ret, G_TYPE_BOOLEAN);
2592 g_value_set_boolean (&ret, TRUE);
2596 res = gst_iterator_fold (iter, (GstIteratorFoldFunction) link_fold_func,
2598 gst_iterator_free (iter);
2600 if (res != GST_ITERATOR_DONE)
2603 gst_object_unref (element);
2605 /* ok not to unset the gvalue */
2606 return g_value_get_boolean (&ret);
2611 g_warning ("Pad list changed during proxy_pad_link for element %s",
2612 GST_ELEMENT_NAME (element));
2613 gst_object_unref (element);
2619 * gst_pad_query_position:
2620 * @pad: a #GstPad to invoke the position query on.
2621 * @format: a pointer to the #GstFormat asked for.
2622 * On return contains the #GstFormat used.
2623 * @cur: A location in which to store the current position, or NULL.
2625 * Queries a pad for the stream position.
2627 * Returns: TRUE if the query could be performed.
2630 gst_pad_query_position (GstPad * pad, GstFormat * format, gint64 * cur)
2635 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2636 g_return_val_if_fail (format != NULL, FALSE);
2638 query = gst_query_new_position (*format);
2639 ret = gst_pad_query (pad, query);
2642 gst_query_parse_position (query, format, cur);
2644 gst_query_unref (query);
2650 * gst_pad_query_peer_position:
2651 * @pad: a #GstPad on whose peer to invoke the position query on.
2652 * Must be a sink pad.
2653 * @format: a pointer to the #GstFormat asked for.
2654 * On return contains the #GstFormat used.
2655 * @cur: A location in which to store the current position, or NULL.
2657 * Queries the peer of a given sink pad for the stream position.
2659 * Returns: TRUE if the query could be performed.
2662 gst_pad_query_peer_position (GstPad * pad, GstFormat * format, gint64 * cur)
2664 gboolean ret = FALSE;
2667 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2668 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2669 g_return_val_if_fail (format != NULL, FALSE);
2671 peer = gst_pad_get_peer (pad);
2673 ret = gst_pad_query_position (peer, format, cur);
2674 gst_object_unref (peer);
2681 * gst_pad_query_duration:
2682 * @pad: a #GstPad to invoke the duration query on.
2683 * @format: a pointer to the #GstFormat asked for.
2684 * On return contains the #GstFormat used.
2685 * @duration: A location in which to store the total duration, or NULL.
2687 * Queries a pad for the total stream duration.
2689 * Returns: TRUE if the query could be performed.
2692 gst_pad_query_duration (GstPad * pad, GstFormat * format, gint64 * duration)
2697 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2698 g_return_val_if_fail (format != NULL, FALSE);
2700 query = gst_query_new_duration (*format);
2701 ret = gst_pad_query (pad, query);
2704 gst_query_parse_duration (query, format, duration);
2706 gst_query_unref (query);
2712 * gst_pad_query_peer_duration:
2713 * @pad: a #GstPad on whose peer pad to invoke the duration query on.
2714 * Must be a sink pad.
2715 * @format: a pointer to the #GstFormat asked for.
2716 * On return contains the #GstFormat used.
2717 * @duration: A location in which to store the total duration, or NULL.
2719 * Queries the peer pad of a given sink pad for the total stream duration.
2721 * Returns: TRUE if the query could be performed.
2724 gst_pad_query_peer_duration (GstPad * pad, GstFormat * format,
2727 gboolean ret = FALSE;
2730 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2731 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2732 g_return_val_if_fail (format != NULL, FALSE);
2734 peer = gst_pad_get_peer (pad);
2736 ret = gst_pad_query_duration (peer, format, duration);
2737 gst_object_unref (peer);
2744 * gst_pad_query_convert:
2745 * @pad: a #GstPad to invoke the convert query on.
2746 * @src_format: a #GstFormat to convert from.
2747 * @src_val: a value to convert.
2748 * @dest_format: a pointer to the #GstFormat to convert to.
2749 * @dest_val: a pointer to the result.
2751 * Queries a pad to convert @src_val in @src_format to @dest_format.
2753 * Returns: TRUE if the query could be performed.
2756 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2757 GstFormat * dest_format, gint64 * dest_val)
2762 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2763 g_return_val_if_fail (src_val >= 0, FALSE);
2764 g_return_val_if_fail (dest_format != NULL, FALSE);
2765 g_return_val_if_fail (dest_val != NULL, FALSE);
2767 if (*dest_format == src_format) {
2768 *dest_val = src_val;
2772 query = gst_query_new_convert (src_format, src_val, *dest_format);
2773 ret = gst_pad_query (pad, query);
2776 gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
2778 gst_query_unref (query);
2784 * gst_pad_query_peer_convert:
2785 * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
2786 * Must be a sink pad.
2787 * @src_format: a #GstFormat to convert from.
2788 * @src_val: a value to convert.
2789 * @dest_format: a pointer to the #GstFormat to convert to.
2790 * @dest_val: a pointer to the result.
2792 * Queries the peer pad of a given sink pad to convert @src_val in @src_format
2795 * Returns: TRUE if the query could be performed.
2798 gst_pad_query_peer_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2799 GstFormat * dest_format, gint64 * dest_val)
2801 gboolean ret = FALSE;
2804 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2805 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2806 g_return_val_if_fail (src_val >= 0, FALSE);
2807 g_return_val_if_fail (dest_format != NULL, FALSE);
2808 g_return_val_if_fail (dest_val != NULL, FALSE);
2810 peer = gst_pad_get_peer (pad);
2812 ret = gst_pad_query_convert (peer, src_format, src_val, dest_format,
2814 gst_object_unref (peer);
2821 * gst_atomic_int_set:
2822 * @atomic_int: pointer to an atomic integer
2823 * @value: value to set
2825 * Unconditionally sets the atomic integer to @value.
2828 gst_atomic_int_set (gint * atomic_int, gint value)
2832 *atomic_int = value;
2833 /* read acts as a memory barrier */
2834 ignore = g_atomic_int_get (atomic_int);
2838 * gst_pad_add_data_probe:
2839 * @pad: pad to add the data probe handler to
2840 * @handler: function to call when data is passed over pad
2841 * @data: data to pass along with the handler
2843 * Adds a "data probe" to a pad. This function will be called whenever data
2844 * passes through a pad. In this case data means both events and buffers. The
2845 * probe will be called with the data as an argument, meaning @handler should
2846 * have the same callback signature as the 'have-data' signal of #GstPad.
2847 * Note that the data will have a reference count greater than 1, so it will
2848 * be immutable -- you must not change it.
2850 * For source pads, the probe will be called after the blocking function, if any
2851 * (see gst_pad_set_blocked_async()), but before looking up the peer to chain
2852 * to. For sink pads, the probe function will be called before configuring the
2853 * sink with new caps, if any, and before calling the pad's chain function.
2855 * Your data probe should return TRUE to let the data continue to flow, or FALSE
2856 * to drop it. Dropping data is rarely useful, but occasionally comes in handy
2859 * Although probes are implemented internally by connecting @handler to the
2860 * have-data signal on the pad, if you want to remove a probe it is insufficient
2861 * to only call g_signal_handler_disconnect on the returned handler id. To
2862 * remove a probe, use the appropriate function, such as
2863 * gst_pad_remove_data_probe().
2865 * Returns: The handler id.
2868 gst_pad_add_data_probe (GstPad * pad, GCallback handler, gpointer data)
2872 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2873 g_return_val_if_fail (handler != NULL, 0);
2875 GST_OBJECT_LOCK (pad);
2876 sigid = g_signal_connect (pad, "have-data", handler, data);
2877 GST_PAD_DO_EVENT_SIGNALS (pad)++;
2878 GST_PAD_DO_BUFFER_SIGNALS (pad)++;
2879 GST_DEBUG ("adding data probe to pad %s:%s, now %d data, %d event probes",
2880 GST_DEBUG_PAD_NAME (pad),
2881 GST_PAD_DO_BUFFER_SIGNALS (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2882 GST_OBJECT_UNLOCK (pad);
2888 * gst_pad_add_event_probe:
2889 * @pad: pad to add the event probe handler to
2890 * @handler: function to call when data is passed over pad
2891 * @data: data to pass along with the handler
2893 * Adds a probe that will be called for all events passing through a pad. See
2894 * gst_pad_add_data_probe() for more information.
2896 * Returns: The handler id
2899 gst_pad_add_event_probe (GstPad * pad, GCallback handler, gpointer data)
2903 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2904 g_return_val_if_fail (handler != NULL, 0);
2906 GST_OBJECT_LOCK (pad);
2907 sigid = g_signal_connect (pad, "have-data::event", handler, data);
2908 GST_PAD_DO_EVENT_SIGNALS (pad)++;
2909 GST_DEBUG ("adding event probe to pad %s:%s, now %d probes",
2910 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2911 GST_OBJECT_UNLOCK (pad);
2917 * gst_pad_add_buffer_probe:
2918 * @pad: pad to add the buffer probe handler to
2919 * @handler: function to call when data is passed over pad
2920 * @data: data to pass along with the handler
2922 * Adds a probe that will be called for all buffers passing through a pad. See
2923 * gst_pad_add_data_probe() for more information.
2925 * Returns: The handler id
2928 gst_pad_add_buffer_probe (GstPad * pad, GCallback handler, gpointer data)
2932 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2933 g_return_val_if_fail (handler != NULL, 0);
2935 GST_OBJECT_LOCK (pad);
2936 sigid = g_signal_connect (pad, "have-data::buffer", handler, data);
2937 GST_PAD_DO_BUFFER_SIGNALS (pad)++;
2938 GST_DEBUG ("adding buffer probe to pad %s:%s, now %d probes",
2939 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_BUFFER_SIGNALS (pad));
2940 GST_OBJECT_UNLOCK (pad);
2946 * gst_pad_remove_data_probe:
2947 * @pad: pad to remove the data probe handler from
2948 * @handler_id: handler id returned from gst_pad_add_data_probe
2950 * Removes a data probe from @pad.
2953 gst_pad_remove_data_probe (GstPad * pad, guint handler_id)
2955 g_return_if_fail (GST_IS_PAD (pad));
2956 g_return_if_fail (handler_id > 0);
2958 GST_OBJECT_LOCK (pad);
2959 g_signal_handler_disconnect (pad, handler_id);
2960 GST_PAD_DO_BUFFER_SIGNALS (pad)--;
2961 GST_PAD_DO_EVENT_SIGNALS (pad)--;
2963 ("removed data probe from pad %s:%s, now %d event, %d buffer probes",
2964 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad),
2965 GST_PAD_DO_BUFFER_SIGNALS (pad));
2966 GST_OBJECT_UNLOCK (pad);
2971 * gst_pad_remove_event_probe:
2972 * @pad: pad to remove the event probe handler from
2973 * @handler_id: handler id returned from gst_pad_add_event_probe
2975 * Removes an event probe from @pad.
2978 gst_pad_remove_event_probe (GstPad * pad, guint handler_id)
2980 g_return_if_fail (GST_IS_PAD (pad));
2981 g_return_if_fail (handler_id > 0);
2983 GST_OBJECT_LOCK (pad);
2984 g_signal_handler_disconnect (pad, handler_id);
2985 GST_PAD_DO_EVENT_SIGNALS (pad)--;
2986 GST_DEBUG ("removed event probe from pad %s:%s, now %d event probes",
2987 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
2988 GST_OBJECT_UNLOCK (pad);
2992 * gst_pad_remove_buffer_probe:
2993 * @pad: pad to remove the buffer probe handler from
2994 * @handler_id: handler id returned from gst_pad_add_buffer_probe
2996 * Removes a buffer probe from @pad.
2999 gst_pad_remove_buffer_probe (GstPad * pad, guint handler_id)
3001 g_return_if_fail (GST_IS_PAD (pad));
3002 g_return_if_fail (handler_id > 0);
3004 GST_OBJECT_LOCK (pad);
3005 g_signal_handler_disconnect (pad, handler_id);
3006 GST_PAD_DO_BUFFER_SIGNALS (pad)--;
3007 GST_DEBUG ("removed buffer probe from pad %s:%s, now %d buffer probes",
3008 GST_DEBUG_PAD_NAME (pad), GST_PAD_DO_BUFFER_SIGNALS (pad));
3009 GST_OBJECT_UNLOCK (pad);
3014 * gst_element_found_tags_for_pad:
3015 * @element: element for which to post taglist to bus.
3016 * @pad: pad on which to push tag-event.
3017 * @list: the taglist to post on the bus and create event from.
3019 * Posts a message to the bus that new tags were found and pushes the
3020 * tags as event. Takes ownership of the @list.
3022 * This is a utility method for elements. Applications should use the
3023 * #GstTagSetter interface.
3026 gst_element_found_tags_for_pad (GstElement * element,
3027 GstPad * pad, GstTagList * list)
3029 g_return_if_fail (element != NULL);
3030 g_return_if_fail (pad != NULL);
3031 g_return_if_fail (list != NULL);
3033 gst_pad_push_event (pad, gst_event_new_tag (gst_tag_list_copy (list)));
3034 gst_element_post_message (element,
3035 gst_message_new_tag (GST_OBJECT (element), list));
3039 push_and_ref (GstPad * pad, GstEvent * event)
3041 gst_pad_push_event (pad, gst_event_ref (event));
3042 /* iterator refs pad, we unref when we are done with it */
3043 gst_object_unref (pad);
3047 * gst_element_found_tags:
3048 * @element: element for which we found the tags.
3049 * @list: list of tags.
3051 * Posts a message to the bus that new tags were found, and pushes an event
3052 * to all sourcepads. Takes ownership of the @list.
3054 * This is a utility method for elements. Applications should use the
3055 * #GstTagSetter interface.
3058 gst_element_found_tags (GstElement * element, GstTagList * list)
3063 g_return_if_fail (element != NULL);
3064 g_return_if_fail (list != NULL);
3066 iter = gst_element_iterate_src_pads (element);
3067 event = gst_event_new_tag (gst_tag_list_copy (list));
3068 gst_iterator_foreach (iter, (GFunc) push_and_ref, event);
3069 gst_iterator_free (iter);
3070 gst_event_unref (event);
3072 gst_element_post_message (element,
3073 gst_message_new_tag (GST_OBJECT (element), list));
3077 element_find_unconnected_pad (GstElement * element, GstPadDirection direction)
3080 GstPad *unconnected_pad = NULL;
3083 switch (direction) {
3085 iter = gst_element_iterate_src_pads (element);
3088 iter = gst_element_iterate_sink_pads (element);
3091 g_assert_not_reached ();
3098 switch (gst_iterator_next (iter, &pad)) {
3099 case GST_ITERATOR_OK:{
3102 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
3103 GST_DEBUG_PAD_NAME (pad));
3105 peer = gst_pad_get_peer (GST_PAD (pad));
3107 unconnected_pad = pad;
3109 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
3110 "found existing unlinked pad %s:%s",
3111 GST_DEBUG_PAD_NAME (unconnected_pad));
3113 gst_object_unref (pad);
3114 gst_object_unref (peer);
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);
3132 return unconnected_pad;
3136 * gst_bin_find_unconnected_pad:
3137 * @bin: bin in which to look for elements with unconnected pads
3138 * @direction: whether to look for an unconnected source or sink pad
3140 * Recursively looks for elements with an unconnected pad of the given
3141 * direction within the specified bin and returns an unconnected pad
3142 * if one is found, or NULL otherwise. If a pad is found, the caller
3143 * owns a reference to it and should use gst_object_unref() on the
3144 * pad when it is not needed any longer.
3146 * Returns: unconnected pad of the given direction, or NULL.
3151 gst_bin_find_unconnected_pad (GstBin * bin, GstPadDirection direction)
3157 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3158 g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3161 iter = gst_bin_iterate_recurse (bin);
3165 switch (gst_iterator_next (iter, &element)) {
3166 case GST_ITERATOR_OK:
3167 pad = element_find_unconnected_pad (GST_ELEMENT (element), direction);
3168 gst_object_unref (element);
3172 case GST_ITERATOR_DONE:
3175 case GST_ITERATOR_RESYNC:
3176 gst_iterator_resync (iter);
3178 case GST_ITERATOR_ERROR:
3179 g_return_val_if_reached (NULL);
3184 gst_iterator_free (iter);
3190 * gst_parse_bin_from_description:
3191 * @bin_description: command line describing the bin
3192 * @ghost_unconnected_pads: whether to automatically create ghost pads
3193 * for unconnected source or sink pads within
3195 * @err: where to store the error message in case of an error, or NULL
3197 * This is a convenience wrapper around gst_parse_launch() to create a
3198 * #GstBin from a gst-launch-style pipeline description. See
3199 * gst_parse_launch() and the gst-launch man page for details about the
3200 * syntax. Ghost pads on the bin for unconnected source or sink pads
3201 * within the bin can automatically be created (but only a maximum of
3202 * one ghost pad for each direction will be created; if you expect
3203 * multiple unconnected source pads or multiple unconnected sink pads
3204 * and want them all ghosted, you will have to create the ghost pads
3207 * Returns: a newly-created bin, or NULL if an error occurred.
3212 gst_parse_bin_from_description (const gchar * bin_description,
3213 gboolean ghost_unconnected_pads, GError ** err)
3215 #ifndef GST_DISABLE_PARSE
3220 g_return_val_if_fail (bin_description != NULL, NULL);
3221 g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3223 GST_DEBUG ("Making bin from description '%s'", bin_description);
3225 /* parse the pipeline to a bin */
3226 desc = g_strdup_printf ("bin.( %s )", bin_description);
3227 bin = (GstBin *) gst_parse_launch (desc, err);
3230 if (bin == NULL || (err && *err != NULL)) {
3232 gst_object_unref (bin);
3236 /* find pads and ghost them if necessary */
3237 if (ghost_unconnected_pads) {
3238 if ((pad = gst_bin_find_unconnected_pad (bin, GST_PAD_SRC))) {
3239 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3240 gst_object_unref (pad);
3242 if ((pad = gst_bin_find_unconnected_pad (bin, GST_PAD_SINK))) {
3243 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3244 gst_object_unref (pad);
3248 return GST_ELEMENT (bin);
3252 GST_WARNING ("Disabled API called: gst_parse_bin_from_description()");
3254 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
3255 g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
3263 * gst_type_register_static_full:
3264 * @parent_type: The GType of the parent type the newly registered type will
3266 * @type_name: NULL-terminated string used as the name of the new type
3267 * @class_size: Size of the class structure.
3268 * @base_init: Location of the base initialization function (optional).
3269 * @base_finalize: Location of the base finalization function (optional).
3270 * @class_init: Location of the class initialization function for class types
3271 * Location of the default vtable inititalization function for interface
3273 * @class_finalize: Location of the class finalization function for class types.
3274 * Location of the default vtable finalization function for interface types.
3276 * @class_data: User-supplied data passed to the class init/finalize functions.
3277 * @instance_size: Size of the instance (object) structure (required for
3278 * instantiatable types only).
3279 * @n_preallocs: The number of pre-allocated (cached) instances to reserve
3280 * memory for (0 indicates no caching). Ignored on recent GLib's.
3281 * @instance_init: Location of the instance initialization function (optional,
3282 * for instantiatable types only).
3283 * @value_table: A GTypeValueTable function table for generic handling of
3284 * GValues of this type (usually only useful for fundamental types).
3285 * @flags: #GTypeFlags for this GType. E.g: G_TYPE_FLAG_ABSTRACT
3287 * Helper function which constructs a #GTypeInfo structure and registers a
3288 * GType, but which generates less linker overhead than a static const
3289 * #GTypeInfo structure. For further details of the parameters, please see
3290 * #GTypeInfo in the GLib documentation.
3292 * Registers type_name as the name of a new static type derived from
3293 * parent_type. The value of flags determines the nature (e.g. abstract or
3294 * not) of the type. It works by filling a GTypeInfo struct and calling
3295 * g_type_info_register_static().
3297 * Returns: A #GType for the newly-registered type.
3302 gst_type_register_static_full (GType parent_type,
3303 const gchar * type_name,
3305 GBaseInitFunc base_init,
3306 GBaseFinalizeFunc base_finalize,
3307 GClassInitFunc class_init,
3308 GClassFinalizeFunc class_finalize,
3309 gconstpointer class_data,
3310 guint instance_size,
3311 guint16 n_preallocs,
3312 GInstanceInitFunc instance_init,
3313 const GTypeValueTable * value_table, GTypeFlags flags)
3317 info.class_size = class_size;
3318 info.base_init = base_init;
3319 info.base_finalize = base_finalize;
3320 info.class_init = class_init;
3321 info.class_finalize = class_finalize;
3322 info.class_data = class_data;
3323 info.instance_size = instance_size;
3324 info.n_preallocs = n_preallocs;
3325 info.instance_init = instance_init;
3326 info.value_table = value_table;
3328 return g_type_register_static (parent_type, type_name, &info, flags);
3333 * gst_util_get_timestamp:
3335 * Get a timestamp as GstClockTime to be used for interval meassurements.
3336 * The timestamp should not be interpreted in any other way.
3338 * Returns: the timestamp
3342 G_GNUC_PURE GstClockTime
3343 gst_util_get_timestamp (void)
3345 #ifdef HAVE_POSIX_TIMERS
3346 struct timespec now;
3348 clock_gettime (CLOCK_MONOTONIC, &now);
3349 return GST_TIMESPEC_TO_TIME (now);
3353 g_get_current_time (&now);
3354 return GST_TIMEVAL_TO_TIME (now);