2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstoptimalscheduler.c: Default scheduling code for most cases
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
29 GST_DEBUG_CATEGORY_STATIC (debug_scheduler);
30 #define GST_CAT_DEFAULT debug_scheduler
33 # include "cothreads_compat.h"
35 # define COTHREADS_NAME_CAPITAL ""
36 # define COTHREADS_NAME ""
39 #define GST_ELEMENT_SCHED_CONTEXT(elem) ((GstOptSchedulerCtx*) (GST_ELEMENT (elem)->sched_private))
40 #define GST_ELEMENT_SCHED_GROUP(elem) (GST_ELEMENT_SCHED_CONTEXT (elem)->group)
41 /* need this first macro to not run into lvalue casts */
42 #define GST_PAD_DATAPEN(pad) (GST_REAL_PAD(pad)->sched_private)
43 #define GST_PAD_DATALIST(pad) ((GList*) GST_PAD_DATAPEN(pad))
45 #define GST_ELEMENT_COTHREAD_STOPPING GST_ELEMENT_SCHEDULER_PRIVATE1
46 #define GST_ELEMENT_IS_COTHREAD_STOPPING(element) GST_FLAG_IS_SET((element), GST_ELEMENT_COTHREAD_STOPPING)
47 #define GST_ELEMENT_VISITED GST_ELEMENT_SCHEDULER_PRIVATE2
48 #define GST_ELEMENT_IS_VISITED(element) GST_FLAG_IS_SET((element), GST_ELEMENT_VISITED)
49 #define GST_ELEMENT_SET_VISITED(element) GST_FLAG_SET((element), GST_ELEMENT_VISITED)
50 #define GST_ELEMENT_UNSET_VISITED(element) GST_FLAG_UNSET((element), GST_ELEMENT_VISITED)
52 typedef struct _GstOptScheduler GstOptScheduler;
53 typedef struct _GstOptSchedulerClass GstOptSchedulerClass;
55 #define GST_TYPE_OPT_SCHEDULER \
56 (gst_opt_scheduler_get_type())
57 #define GST_OPT_SCHEDULER(obj) \
58 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OPT_SCHEDULER,GstOptScheduler))
59 #define GST_OPT_SCHEDULER_CLASS(klass) \
60 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OPT_SCHEDULER,GstOptSchedulerClass))
61 #define GST_IS_OPT_SCHEDULER(obj) \
62 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OPT_SCHEDULER))
63 #define GST_IS_OPT_SCHEDULER_CLASS(obj) \
64 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OPT_SCHEDULER))
68 GST_OPT_SCHEDULER_STATE_NONE,
69 GST_OPT_SCHEDULER_STATE_STOPPED,
70 GST_OPT_SCHEDULER_STATE_ERROR,
71 GST_OPT_SCHEDULER_STATE_RUNNING,
72 GST_OPT_SCHEDULER_STATE_INTERRUPTED
76 #define GST_OPT_LOCK(sched) (g_static_rec_mutex_lock (&((GstOptScheduler *)sched)->lock))
77 #define GST_OPT_UNLOCK(sched) (g_static_rec_mutex_unlock (&((GstOptScheduler *)sched)->lock))
79 struct _GstOptScheduler
85 GstOptSchedulerState state;
88 cothread_context *context;
104 struct _GstOptSchedulerClass
106 GstSchedulerClass parent_class;
109 static GType _gst_opt_scheduler_type = 0;
113 GST_OPT_SCHEDULER_CHAIN_DIRTY = (1 << 1),
114 GST_OPT_SCHEDULER_CHAIN_DISABLED = (1 << 2),
115 GST_OPT_SCHEDULER_CHAIN_RUNNING = (1 << 3)
117 GstOptSchedulerChainFlags;
119 #define GST_OPT_SCHEDULER_CHAIN_SET_DIRTY(chain) ((chain)->flags |= GST_OPT_SCHEDULER_CHAIN_DIRTY)
120 #define GST_OPT_SCHEDULER_CHAIN_SET_CLEAN(chain) ((chain)->flags &= ~GST_OPT_SCHEDULER_CHAIN_DIRTY)
121 #define GST_OPT_SCHEDULER_CHAIN_IS_DIRTY(chain) ((chain)->flags & GST_OPT_SCHEDULER_CHAIN_DIRTY)
123 #define GST_OPT_SCHEDULER_CHAIN_DISABLE(chain) ((chain)->flags |= GST_OPT_SCHEDULER_CHAIN_DISABLED)
124 #define GST_OPT_SCHEDULER_CHAIN_ENABLE(chain) ((chain)->flags &= ~GST_OPT_SCHEDULER_CHAIN_DISABLED)
125 #define GST_OPT_SCHEDULER_CHAIN_IS_DISABLED(chain) ((chain)->flags & GST_OPT_SCHEDULER_CHAIN_DISABLED)
127 typedef struct _GstOptSchedulerChain GstOptSchedulerChain;
129 struct _GstOptSchedulerChain
133 GstOptScheduler *sched;
135 GstOptSchedulerChainFlags flags;
137 GSList *groups; /* the groups in this chain */
143 * elements that are scheduled in one cothread
147 GST_OPT_SCHEDULER_GROUP_DIRTY = (1 << 1), /* this group has been modified */
148 GST_OPT_SCHEDULER_GROUP_COTHREAD_STOPPING = (1 << 2), /* the group's cothread stops after one iteration */
149 GST_OPT_SCHEDULER_GROUP_DISABLED = (1 << 3), /* this group is disabled */
150 GST_OPT_SCHEDULER_GROUP_RUNNING = (1 << 4), /* this group is running */
151 GST_OPT_SCHEDULER_GROUP_SCHEDULABLE = (1 << 5), /* this group is schedulable */
152 GST_OPT_SCHEDULER_GROUP_VISITED = (1 << 6) /* this group is visited when finding links */
154 GstOptSchedulerGroupFlags;
158 GST_OPT_SCHEDULER_GROUP_UNKNOWN = 3,
159 GST_OPT_SCHEDULER_GROUP_GET = 1,
160 GST_OPT_SCHEDULER_GROUP_LOOP = 2
162 GstOptSchedulerGroupType;
164 #define GST_OPT_SCHEDULER_GROUP_SET_FLAG(group,flag) ((group)->flags |= (flag))
165 #define GST_OPT_SCHEDULER_GROUP_UNSET_FLAG(group,flag) ((group)->flags &= ~(flag))
166 #define GST_OPT_SCHEDULER_GROUP_IS_FLAG_SET(group,flag) ((group)->flags & (flag))
168 #define GST_OPT_SCHEDULER_GROUP_DISABLE(group) ((group)->flags |= GST_OPT_SCHEDULER_GROUP_DISABLED)
169 #define GST_OPT_SCHEDULER_GROUP_ENABLE(group) ((group)->flags &= ~GST_OPT_SCHEDULER_GROUP_DISABLED)
170 #define GST_OPT_SCHEDULER_GROUP_IS_ENABLED(group) (!((group)->flags & GST_OPT_SCHEDULER_GROUP_DISABLED))
171 #define GST_OPT_SCHEDULER_GROUP_IS_DISABLED(group) ((group)->flags & GST_OPT_SCHEDULER_GROUP_DISABLED)
174 typedef struct _GstOptSchedulerGroup GstOptSchedulerGroup;
175 typedef struct _GstOptSchedulerGroupLink GstOptSchedulerGroupLink;
177 /* used to keep track of links with other groups */
178 struct _GstOptSchedulerGroupLink
180 GstOptSchedulerGroup *src; /* the group we are linked with */
181 GstOptSchedulerGroup *sink; /* the group we are linked with */
182 gint count; /* the number of links with the group */
185 #define IS_GROUP_LINK(link, srcg, sinkg) ((link->src == srcg && link->sink == sinkg) || \
186 (link->sink == srcg && link->src == sinkg))
187 #define OTHER_GROUP_LINK(link, group) (link->src == group ? link->sink : link->src)
189 typedef int (*GroupScheduleFunction) (int argc, char *argv[]);
191 struct _GstOptSchedulerGroup
193 GstOptSchedulerChain *chain; /* the chain this group belongs to */
194 GstOptSchedulerGroupFlags flags; /* flags for this group */
195 GstOptSchedulerGroupType type; /* flags for this group */
196 GstOptScheduler *sched; /* the scheduler */
200 GSList *elements; /* elements of this group */
203 GstElement *entry; /* the group's entry point */
205 GSList *group_links; /* other groups that are linked with this group */
208 cothread *cothread; /* the cothread of this group */
210 GroupScheduleFunction schedulefunc;
217 * A group is a set of elements through which data can flow without switching
218 * cothreads or without invoking the scheduler's run queue.
220 static GstOptSchedulerGroup *ref_group (GstOptSchedulerGroup * group);
221 static GstOptSchedulerGroup *unref_group (GstOptSchedulerGroup * group);
222 static GstOptSchedulerGroup *create_group (GstOptSchedulerChain * chain,
223 GstElement * element, GstOptSchedulerGroupType type);
224 static void destroy_group (GstOptSchedulerGroup * group);
225 static GstOptSchedulerGroup *add_to_group (GstOptSchedulerGroup * group,
226 GstElement * element, gboolean with_links);
227 static GstOptSchedulerGroup *remove_from_group (GstOptSchedulerGroup * group,
228 GstElement * element);
229 static void group_dec_links_for_element (GstOptSchedulerGroup * group,
230 GstElement * element);
231 static void group_inc_links_for_element (GstOptSchedulerGroup * group,
232 GstElement * element);
233 static GstOptSchedulerGroup *merge_groups (GstOptSchedulerGroup * group1,
234 GstOptSchedulerGroup * group2);
235 static void setup_group_scheduler (GstOptScheduler * osched,
236 GstOptSchedulerGroup * group);
237 static void destroy_group_scheduler (GstOptSchedulerGroup * group);
238 static void group_error_handler (GstOptSchedulerGroup * group);
239 static void group_element_set_enabled (GstOptSchedulerGroup * group,
240 GstElement * element, gboolean enabled);
241 static gboolean schedule_group (GstOptSchedulerGroup * group);
242 static void get_group (GstElement * element, GstOptSchedulerGroup ** group);
246 * A chain is a set of groups that are linked to each other.
248 static void destroy_chain (GstOptSchedulerChain * chain);
249 static GstOptSchedulerChain *create_chain (GstOptScheduler * osched);
250 static GstOptSchedulerChain *ref_chain (GstOptSchedulerChain * chain);
251 static GstOptSchedulerChain *unref_chain (GstOptSchedulerChain * chain);
252 static GstOptSchedulerChain *add_to_chain (GstOptSchedulerChain * chain,
253 GstOptSchedulerGroup * group);
254 static GstOptSchedulerChain *remove_from_chain (GstOptSchedulerChain * chain,
255 GstOptSchedulerGroup * group);
256 static GstOptSchedulerChain *merge_chains (GstOptSchedulerChain * chain1,
257 GstOptSchedulerChain * chain2);
258 static void chain_recursively_migrate_group (GstOptSchedulerChain * chain,
259 GstOptSchedulerGroup * group);
260 static void chain_group_set_enabled (GstOptSchedulerChain * chain,
261 GstOptSchedulerGroup * group, gboolean enabled);
262 static gboolean schedule_chain (GstOptSchedulerChain * chain);
266 * The schedule functions are the entry points for cothreads, or called directly
267 * by gst_opt_scheduler_schedule_run_queue
269 static int get_group_schedule_function (int argc, char *argv[]);
270 static int loop_group_schedule_function (int argc, char *argv[]);
271 static int unknown_group_schedule_function (int argc, char *argv[]);
275 * These wrappers are set on the pads as the chain handler (what happens when
276 * gst_pad_push is called) or get handler (for gst_pad_pull).
278 static void gst_opt_scheduler_loop_wrapper (GstPad * sinkpad, GstData * data);
279 static GstData *gst_opt_scheduler_get_wrapper (GstPad * srcpad);
283 * Without cothreads, gst_pad_push or gst_pad_pull on a loop-based group will
284 * just queue the peer element on a list. We need to actually run the queue
285 * instead of relying on cothreads to do the switch for us.
287 #ifndef USE_COTHREADS
288 static void gst_opt_scheduler_schedule_run_queue (GstOptScheduler * osched,
289 GstOptSchedulerGroup * only_group);
294 * Scheduler private data for an element
296 typedef struct _GstOptSchedulerCtx GstOptSchedulerCtx;
300 GST_OPT_SCHEDULER_CTX_DISABLED = (1 << 1) /* the element is disabled */
302 GstOptSchedulerCtxFlags;
304 struct _GstOptSchedulerCtx
306 GstOptSchedulerGroup *group; /* the group this element belongs to */
308 GstOptSchedulerCtxFlags flags; /* flags for this element */
313 * Implementation of GstScheduler
322 static void gst_opt_scheduler_class_init (GstOptSchedulerClass * klass);
323 static void gst_opt_scheduler_init (GstOptScheduler * scheduler);
325 static void gst_opt_scheduler_set_property (GObject * object, guint prop_id,
326 const GValue * value, GParamSpec * pspec);
327 static void gst_opt_scheduler_get_property (GObject * object, guint prop_id,
328 GValue * value, GParamSpec * pspec);
330 static void gst_opt_scheduler_dispose (GObject * object);
331 static void gst_opt_scheduler_finalize (GObject * object);
333 static void gst_opt_scheduler_setup (GstScheduler * sched);
334 static void gst_opt_scheduler_reset (GstScheduler * sched);
335 static void gst_opt_scheduler_add_element (GstScheduler * sched,
336 GstElement * element);
337 static void gst_opt_scheduler_remove_element (GstScheduler * sched,
338 GstElement * element);
339 static GstElementStateReturn gst_opt_scheduler_state_transition (GstScheduler *
340 sched, GstElement * element, gint transition);
341 static void gst_opt_scheduler_scheduling_change (GstScheduler * sched,
342 GstElement * element);
343 static gboolean gst_opt_scheduler_yield (GstScheduler * sched,
344 GstElement * element);
345 static gboolean gst_opt_scheduler_interrupt (GstScheduler * sched,
346 GstElement * element);
347 static void gst_opt_scheduler_error (GstScheduler * sched,
348 GstElement * element);
349 static void gst_opt_scheduler_pad_link (GstScheduler * sched, GstPad * srcpad,
351 static void gst_opt_scheduler_pad_unlink (GstScheduler * sched, GstPad * srcpad,
353 static GstSchedulerState gst_opt_scheduler_iterate (GstScheduler * sched);
355 static void gst_opt_scheduler_show (GstScheduler * sched);
357 static GstSchedulerClass *parent_class = NULL;
360 gst_opt_scheduler_get_type (void)
362 if (!_gst_opt_scheduler_type) {
363 static const GTypeInfo scheduler_info = {
364 sizeof (GstOptSchedulerClass),
367 (GClassInitFunc) gst_opt_scheduler_class_init,
370 sizeof (GstOptScheduler),
372 (GInstanceInitFunc) gst_opt_scheduler_init,
376 _gst_opt_scheduler_type = g_type_register_static (GST_TYPE_SCHEDULER,
377 "GstOpt" COTHREADS_NAME_CAPITAL "Scheduler", &scheduler_info, 0);
379 return _gst_opt_scheduler_type;
383 gst_opt_scheduler_class_init (GstOptSchedulerClass * klass)
385 GObjectClass *gobject_class;
386 GstObjectClass *gstobject_class;
387 GstSchedulerClass *gstscheduler_class;
389 gobject_class = (GObjectClass *) klass;
390 gstobject_class = (GstObjectClass *) klass;
391 gstscheduler_class = (GstSchedulerClass *) klass;
393 parent_class = g_type_class_ref (GST_TYPE_SCHEDULER);
395 gobject_class->set_property =
396 GST_DEBUG_FUNCPTR (gst_opt_scheduler_set_property);
397 gobject_class->get_property =
398 GST_DEBUG_FUNCPTR (gst_opt_scheduler_get_property);
399 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_opt_scheduler_dispose);
400 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_opt_scheduler_finalize);
402 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ITERATIONS,
403 g_param_spec_int ("iterations", "Iterations",
404 "Number of groups to schedule in one iteration (-1 == until EOS/error)",
405 -1, G_MAXINT, 1, G_PARAM_READWRITE));
406 #ifndef USE_COTHREADS
407 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_RECURSION,
408 g_param_spec_int ("max_recursion", "Max recursion",
409 "Maximum number of recursions", 1, G_MAXINT, 100, G_PARAM_READWRITE));
412 gstscheduler_class->setup = GST_DEBUG_FUNCPTR (gst_opt_scheduler_setup);
413 gstscheduler_class->reset = GST_DEBUG_FUNCPTR (gst_opt_scheduler_reset);
414 gstscheduler_class->add_element =
415 GST_DEBUG_FUNCPTR (gst_opt_scheduler_add_element);
416 gstscheduler_class->remove_element =
417 GST_DEBUG_FUNCPTR (gst_opt_scheduler_remove_element);
418 gstscheduler_class->state_transition =
419 GST_DEBUG_FUNCPTR (gst_opt_scheduler_state_transition);
420 gstscheduler_class->scheduling_change =
421 GST_DEBUG_FUNCPTR (gst_opt_scheduler_scheduling_change);
422 gstscheduler_class->yield = GST_DEBUG_FUNCPTR (gst_opt_scheduler_yield);
423 gstscheduler_class->interrupt =
424 GST_DEBUG_FUNCPTR (gst_opt_scheduler_interrupt);
425 gstscheduler_class->error = GST_DEBUG_FUNCPTR (gst_opt_scheduler_error);
426 gstscheduler_class->pad_link = GST_DEBUG_FUNCPTR (gst_opt_scheduler_pad_link);
427 gstscheduler_class->pad_unlink =
428 GST_DEBUG_FUNCPTR (gst_opt_scheduler_pad_unlink);
429 gstscheduler_class->clock_wait = NULL;
430 gstscheduler_class->iterate = GST_DEBUG_FUNCPTR (gst_opt_scheduler_iterate);
431 gstscheduler_class->show = GST_DEBUG_FUNCPTR (gst_opt_scheduler_show);
434 do_cothreads_init (NULL);
439 gst_opt_scheduler_init (GstOptScheduler * scheduler)
441 g_static_rec_mutex_init (&scheduler->lock);
443 scheduler->elements = NULL;
444 scheduler->iterations = 1;
445 scheduler->max_recursion = 100;
446 scheduler->live_groups = 0;
447 scheduler->live_chains = 0;
448 scheduler->live_links = 0;
452 gst_opt_scheduler_dispose (GObject * object)
454 G_OBJECT_CLASS (parent_class)->dispose (object);
458 gst_opt_scheduler_finalize (GObject * object)
460 GstOptScheduler *osched = GST_OPT_SCHEDULER (object);
462 g_static_rec_mutex_free (&osched->lock);
464 G_OBJECT_CLASS (parent_class)->finalize (object);
468 plugin_init (GstPlugin * plugin)
471 if (!gst_scheduler_register (plugin, "opt" COTHREADS_NAME,
472 "An optimal scheduler using " COTHREADS_NAME " cothreads",
473 gst_opt_scheduler_get_type ()))
475 if (!gst_scheduler_register (plugin, "opt",
476 "An optimal scheduler using no cothreads",
477 gst_opt_scheduler_get_type ()))
481 GST_DEBUG_CATEGORY_INIT (debug_scheduler, "scheduler", 0,
482 "optimal scheduler");
487 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
489 "gstopt" COTHREADS_NAME "scheduler",
490 "An optimal scheduler using " COTHREADS_NAME " cothreads",
491 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN);
494 static GstOptSchedulerChain *
495 ref_chain (GstOptSchedulerChain * chain)
497 GST_LOG ("ref chain %p %d->%d", chain, chain->refcount, chain->refcount + 1);
503 static GstOptSchedulerChain *
504 unref_chain (GstOptSchedulerChain * chain)
506 GST_LOG ("unref chain %p %d->%d", chain,
507 chain->refcount, chain->refcount - 1);
509 if (--chain->refcount == 0) {
510 destroy_chain (chain);
517 static GstOptSchedulerChain *
518 create_chain (GstOptScheduler * osched)
520 GstOptSchedulerChain *chain;
522 chain = g_new0 (GstOptSchedulerChain, 1);
523 chain->sched = osched;
525 chain->flags = GST_OPT_SCHEDULER_CHAIN_DISABLED;
526 osched->live_chains++;
528 gst_object_ref (GST_OBJECT (osched));
529 osched->chains = g_slist_prepend (osched->chains, chain);
531 GST_LOG ("new chain %p, %d live chains now", chain, osched->live_chains);
537 destroy_chain (GstOptSchedulerChain * chain)
539 GstOptScheduler *osched;
541 GST_LOG ("destroy chain %p", chain);
543 g_assert (chain->num_groups == 0);
544 g_assert (chain->groups == NULL);
546 osched = chain->sched;
547 osched->chains = g_slist_remove (osched->chains, chain);
548 osched->live_chains--;
550 GST_LOG ("%d live chains now", osched->live_chains);
552 gst_object_unref (GST_OBJECT (osched));
557 static GstOptSchedulerChain *
558 add_to_chain (GstOptSchedulerChain * chain, GstOptSchedulerGroup * group)
562 GST_LOG ("adding group %p to chain %p", group, chain);
564 g_assert (group->chain == NULL);
566 group = ref_group (group);
568 group->chain = ref_chain (chain);
569 chain->groups = g_slist_prepend (chain->groups, group);
572 enabled = GST_OPT_SCHEDULER_GROUP_IS_ENABLED (group);
575 /* we can now setup the scheduling of the group */
576 setup_group_scheduler (chain->sched, group);
578 chain->num_enabled++;
579 if (chain->num_enabled == chain->num_groups) {
580 GST_LOG ("enabling chain %p after adding of enabled group", chain);
581 GST_OPT_SCHEDULER_CHAIN_ENABLE (chain);
585 /* queue a resort of the group list, which determines which group will be run
587 GST_OPT_SCHEDULER_CHAIN_SET_DIRTY (chain);
592 static GstOptSchedulerChain *
593 remove_from_chain (GstOptSchedulerChain * chain, GstOptSchedulerGroup * group)
597 GST_LOG ("removing group %p from chain %p", group, chain);
603 g_assert (group->chain == chain);
605 enabled = GST_OPT_SCHEDULER_GROUP_IS_ENABLED (group);
608 chain->groups = g_slist_remove (chain->groups, group);
612 if (chain->num_groups == 0)
613 chain = unref_chain (chain);
615 /* removing an enabled group from the chain decrements the
618 chain->num_enabled--;
619 if (chain->num_enabled == 0) {
620 GST_LOG ("disabling chain %p after removal of the only enabled group",
622 GST_OPT_SCHEDULER_CHAIN_DISABLE (chain);
625 if (chain->num_enabled == chain->num_groups) {
626 GST_LOG ("enabling chain %p after removal of the only disabled group",
628 GST_OPT_SCHEDULER_CHAIN_ENABLE (chain);
633 GST_OPT_SCHEDULER_CHAIN_SET_DIRTY (chain);
635 chain = unref_chain (chain);
639 static GstOptSchedulerChain *
640 merge_chains (GstOptSchedulerChain * chain1, GstOptSchedulerChain * chain2)
644 g_assert (chain1 != NULL);
646 GST_LOG ("merging chain %p and %p", chain1, chain2);
648 /* FIXME: document how chain2 can be NULL */
649 if (chain1 == chain2 || chain2 == NULL)
652 /* switch if it's more efficient */
653 if (chain1->num_groups < chain2->num_groups) {
654 GstOptSchedulerChain *tmp = chain2;
660 walk = chain2->groups;
662 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) walk->data;
664 walk = g_slist_next (walk);
666 GST_LOG ("reparenting group %p from chain %p to %p", group, chain2, chain1);
670 remove_from_chain (chain2, group);
671 add_to_chain (chain1, group);
676 /* chain2 is now freed, if nothing else was referencing it before */
681 /* sorts the group list so that terminal sinks come first -- prevents pileup of
682 * datas in datapens */
684 sort_chain (GstOptSchedulerChain * chain)
686 GSList *original = chain->groups;
688 GSList *walk, *links, *this;
690 /* if there's only one group, just return */
693 /* otherwise, we know that all groups are somehow linked together */
695 GST_LOG ("sorting chain %p (%d groups)", chain, g_slist_length (original));
697 /* first find the terminal sinks */
698 for (walk = original; walk;) {
699 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) walk->data;
703 if (group->group_links) {
704 gboolean is_sink = TRUE;
706 for (links = group->group_links; links; links = links->next)
707 if (((GstOptSchedulerGroupLink *) links->data)->src == group)
711 original = g_slist_remove_link (original, this);
712 new = g_slist_concat (new, this);
716 g_assert (new != NULL);
718 /* now look for the elements that are linked to the terminal sinks */
719 for (walk = new; walk; walk = walk->next) {
720 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) walk->data;
722 for (links = group->group_links; links; links = links->next) {
724 g_slist_find (original,
725 ((GstOptSchedulerGroupLink *) links->data)->src);
727 original = g_slist_remove_link (original, this);
728 new = g_slist_concat (new, this);
732 g_assert (original == NULL);
738 chain_group_set_enabled (GstOptSchedulerChain * chain,
739 GstOptSchedulerGroup * group, gboolean enabled)
743 g_assert (group != NULL);
744 g_assert (chain != NULL);
747 ("request to %d group %p in chain %p, have %d groups enabled out of %d",
748 enabled, group, chain, chain->num_enabled, chain->num_groups);
750 oldstate = (GST_OPT_SCHEDULER_GROUP_IS_ENABLED (group) ? TRUE : FALSE);
751 if (oldstate == enabled) {
752 GST_LOG ("group %p in chain %p was in correct state", group, chain);
757 GST_OPT_SCHEDULER_GROUP_ENABLE (group);
759 GST_OPT_SCHEDULER_GROUP_DISABLE (group);
762 g_assert (chain->num_enabled < chain->num_groups);
764 chain->num_enabled++;
766 GST_DEBUG ("enable group %p in chain %p, now %d groups enabled out of %d",
767 group, chain, chain->num_enabled, chain->num_groups);
769 /* OK to call even if the scheduler (cothread context / schedulerfunc) was
770 setup already -- will get destroyed when the group is destroyed */
771 setup_group_scheduler (chain->sched, group);
773 if (chain->num_enabled == chain->num_groups) {
774 GST_DEBUG ("enable chain %p", chain);
775 GST_OPT_SCHEDULER_CHAIN_ENABLE (chain);
778 g_assert (chain->num_enabled > 0);
780 chain->num_enabled--;
781 GST_DEBUG ("disable group %p in chain %p, now %d groups enabled out of %d",
782 group, chain, chain->num_enabled, chain->num_groups);
784 if (chain->num_enabled == 0) {
785 GST_DEBUG ("disable chain %p", chain);
786 GST_OPT_SCHEDULER_CHAIN_DISABLE (chain);
791 /* recursively migrate the group and all connected groups into the new chain */
793 chain_recursively_migrate_group (GstOptSchedulerChain * chain,
794 GstOptSchedulerGroup * group)
798 /* group already in chain */
799 if (group->chain == chain)
802 /* first remove the group from its old chain */
803 remove_from_chain (group->chain, group);
804 /* add to new chain */
805 add_to_chain (chain, group);
807 /* then follow all links */
808 for (links = group->group_links; links; links = g_slist_next (links)) {
809 GstOptSchedulerGroupLink *link = (GstOptSchedulerGroupLink *) links->data;
811 chain_recursively_migrate_group (chain, OTHER_GROUP_LINK (link, group));
815 static GstOptSchedulerGroup *
816 ref_group (GstOptSchedulerGroup * group)
818 GST_LOG ("ref group %p %d->%d", group, group->refcount, group->refcount + 1);
825 static GstOptSchedulerGroup *
826 unref_group (GstOptSchedulerGroup * group)
828 GST_LOG ("unref group %p %d->%d", group,
829 group->refcount, group->refcount - 1);
831 if (--group->refcount == 0) {
832 destroy_group (group);
839 static GstOptSchedulerGroup *
840 create_group (GstOptSchedulerChain * chain, GstElement * element,
841 GstOptSchedulerGroupType type)
843 GstOptSchedulerGroup *group;
845 group = g_new0 (GstOptSchedulerGroup, 1);
846 GST_LOG ("new group %p, type %d", group, type);
847 group->refcount = 1; /* float... */
848 group->flags = GST_OPT_SCHEDULER_GROUP_DISABLED;
850 group->sched = chain->sched;
851 group->sched->live_groups++;
853 add_to_group (group, element, FALSE);
854 add_to_chain (chain, group);
855 group = unref_group (group); /* ...and sink. */
857 GST_LOG ("%d live groups now", group->sched->live_groups);
858 /* group's refcount is now 2 (one for the element, one for the chain) */
864 destroy_group (GstOptSchedulerGroup * group)
866 GST_LOG ("destroy group %p", group);
868 g_assert (group != NULL);
869 g_assert (group->elements == NULL);
870 g_assert (group->chain == NULL);
871 g_assert (group->group_links == NULL);
873 if (group->flags & GST_OPT_SCHEDULER_GROUP_SCHEDULABLE)
874 destroy_group_scheduler (group);
876 group->sched->live_groups--;
877 GST_LOG ("%d live groups now", group->sched->live_groups);
882 static GstOptSchedulerGroup *
883 add_to_group (GstOptSchedulerGroup * group, GstElement * element,
886 g_assert (group != NULL);
887 g_assert (element != NULL);
889 GST_DEBUG ("adding element %p \"%s\" to group %p", element,
890 GST_ELEMENT_NAME (element), group);
892 if (GST_ELEMENT_IS_DECOUPLED (element)) {
893 GST_DEBUG ("element \"%s\" is decoupled, not adding to group %p",
894 GST_ELEMENT_NAME (element), group);
898 g_assert (GST_ELEMENT_SCHED_GROUP (element) == NULL);
900 /* first increment the links that this group has with other groups through
903 group_inc_links_for_element (group, element);
905 /* Ref the group... */
906 GST_ELEMENT_SCHED_GROUP (element) = ref_group (group);
908 gst_object_ref (GST_OBJECT (element));
909 group->elements = g_slist_prepend (group->elements, element);
910 group->num_elements++;
912 if (gst_element_get_state (element) == GST_STATE_PLAYING) {
913 group_element_set_enabled (group, element, TRUE);
919 /* we need to remove a decoupled element from the scheduler, this
920 * involves finding all the groups that have this element as an
921 * entry point and disabling them. */
923 remove_decoupled (GstScheduler * sched, GstElement * element)
927 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
929 GST_DEBUG_OBJECT (sched, "removing decoupled element \"%s\"",
930 GST_OBJECT_NAME (element));
932 for (chains = osched->chains; chains; chains = g_slist_next (chains)) {
933 GstOptSchedulerChain *chain = (GstOptSchedulerChain *) chains->data;
936 for (groups = chain->groups; groups; groups = g_slist_next (groups)) {
937 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) groups->data;
940 GST_DEBUG_OBJECT (sched, "group %p, entry %s", group,
941 GST_STR_NULL (GST_OBJECT_NAME (group->entry)));
944 if (group->entry == element) {
945 GST_DEBUG ("clearing element %p \"%s\" as entry from group %p",
946 element, GST_ELEMENT_NAME (element), group);
948 group->type = GST_OPT_SCHEDULER_GROUP_UNKNOWN;
952 /* and remove from any child scheduler */
953 for (schedulers = sched->schedulers; schedulers;
954 schedulers = g_list_next (schedulers)) {
955 remove_decoupled (GST_SCHEDULER (schedulers->data), element);
959 static GstOptSchedulerGroup *
960 remove_from_group (GstOptSchedulerGroup * group, GstElement * element)
962 GST_DEBUG ("removing element %p \"%s\" from group %p",
963 element, GST_ELEMENT_NAME (element), group);
965 g_assert (group != NULL);
966 g_assert (element != NULL);
967 /* this assert also catches the decoupled elements */
968 g_assert (GST_ELEMENT_SCHED_GROUP (element) == group);
970 /* first decrement the links that this group has with other groups through
972 group_dec_links_for_element (group, element);
974 if (gst_element_get_state (element) == GST_STATE_PLAYING) {
975 group_element_set_enabled (group, element, FALSE);
978 group->elements = g_slist_remove (group->elements, element);
979 group->num_elements--;
981 /* if the element was an entry point in the group, clear the group's
982 * entry point, and mark it as unknown */
983 if (group->entry == element) {
984 GST_DEBUG ("clearing element %p \"%s\" as entry from group %p",
985 element, GST_ELEMENT_NAME (element), group);
987 group->type = GST_OPT_SCHEDULER_GROUP_UNKNOWN;
990 GST_ELEMENT_SCHED_GROUP (element) = NULL;
991 gst_object_unref (GST_OBJECT (element));
993 if (group->num_elements == 0) {
994 GST_LOG ("group %p is now empty", group);
995 /* don't know in what case group->chain would be NULL, but putting this here
996 in deference to 0.8 -- remove me in 0.9 */
998 GST_LOG ("removing group %p from its chain", group);
999 chain_group_set_enabled (group->chain, group, FALSE);
1000 remove_from_chain (group->chain, group);
1003 group = unref_group (group);
1008 /* check if an element is part of the given group. We have to be carefull
1009 * as decoupled elements are added as entry but are not added to the elements
1012 group_has_element (GstOptSchedulerGroup * group, GstElement * element)
1014 if (group->entry == element)
1017 return (g_slist_find (group->elements, element) != NULL);
1020 /* FIXME need to check if the groups are of the same type -- otherwise need to
1021 setup the scheduler again, if it is setup */
1022 static GstOptSchedulerGroup *
1023 merge_groups (GstOptSchedulerGroup * group1, GstOptSchedulerGroup * group2)
1025 g_assert (group1 != NULL);
1027 GST_DEBUG ("merging groups %p and %p", group1, group2);
1029 if (group1 == group2 || group2 == NULL)
1032 /* make sure they end up in the same chain */
1033 merge_chains (group1->chain, group2->chain);
1035 while (group2 && group2->elements) {
1036 GstElement *element = (GstElement *) group2->elements->data;
1038 group2 = remove_from_group (group2, element);
1039 add_to_group (group1, element, TRUE);
1045 /* setup the scheduler context for a group. The right schedule function
1046 * is selected based on the group type and cothreads are created if
1049 setup_group_scheduler (GstOptScheduler * osched, GstOptSchedulerGroup * group)
1051 GroupScheduleFunction wrapper;
1053 GST_DEBUG ("setup group %p scheduler, type %d", group, group->type);
1055 wrapper = unknown_group_schedule_function;
1057 /* figure out the wrapper function for this group */
1058 if (group->type == GST_OPT_SCHEDULER_GROUP_GET)
1059 wrapper = get_group_schedule_function;
1060 else if (group->type == GST_OPT_SCHEDULER_GROUP_LOOP)
1061 wrapper = loop_group_schedule_function;
1063 #ifdef USE_COTHREADS
1064 /* we can only initialize the cothread stuff when we have
1065 * a cothread context */
1066 if (osched->context) {
1067 if (!(group->flags & GST_OPT_SCHEDULER_GROUP_SCHEDULABLE)) {
1068 do_cothread_create (group->cothread, osched->context,
1069 (cothread_func) wrapper, 0, (char **) group);
1071 do_cothread_setfunc (group->cothread, osched->context,
1072 (cothread_func) wrapper, 0, (char **) group);
1076 group->schedulefunc = wrapper;
1078 group->argv = (char **) group;
1080 group->flags |= GST_OPT_SCHEDULER_GROUP_SCHEDULABLE;
1084 destroy_group_scheduler (GstOptSchedulerGroup * group)
1088 if (group->flags & GST_OPT_SCHEDULER_GROUP_RUNNING)
1089 g_warning ("destroying running group scheduler");
1091 #ifdef USE_COTHREADS
1092 if (group->cothread) {
1093 do_cothread_destroy (group->cothread);
1094 group->cothread = NULL;
1097 group->schedulefunc = NULL;
1102 group->flags &= ~GST_OPT_SCHEDULER_GROUP_SCHEDULABLE;
1106 group_error_handler (GstOptSchedulerGroup * group)
1108 GST_DEBUG ("group %p has errored", group);
1110 chain_group_set_enabled (group->chain, group, FALSE);
1111 group->chain->sched->state = GST_OPT_SCHEDULER_STATE_ERROR;
1114 /* this function enables/disables an element, it will set/clear a flag on the element
1115 * and tells the chain that the group is enabled if all elements inside the group are
1118 group_element_set_enabled (GstOptSchedulerGroup * group, GstElement * element,
1121 g_assert (group != NULL);
1122 g_assert (element != NULL);
1125 ("request to %d element %s in group %p, have %d elements enabled out of %d",
1126 enabled, GST_ELEMENT_NAME (element), group, group->num_enabled,
1127 group->num_elements);
1129 /* Note that if an unlinked PLAYING element is added to a bin, we have to
1130 create a new group to hold the element, and this function will be called
1131 before the group is added to the chain. Thus we have a valid case for
1132 group->chain==NULL. */
1135 g_assert (group->num_enabled < group->num_elements);
1137 group->num_enabled++;
1140 ("enable element %s in group %p, now %d elements enabled out of %d",
1141 GST_ELEMENT_NAME (element), group, group->num_enabled,
1142 group->num_elements);
1144 if (group->num_enabled == group->num_elements) {
1145 if (!group->chain) {
1146 GST_DEBUG ("enable chainless group %p", group);
1147 GST_OPT_SCHEDULER_GROUP_ENABLE (group);
1149 GST_LOG ("enable group %p", group);
1150 chain_group_set_enabled (group->chain, group, TRUE);
1154 g_assert (group->num_enabled > 0);
1156 group->num_enabled--;
1159 ("disable element %s in group %p, now %d elements enabled out of %d",
1160 GST_ELEMENT_NAME (element), group, group->num_enabled,
1161 group->num_elements);
1163 if (group->num_enabled == 0) {
1164 if (!group->chain) {
1165 GST_DEBUG ("disable chainless group %p", group);
1166 GST_OPT_SCHEDULER_GROUP_DISABLE (group);
1168 GST_LOG ("disable group %p", group);
1169 chain_group_set_enabled (group->chain, group, FALSE);
1175 /* a group is scheduled by doing a cothread switch to it or
1176 * by calling the schedule function. In the non-cothread case
1177 * we cannot run already running groups so we return FALSE here
1178 * to indicate this to the caller */
1180 schedule_group (GstOptSchedulerGroup * group)
1182 if (!group->entry) {
1183 GST_INFO ("not scheduling group %p without entry", group);
1184 /* FIXME, we return true here, while the group is actually
1185 * not schedulable. We might want to disable the element that caused
1186 * this group to be scheduled instead */
1189 #ifdef USE_COTHREADS
1190 if (group->cothread)
1191 do_cothread_switch (group->cothread);
1193 g_warning ("(internal error): trying to schedule group without cothread");
1196 /* cothreads automatically call the pre- and post-run functions for us;
1197 * without cothreads we need to call them manually */
1198 if (group->schedulefunc == NULL) {
1199 GST_INFO ("not scheduling group %p without schedulefunc", group);
1203 GstElement *entry = NULL;
1205 lcopy = g_slist_copy (group->elements);
1206 /* also add entry point, this is made so that decoupled elements
1207 * are also reffed since they are not added to the list of group
1209 if (group->entry && GST_ELEMENT_IS_DECOUPLED (group->entry)) {
1210 entry = group->entry;
1211 gst_object_ref (GST_OBJECT (entry));
1214 for (l = lcopy; l; l = l->next) {
1215 GstElement *e = (GstElement *) l->data;
1217 gst_object_ref (GST_OBJECT (e));
1218 if (e->pre_run_func)
1219 e->pre_run_func (e);
1222 group->schedulefunc (group->argc, group->argv);
1224 for (l = lcopy; l; l = l->next) {
1225 GstElement *e = (GstElement *) l->data;
1227 if (e->post_run_func)
1228 e->post_run_func (e);
1230 gst_object_unref (GST_OBJECT (e));
1233 gst_object_unref (GST_OBJECT (entry));
1234 g_slist_free (lcopy);
1240 #ifndef USE_COTHREADS
1242 gst_opt_scheduler_schedule_run_queue (GstOptScheduler * osched,
1243 GstOptSchedulerGroup * only_group)
1245 GST_LOG_OBJECT (osched, "running queue: %d groups, recursed %d times",
1246 g_list_length (osched->runqueue),
1247 osched->recursion, g_list_length (osched->runqueue));
1249 /* note that we have a ref on each group on the queue (unref after running) */
1251 /* make sure we don't exceed max_recursion */
1252 if (osched->recursion > osched->max_recursion) {
1253 osched->state = GST_OPT_SCHEDULER_STATE_ERROR;
1257 osched->recursion++;
1260 GstOptSchedulerGroup *group;
1266 group = (GstOptSchedulerGroup *) osched->runqueue->data;
1268 /* runqueue holds refcount to group */
1269 osched->runqueue = g_list_remove (osched->runqueue, group);
1271 GST_LOG_OBJECT (osched, "scheduling group %p", group);
1273 if (GST_OPT_SCHEDULER_GROUP_IS_ENABLED (group)) {
1274 res = schedule_group (group);
1276 GST_INFO_OBJECT (osched,
1277 "group was disabled while it was on the queue, not scheduling");
1281 g_warning ("error scheduling group %p", group);
1282 group_error_handler (group);
1284 GST_LOG_OBJECT (osched, "done scheduling group %p", group);
1286 unref_group (group);
1287 } while (osched->runqueue && !only_group);
1289 GST_LOG_OBJECT (osched, "run queue length after scheduling %d",
1290 g_list_length (osched->runqueue));
1292 osched->recursion--;
1296 /* a chain is scheduled by picking the first active group and scheduling it */
1298 schedule_chain (GstOptSchedulerChain * chain)
1301 GstOptScheduler *osched;
1302 gboolean scheduled = FALSE;
1304 osched = chain->sched;
1306 /* if the chain has changed, we need to resort the groups so we enter in the
1308 if (GST_OPT_SCHEDULER_CHAIN_IS_DIRTY (chain))
1310 GST_OPT_SCHEDULER_CHAIN_SET_CLEAN (chain);
1312 /* since the lock on the group list is only released when we schedule
1313 * a group and since we only schedule one group, we don't need to
1314 * worry about the list getting corrupted. */
1315 groups = chain->groups;
1317 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) groups->data;
1319 if (!GST_OPT_SCHEDULER_GROUP_IS_DISABLED (group)) {
1321 GST_LOG ("scheduling group %p in chain %p", group, chain);
1323 #ifdef USE_COTHREADS
1324 schedule_group (group);
1326 osched->recursion = 0;
1327 if (!g_list_find (osched->runqueue, group)) {
1329 osched->runqueue = g_list_append (osched->runqueue, group);
1331 gst_opt_scheduler_schedule_run_queue (osched, NULL);
1336 GST_LOG ("done scheduling group %p in chain %p", group, chain);
1337 unref_group (group);
1338 /* stop scheduling more groups */
1342 groups = g_slist_next (groups);
1347 /* this function is inserted in the gethandler when you are not
1348 * supposed to call _pull on the pad. */
1350 get_invalid_call (GstPad * pad)
1352 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
1353 ("get on pad %s:%s but the peer is operating chain based and so is not "
1354 "allowed to pull, fix the element.", GST_DEBUG_PAD_NAME (pad)));
1356 return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
1359 /* this function is inserted in the chainhandler when you are not
1360 * supposed to call _push on the pad. */
1362 chain_invalid_call (GstPad * pad, GstData * data)
1364 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
1365 ("chain on pad %s:%s but the pad is get based",
1366 GST_DEBUG_PAD_NAME (pad)));
1368 gst_data_unref (data);
1371 /* a get-based group is scheduled by getting a buffer from the get based
1372 * entry point and by pushing the buffer to the peer.
1373 * We also set the running flag on this group for as long as this
1374 * function is running. */
1376 get_group_schedule_function (int argc, char *argv[])
1378 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) argv;
1379 GstElement *entry = group->entry;
1381 GstOptScheduler *osched;
1383 /* what if the entry point disappeared? */
1384 if (entry == NULL || group->chain == NULL)
1387 osched = group->chain->sched;
1391 GST_LOG ("executing get-based group %p", group);
1393 group->flags |= GST_OPT_SCHEDULER_GROUP_RUNNING;
1395 GST_OPT_UNLOCK (osched);
1398 GstPad *pad = GST_PAD (pads->data);
1400 pads = g_list_next (pads);
1402 /* skip sinks and ghostpads */
1403 if (!GST_PAD_IS_SRC (pad) || !GST_IS_REAL_PAD (pad))
1406 GST_DEBUG ("doing get and push on pad \"%s:%s\" in group %p",
1407 GST_DEBUG_PAD_NAME (pad), group);
1409 data = gst_pad_call_get_function (pad);
1411 if (GST_EVENT_IS_INTERRUPT (data)) {
1412 GST_DEBUG ("unreffing interrupt event %p", data);
1413 gst_event_unref (GST_EVENT (data));
1416 gst_pad_push (pad, data);
1419 GST_OPT_LOCK (osched);
1421 group->flags &= ~GST_OPT_SCHEDULER_GROUP_RUNNING;
1426 /* a loop-based group is scheduled by calling the loop function
1427 * on the entry point.
1428 * We also set the running flag on this group for as long as this
1429 * function is running.
1430 * This function should be called with the scheduler lock held. */
1432 loop_group_schedule_function (int argc, char *argv[])
1434 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) argv;
1435 GstElement *entry = group->entry;
1437 GST_LOG ("executing loop-based group %p", group);
1439 group->flags |= GST_OPT_SCHEDULER_GROUP_RUNNING;
1441 GST_DEBUG ("calling loopfunc of element %s in group %p",
1442 GST_ELEMENT_NAME (entry), group);
1444 if (group->chain == NULL)
1447 if (entry->loopfunc) {
1448 GstOptScheduler *osched = group->chain->sched;
1450 GST_OPT_UNLOCK (osched);
1451 entry->loopfunc (entry);
1452 GST_OPT_LOCK (osched);
1454 group_error_handler (group);
1456 GST_LOG ("returned from loopfunc of element %s in group %p",
1457 GST_ELEMENT_NAME (entry), group);
1459 group->flags &= ~GST_OPT_SCHEDULER_GROUP_RUNNING;
1465 /* the function to schedule an unknown group, which just gives an error */
1467 unknown_group_schedule_function (int argc, char *argv[])
1469 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) argv;
1471 g_warning ("(internal error) unknown group type %d, disabling\n",
1473 group_error_handler (group);
1478 /* this function is called when the first element of a chain-loop or a loop-loop
1479 * link performs a push to the loop element. We then schedule the
1480 * group with the loop-based element until the datapen is empty */
1482 gst_opt_scheduler_loop_wrapper (GstPad * sinkpad, GstData * data)
1484 GstOptSchedulerGroup *group;
1485 GstOptScheduler *osched;
1488 group = GST_ELEMENT_SCHED_GROUP (GST_PAD_PARENT (sinkpad));
1489 osched = group->chain->sched;
1490 peer = GST_RPAD_PEER (sinkpad);
1492 GST_LOG ("chain handler for loop-based pad %" GST_PTR_FORMAT, sinkpad);
1494 GST_OPT_LOCK (osched);
1495 #ifdef USE_COTHREADS
1496 if (GST_PAD_DATALIST (peer)) {
1497 g_warning ("deadlock detected, disabling group %p", group);
1498 group_error_handler (group);
1500 GST_LOG ("queueing data %p on %s:%s's datapen", data,
1501 GST_DEBUG_PAD_NAME (peer));
1502 GST_PAD_DATAPEN (peer) = g_list_append (GST_PAD_DATALIST (peer), data);
1503 schedule_group (group);
1506 GST_LOG ("queueing data %p on %s:%s's datapen", data,
1507 GST_DEBUG_PAD_NAME (peer));
1508 GST_PAD_DATAPEN (peer) = g_list_append (GST_PAD_DATALIST (peer), data);
1509 if (!(group->flags & GST_OPT_SCHEDULER_GROUP_RUNNING)) {
1510 GST_LOG ("adding group %p to runqueue", group);
1511 if (!g_list_find (osched->runqueue, group)) {
1513 osched->runqueue = g_list_append (osched->runqueue, group);
1517 GST_OPT_UNLOCK (osched);
1519 GST_LOG ("%d datas left on %s:%s's datapen after chain handler",
1520 g_list_length (GST_PAD_DATALIST (peer)), GST_DEBUG_PAD_NAME (peer));
1523 /* this function is called by a loop based element that performs a
1524 * pull on a sinkpad. We schedule the peer group until the datapen
1525 * is filled with the data so that this function can return */
1527 gst_opt_scheduler_get_wrapper (GstPad * srcpad)
1530 GstOptSchedulerGroup *group;
1531 GstOptScheduler *osched;
1534 GST_LOG ("get handler for %" GST_PTR_FORMAT, srcpad);
1536 /* first try to grab a queued data */
1537 if (GST_PAD_DATALIST (srcpad)) {
1538 data = GST_PAD_DATALIST (srcpad)->data;
1539 GST_PAD_DATAPEN (srcpad) = g_list_remove (GST_PAD_DATALIST (srcpad), data);
1541 GST_LOG ("returning popped queued data %p", data);
1545 GST_LOG ("need to schedule the peer element");
1547 /* else we need to schedule the peer element */
1548 get_group (GST_PAD_PARENT (srcpad), &group);
1549 if (group == NULL) {
1550 /* wow, peer has no group */
1551 GST_LOG ("peer without group detected");
1552 //group_error_handler (group);
1553 return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
1555 osched = group->chain->sched;
1559 GST_OPT_LOCK (osched);
1561 GST_LOG ("scheduling upstream group %p to fill datapen", group);
1562 #ifdef USE_COTHREADS
1563 schedule_group (group);
1565 if (!(group->flags & GST_OPT_SCHEDULER_GROUP_RUNNING)) {
1568 if (!g_list_find (osched->runqueue, group)) {
1570 osched->runqueue = g_list_append (osched->runqueue, group);
1573 GST_LOG ("recursing into scheduler group %p", group);
1574 gst_opt_scheduler_schedule_run_queue (osched, group);
1575 GST_LOG ("return from recurse group %p", group);
1577 /* if the other group was disabled we might have to break out of the loop */
1578 disabled = GST_OPT_SCHEDULER_GROUP_IS_DISABLED (group);
1579 group = unref_group (group);
1581 if (group == NULL) {
1582 /* if the group was gone we also might have to break out of the loop */
1586 /* in this case, the group was running and we wanted to swtich to it,
1587 * this is not allowed in the optimal scheduler (yet) */
1588 g_warning ("deadlock detected, disabling group %p", group);
1589 group_error_handler (group);
1590 data = GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
1594 /* if the scheduler interrupted, make sure we send an INTERRUPTED event
1595 * to the loop based element */
1596 if (osched->state == GST_OPT_SCHEDULER_STATE_INTERRUPTED) {
1597 GST_INFO ("scheduler interrupted, return interrupt event");
1598 data = GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
1600 if (GST_PAD_DATALIST (srcpad)) {
1601 data = GST_PAD_DATALIST (srcpad)->data;
1602 GST_PAD_DATAPEN (srcpad) =
1603 g_list_remove (GST_PAD_DATALIST (srcpad), data);
1604 } else if (disabled) {
1605 /* no data in queue and peer group was disabled */
1606 data = GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
1610 while (data == NULL);
1612 #ifndef USE_COTHREADS
1615 GST_OPT_UNLOCK (osched);
1617 GST_LOG ("get handler, returning data %p, queue length %d",
1618 data, g_list_length (GST_PAD_DATALIST (srcpad)));
1624 pad_clear_queued (GstPad * srcpad, gpointer user_data)
1626 GList *datalist = GST_PAD_DATALIST (srcpad);
1629 GST_LOG ("need to clear some datas");
1630 g_list_foreach (datalist, (GFunc) gst_data_unref, NULL);
1631 g_list_free (datalist);
1632 GST_PAD_DATAPEN (srcpad) = NULL;
1637 gst_opt_scheduler_event_wrapper (GstPad * srcpad, GstEvent * event)
1641 GST_DEBUG ("intercepting event type %d on pad %s:%s",
1642 GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (srcpad));
1644 /* figure out if this is a flush event */
1645 switch (GST_EVENT_TYPE (event)) {
1646 case GST_EVENT_FLUSH:
1649 case GST_EVENT_SEEK:
1650 case GST_EVENT_SEEK_SEGMENT:
1651 flush = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH;
1659 GST_LOG ("event triggers a flush");
1661 pad_clear_queued (srcpad, NULL);
1663 return GST_RPAD_EVENTFUNC (srcpad) (srcpad, event);
1666 static GstElementStateReturn
1667 gst_opt_scheduler_state_transition (GstScheduler * sched, GstElement * element,
1670 GstOptSchedulerGroup *group;
1671 GstElementStateReturn res = GST_STATE_SUCCESS;
1673 GST_DEBUG ("element \"%s\" state change (%04x)",
1674 GST_ELEMENT_NAME (element) ? GST_ELEMENT_NAME (element) : "(null)",
1677 GST_OPT_LOCK (sched);
1678 /* we check the state of the managing pipeline here */
1679 if (GST_IS_BIN (element)) {
1680 if (GST_SCHEDULER_PARENT (sched) == element) {
1681 GST_LOG ("parent \"%s\" changed state",
1682 GST_ELEMENT_NAME (element) ? GST_ELEMENT_NAME (element) : "(null)");
1684 switch (transition) {
1685 case GST_STATE_PLAYING_TO_PAUSED:
1686 GST_INFO ("setting scheduler state to stopped");
1687 GST_SCHEDULER_STATE (sched) = GST_SCHEDULER_STATE_STOPPED;
1689 case GST_STATE_PAUSED_TO_PLAYING:
1690 GST_INFO ("setting scheduler state to running");
1691 GST_SCHEDULER_STATE (sched) = GST_SCHEDULER_STATE_RUNNING;
1694 GST_LOG ("no interesting state change, doing nothing");
1700 /* we don't care about decoupled elements after this */
1701 if (GST_ELEMENT_IS_DECOUPLED (element)) {
1702 res = GST_STATE_SUCCESS;
1706 /* get the group of the element */
1707 group = GST_ELEMENT_SCHED_GROUP (element);
1709 switch (transition) {
1710 case GST_STATE_PAUSED_TO_PLAYING:
1711 /* an element without a group has to be an unlinked src, sink
1714 GST_INFO ("element \"%s\" has no group", GST_ELEMENT_NAME (element));
1716 /* else construct the scheduling context of this group and enable it */
1718 group_element_set_enabled (group, element, TRUE);
1721 case GST_STATE_PLAYING_TO_PAUSED:
1722 /* if the element still has a group, we disable it */
1724 group_element_set_enabled (group, element, FALSE);
1726 case GST_STATE_PAUSED_TO_READY:
1728 GList *pads = (GList *) element->pads;
1730 g_list_foreach (pads, (GFunc) pad_clear_queued, NULL);
1737 //gst_scheduler_show (sched);
1740 GST_OPT_UNLOCK (sched);
1746 gst_opt_scheduler_scheduling_change (GstScheduler * sched, GstElement * element)
1748 g_warning ("scheduling change, implement me");
1752 get_group (GstElement * element, GstOptSchedulerGroup ** group)
1754 GstOptSchedulerCtx *ctx;
1756 ctx = GST_ELEMENT_SCHED_CONTEXT (element);
1758 *group = ctx->group;
1764 * the idea is to put the two elements into the same group.
1765 * - When no element is inside a group, we create a new group and add
1766 * the elements to it.
1767 * - When one of the elements has a group, add the other element to
1769 * - if both of the elements have a group, we merge the groups, which
1770 * will also merge the chains.
1771 * Group links must be managed by the caller.
1773 static GstOptSchedulerGroup *
1774 group_elements (GstOptScheduler * osched, GstElement * element1,
1775 GstElement * element2, GstOptSchedulerGroupType type)
1777 GstOptSchedulerGroup *group1, *group2, *group = NULL;
1779 get_group (element1, &group1);
1780 get_group (element2, &group2);
1782 /* none of the elements is added to a group, create a new group
1783 * and chain to add the elements to */
1784 if (!group1 && !group2) {
1785 GstOptSchedulerChain *chain;
1787 GST_DEBUG ("creating new group to hold \"%s\" and \"%s\"",
1788 GST_ELEMENT_NAME (element1), GST_ELEMENT_NAME (element2));
1790 chain = create_chain (osched);
1791 group = create_group (chain, element1, type);
1792 add_to_group (group, element2, TRUE);
1794 /* the first element has a group */
1796 GST_DEBUG ("adding \"%s\" to \"%s\"'s group",
1797 GST_ELEMENT_NAME (element2), GST_ELEMENT_NAME (element1));
1799 /* the second element also has a group, merge */
1801 merge_groups (group1, group2);
1802 /* the second element has no group, add it to the group
1803 * of the first element */
1805 add_to_group (group1, element2, TRUE);
1809 /* element1 has no group, element2 does. Add element1 to the
1810 * group of element2 */
1812 GST_DEBUG ("adding \"%s\" to \"%s\"'s group",
1813 GST_ELEMENT_NAME (element1), GST_ELEMENT_NAME (element2));
1814 add_to_group (group2, element1, TRUE);
1821 * increment link counts between groups -- it's important that src is actually
1822 * the src group, so we can introspect the topology later
1825 group_inc_link (GstOptSchedulerGroup * src, GstOptSchedulerGroup * sink)
1827 GSList *links = src->group_links;
1828 gboolean done = FALSE;
1829 GstOptSchedulerGroupLink *link;
1831 /* first try to find a previous link */
1832 while (links && !done) {
1833 link = (GstOptSchedulerGroupLink *) links->data;
1834 links = g_slist_next (links);
1836 if (IS_GROUP_LINK (link, src, sink)) {
1837 /* we found a link to this group, increment the link count */
1839 GST_LOG ("incremented group link count between %p and %p to %d",
1840 src, sink, link->count);
1845 /* no link was found, create a new one */
1846 link = g_new0 (GstOptSchedulerGroupLink, 1);
1852 src->group_links = g_slist_prepend (src->group_links, link);
1853 sink->group_links = g_slist_prepend (sink->group_links, link);
1855 src->sched->live_links++;
1857 GST_DEBUG ("added group link between %p and %p, %d live links now",
1858 src, sink, src->sched->live_links);
1863 * decrement link counts between groups, returns TRUE if the link count reaches
1864 * 0 -- note that the groups are not necessarily ordered as (src, sink) like
1868 group_dec_link (GstOptSchedulerGroup * group1, GstOptSchedulerGroup * group2)
1870 GSList *links = group1->group_links;
1871 gboolean res = FALSE;
1872 GstOptSchedulerGroupLink *link;
1875 link = (GstOptSchedulerGroupLink *) links->data;
1876 links = g_slist_next (links);
1878 if (IS_GROUP_LINK (link, group1, group2)) {
1879 g_assert (link->count > 0);
1881 GST_LOG ("link count between %p and %p is now %d",
1882 group1, group2, link->count);
1883 if (link->count == 0) {
1884 GstOptSchedulerGroup *iso_group = NULL;
1886 group1->group_links = g_slist_remove (group1->group_links, link);
1887 group2->group_links = g_slist_remove (group2->group_links, link);
1888 group1->sched->live_links--;
1890 GST_LOG ("%d live links now", group1->sched->live_links);
1893 GST_DEBUG ("removed group link between %p and %p", group1, group2);
1894 if (group1->group_links == NULL) {
1895 /* group1 has no more links with other groups */
1897 } else if (group2->group_links == NULL) {
1898 /* group2 has no more links with other groups */
1902 GstOptSchedulerChain *chain;
1904 GST_DEBUG ("group %p has become isolated, moving to new chain",
1907 chain = create_chain (iso_group->chain->sched);
1908 remove_from_chain (iso_group->chain, iso_group);
1909 add_to_chain (chain, iso_group);
1923 GST_OPT_GET_TO_CHAIN,
1924 GST_OPT_LOOP_TO_CHAIN,
1925 GST_OPT_GET_TO_LOOP,
1926 GST_OPT_CHAIN_TO_CHAIN,
1927 GST_OPT_CHAIN_TO_LOOP,
1928 GST_OPT_LOOP_TO_LOOP
1933 * Entry points for this scheduler.
1936 gst_opt_scheduler_setup (GstScheduler * sched)
1938 #ifdef USE_COTHREADS
1939 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
1941 /* first create thread context */
1942 if (osched->context == NULL) {
1943 GST_DEBUG ("initializing cothread context");
1944 osched->context = do_cothread_context_init ();
1950 gst_opt_scheduler_reset (GstScheduler * sched)
1952 #ifdef USE_COTHREADS
1953 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
1954 GSList *chains = osched->chains;
1957 GstOptSchedulerChain *chain = (GstOptSchedulerChain *) chains->data;
1958 GSList *groups = chain->groups;
1961 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) groups->data;
1963 destroy_group_scheduler (group);
1964 groups = groups->next;
1966 chains = chains->next;
1969 if (osched->context) {
1970 do_cothread_context_destroy (osched->context);
1971 osched->context = NULL;
1977 gst_opt_scheduler_add_element (GstScheduler * sched, GstElement * element)
1979 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
1980 GstOptSchedulerCtx *ctx;
1983 GST_DEBUG_OBJECT (sched, "adding element \"%s\"", GST_OBJECT_NAME (element));
1985 /* decoupled elements are not added to the scheduler lists */
1986 if (GST_ELEMENT_IS_DECOUPLED (element))
1989 ctx = g_new0 (GstOptSchedulerCtx, 1);
1990 GST_ELEMENT (element)->sched_private = ctx;
1991 ctx->flags = GST_OPT_SCHEDULER_CTX_DISABLED;
1993 /* set event handler on all pads here so events work unconnected too;
1994 * in _link, it can be overruled if need be */
1995 /* FIXME: we should also do this when new pads on the element are created;
1996 but there are no hooks, so we do it again in _link */
1997 pads = element->pads;
1999 GstPad *pad = GST_PAD (pads->data);
2001 pads = g_list_next (pads);
2003 if (!GST_IS_REAL_PAD (pad))
2005 GST_RPAD_EVENTHANDLER (pad) = GST_RPAD_EVENTFUNC (pad);
2008 /* loop based elements *always* end up in their own group. It can eventually
2009 * be merged with another group when a link is made */
2010 if (element->loopfunc) {
2011 GstOptSchedulerGroup *group;
2012 GstOptSchedulerChain *chain;
2014 GST_OPT_LOCK (sched);
2015 chain = create_chain (osched);
2017 group = create_group (chain, element, GST_OPT_SCHEDULER_GROUP_LOOP);
2018 group->entry = element;
2019 GST_OPT_UNLOCK (sched);
2021 GST_LOG ("added element \"%s\" as loop based entry",
2022 GST_ELEMENT_NAME (element));
2027 gst_opt_scheduler_remove_element (GstScheduler * sched, GstElement * element)
2029 GstOptSchedulerGroup *group;
2031 GST_DEBUG_OBJECT (sched, "removing element \"%s\"",
2032 GST_OBJECT_NAME (element));
2034 GST_OPT_LOCK (sched);
2035 /* decoupled elements are not added to the scheduler lists and should therefore
2037 if (GST_ELEMENT_IS_DECOUPLED (element)) {
2038 remove_decoupled (sched, element);
2042 /* the element is guaranteed to live in it's own group/chain now */
2043 get_group (element, &group);
2045 remove_from_group (group, element);
2048 g_free (GST_ELEMENT (element)->sched_private);
2049 GST_ELEMENT (element)->sched_private = NULL;
2052 GST_OPT_UNLOCK (sched);
2056 gst_opt_scheduler_yield (GstScheduler * sched, GstElement * element)
2058 #ifdef USE_COTHREADS
2059 /* yield hands control to the main cothread context if the requesting
2060 * element is the entry point of the group */
2061 GstOptSchedulerGroup *group;
2063 get_group (element, &group);
2064 if (group && group->entry == element)
2065 do_cothread_switch (do_cothread_get_main (((GstOptScheduler *) sched)->
2070 g_warning ("element %s performs a yield, please fix the element",
2071 GST_ELEMENT_NAME (element));
2077 gst_opt_scheduler_interrupt (GstScheduler * sched, GstElement * element)
2079 GST_INFO ("interrupt from \"%s\"", GST_OBJECT_NAME (element));
2081 #ifdef USE_COTHREADS
2082 do_cothread_switch (do_cothread_get_main (((GstOptScheduler *) sched)->
2087 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
2089 GST_OPT_LOCK (sched);
2090 GST_INFO ("scheduler set interrupted state");
2091 osched->state = GST_OPT_SCHEDULER_STATE_INTERRUPTED;
2092 GST_OPT_UNLOCK (sched);
2099 gst_opt_scheduler_error (GstScheduler * sched, GstElement * element)
2101 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
2102 GstOptSchedulerGroup *group;
2104 GST_OPT_LOCK (sched);
2105 get_group (element, &group);
2107 group_error_handler (group);
2109 osched->state = GST_OPT_SCHEDULER_STATE_ERROR;
2110 GST_OPT_UNLOCK (sched);
2113 /* link pads, merge groups and chains */
2115 gst_opt_scheduler_pad_link (GstScheduler * sched, GstPad * srcpad,
2118 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
2119 LinkType type = GST_OPT_INVALID;
2120 GstElement *src_element, *sink_element;
2122 GST_INFO ("scheduling link between %s:%s and %s:%s",
2123 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2125 src_element = GST_PAD_PARENT (srcpad);
2126 sink_element = GST_PAD_PARENT (sinkpad);
2128 GST_OPT_LOCK (sched);
2129 /* first we need to figure out what type of link we're dealing
2131 if (src_element->loopfunc && sink_element->loopfunc)
2132 type = GST_OPT_LOOP_TO_LOOP;
2134 if (src_element->loopfunc) {
2135 if (GST_RPAD_CHAINFUNC (sinkpad))
2136 type = GST_OPT_LOOP_TO_CHAIN;
2137 } else if (sink_element->loopfunc) {
2138 if (GST_RPAD_GETFUNC (srcpad)) {
2139 type = GST_OPT_GET_TO_LOOP;
2140 /* this could be tricky, the get based source could
2141 * already be part of a loop based group in another pad,
2142 * we assert on that for now */
2143 if (GST_ELEMENT_SCHED_CONTEXT (src_element) != NULL &&
2144 GST_ELEMENT_SCHED_GROUP (src_element) != NULL) {
2145 GstOptSchedulerGroup *group = GST_ELEMENT_SCHED_GROUP (src_element);
2147 /* if the loop based element is the entry point we're ok, if it
2148 * isn't then we have multiple loop based elements in this group */
2149 if (group->entry != sink_element) {
2151 ("internal error: cannot schedule get to loop in multi-loop based group");
2156 type = GST_OPT_CHAIN_TO_LOOP;
2158 if (GST_RPAD_GETFUNC (srcpad) && GST_RPAD_CHAINFUNC (sinkpad)) {
2159 type = GST_OPT_GET_TO_CHAIN;
2160 /* the get based source could already be part of a loop
2161 * based group in another pad, we assert on that for now */
2162 if (GST_ELEMENT_SCHED_CONTEXT (src_element) != NULL &&
2163 GST_ELEMENT_SCHED_GROUP (src_element) != NULL) {
2164 GstOptSchedulerGroup *group = GST_ELEMENT_SCHED_GROUP (src_element);
2166 /* if the get based element is the entry point we're ok, if it
2167 * isn't then we have a mixed loop/chain based group */
2168 if (group->entry != src_element) {
2169 g_error ("internal error: cannot schedule get to chain "
2170 "with mixed loop/chain based group");
2175 type = GST_OPT_CHAIN_TO_CHAIN;
2179 /* since we can't set event handlers on pad creation after addition, it is
2180 * best we set all of them again to the default before linking */
2181 GST_RPAD_EVENTHANDLER (srcpad) = GST_RPAD_EVENTFUNC (srcpad);
2182 GST_RPAD_EVENTHANDLER (sinkpad) = GST_RPAD_EVENTFUNC (sinkpad);
2184 /* for each link type, perform specific actions */
2186 case GST_OPT_GET_TO_CHAIN:
2188 GstOptSchedulerGroup *group = NULL;
2190 GST_LOG ("get to chain based link");
2192 /* setup get/chain handlers */
2193 GST_RPAD_GETHANDLER (srcpad) = get_invalid_call;
2194 GST_RPAD_CHAINHANDLER (sinkpad) = gst_pad_call_chain_function;
2196 /* the two elements should be put into the same group,
2197 * this also means that they are in the same chain automatically */
2198 group = group_elements (osched, src_element, sink_element,
2199 GST_OPT_SCHEDULER_GROUP_GET);
2201 /* if there is not yet an entry in the group, select the source
2202 * element as the entry point and mark the group as a get based
2204 if (!group->entry) {
2205 group->entry = src_element;
2206 group->type = GST_OPT_SCHEDULER_GROUP_GET;
2208 GST_DEBUG ("setting \"%s\" as entry point of _get-based group %p",
2209 GST_ELEMENT_NAME (src_element), group);
2211 setup_group_scheduler (osched, group);
2215 case GST_OPT_LOOP_TO_CHAIN:
2216 case GST_OPT_CHAIN_TO_CHAIN:
2217 GST_LOG ("loop/chain to chain based link");
2219 GST_RPAD_GETHANDLER (srcpad) = get_invalid_call;
2220 GST_RPAD_CHAINHANDLER (sinkpad) = gst_pad_call_chain_function;
2222 /* the two elements should be put into the same group, this also means
2223 * that they are in the same chain automatically, in case of a loop-based
2224 * src_element, there will be a group for src_element and sink_element
2225 * will be added to it. In the case a new group is created, we can't know
2226 * the type so we pass UNKNOWN as an arg */
2227 group_elements (osched, src_element, sink_element,
2228 GST_OPT_SCHEDULER_GROUP_UNKNOWN);
2230 case GST_OPT_GET_TO_LOOP:
2231 GST_LOG ("get to loop based link");
2233 GST_RPAD_GETHANDLER (srcpad) = gst_pad_call_get_function;
2234 GST_RPAD_CHAINHANDLER (sinkpad) = chain_invalid_call;
2236 /* the two elements should be put into the same group, this also means
2237 * that they are in the same chain automatically, sink_element is
2238 * loop-based so it already has a group where src_element will be added
2240 group_elements (osched, src_element, sink_element,
2241 GST_OPT_SCHEDULER_GROUP_LOOP);
2243 case GST_OPT_CHAIN_TO_LOOP:
2244 case GST_OPT_LOOP_TO_LOOP:
2246 GstOptSchedulerGroup *group1, *group2;
2248 GST_LOG ("chain/loop to loop based link");
2250 GST_RPAD_GETHANDLER (srcpad) = gst_opt_scheduler_get_wrapper;
2251 GST_RPAD_CHAINHANDLER (sinkpad) = gst_opt_scheduler_loop_wrapper;
2252 /* events on the srcpad have to be intercepted as we might need to
2253 * flush the buffer lists, so override the given eventfunc */
2254 GST_RPAD_EVENTHANDLER (srcpad) = gst_opt_scheduler_event_wrapper;
2256 group1 = GST_ELEMENT_SCHED_GROUP (src_element);
2257 group2 = GST_ELEMENT_SCHED_GROUP (sink_element);
2259 g_assert (group2 != NULL);
2261 /* group2 is guaranteed to exist as it contains a loop-based element.
2262 * group1 only exists if src_element is linked to some other element */
2264 /* create a new group for src_element as it cannot be merged into another group
2265 * here. we create the group in the same chain as the loop-based element. note
2266 * that creating a new group will not increment the links with other groups */
2267 GST_DEBUG ("creating new group for element %s",
2268 GST_ELEMENT_NAME (src_element));
2270 create_group (group2->chain, src_element,
2271 GST_OPT_SCHEDULER_GROUP_LOOP);
2273 /* both elements are already in a group, make sure they are added to
2275 merge_chains (group1->chain, group2->chain);
2277 /* increment the group link counters */
2278 group_inc_link (group1, group2);
2281 case GST_OPT_INVALID:
2282 g_error ("(internal error) invalid element link, what are you doing?");
2286 GST_OPT_UNLOCK (sched);
2290 group_elements_set_visited (GstOptSchedulerGroup * group, gboolean visited)
2294 for (elements = group->elements; elements; elements = g_slist_next (elements)) {
2295 GstElement *element = GST_ELEMENT (elements->data);
2298 GST_ELEMENT_SET_VISITED (element);
2300 GST_ELEMENT_UNSET_VISITED (element);
2303 /* don't forget to set any decoupled entry points that are not accounted for in the
2304 * element list (since they belong to two groups). */
2307 GST_ELEMENT_SET_VISITED (group->entry);
2309 GST_ELEMENT_UNSET_VISITED (group->entry);
2315 element_get_reachables_func (GstElement * element, GstOptSchedulerGroup * group,
2318 GList *result = NULL;
2321 /* if no element or element not in group or been there, return NULL */
2322 if (element == NULL || !group_has_element (group, element) ||
2323 GST_ELEMENT_IS_VISITED (element))
2326 GST_ELEMENT_SET_VISITED (element);
2328 result = g_list_prepend (result, element);
2330 pads = element->pads;
2332 GstPad *pad = GST_PAD (pads->data);
2335 pads = g_list_next (pads);
2337 /* we only operate on real pads and on the pad that is not broken */
2338 if (!GST_IS_REAL_PAD (pad) || pad == brokenpad)
2341 peer = GST_PAD_PEER (pad);
2342 if (!GST_IS_REAL_PAD (peer) || peer == brokenpad)
2349 parent = GST_PAD_PARENT (peer);
2351 res = element_get_reachables_func (parent, group, brokenpad);
2353 result = g_list_concat (result, res);
2361 element_get_reachables (GstElement * element, GstOptSchedulerGroup * group,
2366 /* reset visited flags */
2367 group_elements_set_visited (group, FALSE);
2369 result = element_get_reachables_func (element, group, brokenpad);
2371 /* and reset visited flags again */
2372 group_elements_set_visited (group, FALSE);
2378 * checks if a target group is still reachable from the group without taking the broken
2379 * group link into account.
2382 group_can_reach_group (GstOptSchedulerGroup * group,
2383 GstOptSchedulerGroup * target)
2385 gboolean reachable = FALSE;
2386 const GSList *links = group->group_links;
2388 GST_LOG ("checking if group %p can reach %p", group, target);
2390 /* seems like we found the target element */
2391 if (group == target) {
2392 GST_LOG ("found way to reach %p", target);
2396 /* if the group is marked as visited, we don't need to check here */
2397 if (GST_OPT_SCHEDULER_GROUP_IS_FLAG_SET (group,
2398 GST_OPT_SCHEDULER_GROUP_VISITED)) {
2399 GST_LOG ("already visited %p", group);
2403 /* mark group as visited */
2404 GST_OPT_SCHEDULER_GROUP_SET_FLAG (group, GST_OPT_SCHEDULER_GROUP_VISITED);
2406 while (links && !reachable) {
2407 GstOptSchedulerGroupLink *link = (GstOptSchedulerGroupLink *) links->data;
2408 GstOptSchedulerGroup *other;
2410 links = g_slist_next (links);
2412 /* find other group in this link */
2413 other = OTHER_GROUP_LINK (link, group);
2415 GST_LOG ("found link from %p to %p, count %d", group, other, link->count);
2417 /* check if we can reach the target recursively */
2418 reachable = group_can_reach_group (other, target);
2420 /* unset the visited flag, note that this is not optimal as we might be checking
2421 * groups several times when they are reachable with a loop. An alternative would be
2422 * to not clear the group flag at this stage but clear all flags in the chain when
2423 * all groups are checked. */
2424 GST_OPT_SCHEDULER_GROUP_UNSET_FLAG (group, GST_OPT_SCHEDULER_GROUP_VISITED);
2426 GST_LOG ("leaving group %p with %s", group, (reachable ? "TRUE" : "FALSE"));
2432 * Go through all the pads of the given element and decrement the links that
2433 * this group has with the group of the peer element. This function is mainly used
2434 * to update the group connections before we remove the element from the group.
2437 group_dec_links_for_element (GstOptSchedulerGroup * group, GstElement * element)
2441 GstOptSchedulerGroup *peer_group;
2443 for (l = GST_ELEMENT_PADS (element); l; l = l->next) {
2444 pad = (GstPad *) l->data;
2445 if (GST_IS_REAL_PAD (pad) && GST_PAD_PEER (pad)) {
2446 get_group (GST_PAD_PARENT (GST_PAD_PEER (pad)), &peer_group);
2447 if (peer_group && peer_group != group)
2448 group_dec_link (group, peer_group);
2454 * Go through all the pads of the given element and increment the links that
2455 * this group has with the group of the peer element. This function is mainly used
2456 * to update the group connections before we add the element to the group.
2459 group_inc_links_for_element (GstOptSchedulerGroup * group, GstElement * element)
2463 GstOptSchedulerGroup *peer_group;
2465 GST_DEBUG ("group %p, element %s ", group, gst_element_get_name (element));
2467 for (l = GST_ELEMENT_PADS (element); l; l = l->next) {
2468 pad = (GstPad *) l->data;
2469 if (GST_IS_REAL_PAD (pad) && GST_PAD_PEER (pad)) {
2470 get_group (GST_PAD_PARENT (GST_PAD_PEER (pad)), &peer_group);
2471 if (peer_group && peer_group != group)
2472 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC)
2473 group_inc_link (group, peer_group);
2475 group_inc_link (peer_group, group);
2481 debug_element (GstElement * element, GstOptScheduler * osched)
2483 GST_LOG ("element %s", gst_element_get_name (element));
2486 /* move this group in the chain of the groups it has links with */
2488 rechain_group (GstOptSchedulerGroup * group)
2490 GstOptSchedulerChain *chain = NULL;
2493 GST_LOG ("checking if this group needs rechaining");
2495 /* follow all links */
2496 for (links = group->group_links; links; links = g_slist_next (links)) {
2497 GstOptSchedulerGroupLink *link = (GstOptSchedulerGroupLink *) links->data;
2498 GstOptSchedulerGroup *other;
2500 other = OTHER_GROUP_LINK (link, group);
2501 GST_LOG ("found link with other group %p with chain %p", other,
2504 /* first time, take chain */
2505 if (chain == NULL) {
2506 chain = other->chain;
2508 /* second time, chain should be the same */
2509 else if (other->chain != chain) {
2510 g_warning ("(internal error): chain inconsistency");
2514 GST_LOG ("no new chain found, not rechaining");
2515 } else if (chain != group->chain) {
2516 GST_LOG ("need to move group %p to chain %p", group, chain);
2517 /* remove from old chain */
2518 remove_from_chain (group->chain, group);
2519 /* and move to new chain */
2520 add_to_chain (chain, group);
2522 GST_LOG ("group %p is in correct chain %p", group, chain);
2526 /* make sure that the group does not contain only single element.
2527 * Only loop-based groups can contain a single element. */
2528 static GstOptSchedulerGroup *
2529 normalize_group (GstOptSchedulerGroup * group)
2532 gboolean have_decoupled = FALSE;
2537 num = group->num_elements;
2538 /* decoupled elements are not added to the group but are
2539 * added as an entry */
2540 if (group->entry && GST_ELEMENT_IS_DECOUPLED (group->entry)) {
2542 have_decoupled = TRUE;
2545 if (num == 1 && group->type != GST_OPT_SCHEDULER_GROUP_LOOP) {
2546 GST_LOG ("removing last element from group %p", group);
2547 if (have_decoupled) {
2548 group->entry = NULL;
2550 GST_LOG ("removing group %p from its chain", group);
2551 chain_group_set_enabled (group->chain, group, FALSE);
2552 remove_from_chain (group->chain, group);
2554 group = unref_group (group);
2556 group = remove_from_group (group, GST_ELEMENT (group->elements->data));
2562 /* migrate the element and all connected elements to a new group without looking at
2564 static GstOptSchedulerGroup *
2565 group_migrate_connected (GstOptScheduler * osched, GstElement * element,
2566 GstOptSchedulerGroup * group, GstPad * brokenpad)
2568 GList *connected, *c;
2569 GstOptSchedulerGroup *new_group = NULL, *tst;
2570 GstOptSchedulerChain *chain;
2573 if (GST_ELEMENT_IS_DECOUPLED (element)) {
2574 GST_LOG ("element is decoupled and thus not in the group");
2575 /* the element is decoupled and is therefore not in the group */
2579 get_group (element, &tst);
2581 GST_LOG ("element has no group, not interesting");
2585 GST_LOG ("migrate connected elements to new group");
2586 connected = element_get_reachables (element, group, brokenpad);
2587 GST_LOG ("elements to move to new group:");
2588 g_list_foreach (connected, (GFunc) debug_element, NULL);
2590 len = g_list_length (connected);
2593 g_warning ("(internal error) found lost element %s",
2594 gst_element_get_name (element));
2596 } else if (len == 1) {
2597 group = remove_from_group (group, GST_ELEMENT (connected->data));
2599 ("not migrating to new group as the group would only contain 1 element");
2600 g_list_free (connected);
2601 GST_LOG ("new group is old group now");
2604 /* we create a new chain to hold the new group */
2605 chain = create_chain (osched);
2607 for (c = connected; c; c = g_list_next (c)) {
2608 GstElement *element = GST_ELEMENT (c->data);
2610 group = remove_from_group (group, element);
2611 if (new_group == NULL) {
2613 create_group (chain, element, GST_OPT_SCHEDULER_GROUP_UNKNOWN);
2615 add_to_group (new_group, element, TRUE);
2618 g_list_free (connected);
2620 /* remove last element from the group if any. Make sure not to remove
2621 * the loop based entry point of a group as this always needs one group */
2622 if (group != NULL) {
2623 group = normalize_group (group);
2627 if (new_group != NULL) {
2628 new_group = normalize_group (new_group);
2629 if (new_group == NULL)
2631 /* at this point the new group lives in its own chain but might
2632 * have to be merged with another chain, this happens when the new
2633 * group has a link with another group in another chain */
2634 rechain_group (new_group);
2642 gst_opt_scheduler_pad_unlink (GstScheduler * sched,
2643 GstPad * srcpad, GstPad * sinkpad)
2645 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
2646 GstElement *src_element, *sink_element;
2647 GstOptSchedulerGroup *group1, *group2;
2649 GST_INFO ("unscheduling link between %s:%s and %s:%s",
2650 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2652 src_element = GST_PAD_PARENT (srcpad);
2653 sink_element = GST_PAD_PARENT (sinkpad);
2655 GST_OPT_LOCK (sched);
2656 get_group (src_element, &group1);
2657 get_group (sink_element, &group2);
2659 /* for decoupled elements (that are never put into a group) we use the
2660 * group of the peer element for the remainder of the algorithm */
2661 if (GST_ELEMENT_IS_DECOUPLED (src_element)) {
2664 if (GST_ELEMENT_IS_DECOUPLED (sink_element)) {
2668 /* if one the elements has no group (anymore) we don't really care
2670 if (!group1 || !group2) {
2672 ("one (or both) of the elements is not in a group, not interesting");
2676 /* easy part, groups are different */
2677 if (group1 != group2) {
2680 GST_LOG ("elements are in different groups");
2682 /* we can remove the links between the groups now */
2683 zero = group_dec_link (group1, group2);
2685 /* if the groups are not directly connected anymore, we have to perform a
2686 * recursive check to see if they are really unlinked */
2688 gboolean still_link;
2689 GstOptSchedulerChain *chain;
2691 /* see if group1 and group2 are still connected in any indirect way */
2692 still_link = group_can_reach_group (group1, group2);
2694 GST_DEBUG ("group %p %s reach group %p", group1,
2695 (still_link ? "can" : "can't"), group2);
2697 /* groups are really disconnected, migrate one group to a new chain */
2698 chain = create_chain (osched);
2699 chain_recursively_migrate_group (chain, group1);
2701 GST_DEBUG ("migrated group %p to new chain %p", group1, chain);
2704 GST_DEBUG ("group %p still has direct link with group %p", group1,
2708 /* hard part, groups are equal */
2710 GstOptSchedulerGroup *group;
2712 /* since group1 == group2, it doesn't matter which group we take */
2715 GST_LOG ("elements are in the same group %p", group);
2717 if (group->entry == NULL) {
2718 /* it doesn't really matter, we just have to make sure that both
2719 * elements end up in another group if they are not connected */
2720 GST_LOG ("group %p has no entry, moving source element to new group",
2722 group_migrate_connected (osched, src_element, group, srcpad);
2726 GST_LOG ("group %p has entry %p", group, group->entry);
2728 /* get of a list of all elements that are still managed by the old
2730 reachables = element_get_reachables (group->entry, group, srcpad);
2731 GST_LOG ("elements still reachable from the entry:");
2732 g_list_foreach (reachables, (GFunc) debug_element, sched);
2734 /* if the source is reachable from the entry, we can leave it in the group */
2735 if (g_list_find (reachables, src_element)) {
2737 ("source element still reachable from the entry, leaving in group");
2740 ("source element not reachable from the entry, moving to new group");
2741 group_migrate_connected (osched, src_element, group, srcpad);
2744 /* if the sink is reachable from the entry, we can leave it in the group */
2745 if (g_list_find (reachables, sink_element)) {
2747 ("sink element still reachable from the entry, leaving in group");
2750 ("sink element not reachable from the entry, moving to new group");
2751 group_migrate_connected (osched, sink_element, group, srcpad);
2753 g_list_free (reachables);
2755 /* at this point the group can be freed and gone, so don't touch */
2758 GST_OPT_UNLOCK (sched);
2761 /* a scheduler iteration is done by looping and scheduling the active chains */
2762 static GstSchedulerState
2763 gst_opt_scheduler_iterate (GstScheduler * sched)
2765 GstSchedulerState state = GST_SCHEDULER_STATE_STOPPED;
2766 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
2769 GST_OPT_LOCK (sched);
2770 iterations = osched->iterations;
2772 osched->state = GST_OPT_SCHEDULER_STATE_RUNNING;
2774 GST_DEBUG_OBJECT (sched, "iterating");
2776 while (iterations) {
2777 gboolean scheduled = FALSE;
2780 /* we have to schedule each of the scheduler chains now. */
2781 chains = osched->chains;
2783 GstOptSchedulerChain *chain = (GstOptSchedulerChain *) chains->data;
2786 /* if the chain is not disabled, schedule it */
2787 if (!GST_OPT_SCHEDULER_CHAIN_IS_DISABLED (chain)) {
2788 GST_LOG ("scheduling chain %p", chain);
2789 scheduled = schedule_chain (chain);
2790 GST_LOG ("scheduled chain %p", chain);
2792 GST_LOG ("not scheduling disabled chain %p", chain);
2795 /* don't schedule any more chains when in error */
2796 if (osched->state == GST_OPT_SCHEDULER_STATE_ERROR) {
2797 GST_ERROR_OBJECT (sched, "in error state");
2798 /* unref the chain here as we move out of the while loop */
2799 unref_chain (chain);
2801 } else if (osched->state == GST_OPT_SCHEDULER_STATE_INTERRUPTED) {
2802 GST_DEBUG_OBJECT (osched, "got interrupted, continue with next chain");
2803 osched->state = GST_OPT_SCHEDULER_STATE_RUNNING;
2806 /* grab the next chain before we unref, the list we are iterating
2807 * can only be updated in the unref method */
2808 chains = g_slist_next (chains);
2809 unref_chain (chain);
2812 /* at this point it's possible that the scheduler state is
2813 * in error, we then return an error */
2814 if (osched->state == GST_OPT_SCHEDULER_STATE_ERROR) {
2815 state = GST_SCHEDULER_STATE_ERROR;
2818 /* if chains were scheduled, return our current state */
2820 state = GST_SCHEDULER_STATE (sched);
2821 /* if no chains were scheduled, we say we are stopped */
2823 state = GST_SCHEDULER_STATE_STOPPED;
2830 GST_OPT_UNLOCK (sched);
2837 gst_opt_scheduler_show (GstScheduler * sched)
2839 GstOptScheduler *osched = GST_OPT_SCHEDULER (sched);
2842 GST_OPT_LOCK (sched);
2844 g_print ("iterations: %d\n", osched->iterations);
2845 g_print ("max recursion: %d\n", osched->max_recursion);
2847 chains = osched->chains;
2849 GstOptSchedulerChain *chain = (GstOptSchedulerChain *) chains->data;
2850 GSList *groups = chain->groups;
2852 chains = g_slist_next (chains);
2854 g_print ("+- chain %p: refcount %d, %d groups, %d enabled, flags %d\n",
2855 chain, chain->refcount, chain->num_groups, chain->num_enabled,
2859 GstOptSchedulerGroup *group = (GstOptSchedulerGroup *) groups->data;
2860 GSList *elements = group->elements;
2861 GSList *group_links = group->group_links;
2863 groups = g_slist_next (groups);
2866 (" +- group %p: refcount %d, %d elements, %d enabled, flags %d, entry %s, %s\n",
2867 group, group->refcount, group->num_elements, group->num_enabled,
2869 (group->entry ? GST_ELEMENT_NAME (group->entry) : "(none)"),
2871 GST_OPT_SCHEDULER_GROUP_GET ? "get-based" : "loop-based"));
2874 GstElement *element = (GstElement *) elements->data;
2876 elements = g_slist_next (elements);
2878 g_print (" +- element %s\n", GST_ELEMENT_NAME (element));
2880 while (group_links) {
2881 GstOptSchedulerGroupLink *link =
2882 (GstOptSchedulerGroupLink *) group_links->data;
2884 group_links = g_slist_next (group_links);
2886 g_print ("group link %p between %p and %p, count %d\n",
2887 link, link->src, link->sink, link->count);
2891 GST_OPT_UNLOCK (sched);
2895 gst_opt_scheduler_get_property (GObject * object, guint prop_id,
2896 GValue * value, GParamSpec * pspec)
2898 GstOptScheduler *osched;
2900 g_return_if_fail (GST_IS_OPT_SCHEDULER (object));
2902 osched = GST_OPT_SCHEDULER (object);
2905 case ARG_ITERATIONS:
2906 g_value_set_int (value, osched->iterations);
2908 case ARG_MAX_RECURSION:
2909 g_value_set_int (value, osched->max_recursion);
2912 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2918 gst_opt_scheduler_set_property (GObject * object, guint prop_id,
2919 const GValue * value, GParamSpec * pspec)
2921 GstOptScheduler *osched;
2923 g_return_if_fail (GST_IS_OPT_SCHEDULER (object));
2925 osched = GST_OPT_SCHEDULER (object);
2928 case ARG_ITERATIONS:
2929 osched->iterations = g_value_get_int (value);
2931 case ARG_MAX_RECURSION:
2932 osched->max_recursion = g_value_get_int (value);
2935 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);