GST_DEBUG and _INFO should have no trailing \n
[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 };
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 /*  gstbin_class->schedule = gst_thread_schedule_dummy; */
127
128   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
129   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
130
131 }
132
133 static void
134 gst_thread_init (GstThread *thread)
135 {
136   const gchar *schedname;
137   GstScheduler *scheduler;
138
139   GST_DEBUG (GST_CAT_THREAD, "initializing thread");
140
141   /* we're a manager by default */
142   GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
143   GST_FLAG_SET (thread, GST_BIN_SELF_SCHEDULABLE);
144
145   schedname = gst_schedulerfactory_get_default_name ();
146
147   scheduler = gst_schedulerfactory_make (schedname, GST_ELEMENT (thread));
148
149   GST_ELEMENT_SCHED (thread) = scheduler;
150
151   gst_object_ref (GST_OBJECT (scheduler));
152   gst_object_sink (GST_OBJECT (scheduler));
153
154   thread->lock = g_mutex_new ();
155   thread->cond = g_cond_new ();
156
157   thread->ppid = getpid ();
158   thread->thread_id = -1;
159 }
160
161 static void
162 gst_thread_dispose (GObject *object)
163 {
164   GstThread *thread = GST_THREAD (object);
165
166   GST_DEBUG (GST_CAT_REFCOUNTING, "dispose");
167
168   g_mutex_free (thread->lock);
169   g_cond_free (thread->cond);
170
171   G_OBJECT_CLASS (parent_class)->dispose (object);
172
173   if (GST_ELEMENT_SCHED (thread)) {
174     gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
175   }
176 }
177
178 static void
179 gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
180 {
181   /* it's not null if we got it, but it might not be ours */
182   g_return_if_fail (GST_IS_THREAD (object));
183
184   switch (prop_id) {
185     default:
186       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187       break;
188   }
189 }
190
191 static void
192 gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
193 {
194   /* it's not null if we got it, but it might not be ours */
195   g_return_if_fail (GST_IS_THREAD (object));
196
197   switch (prop_id) {
198     default:
199       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200       break;
201   }
202 }
203
204
205 /**
206  * gst_thread_new:
207  * @name: the name of the thread
208  *
209  * Create a new thread with the given name.
210  *
211  * Returns: The new thread
212  */
213 GstElement*
214 gst_thread_new (const gchar *name)
215 {
216   return gst_elementfactory_make ("thread", name);
217 }
218
219
220 #define THR_INFO(format,args...) \
221   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
222   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
223 #define THR_DEBUG(format,args...) \
224   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
225   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
226
227 #define THR_INFO_MAIN(format,args...) \
228   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
229   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
230 #define THR_DEBUG_MAIN(format,args...) \
231   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
232   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
233
234 static GstElementStateReturn 
235 gst_thread_update_state (GstThread *thread)
236 {
237   /* check for state change */
238   if (GST_STATE_PENDING(thread) != GST_STATE_VOID_PENDING) {
239     /* punt and change state on all the children */
240     if (GST_ELEMENT_CLASS (parent_class)->change_state)
241       return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
242   }
243
244   return GST_STATE_SUCCESS;
245 }
246
247
248 static GstElementStateReturn
249 gst_thread_change_state (GstElement * element)
250 {
251   GstThread *thread;
252   gboolean stateset = GST_STATE_SUCCESS;
253   gint transition;
254   pthread_t self = pthread_self ();
255
256   g_return_val_if_fail (GST_IS_THREAD (element), FALSE);
257
258   thread = GST_THREAD (element);
259
260   transition = GST_STATE_TRANSITION (element);
261
262   THR_INFO ("changing state from %s to %s",
263             gst_element_statename (GST_STATE (element)),
264             gst_element_statename (GST_STATE_PENDING (element)));
265
266   if (pthread_equal (self, thread->thread_id)) {
267     GST_DEBUG (GST_CAT_THREAD,
268                "no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning",
269                GST_DEBUG_THREAD_ARGS (thread->pid));
270     return gst_thread_update_state (thread);
271   }
272
273   switch (transition) {
274     case GST_STATE_NULL_TO_READY:
275       /* set the state to idle */
276       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
277
278       THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (element));
279
280       g_mutex_lock (thread->lock);
281
282       /* create the thread */
283       pthread_create (&thread->thread_id, NULL, gst_thread_main_loop, thread);
284
285       /* wait for it to 'spin up' */
286       THR_DEBUG ("waiting for child thread spinup");
287       g_cond_wait (thread->cond, thread->lock);
288       THR_DEBUG ("thread claims to be up");
289       g_mutex_unlock (thread->lock);
290       break;
291     case GST_STATE_READY_TO_PAUSED:
292       THR_INFO ("readying thread");
293       g_mutex_lock (thread->lock);
294       THR_DEBUG ("signaling");
295       g_cond_signal (thread->cond);
296       THR_DEBUG ("waiting for ack");
297       g_cond_wait (thread->cond, thread->lock);
298       THR_DEBUG ("got ack");
299       g_mutex_unlock (thread->lock);
300       break;
301     case GST_STATE_PAUSED_TO_PLAYING:
302       THR_DEBUG ("telling thread to start spinning");
303       g_mutex_lock (thread->lock);
304       THR_DEBUG ("signaling");
305       g_cond_signal (thread->cond);
306       THR_DEBUG ("waiting for ack");
307       g_cond_wait (thread->cond, thread->lock);
308       THR_DEBUG ("got ack");
309       g_mutex_unlock (thread->lock);
310       break;
311     case GST_STATE_PLAYING_TO_PAUSED:
312     {
313       GList *elements = gst_bin_get_list (GST_BIN (thread));
314
315       THR_INFO ("pausing thread");
316
317       /* the following code ensures that the bottom half of thread will run
318        * to perform each elements' change_state() (by calling gstbin.c::
319        * change_state()).
320        * + the pending state was already set by gstelement.c::set_state()
321        * + find every queue we manage, and signal its empty and full conditions
322        */ 
323       g_mutex_lock (thread->lock);
324
325       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
326
327       while (elements) {
328         GstElement *element = GST_ELEMENT (elements->data);
329
330         g_assert (element);
331         THR_DEBUG ("  element \"%s\"", GST_ELEMENT_NAME (element));
332         elements = g_list_next (elements);
333         if (GST_IS_QUEUE (element)) {
334           GstQueue *queue = GST_QUEUE (element);
335   /* FIXME make this more efficient by only waking queues that are asleep
336    *  FIXME and only waking the appropriate condition (depending on if it's
337    *  FIXME on up- or down-stream side)
338    * FIXME also make this more efficient by keeping list of managed queues
339    */
340           THR_DEBUG ("waking queue \"%s\"", GST_ELEMENT_NAME (element));
341           g_mutex_lock (queue->qlock);
342           GST_STATE_PENDING (element) = GST_STATE_PAUSED;
343           g_cond_signal (queue->not_full);
344           g_cond_signal (queue->not_empty);
345           g_mutex_unlock (queue->qlock);
346         }
347         else {
348           GList *pads = GST_ELEMENT_PADS (element);
349
350           while (pads) {
351             GstRealPad *peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
352             GstElement *peerelement;
353
354             pads = g_list_next (pads);
355
356             if (!peer)
357               continue;
358
359             peerelement = GST_PAD_PARENT (peer);
360             if (!peerelement)
361               continue;         /* deal with case where there's no peer */
362
363             if (!GST_FLAG_IS_SET (peerelement, GST_ELEMENT_DECOUPLED)) {
364               GST_DEBUG (GST_CAT_THREAD, "peer element isn't DECOUPLED");
365               continue;
366             }
367
368             /* FIXME this needs to go away eventually */
369             if (!GST_IS_QUEUE (peerelement)) {
370               GST_DEBUG (GST_CAT_THREAD, "peer element isn't a Queue");
371               continue;
372             }
373
374             if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
375               GstQueue *queue = GST_QUEUE (peerelement);
376
377               THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
378               /* FIXME!! */
379               g_mutex_lock (queue->qlock);
380               g_cond_signal (queue->not_full);
381               g_cond_signal (queue->not_empty);
382               g_mutex_unlock (queue->qlock);
383             }
384           }
385         }
386       }
387       THR_DEBUG ("telling thread to pause, signaling");
388       g_cond_signal (thread->cond);
389       THR_DEBUG ("waiting for ack");
390       g_cond_wait (thread->cond, thread->lock);
391       THR_DEBUG ("got ack");
392       g_mutex_unlock (thread->lock);
393       break;
394     }
395     case GST_STATE_READY_TO_NULL:
396       THR_DEBUG ("telling thread to pause (null) - and joining");
397       /* MattH FIXME revisit */
398       g_mutex_lock (thread->lock);
399       THR_DEBUG ("signaling");
400       g_cond_signal (thread->cond);
401       THR_DEBUG ("waiting for ack");
402       g_cond_wait (thread->cond, thread->lock);
403       THR_DEBUG ("got ack");
404       pthread_join (thread->thread_id, NULL);
405       thread->thread_id = -1;
406       g_mutex_unlock (thread->lock);
407
408       GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
409       GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
410       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
411
412       break;
413     case GST_STATE_PAUSED_TO_READY:
414       THR_DEBUG ("telling thread to stop spinning");
415       g_mutex_lock (thread->lock);
416       THR_DEBUG ("signaling");
417       g_cond_signal (thread->cond);
418       THR_DEBUG ("waiting for ack");
419       g_cond_wait (thread->cond, thread->lock);
420       THR_DEBUG ("got ack");
421       g_mutex_unlock (thread->lock);
422
423       break;
424     default:
425       GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x", transition);
426       break;
427   }
428
429   return stateset;
430 }
431
432 /**
433  * gst_thread_main_loop:
434  * @arg: the thread to start
435  *
436  * The main loop of the thread. The thread will iterate
437  * while the state is GST_THREAD_STATE_SPINNING
438  */
439 static void *
440 gst_thread_main_loop (void *arg)
441 {
442   GstThread *thread = GST_THREAD (arg);
443   gint stateset;
444
445   g_mutex_lock (thread->lock);
446
447   gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
448   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
449
450   thread->pid = getpid();
451   THR_INFO_MAIN("thread is running");
452
453   /* first we need to change the state of all the children */
454   if (GST_ELEMENT_CLASS (parent_class)->change_state) {
455     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
456
457     if (stateset != GST_STATE_SUCCESS) {
458       THR_DEBUG_MAIN ("state change of children failed");
459     }
460   }
461
462
463   THR_DEBUG_MAIN ("indicating spinup");
464   g_cond_signal (thread->cond);
465   /* don't unlock the mutex because we hold it into the top of the while loop */
466   THR_DEBUG_MAIN ("thread has indicated spinup to parent process");
467
468   /***** THREAD IS NOW IN READY STATE *****/
469
470   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
471     /* NOTE we hold the thread lock at this point */
472     /* what we do depends on what state we're in */
473     switch (GST_STATE (thread)) {
474       /* NOTE: cannot be in NULL, we're not running in that state at all */
475       case GST_STATE_READY:
476         /* wait to be set to either the NULL or PAUSED states */
477         THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
478                         gst_element_statename (GST_STATE_READY),
479                         gst_element_statename (GST_STATE_NULL),
480                         gst_element_statename (GST_STATE_PAUSED));
481         g_cond_wait (thread->cond,thread->lock);
482         
483         /* this must have happened by a state change in the thread context */
484         if (GST_STATE_PENDING (thread) != GST_STATE_NULL &&
485             GST_STATE_PENDING (thread) != GST_STATE_PAUSED) {
486           g_cond_signal (thread->cond);
487           continue;
488         }
489
490         /* been signaled, we need to state transition now and signal back */
491         gst_thread_update_state (thread);
492         THR_DEBUG_MAIN ("done with state transition, signaling back to parent process");
493         g_cond_signal (thread->cond);
494         /* now we decide what to do next */
495         if (GST_STATE (thread) == GST_STATE_NULL) {
496           /* REAPING must be set, we can simply break this iteration */
497           GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
498         }
499         continue;
500       case GST_STATE_PAUSED:
501         /* wait to be set to either the READY or PLAYING states */
502         THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s",
503                        gst_element_statename (GST_STATE_PAUSED),
504                        gst_element_statename (GST_STATE_READY),
505                        gst_element_statename (GST_STATE_PLAYING));
506         g_cond_wait (thread->cond,thread->lock);
507
508         /* this must have happened by a state change in the thread context */
509         if (GST_STATE_PENDING (thread) != GST_STATE_READY &&
510             GST_STATE_PENDING (thread) != GST_STATE_PLAYING) {
511           g_cond_signal (thread->cond);
512           continue;
513         }
514
515         /* been signaled, we need to state transition now and signal back */
516         gst_thread_update_state (thread);
517         /* now we decide what to do next */
518         if (GST_STATE (thread) != GST_STATE_PLAYING) {
519           /* either READY or the state change failed for some reason */
520           g_cond_signal (thread->cond);
521           continue;
522         } 
523         else {
524           GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
525           /* PLAYING is coming up, so we can now start spinning */
526           while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
527             gboolean status;
528
529             g_cond_signal (thread->cond);
530             g_mutex_unlock (thread->lock);
531             status = gst_bin_iterate (GST_BIN (thread));
532             g_mutex_lock (thread->lock);
533             /* g_cond_signal(thread->cond); */
534
535             if (!status || GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING)
536               GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
537           }
538           /* looks like we were stopped because of a statechange */
539           if (GST_STATE_PENDING (thread)) {
540             gst_thread_update_state (thread);
541           }
542           /* once we're here, SPINNING has stopped, we should signal that we're done */
543           THR_DEBUG_MAIN ("SPINNING stopped, signaling back to parent process");
544           g_cond_signal (thread->cond);
545           /* now we can wait for PAUSED */
546           continue;
547         }
548       case GST_STATE_PLAYING:
549         /* wait to be set to PAUSED */
550         THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
551                         gst_element_statename(GST_STATE_PLAYING),
552                         gst_element_statename(GST_STATE_PAUSED));
553         g_cond_wait (thread->cond,thread->lock);
554
555         /* been signaled, we need to state transition now and signal back */
556         gst_thread_update_state (thread);
557         g_cond_signal (thread->cond);
558         /* now we decide what to do next */
559         /* there's only PAUSED, we we just wait for it */
560         continue;
561       case GST_STATE_NULL:
562         THR_DEBUG_MAIN ("thread in %s state, preparing to die",
563                         gst_element_statename(GST_STATE_NULL));
564         GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
565         break;
566       default:
567         g_assert_not_reached ();
568         break;
569     }
570   }
571   /* we need to destroy the scheduler here bacause it has mapped it's
572    * stack into the threads stack space */
573   gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
574
575   /* since we don't unlock at the end of the while loop, do it here */
576   g_mutex_unlock (thread->lock);
577
578   GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
579                   GST_ELEMENT_NAME (thread));
580   return NULL;
581 }
582
583 #ifndef GST_DISABLE_LOADSAVE
584 static xmlNodePtr
585 gst_thread_save_thyself (GstObject *object,
586                          xmlNodePtr self)
587 {
588   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
589     GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
590   return NULL;
591 }
592
593 static void
594 gst_thread_restore_thyself (GstObject *object,
595                             xmlNodePtr self)
596 {
597   GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
598
599   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
600     GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
601 }
602 #endif /* GST_DISABLE_LOADSAVE */