summaryrefslogtreecommitdiff
path: root/gst/gstscheduler.c
blob: d11c93410c1d1edcbb8067e7a809c5c8aebe645b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wim.taymans@chello.be>
 *
 * gstscheduler.c: Default scheduling code for most cases
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#define CLASS(obj)	GST_SCHEDULER_CLASS (G_OBJECT_GET_CLASS (obj))

#include "gst_private.h"

#include "gstscheduler.h"

static void 	gst_scheduler_class_init 	(GstSchedulerClass *klass);
static void 	gst_scheduler_init 		(GstScheduler *sched);

static GstObjectClass *parent_class = NULL;

GType
gst_scheduler_get_type (void)
{
  static GType _gst_scheduler_type = 0;

  if (!_gst_scheduler_type) {
    static const GTypeInfo scheduler_info = {
      sizeof (GstSchedulerClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_scheduler_class_init,
      NULL,
      NULL,
      sizeof (GstScheduler),
      0,
      (GInstanceInitFunc) gst_scheduler_init,
      NULL
    };

    _gst_scheduler_type = g_type_register_static (GST_TYPE_OBJECT, "GstScheduler", 
		    &scheduler_info, G_TYPE_FLAG_ABSTRACT);
  }
  return _gst_scheduler_type;
}

static void
gst_scheduler_class_init (GstSchedulerClass *klass)
{
  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
}

static void
gst_scheduler_init (GstScheduler *sched)
{
}

/**
 * gst_scheduler_pad_connect:
 * @sched: the schedulerr
 * @srcpad: the srcpad to connect
 * @sinkpad: the sinkpad to connect to
 *
 * Connect the srcpad to the given sinkpad.
 */
void
gst_scheduler_pad_connect (GstScheduler *sched, GstPad *srcpad, GstPad *sinkpad)
{
  if (CLASS (sched)->pad_connect)
    CLASS (sched)->pad_connect (sched, srcpad, sinkpad);
}

/**
 * gst_scheduler_pad_disconnect:
 * @sched: the schedulerr
 * @srcpad: the srcpad to disconnect
 * @sinkpad: the sinkpad to disconnect from
 *
 * Disconnect the srcpad to the given sinkpad.
 */
void
gst_scheduler_pad_disconnect (GstScheduler *sched, GstPad *srcpad, GstPad *sinkpad)
{
  if (CLASS (sched)->pad_disconnect)
    CLASS (sched)->pad_disconnect (sched, srcpad, sinkpad);
}

/**
 * gst_scheduler_pad_select:
 * @sched: the schedulerr
 * @padlist: the padlist to select on
 *
 * register the given padlist for a select operation. 
 *
 * Returns: the pad which received a buffer.
 */
GstPad *
gst_scheduler_pad_select (GstScheduler *sched, GList *padlist)
{
  if (CLASS (sched)->pad_select)
    CLASS (sched)->pad_select (sched, padlist);
}

/**
 * gst_scheduler_add_element:
 * @sched: the schedulerr
 * @element: the element to add to the schedulerr
 *
 * Add an element to the schedulerr.
 */
void
gst_scheduler_add_element (GstScheduler *sched, GstElement *element)
{
  if (CLASS (sched)->add_element)
    CLASS (sched)->add_element (sched, element);
}

/**
 * gst_scheduler_enable_element:
 * @sched: the schedulerr
 * @element: the element to enable
 *
 * Enable an element for scheduling.
 */
void
gst_scheduler_enable_element (GstScheduler *sched, GstElement *element)
{
  if (CLASS (sched)->enable_element)
    CLASS (sched)->enable_element (sched, element);
}

/**
 * gst_scheduler_disable_element:
 * @sched: the schedulerr
 * @element: the element to disable
 *
 * Disable an element for scheduling.
 */
void
gst_scheduler_disable_element (GstScheduler *sched, GstElement *element)
{
  if (CLASS (sched)->disable_element)
    CLASS (sched)->disable_element (sched, element);
}

/**
 * gst_scheduler_remove_element:
 * @sched: the schedulerr
 * @element: the element to remove
 *
 * Remove an element from the schedulerr.
 */
void
gst_scheduler_remove_element (GstScheduler *sched, GstElement *element)
{
  if (CLASS (sched)->remove_element)
    CLASS (sched)->remove_element (sched, element);
}

void
gst_scheduler_lock_element (GstScheduler *sched, GstElement *element)
{
  if (CLASS (sched)->lock_element)
    CLASS (sched)->lock_element (sched, element);
}

void
gst_scheduler_unlock_element (GstScheduler *sched, GstElement *element)
{
  if (CLASS (sched)->unlock_element)
    CLASS (sched)->unlock_element (sched, element);
}

/**
 * gst_scheduler_iterate:
 * @sched: the schedulerr
 *
 * Perform one iteration on the schedulerr.
 *
 * Returns: a boolean indicating something usefull has happened.
 */
gboolean
gst_scheduler_iterate (GstScheduler *sched)
{
  if (CLASS (sched)->iterate)
    CLASS (sched)->iterate (sched);
}


/**
 * gst_scheduler_show:
 * @sched: the schedulerr
 *
 * Dump the state of the schedulerr
 */
void
gst_scheduler_show (GstScheduler *sched)
{
  if (CLASS (sched)->show)
    CLASS (sched)->show (sched);
}

/*
 * Factory stuff starts here
 *
 */

static GList* _gst_schedulerfactories;

static void 		gst_schedulerfactory_class_init		(GstSchedulerFactoryClass *klass);
static void 		gst_schedulerfactory_init 		(GstSchedulerFactory *factory);

#ifndef GST_DISABLE_REGISTRY
static xmlNodePtr 	gst_schedulerfactory_save_thyself 	(GstObject *object, xmlNodePtr parent);
static void 		gst_schedulerfactory_restore_thyself 	(GstObject *object, xmlNodePtr parent);
#endif

static GstPluginFeatureClass *factory_parent_class = NULL;
//static guint gst_schedulerfactory_signals[LAST_SIGNAL] = { 0 };

GType 
gst_schedulerfactory_get_type (void) 
{
  static GType schedulerfactory_type = 0;

  if (!schedulerfactory_type) {
    static const GTypeInfo schedulerfactory_info = {
      sizeof (GstSchedulerFactoryClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_schedulerfactory_class_init,
      NULL,
      NULL,
      sizeof(GstSchedulerFactory),
      0,
      (GInstanceInitFunc) gst_schedulerfactory_init,
      NULL
    };
    schedulerfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE, 
		    				  "GstSchedulerFactory", &schedulerfactory_info, 0);
  }
  return schedulerfactory_type;
}

static void
gst_schedulerfactory_class_init (GstSchedulerFactoryClass *klass)
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;
  GstPluginFeatureClass *gstpluginfeature_class;

  gobject_class = (GObjectClass*)klass;
  gstobject_class = (GstObjectClass*)klass;
  gstpluginfeature_class = (GstPluginFeatureClass*) klass;

  factory_parent_class = g_type_class_ref (GST_TYPE_PLUGIN_FEATURE);

#ifndef GST_DISABLE_REGISTRY
  gstobject_class->save_thyself = 	GST_DEBUG_FUNCPTR (gst_schedulerfactory_save_thyself);
  gstobject_class->restore_thyself = 	GST_DEBUG_FUNCPTR (gst_schedulerfactory_restore_thyself);
#endif

  _gst_schedulerfactories = NULL;
}

static void
gst_schedulerfactory_init (GstSchedulerFactory *factory)
{
  _gst_schedulerfactories = g_list_prepend (_gst_schedulerfactories, factory);
}
	

/**
 * gst_schedulerfactory_new:
 * @name: name of schedulerfactory to create
 * @longdesc: long description of schedulerfactory to create
 * @type: the gtk type of the GstScheduler element of this factory
 *
 * Create a new schedulerfactory with the given parameters
 *
 * Returns: a new #GstSchedulerFactory.
 */
GstSchedulerFactory*
gst_schedulerfactory_new (const gchar *name, const gchar *longdesc, GType type)
{
  GstSchedulerFactory *factory;

  g_return_val_if_fail(name != NULL, NULL);
  factory = gst_schedulerfactory_find (name);
  if (!factory) {
    factory = GST_SCHEDULERFACTORY (g_object_new (GST_TYPE_SCHEDULERFACTORY, NULL));
  }

  gst_object_set_name (GST_OBJECT (factory), name);
  if (factory->longdesc)
    g_free (factory->longdesc);
  factory->longdesc = g_strdup (longdesc);
  factory->type = type;

  return factory;
}

/**
 * gst_schedulerfactory_destroy:
 * @factory: factory to destroy
 *
 * Removes the scheduler from the global list.
 */
void
gst_schedulerfactory_destroy (GstSchedulerFactory *factory)
{
  g_return_if_fail (factory != NULL);

  _gst_schedulerfactories = g_list_remove (_gst_schedulerfactories, factory);

  // we don't free the struct bacause someone might  have a handle to it..
}

/**
 * gst_schedulerfactory_find:
 * @name: name of schedulerfactory to find
 *
 * Search for an schedulerfactory of the given name.
 *
 * Returns: #GstSchedulerFactory if found, NULL otherwise
 */
GstSchedulerFactory*
gst_schedulerfactory_find (const gchar *name)
{
  GList *walk;
  GstSchedulerFactory *factory;

  g_return_val_if_fail(name != NULL, NULL);

  GST_DEBUG (0,"gstscheduler: find \"%s\"\n", name);

  walk = _gst_schedulerfactories;
  while (walk) {
    factory = (GstSchedulerFactory *)(walk->data);
    if (!strcmp (name, GST_OBJECT_NAME (factory)))
      return factory;
    walk = g_list_next (walk);
  }

  return NULL;
}

/**
 * gst_schedulerfactory_get_list:
 *
 * Get the global list of schedulerfactories.
 *
 * Returns: GList of type #GstSchedulerFactory
 */
GList*
gst_schedulerfactory_get_list (void)
{
  return _gst_schedulerfactories;
}

/**
 * gst_schedulerfactory_create:
 * @factory: the factory used to create the instance
 *
 * Create a new #GstScheduler instance from the 
 * given schedulerfactory.
 *
 * Returns: A new #GstScheduler instance.
 */
GstScheduler*
gst_schedulerfactory_create (GstSchedulerFactory *factory, GstElement *parent)
{
  GstScheduler *new = NULL;

  g_return_val_if_fail (factory != NULL, NULL);

  if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
    g_return_val_if_fail (factory->type != 0, NULL);

    new = GST_SCHEDULER (g_object_new (factory->type, NULL));
    new->parent = parent;
  }

  return new;
}

/**
 * gst_schedulerfactory_make:
 * @name: the name of the factory used to create the instance
 *
 * Create a new #GstScheduler instance from the 
 * schedulerfactory with the given name.
 *
 * Returns: A new #GstScheduler instance.
 */
GstScheduler*
gst_schedulerfactory_make (const gchar *name, GstElement *parent)
{
  GstSchedulerFactory *factory;

  g_return_val_if_fail (name != NULL, NULL);

  factory = gst_schedulerfactory_find (name);

  if (factory == NULL)
    return NULL;

  return gst_schedulerfactory_create (factory, parent);
}

#ifndef GST_DISABLE_REGISTRY
static xmlNodePtr
gst_schedulerfactory_save_thyself (GstObject *object, xmlNodePtr parent)
{
  GstSchedulerFactory *factory;

  g_return_val_if_fail (GST_IS_SCHEDULERFACTORY (object), parent);

  factory = GST_SCHEDULERFACTORY (object);

  if (GST_OBJECT_CLASS (factory_parent_class)->save_thyself) {
    GST_OBJECT_CLASS (factory_parent_class)->save_thyself (object, parent);
  }

  xmlNewChild (parent, NULL, "longdesc", factory->longdesc);

  return parent;
}

/**
 * gst_schedulerfactory_load_thyself:
 * @parent: the parent XML node pointer
 *
 * Load an schedulerfactory from the given XML parent node.
 *
 * Returns: A new factory based on the XML node.
 */
static void
gst_schedulerfactory_restore_thyself (GstObject *object, xmlNodePtr parent)
{
  GstSchedulerFactory *factory = GST_SCHEDULERFACTORY (object);
  xmlNodePtr children = parent->xmlChildrenNode;

  if (GST_OBJECT_CLASS (factory_parent_class)->restore_thyself) {
    GST_OBJECT_CLASS (factory_parent_class)->restore_thyself (object, parent);
  }

  while (children) {
    if (!strcmp(children->name, "name")) {
      gst_object_set_name (GST_OBJECT (factory), xmlNodeGetContent (children));
    }
    if (!strcmp(children->name, "longdesc")) {
      factory->longdesc = xmlNodeGetContent (children);
    }
    children = children->next;
  }
}
#endif /* GST_DISABLE_REGISTRY */