Updated copyright in all the libgst files.
[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
30
31 GstElementDetails gst_thread_details = {
32   "Threaded container",
33   "Bin",
34   "Container that creates/manages a thread",
35   VERSION,
36   "Erik Walthinsen <omega@cse.ogi.edu>",
37   "(C) 1999",
38 };
39
40
41 /* Thread signals and args */
42 enum {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46
47 enum {
48   ARG_0,
49   ARG_CREATE_THREAD,
50 };
51
52
53 static void                     gst_thread_class_init           (GstThreadClass *klass);
54 static void                     gst_thread_init                 (GstThread *thread);
55
56 static void                     gst_thread_set_arg              (GtkObject *object,GtkArg *arg,guint id);
57 static void                     gst_thread_get_arg              (GtkObject *object,GtkArg *arg,guint id);
58
59 static GstElementStateReturn    gst_thread_change_state         (GstElement *element);
60
61 static xmlNodePtr               gst_thread_save_thyself         (GstElement *element,xmlNodePtr parent);
62 static void                     gst_thread_restore_thyself      (GstElement *element,xmlNodePtr parent, 
63                                                                  GHashTable *elements);
64
65 static void                     gst_thread_signal_thread        (GstThread *thread);
66 static void                     gst_thread_wait_thread          (GstThread *thread);
67 static void                     gst_thread_create_plan_dummy    (GstBin *bin);
68
69 static void*                    gst_thread_main_loop            (void *arg);
70
71 static GstBinClass *parent_class = NULL;
72 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
73
74 GtkType
75 gst_thread_get_type(void) {
76   static GtkType thread_type = 0;
77
78   if (!thread_type) {
79     static const GtkTypeInfo thread_info = {
80       "GstThread",
81       sizeof(GstThread),
82       sizeof(GstThreadClass),
83       (GtkClassInitFunc)gst_thread_class_init,
84       (GtkObjectInitFunc)gst_thread_init,
85       (GtkArgSetFunc)NULL,
86       (GtkArgGetFunc)NULL,
87       (GtkClassInitFunc)NULL,
88     };
89     thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
90   }
91   return thread_type;
92 }
93
94 static void
95 gst_thread_class_init (GstThreadClass *klass) 
96 {
97   GtkObjectClass *gtkobject_class;
98   GstObjectClass *gstobject_class;
99   GstElementClass *gstelement_class;
100   GstBinClass *gstbin_class;
101
102   gtkobject_class =     (GtkObjectClass*)klass;
103   gstobject_class =     (GstObjectClass*)klass;
104   gstelement_class =    (GstElementClass*)klass;
105   gstbin_class =        (GstBinClass*)klass;
106
107   parent_class = gtk_type_class (GST_TYPE_BIN);
108
109   gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
110                            GTK_ARG_READWRITE, ARG_CREATE_THREAD);
111
112   gstelement_class->change_state =      gst_thread_change_state;
113   gstelement_class->save_thyself =      gst_thread_save_thyself;
114   gstelement_class->restore_thyself =   gst_thread_restore_thyself;
115
116   gstbin_class->create_plan = gst_thread_create_plan_dummy;
117
118   gtkobject_class->set_arg = gst_thread_set_arg;
119   gtkobject_class->get_arg = gst_thread_get_arg;
120
121 }
122
123 static void 
124 gst_thread_init (GstThread *thread) 
125 {
126   DEBUG("initializing thread '%s'\n",gst_element_get_name(GST_ELEMENT(thread)));
127
128   // we're a manager by default
129   GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
130
131   // default is to create a thread
132   GST_FLAG_SET (thread, GST_THREAD_CREATE);
133   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
134
135   thread->lock = g_mutex_new();
136   thread->cond = g_cond_new();
137 }
138
139 static void 
140 gst_thread_create_plan_dummy (GstBin *bin) 
141 {
142   g_return_if_fail (GST_IS_THREAD (bin));
143
144   if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING)) 
145     gst_info("gstthread: create plan delayed until thread starts\n");
146 }
147
148 static void 
149 gst_thread_set_arg (GtkObject *object,
150                     GtkArg *arg,
151                     guint id) 
152 {
153   /* it's not null if we got it, but it might not be ours */
154   g_return_if_fail (GST_IS_THREAD (object));
155
156   switch(id) {
157     case ARG_CREATE_THREAD:
158       if (GTK_VALUE_BOOL (*arg)) {
159         gst_info("gstthread: turning ON the creation of the thread\n");
160         GST_FLAG_SET (object, GST_THREAD_CREATE);
161         gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
162       } else {
163         gst_info("gstthread: turning OFF the creation of the thread\n");
164         GST_FLAG_UNSET (object, GST_THREAD_CREATE);
165         gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
166       }
167       break;
168     default:
169       break;
170   }
171 }
172
173 static void 
174 gst_thread_get_arg (GtkObject *object,
175                     GtkArg *arg,
176                     guint id) 
177 {
178   /* it's not null if we got it, but it might not be ours */
179   g_return_if_fail (GST_IS_THREAD (object));
180
181   switch (id) {
182     case ARG_CREATE_THREAD:
183       GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
184       break;
185     default:
186       break;
187   }
188 }
189
190
191 /**
192  * gst_thread_new:
193  * @name: the name of the thread
194  *
195  * Create a new thrad with the given name
196  *
197  * Returns: The new thread
198  */
199 GstElement*
200 gst_thread_new (guchar *name) 
201 {
202   return gst_elementfactory_make ("thread", name);
203 }
204
205
206
207 static GstElementStateReturn 
208 gst_thread_change_state (GstElement *element) 
209 {
210   GstThread *thread;
211   gboolean stateset = GST_STATE_SUCCESS;
212   gint pending, transition;
213
214   g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
215   DEBUG_ENTER("(\"%s\")",gst_element_get_name(element));
216
217   thread = GST_THREAD (element);
218
219   gst_info("gstthread: thread \"%s\" change state %d\n",
220                gst_element_get_name (GST_ELEMENT (element)), 
221                GST_STATE_PENDING (element));
222
223   pending = GST_STATE_PENDING (element);
224   transition = GST_STATE_TRANSITION (element);
225
226 //  if (pending == GST_STATE (element)) return GST_STATE_SUCCESS;
227
228   GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
229
230   if (GST_ELEMENT_CLASS (parent_class)->change_state)
231     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
232   
233   gst_info("gstthread: stateset %d %d %d %02x\n", GST_STATE (element), stateset, 
234                   GST_STATE_PENDING (element), GST_STATE_TRANSITION (element));
235
236   switch (transition) {
237     case GST_STATE_NULL_TO_READY:
238 //      if (!stateset) return FALSE;
239       // we want to prepare our internal state for doing the iterations
240       gst_info("gstthread: preparing thread \"%s\" for iterations:\n",
241                gst_element_get_name (GST_ELEMENT (element)));
242
243       // set the state to idle
244       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
245       // create the thread if that's what we're supposed to do
246       gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (thread));
247
248       if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
249         gst_info("gstthread: starting thread \"%s\"\n",
250                  gst_element_get_name (GST_ELEMENT (element)));
251
252         // create the thread
253         pthread_create (&thread->thread_id, NULL,
254                         gst_thread_main_loop, thread);
255
256         // wait for it to 'spin up'
257 //        gst_thread_wait_thread (thread);
258       } else {
259         gst_info("gstthread: NOT starting thread \"%s\"\n",
260                 gst_element_get_name (GST_ELEMENT (element)));
261       }
262       break;
263     case GST_STATE_PAUSED_TO_PLAYING:
264     case GST_STATE_READY_TO_PLAYING:
265       if (!stateset) return FALSE;
266       gst_info("gstthread: starting thread \"%s\"\n",
267               gst_element_get_name (GST_ELEMENT (element)));
268
269       GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
270       gst_thread_signal_thread (thread);
271       break;  
272     case GST_STATE_PLAYING_TO_PAUSED:
273       gst_info("gstthread: pausing thread \"%s\"\n",
274               gst_element_get_name (GST_ELEMENT (element)));
275       
276       //GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
277       gst_thread_signal_thread (thread);
278       break;
279     case GST_STATE_READY_TO_NULL:
280       gst_info("gstthread: stopping thread \"%s\"\n",
281               gst_element_get_name (GST_ELEMENT (element)));
282       
283       GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
284       gst_thread_signal_thread (thread);
285       break;
286     default:
287       break;
288   }
289
290   return stateset;
291 }
292
293 /**
294  * gst_thread_main_loop:
295  * @arg: the thread to start
296  *
297  * The main loop of the thread. The thread will iterate
298  * while the state is GST_THREAD_STATE_SPINNING
299  */
300 static void *
301 gst_thread_main_loop (void *arg) 
302 {
303   GstThread *thread = GST_THREAD (arg);
304
305   gst_info("gstthread: thread \"%s\" is running with PID %d\n",
306                   gst_element_get_name (GST_ELEMENT (thread)), getpid ());
307
308   // construct the plan and signal back
309   if (GST_BIN_CLASS (parent_class)->create_plan)
310     GST_BIN_CLASS (parent_class)->create_plan (GST_BIN (thread));
311   gst_thread_signal_thread (thread);
312
313   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
314     if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING))
315       gst_bin_iterate (GST_BIN (thread));
316     else {
317       gst_thread_wait_thread (thread);
318     }
319   }
320
321   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
322 //  pthread_join (thread->thread_id, 0);
323
324   gst_info("gstthread: thread \"%s\" is stopped\n",
325                   gst_element_get_name (GST_ELEMENT (thread)));
326   return NULL;
327 }
328
329 static void 
330 gst_thread_signal_thread (GstThread *thread) 
331 {
332   DEBUG("signaling thread\n");
333   g_mutex_lock (thread->lock);
334   g_cond_signal (thread->cond);
335   g_mutex_unlock (thread->lock);
336 }
337
338 static void
339 gst_thread_wait_thread (GstThread *thread)
340 {
341   DEBUG("waiting for thread\n");
342   g_mutex_lock (thread->lock);
343   g_cond_wait (thread->cond, thread->lock);
344   g_mutex_unlock (thread->lock);
345 }
346
347
348 static void 
349 gst_thread_restore_thyself (GstElement *element,
350                             xmlNodePtr parent, 
351                             GHashTable *elements) 
352 {
353   g_print("gstthread: restore\n");
354
355   if (GST_ELEMENT_CLASS (parent_class)->restore_thyself)
356     GST_ELEMENT_CLASS (parent_class)->restore_thyself (element,parent, elements);
357 }
358
359 static xmlNodePtr 
360 gst_thread_save_thyself (GstElement *element,
361                          xmlNodePtr parent) 
362 {
363   if (GST_ELEMENT_CLASS (parent_class)->save_thyself)
364     GST_ELEMENT_CLASS (parent_class)->save_thyself (element,parent);
365   return NULL;
366 }