1. Add more warnings for the gst core only. Various trival fixes to quiet the warnings.
[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 "gstthread.h"
29 #include "gstscheduler.h"
30 #include "gstqueue.h"
31
32 GstElementDetails gst_thread_details = {
33   "Threaded container",
34   "Bin",
35   "Container that creates/manages a thread",
36   VERSION,
37   "Erik Walthinsen <omega@cse.ogi.edu>",
38   "(C) 1999, 2000",
39 };
40
41
42 /* Thread signals and args */
43 enum {
44   /* FILL ME */
45   LAST_SIGNAL
46 };
47
48 enum {
49   SPINUP=0,
50   STATECHANGE,
51   STARTUP
52 };
53
54 enum {
55   ARG_0,
56   ARG_CREATE_THREAD,
57 };
58
59
60
61 static void                     gst_thread_class_init           (GstThreadClass *klass);
62 static void                     gst_thread_init                 (GstThread *thread);
63
64 static void                     gst_thread_real_destroy         (GObject *object);
65
66 static void                     gst_thread_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
67 static void                     gst_thread_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
68
69 static GstElementStateReturn    gst_thread_change_state         (GstElement *element);
70
71 #ifndef GST_DISABLE_LOADSAVE
72 static xmlNodePtr               gst_thread_save_thyself         (GstObject *object, xmlNodePtr parent);
73 static void                     gst_thread_restore_thyself      (GstObject *object, xmlNodePtr self);
74 #endif
75
76 static void                     gst_thread_signal_thread        (GstThread *thread, gboolean spinning);
77
78 static void*                    gst_thread_main_loop            (void *arg);
79
80 static GstBinClass *parent_class = NULL;
81 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
82
83 GType
84 gst_thread_get_type(void) {
85   static GType thread_type = 0;
86
87   if (!thread_type) {
88     static const GTypeInfo thread_info = {
89       sizeof(GstThreadClass),
90       NULL,
91       NULL,
92       (GClassInitFunc)gst_thread_class_init,
93       NULL,
94       NULL,
95       sizeof(GstThread),
96       4,
97       (GInstanceInitFunc)gst_thread_init,
98       NULL
99     };
100     thread_type = g_type_register_static(GST_TYPE_BIN, "GstThread", &thread_info, 0);
101   }
102   return thread_type;
103 }
104
105 static void
106 gst_thread_class_init (GstThreadClass *klass)
107 {
108   GObjectClass *gobject_class;
109   GstObjectClass *gstobject_class;
110   GstElementClass *gstelement_class;
111   GstBinClass *gstbin_class;
112
113   gobject_class =       (GObjectClass*)klass;
114   gstobject_class =     (GstObjectClass*)klass;
115   gstelement_class =    (GstElementClass*)klass;
116   gstbin_class =        (GstBinClass*)klass;
117
118   parent_class = g_type_class_ref (GST_TYPE_BIN);
119
120   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CREATE_THREAD,
121     g_param_spec_boolean("create_thread", "Create Thread", "Whether to create a thread.",
122                          TRUE,G_PARAM_READWRITE));
123
124 // FIXME!
125 //  gobject_class->destroy =            gst_thread_real_destroy;
126
127 #ifndef GST_DISABLE_LOADSAVE
128   gstobject_class->save_thyself =       gst_thread_save_thyself;
129   gstobject_class->restore_thyself =    gst_thread_restore_thyself;
130 #endif
131
132   gstelement_class->change_state =      gst_thread_change_state;
133
134 //  gstbin_class->schedule = gst_thread_schedule_dummy;
135
136   gobject_class->set_property = gst_thread_set_property;
137   gobject_class->get_property = gst_thread_get_property;
138
139 }
140
141 static void
142 gst_thread_init (GstThread *thread)
143 {
144
145   GST_DEBUG (GST_CAT_THREAD,"initializing thread\n");
146
147   // we're a manager by default
148   GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
149
150   // default is to create a thread
151   GST_FLAG_SET (thread, GST_THREAD_CREATE);
152
153   thread->lock = g_mutex_new();
154   thread->cond = g_cond_new();
155
156   GST_ELEMENT_SCHED(thread) = gst_schedule_new(GST_ELEMENT(thread));
157   GST_DEBUG(GST_CAT_THREAD, "thread's scheduler is %p\n",GST_ELEMENT_SCHED(thread));
158
159   thread->ppid = getpid();
160
161 //  gst_element_set_manager(GST_ELEMENT(thread),GST_ELEMENT(thread));
162 }
163
164 static void
165 gst_thread_real_destroy (GObject *object)
166 {
167   GstThread *thread = GST_THREAD (object);
168
169   GST_DEBUG (GST_CAT_REFCOUNTING,"destroy()\n");
170
171   g_mutex_free (thread->lock);
172   g_cond_free (thread->cond);
173
174 // FIXME!
175 //  if (G_OBJECT_CLASS (parent_class)->destroy)
176 //    G_OBJECT_CLASS (parent_class)->destroy (object);
177
178   gst_object_destroy (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
179   gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
180 }
181
182 static void
183 gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
184 {
185   /* it's not null if we got it, but it might not be ours */
186   g_return_if_fail (GST_IS_THREAD (object));
187
188   switch (prop_id) {
189     case ARG_CREATE_THREAD:
190       if (g_value_get_boolean(value)) {
191         GST_INFO (GST_CAT_THREAD,"turning ON the creation of the thread");
192         GST_FLAG_SET (object, GST_THREAD_CREATE);
193 //        GST_DEBUG (GST_CAT_THREAD,"flags are 0x%08x\n", GST_FLAGS (object));
194       } else {
195         GST_INFO (GST_CAT_THREAD,"gstthread: turning OFF the creation of the thread");
196         GST_FLAG_UNSET (object, GST_THREAD_CREATE);
197 //        GST_DEBUG (GST_CAT_THREAD,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
198       }
199       break;
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202       break;
203   }
204 }
205
206 static void
207 gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
208 {
209   /* it's not null if we got it, but it might not be ours */
210   g_return_if_fail (GST_IS_THREAD (object));
211
212   switch (prop_id) {
213     case ARG_CREATE_THREAD:
214       g_value_set_boolean(value, GST_FLAG_IS_SET (object, GST_THREAD_CREATE));
215       break;
216     default:
217       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218       break;
219   }
220 }
221
222
223 /**
224  * gst_thread_new:
225  * @name: the name of the thread
226  *
227  * Create a new thread with the given name.
228  *
229  * Returns: The new thread
230  */
231 GstElement*
232 gst_thread_new (const guchar *name)
233 {
234   return gst_elementfactory_make ("thread", name);
235 }
236
237
238 #define THR_INFO(format,args...) \
239   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
240   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
241 #define THR_DEBUG(format,args...) \
242   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
243   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
244
245 #define THR_INFO_MAIN(format,args...) \
246   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
247   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
248 #define THR_DEBUG_MAIN(format,args...) \
249   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
250   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
251
252
253 static GstElementStateReturn
254 gst_thread_change_state (GstElement *element)
255 {
256   GstThread *thread;
257   gboolean stateset = GST_STATE_SUCCESS;
258   gint transition;
259   pthread_t self = pthread_self();
260
261   g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
262 //  GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME(element));
263
264   thread = GST_THREAD (element);
265 //  GST_DEBUG (GST_CAT_THREAD, "**** THREAD %ld changing THREAD %ld ****\n",self,thread->thread_id);
266 //  GST_DEBUG (GST_CAT_THREAD, "**** current pid=%d\n",getpid());
267
268   transition = GST_STATE_TRANSITION (element);
269
270   THR_INFO("changing state from %s to %s",
271            gst_element_statename(GST_STATE (element)),
272            gst_element_statename(GST_STATE_PENDING (element)));
273
274   //GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
275
276   switch (transition) {
277     case GST_STATE_NULL_TO_READY:
278       // set the state to idle
279       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
280       GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
281
282       // create the thread if that's what we're supposed to do
283       if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
284         THR_DEBUG ("creating thread \"%s\"\n",
285                    GST_ELEMENT_NAME (element));
286
287         g_mutex_lock (thread->lock);
288
289         // create the thread
290         pthread_create (&thread->thread_id, NULL,
291                         gst_thread_main_loop, thread);
292
293         // wait for it to 'spin up'
294         THR_DEBUG("waiting for child thread spinup\n");
295         g_cond_wait(thread->cond,thread->lock);
296         THR_DEBUG("thread claims to be up\n");
297         g_mutex_unlock(thread->lock);
298       } else {
299         GST_INFO (GST_CAT_THREAD, "NOT creating thread \"%s\"",
300                 GST_ELEMENT_NAME (GST_ELEMENT (element)));
301
302         // punt and change state on all the children
303         if (GST_ELEMENT_CLASS (parent_class)->change_state)
304           stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
305       }
306       break;
307     case GST_STATE_READY_TO_PAUSED:
308       THR_INFO("readying thread");
309
310       // check to see if the thread is somehow changing its own state.
311       // FIXME this is currently illegal, but must somehow be made legal at some point.
312       if (pthread_equal(self, thread->thread_id))
313       {
314         //FIXME this should not happen
315         g_assert(!pthread_equal(self, thread->thread_id));
316         GST_FLAG_SET(thread, GST_THREAD_STATE_SPINNING);
317         GST_DEBUG(GST_CAT_THREAD,"no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning\n",
318                   GST_DEBUG_THREAD_ARGS(thread->pid));
319       }
320       else
321       {
322         g_mutex_lock(thread->lock);
323         gst_thread_signal_thread(thread,FALSE);
324       }
325       break;
326     case GST_STATE_PAUSED_TO_PLAYING:
327       THR_INFO("starting thread");
328
329       // check to see if the thread is somehow changing its own state.
330       // FIXME this is currently illegal, but must somehow be made legal at some point.
331       if (pthread_equal(self, thread->thread_id))
332       {
333         //FIXME this should not happen
334         g_assert(!pthread_equal(self, thread->thread_id));
335         GST_FLAG_SET(thread, GST_THREAD_STATE_SPINNING);
336         GST_DEBUG(GST_CAT_THREAD,"no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning\n",
337                   GST_DEBUG_THREAD_ARGS(thread->pid));
338       }
339       else
340       {
341         THR_DEBUG("telling thread to start spinning\n");
342         g_mutex_lock(thread->lock);
343         gst_thread_signal_thread(thread,TRUE);
344       }
345       break;
346     case GST_STATE_PLAYING_TO_PAUSED:
347       THR_INFO("pausing thread");
348
349       // check to see if the thread is somehow changing its own state.
350       // FIXME this is currently illegal, but must somehow be made legal at some point.
351       if (pthread_equal(self, thread->thread_id))
352       {
353         //FIXME this should not happen
354         GST_DEBUG(GST_CAT_THREAD,"no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to paused\n",
355                   GST_DEBUG_THREAD_ARGS(thread->pid));
356         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
357         g_assert(!pthread_equal(self, thread->thread_id));
358       }
359       else
360       {
361         GList *elements = (element->sched)->elements;
362
363         // the following code ensures that the bottom half of thread will run
364         // to perform each elements' change_state() (by calling gstbin.c::
365         // change_state()).
366         // + the pending state was already set by gstelement.c::set_state()
367         // + find every queue we manage, and signal its empty and full conditions
368
369         g_mutex_lock(thread->lock);
370
371         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
372
373         while (elements)
374         {
375           GstElement *e = GST_ELEMENT(elements->data);
376           g_assert(e);
377           THR_DEBUG("  element \"%s\"\n",GST_ELEMENT_NAME(e));
378           elements = g_list_next(elements);
379           if (GST_IS_QUEUE(e))
380           {
381             //FIXME make this more efficient by only waking queues that are asleep
382             //FIXME and only waking the appropriate condition (depending on if it's
383             //FIXME on up- or down-stream side)
384             //
385             //FIXME also make this more efficient by keeping list of managed queues
386             THR_DEBUG("waking queue \"%s\"\n",GST_ELEMENT_NAME(e));
387             GST_LOCK(e);
388             g_cond_signal((GST_QUEUE(e)->emptycond));
389             g_cond_signal((GST_QUEUE(e)->fullcond));
390             GST_UNLOCK(e);
391           }
392           else
393           {
394             GList *pads = GST_ELEMENT_PADS(e);
395             while (pads)
396             {
397               GstRealPad *peer;
398               GstElement *peerelement;
399               GstPad *p = GST_PAD(pads->data);
400               pads = g_list_next(pads);
401
402               peer = GST_PAD_PEER(p);
403               if (!peer) continue;
404
405               peerelement = GST_PAD_PARENT(peer);
406               if (!peerelement) continue;               // deal with case where there's no peer
407
408               if (!GST_FLAG_IS_SET(peerelement,GST_ELEMENT_DECOUPLED)) {
409                 GST_DEBUG(GST_CAT_THREAD,"peer element isn't DECOUPLED\n");
410                 continue;
411               }
412
413               // FIXME this needs to go away eventually
414               if (!GST_IS_QUEUE(peerelement)) {
415                 GST_DEBUG(GST_CAT_THREAD,"peer element isn't a Queue\n");
416                 continue;
417               }
418
419               if (GST_ELEMENT_SCHED(peerelement) != GST_ELEMENT_SCHED(thread))
420               {
421                 THR_DEBUG("  element \"%s\" has pad cross sched boundary\n",GST_ELEMENT_NAME(e));
422                 GST_LOCK(peerelement);
423                 g_cond_signal(GST_QUEUE(peerelement)->emptycond);
424                 g_cond_signal(GST_QUEUE(peerelement)->fullcond);
425                 GST_UNLOCK(peerelement);
426               }
427             }
428           }
429         }
430         THR_DEBUG("waiting for thread to stop spinning\n");
431         g_cond_wait (thread->cond, thread->lock);
432         THR_DEBUG("telling thread to pause\n");
433         gst_thread_signal_thread(thread,FALSE);
434       }
435       break;
436     case GST_STATE_READY_TO_NULL:
437       THR_INFO("stopping thread");
438
439       GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
440
441       // check to see if the thread is somehow changing its own state.
442       // FIXME this is currently illegal, but must somehow be made legal at some point.
443       if (pthread_equal(self, thread->thread_id))
444       {
445         //FIXME this should not happen
446         g_assert(!pthread_equal(self, thread->thread_id));
447         THR_DEBUG("setting own thread's state to NULL (paused)\n");
448         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
449       }
450       else
451       {
452         THR_DEBUG("telling thread to pause (null) - and joining\n");
453         //MattH FIXME revisit
454         g_mutex_lock(thread->lock);
455         gst_thread_signal_thread(thread,FALSE);
456         pthread_join(thread->thread_id,NULL);
457       }
458
459       GST_FLAG_UNSET(thread,GST_THREAD_STATE_REAPING);
460       GST_FLAG_UNSET(thread,GST_THREAD_STATE_STARTED);
461       GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
462       GST_FLAG_UNSET(thread,GST_THREAD_STATE_ELEMENT_CHANGED);
463
464       break;
465     case GST_STATE_PAUSED_TO_READY:
466       THR_INFO("stopping thread");
467
468       // check to see if the thread is somehow changing its own state.
469       // FIXME this is currently illegal, but must somehow be made legal at some point.
470       if (pthread_equal(self, thread->thread_id))
471       {
472         //FIXME this should not happen
473         g_assert(!pthread_equal(self, thread->thread_id));
474         GST_FLAG_SET(thread, GST_THREAD_STATE_SPINNING);
475         GST_DEBUG(GST_CAT_THREAD,"no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning\n",
476                   GST_DEBUG_THREAD_ARGS(thread->pid));
477       }
478       else
479       {
480         THR_DEBUG("telling thread to stop spinning\n");
481         g_mutex_lock(thread->lock);
482         gst_thread_signal_thread(thread,FALSE);
483       }
484       
485       break;
486     default:
487       GST_DEBUG_ELEMENT(GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x\n",transition);
488       break;
489   }
490
491   return stateset;
492 }
493
494 static void gst_thread_update_state (GstThread *thread)
495 {
496   // check for state change
497   if (GST_STATE_PENDING(thread) != GST_STATE_VOID_PENDING) {
498     // punt and change state on all the children
499     if (GST_ELEMENT_CLASS (parent_class)->change_state)
500       GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
501   }
502 }
503
504 /**
505  * gst_thread_main_loop:
506  * @arg: the thread to start
507  *
508  * The main loop of the thread. The thread will iterate
509  * while the state is GST_THREAD_STATE_SPINNING
510  */
511 static void *
512 gst_thread_main_loop (void *arg)
513 {
514   GstThread *thread = GST_THREAD (arg);
515   gint stateset;
516
517   thread->pid = getpid();
518   THR_INFO_MAIN("thread is running");
519
520   // first we need to change the state of all the children
521   if (GST_ELEMENT_CLASS (parent_class)->change_state)
522     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
523
524   // construct the plan and signal back
525 /* DEPRACATED for INCSCHED1
526   THR_DEBUG_MAIN("creating plan for thread\n");
527   if (GST_BIN_CLASS (parent_class)->schedule)
528     GST_BIN_CLASS (parent_class)->schedule (GST_BIN (thread));
529 */
530
531 //  THR_DEBUG_MAIN("indicating spinup\n");
532   g_mutex_lock (thread->lock);
533   g_cond_signal (thread->cond);
534   // don't unlock the mutex because we hold it into the top of the while loop
535   THR_DEBUG_MAIN("thread has indicated spinup to parent process\n");
536
537   /***** THREAD IS NOW IN READY STATE *****/
538
539   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
540     // NOTE we hold the thread lock at this point
541     // what we do depends on what state we're in
542     switch (GST_STATE(thread)) {
543       // NOTE: cannot be in NULL, we're not running in that state at all
544       case GST_STATE_READY:
545         // wait to be set to either the NULL or PAUSED states
546         THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s\n",
547                        gst_element_statename(GST_STATE_READY),
548                        gst_element_statename(GST_STATE_NULL),
549                        gst_element_statename(GST_STATE_PAUSED));
550         g_cond_wait(thread->cond,thread->lock);
551         // been signaled, we need to state transition now and signal back
552         gst_thread_update_state(thread);
553         THR_DEBUG_MAIN("done with state transition, signaling back to parent process\n");
554         g_cond_signal(thread->cond);
555 //        g_mutex_unlock(thread->lock);
556         // now we decide what to do next (FIXME can be collapsed to a continue)
557         if (GST_STATE(thread) == GST_STATE_NULL) {
558           // REAPING must be set, we can simply break this iteration
559           continue;
560         } else {
561           // PAUSED is the next state, we can wait for that next
562           continue;
563         }
564         break;
565       case GST_STATE_PAUSED:
566         // wait to be set to either the READY or PLAYING states
567         THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s\n",
568                        gst_element_statename(GST_STATE_PAUSED),
569                        gst_element_statename(GST_STATE_READY),
570                        gst_element_statename(GST_STATE_PLAYING));
571         g_cond_wait(thread->cond,thread->lock);
572         // been signaled, we need to state transition now and signal back
573         gst_thread_update_state(thread);
574         g_cond_signal(thread->cond);
575 //        g_mutex_unlock(thread->lock);
576         // now we decide what to do next
577         if (GST_STATE(thread) == GST_STATE_READY) {
578           // READY is the next state, we can wait for that next
579           continue;
580         } else {
581           g_mutex_unlock(thread->lock);
582           // PLAYING is coming up, so we can now start spinning
583           while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
584             if (!gst_bin_iterate (GST_BIN (thread))) {
585 //              GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
586 //              THR_DEBUG_MAIN("removed spinning state due to failed iteration!\n");
587               // FIXME FIXME FIXME this is ugly!
588               THR_DEBUG_MAIN("iteration failed, something very wrong, spinning to let parent sync\n");
589               while (GST_FLAG_IS_SET(thread, GST_THREAD_STATE_SPINNING)) ;
590             }
591           }
592           g_mutex_lock(thread->lock);
593           // once we're here, SPINNING has stopped, we should signal that we're done
594           THR_DEBUG_MAIN("SPINNING stopped, signaling back to parent process\n");
595           g_cond_signal (thread->cond);
596           // now we can wait for PAUSED
597           continue;
598         }
599         break;
600       case GST_STATE_PLAYING:
601         // wait to be set to PAUSED
602         THR_DEBUG_MAIN("thread in %s state, waiting for %s\n",
603                        gst_element_statename(GST_STATE_PLAYING),
604                        gst_element_statename(GST_STATE_PAUSED));
605         g_cond_wait(thread->cond,thread->lock);
606         // been signaled, we need to state transition now and signal back
607         gst_thread_update_state(thread);
608         g_cond_signal(thread->cond);
609 //        g_mutex_unlock(thread->lock);
610         // now we decide what to do next
611         // there's only PAUSED, we we just wait for it
612         continue;
613         break;
614     }
615
616     // need to grab the lock so we're ready for the top of the loop
617 //    g_mutex_lock(thread->lock);
618   }
619
620 /*
621   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
622     // start out by waiting for a state change into spinning
623     THR_DEBUG_MAIN("waiting for signal from parent process (at top of while())\n");
624     g_cond_wait (thread->cond,thread->lock);
625     THR_DEBUG_MAIN("woken up with %s pending\n",gst_element_statename(GST_STATE(thread)));
626     // now is a good time to change the state of the children and the thread itself
627     gst_thread_update_state (thread);
628     THR_DEBUG_MAIN("done changing state, signaling back\n");
629     g_cond_signal (thread->cond);
630     g_mutex_unlock (thread->lock);
631     THR_DEBUG_MAIN("finished sycnronizing with main process\n");
632
633     while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
634       if (!gst_bin_iterate (GST_BIN (thread))) {
635         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
636         THR_DEBUG_MAIN("removed spinning state due to failed iteration!\n");
637       }
638     }
639
640     g_mutex_lock (thread->lock);
641
642     if (GST_STATE_PENDING(thread) == GST_STATE_PAUSED) {
643       // we've stopped spinning, because of PLAYING->PAUSED
644       THR_DEBUG_MAIN("SPINNING flag unset, signaling parent process we're stopped\n");
645       // we need to signal back that we've stopped spinning
646       g_cond_signal (thread->cond);
647     }
648
649 //    THR_DEBUG_MAIN("signaling that the thread is out of the SPINNING loop\n");
650 //    g_cond_signal (thread->cond);
651 //    g_cond_wait (thread->cond, thread->lock);
652 //    THR_DEBUG_MAIN("parent process has signaled at bottom of while\n");
653 //    // now change the children's and thread's state
654 //    gst_thread_update_state (thread);
655 //    THR_DEBUG_MAIN("done changing state, signaling back to parent process\n");
656 //    g_cond_signal (thread->cond);
657 //    // don't release the mutex, we hold that into the top of the loop
658 //    THR_DEBUG_MAIN("done syncing with parent process at bottom of while\n");
659   }
660 */
661
662   // since we don't unlock at the end of the while loop, do it here
663   g_mutex_unlock (thread->lock);
664
665   GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
666                   GST_ELEMENT_NAME (thread));
667   return NULL;
668 }
669
670 // the set flag is to say whether it should set TRUE or FALSE
671 //
672 // WARNING: this has synchronization built in!  if you remove or add any
673 // locks, waits, signals, or unlocks you need to be sure they match the 
674 // code above (in gst_thread_main_loop()).  basically, don't change anything.
675 static void
676 gst_thread_signal_thread (GstThread *thread, gboolean spinning)
677 {
678   // set the spinning state
679   if (spinning) GST_FLAG_SET(thread,GST_THREAD_STATE_SPINNING);
680   else GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
681
682   THR_DEBUG("thread locked\n");
683 //  g_mutex_lock(thread->lock);
684
685 //  if (!spinning) {
686 //    THR_DEBUG("waiting for spindown\n");
687 //    g_cond_wait (thread->cond, thread->lock);
688 //  }
689   THR_DEBUG("signaling\n");
690   g_cond_signal (thread->cond);
691   THR_DEBUG("waiting for ack\n");
692   g_cond_wait (thread->cond,thread->lock);
693   THR_DEBUG("got ack\n");
694
695   THR_DEBUG("unlocking\n");
696   g_mutex_unlock(thread->lock);
697   THR_DEBUG("unlocked\n");
698 }
699
700
701 #ifndef GST_DISABLE_LOADSAVE
702 static xmlNodePtr
703 gst_thread_save_thyself (GstObject *object,
704                          xmlNodePtr self)
705 {
706   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
707     GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
708   return NULL;
709 }
710
711 static void
712 gst_thread_restore_thyself (GstObject *object,
713                             xmlNodePtr self)
714 {
715   GST_DEBUG (GST_CAT_THREAD,"gstthread: restore\n");
716
717   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
718     GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
719 }
720 #endif // GST_DISABLE_LOADSAVE