More massive changes to the scheduling system. Moved the scheduling code to gstsched...
[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 GstBinClass *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
109   gstbin_class->create_plan = gst_thread_create_plan_dummy;
110
111   gtkobject_class->set_arg = gst_thread_set_arg;
112   gtkobject_class->get_arg = gst_thread_get_arg;
113
114 }
115
116 static void 
117 gst_thread_init (GstThread *thread) 
118 {
119   DEBUG("initializing thread '%s'\n",gst_element_get_name(GST_ELEMENT(thread)));
120
121   // we're a manager by default
122   GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
123
124   // default is to create a thread
125   GST_FLAG_SET (thread, GST_THREAD_CREATE);
126   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
127
128   thread->lock = g_mutex_new();
129   thread->cond = g_cond_new();
130 }
131
132 static void 
133 gst_thread_create_plan_dummy (GstBin *bin) 
134 {
135   g_return_if_fail (GST_IS_THREAD (bin));
136
137   if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING)) 
138     gst_info("gstthread: create plan delayed until thread starts\n");
139 }
140
141 static void 
142 gst_thread_set_arg (GtkObject *object,
143                     GtkArg *arg,
144                     guint id) 
145 {
146   /* it's not null if we got it, but it might not be ours */
147   g_return_if_fail (GST_IS_THREAD (object));
148
149   switch(id) {
150     case ARG_CREATE_THREAD:
151       if (GTK_VALUE_BOOL (*arg)) {
152         gst_info("gstthread: turning ON the creation of the thread\n");
153         GST_FLAG_SET (object, GST_THREAD_CREATE);
154         gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
155       } else {
156         gst_info("gstthread: turning OFF the creation of the thread\n");
157         GST_FLAG_UNSET (object, GST_THREAD_CREATE);
158         gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
159       }
160       break;
161     default:
162       break;
163   }
164 }
165
166 static void 
167 gst_thread_get_arg (GtkObject *object,
168                     GtkArg *arg,
169                     guint id) 
170 {
171   /* it's not null if we got it, but it might not be ours */
172   g_return_if_fail (GST_IS_THREAD (object));
173
174   switch (id) {
175     case ARG_CREATE_THREAD:
176       GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
177       break;
178     default:
179       break;
180   }
181 }
182
183
184 /**
185  * gst_thread_new:
186  * @name: the name of the thread
187  *
188  * Create a new thrad with the given name
189  *
190  * Returns; The new thread
191  */
192 GstElement*
193 gst_thread_new (guchar *name) 
194 {
195   return gst_elementfactory_make ("thread", name);
196 }
197
198
199
200 static GstElementStateReturn 
201 gst_thread_change_state (GstElement *element) 
202 {
203   GstThread *thread;
204   gboolean stateset = GST_STATE_SUCCESS;
205   gint pending, transition;
206
207   g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
208   DEBUG_ENTER("(\"%s\")",gst_element_get_name(element));
209
210   thread = GST_THREAD (element);
211
212   gst_info("gstthread: thread \"%s\" change state %d\n",
213                gst_element_get_name (GST_ELEMENT (element)), 
214                GST_STATE_PENDING (element));
215
216   pending = GST_STATE_PENDING (element);
217   transition = GST_STATE_TRANSITION (element);
218
219   if (pending == GST_STATE (element)) return GST_STATE_SUCCESS;
220
221   GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
222
223   if (GST_ELEMENT_CLASS (parent_class)->change_state)
224     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
225   
226   gst_info("gstthread: stateset %d %d %d %02x\n", GST_STATE (element), stateset, 
227                   GST_STATE_PENDING (element), GST_STATE_TRANSITION (element));
228
229   switch (transition) {
230     case GST_STATE_NULL_TO_READY:
231       if (!stateset) return FALSE;
232       // we want to prepare our internal state for doing the iterations
233       gst_info("gstthread: preparing thread \"%s\" for iterations:\n",
234                gst_element_get_name (GST_ELEMENT (element)));
235       
236       // set the state to idle
237       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
238       // create the thread if that's what we're supposed to do
239       gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (thread));
240       
241       if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
242         gst_info("gstthread: starting thread \"%s\"\n",
243                  gst_element_get_name (GST_ELEMENT (element)));
244         
245         pthread_create (&thread->thread_id, NULL,
246                         gst_thread_main_loop, thread);
247       } else {
248         gst_info("gstthread: NOT starting thread \"%s\"\n",
249                 gst_element_get_name (GST_ELEMENT (element)));
250       }
251       return GST_STATE_ASYNC;
252       break;
253     case GST_STATE_PAUSED_TO_PLAYING:
254     case GST_STATE_READY_TO_PLAYING:
255       if (!stateset) return FALSE;
256       gst_info("gstthread: starting thread \"%s\"\n",
257               gst_element_get_name (GST_ELEMENT (element)));
258       
259       GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
260       gst_thread_signal_thread (thread);
261       break;  
262     case GST_STATE_PLAYING_TO_PAUSED:
263       gst_info("gstthread: pausing thread \"%s\"\n",
264               gst_element_get_name (GST_ELEMENT (element)));
265       
266       //GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
267       gst_thread_signal_thread (thread);
268       break;
269     case GST_STATE_READY_TO_NULL:
270       gst_info("gstthread: stopping thread \"%s\"\n",
271               gst_element_get_name (GST_ELEMENT (element)));
272       
273       GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
274       gst_thread_signal_thread (thread);
275       break;
276     default:
277       break;
278   }
279
280   return stateset;
281 }
282
283 /**
284  * gst_thread_main_loop:
285  * @arg: the thread to start
286  *
287  * The main loop of the thread. The thread will iterate
288  * while the state is GST_THREAD_STATE_SPINNING
289  */
290 static void *
291 gst_thread_main_loop (void *arg) 
292 {
293   GstThread *thread = GST_THREAD (arg);
294
295   gst_info("gstthread: thread \"%s\" is running with PID %d\n",
296                   gst_element_get_name (GST_ELEMENT (thread)), getpid ());
297
298   if (GST_BIN_CLASS (parent_class)->create_plan)
299     GST_BIN_CLASS (parent_class)->create_plan (GST_BIN (thread));
300
301   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
302     if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING))
303       gst_bin_iterate (GST_BIN (thread));
304     else {
305       g_mutex_lock (thread->lock);
306       g_cond_wait (thread->cond, thread->lock);
307       g_mutex_unlock (thread->lock);
308     }
309   }
310
311   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
312   pthread_join (thread->thread_id, 0);
313
314   gst_info("gstthread: thread \"%s\" is stopped\n",
315                   gst_element_get_name (GST_ELEMENT (thread)));
316   return NULL;
317 }
318
319 static void 
320 gst_thread_signal_thread (GstThread *thread) 
321 {
322   g_mutex_lock (thread->lock);
323   g_cond_signal (thread->cond);
324   g_mutex_unlock (thread->lock);
325 }
326
327 static void 
328 gst_thread_restore_thyself (GstElement *element,
329                             xmlNodePtr parent, 
330                             GHashTable *elements) 
331 {
332   g_print("gstthread: restore\n");
333
334   if (GST_ELEMENT_CLASS (parent_class)->restore_thyself)
335     GST_ELEMENT_CLASS (parent_class)->restore_thyself (element,parent, elements);
336 }
337
338 static xmlNodePtr 
339 gst_thread_save_thyself (GstElement *element,
340                          xmlNodePtr parent) 
341 {
342   if (GST_ELEMENT_CLASS (parent_class)->save_thyself)
343     GST_ELEMENT_CLASS (parent_class)->save_thyself (element,parent);
344   return NULL;
345 }