gstthread.c 22.1 KB
Newer Older
1
2
3
4
5
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstthread.c: Threaded container object
Erik Walthinsen's avatar
Erik Walthinsen committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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.
 */

23
#include <unistd.h>
Erik Walthinsen's avatar
Erik Walthinsen committed
24

25
/* #define GST_DEBUG_ENABLED */
26
27
#include "gst_private.h"

28
#include "gst.h"
29
#include "gstthread.h"
30
31
#include "gstscheduler.h"
#include "gstqueue.h"
32

Erik Walthinsen's avatar
Erik Walthinsen committed
33
34
GstElementDetails gst_thread_details = {
  "Threaded container",
35
  "Generic/Bin",
Erik Walthinsen's avatar
Erik Walthinsen committed
36
37
38
  "Container that creates/manages a thread",
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>",
39
  "(C) 1999, 2000",
Erik Walthinsen's avatar
Erik Walthinsen committed
40
41
42
43
44
};


/* Thread signals and args */
enum {
45
  SHUTDOWN,
Erik Walthinsen's avatar
Erik Walthinsen committed
46
47
48
49
  /* FILL ME */
  LAST_SIGNAL
};

50
51
52
53
54
55
enum {
  SPINUP=0,
  STATECHANGE,
  STARTUP
};

Erik Walthinsen's avatar
Erik Walthinsen committed
56
57
enum {
  ARG_0,
58
59
  ARG_SCHEDPOLICY,
  ARG_PRIORITY,
Erik Walthinsen's avatar
Erik Walthinsen committed
60
61
62
};


63

64
65
static void			gst_thread_class_init		(GstThreadClass *klass);
static void			gst_thread_init			(GstThread *thread);
Erik Walthinsen's avatar
Erik Walthinsen committed
66

67
static void 			gst_thread_dispose 	(GObject *object);
68

69
70
static void			gst_thread_set_property		(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void			gst_thread_get_property		(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
71

72
static GstElementStateReturn	gst_thread_change_state		(GstElement *element);
Erik Walthinsen's avatar
Erik Walthinsen committed
73

74
#ifndef GST_DISABLE_LOADSAVE
75
76
static xmlNodePtr		gst_thread_save_thyself		(GstObject *object, xmlNodePtr parent);
static void			gst_thread_restore_thyself	(GstObject *object, xmlNodePtr self);
77
#endif
Erik Walthinsen's avatar
Erik Walthinsen committed
78

79
static void*			gst_thread_main_loop		(void *arg);
Erik Walthinsen's avatar
Erik Walthinsen committed
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define GST_TYPE_THREAD_SCHEDPOLICY (gst_thread_schedpolicy_get_type())
static GType
gst_thread_schedpolicy_get_type(void) {
  static GType thread_schedpolicy_type = 0;
  static GEnumValue thread_schedpolicy[] = {
    {SCHED_OTHER, "SCHED_OTHER", "Normal Scheduling"},
    {SCHED_FIFO,  "SCHED_FIFO",  "FIFO Scheduling (requires root)"},
    {SCHED_RR,    "SCHED_RR",    "Round-Robin Scheduling (requires root)"},
    {0, NULL, NULL},
  };
  if (!thread_schedpolicy_type) {
    thread_schedpolicy_type = g_enum_register_static("GstThreadSchedPolicy", thread_schedpolicy);
  }
  return thread_schedpolicy_type;
}

97
static GstBinClass *parent_class = NULL;
98
static guint gst_thread_signals[LAST_SIGNAL] = { 0 }; 
Erik Walthinsen's avatar
Erik Walthinsen committed
99

100
GType
Erik Walthinsen's avatar
Erik Walthinsen committed
101
gst_thread_get_type(void) {
102
  static GType thread_type = 0;
Erik Walthinsen's avatar
Erik Walthinsen committed
103
104

  if (!thread_type) {
105
    static const GTypeInfo thread_info = {
Erik Walthinsen's avatar
Erik Walthinsen committed
106
      sizeof(GstThreadClass),
107
108
109
110
111
112
113
114
      NULL,
      NULL,
      (GClassInitFunc)gst_thread_class_init,
      NULL,
      NULL,
      sizeof(GstThread),
      4,
      (GInstanceInitFunc)gst_thread_init,
115
      NULL
Erik Walthinsen's avatar
Erik Walthinsen committed
116
    };
117
    thread_type = g_type_register_static(GST_TYPE_BIN, "GstThread", &thread_info, 0);
Erik Walthinsen's avatar
Erik Walthinsen committed
118
119
120
121
122
  }
  return thread_type;
}

static void
123
gst_thread_class_init (GstThreadClass *klass)
124
{
125
  GObjectClass *gobject_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
126
127
  GstObjectClass *gstobject_class;
  GstElementClass *gstelement_class;
128
  GstBinClass *gstbin_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
129

130
  gobject_class =	(GObjectClass*)klass;
131
132
133
  gstobject_class =	(GstObjectClass*)klass;
  gstelement_class =	(GstElementClass*)klass;
  gstbin_class =	(GstBinClass*)klass;
Erik Walthinsen's avatar
Erik Walthinsen committed
134

135
  parent_class = g_type_class_ref (GST_TYPE_BIN);
Erik Walthinsen's avatar
Erik Walthinsen committed
136

137
138
139
140
141
142
143
144
145
146
147
148
  g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_SCHEDPOLICY,
    g_param_spec_enum("schedpolicy", "Scheduling Policy", "The scheduling policy of the thread",
                      GST_TYPE_THREAD_SCHEDPOLICY, SCHED_OTHER, G_PARAM_READWRITE));
  g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_PRIORITY,
    g_param_spec_int("priority", "Scheduling Priority", "The scheduling priority of the thread",
                     0, 99, 0, G_PARAM_READWRITE));

  gst_thread_signals[SHUTDOWN] =
    g_signal_new ("shutdown", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GstThreadClass, shutdown), NULL, NULL,
                  gst_marshal_VOID__VOID, G_TYPE_NONE, 0);

149
  gobject_class->dispose =		gst_thread_dispose;
150

151
#ifndef GST_DISABLE_LOADSAVE
152
  gstobject_class->save_thyself =	GST_DEBUG_FUNCPTR (gst_thread_save_thyself);
153
  gstobject_class->restore_thyself =	GST_DEBUG_FUNCPTR (gst_thread_restore_thyself);
154
#endif
155

156
  gstelement_class->change_state =	GST_DEBUG_FUNCPTR (gst_thread_change_state);
Erik Walthinsen's avatar
Erik Walthinsen committed
157

158
159
  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
Wim Taymans's avatar
Wim Taymans committed
160

Erik Walthinsen's avatar
Erik Walthinsen committed
161
162
}

163
164
static void
gst_thread_init (GstThread *thread)
165
{
166
  GstScheduler *scheduler;
167

168
  GST_DEBUG (GST_CAT_THREAD, "initializing thread");
169

170
  /* threads are managing bins and iterate themselves */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
171
  /* CR1: the GstBin code checks these flags */
172
  GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
173
  GST_FLAG_SET (thread, GST_BIN_SELF_SCHEDULABLE);
174

175
  scheduler = gst_scheduler_factory_make (NULL, GST_ELEMENT (thread));
176
177
178
179
180

  thread->lock = g_mutex_new ();
  thread->cond = g_cond_new ();

  thread->ppid = getpid ();
181
  thread->thread_id = -1;
182
183
  thread->sched_policy = SCHED_OTHER;
  thread->priority = 0;
Erik Walthinsen's avatar
Erik Walthinsen committed
184
185
}

186
static void
187
gst_thread_dispose (GObject *object)
188
{
189
  GstThread *thread = GST_THREAD (object);
190

191
  GST_DEBUG (GST_CAT_REFCOUNTING, "dispose");
192
193
194

  g_mutex_free (thread->lock);
  g_cond_free (thread->cond);
195

196
  G_OBJECT_CLASS (parent_class)->dispose (object);
197

198
199
200
  if (GST_ELEMENT_SCHED (thread)) {
    gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
  }
201
202
203
}

static void
204
gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
205
{
206
207
  GstThread *thread;

Erik Walthinsen's avatar
Erik Walthinsen committed
208
  /* it's not null if we got it, but it might not be ours */
209
  g_return_if_fail (GST_IS_THREAD (object));
Erik Walthinsen's avatar
Erik Walthinsen committed
210

211
212
  thread = GST_THREAD (object);

213
  switch (prop_id) {
214
215
216
217
218
219
    case ARG_SCHEDPOLICY:
      thread->sched_policy = g_value_get_enum (value);
      break;
    case ARG_PRIORITY:
      thread->priority = g_value_get_int (value);
      break;
Erik Walthinsen's avatar
Erik Walthinsen committed
220
    default:
221
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
222
223
224
225
      break;
  }
}

226
static void
227
gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
228
{
229
230
  GstThread *thread;

Erik Walthinsen's avatar
Erik Walthinsen committed
231
  /* it's not null if we got it, but it might not be ours */
232
  g_return_if_fail (GST_IS_THREAD (object));
Erik Walthinsen's avatar
Erik Walthinsen committed
233

234
235
  thread = GST_THREAD (object);

236
  switch (prop_id) {
237
238
239
240
241
242
    case ARG_SCHEDPOLICY:
      g_value_set_enum (value, thread->sched_policy);
      break;
    case ARG_PRIORITY:
      g_value_set_int (value, thread->priority);
      break;
Erik Walthinsen's avatar
Erik Walthinsen committed
243
    default:
244
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
245
246
247
248
      break;
  }
}

249

250
251
252
253
/**
 * gst_thread_new:
 * @name: the name of the thread
 *
Erik Walthinsen's avatar
Erik Walthinsen committed
254
 * Create a new thread with the given name.
255
 *
Wim Taymans's avatar
Wim Taymans committed
256
 * Returns: The new thread
257
 */
258
GstElement*
259
gst_thread_new (const gchar *name)
260
{
261
  return gst_element_factory_make ("thread", name);
Erik Walthinsen's avatar
Erik Walthinsen committed
262
263
264
}


265
266
267
268
269
270
271
272
273
274
275
276
277
278
#define THR_INFO(format,args...) \
  GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
  GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
#define THR_DEBUG(format,args...) \
  GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
  GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )

#define THR_INFO_MAIN(format,args...) \
  GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
  GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
#define THR_DEBUG_MAIN(format,args...) \
  GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
  GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )

279
280
281
static GstElementStateReturn 
gst_thread_update_state (GstThread *thread)
{
282
  /* check for state change */
283
  if (GST_STATE_PENDING(thread) != GST_STATE_VOID_PENDING) {
284
    /* punt and change state on all the children */
285
286
287
288
289
290
291
    if (GST_ELEMENT_CLASS (parent_class)->change_state)
      return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
  }

  return GST_STATE_SUCCESS;
}

Erik Walthinsen's avatar
Erik Walthinsen committed
292

293
static GstElementStateReturn
294
gst_thread_change_state (GstElement * element)
295
{
Erik Walthinsen's avatar
Erik Walthinsen committed
296
  GstThread *thread;
Wim Taymans's avatar
Wim Taymans committed
297
  gboolean stateset = GST_STATE_SUCCESS;
298
  gint transition;
299
  pthread_t self = pthread_self ();
300
  glong stacksize;
Erik Walthinsen's avatar
Erik Walthinsen committed
301

302
  g_return_val_if_fail (GST_IS_THREAD (element), GST_STATE_FAILURE);
303
  g_return_val_if_fail (gst_has_threads (), GST_STATE_FAILURE);
304

305
  thread = GST_THREAD (element);
Erik Walthinsen's avatar
Erik Walthinsen committed
306

307
  transition = GST_STATE_TRANSITION (element);
308

309
  THR_INFO ("changing state from %s to %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
310
311
	    gst_element_state_get_name (GST_STATE (element)),
	    gst_element_state_get_name (GST_STATE_PENDING (element)));
312

313
314
  if (pthread_equal (self, thread->thread_id)) {
    GST_DEBUG (GST_CAT_THREAD,
315
	       "no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning",
316
317
318
	       GST_DEBUG_THREAD_ARGS (thread->pid));
    return gst_thread_update_state (thread);
  }
319

320
321
  switch (transition) {
    case GST_STATE_NULL_TO_READY:
322
      /* set the state to idle */
323
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
324

325
      THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (element));
326
327
328

      g_mutex_lock (thread->lock);

329
330
      if (pthread_attr_init (&thread->attr) != 0)
	g_warning ("pthread_attr_init returned an error !");
331

332
333
334
335
336
337
338
      if (gst_scheduler_get_preferred_stack (GST_ELEMENT_SCHED (element), &thread->stack, &stacksize)) {
        if (pthread_attr_setstack (&thread->attr, thread->stack, stacksize) != 0) {
          g_warning ("pthread_attr_setstack failed");
          return GST_STATE_FAILURE;
        }
	GST_DEBUG (GST_CAT_THREAD, "pthread attr set stack at %p of size %ld", 
		   thread->stack, stacksize);
339
      }
340

341
      /* create the thread */
342
      THR_DEBUG ("going to pthread_create...");
343
      if (pthread_create (&thread->thread_id, &thread->attr, gst_thread_main_loop, thread) != 0) {
344
        THR_DEBUG ("pthread create failed");
345
346
347
348
	g_mutex_unlock (thread->lock);
        THR_DEBUG ("could not create thread \"%s\"", GST_ELEMENT_NAME (element));
	return GST_STATE_FAILURE;
      }
349
      THR_DEBUG ("pthread created");
350

351
      /* wait for it to 'spin up' */
352
      THR_DEBUG ("waiting for child thread spinup");
353
      g_cond_wait (thread->cond, thread->lock);
354
      THR_DEBUG ("thread claims to be up");
355
      g_mutex_unlock (thread->lock);
356
357
      break;
    case GST_STATE_READY_TO_PAUSED:
358
359
      THR_INFO ("readying thread");
      g_mutex_lock (thread->lock);
360
      THR_DEBUG ("signaling");
361
      g_cond_signal (thread->cond);
362
      THR_DEBUG ("waiting for ack");
363
      g_cond_wait (thread->cond, thread->lock);
364
      THR_DEBUG ("got ack");
365
      g_mutex_unlock (thread->lock);
Erik Walthinsen's avatar
Erik Walthinsen committed
366
      break;
367
    case GST_STATE_PAUSED_TO_PLAYING:
368
369
370
371
372
373
374
375
    {
      /* fixme: recurse into sub-bins */
      const GList *elements = gst_bin_get_list (GST_BIN (thread));
      while (elements) {
        gst_element_enable_threadsafe_properties ((GstElement*)elements->data);
        elements = g_list_next (elements);
      }
      
376
      THR_DEBUG ("telling thread to start spinning");
377
      g_mutex_lock (thread->lock);
378
      THR_DEBUG ("signaling");
379
      g_cond_signal (thread->cond);
380
      THR_DEBUG ("waiting for ack");
381
      g_cond_wait (thread->cond, thread->lock);
382
      THR_DEBUG ("got ack");
383
      g_mutex_unlock (thread->lock);
384
      break;
385
    }
386
    case GST_STATE_PLAYING_TO_PAUSED:
387
    {
388
      const GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
389
390
391

      THR_INFO ("pausing thread");

392
393
394
395
      /* the following code ensures that the bottom half of thread will run
       * to perform each elements' change_state() (by calling gstbin.c::
       * change_state()).
       * + the pending state was already set by gstelement.c::set_state()
396
       * + unlock all elements so the bottom half can start the state change.
397
       */ 
398
      g_mutex_lock (thread->lock);
399
400
401
402

      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);

      while (elements) {
Wim Taymans's avatar
Wim Taymans committed
403
	GstElement *element = GST_ELEMENT (elements->data);
404
405
	GList *pads;
	
406

Wim Taymans's avatar
Wim Taymans committed
407
	g_assert (element);
408
	THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (element));
409
	elements = g_list_next (elements);
410
411
412

	if (!gst_element_release_locks (element)) {
          g_warning ("element %s could not release locks", GST_ELEMENT_NAME (element));
413
414
	}

415
	pads = GST_ELEMENT_PADS (element);
416

417
418
419
	while (pads) {
	  GstRealPad *peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
	  GstElement *peerelement;
420

421
	  pads = g_list_next (pads);
422

423
424
	  if (!peer)
	    continue;
425

426
427
428
	  peerelement = GST_PAD_PARENT (peer);
	  if (!peerelement)
	    continue;		/* deal with case where there's no peer */
429

430
431
432
433
	  if (!GST_FLAG_IS_SET (peerelement, GST_ELEMENT_DECOUPLED)) {
	    GST_DEBUG (GST_CAT_THREAD, "peer element isn't DECOUPLED");
	    continue;
	  }
434

435
436
	  if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
	    THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
437
438
439
	    THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (peerelement));
	    if (!gst_element_release_locks (peerelement)) {
              g_warning ("element %s could not release locks", GST_ELEMENT_NAME (peerelement));
440
441
442
	    }
	  }
	}
443

444
      }
445
      THR_DEBUG ("telling thread to pause, signaling");
446
      g_cond_signal (thread->cond);
447
      THR_DEBUG ("waiting for ack");
448
      g_cond_wait (thread->cond, thread->lock);
449
      THR_DEBUG ("got ack");
450
      g_mutex_unlock (thread->lock);
451
452
453
454
455
456

      elements = gst_bin_get_list (GST_BIN (thread));
      while (elements) {
        gst_element_disable_threadsafe_properties ((GstElement*)elements->data);
        elements = g_list_next (elements);
      }
Erik Walthinsen's avatar
Erik Walthinsen committed
457
      break;
458
    }
459
    case GST_STATE_READY_TO_NULL:
460
      THR_DEBUG ("telling thread to pause (null) - and joining");
461
      /* MattH FIXME revisit */
462
      g_mutex_lock (thread->lock);
463
      THR_DEBUG ("signaling");
464
      g_cond_signal (thread->cond);
465
      THR_DEBUG ("waiting for ack");
466
      g_cond_wait (thread->cond, thread->lock);
467
      THR_DEBUG ("got ack");
468
469
470
471
      if (pthread_join (thread->thread_id, NULL) != 0)
        g_warning ("pthread_join has failed !\n");
      if (pthread_attr_destroy (&thread->attr) != 0)
	g_warning ("pthread_attr_destroy has failed !\n");
472
473
      thread->thread_id = -1;
      g_mutex_unlock (thread->lock);
474

475
476
477
478
479
480
      if (thread->stack) {
        GST_DEBUG (GST_CAT_THREAD, "freeing allocated stack (%p)", thread->stack);
        free (thread->stack);
        thread->stack = NULL;
      }
 
481
482
483
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
484

485
486
      break;
    case GST_STATE_PAUSED_TO_READY:
487
      THR_DEBUG ("telling thread to stop spinning");
488
      g_mutex_lock (thread->lock);
489
      THR_DEBUG ("signaling");
490
      g_cond_signal (thread->cond);
491
      THR_DEBUG ("waiting for ack");
492
      g_cond_wait (thread->cond, thread->lock);
493
      THR_DEBUG ("got ack");
494
495
      g_mutex_unlock (thread->lock);

496
      break;
Erik Walthinsen's avatar
Erik Walthinsen committed
497
    default:
498
      GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x", transition);
Erik Walthinsen's avatar
Erik Walthinsen committed
499
500
501
502
503
504
      break;
  }

  return stateset;
}

505
506
507
508
509
/**
 * gst_thread_main_loop:
 * @arg: the thread to start
 *
 * The main loop of the thread. The thread will iterate
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
510
 * while the state is GST_THREAD_STATE_SPINNING.
511
 */
Wim Taymans's avatar
Wim Taymans committed
512
static void *
513
gst_thread_main_loop (void *arg)
Wim Taymans's avatar
Wim Taymans committed
514
{
515
  GstThread *thread = NULL;
516
517
  gint stateset;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
518
  GST_DEBUG (GST_CAT_THREAD, "gst_thread_main_loop started");
519
  thread = GST_THREAD (arg);
520
521
  g_mutex_lock (thread->lock);

522
523
524
525
526
527
528
529
530
531
532
533
534
535
  if (thread->sched_policy != SCHED_OTHER) {
    struct sched_param sched_param;

    memset (&sched_param, 0, sizeof (sched_param));
    if (thread->priority == 0) {
      thread->priority = sched_get_priority_max (thread->sched_policy);
    }
    sched_param.sched_priority = thread->priority;

    if (sched_setscheduler (0, thread->sched_policy, &sched_param) != 0) {
      GST_DEBUG (GST_CAT_THREAD, "not running with real-time priority");
    }
  }

Wim Taymans's avatar
Wim Taymans committed
536
  gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
537
538
  GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);

539
540
  thread->pid = getpid();
  THR_INFO_MAIN("thread is running");
Erik Walthinsen's avatar
Erik Walthinsen committed
541

542
  /* first we need to change the state of all the children */
543
  if (GST_ELEMENT_CLASS (parent_class)->change_state) {
544
    stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
Erik Walthinsen's avatar
Erik Walthinsen committed
545

546
    if (stateset != GST_STATE_SUCCESS) {
547
      THR_DEBUG_MAIN ("state change of children failed");
548
549
550
    }
  }

551

552
  THR_DEBUG_MAIN ("indicating spinup");
553
  g_cond_signal (thread->cond);
554
  /* don't unlock the mutex because we hold it into the top of the while loop */
555
  THR_DEBUG_MAIN ("thread has indicated spinup to parent process");
556

557
  /***** THREAD IS NOW IN READY STATE *****/
558

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
559
560
  /* CR1: most of this code is handshaking */
  /* do this while the thread lives */
561
  while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
562
563
    /* NOTE we hold the thread lock at this point */
    /* what we do depends on what state we're in */
564
    switch (GST_STATE (thread)) {
565
      /* NOTE: cannot be in NULL, we're not running in that state at all */
566
      case GST_STATE_READY:
567
        /* wait to be set to either the NULL or PAUSED states */
568
        THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
569
570
571
                        gst_element_state_get_name (GST_STATE_READY),
                        gst_element_state_get_name (GST_STATE_NULL),
                        gst_element_state_get_name (GST_STATE_PAUSED));
572
573
        g_cond_wait (thread->cond,thread->lock);
	
Wim Taymans's avatar
Wim Taymans committed
574
575
576
577
578
579
	/* this must have happened by a state change in the thread context */
	if (GST_STATE_PENDING (thread) != GST_STATE_NULL &&
	    GST_STATE_PENDING (thread) != GST_STATE_PAUSED) {
          g_cond_signal (thread->cond);
	  continue;
	}
580

581
        /* been signaled, we need to state transition now and signal back */
582
        gst_thread_update_state (thread);
583
        THR_DEBUG_MAIN ("done with state transition, signaling back to parent process");
584
        g_cond_signal (thread->cond);
585
        /* now we decide what to do next */
586
        if (GST_STATE (thread) == GST_STATE_NULL) {
587
          /* REAPING must be set, we can simply break this iteration */
588
589
590
          GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
	}
        continue;
591
      case GST_STATE_PAUSED:
592
        /* wait to be set to either the READY or PLAYING states */
593
        THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
594
595
596
                       gst_element_state_get_name (GST_STATE_PAUSED),
                       gst_element_state_get_name (GST_STATE_READY),
                       gst_element_state_get_name (GST_STATE_PLAYING));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
597
        g_cond_wait (thread->cond, thread->lock);
598

Wim Taymans's avatar
Wim Taymans committed
599
600
601
602
603
604
	/* this must have happened by a state change in the thread context */
	if (GST_STATE_PENDING (thread) != GST_STATE_READY &&
	    GST_STATE_PENDING (thread) != GST_STATE_PLAYING) {
          g_cond_signal (thread->cond);
	  continue;
	}
605

606
        /* been signaled, we need to state transition now and signal back */
607
        gst_thread_update_state (thread);
608
        /* now we decide what to do next */
609
        if (GST_STATE (thread) != GST_STATE_PLAYING) {
610
          /* either READY or the state change failed for some reason */
611
          g_cond_signal (thread->cond);
612
          continue;
613
614
615
        } 
	else {
          GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
616
          /* PLAYING is coming up, so we can now start spinning */
617
          while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
618
619
620
621
622
623
	    gboolean status;

            g_cond_signal (thread->cond);
            g_mutex_unlock (thread->lock);
            status = gst_bin_iterate (GST_BIN (thread));
            g_mutex_lock (thread->lock);
624
            /* g_cond_signal(thread->cond); */
625
626
627

	    if (!status || GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING)
              GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
628
          }
629
	  /* looks like we were stopped because of a statechange */
630
631
632
	  if (GST_STATE_PENDING (thread)) {
            gst_thread_update_state (thread);
	  }
633
          /* once we're here, SPINNING has stopped, we should signal that we're done */
634
          THR_DEBUG_MAIN ("SPINNING stopped, signaling back to parent process");
635
          g_cond_signal (thread->cond);
636
          /* now we can wait for PAUSED */
637
638
639
          continue;
        }
      case GST_STATE_PLAYING:
640
        /* wait to be set to PAUSED */
641
        THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
642
643
                        gst_element_state_get_name(GST_STATE_PLAYING),
                        gst_element_state_get_name(GST_STATE_PAUSED));
644
645
        g_cond_wait (thread->cond,thread->lock);

646
        /* been signaled, we need to state transition now and signal back */
647
648
        gst_thread_update_state (thread);
        g_cond_signal (thread->cond);
649
650
        /* now we decide what to do next */
        /* there's only PAUSED, we we just wait for it */
651
        continue;
652
      case GST_STATE_NULL:
653
        THR_DEBUG_MAIN ("thread in %s state, preparing to die",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
654
                        gst_element_state_get_name(GST_STATE_NULL));
655
656
657
658
        GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
        break;
      default:
	g_assert_not_reached ();
659
660
        break;
    }
Erik Walthinsen's avatar
Erik Walthinsen committed
661
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
662
  /* we need to destroy the scheduler here because it has mapped it's
Wim Taymans's avatar
Wim Taymans committed
663
664
665
   * stack into the threads stack space */
  gst_scheduler_reset (GST_ELEMENT_SCHED (thread));

666
  /* since we don't unlock at the end of the while loop, do it here */
667
  g_mutex_unlock (thread->lock);
Erik Walthinsen's avatar
Erik Walthinsen committed
668

669
  GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
670
		  GST_ELEMENT_NAME (thread));
671
672
673

  g_signal_emit (G_OBJECT (thread), gst_thread_signals[SHUTDOWN], 0);

Erik Walthinsen's avatar
Erik Walthinsen committed
674
675
676
  return NULL;
}

677
#ifndef GST_DISABLE_LOADSAVE
678
679
680
681
682
683
684
685
686
static xmlNodePtr
gst_thread_save_thyself (GstObject *object,
		         xmlNodePtr self)
{
  if (GST_OBJECT_CLASS (parent_class)->save_thyself)
    GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
  return NULL;
}

Wim Taymans's avatar
Wim Taymans committed
687
static void
688
689
gst_thread_restore_thyself (GstObject *object,
		            xmlNodePtr self)
690
{
691
  GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
692

693
694
  if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
    GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
695
}
696
#endif /* GST_DISABLE_LOADSAVE */