gstthread.c 18.8 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 "gstthread.h"
29
30
#include "gstscheduler.h"
#include "gstqueue.h"
31

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


/* Thread signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

48
49
50
51
52
53
enum {
  SPINUP=0,
  STATECHANGE,
  STARTUP
};

Erik Walthinsen's avatar
Erik Walthinsen committed
54
55
56
57
58
enum {
  ARG_0,
};


59

60
61
static void			gst_thread_class_init		(GstThreadClass *klass);
static void			gst_thread_init			(GstThread *thread);
Erik Walthinsen's avatar
Erik Walthinsen committed
62

63
static void 			gst_thread_dispose 	(GObject *object);
64

65
66
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
67

68
static GstElementStateReturn	gst_thread_change_state		(GstElement *element);
Erik Walthinsen's avatar
Erik Walthinsen committed
69

70
#ifndef GST_DISABLE_LOADSAVE
71
72
static xmlNodePtr		gst_thread_save_thyself		(GstObject *object, xmlNodePtr parent);
static void			gst_thread_restore_thyself	(GstObject *object, xmlNodePtr self);
73
#endif
Erik Walthinsen's avatar
Erik Walthinsen committed
74

75
static void*			gst_thread_main_loop		(void *arg);
Erik Walthinsen's avatar
Erik Walthinsen committed
76

77
static GstBinClass *parent_class = NULL;
78
/* static guint gst_thread_signals[LAST_SIGNAL] = { 0 }; */
Erik Walthinsen's avatar
Erik Walthinsen committed
79

80
GType
Erik Walthinsen's avatar
Erik Walthinsen committed
81
gst_thread_get_type(void) {
82
  static GType thread_type = 0;
Erik Walthinsen's avatar
Erik Walthinsen committed
83
84

  if (!thread_type) {
85
    static const GTypeInfo thread_info = {
Erik Walthinsen's avatar
Erik Walthinsen committed
86
      sizeof(GstThreadClass),
87
88
89
90
91
92
93
94
      NULL,
      NULL,
      (GClassInitFunc)gst_thread_class_init,
      NULL,
      NULL,
      sizeof(GstThread),
      4,
      (GInstanceInitFunc)gst_thread_init,
95
      NULL
Erik Walthinsen's avatar
Erik Walthinsen committed
96
    };
97
    thread_type = g_type_register_static(GST_TYPE_BIN, "GstThread", &thread_info, 0);
Erik Walthinsen's avatar
Erik Walthinsen committed
98
99
100
101
102
  }
  return thread_type;
}

static void
103
gst_thread_class_init (GstThreadClass *klass)
104
{
105
  GObjectClass *gobject_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
106
107
  GstObjectClass *gstobject_class;
  GstElementClass *gstelement_class;
108
  GstBinClass *gstbin_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
109

110
  gobject_class =	(GObjectClass*)klass;
111
112
113
  gstobject_class =	(GstObjectClass*)klass;
  gstelement_class =	(GstElementClass*)klass;
  gstbin_class =	(GstBinClass*)klass;
Erik Walthinsen's avatar
Erik Walthinsen committed
114

115
  parent_class = g_type_class_ref (GST_TYPE_BIN);
Erik Walthinsen's avatar
Erik Walthinsen committed
116

117
  gobject_class->dispose =		gst_thread_dispose;
118

119
#ifndef GST_DISABLE_LOADSAVE
120
121
  gstobject_class->save_thyself =	GST_DEBUG_FUNCPTR (gst_thread_save_thyself);
  gstobject_class->restore_thyself =	GST_DEBUG_FUNCPTR(gst_thread_restore_thyself);
122
#endif
123

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

126
/*  gstbin_class->schedule = gst_thread_schedule_dummy; */
127

128
129
  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
130

Erik Walthinsen's avatar
Erik Walthinsen committed
131
132
}

133
134
static void
gst_thread_init (GstThread *thread)
135
{
136
137
  const gchar *schedname;
  GstScheduler *scheduler;
138

139
  GST_DEBUG (GST_CAT_THREAD, "initializing thread");
140

141
  /* we're a manager by default */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
142
  /* CR1: the GstBin code checks these flags */
143
  GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
144
  GST_FLAG_SET (thread, GST_BIN_SELF_SCHEDULABLE);
145

146
  schedname = gst_scheduler_factory_get_default_name ();
147

148
  scheduler = gst_scheduler_factory_make (schedname, GST_ELEMENT (thread));
149

150
151
152
153
154
155
156
157
158
  GST_ELEMENT_SCHED (thread) = scheduler;

  gst_object_ref (GST_OBJECT (scheduler));
  gst_object_sink (GST_OBJECT (scheduler));

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

  thread->ppid = getpid ();
159
  thread->thread_id = -1;
Erik Walthinsen's avatar
Erik Walthinsen committed
160
161
}

162
static void
163
gst_thread_dispose (GObject *object)
164
{
165
  GstThread *thread = GST_THREAD (object);
166

167
  GST_DEBUG (GST_CAT_REFCOUNTING, "dispose");
168
169
170

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

172
  G_OBJECT_CLASS (parent_class)->dispose (object);
173

174
175
176
  if (GST_ELEMENT_SCHED (thread)) {
    gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
  }
177
178
179
}

static void
180
gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
181
{
Erik Walthinsen's avatar
Erik Walthinsen committed
182
  /* it's not null if we got it, but it might not be ours */
183
  g_return_if_fail (GST_IS_THREAD (object));
Erik Walthinsen's avatar
Erik Walthinsen committed
184

185
  switch (prop_id) {
Erik Walthinsen's avatar
Erik Walthinsen committed
186
    default:
187
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
188
189
190
191
      break;
  }
}

192
static void
193
gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
194
{
Erik Walthinsen's avatar
Erik Walthinsen committed
195
  /* it's not null if we got it, but it might not be ours */
196
  g_return_if_fail (GST_IS_THREAD (object));
Erik Walthinsen's avatar
Erik Walthinsen committed
197

198
  switch (prop_id) {
Erik Walthinsen's avatar
Erik Walthinsen committed
199
    default:
200
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
201
202
203
204
      break;
  }
}

205

206
207
208
209
/**
 * gst_thread_new:
 * @name: the name of the thread
 *
Erik Walthinsen's avatar
Erik Walthinsen committed
210
 * Create a new thread with the given name.
211
 *
Wim Taymans's avatar
Wim Taymans committed
212
 * Returns: The new thread
213
 */
214
GstElement*
215
gst_thread_new (const gchar *name)
216
{
217
  return gst_element_factory_make ("thread", name);
Erik Walthinsen's avatar
Erik Walthinsen committed
218
219
220
}


221
222
223
224
225
226
227
228
229
230
231
232
233
234
#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 )

235
236
237
static GstElementStateReturn 
gst_thread_update_state (GstThread *thread)
{
238
  /* check for state change */
239
  if (GST_STATE_PENDING(thread) != GST_STATE_VOID_PENDING) {
240
    /* punt and change state on all the children */
241
242
243
244
245
246
247
    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
248

249
static GstElementStateReturn
250
gst_thread_change_state (GstElement * element)
251
{
Erik Walthinsen's avatar
Erik Walthinsen committed
252
  GstThread *thread;
Wim Taymans's avatar
Wim Taymans committed
253
  gboolean stateset = GST_STATE_SUCCESS;
254
  gint transition;
255
  pthread_t self = pthread_self ();
Erik Walthinsen's avatar
Erik Walthinsen committed
256

257
  g_return_val_if_fail (GST_IS_THREAD (element), FALSE);
258

259
  thread = GST_THREAD (element);
Erik Walthinsen's avatar
Erik Walthinsen committed
260

261
  transition = GST_STATE_TRANSITION (element);
262

263
264
265
  THR_INFO ("changing state from %s to %s",
	    gst_element_statename (GST_STATE (element)),
	    gst_element_statename (GST_STATE_PENDING (element)));
266

267
268
  if (pthread_equal (self, thread->thread_id)) {
    GST_DEBUG (GST_CAT_THREAD,
269
	       "no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning",
270
271
272
	       GST_DEBUG_THREAD_ARGS (thread->pid));
    return gst_thread_update_state (thread);
  }
273

274
275
  switch (transition) {
    case GST_STATE_NULL_TO_READY:
276
      /* set the state to idle */
277
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
278

279
      THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (element));
280
281
282

      g_mutex_lock (thread->lock);

283
      /* create the thread */
284
285
      pthread_create (&thread->thread_id, NULL, gst_thread_main_loop, thread);

286
      /* wait for it to 'spin up' */
287
      THR_DEBUG ("waiting for child thread spinup");
288
      g_cond_wait (thread->cond, thread->lock);
289
      THR_DEBUG ("thread claims to be up");
290
      g_mutex_unlock (thread->lock);
291
292
      break;
    case GST_STATE_READY_TO_PAUSED:
293
294
      THR_INFO ("readying thread");
      g_mutex_lock (thread->lock);
295
      THR_DEBUG ("signaling");
296
      g_cond_signal (thread->cond);
297
      THR_DEBUG ("waiting for ack");
298
      g_cond_wait (thread->cond, thread->lock);
299
      THR_DEBUG ("got ack");
300
      g_mutex_unlock (thread->lock);
Erik Walthinsen's avatar
Erik Walthinsen committed
301
      break;
302
    case GST_STATE_PAUSED_TO_PLAYING:
303
      THR_DEBUG ("telling thread to start spinning");
304
      g_mutex_lock (thread->lock);
305
      THR_DEBUG ("signaling");
306
      g_cond_signal (thread->cond);
307
      THR_DEBUG ("waiting for ack");
308
      g_cond_wait (thread->cond, thread->lock);
309
      THR_DEBUG ("got ack");
310
      g_mutex_unlock (thread->lock);
311
      break;
312
    case GST_STATE_PLAYING_TO_PAUSED:
313
    {
314
      GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
315
316
317

      THR_INFO ("pausing thread");

318
319
320
321
322
323
      /* 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()
       * + find every queue we manage, and signal its empty and full conditions
       */ 
324
      g_mutex_lock (thread->lock);
325
326
327
328

      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);

      while (elements) {
Wim Taymans's avatar
Wim Taymans committed
329
	GstElement *element = GST_ELEMENT (elements->data);
330

Wim Taymans's avatar
Wim Taymans committed
331
	g_assert (element);
332
	THR_DEBUG ("  element \"%s\"", GST_ELEMENT_NAME (element));
333
	elements = g_list_next (elements);
Wim Taymans's avatar
Wim Taymans committed
334
	if (GST_IS_QUEUE (element)) {
335
          GstQueue *queue = GST_QUEUE (element);
336
  /* FIXME make this more efficient by only waking queues that are asleep
337
338
339
340
   *  FIXME and only waking the appropriate condition (depending on if it's
   *  FIXME on up- or down-stream side)
   * FIXME also make this more efficient by keeping list of managed queues
   */
341
	  THR_DEBUG ("waking queue \"%s\"", GST_ELEMENT_NAME (element));
342
343
344
345
346
	  g_mutex_lock (queue->qlock);
	  GST_STATE_PENDING (element) = GST_STATE_PAUSED;
	  g_cond_signal (queue->not_full);
	  g_cond_signal (queue->not_empty);
	  g_mutex_unlock (queue->qlock);
347
348
	}
	else {
Wim Taymans's avatar
Wim Taymans committed
349
	  GList *pads = GST_ELEMENT_PADS (element);
350
351

	  while (pads) {
352
	    GstRealPad *peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
353
354
355
356
357
358
359
360
361
	    GstElement *peerelement;

	    pads = g_list_next (pads);

	    if (!peer)
	      continue;

	    peerelement = GST_PAD_PARENT (peer);
	    if (!peerelement)
362
	      continue;		/* deal with case where there's no peer */
363
364

	    if (!GST_FLAG_IS_SET (peerelement, GST_ELEMENT_DECOUPLED)) {
365
	      GST_DEBUG (GST_CAT_THREAD, "peer element isn't DECOUPLED");
366
367
368
	      continue;
	    }

369
	    /* FIXME this needs to go away eventually */
370
	    if (!GST_IS_QUEUE (peerelement)) {
371
	      GST_DEBUG (GST_CAT_THREAD, "peer element isn't a Queue");
372
373
374
375
376
377
	      continue;
	    }

	    if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
	      GstQueue *queue = GST_QUEUE (peerelement);

378
	      THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
379
	      /* FIXME!! */
380
381
382
383
384
385
386
	      g_mutex_lock (queue->qlock);
	      g_cond_signal (queue->not_full);
	      g_cond_signal (queue->not_empty);
	      g_mutex_unlock (queue->qlock);
	    }
	  }
	}
387
      }
388
      THR_DEBUG ("telling thread to pause, signaling");
389
      g_cond_signal (thread->cond);
390
      THR_DEBUG ("waiting for ack");
391
      g_cond_wait (thread->cond, thread->lock);
392
      THR_DEBUG ("got ack");
393
      g_mutex_unlock (thread->lock);
Erik Walthinsen's avatar
Erik Walthinsen committed
394
      break;
395
    }
396
    case GST_STATE_READY_TO_NULL:
397
      THR_DEBUG ("telling thread to pause (null) - and joining");
398
      /* MattH FIXME revisit */
399
      g_mutex_lock (thread->lock);
400
      THR_DEBUG ("signaling");
401
      g_cond_signal (thread->cond);
402
      THR_DEBUG ("waiting for ack");
403
      g_cond_wait (thread->cond, thread->lock);
404
      THR_DEBUG ("got ack");
405
406
407
      pthread_join (thread->thread_id, NULL);
      thread->thread_id = -1;
      g_mutex_unlock (thread->lock);
408

409
410
411
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
412

413
414
      break;
    case GST_STATE_PAUSED_TO_READY:
415
      THR_DEBUG ("telling thread to stop spinning");
416
      g_mutex_lock (thread->lock);
417
      THR_DEBUG ("signaling");
418
      g_cond_signal (thread->cond);
419
      THR_DEBUG ("waiting for ack");
420
      g_cond_wait (thread->cond, thread->lock);
421
      THR_DEBUG ("got ack");
422
423
      g_mutex_unlock (thread->lock);

424
      break;
Erik Walthinsen's avatar
Erik Walthinsen committed
425
    default:
426
      GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x", transition);
Erik Walthinsen's avatar
Erik Walthinsen committed
427
428
429
430
431
432
      break;
  }

  return stateset;
}

433
434
435
436
437
/**
 * 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
438
 * while the state is GST_THREAD_STATE_SPINNING.
439
 */
Wim Taymans's avatar
Wim Taymans committed
440
static void *
441
gst_thread_main_loop (void *arg)
Wim Taymans's avatar
Wim Taymans committed
442
{
443
  GstThread *thread = GST_THREAD (arg);
444
445
  gint stateset;

446
447
  g_mutex_lock (thread->lock);

Wim Taymans's avatar
Wim Taymans committed
448
  gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
449
450
  GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);

451
452
  thread->pid = getpid();
  THR_INFO_MAIN("thread is running");
Erik Walthinsen's avatar
Erik Walthinsen committed
453

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

458
    if (stateset != GST_STATE_SUCCESS) {
459
      THR_DEBUG_MAIN ("state change of children failed");
460
461
462
    }
  }

463

464
  THR_DEBUG_MAIN ("indicating spinup");
465
  g_cond_signal (thread->cond);
466
  /* don't unlock the mutex because we hold it into the top of the while loop */
467
  THR_DEBUG_MAIN ("thread has indicated spinup to parent process");
468

469
  /***** THREAD IS NOW IN READY STATE *****/
470

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
471
472
  /* CR1: most of this code is handshaking */
  /* do this while the thread lives */
473
  while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
474
475
    /* NOTE we hold the thread lock at this point */
    /* what we do depends on what state we're in */
476
    switch (GST_STATE (thread)) {
477
      /* NOTE: cannot be in NULL, we're not running in that state at all */
478
      case GST_STATE_READY:
479
        /* wait to be set to either the NULL or PAUSED states */
480
        THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
481
482
483
484
485
                        gst_element_statename (GST_STATE_READY),
                        gst_element_statename (GST_STATE_NULL),
                        gst_element_statename (GST_STATE_PAUSED));
        g_cond_wait (thread->cond,thread->lock);
	
Wim Taymans's avatar
Wim Taymans committed
486
487
488
489
490
491
	/* 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;
	}
492

493
        /* been signaled, we need to state transition now and signal back */
494
        gst_thread_update_state (thread);
495
        THR_DEBUG_MAIN ("done with state transition, signaling back to parent process");
496
        g_cond_signal (thread->cond);
497
        /* now we decide what to do next */
498
        if (GST_STATE (thread) == GST_STATE_NULL) {
499
          /* REAPING must be set, we can simply break this iteration */
500
501
502
          GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
	}
        continue;
503
      case GST_STATE_PAUSED:
504
        /* wait to be set to either the READY or PLAYING states */
505
        THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s",
506
507
508
                       gst_element_statename (GST_STATE_PAUSED),
                       gst_element_statename (GST_STATE_READY),
                       gst_element_statename (GST_STATE_PLAYING));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
509
        g_cond_wait (thread->cond, thread->lock);
510

Wim Taymans's avatar
Wim Taymans committed
511
512
513
514
515
516
	/* 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;
	}
517

518
        /* been signaled, we need to state transition now and signal back */
519
        gst_thread_update_state (thread);
520
        /* now we decide what to do next */
521
        if (GST_STATE (thread) != GST_STATE_PLAYING) {
522
          /* either READY or the state change failed for some reason */
523
          g_cond_signal (thread->cond);
524
          continue;
525
526
527
        } 
	else {
          GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
528
          /* PLAYING is coming up, so we can now start spinning */
529
          while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
530
531
532
533
534
535
	    gboolean status;

            g_cond_signal (thread->cond);
            g_mutex_unlock (thread->lock);
            status = gst_bin_iterate (GST_BIN (thread));
            g_mutex_lock (thread->lock);
536
            /* g_cond_signal(thread->cond); */
537
538
539

	    if (!status || GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING)
              GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
540
          }
541
	  /* looks like we were stopped because of a statechange */
542
543
544
	  if (GST_STATE_PENDING (thread)) {
            gst_thread_update_state (thread);
	  }
545
          /* once we're here, SPINNING has stopped, we should signal that we're done */
546
          THR_DEBUG_MAIN ("SPINNING stopped, signaling back to parent process");
547
          g_cond_signal (thread->cond);
548
          /* now we can wait for PAUSED */
549
550
551
          continue;
        }
      case GST_STATE_PLAYING:
552
        /* wait to be set to PAUSED */
553
        THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
554
555
556
557
                        gst_element_statename(GST_STATE_PLAYING),
                        gst_element_statename(GST_STATE_PAUSED));
        g_cond_wait (thread->cond,thread->lock);

558
        /* been signaled, we need to state transition now and signal back */
559
560
        gst_thread_update_state (thread);
        g_cond_signal (thread->cond);
561
562
        /* now we decide what to do next */
        /* there's only PAUSED, we we just wait for it */
563
        continue;
564
      case GST_STATE_NULL:
565
        THR_DEBUG_MAIN ("thread in %s state, preparing to die",
566
567
568
569
570
                        gst_element_statename(GST_STATE_NULL));
        GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
        break;
      default:
	g_assert_not_reached ();
571
572
        break;
    }
Erik Walthinsen's avatar
Erik Walthinsen committed
573
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
574
  /* we need to destroy the scheduler here because it has mapped it's
Wim Taymans's avatar
Wim Taymans committed
575
576
577
   * stack into the threads stack space */
  gst_scheduler_reset (GST_ELEMENT_SCHED (thread));

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

581
  GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
582
		  GST_ELEMENT_NAME (thread));
Erik Walthinsen's avatar
Erik Walthinsen committed
583
584
585
  return NULL;
}

586
#ifndef GST_DISABLE_LOADSAVE
587
588
589
590
591
592
593
594
595
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
596
static void
597
598
gst_thread_restore_thyself (GstObject *object,
		            xmlNodePtr self)
599
{
600
  GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
601

602
603
  if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
    GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
604
}
605
#endif /* GST_DISABLE_LOADSAVE */