gstthread.c 18.7 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 */
142
143
  GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);

144
  schedname = gst_schedulerfactory_get_default_name ();
145

146
  scheduler = gst_schedulerfactory_make (schedname, GST_ELEMENT (thread));
147

148
149
150
151
152
153
154
155
156
  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 ();
157
  thread->thread_id = -1;
Erik Walthinsen's avatar
Erik Walthinsen committed
158
159
}

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

165
  GST_DEBUG (GST_CAT_REFCOUNTING, "dispose");
166
167
168

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

170
  G_OBJECT_CLASS (parent_class)->dispose (object);
171

172
173
174
  if (GST_ELEMENT_SCHED (thread)) {
    gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
  }
175
176
177
}

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

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

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

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

203

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


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

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

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

255
  g_return_val_if_fail (GST_IS_THREAD (element), FALSE);
256

257
  thread = GST_THREAD (element);
Erik Walthinsen's avatar
Erik Walthinsen committed
258

259
  transition = GST_STATE_TRANSITION (element);
260

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

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

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

277
278
279
280
      THR_DEBUG ("creating thread \"%s\"\n", GST_ELEMENT_NAME (element));

      g_mutex_lock (thread->lock);

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

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

      THR_INFO ("pausing thread");

316
317
318
319
320
321
      /* 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
       */ 
322
      g_mutex_lock (thread->lock);
323
324
325
326

      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);

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

Wim Taymans's avatar
Wim Taymans committed
329
330
	g_assert (element);
	THR_DEBUG ("  element \"%s\"\n", GST_ELEMENT_NAME (element));
331
	elements = g_list_next (elements);
Wim Taymans's avatar
Wim Taymans committed
332
	if (GST_IS_QUEUE (element)) {
333
          GstQueue *queue = GST_QUEUE (element);
334
  /* FIXME make this more efficient by only waking queues that are asleep
335
336
337
338
   *  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
   */
Wim Taymans's avatar
Wim Taymans committed
339
	  THR_DEBUG ("waking queue \"%s\"\n", GST_ELEMENT_NAME (element));
340
341
342
343
344
	  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);
345
346
	}
	else {
Wim Taymans's avatar
Wim Taymans committed
347
	  GList *pads = GST_ELEMENT_PADS (element);
348
349

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

	    pads = g_list_next (pads);

	    if (!peer)
	      continue;

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

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

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

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

Wim Taymans's avatar
Wim Taymans committed
376
	      THR_DEBUG ("  element \"%s\" has pad cross sched boundary\n", GST_ELEMENT_NAME (element));
377
	      /* FIXME!! */
378
379
380
381
382
383
384
	      g_mutex_lock (queue->qlock);
	      g_cond_signal (queue->not_full);
	      g_cond_signal (queue->not_empty);
	      g_mutex_unlock (queue->qlock);
	    }
	  }
	}
385
      }
386
387
388
389
390
391
      THR_DEBUG ("telling thread to pause, signaling\n");
      g_cond_signal (thread->cond);
      THR_DEBUG ("waiting for ack\n");
      g_cond_wait (thread->cond, thread->lock);
      THR_DEBUG ("got ack\n");
      g_mutex_unlock (thread->lock);
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\n");
396
      /* MattH FIXME revisit */
397
398
399
400
401
402
403
404
405
      g_mutex_lock (thread->lock);
      THR_DEBUG ("signaling\n");
      g_cond_signal (thread->cond);
      THR_DEBUG ("waiting for ack\n");
      g_cond_wait (thread->cond, thread->lock);
      THR_DEBUG ("got ack\n");
      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
414
415
416
417
418
419
420
421
      THR_DEBUG ("telling thread to stop spinning\n");
      g_mutex_lock (thread->lock);
      THR_DEBUG ("signaling\n");
      g_cond_signal (thread->cond);
      THR_DEBUG ("waiting for ack\n");
      g_cond_wait (thread->cond, thread->lock);
      THR_DEBUG ("got ack\n");
      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\n", transition);
Erik Walthinsen's avatar
Erik Walthinsen committed
425
426
427
428
429
430
      break;
  }

  return stateset;
}

431
432
433
434
435
436
437
/**
 * gst_thread_main_loop:
 * @arg: the thread to start
 *
 * The main loop of the thread. The thread will iterate
 * while the state is GST_THREAD_STATE_SPINNING
 */
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
457
458
459
460
    if (stateset != GST_STATE_SUCCESS) {
      THR_DEBUG_MAIN ("state change of children failed\n");
    }
  }

461

462
  THR_DEBUG_MAIN ("indicating spinup\n");
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\n");
466

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

469
  while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
470
471
    /* NOTE we hold the thread lock at this point */
    /* what we do depends on what state we're in */
472
    switch (GST_STATE (thread)) {
473
      /* NOTE: cannot be in NULL, we're not running in that state at all */
474
      case GST_STATE_READY:
475
        /* wait to be set to either the NULL or PAUSED states */
476
477
478
479
480
481
        THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s\n",
                        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
482
483
484
485
486
487
	/* 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;
	}
488

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

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

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

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

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

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

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

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

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

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