2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@chello.be>
5 * gstscheduler.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.
23 #include "gst_private.h"
25 #include "gstsystemclock.h"
26 #include "gstscheduler.h"
28 #include "gstregistrypool.h"
30 static void gst_scheduler_class_init (GstSchedulerClass * klass);
31 static void gst_scheduler_init (GstScheduler * sched);
32 static void gst_scheduler_dispose (GObject * object);
34 static GstObjectClass *parent_class = NULL;
36 static gchar *_default_name = NULL;
39 gst_scheduler_get_type (void)
41 static GType _gst_scheduler_type = 0;
43 if (!_gst_scheduler_type) {
44 static const GTypeInfo scheduler_info = {
45 sizeof (GstSchedulerClass),
48 (GClassInitFunc) gst_scheduler_class_init,
51 sizeof (GstScheduler),
53 (GInstanceInitFunc) gst_scheduler_init,
58 g_type_register_static (GST_TYPE_OBJECT, "GstScheduler",
59 &scheduler_info, G_TYPE_FLAG_ABSTRACT);
61 return _gst_scheduler_type;
65 gst_scheduler_class_init (GstSchedulerClass * klass)
67 GObjectClass *gobject_class;
69 gobject_class = (GObjectClass *) klass;
71 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
73 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_scheduler_dispose);
77 gst_scheduler_init (GstScheduler * sched)
83 gst_scheduler_dispose (GObject * object)
85 GstScheduler *sched = GST_SCHEDULER (object);
87 G_OBJECT_CLASS (parent_class)->dispose (G_OBJECT (sched));
91 * gst_scheduler_setup:
92 * @sched: the scheduler
94 * Prepare the scheduler.
97 gst_scheduler_setup (GstScheduler * sched)
99 GstSchedulerClass *sclass;
101 g_return_if_fail (GST_IS_SCHEDULER (sched));
103 sclass = GST_SCHEDULER_GET_CLASS (sched);
106 sclass->setup (sched);
110 * gst_scheduler_reset:
111 * @sched: a #GstScheduler to reset.
113 * Reset the schedulers.
116 gst_scheduler_reset (GstScheduler * sched)
118 GstSchedulerClass *sclass;
120 g_return_if_fail (GST_IS_SCHEDULER (sched));
122 sclass = GST_SCHEDULER_GET_CLASS (sched);
125 sclass->reset (sched);
129 gst_scheduler_create_task (GstScheduler * sched, GstTaskFunction func,
132 GstSchedulerClass *sclass;
133 GstTask *result = NULL;
135 g_return_val_if_fail (GST_IS_SCHEDULER (sched), result);
137 sclass = GST_SCHEDULER_GET_CLASS (sched);
139 if (sclass->create_task)
140 result = sclass->create_task (sched, func, data);
146 * Factory stuff starts here
149 static void gst_scheduler_factory_class_init (GstSchedulerFactoryClass * klass);
150 static void gst_scheduler_factory_init (GstSchedulerFactory * factory);
152 static GstPluginFeatureClass *factory_parent_class = NULL;
154 /* static guint gst_scheduler_factory_signals[LAST_SIGNAL] = { 0 }; */
157 gst_scheduler_factory_get_type (void)
159 static GType schedulerfactory_type = 0;
161 if (!schedulerfactory_type) {
162 static const GTypeInfo schedulerfactory_info = {
163 sizeof (GstSchedulerFactoryClass),
166 (GClassInitFunc) gst_scheduler_factory_class_init,
169 sizeof (GstSchedulerFactory),
171 (GInstanceInitFunc) gst_scheduler_factory_init,
175 schedulerfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
176 "GstSchedulerFactory", &schedulerfactory_info, 0);
178 return schedulerfactory_type;
182 gst_scheduler_factory_class_init (GstSchedulerFactoryClass * klass)
184 GObjectClass *gobject_class;
185 GstObjectClass *gstobject_class;
186 GstPluginFeatureClass *gstpluginfeature_class;
188 gobject_class = (GObjectClass *) klass;
189 gstobject_class = (GstObjectClass *) klass;
190 gstpluginfeature_class = (GstPluginFeatureClass *) klass;
192 factory_parent_class = g_type_class_ref (GST_TYPE_PLUGIN_FEATURE);
194 if (!_default_name) {
195 if (g_getenv ("GST_SCHEDULER")) {
196 _default_name = g_strdup (g_getenv ("GST_SCHEDULER"));
198 _default_name = g_strdup (GST_SCHEDULER_DEFAULT_NAME);
201 g_assert (_default_name);
205 gst_scheduler_factory_init (GstSchedulerFactory * factory)
211 * gst_scheduler_register:
212 * @plugin: a #GstPlugin
213 * @name: name of the scheduler to register
214 * @longdesc: description of the scheduler
215 * @type: #GType of the scheduler to register
217 * Registers a scheduler with GStreamer.
219 * Returns: TRUE, if the registering succeeded, FALSE on error.
224 gst_scheduler_register (GstPlugin * plugin, const gchar * name,
225 const gchar * longdesc, GType type)
227 GstSchedulerFactory *factory;
229 g_return_val_if_fail (plugin != NULL, FALSE);
230 g_return_val_if_fail (name != NULL, FALSE);
231 g_return_val_if_fail (longdesc != NULL, FALSE);
232 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_SCHEDULER), FALSE);
234 factory = gst_scheduler_factory_find (name);
236 g_return_val_if_fail (factory->type == 0, FALSE);
237 g_free (factory->longdesc);
238 factory->longdesc = g_strdup (longdesc);
239 factory->type = type;
241 factory = gst_scheduler_factory_new (name, longdesc, type);
242 g_return_val_if_fail (factory, FALSE);
243 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
250 * gst_scheduler_factory_new:
251 * @name: name of schedulerfactory to create
252 * @longdesc: long description of schedulerfactory to create
253 * @type: the gtk type of the GstScheduler element of this factory
255 * Create a new schedulerfactory with the given parameters
257 * Returns: a new #GstSchedulerFactory.
259 GstSchedulerFactory *
260 gst_scheduler_factory_new (const gchar * name, const gchar * longdesc,
263 GstSchedulerFactory *factory;
265 g_return_val_if_fail (name != NULL, NULL);
267 factory = gst_scheduler_factory_find (name);
271 GST_SCHEDULER_FACTORY (g_object_new (GST_TYPE_SCHEDULER_FACTORY, NULL));
272 GST_PLUGIN_FEATURE_NAME (factory) = g_strdup (name);
274 g_free (factory->longdesc);
277 factory->longdesc = g_strdup (longdesc);
278 factory->type = type;
284 * gst_scheduler_factory_destroy:
285 * @factory: factory to destroy
287 * Removes the scheduler from the global list.
290 gst_scheduler_factory_destroy (GstSchedulerFactory * factory)
292 g_return_if_fail (factory != NULL);
294 /* we don't free the struct bacause someone might have a handle to it.. */
298 * gst_scheduler_factory_find:
299 * @name: name of schedulerfactory to find
301 * Search for an schedulerfactory of the given name.
303 * Returns: #GstSchedulerFactory if found, NULL otherwise
305 GstSchedulerFactory *
306 gst_scheduler_factory_find (const gchar * name)
308 GstPluginFeature *feature;
310 g_return_val_if_fail (name != NULL, NULL);
312 GST_DEBUG ("gstscheduler: find \"%s\"", name);
314 feature = gst_registry_pool_find_feature (name, GST_TYPE_SCHEDULER_FACTORY);
317 return GST_SCHEDULER_FACTORY (feature);
323 * gst_scheduler_factory_create:
324 * @factory: the factory used to create the instance
325 * @parent: the parent element of this scheduler
327 * Create a new #GstScheduler instance from the
328 * given schedulerfactory with the given parent. @parent will
329 * have its scheduler set to the returned #GstScheduler instance.
331 * Returns: A new #GstScheduler instance with a reference count of %1.
334 gst_scheduler_factory_create (GstSchedulerFactory * factory,
337 GstScheduler *sched = NULL;
339 g_return_val_if_fail (factory != NULL, NULL);
340 g_return_val_if_fail (GST_IS_ELEMENT (parent), NULL);
342 if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
343 g_return_val_if_fail (factory->type != 0, NULL);
345 sched = GST_SCHEDULER (g_object_new (factory->type, NULL));
346 sched->parent = parent;
348 /* let's refcount the scheduler */
349 gst_object_ref (GST_OBJECT (sched));
350 gst_object_sink (GST_OBJECT (sched));
357 * gst_scheduler_factory_make:
358 * @name: the name of the factory used to create the instance
359 * @parent: the parent element of this scheduler
361 * Create a new #GstScheduler instance from the
362 * schedulerfactory with the given name and parent. @parent will
363 * have its scheduler set to the returned #GstScheduler instance.
364 * If %NULL is passed as @name, the default scheduler name will
367 * Returns: A new #GstScheduler instance with a reference count of %1.
370 gst_scheduler_factory_make (const gchar * name, GstElement * parent)
372 GstSchedulerFactory *factory;
373 const gchar *default_name = gst_scheduler_factory_get_default_name ();
376 factory = gst_scheduler_factory_find (name);
378 /* FIXME: do better error handling */
379 if (default_name == NULL)
380 g_error ("No default scheduler name - do you have a registry ?");
381 factory = gst_scheduler_factory_find (default_name);
387 return gst_scheduler_factory_create (factory, parent);
391 * gst_scheduler_factory_set_default_name:
392 * @name: the name of the factory used as a default
394 * Set the default schedulerfactory name.
397 gst_scheduler_factory_set_default_name (const gchar * name)
399 g_free (_default_name);
401 _default_name = g_strdup (name);
405 * gst_scheduler_factory_get_default_name:
407 * Get the default schedulerfactory name.
409 * Returns: the name of the default scheduler.
412 gst_scheduler_factory_get_default_name (void)
414 return _default_name;