I'm too lazy to comment this
[platform/upstream/gstreamer.git] / gst / gstthread.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstthread.c: Threaded container object
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24
25 /* #define GST_DEBUG_ENABLED */
26 #include "gst_private.h"
27
28 #include "gst.h"
29 #include "gstthread.h"
30 #include "gstscheduler.h"
31 #include "gstqueue.h"
32
33 GstElementDetails gst_thread_details = {
34   "Threaded container",
35   "Generic/Bin",
36   "Container that creates/manages a thread",
37   VERSION,
38   "Erik Walthinsen <omega@cse.ogi.edu>",
39   "(C) 1999, 2000",
40 };
41
42
43 /* Thread signals and args */
44 enum {
45   SHUTDOWN,
46   /* FILL ME */
47   LAST_SIGNAL
48 };
49
50 enum {
51   SPINUP=0,
52   STATECHANGE,
53   STARTUP
54 };
55
56 enum {
57   ARG_0,
58   ARG_SCHEDPOLICY,
59   ARG_PRIORITY,
60 };
61
62
63
64 static void                     gst_thread_class_init           (GstThreadClass *klass);
65 static void                     gst_thread_init                 (GstThread *thread);
66
67 static void                     gst_thread_dispose      (GObject *object);
68
69 static void                     gst_thread_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
70 static void                     gst_thread_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
71
72 static GstElementStateReturn    gst_thread_change_state         (GstElement *element);
73
74 #ifndef GST_DISABLE_LOADSAVE
75 static xmlNodePtr               gst_thread_save_thyself         (GstObject *object, xmlNodePtr parent);
76 static void                     gst_thread_restore_thyself      (GstObject *object, xmlNodePtr self);
77 #endif
78
79 static void*                    gst_thread_main_loop            (void *arg);
80
81 #define GST_TYPE_THREAD_SCHEDPOLICY (gst_thread_schedpolicy_get_type())
82 static GType
83 gst_thread_schedpolicy_get_type(void) {
84   static GType thread_schedpolicy_type = 0;
85   static GEnumValue thread_schedpolicy[] = {
86     {SCHED_OTHER, "SCHED_OTHER", "Normal Scheduling"},
87     {SCHED_FIFO,  "SCHED_FIFO",  "FIFO Scheduling (requires root)"},
88     {SCHED_RR,    "SCHED_RR",    "Round-Robin Scheduling (requires root)"},
89     {0, NULL, NULL},
90   };
91   if (!thread_schedpolicy_type) {
92     thread_schedpolicy_type = g_enum_register_static("GstThreadSchedPolicy", thread_schedpolicy);
93   }
94   return thread_schedpolicy_type;
95 }
96
97 static GstBinClass *parent_class = NULL;
98 static guint gst_thread_signals[LAST_SIGNAL] = { 0 }; 
99
100 GType
101 gst_thread_get_type(void) {
102   static GType thread_type = 0;
103
104   if (!thread_type) {
105     static const GTypeInfo thread_info = {
106       sizeof(GstThreadClass),
107       NULL,
108       NULL,
109       (GClassInitFunc)gst_thread_class_init,
110       NULL,
111       NULL,
112       sizeof(GstThread),
113       4,
114       (GInstanceInitFunc)gst_thread_init,
115       NULL
116     };
117     thread_type = g_type_register_static(GST_TYPE_BIN, "GstThread", &thread_info, 0);
118   }
119   return thread_type;
120 }
121
122 static void
123 gst_thread_class_init (GstThreadClass *klass)
124 {
125   GObjectClass *gobject_class;
126   GstObjectClass *gstobject_class;
127   GstElementClass *gstelement_class;
128   GstBinClass *gstbin_class;
129
130   gobject_class =       (GObjectClass*)klass;
131   gstobject_class =     (GstObjectClass*)klass;
132   gstelement_class =    (GstElementClass*)klass;
133   gstbin_class =        (GstBinClass*)klass;
134
135   parent_class = g_type_class_ref (GST_TYPE_BIN);
136
137   g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_SCHEDPOLICY,
138     g_param_spec_enum("schedpolicy", "Scheduling Policy", "The scheduling policy of the thread",
139                       GST_TYPE_THREAD_SCHEDPOLICY, SCHED_OTHER, G_PARAM_READWRITE));
140   g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_PRIORITY,
141     g_param_spec_int("priority", "Scheduling Priority", "The scheduling priority of the thread",
142                      0, 99, 0, G_PARAM_READWRITE));
143
144   gst_thread_signals[SHUTDOWN] =
145     g_signal_new ("shutdown", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
146                   G_STRUCT_OFFSET (GstThreadClass, shutdown), NULL, NULL,
147                   gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
148
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);
157
158   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
159   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
160
161 }
162
163 static void
164 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 */
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   thread->lock = g_mutex_new ();
178   thread->cond = g_cond_new ();
179
180   thread->ppid = getpid ();
181   thread->thread_id = (pthread_t) -1;
182   thread->sched_policy = SCHED_OTHER;
183   thread->priority = 0;
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   g_mutex_free (thread->lock);
194   g_cond_free (thread->cond);
195
196   G_OBJECT_CLASS (parent_class)->dispose (object);
197
198   if (GST_ELEMENT_SCHED (thread)) {
199     gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
200   }
201 }
202
203 static void
204 gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
205 {
206   GstThread *thread;
207
208   /* it's not null if we got it, but it might not be ours */
209   g_return_if_fail (GST_IS_THREAD (object));
210
211   thread = GST_THREAD (object);
212
213   switch (prop_id) {
214     case ARG_SCHEDPOLICY:
215       thread->sched_policy = g_value_get_enum (value);
216       break;
217     case ARG_PRIORITY:
218       thread->priority = g_value_get_int (value);
219       break;
220     default:
221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222       break;
223   }
224 }
225
226 static void
227 gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
228 {
229   GstThread *thread;
230
231   /* it's not null if we got it, but it might not be ours */
232   g_return_if_fail (GST_IS_THREAD (object));
233
234   thread = GST_THREAD (object);
235
236   switch (prop_id) {
237     case ARG_SCHEDPOLICY:
238       g_value_set_enum (value, thread->sched_policy);
239       break;
240     case ARG_PRIORITY:
241       g_value_set_int (value, thread->priority);
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246   }
247 }
248
249
250 /**
251  * gst_thread_new:
252  * @name: the name of the thread
253  *
254  * Create a new thread with the given name.
255  *
256  * Returns: The new thread
257  */
258 GstElement*
259 gst_thread_new (const gchar *name)
260 {
261   return gst_element_factory_make ("thread", name);
262 }
263
264
265 #define THR_INFO(format,args...) \
266   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
267   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
268 #define THR_DEBUG(format,args...) \
269   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
270   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
271
272 #define THR_INFO_MAIN(format,args...) \
273   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
274   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
275 #define THR_DEBUG_MAIN(format,args...) \
276   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
277   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
278
279 static GstElementStateReturn 
280 gst_thread_update_state (GstThread *thread)
281 {
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     if (GST_ELEMENT_CLASS (parent_class)->change_state)
286       return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
287   }
288
289   return GST_STATE_SUCCESS;
290 }
291
292
293 static GstElementStateReturn
294 gst_thread_change_state (GstElement * element)
295 {
296   GstThread *thread;
297   gboolean stateset = GST_STATE_SUCCESS;
298   gint transition;
299   pthread_t self = pthread_self ();
300   glong stacksize;
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);
306
307   transition = GST_STATE_TRANSITION (element);
308
309   THR_INFO ("changing state from %s to %s",
310             gst_element_state_get_name (GST_STATE (element)),
311             gst_element_state_get_name (GST_STATE_PENDING (element)));
312
313   if (pthread_equal (self, thread->thread_id)) {
314     GST_DEBUG (GST_CAT_THREAD,
315                "no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning",
316                GST_DEBUG_THREAD_ARGS (thread->pid));
317     return gst_thread_update_state (thread);
318   }
319
320   switch (transition) {
321     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       g_mutex_lock (thread->lock);
328
329       if (pthread_attr_init (&thread->attr) != 0)
330         g_warning ("pthread_attr_init returned an error !");
331
332       if (gst_scheduler_get_preferred_stack (GST_ELEMENT_SCHED (element), 
333                                              &thread->stack, &stacksize)) {
334 #ifdef HAVE_PTHREAD_ATTR_SETSTACK
335         if (pthread_attr_setstack (&thread->attr, thread->stack, stacksize) != 0) {
336           g_warning ("pthread_attr_setstack failed\n");
337           return GST_STATE_FAILURE;
338         }
339 #else
340         if (pthread_attr_setstackaddr (&thread->attr, thread->stack) != 0) {
341           g_warning ("pthread_attr_setstackaddr failed\n");
342           return GST_STATE_FAILURE;
343         }
344         if (pthread_attr_setstacksize (&thread->attr, stacksize) != 0) {
345           g_warning ("pthread_attr_setstacksize failed\n");
346           return GST_STATE_FAILURE;
347         }
348 #endif
349         GST_DEBUG (GST_CAT_THREAD, "pthread attr set stack at %p of size %ld", 
350                    thread->stack, stacksize);
351       }
352
353       /* create the thread */
354       THR_DEBUG ("going to pthread_create...");
355       if (pthread_create (&thread->thread_id, &thread->attr, gst_thread_main_loop, thread) != 0) {
356         THR_DEBUG ("pthread create failed");
357         g_mutex_unlock (thread->lock);
358         THR_DEBUG ("could not create thread \"%s\"", GST_ELEMENT_NAME (element));
359         return GST_STATE_FAILURE;
360       }
361       THR_DEBUG ("pthread created");
362
363       /* wait for it to 'spin up' */
364       THR_DEBUG ("waiting for child thread spinup");
365       g_cond_wait (thread->cond, thread->lock);
366       THR_DEBUG ("thread claims to be up");
367       g_mutex_unlock (thread->lock);
368       break;
369     case GST_STATE_READY_TO_PAUSED:
370       THR_INFO ("readying thread");
371       g_mutex_lock (thread->lock);
372       THR_DEBUG ("signaling");
373       g_cond_signal (thread->cond);
374       THR_DEBUG ("waiting for ack");
375       g_cond_wait (thread->cond, thread->lock);
376       THR_DEBUG ("got ack");
377       g_mutex_unlock (thread->lock);
378       break;
379     case GST_STATE_PAUSED_TO_PLAYING:
380     {
381       /* fixme: recurse into sub-bins */
382       const GList *elements = gst_bin_get_list (GST_BIN (thread));
383       while (elements) {
384         gst_element_enable_threadsafe_properties ((GstElement*)elements->data);
385         elements = g_list_next (elements);
386       }
387       
388       THR_DEBUG ("telling thread to start spinning");
389       g_mutex_lock (thread->lock);
390       THR_DEBUG ("signaling");
391       g_cond_signal (thread->cond);
392       THR_DEBUG ("waiting for ack");
393       g_cond_wait (thread->cond, thread->lock);
394       THR_DEBUG ("got ack");
395       g_mutex_unlock (thread->lock);
396       break;
397     }
398     case GST_STATE_PLAYING_TO_PAUSED:
399     {
400       const GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
401
402       THR_INFO ("pausing thread");
403
404       /* the following code ensures that the bottom half of thread will run
405        * to perform each elements' change_state() (by calling gstbin.c::
406        * change_state()).
407        * + the pending state was already set by gstelement.c::set_state()
408        * + unlock all elements so the bottom half can start the state change.
409        */ 
410       g_mutex_lock (thread->lock);
411
412       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
413
414       while (elements) {
415         GstElement *element = GST_ELEMENT (elements->data);
416         GList *pads;
417         
418
419         g_assert (element);
420         THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (element));
421         elements = g_list_next (elements);
422
423         if (!gst_element_release_locks (element)) {
424           g_warning ("element %s could not release locks", GST_ELEMENT_NAME (element));
425         }
426
427         pads = GST_ELEMENT_PADS (element);
428
429         while (pads) {
430           GstRealPad *peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
431           GstElement *peerelement;
432
433           pads = g_list_next (pads);
434
435           if (!peer)
436             continue;
437
438           peerelement = GST_PAD_PARENT (peer);
439           if (!peerelement)
440             continue;           /* deal with case where there's no peer */
441
442           if (!GST_FLAG_IS_SET (peerelement, GST_ELEMENT_DECOUPLED)) {
443             GST_DEBUG (GST_CAT_THREAD, "peer element isn't DECOUPLED");
444             continue;
445           }
446
447           if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
448             THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
449             THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (peerelement));
450             if (!gst_element_release_locks (peerelement)) {
451               g_warning ("element %s could not release locks", GST_ELEMENT_NAME (peerelement));
452             }
453           }
454         }
455
456       }
457       THR_DEBUG ("telling thread to pause, signaling");
458       g_cond_signal (thread->cond);
459       THR_DEBUG ("waiting for ack");
460       g_cond_wait (thread->cond, thread->lock);
461       THR_DEBUG ("got ack");
462       g_mutex_unlock (thread->lock);
463
464       elements = gst_bin_get_list (GST_BIN (thread));
465       while (elements) {
466         gst_element_disable_threadsafe_properties ((GstElement*)elements->data);
467         elements = g_list_next (elements);
468       }
469       break;
470     }
471     case GST_STATE_READY_TO_NULL:
472       THR_DEBUG ("telling thread to pause (null) - and joining");
473       /* MattH FIXME revisit */
474       g_mutex_lock (thread->lock);
475       THR_DEBUG ("signaling");
476       g_cond_signal (thread->cond);
477       THR_DEBUG ("waiting for ack");
478       g_cond_wait (thread->cond, thread->lock);
479       THR_DEBUG ("got ack");
480       if (pthread_join (thread->thread_id, NULL) != 0)
481         g_warning ("pthread_join has failed !\n");
482       if (pthread_attr_destroy (&thread->attr) != 0)
483         g_warning ("pthread_attr_destroy has failed !\n");
484       thread->thread_id = -1;
485       g_mutex_unlock (thread->lock);
486
487       if (thread->stack) {
488         GST_DEBUG (GST_CAT_THREAD, "freeing allocated stack (%p)", thread->stack);
489         free (thread->stack);
490         thread->stack = NULL;
491       }
492  
493       GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
494       GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
495       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
496
497       break;
498     case GST_STATE_PAUSED_TO_READY:
499       THR_DEBUG ("telling thread to stop spinning");
500       g_mutex_lock (thread->lock);
501       THR_DEBUG ("signaling");
502       g_cond_signal (thread->cond);
503       THR_DEBUG ("waiting for ack");
504       g_cond_wait (thread->cond, thread->lock);
505       THR_DEBUG ("got ack");
506       g_mutex_unlock (thread->lock);
507
508       break;
509     default:
510       GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x", transition);
511       break;
512   }
513
514   return stateset;
515 }
516
517 /**
518  * gst_thread_main_loop:
519  * @arg: the thread to start
520  *
521  * The main loop of the thread. The thread will iterate
522  * while the state is GST_THREAD_STATE_SPINNING.
523  */
524 static void *
525 gst_thread_main_loop (void *arg)
526 {
527   GstThread *thread = NULL;
528   gint stateset;
529
530   GST_DEBUG (GST_CAT_THREAD, "gst_thread_main_loop started");
531   thread = GST_THREAD (arg);
532   g_mutex_lock (thread->lock);
533
534   if (thread->sched_policy != SCHED_OTHER) {
535     struct sched_param sched_param;
536
537     memset (&sched_param, 0, sizeof (sched_param));
538     if (thread->priority == 0) {
539       thread->priority = sched_get_priority_max (thread->sched_policy);
540     }
541     sched_param.sched_priority = thread->priority;
542
543     if (sched_setscheduler (0, thread->sched_policy, &sched_param) != 0) {
544       GST_DEBUG (GST_CAT_THREAD, "not running with real-time priority");
545     }
546   }
547
548   gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
549   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
550
551   thread->pid = getpid();
552   THR_INFO_MAIN("thread is running");
553
554   /* first we need to change the state of all the children */
555   if (GST_ELEMENT_CLASS (parent_class)->change_state) {
556     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
557
558     if (stateset != GST_STATE_SUCCESS) {
559       THR_DEBUG_MAIN ("state change of children failed");
560     }
561   }
562
563
564   THR_DEBUG_MAIN ("indicating spinup");
565   g_cond_signal (thread->cond);
566   /* don't unlock the mutex because we hold it into the top of the while loop */
567   THR_DEBUG_MAIN ("thread has indicated spinup to parent process");
568
569   /***** THREAD IS NOW IN READY STATE *****/
570
571   /* CR1: most of this code is handshaking */
572   /* do this while the thread lives */
573   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
574     /* NOTE we hold the thread lock at this point */
575     /* what we do depends on what state we're in */
576     switch (GST_STATE (thread)) {
577       /* NOTE: cannot be in NULL, we're not running in that state at all */
578       case GST_STATE_READY:
579         /* wait to be set to either the NULL or PAUSED states */
580         THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
581                         gst_element_state_get_name (GST_STATE_READY),
582                         gst_element_state_get_name (GST_STATE_NULL),
583                         gst_element_state_get_name (GST_STATE_PAUSED));
584         g_cond_wait (thread->cond,thread->lock);
585         
586         /* this must have happened by a state change in the thread context */
587         if (GST_STATE_PENDING (thread) != GST_STATE_NULL &&
588             GST_STATE_PENDING (thread) != GST_STATE_PAUSED) {
589           g_cond_signal (thread->cond);
590           continue;
591         }
592
593         /* been signaled, we need to state transition now and signal back */
594         gst_thread_update_state (thread);
595         THR_DEBUG_MAIN ("done with state transition, signaling back to parent process");
596         g_cond_signal (thread->cond);
597         /* now we decide what to do next */
598         if (GST_STATE (thread) == GST_STATE_NULL) {
599           /* REAPING must be set, we can simply break this iteration */
600           GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
601         }
602         continue;
603       case GST_STATE_PAUSED:
604         /* wait to be set to either the READY or PLAYING states */
605         THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s",
606                        gst_element_state_get_name (GST_STATE_PAUSED),
607                        gst_element_state_get_name (GST_STATE_READY),
608                        gst_element_state_get_name (GST_STATE_PLAYING));
609         g_cond_wait (thread->cond, thread->lock);
610
611         /* this must have happened by a state change in the thread context */
612         if (GST_STATE_PENDING (thread) != GST_STATE_READY &&
613             GST_STATE_PENDING (thread) != GST_STATE_PLAYING) {
614           g_cond_signal (thread->cond);
615           continue;
616         }
617
618         /* been signaled, we need to state transition now and signal back */
619         gst_thread_update_state (thread);
620         /* now we decide what to do next */
621         if (GST_STATE (thread) != GST_STATE_PLAYING) {
622           /* either READY or the state change failed for some reason */
623           g_cond_signal (thread->cond);
624           continue;
625         } 
626         else {
627           GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
628           /* PLAYING is coming up, so we can now start spinning */
629           while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
630             gboolean status;
631
632             g_cond_signal (thread->cond);
633             g_mutex_unlock (thread->lock);
634             status = gst_bin_iterate (GST_BIN (thread));
635             g_mutex_lock (thread->lock);
636             /* g_cond_signal(thread->cond); */
637
638             if (!status || GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING)
639               GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
640           }
641           /* looks like we were stopped because of a statechange */
642           if (GST_STATE_PENDING (thread)) {
643             gst_thread_update_state (thread);
644           }
645           /* once we're here, SPINNING has stopped, we should signal that we're done */
646           THR_DEBUG_MAIN ("SPINNING stopped, signaling back to parent process");
647           g_cond_signal (thread->cond);
648           /* now we can wait for PAUSED */
649           continue;
650         }
651       case GST_STATE_PLAYING:
652         /* wait to be set to PAUSED */
653         THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
654                         gst_element_state_get_name(GST_STATE_PLAYING),
655                         gst_element_state_get_name(GST_STATE_PAUSED));
656         g_cond_wait (thread->cond,thread->lock);
657
658         /* been signaled, we need to state transition now and signal back */
659         gst_thread_update_state (thread);
660         g_cond_signal (thread->cond);
661         /* now we decide what to do next */
662         /* there's only PAUSED, we we just wait for it */
663         continue;
664       case GST_STATE_NULL:
665         THR_DEBUG_MAIN ("thread in %s state, preparing to die",
666                         gst_element_state_get_name(GST_STATE_NULL));
667         GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
668         break;
669       default:
670         g_assert_not_reached ();
671         break;
672     }
673   }
674   /* we need to destroy the scheduler here because it has mapped it's
675    * stack into the threads stack space */
676   gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
677
678   /* since we don't unlock at the end of the while loop, do it here */
679   g_mutex_unlock (thread->lock);
680
681   GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
682                   GST_ELEMENT_NAME (thread));
683
684   g_signal_emit (G_OBJECT (thread), gst_thread_signals[SHUTDOWN], 0);
685
686   return NULL;
687 }
688
689 #ifndef GST_DISABLE_LOADSAVE
690 static xmlNodePtr
691 gst_thread_save_thyself (GstObject *object,
692                          xmlNodePtr self)
693 {
694   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
695     GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
696   return NULL;
697 }
698
699 static void
700 gst_thread_restore_thyself (GstObject *object,
701                             xmlNodePtr self)
702 {
703   GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
704
705   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
706     GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
707 }
708 #endif /* GST_DISABLE_LOADSAVE */