1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004 Wim Taymans <wim@fluendo.com>
4 *
5 * gstelement.c: The base element, all elements derive from this
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 /**
24 * SECTION:gstelement
25 * @title: GstElement
26 * @short_description: Abstract base class for all pipeline elements
27 * @see_also: #GstElementFactory, #GstPad
28 * @symbols:
29 * - GST_ELEMENT_METADATA_LONGNAME
30 * - GST_ELEMENT_METADATA_KLASS
31 * - GST_ELEMENT_METADATA_DESCRIPTION
32 * - GST_ELEMENT_METADATA_AUTHOR
33 * - GST_ELEMENT_METADATA_DOC_URI
34 * - GST_ELEMENT_METADATA_ICON_NAME
35 *
36 * GstElement is the abstract base class needed to construct an element that
37 * can be used in a GStreamer pipeline. Please refer to the plugin writers
38 * guide for more information on creating #GstElement subclasses.
39 *
40 * The name of a #GstElement can be get with gst_element_get_name() and set with
41 * gst_element_set_name(). For speed, GST_ELEMENT_NAME() can be used in the
42 * core when using the appropriate locking. Do not use this in plug-ins or
43 * applications in order to retain ABI compatibility.
44 *
45 * Elements can have pads (of the type #GstPad). These pads link to pads on
46 * other elements. #GstBuffer flow between these linked pads.
47 * A #GstElement has a #GList of #GstPad structures for all their input (or sink)
48 * and output (or source) pads.
49 * Core and plug-in writers can add and remove pads with gst_element_add_pad()
50 * and gst_element_remove_pad().
51 *
52 * An existing pad of an element can be retrieved by name with
53 * gst_element_get_static_pad(). A new dynamic pad can be created using
54 * gst_element_request_pad() with a #GstPadTemplate.
55 * An iterator of all pads can be retrieved with gst_element_iterate_pads().
56 *
57 * Elements can be linked through their pads.
58 * If the link is straightforward, use the gst_element_link()
59 * convenience function to link two elements, or gst_element_link_many()
60 * for more elements in a row.
61 * Use gst_element_link_filtered() to link two elements constrained by
62 * a specified set of #GstCaps.
63 * For finer control, use gst_element_link_pads() and
64 * gst_element_link_pads_filtered() to specify the pads to link on
65 * each element by name.
66 *
67 * Each element has a state (see #GstState). You can get and set the state
68 * of an element with gst_element_get_state() and gst_element_set_state().
69 * Setting a state triggers a #GstStateChange. To get a string representation
70 * of a #GstState, use gst_element_state_get_name().
71 *
72 * You can get and set a #GstClock on an element using gst_element_get_clock()
73 * and gst_element_set_clock().
74 * Some elements can provide a clock for the pipeline if
75 * the #GST_ELEMENT_FLAG_PROVIDE_CLOCK flag is set. With the
76 * gst_element_provide_clock() method one can retrieve the clock provided by
77 * such an element.
78 * Not all elements require a clock to operate correctly. If the
79 * #GST_ELEMENT_FLAG_REQUIRE_CLOCK() flag is set, a clock should be set on the
80 * element with gst_element_set_clock().
81 *
82 * Note that clock selection and distribution is normally handled by the
83 * toplevel #GstPipeline so the clock functions are only to be used in very
84 * specific situations.
85 */
86
87 #include "gst_private.h"
88 #include <glib.h>
89 #include <stdarg.h>
90 #include <gobject/gvaluecollector.h>
91
92 #include "gstelement.h"
93 #include "gstelementmetadata.h"
94 #include "gstenumtypes.h"
95 #include "gstbus.h"
96 #include "gsterror.h"
97 #include "gstevent.h"
98 #include "gstghostpad.h"
99 #include "gstutils.h"
100 #include "gstinfo.h"
101 #include "gstquark.h"
102 #include "gsttracerutils.h"
103 #include "gstvalue.h"
104 #include "gst-i18n-lib.h"
105 #include "glib-compat-private.h"
106
107 #ifndef GST_DISABLE_GST_DEBUG
108 #include "printf/printf.h"
109 #endif
110
111 /* Element signals and args */
112 enum
113 {
114 PAD_ADDED,
115 PAD_REMOVED,
116 NO_MORE_PADS,
117 /* add more above */
118 LAST_SIGNAL
119 };
120
121 enum
122 {
123 ARG_0
124 /* FILL ME */
125 };
126
127 static void gst_element_class_init (GstElementClass * klass);
128 static void gst_element_init (GstElement * element);
129 static void gst_element_base_class_init (gpointer g_class);
130
131 static void gst_element_constructed (GObject * object);
132 static void gst_element_dispose (GObject * object);
133 static void gst_element_finalize (GObject * object);
134
135 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
136 GstStateChange transition);
137 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
138 GstState * state, GstState * pending, GstClockTime timeout);
139 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
140 GstState state);
141 static gboolean gst_element_set_clock_func (GstElement * element,
142 GstClock * clock);
143 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
144 static gboolean gst_element_post_message_default (GstElement * element,
145 GstMessage * message);
146 static void gst_element_set_context_default (GstElement * element,
147 GstContext * context);
148
149 static gboolean gst_element_default_send_event (GstElement * element,
150 GstEvent * event);
151 static gboolean gst_element_default_query (GstElement * element,
152 GstQuery * query);
153
154 static GstPadTemplate
155 * gst_element_class_request_pad_simple_template (GstElementClass *
156 element_class, const gchar * name);
157
158 static void gst_element_call_async_func (gpointer data, gpointer user_data);
159
160 static GstObjectClass *parent_class = NULL;
161 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
162
163 static GMutex _element_pool_lock;
164 static GThreadPool *gst_element_pool = NULL;
165
166 /* this is used in gstelementfactory.c:gst_element_register() */
167 GQuark __gst_elementclass_factory = 0;
168
169 /* used for gst_element_type_set_skip_documentation() and
170 * gst_element_factory_get_skip_documentation() */
171 GQuark __gst_elementclass_skip_doc = 0;
172
173 GType
gst_element_get_type(void)174 gst_element_get_type (void)
175 {
176 static gsize gst_element_type = 0;
177
178 if (g_once_init_enter (&gst_element_type)) {
179 GType _type;
180 static const GTypeInfo element_info = {
181 sizeof (GstElementClass),
182 gst_element_base_class_init,
183 NULL, /* base_class_finalize */
184 (GClassInitFunc) gst_element_class_init,
185 NULL,
186 NULL,
187 sizeof (GstElement),
188 0,
189 (GInstanceInitFunc) gst_element_init,
190 NULL
191 };
192
193 _type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
194 &element_info, G_TYPE_FLAG_ABSTRACT);
195
196 __gst_elementclass_factory =
197 g_quark_from_static_string ("GST_ELEMENTCLASS_FACTORY");
198 __gst_elementclass_skip_doc =
199 g_quark_from_static_string ("GST_ELEMENTCLASS_SKIP_DOCUMENTATION");
200 g_once_init_leave (&gst_element_type, _type);
201 }
202 return gst_element_type;
203 }
204
205 static GThreadPool *
gst_element_setup_thread_pool(void)206 gst_element_setup_thread_pool (void)
207 {
208 GError *err = NULL;
209 GThreadPool *pool;
210
211 GST_DEBUG ("creating element thread pool");
212 pool =
213 g_thread_pool_new ((GFunc) gst_element_call_async_func, NULL, -1, FALSE,
214 &err);
215 if (err != NULL) {
216 g_critical ("could not alloc threadpool %s", err->message);
217 g_clear_error (&err);
218 }
219
220 return pool;
221 }
222
223 static void
gst_element_class_init(GstElementClass * klass)224 gst_element_class_init (GstElementClass * klass)
225 {
226 GObjectClass *gobject_class;
227
228 gobject_class = (GObjectClass *) klass;
229
230 parent_class = g_type_class_peek_parent (klass);
231
232 /**
233 * GstElement::pad-added:
234 * @gstelement: the object which received the signal
235 * @new_pad: the pad that has been added
236 *
237 * a new #GstPad has been added to the element. Note that this signal will
238 * usually be emitted from the context of the streaming thread. Also keep in
239 * mind that if you add new elements to the pipeline in the signal handler
240 * you will need to set them to the desired target state with
241 * gst_element_set_state() or gst_element_sync_state_with_parent().
242 */
243 gst_element_signals[PAD_ADDED] =
244 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
245 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
246 NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
247 /**
248 * GstElement::pad-removed:
249 * @gstelement: the object which received the signal
250 * @old_pad: the pad that has been removed
251 *
252 * a #GstPad has been removed from the element
253 */
254 gst_element_signals[PAD_REMOVED] =
255 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
256 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
257 NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
258 /**
259 * GstElement::no-more-pads:
260 * @gstelement: the object which received the signal
261 *
262 * This signals that the element will not generate more dynamic pads.
263 * Note that this signal will usually be emitted from the context of
264 * the streaming thread.
265 */
266 gst_element_signals[NO_MORE_PADS] =
267 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
268 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
269 NULL, NULL, G_TYPE_NONE, 0);
270
271 gobject_class->dispose = gst_element_dispose;
272 gobject_class->finalize = gst_element_finalize;
273 gobject_class->constructed = gst_element_constructed;
274
275 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
276 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
277 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
278 klass->set_clock = GST_DEBUG_FUNCPTR (gst_element_set_clock_func);
279 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
280 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
281 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
282 klass->numpadtemplates = 0;
283 klass->post_message = GST_DEBUG_FUNCPTR (gst_element_post_message_default);
284 klass->set_context = GST_DEBUG_FUNCPTR (gst_element_set_context_default);
285
286 klass->elementfactory = NULL;
287 }
288
289 static void
gst_element_base_class_init(gpointer g_class)290 gst_element_base_class_init (gpointer g_class)
291 {
292 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
293 GList *node, *padtemplates;
294
295 /* Copy the element details here so elements can inherit the
296 * details from their base class and classes only need to set
297 * the details in class_init instead of base_init */
298 element_class->metadata =
299 element_class->metadata ? gst_structure_copy (element_class->metadata) :
300 gst_structure_new_empty ("metadata");
301
302 /* Copy the pad templates so elements inherit them
303 * from their base class but elements can add pad templates in class_init
304 * instead of base_init.
305 */
306 padtemplates = g_list_copy (element_class->padtemplates);
307 for (node = padtemplates; node != NULL; node = node->next) {
308 GstPadTemplate *tmpl = (GstPadTemplate *) node->data;
309 gst_object_ref (tmpl);
310 }
311 element_class->padtemplates = padtemplates;
312
313 /* set the factory, see gst_element_register() */
314 element_class->elementfactory =
315 g_type_get_qdata (G_TYPE_FROM_CLASS (element_class),
316 __gst_elementclass_factory);
317 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "type %s : factory %p",
318 G_OBJECT_CLASS_NAME (element_class), element_class->elementfactory);
319 }
320
321 static void
gst_element_init(GstElement * element)322 gst_element_init (GstElement * element)
323 {
324 GST_STATE (element) = GST_STATE_NULL;
325 GST_STATE_TARGET (element) = GST_STATE_NULL;
326 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
327 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
328 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
329
330 g_rec_mutex_init (&element->state_lock);
331 g_cond_init (&element->state_cond);
332 }
333
334 static void
gst_element_constructed(GObject * object)335 gst_element_constructed (GObject * object)
336 {
337 GST_TRACER_ELEMENT_NEW (GST_ELEMENT_CAST (object));
338 G_OBJECT_CLASS (parent_class)->constructed (object);
339 }
340
341 /**
342 * gst_element_release_request_pad:
343 * @element: a #GstElement to release the request pad of.
344 * @pad: the #GstPad to release.
345 *
346 * Makes the element free the previously requested pad as obtained
347 * with gst_element_request_pad().
348 *
349 * This does not unref the pad. If the pad was created by using
350 * gst_element_request_pad(), gst_element_release_request_pad() needs to be
351 * followed by gst_object_unref() to free the @pad.
352 *
353 * MT safe.
354 */
355 void
gst_element_release_request_pad(GstElement * element,GstPad * pad)356 gst_element_release_request_pad (GstElement * element, GstPad * pad)
357 {
358 GstElementClass *oclass;
359
360 g_return_if_fail (GST_IS_ELEMENT (element));
361 g_return_if_fail (GST_IS_PAD (pad));
362 g_return_if_fail (GST_PAD_PAD_TEMPLATE (pad) == NULL ||
363 GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad)) ==
364 GST_PAD_REQUEST);
365 g_return_if_fail (GST_PAD_PARENT (pad) == element);
366
367 oclass = GST_ELEMENT_GET_CLASS (element);
368
369 /* if the element implements a custom release function we call that, else we
370 * simply remove the pad from the element */
371 if (oclass->release_pad)
372 oclass->release_pad (element, pad);
373 else
374 gst_element_remove_pad (element, pad);
375 }
376
377 /**
378 * gst_element_provide_clock:
379 * @element: a #GstElement to query
380 *
381 * Get the clock provided by the given element.
382 * > An element is only required to provide a clock in the PAUSED
383 * > state. Some elements can provide a clock in other states.
384 *
385 * Returns: (transfer full) (nullable): the GstClock provided by the
386 * element or %NULL if no clock could be provided. Unref after usage.
387 *
388 * MT safe.
389 */
390 GstClock *
gst_element_provide_clock(GstElement * element)391 gst_element_provide_clock (GstElement * element)
392 {
393 GstClock *result = NULL;
394 GstElementClass *oclass;
395
396 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
397
398 oclass = GST_ELEMENT_GET_CLASS (element);
399
400 if (oclass->provide_clock)
401 result = oclass->provide_clock (element);
402
403 return result;
404 }
405
406 static gboolean
gst_element_set_clock_func(GstElement * element,GstClock * clock)407 gst_element_set_clock_func (GstElement * element, GstClock * clock)
408 {
409 GstClock **clock_p;
410
411 GST_OBJECT_LOCK (element);
412 clock_p = &element->clock;
413 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
414 GST_OBJECT_UNLOCK (element);
415
416 return TRUE;
417 }
418
419 /**
420 * gst_element_set_clock:
421 * @element: a #GstElement to set the clock for.
422 * @clock: (transfer none) (allow-none): the #GstClock to set for the element.
423 *
424 * Sets the clock for the element. This function increases the
425 * refcount on the clock. Any previously set clock on the object
426 * is unreffed.
427 *
428 * Returns: %TRUE if the element accepted the clock. An element can refuse a
429 * clock when it, for example, is not able to slave its internal clock to the
430 * @clock or when it requires a specific clock to operate.
431 *
432 * MT safe.
433 */
434 gboolean
gst_element_set_clock(GstElement * element,GstClock * clock)435 gst_element_set_clock (GstElement * element, GstClock * clock)
436 {
437 GstElementClass *oclass;
438 gboolean res = FALSE;
439
440 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
441 g_return_val_if_fail (clock == NULL || GST_IS_CLOCK (clock), FALSE);
442
443 oclass = GST_ELEMENT_GET_CLASS (element);
444
445 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element, "setting clock %p", clock);
446
447 if (oclass->set_clock)
448 res = oclass->set_clock (element, clock);
449
450 return res;
451 }
452
453 /**
454 * gst_element_get_clock:
455 * @element: a #GstElement to get the clock of.
456 *
457 * Gets the currently configured clock of the element. This is the clock as was
458 * last set with gst_element_set_clock().
459 *
460 * Elements in a pipeline will only have their clock set when the
461 * pipeline is in the PLAYING state.
462 *
463 * Returns: (transfer full) (nullable): the #GstClock of the element. unref after usage.
464 *
465 * MT safe.
466 */
467 GstClock *
gst_element_get_clock(GstElement * element)468 gst_element_get_clock (GstElement * element)
469 {
470 GstClock *result;
471
472 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
473
474 GST_OBJECT_LOCK (element);
475 if ((result = element->clock))
476 gst_object_ref (result);
477 GST_OBJECT_UNLOCK (element);
478
479 return result;
480 }
481
482 /**
483 * gst_element_set_base_time:
484 * @element: a #GstElement.
485 * @time: the base time to set.
486 *
487 * Set the base time of an element. See gst_element_get_base_time().
488 *
489 * MT safe.
490 */
491 void
gst_element_set_base_time(GstElement * element,GstClockTime time)492 gst_element_set_base_time (GstElement * element, GstClockTime time)
493 {
494 GstClockTime old;
495
496 g_return_if_fail (GST_IS_ELEMENT (element));
497 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (time));
498
499 GST_OBJECT_LOCK (element);
500 old = element->base_time;
501 element->base_time = time;
502 GST_OBJECT_UNLOCK (element);
503
504 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
505 "set base_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
506 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
507 }
508
509 /**
510 * gst_element_get_base_time:
511 * @element: a #GstElement.
512 *
513 * Returns the base time of the element. The base time is the
514 * absolute time of the clock when this element was last put to
515 * PLAYING. Subtracting the base time from the clock time gives
516 * the running time of the element.
517 *
518 * Returns: the base time of the element.
519 *
520 * MT safe.
521 */
522 GstClockTime
gst_element_get_base_time(GstElement * element)523 gst_element_get_base_time (GstElement * element)
524 {
525 GstClockTime result;
526
527 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
528
529 GST_OBJECT_LOCK (element);
530 result = element->base_time;
531 GST_OBJECT_UNLOCK (element);
532
533 return result;
534 }
535
536 /**
537 * gst_element_set_start_time:
538 * @element: a #GstElement.
539 * @time: the base time to set.
540 *
541 * Set the start time of an element. The start time of the element is the
542 * running time of the element when it last went to the PAUSED state. In READY
543 * or after a flushing seek, it is set to 0.
544 *
545 * Toplevel elements like #GstPipeline will manage the start_time and
546 * base_time on its children. Setting the start_time to #GST_CLOCK_TIME_NONE
547 * on such a toplevel element will disable the distribution of the base_time to
548 * the children and can be useful if the application manages the base_time
549 * itself, for example if you want to synchronize capture from multiple
550 * pipelines, and you can also ensure that the pipelines have the same clock.
551 *
552 * MT safe.
553 */
554 void
gst_element_set_start_time(GstElement * element,GstClockTime time)555 gst_element_set_start_time (GstElement * element, GstClockTime time)
556 {
557 GstClockTime old;
558
559 g_return_if_fail (GST_IS_ELEMENT (element));
560
561 GST_OBJECT_LOCK (element);
562 old = GST_ELEMENT_START_TIME (element);
563 GST_ELEMENT_START_TIME (element) = time;
564 GST_OBJECT_UNLOCK (element);
565
566 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
567 "set start_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
568 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
569 }
570
571 /**
572 * gst_element_get_start_time:
573 * @element: a #GstElement.
574 *
575 * Returns the start time of the element. The start time is the
576 * running time of the clock when this element was last put to PAUSED.
577 *
578 * Usually the start_time is managed by a toplevel element such as
579 * #GstPipeline.
580 *
581 * MT safe.
582 *
583 * Returns: the start time of the element.
584 */
585 GstClockTime
gst_element_get_start_time(GstElement * element)586 gst_element_get_start_time (GstElement * element)
587 {
588 GstClockTime result;
589
590 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
591
592 GST_OBJECT_LOCK (element);
593 result = GST_ELEMENT_START_TIME (element);
594 GST_OBJECT_UNLOCK (element);
595
596 return result;
597 }
598
599 /**
600 * gst_element_get_current_running_time:
601 * @element: a #GstElement.
602 *
603 * Returns the running time of the element. The running time is the
604 * element's clock time minus its base time. Will return GST_CLOCK_TIME_NONE
605 * if the element has no clock, or if its base time has not been set.
606 *
607 * Returns: the running time of the element, or GST_CLOCK_TIME_NONE if the
608 * element has no clock or its base time has not been set.
609 *
610 * Since: 1.18
611 */
612 GstClockTime
gst_element_get_current_running_time(GstElement * element)613 gst_element_get_current_running_time (GstElement * element)
614 {
615 GstClockTime base_time, clock_time;
616
617 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
618
619 base_time = gst_element_get_base_time (element);
620
621 if (!GST_CLOCK_TIME_IS_VALID (base_time)) {
622 GST_DEBUG_OBJECT (element, "Could not determine base time");
623 return GST_CLOCK_TIME_NONE;
624 }
625
626 clock_time = gst_element_get_current_clock_time (element);
627
628 if (!GST_CLOCK_TIME_IS_VALID (clock_time)) {
629 return GST_CLOCK_TIME_NONE;
630 }
631
632 if (clock_time < base_time) {
633 GST_DEBUG_OBJECT (element, "Got negative current running time");
634 return GST_CLOCK_TIME_NONE;
635 }
636
637 return clock_time - base_time;
638 }
639
640 /**
641 * gst_element_get_current_clock_time:
642 * @element: a #GstElement.
643 *
644 * Returns the current clock time of the element, as in, the time of the
645 * element's clock, or GST_CLOCK_TIME_NONE if there is no clock.
646 *
647 * Returns: the clock time of the element, or GST_CLOCK_TIME_NONE if there is
648 * no clock.
649 *
650 * Since: 1.18
651 */
652 GstClockTime
gst_element_get_current_clock_time(GstElement * element)653 gst_element_get_current_clock_time (GstElement * element)
654 {
655 GstClock *clock = NULL;
656 GstClockTime ret;
657
658 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
659
660 clock = gst_element_get_clock (element);
661
662 if (!clock) {
663 GST_DEBUG_OBJECT (element, "Element has no clock");
664 return GST_CLOCK_TIME_NONE;
665 }
666
667 ret = gst_clock_get_time (clock);
668 gst_object_unref (clock);
669
670 return ret;
671 }
672
673 #if 0
674 /**
675 * gst_element_set_index:
676 * @element: a #GstElement.
677 * @index: (transfer none): a #GstIndex.
678 *
679 * Set @index on the element. The refcount of the index
680 * will be increased, any previously set index is unreffed.
681 *
682 * MT safe.
683 */
684 void
685 gst_element_set_index (GstElement * element, GstIndex * index)
686 {
687 GstElementClass *oclass;
688
689 g_return_if_fail (GST_IS_ELEMENT (element));
690 g_return_if_fail (index == NULL || GST_IS_INDEX (index));
691
692 oclass = GST_ELEMENT_GET_CLASS (element);
693
694 if (oclass->set_index)
695 oclass->set_index (element, index);
696 }
697
698 /**
699 * gst_element_get_index:
700 * @element: a #GstElement.
701 *
702 * Gets the index from the element.
703 *
704 * Returns: (transfer full) (nullable): a #GstIndex or %NULL when no
705 * index was set on the element. unref after usage.
706 *
707 * MT safe.
708 */
709 GstIndex *
710 gst_element_get_index (GstElement * element)
711 {
712 GstElementClass *oclass;
713 GstIndex *result = NULL;
714
715 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
716
717 oclass = GST_ELEMENT_GET_CLASS (element);
718
719 if (oclass->get_index)
720 result = oclass->get_index (element);
721
722 return result;
723 }
724 #endif
725
726 /**
727 * gst_element_add_pad:
728 * @element: a #GstElement to add the pad to.
729 * @pad: (transfer floating): the #GstPad to add to the element.
730 *
731 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
732 * see gst_object_set_parent() for refcounting information.
733 *
734 * Pads are automatically activated when added in the PAUSED or PLAYING
735 * state.
736 *
737 * The pad and the element should be unlocked when calling this function.
738 *
739 * This function will emit the #GstElement::pad-added signal on the element.
740 *
741 * Returns: %TRUE if the pad could be added. This function can fail when
742 * a pad with the same name already existed or the pad already had another
743 * parent.
744 *
745 * MT safe.
746 */
747 gboolean
gst_element_add_pad(GstElement * element,GstPad * pad)748 gst_element_add_pad (GstElement * element, GstPad * pad)
749 {
750 gchar *pad_name;
751 gboolean active;
752
753 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
754 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
755
756 /* locking pad to look at the name */
757 GST_OBJECT_LOCK (pad);
758 pad_name = g_strdup (GST_PAD_NAME (pad));
759 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
760 GST_STR_NULL (pad_name));
761 active = GST_PAD_IS_ACTIVE (pad);
762 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_PARENT);
763 GST_OBJECT_UNLOCK (pad);
764
765 /* then check to see if there's already a pad by that name here */
766 GST_OBJECT_LOCK (element);
767 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
768 goto name_exists;
769
770 /* try to set the pad's parent */
771 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
772 GST_OBJECT_CAST (element))))
773 goto had_parent;
774
775 /* check for active pads */
776 if (!active && (GST_STATE (element) > GST_STATE_READY ||
777 GST_STATE_NEXT (element) == GST_STATE_PAUSED)) {
778 gst_pad_set_active (pad, TRUE);
779 }
780
781 g_free (pad_name);
782
783 /* add it to the list */
784 switch (gst_pad_get_direction (pad)) {
785 case GST_PAD_SRC:
786 element->srcpads = g_list_append (element->srcpads, pad);
787 element->numsrcpads++;
788 break;
789 case GST_PAD_SINK:
790 element->sinkpads = g_list_append (element->sinkpads, pad);
791 element->numsinkpads++;
792 break;
793 default:
794 goto no_direction;
795 }
796 element->pads = g_list_append (element->pads, pad);
797 element->numpads++;
798 element->pads_cookie++;
799 GST_OBJECT_UNLOCK (element);
800
801 /* emit the PAD_ADDED signal */
802 g_signal_emit (element, gst_element_signals[PAD_ADDED], 0, pad);
803 GST_TRACER_ELEMENT_ADD_PAD (element, pad);
804 return TRUE;
805
806 /* ERROR cases */
807 name_exists:
808 {
809 g_critical ("Padname %s is not unique in element %s, not adding",
810 pad_name, GST_ELEMENT_NAME (element));
811 GST_OBJECT_UNLOCK (element);
812 g_free (pad_name);
813 gst_object_ref_sink (pad);
814 gst_object_unref (pad);
815 return FALSE;
816 }
817 had_parent:
818 {
819 g_critical
820 ("Pad %s already has parent when trying to add to element %s",
821 pad_name, GST_ELEMENT_NAME (element));
822 GST_OBJECT_UNLOCK (element);
823 g_free (pad_name);
824 return FALSE;
825 }
826 no_direction:
827 {
828 GST_OBJECT_LOCK (pad);
829 g_critical
830 ("Trying to add pad %s to element %s, but it has no direction",
831 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
832 GST_OBJECT_UNLOCK (pad);
833 GST_OBJECT_UNLOCK (element);
834 return FALSE;
835 }
836 }
837
838 /**
839 * gst_element_remove_pad:
840 * @element: a #GstElement to remove pad from.
841 * @pad: (transfer none): the #GstPad to remove from the element.
842 *
843 * Removes @pad from @element. @pad will be destroyed if it has not been
844 * referenced elsewhere using gst_object_unparent().
845 *
846 * This function is used by plugin developers and should not be used
847 * by applications. Pads that were dynamically requested from elements
848 * with gst_element_request_pad() should be released with the
849 * gst_element_release_request_pad() function instead.
850 *
851 * Pads are not automatically deactivated so elements should perform the needed
852 * steps to deactivate the pad in case this pad is removed in the PAUSED or
853 * PLAYING state. See gst_pad_set_active() for more information about
854 * deactivating pads.
855 *
856 * The pad and the element should be unlocked when calling this function.
857 *
858 * This function will emit the #GstElement::pad-removed signal on the element.
859 *
860 * Returns: %TRUE if the pad could be removed. Can return %FALSE if the
861 * pad does not belong to the provided element.
862 *
863 * MT safe.
864 */
865 gboolean
gst_element_remove_pad(GstElement * element,GstPad * pad)866 gst_element_remove_pad (GstElement * element, GstPad * pad)
867 {
868 GstPad *peer;
869
870 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
871 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
872
873 /* locking pad to look at the name and parent */
874 GST_OBJECT_LOCK (pad);
875 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
876 GST_STR_NULL (GST_PAD_NAME (pad)));
877
878 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
879 goto not_our_pad;
880 GST_OBJECT_UNLOCK (pad);
881
882 /* unlink */
883 if ((peer = gst_pad_get_peer (pad))) {
884 /* window for MT unsafeness, someone else could unlink here
885 * and then we call unlink with wrong pads. The unlink
886 * function would catch this and safely return failed. */
887 if (GST_PAD_IS_SRC (pad))
888 gst_pad_unlink (pad, peer);
889 else
890 gst_pad_unlink (peer, pad);
891
892 gst_object_unref (peer);
893 }
894
895 /* if this is a ghost pad we also need to unset the target or it
896 * will stay linked although not allowed according to the topology.
897 *
898 * FIXME 2.0: Do this generically somehow from inside GstGhostPad
899 * when it gets unparented.
900 */
901 if (GST_IS_GHOST_PAD (pad)) {
902 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
903 }
904
905 GST_OBJECT_LOCK (element);
906 /* remove it from the list */
907 switch (gst_pad_get_direction (pad)) {
908 case GST_PAD_SRC:
909 element->srcpads = g_list_remove (element->srcpads, pad);
910 element->numsrcpads--;
911 break;
912 case GST_PAD_SINK:
913 element->sinkpads = g_list_remove (element->sinkpads, pad);
914 element->numsinkpads--;
915 break;
916 default:
917 g_critical ("Removing pad without direction???");
918 break;
919 }
920 element->pads = g_list_remove (element->pads, pad);
921 element->numpads--;
922 element->pads_cookie++;
923 GST_OBJECT_UNLOCK (element);
924
925 /* emit the PAD_REMOVED signal before unparenting and losing the last ref. */
926 g_signal_emit (element, gst_element_signals[PAD_REMOVED], 0, pad);
927 GST_TRACER_ELEMENT_REMOVE_PAD (element, pad);
928 gst_object_unparent (GST_OBJECT_CAST (pad));
929
930 return TRUE;
931
932 /* ERRORS */
933 not_our_pad:
934 {
935 /* locking order is element > pad */
936 GST_OBJECT_UNLOCK (pad);
937
938 GST_OBJECT_LOCK (element);
939 GST_OBJECT_LOCK (pad);
940 g_critical ("Padname %s:%s does not belong to element %s when removing",
941 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
942 GST_OBJECT_UNLOCK (pad);
943 GST_OBJECT_UNLOCK (element);
944 return FALSE;
945 }
946 }
947
948 /**
949 * gst_element_no_more_pads:
950 * @element: a #GstElement
951 *
952 * Use this function to signal that the element does not expect any more pads
953 * to show up in the current pipeline. This function should be called whenever
954 * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
955 * pad templates use this in combination with autopluggers to figure out that
956 * the element is done initializing its pads.
957 *
958 * This function emits the #GstElement::no-more-pads signal.
959 *
960 * MT safe.
961 */
962 void
gst_element_no_more_pads(GstElement * element)963 gst_element_no_more_pads (GstElement * element)
964 {
965 g_return_if_fail (GST_IS_ELEMENT (element));
966
967 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
968 }
969
970 static gint
pad_compare_name(GstPad * pad1,const gchar * name)971 pad_compare_name (GstPad * pad1, const gchar * name)
972 {
973 gint result;
974
975 GST_OBJECT_LOCK (pad1);
976 result = strcmp (GST_PAD_NAME (pad1), name);
977 GST_OBJECT_UNLOCK (pad1);
978
979 return result;
980 }
981
982 /**
983 * gst_element_get_static_pad:
984 * @element: a #GstElement to find a static pad of.
985 * @name: the name of the static #GstPad to retrieve.
986 *
987 * Retrieves a pad from @element by name. This version only retrieves
988 * already-existing (i.e. 'static') pads.
989 *
990 * Returns: (transfer full) (nullable): the requested #GstPad if
991 * found, otherwise %NULL. unref after usage.
992 *
993 * MT safe.
994 */
995 GstPad *
gst_element_get_static_pad(GstElement * element,const gchar * name)996 gst_element_get_static_pad (GstElement * element, const gchar * name)
997 {
998 GList *find;
999 GstPad *result = NULL;
1000
1001 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1002 g_return_val_if_fail (name != NULL, NULL);
1003
1004 GST_OBJECT_LOCK (element);
1005 find =
1006 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
1007 if (find) {
1008 result = GST_PAD_CAST (find->data);
1009 gst_object_ref (result);
1010 }
1011
1012 if (result == NULL) {
1013 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
1014 name, GST_ELEMENT_NAME (element));
1015 } else {
1016 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1017 GST_ELEMENT_NAME (element), name);
1018 }
1019 GST_OBJECT_UNLOCK (element);
1020
1021 return result;
1022 }
1023
1024 static gboolean
gst_element_is_valid_request_template_name(const gchar * templ_name,const gchar * name)1025 gst_element_is_valid_request_template_name (const gchar * templ_name,
1026 const gchar * name)
1027 {
1028 gchar *endptr;
1029 const gchar *templ_name_ptr, *name_ptr;
1030 gboolean next_specifier;
1031 guint templ_postfix_len = 0, name_postfix_len = 0;
1032
1033 g_return_val_if_fail (templ_name != NULL, FALSE);
1034 g_return_val_if_fail (name != NULL, FALSE);
1035
1036 /* Is this the template name? */
1037 if (strcmp (templ_name, name) == 0)
1038 return TRUE;
1039
1040 /* otherwise check all the specifiers */
1041 do {
1042 /* Because of sanity checks in gst_pad_template_new(), we know that %s
1043 * and %d and %u, occurring at the template_name */
1044 templ_name_ptr = strchr (templ_name, '%');
1045
1046 /* check characters ahead of the specifier */
1047 if (!templ_name_ptr || strlen (name) <= templ_name_ptr - templ_name
1048 || strncmp (templ_name, name, templ_name_ptr - templ_name) != 0) {
1049 return FALSE;
1050 }
1051
1052 /* %s is not allowed for multiple specifiers, just a single specifier can be
1053 * accepted in gst_pad_template_new() and can not be mixed with other
1054 * specifier '%u' and '%d' */
1055 if (*(templ_name_ptr + 1) == 's' && g_strcmp0 (templ_name, name) == 0) {
1056 return TRUE;
1057 }
1058
1059 name_ptr = name + (templ_name_ptr - templ_name);
1060
1061 /* search next specifier, each of specifier should be separated by '_' */
1062 templ_name = strchr (templ_name_ptr, '_');
1063 name = strchr (name_ptr, '_');
1064
1065 /* don't match the number of specifiers */
1066 if ((templ_name && !name) || (!templ_name && name))
1067 return FALSE;
1068
1069 if (templ_name && name)
1070 next_specifier = TRUE;
1071 else
1072 next_specifier = FALSE;
1073
1074 /* check characters followed by the specifier */
1075 if (*(templ_name_ptr + 2) != '\0' && *(templ_name_ptr + 2) != '_') {
1076 if (next_specifier) {
1077 templ_postfix_len = templ_name - (templ_name_ptr + 2);
1078 name_postfix_len = name - name_ptr;
1079 } else {
1080 templ_postfix_len = strlen (templ_name_ptr + 2);
1081 name_postfix_len = strlen (name_ptr);
1082 }
1083
1084 if (strncmp (templ_name_ptr + 2,
1085 name_ptr + name_postfix_len - templ_postfix_len,
1086 templ_postfix_len) != 0) {
1087 return FALSE;
1088 }
1089 }
1090
1091 /* verify the specifier */
1092 if (*(name_ptr) == '%') {
1093 guint len;
1094
1095 len = (next_specifier) ? name - name_ptr : strlen (name_ptr);
1096
1097 if (strncmp (name_ptr, templ_name_ptr, len) != 0)
1098 return FALSE;
1099
1100 } else {
1101 const gchar *specifier;
1102 gchar *target = NULL;
1103
1104 /* extract specifier when it has postfix characters */
1105 if (name_postfix_len > templ_postfix_len) {
1106 target = g_strndup (name_ptr, name_postfix_len - templ_postfix_len);
1107 }
1108 specifier = target ? target : name_ptr;
1109
1110 if (*(templ_name_ptr + 1) == 'd') {
1111 gint64 tmp;
1112
1113 /* it's an int */
1114 tmp = g_ascii_strtoll (specifier, &endptr, 10);
1115 if (tmp < G_MININT || tmp > G_MAXINT || (*endptr != '\0'
1116 && *endptr != '_'))
1117 return FALSE;
1118 } else if (*(templ_name_ptr + 1) == 'u') {
1119 guint64 tmp;
1120
1121 /* it's an int */
1122 tmp = g_ascii_strtoull (specifier, &endptr, 10);
1123 if (tmp > G_MAXUINT || (*endptr != '\0' && *endptr != '_'))
1124 return FALSE;
1125 }
1126
1127 g_free (target);
1128 }
1129
1130 /* otherwise we increment these from NULL to 1 */
1131 if (next_specifier) {
1132 templ_name++;
1133 name++;
1134 }
1135 } while (next_specifier);
1136
1137 return TRUE;
1138 }
1139
1140 static GstPad *
_gst_element_request_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)1141 _gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
1142 const gchar * name, const GstCaps * caps)
1143 {
1144 GstPad *newpad = NULL;
1145 GstElementClass *oclass;
1146
1147 oclass = GST_ELEMENT_GET_CLASS (element);
1148
1149 #ifndef G_DISABLE_CHECKS
1150 /* Some sanity checking here */
1151 if (name) {
1152 GstPad *pad;
1153
1154 g_return_val_if_fail (gst_element_is_valid_request_template_name
1155 (templ->name_template, name), NULL);
1156
1157 pad = gst_element_get_static_pad (element, name);
1158 if (pad) {
1159 gst_object_unref (pad);
1160 /* FIXME 2.0: Change this to g_return_val_if_fail() */
1161 g_critical ("Element %s already has a pad named %s, the behaviour of "
1162 " gst_element_get_request_pad() for existing pads is undefined!",
1163 GST_ELEMENT_NAME (element), name);
1164 }
1165 }
1166 #endif
1167
1168 if (oclass->request_new_pad)
1169 newpad = (oclass->request_new_pad) (element, templ, name, caps);
1170
1171 if (newpad)
1172 gst_object_ref (newpad);
1173
1174 return newpad;
1175 }
1176
1177 #ifndef GST_REMOVE_DEPRECATED
1178 /**
1179 * gst_element_get_request_pad:
1180 * @element: a #GstElement to find a request pad of.
1181 * @name: the name of the request #GstPad to retrieve.
1182 *
1183 * The name of this function is confusing to people learning GStreamer.
1184 * gst_element_request_pad_simple() aims at making it more explicit it is
1185 * a simplified gst_element_request_pad().
1186 *
1187 * Deprecated: 1.20: Prefer using gst_element_request_pad_simple() which
1188 * provides the exact same functionality.
1189 *
1190 * Returns: (transfer full) (nullable): requested #GstPad if found,
1191 * otherwise %NULL. Release after usage.
1192 */
1193 GstPad *
gst_element_get_request_pad(GstElement * element,const gchar * name)1194 gst_element_get_request_pad (GstElement * element, const gchar * name)
1195 {
1196 return gst_element_request_pad_simple (element, name);
1197 }
1198 #endif
1199
1200 /**
1201 * gst_element_request_pad_simple:
1202 * @element: a #GstElement to find a request pad of.
1203 * @name: the name of the request #GstPad to retrieve.
1204 *
1205 * Retrieves a pad from the element by name (e.g. "src_\%d"). This version only
1206 * retrieves request pads. The pad should be released with
1207 * gst_element_release_request_pad().
1208 *
1209 * This method is slower than manually getting the pad template and calling
1210 * gst_element_request_pad() if the pads should have a specific name (e.g.
1211 * @name is "src_1" instead of "src_\%u").
1212 *
1213 * Note that this function was introduced in GStreamer 1.20 in order to provide
1214 * a better name to gst_element_get_request_pad(). Prior to 1.20, users
1215 * should use gst_element_get_request_pad() which provides the same
1216 * functionality.
1217 *
1218 * Returns: (transfer full) (nullable): requested #GstPad if found,
1219 * otherwise %NULL. Release after usage.
1220 *
1221 * Since: 1.20
1222 */
1223 GstPad *
gst_element_request_pad_simple(GstElement * element,const gchar * name)1224 gst_element_request_pad_simple (GstElement * element, const gchar * name)
1225 {
1226 GstPadTemplate *templ = NULL;
1227 GstPad *pad;
1228 const gchar *req_name = NULL;
1229 gboolean templ_found = FALSE;
1230 GList *list;
1231 GstElementClass *class;
1232
1233 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1234 g_return_val_if_fail (name != NULL, NULL);
1235
1236 class = GST_ELEMENT_GET_CLASS (element);
1237
1238 templ = gst_element_class_request_pad_simple_template (class, name);
1239 if (templ) {
1240 req_name = strstr (name, "%") ? NULL : name;
1241 templ_found = TRUE;
1242 } else {
1243 /* there is no % in the name, try to find a matching template */
1244 list = class->padtemplates;
1245 while (!templ_found && list) {
1246 templ = (GstPadTemplate *) list->data;
1247 if (templ->presence == GST_PAD_REQUEST) {
1248 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
1249 templ->name_template);
1250 if (gst_element_is_valid_request_template_name (templ->name_template,
1251 name)) {
1252 templ_found = TRUE;
1253 req_name = name;
1254 break;
1255 }
1256 }
1257 list = list->next;
1258 }
1259 }
1260
1261 if (!templ_found)
1262 return NULL;
1263
1264 pad = _gst_element_request_pad (element, templ, req_name, NULL);
1265
1266 return pad;
1267 }
1268
1269 /**
1270 * gst_element_request_pad: (virtual request_new_pad)
1271 * @element: a #GstElement to find a request pad of.
1272 * @templ: a #GstPadTemplate of which we want a pad of.
1273 * @name: (transfer none) (allow-none): the name of the request #GstPad
1274 * to retrieve. Can be %NULL.
1275 * @caps: (transfer none) (allow-none): the caps of the pad we want to
1276 * request. Can be %NULL.
1277 *
1278 * Retrieves a request pad from the element according to the provided template.
1279 * Pad templates can be looked up using
1280 * gst_element_factory_get_static_pad_templates().
1281 *
1282 * The pad should be released with gst_element_release_request_pad().
1283 *
1284 * Returns: (transfer full) (nullable): requested #GstPad if found,
1285 * otherwise %NULL. Release after usage.
1286 */
1287 GstPad *
gst_element_request_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)1288 gst_element_request_pad (GstElement * element,
1289 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1290 {
1291 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1292 g_return_val_if_fail (templ != NULL, NULL);
1293 g_return_val_if_fail (templ->presence == GST_PAD_REQUEST, NULL);
1294
1295 return _gst_element_request_pad (element, templ, name, caps);
1296 }
1297
1298 static GstIterator *
gst_element_iterate_pad_list(GstElement * element,GList ** padlist)1299 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1300 {
1301 GstIterator *result;
1302
1303 GST_OBJECT_LOCK (element);
1304 result = gst_iterator_new_list (GST_TYPE_PAD,
1305 GST_OBJECT_GET_LOCK (element),
1306 &element->pads_cookie, padlist, (GObject *) element, NULL);
1307 GST_OBJECT_UNLOCK (element);
1308
1309 return result;
1310 }
1311
1312 /**
1313 * gst_element_iterate_pads:
1314 * @element: a #GstElement to iterate pads of.
1315 *
1316 * Retrieves an iterator of @element's pads. The iterator should
1317 * be freed after usage. Also more specialized iterators exists such as
1318 * gst_element_iterate_src_pads() or gst_element_iterate_sink_pads().
1319 *
1320 * The order of pads returned by the iterator will be the order in which
1321 * the pads were added to the element.
1322 *
1323 * Returns: (transfer full): the #GstIterator of #GstPad.
1324 *
1325 * MT safe.
1326 */
1327 GstIterator *
gst_element_iterate_pads(GstElement * element)1328 gst_element_iterate_pads (GstElement * element)
1329 {
1330 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1331
1332 return gst_element_iterate_pad_list (element, &element->pads);
1333 }
1334
1335 /**
1336 * gst_element_iterate_src_pads:
1337 * @element: a #GstElement.
1338 *
1339 * Retrieves an iterator of @element's source pads.
1340 *
1341 * The order of pads returned by the iterator will be the order in which
1342 * the pads were added to the element.
1343 *
1344 * Returns: (transfer full): the #GstIterator of #GstPad.
1345 *
1346 * MT safe.
1347 */
1348 GstIterator *
gst_element_iterate_src_pads(GstElement * element)1349 gst_element_iterate_src_pads (GstElement * element)
1350 {
1351 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1352
1353 return gst_element_iterate_pad_list (element, &element->srcpads);
1354 }
1355
1356 /**
1357 * gst_element_iterate_sink_pads:
1358 * @element: a #GstElement.
1359 *
1360 * Retrieves an iterator of @element's sink pads.
1361 *
1362 * The order of pads returned by the iterator will be the order in which
1363 * the pads were added to the element.
1364 *
1365 * Returns: (transfer full): the #GstIterator of #GstPad.
1366 *
1367 * MT safe.
1368 */
1369 GstIterator *
gst_element_iterate_sink_pads(GstElement * element)1370 gst_element_iterate_sink_pads (GstElement * element)
1371 {
1372 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1373
1374 return gst_element_iterate_pad_list (element, &element->sinkpads);
1375 }
1376
1377 static gboolean
gst_element_do_foreach_pad(GstElement * element,GstElementForeachPadFunc func,gpointer user_data,GList ** p_pads,guint16 * p_npads)1378 gst_element_do_foreach_pad (GstElement * element,
1379 GstElementForeachPadFunc func, gpointer user_data,
1380 GList ** p_pads, guint16 * p_npads)
1381 {
1382 gboolean ret = TRUE;
1383 GstPad **pads;
1384 guint n_pads, i;
1385 GList *l;
1386
1387 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1388 g_return_val_if_fail (func != NULL, FALSE);
1389
1390 GST_OBJECT_LOCK (element);
1391 n_pads = *p_npads;
1392 pads = g_newa (GstPad *, n_pads + 1);
1393 for (l = *p_pads, i = 0; l != NULL; l = l->next) {
1394 g_assert (i < n_pads);
1395 pads[i++] = gst_object_ref (l->data);
1396 }
1397 GST_OBJECT_UNLOCK (element);
1398
1399 if (n_pads == 0)
1400 return FALSE;
1401
1402 for (i = 0; i < n_pads; ++i) {
1403 ret = func (element, pads[i], user_data);
1404 if (!ret)
1405 break;
1406 }
1407
1408 for (i = 0; i < n_pads; ++i)
1409 gst_object_unref (pads[i]);
1410
1411 return ret;
1412 }
1413
1414 /**
1415 * gst_element_foreach_sink_pad:
1416 * @element: a #GstElement to iterate sink pads of
1417 * @func: (scope call): function to call for each sink pad
1418 * @user_data: (closure): user data passed to @func
1419 *
1420 * Call @func with @user_data for each of @element's sink pads. @func will be
1421 * called exactly once for each sink pad that exists at the time of this call,
1422 * unless one of the calls to @func returns %FALSE in which case we will stop
1423 * iterating pads and return early. If new sink pads are added or sink pads
1424 * are removed while the sink pads are being iterated, this will not be taken
1425 * into account until next time this function is used.
1426 *
1427 * Returns: %FALSE if @element had no sink pads or if one of the calls to @func
1428 * returned %FALSE.
1429 *
1430 * Since: 1.14
1431 */
1432 gboolean
gst_element_foreach_sink_pad(GstElement * element,GstElementForeachPadFunc func,gpointer user_data)1433 gst_element_foreach_sink_pad (GstElement * element,
1434 GstElementForeachPadFunc func, gpointer user_data)
1435 {
1436 return gst_element_do_foreach_pad (element, func, user_data,
1437 &element->sinkpads, &element->numsinkpads);
1438 }
1439
1440 /**
1441 * gst_element_foreach_src_pad:
1442 * @element: a #GstElement to iterate source pads of
1443 * @func: (scope call): function to call for each source pad
1444 * @user_data: (closure): user data passed to @func
1445 *
1446 * Call @func with @user_data for each of @element's source pads. @func will be
1447 * called exactly once for each source pad that exists at the time of this call,
1448 * unless one of the calls to @func returns %FALSE in which case we will stop
1449 * iterating pads and return early. If new source pads are added or source pads
1450 * are removed while the source pads are being iterated, this will not be taken
1451 * into account until next time this function is used.
1452 *
1453 * Returns: %FALSE if @element had no source pads or if one of the calls
1454 * to @func returned %FALSE.
1455 *
1456 * Since: 1.14
1457 */
1458 gboolean
gst_element_foreach_src_pad(GstElement * element,GstElementForeachPadFunc func,gpointer user_data)1459 gst_element_foreach_src_pad (GstElement * element,
1460 GstElementForeachPadFunc func, gpointer user_data)
1461 {
1462 return gst_element_do_foreach_pad (element, func, user_data,
1463 &element->srcpads, &element->numsrcpads);
1464 }
1465
1466 /**
1467 * gst_element_foreach_pad:
1468 * @element: a #GstElement to iterate pads of
1469 * @func: (scope call): function to call for each pad
1470 * @user_data: (closure): user data passed to @func
1471 *
1472 * Call @func with @user_data for each of @element's pads. @func will be called
1473 * exactly once for each pad that exists at the time of this call, unless
1474 * one of the calls to @func returns %FALSE in which case we will stop
1475 * iterating pads and return early. If new pads are added or pads are removed
1476 * while pads are being iterated, this will not be taken into account until
1477 * next time this function is used.
1478 *
1479 * Returns: %FALSE if @element had no pads or if one of the calls to @func
1480 * returned %FALSE.
1481 *
1482 * Since: 1.14
1483 */
1484 gboolean
gst_element_foreach_pad(GstElement * element,GstElementForeachPadFunc func,gpointer user_data)1485 gst_element_foreach_pad (GstElement * element, GstElementForeachPadFunc func,
1486 gpointer user_data)
1487 {
1488 return gst_element_do_foreach_pad (element, func, user_data,
1489 &element->pads, &element->numpads);
1490 }
1491
1492 /**
1493 * gst_element_class_add_pad_template:
1494 * @klass: the #GstElementClass to add the pad template to.
1495 * @templ: (transfer floating): a #GstPadTemplate to add to the element class.
1496 *
1497 * Adds a padtemplate to an element class. This is mainly used in the _class_init
1498 * functions of classes. If a pad template with the same name as an already
1499 * existing one is added the old one is replaced by the new one.
1500 *
1501 * @templ's reference count will be incremented, and any floating
1502 * reference will be removed (see gst_object_ref_sink())
1503 *
1504 */
1505 void
gst_element_class_add_pad_template(GstElementClass * klass,GstPadTemplate * templ)1506 gst_element_class_add_pad_template (GstElementClass * klass,
1507 GstPadTemplate * templ)
1508 {
1509 GList *template_list = klass->padtemplates;
1510
1511 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1512 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1513
1514 /* If we already have a pad template with the same name replace the
1515 * old one. */
1516 while (template_list) {
1517 GstPadTemplate *padtempl = (GstPadTemplate *) template_list->data;
1518
1519 /* Found pad with the same name, replace and return */
1520 if (strcmp (templ->name_template, padtempl->name_template) == 0) {
1521 gst_object_ref_sink (padtempl);
1522 gst_object_unref (padtempl);
1523 template_list->data = templ;
1524 return;
1525 }
1526 template_list = g_list_next (template_list);
1527 }
1528
1529 /* Take ownership of the floating ref */
1530 gst_object_ref_sink (templ);
1531
1532 klass->padtemplates = g_list_append (klass->padtemplates, templ);
1533 klass->numpadtemplates++;
1534 }
1535
1536 /**
1537 * gst_element_class_add_static_pad_template:
1538 * @klass: the #GstElementClass to add the pad template to.
1539 * @static_templ: #GstStaticPadTemplate to add as pad template to the element class.
1540 *
1541 * Adds a pad template to an element class based on the static pad template
1542 * @templ. This is mainly used in the _class_init functions of element
1543 * implementations. If a pad template with the same name already exists,
1544 * the old one is replaced by the new one.
1545 *
1546 * Since: 1.8
1547 */
1548 void
gst_element_class_add_static_pad_template(GstElementClass * klass,GstStaticPadTemplate * static_templ)1549 gst_element_class_add_static_pad_template (GstElementClass * klass,
1550 GstStaticPadTemplate * static_templ)
1551 {
1552 gst_element_class_add_pad_template (klass,
1553 gst_static_pad_template_get (static_templ));
1554 }
1555
1556 /**
1557 * gst_element_class_add_static_pad_template_with_gtype:
1558 * @klass: the #GstElementClass to add the pad template to.
1559 * @static_templ: #GstStaticPadTemplate to add as pad template to the element class.
1560 * @pad_type: The #GType of the pad to create
1561 *
1562 * Adds a pad template to an element class based on the static pad template
1563 * @templ. This is mainly used in the _class_init functions of element
1564 * implementations. If a pad template with the same name already exists,
1565 * the old one is replaced by the new one.
1566 *
1567 * Since: 1.14
1568 */
1569 void
gst_element_class_add_static_pad_template_with_gtype(GstElementClass * klass,GstStaticPadTemplate * static_templ,GType pad_type)1570 gst_element_class_add_static_pad_template_with_gtype (GstElementClass * klass,
1571 GstStaticPadTemplate * static_templ, GType pad_type)
1572 {
1573 gst_element_class_add_pad_template (klass,
1574 gst_pad_template_new_from_static_pad_template_with_gtype (static_templ,
1575 pad_type));
1576 }
1577
1578 /**
1579 * gst_element_class_add_metadata:
1580 * @klass: class to set metadata for
1581 * @key: the key to set
1582 * @value: the value to set
1583 *
1584 * Set @key with @value as metadata in @klass.
1585 */
1586 void
gst_element_class_add_metadata(GstElementClass * klass,const gchar * key,const gchar * value)1587 gst_element_class_add_metadata (GstElementClass * klass,
1588 const gchar * key, const gchar * value)
1589 {
1590 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1591 g_return_if_fail (key != NULL);
1592 g_return_if_fail (value != NULL);
1593
1594 gst_structure_set ((GstStructure *) klass->metadata,
1595 key, G_TYPE_STRING, value, NULL);
1596 }
1597
1598 /**
1599 * gst_element_class_add_static_metadata:
1600 * @klass: class to set metadata for
1601 * @key: the key to set
1602 * @value: the value to set
1603 *
1604 * Set @key with @value as metadata in @klass.
1605 *
1606 * Same as gst_element_class_add_metadata(), but @value must be a static string
1607 * or an inlined string, as it will not be copied. (GStreamer plugins will
1608 * be made resident once loaded, so this function can be used even from
1609 * dynamically loaded plugins.)
1610 */
1611 void
gst_element_class_add_static_metadata(GstElementClass * klass,const gchar * key,const gchar * value)1612 gst_element_class_add_static_metadata (GstElementClass * klass,
1613 const gchar * key, const gchar * value)
1614 {
1615 GValue val = G_VALUE_INIT;
1616
1617 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1618 g_return_if_fail (key != NULL);
1619 g_return_if_fail (value != NULL);
1620
1621 g_value_init (&val, G_TYPE_STRING);
1622 g_value_set_static_string (&val, value);
1623 gst_structure_take_value ((GstStructure *) klass->metadata, key, &val);
1624 }
1625
1626 /**
1627 * gst_element_class_set_metadata:
1628 * @klass: class to set metadata for
1629 * @longname: The long English name of the element. E.g. "File Sink"
1630 * @classification: String describing the type of element, as an unordered list
1631 * separated with slashes ('/'). See draft-klass.txt of the design docs
1632 * for more details and common types. E.g: "Sink/File"
1633 * @description: Sentence describing the purpose of the element.
1634 * E.g: "Write stream to a file"
1635 * @author: Name and contact details of the author(s). Use \n to separate
1636 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1637 *
1638 * Sets the detailed information for a #GstElementClass.
1639 * > This function is for use in _class_init functions only.
1640 */
1641 void
gst_element_class_set_metadata(GstElementClass * klass,const gchar * longname,const gchar * classification,const gchar * description,const gchar * author)1642 gst_element_class_set_metadata (GstElementClass * klass,
1643 const gchar * longname, const gchar * classification,
1644 const gchar * description, const gchar * author)
1645 {
1646 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1647 g_return_if_fail (longname != NULL && *longname != '\0');
1648 g_return_if_fail (classification != NULL && *classification != '\0');
1649 g_return_if_fail (description != NULL && *description != '\0');
1650 g_return_if_fail (author != NULL && *author != '\0');
1651
1652 gst_structure_id_set ((GstStructure *) klass->metadata,
1653 GST_QUARK (ELEMENT_METADATA_LONGNAME), G_TYPE_STRING, longname,
1654 GST_QUARK (ELEMENT_METADATA_KLASS), G_TYPE_STRING, classification,
1655 GST_QUARK (ELEMENT_METADATA_DESCRIPTION), G_TYPE_STRING, description,
1656 GST_QUARK (ELEMENT_METADATA_AUTHOR), G_TYPE_STRING, author, NULL);
1657 }
1658
1659 /**
1660 * gst_element_class_set_static_metadata:
1661 * @klass: class to set metadata for
1662 * @longname: The long English name of the element. E.g. "File Sink"
1663 * @classification: String describing the type of element, as an unordered list
1664 * separated with slashes ('/'). See draft-klass.txt of the design docs
1665 * for more details and common types. E.g: "Sink/File"
1666 * @description: Sentence describing the purpose of the element.
1667 * E.g: "Write stream to a file"
1668 * @author: Name and contact details of the author(s). Use \n to separate
1669 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1670 *
1671 * Sets the detailed information for a #GstElementClass.
1672 *
1673 * > This function is for use in _class_init functions only.
1674 *
1675 * Same as gst_element_class_set_metadata(), but @longname, @classification,
1676 * @description, and @author must be static strings or inlined strings, as
1677 * they will not be copied. (GStreamer plugins will be made resident once
1678 * loaded, so this function can be used even from dynamically loaded plugins.)
1679 */
1680 void
gst_element_class_set_static_metadata(GstElementClass * klass,const gchar * longname,const gchar * classification,const gchar * description,const gchar * author)1681 gst_element_class_set_static_metadata (GstElementClass * klass,
1682 const gchar * longname, const gchar * classification,
1683 const gchar * description, const gchar * author)
1684 {
1685 GstStructure *s = (GstStructure *) klass->metadata;
1686 GValue val = G_VALUE_INIT;
1687
1688 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1689 g_return_if_fail (longname != NULL && *longname != '\0');
1690 g_return_if_fail (classification != NULL && *classification != '\0');
1691 g_return_if_fail (description != NULL && *description != '\0');
1692 g_return_if_fail (author != NULL && *author != '\0');
1693
1694 g_value_init (&val, G_TYPE_STRING);
1695
1696 g_value_set_static_string (&val, longname);
1697 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_LONGNAME), &val);
1698
1699 g_value_set_static_string (&val, classification);
1700 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_KLASS), &val);
1701
1702 g_value_set_static_string (&val, description);
1703 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_DESCRIPTION),
1704 &val);
1705
1706 g_value_set_static_string (&val, author);
1707 gst_structure_id_take_value (s, GST_QUARK (ELEMENT_METADATA_AUTHOR), &val);
1708 }
1709
1710 /**
1711 * gst_element_class_get_metadata:
1712 * @klass: class to get metadata for
1713 * @key: the key to get
1714 *
1715 * Get metadata with @key in @klass.
1716 *
1717 * Returns: the metadata for @key.
1718 */
1719 const gchar *
gst_element_class_get_metadata(GstElementClass * klass,const gchar * key)1720 gst_element_class_get_metadata (GstElementClass * klass, const gchar * key)
1721 {
1722 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (klass), NULL);
1723 g_return_val_if_fail (key != NULL, NULL);
1724
1725 return gst_structure_get_string ((GstStructure *) klass->metadata, key);
1726 }
1727
1728 /**
1729 * gst_element_get_metadata:
1730 * @element: class to get metadata for
1731 * @key: the key to get
1732 *
1733 * Get metadata with @key in @klass.
1734 *
1735 * Returns: the metadata for @key.
1736 *
1737 * Since: 1.14
1738 */
1739 const gchar *
gst_element_get_metadata(GstElement * element,const gchar * key)1740 gst_element_get_metadata (GstElement * element, const gchar * key)
1741 {
1742 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1743 g_return_val_if_fail (key != NULL, NULL);
1744
1745 return gst_element_class_get_metadata (GST_ELEMENT_GET_CLASS (element), key);
1746 }
1747
1748 /**
1749 * gst_element_class_get_pad_template_list:
1750 * @element_class: a #GstElementClass to get pad templates of.
1751 *
1752 * Retrieves a list of the pad templates associated with @element_class. The
1753 * list must not be modified by the calling code.
1754 * > If you use this function in the #GInstanceInitFunc of an object class
1755 * > that has subclasses, make sure to pass the g_class parameter of the
1756 * > #GInstanceInitFunc here.
1757 *
1758 * Returns: (transfer none) (element-type Gst.PadTemplate): the #GList of
1759 * pad templates.
1760 */
1761 GList *
gst_element_class_get_pad_template_list(GstElementClass * element_class)1762 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1763 {
1764 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1765
1766 return element_class->padtemplates;
1767 }
1768
1769 /**
1770 * gst_element_get_pad_template_list:
1771 * @element: a #GstElement to get pad templates of.
1772 *
1773 * Retrieves a list of the pad templates associated with @element. The
1774 * list must not be modified by the calling code.
1775 *
1776 * Returns: (transfer none) (element-type Gst.PadTemplate): the #GList of
1777 * pad templates.
1778 *
1779 * Since: 1.14
1780 */
1781 GList *
gst_element_get_pad_template_list(GstElement * element)1782 gst_element_get_pad_template_list (GstElement * element)
1783 {
1784 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1785
1786 return
1787 gst_element_class_get_pad_template_list (GST_ELEMENT_GET_CLASS (element));
1788 }
1789
1790 /**
1791 * gst_element_class_get_pad_template:
1792 * @element_class: a #GstElementClass to get the pad template of.
1793 * @name: the name of the #GstPadTemplate to get.
1794 *
1795 * Retrieves a padtemplate from @element_class with the given name.
1796 * > If you use this function in the #GInstanceInitFunc of an object class
1797 * > that has subclasses, make sure to pass the g_class parameter of the
1798 * > #GInstanceInitFunc here.
1799 *
1800 * Returns: (transfer none) (nullable): the #GstPadTemplate with the
1801 * given name, or %NULL if none was found. No unreferencing is
1802 * necessary.
1803 */
1804 GstPadTemplate *
gst_element_class_get_pad_template(GstElementClass * element_class,const gchar * name)1805 gst_element_class_get_pad_template (GstElementClass *
1806 element_class, const gchar * name)
1807 {
1808 GList *padlist;
1809
1810 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1811 g_return_val_if_fail (name != NULL, NULL);
1812
1813 padlist = element_class->padtemplates;
1814
1815 while (padlist) {
1816 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1817
1818 if (strcmp (padtempl->name_template, name) == 0)
1819 return padtempl;
1820
1821 padlist = g_list_next (padlist);
1822 }
1823
1824 return NULL;
1825 }
1826
1827 /**
1828 * gst_element_get_pad_template:
1829 * @element: a #GstElement to get the pad template of.
1830 * @name: the name of the #GstPadTemplate to get.
1831 *
1832 * Retrieves a padtemplate from @element with the given name.
1833 *
1834 * Returns: (transfer none) (nullable): the #GstPadTemplate with the
1835 * given name, or %NULL if none was found. No unreferencing is
1836 * necessary.
1837 *
1838 * Since: 1.14
1839 */
1840 GstPadTemplate *
gst_element_get_pad_template(GstElement * element,const gchar * name)1841 gst_element_get_pad_template (GstElement * element, const gchar * name)
1842 {
1843 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1844 g_return_val_if_fail (name != NULL, NULL);
1845
1846 return gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
1847 name);
1848 }
1849
1850 static GstPadTemplate *
gst_element_class_request_pad_simple_template(GstElementClass * element_class,const gchar * name)1851 gst_element_class_request_pad_simple_template (GstElementClass *
1852 element_class, const gchar * name)
1853 {
1854 GstPadTemplate *tmpl;
1855
1856 tmpl = gst_element_class_get_pad_template (element_class, name);
1857 if (tmpl != NULL && tmpl->presence == GST_PAD_REQUEST)
1858 return tmpl;
1859
1860 return NULL;
1861 }
1862
1863 /* get a random pad on element of the given direction.
1864 * The pad is random in a sense that it is the first pad that is (optionally) linked.
1865 */
1866 static GstPad *
gst_element_get_random_pad(GstElement * element,gboolean need_linked,GstPadDirection dir)1867 gst_element_get_random_pad (GstElement * element,
1868 gboolean need_linked, GstPadDirection dir)
1869 {
1870 GstPad *result = NULL;
1871 GList *pads;
1872
1873 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1874
1875 switch (dir) {
1876 case GST_PAD_SRC:
1877 GST_OBJECT_LOCK (element);
1878 pads = element->srcpads;
1879 break;
1880 case GST_PAD_SINK:
1881 GST_OBJECT_LOCK (element);
1882 pads = element->sinkpads;
1883 break;
1884 default:
1885 goto wrong_direction;
1886 }
1887 for (; pads; pads = g_list_next (pads)) {
1888 GstPad *pad = GST_PAD_CAST (pads->data);
1889
1890 GST_OBJECT_LOCK (pad);
1891 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1892 GST_DEBUG_PAD_NAME (pad));
1893
1894 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1895 /* if we require a linked pad, and it is not linked, continue the
1896 * search */
1897 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1898 GST_DEBUG_PAD_NAME (pad));
1899 GST_OBJECT_UNLOCK (pad);
1900 continue;
1901 } else {
1902 /* found a pad, stop search */
1903 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1904 GST_DEBUG_PAD_NAME (pad));
1905 GST_OBJECT_UNLOCK (pad);
1906 result = pad;
1907 break;
1908 }
1909 }
1910 if (result)
1911 gst_object_ref (result);
1912
1913 GST_OBJECT_UNLOCK (element);
1914
1915 return result;
1916
1917 /* ERROR handling */
1918 wrong_direction:
1919 {
1920 g_warning ("unknown pad direction %d", dir);
1921 return NULL;
1922 }
1923 }
1924
1925 static gboolean
gst_element_default_send_event(GstElement * element,GstEvent * event)1926 gst_element_default_send_event (GstElement * element, GstEvent * event)
1927 {
1928 gboolean result = FALSE;
1929 GstPad *pad;
1930
1931 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1932 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK) :
1933 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC);
1934
1935 if (pad) {
1936 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1937 "pushing %s event to random %s pad %s:%s",
1938 GST_EVENT_TYPE_NAME (event),
1939 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1940 GST_DEBUG_PAD_NAME (pad));
1941
1942 result = gst_pad_send_event (pad, event);
1943 gst_object_unref (pad);
1944 } else {
1945 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1946 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1947 gst_event_unref (event);
1948 }
1949 return result;
1950 }
1951
1952 /**
1953 * gst_element_send_event:
1954 * @element: a #GstElement to send the event to.
1955 * @event: (transfer full): the #GstEvent to send to the element.
1956 *
1957 * Sends an event to an element. If the element doesn't implement an
1958 * event handler, the event will be pushed on a random linked sink pad for
1959 * downstream events or a random linked source pad for upstream events.
1960 *
1961 * This function takes ownership of the provided event so you should
1962 * gst_event_ref() it if you want to reuse the event after this call.
1963 *
1964 * MT safe.
1965 *
1966 * Returns: %TRUE if the event was handled. Events that trigger a preroll (such
1967 * as flushing seeks and steps) will emit %GST_MESSAGE_ASYNC_DONE.
1968 */
1969 gboolean
gst_element_send_event(GstElement * element,GstEvent * event)1970 gst_element_send_event (GstElement * element, GstEvent * event)
1971 {
1972 GstElementClass *oclass;
1973 gboolean result = FALSE;
1974
1975 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1976 g_return_val_if_fail (event != NULL, FALSE);
1977
1978 oclass = GST_ELEMENT_GET_CLASS (element);
1979
1980 GST_STATE_LOCK (element);
1981 if (oclass->send_event) {
1982 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1983 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1984 result = oclass->send_event (element, event);
1985 } else {
1986 gst_event_unref (event);
1987 }
1988 GST_STATE_UNLOCK (element);
1989
1990 return result;
1991 }
1992
1993 /**
1994 * gst_element_seek:
1995 * @element: a #GstElement to send the event to.
1996 * @rate: The new playback rate
1997 * @format: The format of the seek values
1998 * @flags: The optional seek flags.
1999 * @start_type: The type and flags for the new start position
2000 * @start: The value of the new start position
2001 * @stop_type: The type and flags for the new stop position
2002 * @stop: The value of the new stop position
2003 *
2004 * Sends a seek event to an element. See gst_event_new_seek() for the details of
2005 * the parameters. The seek event is sent to the element using
2006 * gst_element_send_event().
2007 *
2008 * MT safe.
2009 *
2010 * Returns: %TRUE if the event was handled. Flushing seeks will trigger a
2011 * preroll, which will emit %GST_MESSAGE_ASYNC_DONE.
2012 */
2013 gboolean
gst_element_seek(GstElement * element,gdouble rate,GstFormat format,GstSeekFlags flags,GstSeekType start_type,gint64 start,GstSeekType stop_type,gint64 stop)2014 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
2015 GstSeekFlags flags, GstSeekType start_type, gint64 start,
2016 GstSeekType stop_type, gint64 stop)
2017 {
2018 GstEvent *event;
2019 gboolean result;
2020
2021 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2022
2023 event =
2024 gst_event_new_seek (rate, format, flags, start_type, start, stop_type,
2025 stop);
2026 result = gst_element_send_event (element, event);
2027
2028 return result;
2029 }
2030
2031 static gboolean
gst_element_default_query(GstElement * element,GstQuery * query)2032 gst_element_default_query (GstElement * element, GstQuery * query)
2033 {
2034 gboolean result = FALSE;
2035 GstPad *pad;
2036
2037 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
2038 if (pad) {
2039 result = gst_pad_query (pad, query);
2040
2041 gst_object_unref (pad);
2042 } else {
2043 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
2044 if (pad) {
2045 GstPad *peer = gst_pad_get_peer (pad);
2046
2047 if (peer) {
2048 result = gst_pad_query (peer, query);
2049
2050 gst_object_unref (peer);
2051 }
2052 gst_object_unref (pad);
2053 }
2054 }
2055 return result;
2056 }
2057
2058 /**
2059 * gst_element_query:
2060 * @element: a #GstElement to perform the query on.
2061 * @query: (transfer none): the #GstQuery.
2062 *
2063 * Performs a query on the given element.
2064 *
2065 * For elements that don't implement a query handler, this function
2066 * forwards the query to a random srcpad or to the peer of a
2067 * random linked sinkpad of this element.
2068 *
2069 * Please note that some queries might need a running pipeline to work.
2070 *
2071 * Returns: %TRUE if the query could be performed.
2072 *
2073 * MT safe.
2074 */
2075 gboolean
gst_element_query(GstElement * element,GstQuery * query)2076 gst_element_query (GstElement * element, GstQuery * query)
2077 {
2078 GstElementClass *klass;
2079 gboolean res = FALSE;
2080
2081 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2082 g_return_val_if_fail (query != NULL, FALSE);
2083
2084 GST_TRACER_ELEMENT_QUERY_PRE (element, query);
2085
2086 klass = GST_ELEMENT_GET_CLASS (element);
2087 if (klass->query) {
2088 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
2089 GST_ELEMENT_NAME (element));
2090 res = klass->query (element, query);
2091 }
2092
2093 GST_TRACER_ELEMENT_QUERY_POST (element, query, res);
2094 return res;
2095 }
2096
2097 static gboolean
gst_element_post_message_default(GstElement * element,GstMessage * message)2098 gst_element_post_message_default (GstElement * element, GstMessage * message)
2099 {
2100 GstBus *bus;
2101 gboolean result = FALSE;
2102
2103 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2104 g_return_val_if_fail (message != NULL, FALSE);
2105
2106 GST_OBJECT_LOCK (element);
2107 bus = element->bus;
2108
2109 if (G_UNLIKELY (bus == NULL))
2110 goto no_bus;
2111
2112 gst_object_ref (bus);
2113 GST_OBJECT_UNLOCK (element);
2114
2115 /* we release the element lock when posting the message so that any
2116 * (synchronous) message handlers can operate on the element */
2117 result = gst_bus_post (bus, message);
2118 gst_object_unref (bus);
2119
2120 return result;
2121
2122 /* ERRORS */
2123 no_bus:
2124 {
2125 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element,
2126 "not posting message %p: no bus", message);
2127 GST_OBJECT_UNLOCK (element);
2128 gst_message_unref (message);
2129 return FALSE;
2130 }
2131 }
2132
2133 /**
2134 * gst_element_post_message:
2135 * @element: a #GstElement posting the message
2136 * @message: (transfer full): a #GstMessage to post
2137 *
2138 * Post a message on the element's #GstBus. This function takes ownership of the
2139 * message; if you want to access the message after this call, you should add an
2140 * additional reference before calling.
2141 *
2142 * Returns: %TRUE if the message was successfully posted. The function returns
2143 * %FALSE if the element did not have a bus.
2144 *
2145 * MT safe.
2146 */
2147 gboolean
gst_element_post_message(GstElement * element,GstMessage * message)2148 gst_element_post_message (GstElement * element, GstMessage * message)
2149 {
2150 GstElementClass *klass;
2151 gboolean res = FALSE;
2152
2153 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2154 g_return_val_if_fail (message != NULL, FALSE);
2155
2156 GST_TRACER_ELEMENT_POST_MESSAGE_PRE (element, message);
2157
2158 klass = GST_ELEMENT_GET_CLASS (element);
2159 if (klass->post_message)
2160 res = klass->post_message (element, message);
2161 else
2162 gst_message_unref (message);
2163
2164 GST_TRACER_ELEMENT_POST_MESSAGE_POST (element, res);
2165 return res;
2166 }
2167
2168 /**
2169 * _gst_element_error_printf:
2170 * @format: (allow-none): the printf-like format to use, or %NULL
2171 *
2172 * This function is only used internally by the gst_element_error() macro.
2173 *
2174 * Returns: (transfer full) (nullable): a newly allocated string, or
2175 * %NULL if the format was %NULL or ""
2176 *
2177 * MT safe.
2178 */
2179 gchar *
_gst_element_error_printf(const gchar * format,...)2180 _gst_element_error_printf (const gchar * format, ...)
2181 {
2182 va_list args;
2183 gchar *buffer;
2184 int len;
2185
2186 if (format == NULL)
2187 return NULL;
2188 if (format[0] == 0)
2189 return NULL;
2190
2191 va_start (args, format);
2192
2193 len = __gst_vasprintf (&buffer, format, args);
2194
2195 va_end (args);
2196
2197 if (len < 0)
2198 buffer = NULL;
2199
2200 return buffer;
2201 }
2202
2203 /**
2204 * gst_element_message_full_with_details:
2205 * @element: a #GstElement to send message from
2206 * @type: the #GstMessageType
2207 * @domain: the GStreamer GError domain this message belongs to
2208 * @code: the GError code belonging to the domain
2209 * @text: (allow-none) (transfer full): an allocated text string to be used
2210 * as a replacement for the default message connected to code,
2211 * or %NULL
2212 * @debug: (allow-none) (transfer full): an allocated debug message to be
2213 * used as a replacement for the default debugging information,
2214 * or %NULL
2215 * @file: the source code file where the error was generated
2216 * @function: the source code function where the error was generated
2217 * @line: the source code line where the error was generated
2218 * @structure:(transfer full): optional details structure
2219 *
2220 * Post an error, warning or info message on the bus from inside an element.
2221 *
2222 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
2223 * #GST_MESSAGE_INFO.
2224 *
2225 * Since: 1.10
2226 */
gst_element_message_full_with_details(GstElement * element,GstMessageType type,GQuark domain,gint code,gchar * text,gchar * debug,const gchar * file,const gchar * function,gint line,GstStructure * structure)2227 void gst_element_message_full_with_details
2228 (GstElement * element, GstMessageType type,
2229 GQuark domain, gint code, gchar * text,
2230 gchar * debug, const gchar * file, const gchar * function, gint line,
2231 GstStructure * structure)
2232 {
2233 GError *gerror = NULL;
2234 gchar *name;
2235 gchar *sent_text;
2236 gchar *sent_debug;
2237 gboolean has_debug = TRUE;
2238 GstMessage *message = NULL;
2239
2240 /* checks */
2241 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element, "start");
2242 g_return_if_fail (GST_IS_ELEMENT (element));
2243 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
2244 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
2245
2246 /* check if we send the given text or the default error text */
2247 if ((text == NULL) || (text[0] == 0)) {
2248 /* text could have come from g_strdup_printf (""); */
2249 g_free (text);
2250 sent_text = gst_error_get_message (domain, code);
2251 } else
2252 sent_text = text;
2253
2254 /* construct a sent_debug with extra information from source */
2255 if ((debug == NULL) || (debug[0] == 0)) {
2256 /* debug could have come from g_strdup_printf (""); */
2257 has_debug = FALSE;
2258 }
2259
2260 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
2261 if (has_debug)
2262 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
2263 file, line, function, name, debug);
2264 else
2265 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
2266 file, line, function, name);
2267 g_free (name);
2268 g_free (debug);
2269
2270 /* create gerror and post message */
2271 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
2272 sent_text);
2273 gerror = g_error_new_literal (domain, code, sent_text);
2274
2275 switch (type) {
2276 case GST_MESSAGE_ERROR:
2277 message =
2278 gst_message_new_error_with_details (GST_OBJECT_CAST (element), gerror,
2279 sent_debug, structure);
2280 break;
2281 case GST_MESSAGE_WARNING:
2282 message =
2283 gst_message_new_warning_with_details (GST_OBJECT_CAST (element),
2284 gerror, sent_debug, structure);
2285 break;
2286 case GST_MESSAGE_INFO:
2287 message =
2288 gst_message_new_info_with_details (GST_OBJECT_CAST (element), gerror,
2289 sent_debug, structure);
2290 break;
2291 default:
2292 g_assert_not_reached ();
2293 break;
2294 }
2295
2296 gst_element_post_message (element, message);
2297
2298 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
2299 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
2300
2301 /* cleanup */
2302 g_error_free (gerror);
2303 g_free (sent_debug);
2304 g_free (sent_text);
2305 }
2306
2307 /**
2308 * gst_element_message_full:
2309 * @element: a #GstElement to send message from
2310 * @type: the #GstMessageType
2311 * @domain: the GStreamer GError domain this message belongs to
2312 * @code: the GError code belonging to the domain
2313 * @text: (allow-none) (transfer full): an allocated text string to be used
2314 * as a replacement for the default message connected to code,
2315 * or %NULL
2316 * @debug: (allow-none) (transfer full): an allocated debug message to be
2317 * used as a replacement for the default debugging information,
2318 * or %NULL
2319 * @file: the source code file where the error was generated
2320 * @function: the source code function where the error was generated
2321 * @line: the source code line where the error was generated
2322 *
2323 * Post an error, warning or info message on the bus from inside an element.
2324 *
2325 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
2326 * #GST_MESSAGE_INFO.
2327 *
2328 * MT safe.
2329 */
gst_element_message_full(GstElement * element,GstMessageType type,GQuark domain,gint code,gchar * text,gchar * debug,const gchar * file,const gchar * function,gint line)2330 void gst_element_message_full
2331 (GstElement * element, GstMessageType type,
2332 GQuark domain, gint code, gchar * text,
2333 gchar * debug, const gchar * file, const gchar * function, gint line)
2334 {
2335 gst_element_message_full_with_details (element, type, domain, code, text,
2336 debug, file, function, line, NULL);
2337 }
2338
2339 /**
2340 * gst_element_is_locked_state:
2341 * @element: a #GstElement.
2342 *
2343 * Checks if the state of an element is locked.
2344 * If the state of an element is locked, state changes of the parent don't
2345 * affect the element.
2346 * This way you can leave currently unused elements inside bins. Just lock their
2347 * state before changing the state from #GST_STATE_NULL.
2348 *
2349 * MT safe.
2350 *
2351 * Returns: %TRUE, if the element's state is locked.
2352 */
2353 gboolean
gst_element_is_locked_state(GstElement * element)2354 gst_element_is_locked_state (GstElement * element)
2355 {
2356 gboolean result;
2357
2358 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2359
2360 GST_OBJECT_LOCK (element);
2361 result = GST_ELEMENT_IS_LOCKED_STATE (element);
2362 GST_OBJECT_UNLOCK (element);
2363
2364 return result;
2365 }
2366
2367 /**
2368 * gst_element_set_locked_state:
2369 * @element: a #GstElement
2370 * @locked_state: %TRUE to lock the element's state
2371 *
2372 * Locks the state of an element, so state changes of the parent don't affect
2373 * this element anymore.
2374 *
2375 * Note that this is racy if the state lock of the parent bin is not taken.
2376 * The parent bin might've just checked the flag in another thread and as the
2377 * next step proceed to change the child element's state.
2378 *
2379 * MT safe.
2380 *
2381 * Returns: %TRUE if the state was changed, %FALSE if bad parameters were given
2382 * or the elements state-locking needed no change.
2383 */
2384 gboolean
gst_element_set_locked_state(GstElement * element,gboolean locked_state)2385 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
2386 {
2387 gboolean old;
2388
2389 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2390
2391 GST_OBJECT_LOCK (element);
2392 old = GST_ELEMENT_IS_LOCKED_STATE (element);
2393
2394 if (G_UNLIKELY (old == locked_state))
2395 goto was_ok;
2396
2397 if (locked_state) {
2398 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
2399 GST_ELEMENT_NAME (element));
2400 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_FLAG_LOCKED_STATE);
2401 } else {
2402 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
2403 GST_ELEMENT_NAME (element));
2404 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_FLAG_LOCKED_STATE);
2405 }
2406 GST_OBJECT_UNLOCK (element);
2407
2408 return TRUE;
2409
2410 was_ok:
2411 {
2412 GST_CAT_DEBUG (GST_CAT_STATES,
2413 "elements %s was already in locked state %d",
2414 GST_ELEMENT_NAME (element), old);
2415 GST_OBJECT_UNLOCK (element);
2416
2417 return FALSE;
2418 }
2419 }
2420
2421 /**
2422 * gst_element_sync_state_with_parent:
2423 * @element: a #GstElement.
2424 *
2425 * Tries to change the state of the element to the same as its parent.
2426 * If this function returns %FALSE, the state of element is undefined.
2427 *
2428 * Returns: %TRUE, if the element's state could be synced to the parent's state.
2429 *
2430 * MT safe.
2431 */
2432 gboolean
gst_element_sync_state_with_parent(GstElement * element)2433 gst_element_sync_state_with_parent (GstElement * element)
2434 {
2435 GstElement *parent;
2436 GstState target;
2437 GstStateChangeReturn ret;
2438
2439 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2440
2441 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
2442 GstState parent_current, parent_pending;
2443
2444 GST_OBJECT_LOCK (parent);
2445 parent_current = GST_STATE (parent);
2446 parent_pending = GST_STATE_PENDING (parent);
2447 GST_OBJECT_UNLOCK (parent);
2448
2449 /* set to pending if there is one, else we set it to the current state of
2450 * the parent */
2451 if (parent_pending != GST_STATE_VOID_PENDING)
2452 target = parent_pending;
2453 else
2454 target = parent_current;
2455
2456 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2457 "syncing state (%s) to parent %s %s (%s, %s)",
2458 gst_element_state_get_name (GST_STATE (element)),
2459 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
2460 gst_element_state_get_name (parent_current),
2461 gst_element_state_get_name (parent_pending));
2462
2463 ret = gst_element_set_state (element, target);
2464 if (ret == GST_STATE_CHANGE_FAILURE)
2465 goto failed;
2466
2467 gst_object_unref (parent);
2468
2469 return TRUE;
2470 } else {
2471 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
2472 }
2473 return FALSE;
2474
2475 /* ERROR */
2476 failed:
2477 {
2478 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2479 "syncing state failed (%s)",
2480 gst_element_state_change_return_get_name (ret));
2481 gst_object_unref (parent);
2482 return FALSE;
2483 }
2484 }
2485
2486 /* MT safe */
2487 static GstStateChangeReturn
gst_element_get_state_func(GstElement * element,GstState * state,GstState * pending,GstClockTime timeout)2488 gst_element_get_state_func (GstElement * element,
2489 GstState * state, GstState * pending, GstClockTime timeout)
2490 {
2491 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
2492 GstState old_pending;
2493
2494 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
2495 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
2496
2497 GST_OBJECT_LOCK (element);
2498 ret = GST_STATE_RETURN (element);
2499 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
2500 gst_element_state_change_return_get_name (ret));
2501
2502 /* we got an error, report immediately */
2503 if (ret == GST_STATE_CHANGE_FAILURE)
2504 goto done;
2505
2506 /* we got no_preroll, report immediately */
2507 if (ret == GST_STATE_CHANGE_NO_PREROLL)
2508 goto done;
2509
2510 /* no need to wait async if we are not async */
2511 if (ret != GST_STATE_CHANGE_ASYNC)
2512 goto done;
2513
2514 old_pending = GST_STATE_PENDING (element);
2515 if (old_pending != GST_STATE_VOID_PENDING) {
2516 gboolean signaled;
2517 guint32 cookie;
2518
2519 /* get cookie to detect state changes during waiting */
2520 cookie = element->state_cookie;
2521
2522 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2523 "waiting for element to commit state");
2524
2525 /* we have a pending state change, wait for it to complete */
2526 if (timeout != GST_CLOCK_TIME_NONE) {
2527 gint64 end_time;
2528 /* make timeout absolute */
2529 end_time = g_get_monotonic_time () + (timeout / 1000);
2530 signaled = GST_STATE_WAIT_UNTIL (element, end_time);
2531 } else {
2532 GST_STATE_WAIT (element);
2533 signaled = TRUE;
2534 }
2535
2536 if (!signaled) {
2537 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
2538 /* timeout triggered */
2539 ret = GST_STATE_CHANGE_ASYNC;
2540 } else {
2541 if (cookie != element->state_cookie)
2542 goto interrupted;
2543
2544 /* could be success or failure */
2545 if (old_pending == GST_STATE (element)) {
2546 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
2547 ret = GST_STATE_CHANGE_SUCCESS;
2548 } else {
2549 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
2550 ret = GST_STATE_CHANGE_FAILURE;
2551 }
2552 }
2553 /* if nothing is pending anymore we can return SUCCESS */
2554 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
2555 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
2556 ret = GST_STATE_CHANGE_SUCCESS;
2557 }
2558 }
2559
2560 done:
2561 if (state)
2562 *state = GST_STATE (element);
2563 if (pending)
2564 *pending = GST_STATE_PENDING (element);
2565
2566 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2567 "state current: %s, pending: %s, result: %s",
2568 gst_element_state_get_name (GST_STATE (element)),
2569 gst_element_state_get_name (GST_STATE_PENDING (element)),
2570 gst_element_state_change_return_get_name (ret));
2571 GST_OBJECT_UNLOCK (element);
2572
2573 return ret;
2574
2575 interrupted:
2576 {
2577 if (state)
2578 *state = GST_STATE_VOID_PENDING;
2579 if (pending)
2580 *pending = GST_STATE_VOID_PENDING;
2581
2582 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "interrupted");
2583
2584 GST_OBJECT_UNLOCK (element);
2585
2586 return GST_STATE_CHANGE_FAILURE;
2587 }
2588 }
2589
2590 /**
2591 * gst_element_get_state:
2592 * @element: a #GstElement to get the state of.
2593 * @state: (out) (allow-none): a pointer to #GstState to hold the state.
2594 * Can be %NULL.
2595 * @pending: (out) (allow-none): a pointer to #GstState to hold the pending
2596 * state. Can be %NULL.
2597 * @timeout: a #GstClockTime to specify the timeout for an async
2598 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
2599 *
2600 * Gets the state of the element.
2601 *
2602 * For elements that performed an ASYNC state change, as reported by
2603 * gst_element_set_state(), this function will block up to the
2604 * specified timeout value for the state change to complete.
2605 * If the element completes the state change or goes into
2606 * an error, this function returns immediately with a return value of
2607 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
2608 *
2609 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
2610 * returns the current and pending state immediately.
2611 *
2612 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
2613 * successfully changed its state but is not able to provide data yet.
2614 * This mostly happens for live sources that only produce data in
2615 * %GST_STATE_PLAYING. While the state change return is equivalent to
2616 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
2617 * some sink elements might not be able to complete their state change because
2618 * an element is not producing data to complete the preroll. When setting the
2619 * element to playing, the preroll will complete and playback will start.
2620 *
2621 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
2622 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
2623 * element is still performing a state change or
2624 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
2625 *
2626 * MT safe.
2627 */
2628 GstStateChangeReturn
gst_element_get_state(GstElement * element,GstState * state,GstState * pending,GstClockTime timeout)2629 gst_element_get_state (GstElement * element,
2630 GstState * state, GstState * pending, GstClockTime timeout)
2631 {
2632 GstElementClass *oclass;
2633 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2634
2635 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2636
2637 oclass = GST_ELEMENT_GET_CLASS (element);
2638
2639 if (oclass->get_state)
2640 result = (oclass->get_state) (element, state, pending, timeout);
2641
2642 return result;
2643 }
2644
2645 /**
2646 * gst_element_abort_state:
2647 * @element: a #GstElement to abort the state of.
2648 *
2649 * Abort the state change of the element. This function is used
2650 * by elements that do asynchronous state changes and find out
2651 * something is wrong.
2652 *
2653 * This function should be called with the STATE_LOCK held.
2654 *
2655 * MT safe.
2656 */
2657 void
gst_element_abort_state(GstElement * element)2658 gst_element_abort_state (GstElement * element)
2659 {
2660 GstState pending;
2661
2662 #ifndef GST_DISABLE_GST_DEBUG
2663 GstState old_state;
2664 #endif
2665
2666 g_return_if_fail (GST_IS_ELEMENT (element));
2667
2668 GST_OBJECT_LOCK (element);
2669 pending = GST_STATE_PENDING (element);
2670
2671 if (pending == GST_STATE_VOID_PENDING ||
2672 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2673 goto nothing_aborted;
2674
2675 #ifndef GST_DISABLE_GST_DEBUG
2676 old_state = GST_STATE (element);
2677
2678 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2679 "aborting state from %s to %s", gst_element_state_get_name (old_state),
2680 gst_element_state_get_name (pending));
2681 #endif
2682
2683 /* flag error */
2684 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
2685
2686 GST_STATE_BROADCAST (element);
2687 GST_OBJECT_UNLOCK (element);
2688
2689 return;
2690
2691 nothing_aborted:
2692 {
2693 GST_OBJECT_UNLOCK (element);
2694 return;
2695 }
2696 }
2697
2698 /* Not static because GstBin has manual state handling too */
2699 void
_priv_gst_element_state_changed(GstElement * element,GstState oldstate,GstState newstate,GstState pending)2700 _priv_gst_element_state_changed (GstElement * element, GstState oldstate,
2701 GstState newstate, GstState pending)
2702 {
2703 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
2704 GstMessage *message;
2705
2706 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2707 "notifying about state-changed %s to %s (%s pending)",
2708 gst_element_state_get_name (oldstate),
2709 gst_element_state_get_name (newstate),
2710 gst_element_state_get_name (pending));
2711
2712 if (klass->state_changed)
2713 klass->state_changed (element, oldstate, newstate, pending);
2714
2715 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2716 oldstate, newstate, pending);
2717 gst_element_post_message (element, message);
2718 }
2719
2720 /**
2721 * gst_element_continue_state:
2722 * @element: a #GstElement to continue the state change of.
2723 * @ret: The previous state return value
2724 *
2725 * Commit the state change of the element and proceed to the next
2726 * pending state if any. This function is used
2727 * by elements that do asynchronous state changes.
2728 * The core will normally call this method automatically when an
2729 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
2730 *
2731 * If after calling this method the element still has not reached
2732 * the pending state, the next state change is performed.
2733 *
2734 * This method is used internally and should normally not be called by plugins
2735 * or applications.
2736 *
2737 * This function must be called with STATE_LOCK held.
2738 *
2739 * Returns: The result of the commit state change.
2740 *
2741 * MT safe.
2742 */
2743 GstStateChangeReturn
gst_element_continue_state(GstElement * element,GstStateChangeReturn ret)2744 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2745 {
2746 GstStateChangeReturn old_ret;
2747 GstState old_state, old_next;
2748 GstState current, next, pending;
2749 GstStateChange transition;
2750
2751 GST_OBJECT_LOCK (element);
2752 old_ret = GST_STATE_RETURN (element);
2753 GST_STATE_RETURN (element) = ret;
2754 pending = GST_STATE_PENDING (element);
2755
2756 /* check if there is something to commit */
2757 if (pending == GST_STATE_VOID_PENDING)
2758 goto nothing_pending;
2759
2760 old_state = GST_STATE (element);
2761 /* this is the state we should go to next */
2762 old_next = GST_STATE_NEXT (element);
2763 /* update current state */
2764 current = GST_STATE (element) = old_next;
2765
2766 /* see if we reached the final state */
2767 if (pending == current)
2768 goto complete;
2769
2770 next = GST_STATE_GET_NEXT (current, pending);
2771 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2772
2773 GST_STATE_NEXT (element) = next;
2774 /* mark busy */
2775 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2776 GST_OBJECT_UNLOCK (element);
2777
2778 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2779 "committing state from %s to %s, pending %s, next %s",
2780 gst_element_state_get_name (old_state),
2781 gst_element_state_get_name (old_next),
2782 gst_element_state_get_name (pending), gst_element_state_get_name (next));
2783
2784 _priv_gst_element_state_changed (element, old_state, old_next, pending);
2785
2786 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2787 "continue state change %s to %s, final %s",
2788 gst_element_state_get_name (current),
2789 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2790
2791 ret = gst_element_change_state (element, transition);
2792
2793 return ret;
2794
2795 nothing_pending:
2796 {
2797 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2798 GST_OBJECT_UNLOCK (element);
2799 return ret;
2800 }
2801 complete:
2802 {
2803 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2804 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2805
2806 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2807 "completed state change to %s", gst_element_state_get_name (pending));
2808 GST_OBJECT_UNLOCK (element);
2809
2810 /* don't post silly messages with the same state. This can happen
2811 * when an element state is changed to what it already was. For bins
2812 * this can be the result of a lost state, which we check with the
2813 * previous return value.
2814 * We do signal the cond though as a _get_state() might be blocking
2815 * on it. */
2816 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC)
2817 _priv_gst_element_state_changed (element, old_state, old_next,
2818 GST_STATE_VOID_PENDING);
2819
2820 GST_STATE_BROADCAST (element);
2821
2822 return ret;
2823 }
2824 }
2825
2826 /**
2827 * gst_element_lost_state:
2828 * @element: a #GstElement the state is lost of
2829 *
2830 * Brings the element to the lost state. The current state of the
2831 * element is copied to the pending state so that any call to
2832 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2833 *
2834 * An ASYNC_START message is posted. If the element was PLAYING, it will
2835 * go to PAUSED. The element will be restored to its PLAYING state by
2836 * the parent pipeline when it prerolls again.
2837 *
2838 * This is mostly used for elements that lost their preroll buffer
2839 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2840 * they will go to their pending state again when a new preroll buffer is
2841 * queued. This function can only be called when the element is currently
2842 * not in error or an async state change.
2843 *
2844 * This function is used internally and should normally not be called from
2845 * plugins or applications.
2846 */
2847 void
gst_element_lost_state(GstElement * element)2848 gst_element_lost_state (GstElement * element)
2849 {
2850 GstState old_state, new_state;
2851 GstMessage *message;
2852
2853 g_return_if_fail (GST_IS_ELEMENT (element));
2854
2855 GST_OBJECT_LOCK (element);
2856 if (GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2857 goto nothing_lost;
2858
2859 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING)
2860 goto only_async_start;
2861
2862 old_state = GST_STATE (element);
2863
2864 /* when we were PLAYING, the new state is PAUSED. We will also not
2865 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2866 * when we preroll. */
2867 if (old_state > GST_STATE_PAUSED)
2868 new_state = GST_STATE_PAUSED;
2869 else
2870 new_state = old_state;
2871
2872 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2873 "lost state of %s to %s", gst_element_state_get_name (old_state),
2874 gst_element_state_get_name (new_state));
2875
2876 GST_STATE (element) = new_state;
2877 GST_STATE_NEXT (element) = new_state;
2878 GST_STATE_PENDING (element) = new_state;
2879 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2880 GST_OBJECT_UNLOCK (element);
2881
2882 _priv_gst_element_state_changed (element, new_state, new_state, new_state);
2883
2884 message = gst_message_new_async_start (GST_OBJECT_CAST (element));
2885 gst_element_post_message (element, message);
2886
2887 return;
2888
2889 nothing_lost:
2890 {
2891 GST_OBJECT_UNLOCK (element);
2892 return;
2893 }
2894 only_async_start:
2895 {
2896 GST_OBJECT_UNLOCK (element);
2897
2898 message = gst_message_new_async_start (GST_OBJECT_CAST (element));
2899 gst_element_post_message (element, message);
2900 return;
2901 }
2902 }
2903
2904 /**
2905 * gst_element_set_state:
2906 * @element: a #GstElement to change state of.
2907 * @state: the element's new #GstState.
2908 *
2909 * Sets the state of the element. This function will try to set the
2910 * requested state by going through all the intermediary states and calling
2911 * the class's state change function for each.
2912 *
2913 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2914 * element will perform the remainder of the state change asynchronously in
2915 * another thread.
2916 * An application can use gst_element_get_state() to wait for the completion
2917 * of the state change or it can wait for a %GST_MESSAGE_ASYNC_DONE or
2918 * %GST_MESSAGE_STATE_CHANGED on the bus.
2919 *
2920 * State changes to %GST_STATE_READY or %GST_STATE_NULL never return
2921 * #GST_STATE_CHANGE_ASYNC.
2922 *
2923 * Returns: Result of the state change using #GstStateChangeReturn.
2924 *
2925 * MT safe.
2926 */
2927 GstStateChangeReturn
gst_element_set_state(GstElement * element,GstState state)2928 gst_element_set_state (GstElement * element, GstState state)
2929 {
2930 GstElementClass *oclass;
2931 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2932
2933 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2934
2935 oclass = GST_ELEMENT_GET_CLASS (element);
2936
2937 if (oclass->set_state)
2938 result = (oclass->set_state) (element, state);
2939
2940 return result;
2941 }
2942
2943 /*
2944 * default set state function, calculates the next state based
2945 * on current state and calls the change_state function
2946 */
2947 static GstStateChangeReturn
gst_element_set_state_func(GstElement * element,GstState state)2948 gst_element_set_state_func (GstElement * element, GstState state)
2949 {
2950 GstState current, next, old_pending;
2951 GstStateChangeReturn ret;
2952 GstStateChange transition;
2953 GstStateChangeReturn old_ret;
2954
2955 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2956
2957 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2958 gst_element_state_get_name (state));
2959
2960 /* state lock is taken to protect the set_state() and get_state()
2961 * procedures, it does not lock any variables. */
2962 GST_STATE_LOCK (element);
2963
2964 /* now calculate how to get to the new state */
2965 GST_OBJECT_LOCK (element);
2966 old_ret = GST_STATE_RETURN (element);
2967 /* previous state change returned an error, remove all pending
2968 * and next states */
2969 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2970 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2971 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2972 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2973 }
2974
2975 current = GST_STATE (element);
2976 next = GST_STATE_NEXT (element);
2977 old_pending = GST_STATE_PENDING (element);
2978
2979 /* this is the (new) state we should go to. TARGET is the last state we set on
2980 * the element. */
2981 if (state != GST_STATE_TARGET (element)) {
2982 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2983 "setting target state to %s", gst_element_state_get_name (state));
2984 GST_STATE_TARGET (element) = state;
2985 /* increment state cookie so that we can track each state change. We only do
2986 * this if this is actually a new state change. */
2987 element->state_cookie++;
2988 }
2989 GST_STATE_PENDING (element) = state;
2990
2991 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2992 "current %s, old_pending %s, next %s, old return %s",
2993 gst_element_state_get_name (current),
2994 gst_element_state_get_name (old_pending),
2995 gst_element_state_get_name (next),
2996 gst_element_state_change_return_get_name (old_ret));
2997
2998 /* if the element was busy doing a state change, we just update the
2999 * target state, it'll get to it async then. */
3000 if (old_pending != GST_STATE_VOID_PENDING) {
3001 /* upwards state change will happen ASYNC */
3002 if (old_pending <= state)
3003 goto was_busy;
3004 /* element is going to this state already */
3005 else if (next == state)
3006 goto was_busy;
3007 /* element was performing an ASYNC upward state change and
3008 * we request to go downward again. Start from the next pending
3009 * state then. */
3010 else if (next > state
3011 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
3012 current = next;
3013 }
3014 }
3015 next = GST_STATE_GET_NEXT (current, state);
3016 /* now we store the next state */
3017 GST_STATE_NEXT (element) = next;
3018 /* mark busy, we need to check that there is actually a state change
3019 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
3020 * the default element change_state function has no way to know what the
3021 * old value was... could consider this a FIXME...*/
3022 if (current != next)
3023 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
3024
3025 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
3026
3027 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3028 "%s: setting state from %s to %s",
3029 (next != state ? "intermediate" : "final"),
3030 gst_element_state_get_name (current), gst_element_state_get_name (next));
3031
3032 /* now signal any waiters, they will error since the cookie was incremented */
3033 GST_STATE_BROADCAST (element);
3034
3035 GST_OBJECT_UNLOCK (element);
3036
3037 ret = gst_element_change_state (element, transition);
3038
3039 GST_STATE_UNLOCK (element);
3040
3041 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
3042 gst_element_state_change_return_get_name (ret));
3043
3044 return ret;
3045
3046 was_busy:
3047 {
3048 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
3049 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3050 "element was busy with async state change");
3051 GST_OBJECT_UNLOCK (element);
3052
3053 GST_STATE_UNLOCK (element);
3054
3055 return GST_STATE_CHANGE_ASYNC;
3056 }
3057 }
3058
3059 /**
3060 * gst_element_change_state:
3061 * @element: a #GstElement
3062 * @transition: the requested transition
3063 *
3064 * Perform @transition on @element.
3065 *
3066 * This function must be called with STATE_LOCK held and is mainly used
3067 * internally.
3068 *
3069 * Returns: the #GstStateChangeReturn of the state transition.
3070 */
3071 GstStateChangeReturn
gst_element_change_state(GstElement * element,GstStateChange transition)3072 gst_element_change_state (GstElement * element, GstStateChange transition)
3073 {
3074 GstElementClass *oclass;
3075 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3076
3077 oclass = GST_ELEMENT_GET_CLASS (element);
3078
3079 GST_TRACER_ELEMENT_CHANGE_STATE_PRE (element, transition);
3080
3081 /* call the state change function so it can set the state */
3082 if (oclass->change_state)
3083 ret = (oclass->change_state) (element, transition);
3084 else
3085 ret = GST_STATE_CHANGE_FAILURE;
3086
3087 GST_TRACER_ELEMENT_CHANGE_STATE_POST (element, transition, ret);
3088
3089 switch (ret) {
3090 case GST_STATE_CHANGE_FAILURE:
3091 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
3092 "have FAILURE change_state return");
3093 /* state change failure */
3094 gst_element_abort_state (element);
3095 break;
3096 case GST_STATE_CHANGE_ASYNC:
3097 {
3098 GstState target;
3099
3100 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3101 "element will change state ASYNC");
3102
3103 target = GST_STATE_TARGET (element);
3104
3105 if (target > GST_STATE_READY)
3106 goto async;
3107
3108 /* else we just continue the state change downwards */
3109 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
3110 "forcing commit state %s <= %s",
3111 gst_element_state_get_name (target),
3112 gst_element_state_get_name (GST_STATE_READY));
3113
3114 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
3115 break;
3116 }
3117 case GST_STATE_CHANGE_SUCCESS:
3118 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3119 "element changed state SUCCESS");
3120 /* we can commit the state now which will proceed to
3121 * the next state */
3122 ret = gst_element_continue_state (element, ret);
3123 break;
3124 case GST_STATE_CHANGE_NO_PREROLL:
3125 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3126 "element changed state NO_PREROLL");
3127 /* we can commit the state now which will proceed to
3128 * the next state */
3129 ret = gst_element_continue_state (element, ret);
3130 break;
3131 default:
3132 goto invalid_return;
3133 }
3134
3135 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
3136
3137 return ret;
3138
3139 async:
3140 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
3141 ret);
3142
3143 return ret;
3144
3145 /* ERROR */
3146 invalid_return:
3147 {
3148 GST_OBJECT_LOCK (element);
3149 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
3150 g_critical ("%s: unknown return value %d from a state change function",
3151 GST_ELEMENT_NAME (element), ret);
3152
3153 /* we are in error now */
3154 ret = GST_STATE_CHANGE_FAILURE;
3155 GST_STATE_RETURN (element) = ret;
3156 GST_OBJECT_UNLOCK (element);
3157
3158 return ret;
3159 }
3160 }
3161
3162 /* gst_iterator_fold functions for pads_activate
3163 * Stop the iterator if activating one pad failed, but only if that pad
3164 * has not been removed from the element. */
3165 static gboolean
activate_pads(const GValue * vpad,GValue * ret,gboolean * active)3166 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
3167 {
3168 GstPad *pad = g_value_get_object (vpad);
3169 gboolean cont = TRUE;
3170
3171 if (!gst_pad_set_active (pad, *active)) {
3172 if (GST_PAD_PARENT (pad) != NULL) {
3173 cont = FALSE;
3174 g_value_set_boolean (ret, FALSE);
3175 }
3176 }
3177
3178 return cont;
3179 }
3180
3181 /* returns false on error or early cutout of the fold, true if all
3182 * pads in @iter were (de)activated successfully. */
3183 static gboolean
iterator_activate_fold_with_resync(GstIterator * iter,GstIteratorFoldFunction func,gpointer user_data)3184 iterator_activate_fold_with_resync (GstIterator * iter,
3185 GstIteratorFoldFunction func, gpointer user_data)
3186 {
3187 GstIteratorResult ires;
3188 GValue ret = { 0 };
3189
3190 /* no need to unset this later, it's just a boolean */
3191 g_value_init (&ret, G_TYPE_BOOLEAN);
3192 g_value_set_boolean (&ret, TRUE);
3193
3194 while (1) {
3195 ires = gst_iterator_fold (iter, func, &ret, user_data);
3196 switch (ires) {
3197 case GST_ITERATOR_RESYNC:
3198 /* need to reset the result again */
3199 g_value_set_boolean (&ret, TRUE);
3200 gst_iterator_resync (iter);
3201 break;
3202 case GST_ITERATOR_DONE:
3203 /* all pads iterated, return collected value */
3204 goto done;
3205 default:
3206 /* iterator returned _ERROR or premature end with _OK,
3207 * mark an error and exit */
3208 g_value_set_boolean (&ret, FALSE);
3209 goto done;
3210 }
3211 }
3212 done:
3213 /* return collected value */
3214 return g_value_get_boolean (&ret);
3215 }
3216
3217 /* is called with STATE_LOCK
3218 *
3219 * Pads are activated from source pads to sinkpads.
3220 */
3221 static gboolean
gst_element_pads_activate(GstElement * element,gboolean active)3222 gst_element_pads_activate (GstElement * element, gboolean active)
3223 {
3224 GstIterator *iter;
3225 gboolean res;
3226
3227 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3228 "%s pads", active ? "activate" : "deactivate");
3229
3230 iter = gst_element_iterate_src_pads (element);
3231 res =
3232 iterator_activate_fold_with_resync (iter,
3233 (GstIteratorFoldFunction) activate_pads, &active);
3234 gst_iterator_free (iter);
3235 if (G_UNLIKELY (!res))
3236 goto src_failed;
3237
3238 iter = gst_element_iterate_sink_pads (element);
3239 res =
3240 iterator_activate_fold_with_resync (iter,
3241 (GstIteratorFoldFunction) activate_pads, &active);
3242 gst_iterator_free (iter);
3243 if (G_UNLIKELY (!res))
3244 goto sink_failed;
3245
3246 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3247 "pad %sactivation successful", active ? "" : "de");
3248
3249 return TRUE;
3250
3251 /* ERRORS */
3252 src_failed:
3253 {
3254 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3255 "pad %sactivation failed", active ? "" : "de");
3256 return FALSE;
3257 }
3258 sink_failed:
3259 {
3260 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3261 "sink pads_activate failed");
3262 return FALSE;
3263 }
3264 }
3265
3266 /* is called with STATE_LOCK */
3267 static GstStateChangeReturn
gst_element_change_state_func(GstElement * element,GstStateChange transition)3268 gst_element_change_state_func (GstElement * element, GstStateChange transition)
3269 {
3270 GstState state, next;
3271 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
3272
3273 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
3274
3275 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
3276 next = GST_STATE_TRANSITION_NEXT (transition);
3277
3278 /* if the element already is in the given state, we just return success */
3279 if (next == GST_STATE_VOID_PENDING || state == next)
3280 goto was_ok;
3281
3282 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
3283 "default handler tries setting state from %s to %s (%04x)",
3284 gst_element_state_get_name (state),
3285 gst_element_state_get_name (next), transition);
3286
3287 switch (transition) {
3288 case GST_STATE_CHANGE_NULL_TO_READY:
3289 break;
3290 case GST_STATE_CHANGE_READY_TO_PAUSED:
3291 if (!gst_element_pads_activate (element, TRUE)) {
3292 result = GST_STATE_CHANGE_FAILURE;
3293 }
3294 break;
3295 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3296 break;
3297 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3298 break;
3299 case GST_STATE_CHANGE_PAUSED_TO_READY:
3300 case GST_STATE_CHANGE_READY_TO_NULL:{
3301 GList *l;
3302
3303 /* deactivate pads in both cases, since they are activated on
3304 ready->paused but the element might not have made it to paused */
3305 if (!gst_element_pads_activate (element, FALSE)) {
3306 result = GST_STATE_CHANGE_FAILURE;
3307 }
3308
3309 /* Remove all non-persistent contexts */
3310 GST_OBJECT_LOCK (element);
3311 for (l = element->contexts; l;) {
3312 GstContext *context = l->data;
3313
3314 if (!gst_context_is_persistent (context)) {
3315 GList *next;
3316
3317 gst_context_unref (context);
3318 next = l->next;
3319 element->contexts = g_list_delete_link (element->contexts, l);
3320 l = next;
3321 } else {
3322 l = l->next;
3323 }
3324 }
3325 GST_OBJECT_UNLOCK (element);
3326 break;
3327 }
3328 default:
3329 /* this will catch real but unhandled state changes;
3330 * can only be caused by:
3331 * - a new state was added
3332 * - somehow the element was asked to jump across an intermediate state
3333 */
3334 g_warning ("Unhandled state change from %s to %s",
3335 gst_element_state_get_name (state),
3336 gst_element_state_get_name (next));
3337 break;
3338 }
3339 return result;
3340
3341 was_ok:
3342 {
3343 GST_OBJECT_LOCK (element);
3344 result = GST_STATE_RETURN (element);
3345 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3346 "element is already in the %s state",
3347 gst_element_state_get_name (state));
3348 GST_OBJECT_UNLOCK (element);
3349
3350 return result;
3351 }
3352 }
3353
3354 /**
3355 * gst_element_get_factory:
3356 * @element: a #GstElement to request the element factory of.
3357 *
3358 * Retrieves the factory that was used to create this element.
3359 *
3360 * Returns: (transfer none) (nullable): the #GstElementFactory used for creating this
3361 * element or %NULL if element has not been registered (static element). no refcounting is needed.
3362 */
3363 GstElementFactory *
gst_element_get_factory(GstElement * element)3364 gst_element_get_factory (GstElement * element)
3365 {
3366 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3367
3368 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
3369 }
3370
3371 static void
gst_element_dispose(GObject * object)3372 gst_element_dispose (GObject * object)
3373 {
3374 GstElement *element = GST_ELEMENT_CAST (object);
3375 GstClock **clock_p;
3376 GstBus **bus_p;
3377 GstElementClass *oclass;
3378 GList *walk;
3379
3380 oclass = GST_ELEMENT_GET_CLASS (element);
3381
3382 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p dispose", element);
3383
3384 if (GST_STATE (element) != GST_STATE_NULL)
3385 goto not_null;
3386
3387 /* start by releasing all request pads, this might also remove some dynamic
3388 * pads */
3389 walk = element->pads;
3390 while (walk) {
3391 GstPad *pad = GST_PAD_CAST (walk->data);
3392
3393 walk = walk->next;
3394
3395 if (oclass->release_pad && GST_PAD_PAD_TEMPLATE (pad) &&
3396 GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad))
3397 == GST_PAD_REQUEST) {
3398 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3399 "removing request pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3400 oclass->release_pad (element, pad);
3401
3402 /* in case the release_pad function removed the next pad too */
3403 if (walk && g_list_position (element->pads, walk) == -1)
3404 walk = element->pads;
3405 }
3406 }
3407 /* remove the remaining pads */
3408 while (element->pads) {
3409 GstPad *pad = GST_PAD_CAST (element->pads->data);
3410 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3411 "removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3412 if (!gst_element_remove_pad (element, pad)) {
3413 /* only happens when someone unparented our pad.. */
3414 g_critical ("failed to remove pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3415 break;
3416 }
3417 }
3418
3419 GST_OBJECT_LOCK (element);
3420 clock_p = &element->clock;
3421 bus_p = &element->bus;
3422 gst_object_replace ((GstObject **) clock_p, NULL);
3423 gst_object_replace ((GstObject **) bus_p, NULL);
3424 g_list_free_full (element->contexts, (GDestroyNotify) gst_context_unref);
3425 element->contexts = NULL;
3426 GST_OBJECT_UNLOCK (element);
3427
3428 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p parent class dispose",
3429 element);
3430
3431 G_OBJECT_CLASS (parent_class)->dispose (object);
3432
3433 return;
3434
3435 /* ERRORS */
3436 not_null:
3437 {
3438 gboolean is_locked;
3439
3440 is_locked = GST_ELEMENT_IS_LOCKED_STATE (element);
3441 g_critical
3442 ("\nTrying to dispose element %s, but it is in %s%s instead of the NULL"
3443 " state.\n"
3444 "You need to explicitly set elements to the NULL state before\n"
3445 "dropping the final reference, to allow them to clean up.\n"
3446 "This problem may also be caused by a refcounting bug in the\n"
3447 "application or some element.\n",
3448 GST_OBJECT_NAME (element),
3449 gst_element_state_get_name (GST_STATE (element)),
3450 is_locked ? " (locked)" : "");
3451 return;
3452 }
3453 }
3454
3455 static void
gst_element_finalize(GObject * object)3456 gst_element_finalize (GObject * object)
3457 {
3458 GstElement *element = GST_ELEMENT_CAST (object);
3459
3460 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p finalize", element);
3461
3462 g_cond_clear (&element->state_cond);
3463 g_rec_mutex_clear (&element->state_lock);
3464
3465 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p finalize parent",
3466 element);
3467
3468 G_OBJECT_CLASS (parent_class)->finalize (object);
3469 }
3470
3471 static void
gst_element_set_bus_func(GstElement * element,GstBus * bus)3472 gst_element_set_bus_func (GstElement * element, GstBus * bus)
3473 {
3474 GstBus **bus_p;
3475
3476 g_return_if_fail (GST_IS_ELEMENT (element));
3477
3478 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
3479
3480 GST_OBJECT_LOCK (element);
3481 bus_p = &GST_ELEMENT_BUS (element);
3482 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
3483 GST_OBJECT_UNLOCK (element);
3484 }
3485
3486 /**
3487 * gst_element_set_bus:
3488 * @element: a #GstElement to set the bus of.
3489 * @bus: (transfer none) (allow-none): the #GstBus to set.
3490 *
3491 * Sets the bus of the element. Increases the refcount on the bus.
3492 * For internal use only, unless you're testing elements.
3493 *
3494 * MT safe.
3495 */
3496 void
gst_element_set_bus(GstElement * element,GstBus * bus)3497 gst_element_set_bus (GstElement * element, GstBus * bus)
3498 {
3499 GstElementClass *oclass;
3500
3501 g_return_if_fail (GST_IS_ELEMENT (element));
3502
3503 oclass = GST_ELEMENT_GET_CLASS (element);
3504
3505 if (oclass->set_bus)
3506 oclass->set_bus (element, bus);
3507 }
3508
3509 /**
3510 * gst_element_get_bus:
3511 * @element: a #GstElement to get the bus of.
3512 *
3513 * Returns the bus of the element. Note that only a #GstPipeline will provide a
3514 * bus for the application.
3515 *
3516 * Returns: (transfer full) (nullable): the element's #GstBus. unref after
3517 * usage.
3518 *
3519 * MT safe.
3520 */
3521 GstBus *
gst_element_get_bus(GstElement * element)3522 gst_element_get_bus (GstElement * element)
3523 {
3524 GstBus *result = NULL;
3525
3526 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
3527
3528 GST_OBJECT_LOCK (element);
3529 if ((result = GST_ELEMENT_BUS (element)))
3530 gst_object_ref (result);
3531 GST_OBJECT_UNLOCK (element);
3532
3533 GST_CAT_DEBUG_OBJECT (GST_CAT_BUS, element, "got bus %" GST_PTR_FORMAT,
3534 result);
3535
3536 return result;
3537 }
3538
3539 static void
gst_element_set_context_default(GstElement * element,GstContext * context)3540 gst_element_set_context_default (GstElement * element, GstContext * context)
3541 {
3542 const gchar *context_type;
3543 GList *l;
3544
3545 g_return_if_fail (GST_IS_CONTEXT (context));
3546 context_type = gst_context_get_context_type (context);
3547 g_return_if_fail (context_type != NULL);
3548
3549 GST_OBJECT_LOCK (element);
3550 for (l = element->contexts; l; l = l->next) {
3551 GstContext *tmp = l->data;
3552 const gchar *tmp_type = gst_context_get_context_type (tmp);
3553
3554 /* Always store newest context but never replace
3555 * a persistent one by a non-persistent one */
3556 if (g_strcmp0 (context_type, tmp_type) == 0 &&
3557 (gst_context_is_persistent (context) ||
3558 !gst_context_is_persistent (tmp))) {
3559 gst_context_replace ((GstContext **) & l->data, context);
3560 break;
3561 }
3562 }
3563 /* Not found? Add */
3564 if (l == NULL) {
3565 element->contexts =
3566 g_list_prepend (element->contexts, gst_context_ref (context));
3567 }
3568 GST_OBJECT_UNLOCK (element);
3569 }
3570
3571 /**
3572 * gst_element_set_context:
3573 * @element: a #GstElement to set the context of.
3574 * @context: (transfer none): the #GstContext to set.
3575 *
3576 * Sets the context of the element. Increases the refcount of the context.
3577 *
3578 * MT safe.
3579 */
3580 void
gst_element_set_context(GstElement * element,GstContext * context)3581 gst_element_set_context (GstElement * element, GstContext * context)
3582 {
3583 GstElementClass *oclass;
3584
3585 g_return_if_fail (GST_IS_ELEMENT (element));
3586 g_return_if_fail (GST_IS_CONTEXT (context));
3587
3588 oclass = GST_ELEMENT_GET_CLASS (element);
3589
3590 GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element,
3591 "set context %p %" GST_PTR_FORMAT, context,
3592 gst_context_get_structure (context));
3593
3594 if (oclass->set_context)
3595 oclass->set_context (element, context);
3596 }
3597
3598 /**
3599 * gst_element_get_contexts:
3600 * @element: a #GstElement to set the context of.
3601 *
3602 * Gets the contexts set on the element.
3603 *
3604 * MT safe.
3605 *
3606 * Returns: (element-type Gst.Context) (transfer full): List of #GstContext
3607 *
3608 * Since: 1.8
3609 */
3610 GList *
gst_element_get_contexts(GstElement * element)3611 gst_element_get_contexts (GstElement * element)
3612 {
3613 GList *ret;
3614
3615 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3616
3617 GST_OBJECT_LOCK (element);
3618 ret = g_list_copy_deep (element->contexts, (GCopyFunc) gst_context_ref, NULL);
3619 GST_OBJECT_UNLOCK (element);
3620
3621 return ret;
3622 }
3623
3624 static gint
_match_context_type(GstContext * c1,const gchar * context_type)3625 _match_context_type (GstContext * c1, const gchar * context_type)
3626 {
3627 const gchar *c1_type;
3628
3629 c1_type = gst_context_get_context_type (c1);
3630
3631 return g_strcmp0 (c1_type, context_type);
3632 }
3633
3634 /**
3635 * gst_element_get_context_unlocked:
3636 * @element: a #GstElement to get the context of.
3637 * @context_type: a name of a context to retrieve
3638 *
3639 * Gets the context with @context_type set on the element or NULL.
3640 *
3641 * Returns: (transfer full) (nullable): A #GstContext or NULL
3642 *
3643 * Since: 1.8
3644 */
3645 GstContext *
gst_element_get_context_unlocked(GstElement * element,const gchar * context_type)3646 gst_element_get_context_unlocked (GstElement * element,
3647 const gchar * context_type)
3648 {
3649 GstContext *ret = NULL;
3650 GList *node;
3651
3652 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3653
3654 node =
3655 g_list_find_custom (element->contexts, context_type,
3656 (GCompareFunc) _match_context_type);
3657 if (node && node->data)
3658 ret = gst_context_ref (node->data);
3659
3660 return ret;
3661 }
3662
3663 /**
3664 * gst_element_get_context:
3665 * @element: a #GstElement to get the context of.
3666 * @context_type: a name of a context to retrieve
3667 *
3668 * Gets the context with @context_type set on the element or NULL.
3669 *
3670 * MT safe.
3671 *
3672 * Returns: (transfer full) (nullable): A #GstContext or NULL
3673 *
3674 * Since: 1.8
3675 */
3676 GstContext *
gst_element_get_context(GstElement * element,const gchar * context_type)3677 gst_element_get_context (GstElement * element, const gchar * context_type)
3678 {
3679 GstContext *ret = NULL;
3680
3681 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3682
3683 GST_OBJECT_LOCK (element);
3684 ret = gst_element_get_context_unlocked (element, context_type);
3685 GST_OBJECT_UNLOCK (element);
3686
3687 return ret;
3688 }
3689
3690 static void
gst_element_property_post_notify_msg(GstElement * element,GObject * obj,GParamSpec * pspec,gboolean include_value)3691 gst_element_property_post_notify_msg (GstElement * element, GObject * obj,
3692 GParamSpec * pspec, gboolean include_value)
3693 {
3694 GValue val = G_VALUE_INIT;
3695 GValue *v;
3696
3697 GST_LOG_OBJECT (element, "property '%s' of object %" GST_PTR_FORMAT " has "
3698 "changed, posting message with%s value", pspec->name, obj,
3699 include_value ? "" : "out");
3700
3701 if (include_value && (pspec->flags & G_PARAM_READABLE) != 0) {
3702 g_value_init (&val, pspec->value_type);
3703 g_object_get_property (obj, pspec->name, &val);
3704 v = &val;
3705 } else {
3706 v = NULL;
3707 }
3708 gst_element_post_message (element,
3709 gst_message_new_property_notify (GST_OBJECT_CAST (obj), pspec->name, v));
3710 }
3711
3712 static void
gst_element_property_deep_notify_cb(GstElement * element,GObject * prop_obj,GParamSpec * pspec,gpointer user_data)3713 gst_element_property_deep_notify_cb (GstElement * element, GObject * prop_obj,
3714 GParamSpec * pspec, gpointer user_data)
3715 {
3716 gboolean include_value = GPOINTER_TO_INT (user_data);
3717
3718 gst_element_property_post_notify_msg (element, prop_obj, pspec,
3719 include_value);
3720 }
3721
3722 static void
gst_element_property_notify_cb(GObject * obj,GParamSpec * pspec,gpointer user_data)3723 gst_element_property_notify_cb (GObject * obj, GParamSpec * pspec,
3724 gpointer user_data)
3725 {
3726 gboolean include_value = GPOINTER_TO_INT (user_data);
3727
3728 gst_element_property_post_notify_msg (GST_ELEMENT_CAST (obj), obj, pspec,
3729 include_value);
3730 }
3731
3732 /**
3733 * gst_element_add_property_notify_watch:
3734 * @element: a #GstElement to watch for property changes
3735 * @property_name: (allow-none): name of property to watch for changes, or
3736 * NULL to watch all properties
3737 * @include_value: whether to include the new property value in the message
3738 *
3739 * Returns: a watch id, which can be used in connection with
3740 * gst_element_remove_property_notify_watch() to remove the watch again.
3741 *
3742 * Since: 1.10
3743 */
3744 gulong
gst_element_add_property_notify_watch(GstElement * element,const gchar * property_name,gboolean include_value)3745 gst_element_add_property_notify_watch (GstElement * element,
3746 const gchar * property_name, gboolean include_value)
3747 {
3748 const gchar *sep;
3749 gchar *signal_name;
3750 gulong id;
3751
3752 g_return_val_if_fail (GST_IS_ELEMENT (element), 0);
3753
3754 sep = (property_name != NULL) ? "::" : NULL;
3755 signal_name = g_strconcat ("notify", sep, property_name, NULL);
3756 id = g_signal_connect (element, signal_name,
3757 G_CALLBACK (gst_element_property_notify_cb),
3758 GINT_TO_POINTER (include_value));
3759 g_free (signal_name);
3760
3761 return id;
3762 }
3763
3764 /**
3765 * gst_element_add_property_deep_notify_watch:
3766 * @element: a #GstElement to watch (recursively) for property changes
3767 * @property_name: (allow-none): name of property to watch for changes, or
3768 * NULL to watch all properties
3769 * @include_value: whether to include the new property value in the message
3770 *
3771 * Returns: a watch id, which can be used in connection with
3772 * gst_element_remove_property_notify_watch() to remove the watch again.
3773 *
3774 * Since: 1.10
3775 */
3776 gulong
gst_element_add_property_deep_notify_watch(GstElement * element,const gchar * property_name,gboolean include_value)3777 gst_element_add_property_deep_notify_watch (GstElement * element,
3778 const gchar * property_name, gboolean include_value)
3779 {
3780 const gchar *sep;
3781 gchar *signal_name;
3782 gulong id;
3783
3784 g_return_val_if_fail (GST_IS_ELEMENT (element), 0);
3785
3786 sep = (property_name != NULL) ? "::" : NULL;
3787 signal_name = g_strconcat ("deep-notify", sep, property_name, NULL);
3788 id = g_signal_connect (element, signal_name,
3789 G_CALLBACK (gst_element_property_deep_notify_cb),
3790 GINT_TO_POINTER (include_value));
3791 g_free (signal_name);
3792
3793 return id;
3794 }
3795
3796 /**
3797 * gst_element_remove_property_notify_watch:
3798 * @element: a #GstElement being watched for property changes
3799 * @watch_id: watch id to remove
3800 *
3801 * Since: 1.10
3802 */
3803 void
gst_element_remove_property_notify_watch(GstElement * element,gulong watch_id)3804 gst_element_remove_property_notify_watch (GstElement * element, gulong watch_id)
3805 {
3806 g_signal_handler_disconnect (element, watch_id);
3807 }
3808
3809 typedef struct
3810 {
3811 GstElement *element;
3812 GstElementCallAsyncFunc func;
3813 gpointer user_data;
3814 GDestroyNotify destroy_notify;
3815 } GstElementCallAsyncData;
3816
3817 static void
gst_element_call_async_func(gpointer data,gpointer user_data)3818 gst_element_call_async_func (gpointer data, gpointer user_data)
3819 {
3820 GstElementCallAsyncData *async_data = data;
3821
3822 async_data->func (async_data->element, async_data->user_data);
3823 if (async_data->destroy_notify)
3824 async_data->destroy_notify (async_data->user_data);
3825 gst_object_unref (async_data->element);
3826 g_free (async_data);
3827 }
3828
3829 /**
3830 * gst_element_call_async:
3831 * @element: a #GstElement
3832 * @func: Function to call asynchronously from another thread
3833 * @user_data: Data to pass to @func
3834 * @destroy_notify: GDestroyNotify for @user_data
3835 *
3836 * Calls @func from another thread and passes @user_data to it. This is to be
3837 * used for cases when a state change has to be performed from a streaming
3838 * thread, directly via gst_element_set_state() or indirectly e.g. via SEEK
3839 * events.
3840 *
3841 * Calling those functions directly from the streaming thread will cause
3842 * deadlocks in many situations, as they might involve waiting for the
3843 * streaming thread to shut down from this very streaming thread.
3844 *
3845 * MT safe.
3846 *
3847 * Since: 1.10
3848 */
3849 void
gst_element_call_async(GstElement * element,GstElementCallAsyncFunc func,gpointer user_data,GDestroyNotify destroy_notify)3850 gst_element_call_async (GstElement * element, GstElementCallAsyncFunc func,
3851 gpointer user_data, GDestroyNotify destroy_notify)
3852 {
3853 GstElementCallAsyncData *async_data;
3854
3855 g_return_if_fail (GST_IS_ELEMENT (element));
3856
3857 async_data = g_new0 (GstElementCallAsyncData, 1);
3858 async_data->element = gst_object_ref (element);
3859 async_data->func = func;
3860 async_data->user_data = user_data;
3861 async_data->destroy_notify = destroy_notify;
3862
3863 g_mutex_lock (&_element_pool_lock);
3864 if (G_UNLIKELY (gst_element_pool == NULL))
3865 gst_element_pool = gst_element_setup_thread_pool ();
3866 g_thread_pool_push ((GThreadPool *) gst_element_pool, async_data, NULL);
3867 g_mutex_unlock (&_element_pool_lock);
3868 }
3869
3870 void
_priv_gst_element_cleanup(void)3871 _priv_gst_element_cleanup (void)
3872 {
3873 g_mutex_lock (&_element_pool_lock);
3874 if (gst_element_pool) {
3875 g_thread_pool_free ((GThreadPool *) gst_element_pool, FALSE, TRUE);
3876 gst_element_pool = NULL;
3877 }
3878 g_mutex_unlock (&_element_pool_lock);
3879 }
3880
3881 /**
3882 * gst_make_element_message_details:
3883 * @name: Name of the first field to set
3884 * @...: variable arguments in the same form as #GstStructure
3885 *
3886 * Create a #GstStructure to be used with #gst_element_message_full_with_details.
3887 * %NULL terminator required.
3888 *
3889 * Since: 1.10
3890 */
3891 GstStructure *
gst_make_element_message_details(const char * name,...)3892 gst_make_element_message_details (const char *name, ...)
3893 {
3894 GstStructure *structure;
3895 va_list varargs;
3896
3897 if (name == NULL)
3898 return NULL;
3899
3900 va_start (varargs, name);
3901 structure = gst_structure_new_valist ("details", name, varargs);
3902 va_end (varargs);
3903
3904 return structure;
3905 }
3906