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
GstElementDetails gst_thread_details = {
  "Threaded container",
34
  "Generic/Bin",
Erik Walthinsen's avatar
Erik Walthinsen committed
35
36
37
  "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
  gstobject_class->save_thyself =	GST_DEBUG_FUNCPTR (gst_thread_save_thyself);
121
  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
127
  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
128

Erik Walthinsen's avatar
Erik Walthinsen committed
129
130
}

131
132
static void
gst_thread_init (GstThread *thread)
133
{
134
  GstScheduler *scheduler;
135

136
  GST_DEBUG (GST_CAT_THREAD, "initializing thread");
137

138
  /* threads are managing bins and iterate themselves */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
139
  /* CR1: the GstBin code checks these flags */
140
  GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
141
  GST_FLAG_SET (thread, GST_BIN_SELF_SCHEDULABLE);
142

143
  scheduler = gst_scheduler_factory_make (NULL, GST_ELEMENT (thread));
144
145
146
147
148

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

  thread->ppid = getpid ();
149
  thread->thread_id = -1;
Erik Walthinsen's avatar
Erik Walthinsen committed
150
151
}

152
static void
153
gst_thread_dispose (GObject *object)
154
{
155
  GstThread *thread = GST_THREAD (object);
156

157
  GST_DEBUG (GST_CAT_REFCOUNTING, "dispose");
158
159
160

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

162
  G_OBJECT_CLASS (parent_class)->dispose (object);
163

164
165
166
  if (GST_ELEMENT_SCHED (thread)) {
    gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
  }
167
168
169
}

static void
170
gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
171
{
Erik Walthinsen's avatar
Erik Walthinsen committed
172
  /* it's not null if we got it, but it might not be ours */
173
  g_return_if_fail (GST_IS_THREAD (object));
Erik Walthinsen's avatar
Erik Walthinsen committed
174

175
  switch (prop_id) {
Erik Walthinsen's avatar
Erik Walthinsen committed
176
    default:
177
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
178
179
180
181
      break;
  }
}

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

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

195

196
197
198
199
/**
 * gst_thread_new:
 * @name: the name of the thread
 *
Erik Walthinsen's avatar
Erik Walthinsen committed
200
 * Create a new thread with the given name.
201
 *
Wim Taymans's avatar
Wim Taymans committed
202
 * Returns: The new thread
203
 */
204
GstElement*
205
gst_thread_new (const gchar *name)
206
{
207
  return gst_element_factory_make ("thread", name);
Erik Walthinsen's avatar
Erik Walthinsen committed
208
209
210
}


211
212
213
214
215
216
217
218
219
220
221
222
223
224
#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 )

225
226
227
static GstElementStateReturn 
gst_thread_update_state (GstThread *thread)
{
228
  /* check for state change */
229
  if (GST_STATE_PENDING(thread) != GST_STATE_VOID_PENDING) {
230
    /* punt and change state on all the children */
231
232
233
234
235
236
237
    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
238

239
static GstElementStateReturn
240
gst_thread_change_state (GstElement * element)
241
{
Erik Walthinsen's avatar
Erik Walthinsen committed
242
  GstThread *thread;
Wim Taymans's avatar
Wim Taymans committed
243
  gboolean stateset = GST_STATE_SUCCESS;
244
  gint transition;
245
  pthread_t self = pthread_self ();
246
247
  void *stack;
  glong stacksize;
Erik Walthinsen's avatar
Erik Walthinsen committed
248

249
  g_return_val_if_fail (GST_IS_THREAD (element), FALSE);
250

251
  thread = GST_THREAD (element);
Erik Walthinsen's avatar
Erik Walthinsen committed
252

253
  transition = GST_STATE_TRANSITION (element);
254

255
  THR_INFO ("changing state from %s to %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
256
257
	    gst_element_state_get_name (GST_STATE (element)),
	    gst_element_state_get_name (GST_STATE_PENDING (element)));
258

259
260
  if (pthread_equal (self, thread->thread_id)) {
    GST_DEBUG (GST_CAT_THREAD,
261
	       "no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning",
262
263
264
	       GST_DEBUG_THREAD_ARGS (thread->pid));
    return gst_thread_update_state (thread);
  }
265

266
267
  switch (transition) {
    case GST_STATE_NULL_TO_READY:
268
      /* set the state to idle */
269
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
270

271
      THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (element));
272
273
274

      g_mutex_lock (thread->lock);

275
276
277
278
      pthread_attr_init (&thread->attr);
      if (gst_scheduler_get_prefered_stack (GST_ELEMENT_SCHED (element), &stack, &stacksize)) {
        pthread_attr_setstack (&thread->attr, stack, stacksize);
      }
279
      /* create the thread */
280
      if (pthread_create (&thread->thread_id, &thread->attr, gst_thread_main_loop, thread) != 0) {
281
282
283
284
	g_mutex_unlock (thread->lock);
        THR_DEBUG ("could not create thread \"%s\"", GST_ELEMENT_NAME (element));
	return GST_STATE_FAILURE;
      }
285

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
304
305
306
307
308
309
310
    {
      /* 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);
      }
      
311
      THR_DEBUG ("telling thread to start spinning");
312
      g_mutex_lock (thread->lock);
313
      THR_DEBUG ("signaling");
314
      g_cond_signal (thread->cond);
315
      THR_DEBUG ("waiting for ack");
316
      g_cond_wait (thread->cond, thread->lock);
317
      THR_DEBUG ("got ack");
318
      g_mutex_unlock (thread->lock);
319
      break;
320
    }
321
    case GST_STATE_PLAYING_TO_PAUSED:
322
    {
323
      const GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
324
325
326

      THR_INFO ("pausing thread");

327
328
329
330
      /* 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()
331
       * + unlock all elements so the bottom half can start the state change.
332
       */ 
333
      g_mutex_lock (thread->lock);
334
335
336
337

      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);

      while (elements) {
Wim Taymans's avatar
Wim Taymans committed
338
	GstElement *element = GST_ELEMENT (elements->data);
339
340
	GList *pads;
	
341

Wim Taymans's avatar
Wim Taymans committed
342
	g_assert (element);
343
	THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (element));
344
	elements = g_list_next (elements);
345
346
347

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

350
	pads = GST_ELEMENT_PADS (element);
351

352
353
354
	while (pads) {
	  GstRealPad *peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
	  GstElement *peerelement;
355

356
	  pads = g_list_next (pads);
357

358
359
	  if (!peer)
	    continue;
360

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

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

370
371
	  if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
	    THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
372
373
374
	    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));
375
376
377
	    }
	  }
	}
378

379
      }
380
      THR_DEBUG ("telling thread to pause, signaling");
381
      g_cond_signal (thread->cond);
382
      THR_DEBUG ("waiting for ack");
383
      g_cond_wait (thread->cond, thread->lock);
384
      THR_DEBUG ("got ack");
385
      g_mutex_unlock (thread->lock);
386
387
388
389
390
391

      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
392
      break;
393
    }
394
    case GST_STATE_READY_TO_NULL:
395
      THR_DEBUG ("telling thread to pause (null) - and joining");
396
      /* MattH FIXME revisit */
397
      g_mutex_lock (thread->lock);
398
      THR_DEBUG ("signaling");
399
      g_cond_signal (thread->cond);
400
      THR_DEBUG ("waiting for ack");
401
      g_cond_wait (thread->cond, thread->lock);
402
      THR_DEBUG ("got ack");
403
404
405
      pthread_join (thread->thread_id, NULL);
      thread->thread_id = -1;
      g_mutex_unlock (thread->lock);
406

407
408
409
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
410

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

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

  return stateset;
}

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

444
445
  g_mutex_lock (thread->lock);

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

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

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

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

461

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

467
  /***** THREAD IS NOW IN READY STATE *****/
468

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

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

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

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

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

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

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

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

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

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

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