Fix a little typo, unlock the peer element if it's decoupled instead of our own element
[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   "Generic/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 };
57
58
59
60 static void                     gst_thread_class_init           (GstThreadClass *klass);
61 static void                     gst_thread_init                 (GstThread *thread);
62
63 static void                     gst_thread_dispose      (GObject *object);
64
65 static void                     gst_thread_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
66 static void                     gst_thread_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
67
68 static GstElementStateReturn    gst_thread_change_state         (GstElement *element);
69
70 #ifndef GST_DISABLE_LOADSAVE
71 static xmlNodePtr               gst_thread_save_thyself         (GstObject *object, xmlNodePtr parent);
72 static void                     gst_thread_restore_thyself      (GstObject *object, xmlNodePtr self);
73 #endif
74
75 static void*                    gst_thread_main_loop            (void *arg);
76
77 static GstBinClass *parent_class = NULL;
78 /* static guint gst_thread_signals[LAST_SIGNAL] = { 0 }; */
79
80 GType
81 gst_thread_get_type(void) {
82   static GType thread_type = 0;
83
84   if (!thread_type) {
85     static const GTypeInfo thread_info = {
86       sizeof(GstThreadClass),
87       NULL,
88       NULL,
89       (GClassInitFunc)gst_thread_class_init,
90       NULL,
91       NULL,
92       sizeof(GstThread),
93       4,
94       (GInstanceInitFunc)gst_thread_init,
95       NULL
96     };
97     thread_type = g_type_register_static(GST_TYPE_BIN, "GstThread", &thread_info, 0);
98   }
99   return thread_type;
100 }
101
102 static void
103 gst_thread_class_init (GstThreadClass *klass)
104 {
105   GObjectClass *gobject_class;
106   GstObjectClass *gstobject_class;
107   GstElementClass *gstelement_class;
108   GstBinClass *gstbin_class;
109
110   gobject_class =       (GObjectClass*)klass;
111   gstobject_class =     (GstObjectClass*)klass;
112   gstelement_class =    (GstElementClass*)klass;
113   gstbin_class =        (GstBinClass*)klass;
114
115   parent_class = g_type_class_ref (GST_TYPE_BIN);
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);
125
126   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
127   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
128
129 }
130
131 static void
132 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 */
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   thread->lock = g_mutex_new ();
146   thread->cond = g_cond_new ();
147
148   thread->ppid = getpid ();
149   thread->thread_id = -1;
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   g_mutex_free (thread->lock);
160   g_cond_free (thread->cond);
161
162   G_OBJECT_CLASS (parent_class)->dispose (object);
163
164   if (GST_ELEMENT_SCHED (thread)) {
165     gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
166   }
167 }
168
169 static void
170 gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
171 {
172   /* it's not null if we got it, but it might not be ours */
173   g_return_if_fail (GST_IS_THREAD (object));
174
175   switch (prop_id) {
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178       break;
179   }
180 }
181
182 static void
183 gst_thread_get_property (GObject *object, guint prop_id, 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     default:
190       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191       break;
192   }
193 }
194
195
196 /**
197  * gst_thread_new:
198  * @name: the name of the thread
199  *
200  * Create a new thread with the given name.
201  *
202  * Returns: The new thread
203  */
204 GstElement*
205 gst_thread_new (const gchar *name)
206 {
207   return gst_element_factory_make ("thread", name);
208 }
209
210
211 #define THR_INFO(format,args...) \
212   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
213   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
214 #define THR_DEBUG(format,args...) \
215   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
216   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
217
218 #define THR_INFO_MAIN(format,args...) \
219   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
220   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
221 #define THR_DEBUG_MAIN(format,args...) \
222   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
223   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
224
225 static GstElementStateReturn 
226 gst_thread_update_state (GstThread *thread)
227 {
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     if (GST_ELEMENT_CLASS (parent_class)->change_state)
232       return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
233   }
234
235   return GST_STATE_SUCCESS;
236 }
237
238
239 static GstElementStateReturn
240 gst_thread_change_state (GstElement * element)
241 {
242   GstThread *thread;
243   gboolean stateset = GST_STATE_SUCCESS;
244   gint transition;
245   pthread_t self = pthread_self ();
246
247   g_return_val_if_fail (GST_IS_THREAD (element), FALSE);
248
249   thread = GST_THREAD (element);
250
251   transition = GST_STATE_TRANSITION (element);
252
253   THR_INFO ("changing state from %s to %s",
254             gst_element_state_get_name (GST_STATE (element)),
255             gst_element_state_get_name (GST_STATE_PENDING (element)));
256
257   if (pthread_equal (self, thread->thread_id)) {
258     GST_DEBUG (GST_CAT_THREAD,
259                "no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning",
260                GST_DEBUG_THREAD_ARGS (thread->pid));
261     return gst_thread_update_state (thread);
262   }
263
264   switch (transition) {
265     case GST_STATE_NULL_TO_READY:
266       /* set the state to idle */
267       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
268
269       THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (element));
270
271       g_mutex_lock (thread->lock);
272
273       /* create the thread */
274       if (pthread_create (&thread->thread_id, NULL, gst_thread_main_loop, thread) != 0) {
275         g_mutex_unlock (thread->lock);
276         THR_DEBUG ("could not create thread \"%s\"", GST_ELEMENT_NAME (element));
277         return GST_STATE_FAILURE;
278       }
279
280       /* wait for it to 'spin up' */
281       THR_DEBUG ("waiting for child thread spinup");
282       g_cond_wait (thread->cond, thread->lock);
283       THR_DEBUG ("thread claims to be up");
284       g_mutex_unlock (thread->lock);
285       break;
286     case GST_STATE_READY_TO_PAUSED:
287       THR_INFO ("readying thread");
288       g_mutex_lock (thread->lock);
289       THR_DEBUG ("signaling");
290       g_cond_signal (thread->cond);
291       THR_DEBUG ("waiting for ack");
292       g_cond_wait (thread->cond, thread->lock);
293       THR_DEBUG ("got ack");
294       g_mutex_unlock (thread->lock);
295       break;
296     case GST_STATE_PAUSED_TO_PLAYING:
297     {
298       /* fixme: recurse into sub-bins */
299       const GList *elements = gst_bin_get_list (GST_BIN (thread));
300       while (elements) {
301         gst_element_enable_threadsafe_properties ((GstElement*)elements->data);
302         elements = g_list_next (elements);
303       }
304       
305       THR_DEBUG ("telling thread to start spinning");
306       g_mutex_lock (thread->lock);
307       THR_DEBUG ("signaling");
308       g_cond_signal (thread->cond);
309       THR_DEBUG ("waiting for ack");
310       g_cond_wait (thread->cond, thread->lock);
311       THR_DEBUG ("got ack");
312       g_mutex_unlock (thread->lock);
313       break;
314     }
315     case GST_STATE_PLAYING_TO_PAUSED:
316     {
317       const GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
318
319       THR_INFO ("pausing thread");
320
321       /* the following code ensures that the bottom half of thread will run
322        * to perform each elements' change_state() (by calling gstbin.c::
323        * change_state()).
324        * + the pending state was already set by gstelement.c::set_state()
325        * + unlock all elements so the bottom half can start the state change.
326        */ 
327       g_mutex_lock (thread->lock);
328
329       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
330
331       while (elements) {
332         GstElement *element = GST_ELEMENT (elements->data);
333         GList *pads;
334         
335
336         g_assert (element);
337         THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (element));
338         elements = g_list_next (elements);
339
340         if (!gst_element_release_locks (element)) {
341           g_warning ("element %s could not release locks", GST_ELEMENT_NAME (element));
342         }
343
344         pads = GST_ELEMENT_PADS (element);
345
346         while (pads) {
347           GstRealPad *peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
348           GstElement *peerelement;
349
350           pads = g_list_next (pads);
351
352           if (!peer)
353             continue;
354
355           peerelement = GST_PAD_PARENT (peer);
356           if (!peerelement)
357             continue;           /* deal with case where there's no peer */
358
359           if (!GST_FLAG_IS_SET (peerelement, GST_ELEMENT_DECOUPLED)) {
360             GST_DEBUG (GST_CAT_THREAD, "peer element isn't DECOUPLED");
361             continue;
362           }
363
364           if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
365             THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
366             THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (peerelement));
367             if (!gst_element_release_locks (peerelement)) {
368               g_warning ("element %s could not release locks", GST_ELEMENT_NAME (peerelement));
369             }
370           }
371         }
372
373       }
374       THR_DEBUG ("telling thread to pause, signaling");
375       g_cond_signal (thread->cond);
376       THR_DEBUG ("waiting for ack");
377       g_cond_wait (thread->cond, thread->lock);
378       THR_DEBUG ("got ack");
379       g_mutex_unlock (thread->lock);
380
381       elements = gst_bin_get_list (GST_BIN (thread));
382       while (elements) {
383         gst_element_disable_threadsafe_properties ((GstElement*)elements->data);
384         elements = g_list_next (elements);
385       }
386       break;
387     }
388     case GST_STATE_READY_TO_NULL:
389       THR_DEBUG ("telling thread to pause (null) - and joining");
390       /* MattH FIXME revisit */
391       g_mutex_lock (thread->lock);
392       THR_DEBUG ("signaling");
393       g_cond_signal (thread->cond);
394       THR_DEBUG ("waiting for ack");
395       g_cond_wait (thread->cond, thread->lock);
396       THR_DEBUG ("got ack");
397       pthread_join (thread->thread_id, NULL);
398       thread->thread_id = -1;
399       g_mutex_unlock (thread->lock);
400
401       GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
402       GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
403       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
404
405       break;
406     case GST_STATE_PAUSED_TO_READY:
407       THR_DEBUG ("telling thread to stop spinning");
408       g_mutex_lock (thread->lock);
409       THR_DEBUG ("signaling");
410       g_cond_signal (thread->cond);
411       THR_DEBUG ("waiting for ack");
412       g_cond_wait (thread->cond, thread->lock);
413       THR_DEBUG ("got ack");
414       g_mutex_unlock (thread->lock);
415
416       break;
417     default:
418       GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x", transition);
419       break;
420   }
421
422   return stateset;
423 }
424
425 /**
426  * gst_thread_main_loop:
427  * @arg: the thread to start
428  *
429  * The main loop of the thread. The thread will iterate
430  * while the state is GST_THREAD_STATE_SPINNING.
431  */
432 static void *
433 gst_thread_main_loop (void *arg)
434 {
435   GstThread *thread = GST_THREAD (arg);
436   gint stateset;
437
438   g_mutex_lock (thread->lock);
439
440   gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
441   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
442
443   thread->pid = getpid();
444   THR_INFO_MAIN("thread is running");
445
446   /* first we need to change the state of all the children */
447   if (GST_ELEMENT_CLASS (parent_class)->change_state) {
448     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
449
450     if (stateset != GST_STATE_SUCCESS) {
451       THR_DEBUG_MAIN ("state change of children failed");
452     }
453   }
454
455
456   THR_DEBUG_MAIN ("indicating spinup");
457   g_cond_signal (thread->cond);
458   /* don't unlock the mutex because we hold it into the top of the while loop */
459   THR_DEBUG_MAIN ("thread has indicated spinup to parent process");
460
461   /***** THREAD IS NOW IN READY STATE *****/
462
463   /* CR1: most of this code is handshaking */
464   /* do this while the thread lives */
465   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
466     /* NOTE we hold the thread lock at this point */
467     /* what we do depends on what state we're in */
468     switch (GST_STATE (thread)) {
469       /* NOTE: cannot be in NULL, we're not running in that state at all */
470       case GST_STATE_READY:
471         /* wait to be set to either the NULL or PAUSED states */
472         THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
473                         gst_element_state_get_name (GST_STATE_READY),
474                         gst_element_state_get_name (GST_STATE_NULL),
475                         gst_element_state_get_name (GST_STATE_PAUSED));
476         g_cond_wait (thread->cond,thread->lock);
477         
478         /* this must have happened by a state change in the thread context */
479         if (GST_STATE_PENDING (thread) != GST_STATE_NULL &&
480             GST_STATE_PENDING (thread) != GST_STATE_PAUSED) {
481           g_cond_signal (thread->cond);
482           continue;
483         }
484
485         /* been signaled, we need to state transition now and signal back */
486         gst_thread_update_state (thread);
487         THR_DEBUG_MAIN ("done with state transition, signaling back to parent process");
488         g_cond_signal (thread->cond);
489         /* now we decide what to do next */
490         if (GST_STATE (thread) == GST_STATE_NULL) {
491           /* REAPING must be set, we can simply break this iteration */
492           GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
493         }
494         continue;
495       case GST_STATE_PAUSED:
496         /* wait to be set to either the READY or PLAYING states */
497         THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s",
498                        gst_element_state_get_name (GST_STATE_PAUSED),
499                        gst_element_state_get_name (GST_STATE_READY),
500                        gst_element_state_get_name (GST_STATE_PLAYING));
501         g_cond_wait (thread->cond, thread->lock);
502
503         /* this must have happened by a state change in the thread context */
504         if (GST_STATE_PENDING (thread) != GST_STATE_READY &&
505             GST_STATE_PENDING (thread) != GST_STATE_PLAYING) {
506           g_cond_signal (thread->cond);
507           continue;
508         }
509
510         /* been signaled, we need to state transition now and signal back */
511         gst_thread_update_state (thread);
512         /* now we decide what to do next */
513         if (GST_STATE (thread) != GST_STATE_PLAYING) {
514           /* either READY or the state change failed for some reason */
515           g_cond_signal (thread->cond);
516           continue;
517         } 
518         else {
519           GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
520           /* PLAYING is coming up, so we can now start spinning */
521           while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
522             gboolean status;
523
524             g_cond_signal (thread->cond);
525             g_mutex_unlock (thread->lock);
526             status = gst_bin_iterate (GST_BIN (thread));
527             g_mutex_lock (thread->lock);
528             /* g_cond_signal(thread->cond); */
529
530             if (!status || GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING)
531               GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
532           }
533           /* looks like we were stopped because of a statechange */
534           if (GST_STATE_PENDING (thread)) {
535             gst_thread_update_state (thread);
536           }
537           /* once we're here, SPINNING has stopped, we should signal that we're done */
538           THR_DEBUG_MAIN ("SPINNING stopped, signaling back to parent process");
539           g_cond_signal (thread->cond);
540           /* now we can wait for PAUSED */
541           continue;
542         }
543       case GST_STATE_PLAYING:
544         /* wait to be set to PAUSED */
545         THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
546                         gst_element_state_get_name(GST_STATE_PLAYING),
547                         gst_element_state_get_name(GST_STATE_PAUSED));
548         g_cond_wait (thread->cond,thread->lock);
549
550         /* been signaled, we need to state transition now and signal back */
551         gst_thread_update_state (thread);
552         g_cond_signal (thread->cond);
553         /* now we decide what to do next */
554         /* there's only PAUSED, we we just wait for it */
555         continue;
556       case GST_STATE_NULL:
557         THR_DEBUG_MAIN ("thread in %s state, preparing to die",
558                         gst_element_state_get_name(GST_STATE_NULL));
559         GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
560         break;
561       default:
562         g_assert_not_reached ();
563         break;
564     }
565   }
566   /* we need to destroy the scheduler here because it has mapped it's
567    * stack into the threads stack space */
568   gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
569
570   /* since we don't unlock at the end of the while loop, do it here */
571   g_mutex_unlock (thread->lock);
572
573   GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
574                   GST_ELEMENT_NAME (thread));
575   return NULL;
576 }
577
578 #ifndef GST_DISABLE_LOADSAVE
579 static xmlNodePtr
580 gst_thread_save_thyself (GstObject *object,
581                          xmlNodePtr self)
582 {
583   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
584     GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
585   return NULL;
586 }
587
588 static void
589 gst_thread_restore_thyself (GstObject *object,
590                             xmlNodePtr self)
591 {
592   GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
593
594   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
595     GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
596 }
597 #endif /* GST_DISABLE_LOADSAVE */