Header cleanup: try to include as little as possible; this will probably speed up...
[platform/upstream/gstreamer.git] / gst / gstthread.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <unistd.h>
21
22 #include "gstthread.h"
23 #include "gstdebug.h"
24
25 GstElementDetails gst_thread_details = {
26   "Threaded container",
27   "Bin",
28   "Container that creates/manages a thread",
29   VERSION,
30   "Erik Walthinsen <omega@cse.ogi.edu>",
31   "(C) 1999",
32 };
33
34
35 /* Thread signals and args */
36 enum {
37   /* FILL ME */
38   LAST_SIGNAL
39 };
40
41 enum {
42   ARG_0,
43   ARG_CREATE_THREAD,
44 };
45
46
47 static void                     gst_thread_class_init           (GstThreadClass *klass);
48 static void                     gst_thread_init                 (GstThread *thread);
49
50 static void                     gst_thread_set_arg              (GtkObject *object,GtkArg *arg,guint id);
51 static void                     gst_thread_get_arg              (GtkObject *object,GtkArg *arg,guint id);
52
53 static GstElementStateReturn    gst_thread_change_state         (GstElement *element);
54
55 static xmlNodePtr               gst_thread_save_thyself         (GstElement *element,xmlNodePtr parent);
56 static void                     gst_thread_restore_thyself      (GstElement *element,xmlNodePtr parent, 
57                                                                  GHashTable *elements);
58
59 static void                     gst_thread_signal_thread        (GstThread *thread);
60 static void                     gst_thread_create_plan_dummy    (GstBin *bin);
61
62 static void*                    gst_thread_main_loop            (void *arg);
63
64 static GstBin *parent_class = NULL;
65 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
66
67 GtkType
68 gst_thread_get_type(void) {
69   static GtkType thread_type = 0;
70
71   if (!thread_type) {
72     static const GtkTypeInfo thread_info = {
73       "GstThread",
74       sizeof(GstThread),
75       sizeof(GstThreadClass),
76       (GtkClassInitFunc)gst_thread_class_init,
77       (GtkObjectInitFunc)gst_thread_init,
78       (GtkArgSetFunc)NULL,
79       (GtkArgGetFunc)NULL,
80       (GtkClassInitFunc)NULL,
81     };
82     thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
83   }
84   return thread_type;
85 }
86
87 static void
88 gst_thread_class_init (GstThreadClass *klass) 
89 {
90   GtkObjectClass *gtkobject_class;
91   GstObjectClass *gstobject_class;
92   GstElementClass *gstelement_class;
93   GstBinClass *gstbin_class;
94
95   gtkobject_class =     (GtkObjectClass*)klass;
96   gstobject_class =     (GstObjectClass*)klass;
97   gstelement_class =    (GstElementClass*)klass;
98   gstbin_class =        (GstBinClass*)klass;
99
100   parent_class = gtk_type_class (GST_TYPE_BIN);
101
102   gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
103                            GTK_ARG_READWRITE, ARG_CREATE_THREAD);
104
105   gstelement_class->change_state =      gst_thread_change_state;
106   gstelement_class->save_thyself =      gst_thread_save_thyself;
107   gstelement_class->restore_thyself =   gst_thread_restore_thyself;
108   gstelement_class->elementfactory =    gst_elementfactory_find("thread");
109
110   gstbin_class->create_plan = gst_thread_create_plan_dummy;
111
112   gtkobject_class->set_arg = gst_thread_set_arg;
113   gtkobject_class->get_arg = gst_thread_get_arg;
114
115 }
116
117 static void 
118 gst_thread_init (GstThread *thread) 
119 {
120   GST_FLAG_SET (thread, GST_THREAD_CREATE);
121
122   thread->lock = g_mutex_new();
123   thread->cond = g_cond_new();
124 }
125
126 static void 
127 gst_thread_create_plan_dummy (GstBin *bin) 
128 {
129   g_return_if_fail (GST_IS_THREAD (bin));
130
131   if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING)) 
132     gst_info("gstthread: create plan delayed until thread starts\n");
133 }
134
135 static void 
136 gst_thread_set_arg (GtkObject *object,
137                     GtkArg *arg,
138                     guint id) 
139 {
140   /* it's not null if we got it, but it might not be ours */
141   g_return_if_fail (GST_IS_THREAD (object));
142
143   switch(id) {
144     case ARG_CREATE_THREAD:
145       if (GTK_VALUE_BOOL (*arg)) {
146         gst_info("gstthread: turning ON the creation of the thread\n");
147         GST_FLAG_SET (object, GST_THREAD_CREATE);
148         gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
149       } else {
150         gst_info("gstthread: turning OFF the creation of the thread\n");
151         GST_FLAG_UNSET (object, GST_THREAD_CREATE);
152         gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
153       }
154       break;
155     default:
156       break;
157   }
158 }
159
160 static void 
161 gst_thread_get_arg (GtkObject *object,
162                     GtkArg *arg,
163                     guint id) 
164 {
165   /* it's not null if we got it, but it might not be ours */
166   g_return_if_fail (GST_IS_THREAD (object));
167
168   switch (id) {
169     case ARG_CREATE_THREAD:
170       g_print("gstthread: query thread %d\n", GST_FLAG_IS_SET (object, GST_THREAD_CREATE));
171       GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
172       break;
173     default:
174       break;
175   }
176 }
177
178
179 /**
180  * gst_thread_new:
181  * @name: the name of the thread
182  *
183  * Create a new thrad with the given name
184  *
185  * Returns; The new thread
186  */
187 GstElement*
188 gst_thread_new (guchar *name) 
189 {
190   GstThread *thread;
191
192   thread = gtk_type_new (gst_thread_get_type ());
193   
194   gst_element_set_name (GST_ELEMENT (thread), name);
195   
196   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
197   
198   return GST_ELEMENT (thread);
199 }
200
201
202
203 static GstElementStateReturn 
204 gst_thread_change_state (GstElement *element) 
205 {
206   GstThread *thread;
207   gboolean stateset = GST_STATE_SUCCESS;
208   gint pending;
209
210   g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
211   DEBUG_ENTER("(\"%s\")",gst_element_get_name(element));
212
213   thread = GST_THREAD (element);
214
215   gst_info("gstthread: thread \"%s\" change state %d\n",
216                gst_element_get_name (GST_ELEMENT (element)), 
217                GST_STATE_PENDING (element));
218
219   pending = GST_STATE_PENDING (element);
220
221   if (pending == GST_STATE (element)) return GST_STATE_SUCCESS;
222
223   GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
224
225   if (GST_ELEMENT_CLASS (parent_class)->change_state)
226     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
227   
228   gst_info("gstthread: stateset %d %d %d\n", GST_STATE (element), stateset, 
229                   GST_STATE_PENDING (element));
230
231   switch (pending) {
232     case GST_STATE_READY:
233       if (!stateset) return FALSE;
234       // we want to prepare our internal state for doing the iterations
235       gst_info("gstthread: preparing thread \"%s\" for iterations:\n",
236                gst_element_get_name (GST_ELEMENT (element)));
237       
238       // set the state to idle
239       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
240       // create the thread if that's what we're supposed to do
241       gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (thread));
242       
243       if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
244         gst_info("gstthread: starting thread \"%s\"\n",
245                  gst_element_get_name (GST_ELEMENT (element)));
246         
247         pthread_create (&thread->thread_id, NULL,
248                         gst_thread_main_loop, thread);
249       } else {
250         gst_info("gstthread: NOT starting thread \"%s\"\n",
251                 gst_element_get_name (GST_ELEMENT (element)));
252       }
253       return GST_STATE_ASYNC;
254       break;
255     case GST_STATE_PLAYING:
256       if (!stateset) return FALSE;
257       gst_info("gstthread: starting thread \"%s\"\n",
258               gst_element_get_name (GST_ELEMENT (element)));
259       
260       GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
261       gst_thread_signal_thread (thread);
262       break;  
263     case GST_STATE_PAUSED:
264       gst_info("gstthread: pausing thread \"%s\"\n",
265               gst_element_get_name (GST_ELEMENT (element)));
266       
267       //GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
268       gst_thread_signal_thread (thread);
269       break;
270     case GST_STATE_NULL:
271       gst_info("gstthread: stopping thread \"%s\"\n",
272               gst_element_get_name (GST_ELEMENT (element)));
273       
274       GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
275       gst_thread_signal_thread (thread);
276       break;
277     default:
278       break;
279   }
280
281   return stateset;
282 }
283
284 /**
285  * gst_thread_main_loop:
286  * @arg: the thread to start
287  *
288  * The main loop of the thread. The thread will iterate
289  * while the state is GST_THREAD_STATE_SPINNING
290  */
291 static void *
292 gst_thread_main_loop (void *arg) 
293 {
294   GstThread *thread = GST_THREAD (arg);
295
296   gst_info("gstthread: thread \"%s\" is running with PID %d\n",
297                   gst_element_get_name (GST_ELEMENT (thread)), getpid ());
298
299   if (GST_BIN_CLASS (parent_class)->create_plan)
300     GST_BIN_CLASS (parent_class)->create_plan (GST_BIN (thread));
301
302   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
303     if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING))
304       gst_bin_iterate (GST_BIN (thread));
305     else {
306       g_mutex_lock (thread->lock);
307       g_cond_wait (thread->cond, thread->lock);
308       g_mutex_unlock (thread->lock);
309     }
310   }
311
312   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
313   pthread_join (thread->thread_id, 0);
314
315   gst_info("gstthread: thread \"%s\" is stopped\n",
316                   gst_element_get_name (GST_ELEMENT (thread)));
317   return NULL;
318 }
319
320 static void 
321 gst_thread_signal_thread (GstThread *thread) 
322 {
323   g_mutex_lock (thread->lock);
324   g_cond_signal (thread->cond);
325   g_mutex_unlock (thread->lock);
326 }
327
328 static void 
329 gst_thread_restore_thyself (GstElement *element,
330                             xmlNodePtr parent, 
331                             GHashTable *elements) 
332 {
333   g_print("gstthread: restore\n");
334
335   if (GST_ELEMENT_CLASS (parent_class)->restore_thyself)
336     GST_ELEMENT_CLASS (parent_class)->restore_thyself (element,parent, elements);
337 }
338
339 static xmlNodePtr 
340 gst_thread_save_thyself (GstElement *element,
341                          xmlNodePtr parent) 
342 {
343   if (GST_ELEMENT_CLASS (parent_class)->save_thyself)
344     GST_ELEMENT_CLASS (parent_class)->save_thyself (element,parent);
345   return NULL;
346 }