Code cleanup, make it adhere to the Gnome/Gtk+ code formatting, which is quite clean...
[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 <gst/gst.h>
21 #include <gst/gstthread.h>
22
23 #include "config.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 static GstElementStateReturn gst_thread_change_state(GstElement *element);
53
54 static xmlNodePtr gst_thread_save_thyself(GstElement *element,xmlNodePtr parent);
55 static void gst_thread_restore_thyself(GstElement *element,xmlNodePtr parent, GHashTable *elements);
56
57 static void gst_thread_signal_thread(GstThread *thread);
58 static void gst_thread_create_plan_dummy(GstBin *bin);
59
60 static void *gst_thread_main_loop(void *arg);
61
62 static GstBin *parent_class = NULL;
63 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
64
65 GtkType
66 gst_thread_get_type(void) {
67   static GtkType thread_type = 0;
68
69   if (!thread_type) {
70     static const GtkTypeInfo thread_info = {
71       "GstThread",
72       sizeof(GstThread),
73       sizeof(GstThreadClass),
74       (GtkClassInitFunc)gst_thread_class_init,
75       (GtkObjectInitFunc)gst_thread_init,
76       (GtkArgSetFunc)NULL,
77       (GtkArgGetFunc)NULL,
78       (GtkClassInitFunc)NULL,
79     };
80     thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
81   }
82   return thread_type;
83 }
84
85 static void
86 gst_thread_class_init(GstThreadClass *klass) {
87   GtkObjectClass *gtkobject_class;
88   GstObjectClass *gstobject_class;
89   GstElementClass *gstelement_class;
90   GstBinClass *gstbin_class;
91
92   gtkobject_class = (GtkObjectClass*)klass;
93   gstobject_class = (GstObjectClass*)klass;
94   gstelement_class = (GstElementClass*)klass;
95   gstbin_class = (GstBinClass*)klass;
96
97   parent_class = gtk_type_class(GST_TYPE_BIN);
98
99   gtk_object_add_arg_type("GstThread::create_thread", GTK_TYPE_BOOL,
100                           GTK_ARG_READWRITE, ARG_CREATE_THREAD);
101
102   gstelement_class->change_state = gst_thread_change_state;
103   gstelement_class->save_thyself = gst_thread_save_thyself;
104   gstelement_class->restore_thyself = gst_thread_restore_thyself;
105   gstelement_class->elementfactory = gst_elementfactory_find("thread");
106
107   gstbin_class->create_plan = gst_thread_create_plan_dummy;
108
109   gtkobject_class->set_arg = gst_thread_set_arg;
110   gtkobject_class->get_arg = gst_thread_get_arg;
111
112 }
113
114 static void gst_thread_init(GstThread *thread) {
115   GST_FLAG_SET(thread,GST_THREAD_CREATE);
116
117   thread->lock = g_mutex_new();
118   thread->cond = g_cond_new();
119 }
120
121 static void gst_thread_create_plan_dummy(GstBin *bin) {
122   gst_info("gstthread: create plan delayed until thread starts\n");
123 }
124
125 static void gst_thread_set_arg(GtkObject *object,GtkArg *arg,guint id) {
126   /* it's not null if we got it, but it might not be ours */
127   g_return_if_fail(GST_IS_THREAD(object));
128
129   switch(id) {
130     case ARG_CREATE_THREAD:
131       if (GTK_VALUE_BOOL(*arg)) {
132         gst_info("gstthread: turning ON the creation of the thread\n");
133         GST_FLAG_SET(object,GST_THREAD_CREATE);
134         gst_info("gstthread: flags are 0x%08x\n",GST_FLAGS(object));
135       } else {
136         gst_info("gstthread: turning OFF the creation of the thread\n");
137         GST_FLAG_UNSET(object,GST_THREAD_CREATE);
138         gst_info("gstthread: flags are 0x%08x\n",GST_FLAGS(object));
139       }
140       break;
141     default:
142       break;
143   }
144 }
145
146 static void gst_thread_get_arg(GtkObject *object,GtkArg *arg,guint id) {
147   /* it's not null if we got it, but it might not be ours */
148   g_return_if_fail(GST_IS_THREAD(object));
149
150   switch(id) {
151     case ARG_CREATE_THREAD:
152       GTK_VALUE_BOOL(*arg) = GST_FLAG_IS_SET(object,GST_THREAD_CREATE);
153       break;
154     default:
155       break;
156   }
157 }
158
159
160 /**
161  * gst_thread_new:
162  * @name: the name of the thread
163  *
164  * Create a new thrad with the given name
165  *
166  * Returns; The new thread
167  */
168 GstElement *gst_thread_new(guchar *name) {
169   GstThread *thread;
170
171   thread = gtk_type_new(gst_thread_get_type());
172   gst_element_set_name(GST_ELEMENT(thread),name);
173   GST_FLAG_UNSET(thread,GST_THREAD_STATE_REAPING);
174   return GST_ELEMENT(thread);
175 }
176
177
178
179 static GstElementStateReturn gst_thread_change_state(GstElement *element) {
180   GstThread *thread;
181   gboolean stateset = TRUE;
182   gint pending;
183
184   g_return_val_if_fail(GST_IS_THREAD(element), FALSE);
185   thread = GST_THREAD(element);
186
187   gst_info("gstthread: thread \"%s\" change state %d\n",
188                gst_element_get_name(GST_ELEMENT(element)), GST_STATE_PENDING(element));
189
190   pending = GST_STATE_PENDING(element);
191
192   if (pending == GST_STATE(element)) return GST_STATE_SUCCESS;
193
194   GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
195
196   if (GST_ELEMENT_CLASS(parent_class)->change_state)
197     stateset = GST_ELEMENT_CLASS(parent_class)->change_state(element);
198   
199   gst_info("gstthread: stateset %d %d %d\n", GST_STATE(element), stateset, GST_STATE_PENDING(element));
200
201   switch (pending) {
202     case GST_STATE_READY:
203       if (!stateset) return FALSE;
204       // we want to prepare our internal state for doing the iterations
205       gst_info("gstthread: preparing thread \"%s\" for iterations:\n",
206                gst_element_get_name(GST_ELEMENT(element)));
207       
208       // set the state to idle
209       GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
210       // create the thread if that's what we're supposed to do
211       gst_info("gstthread: flags are 0x%08x\n",GST_FLAGS(thread));
212       if (GST_FLAG_IS_SET(thread,GST_THREAD_CREATE)) {
213         gst_info("gstthread: starting thread \"%s\"\n",
214                  gst_element_get_name(GST_ELEMENT(element)));
215         pthread_create(&thread->thread_id,NULL,
216                        gst_thread_main_loop,thread);
217       } else {
218         gst_info("gstthread: NOT starting thread \"%s\"\n",
219                 gst_element_get_name(GST_ELEMENT(element)));
220       }
221       return GST_STATE_SUCCESS;
222       break;
223     case GST_STATE_PLAYING:
224       if (!stateset) return FALSE;
225       gst_info("gstthread: starting thread \"%s\"\n",
226               gst_element_get_name(GST_ELEMENT(element)));
227       GST_FLAG_SET(thread,GST_THREAD_STATE_SPINNING);
228       gst_thread_signal_thread(thread);
229       break;  
230     case GST_STATE_PAUSED:
231       gst_info("gstthread: pausing thread \"%s\"\n",
232               gst_element_get_name(GST_ELEMENT(element)));
233       //GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
234       gst_thread_signal_thread(thread);
235       break;
236     case GST_STATE_NULL:
237       gst_info("gstthread: stopping thread \"%s\"\n",
238               gst_element_get_name(GST_ELEMENT(element)));
239       GST_FLAG_SET(thread,GST_THREAD_STATE_REAPING);
240       gst_thread_signal_thread(thread);
241       break;
242     default:
243       break;
244   }
245
246   return stateset;
247 }
248
249 /**
250  * gst_thread_main_loop:
251  * @arg: the thread to start
252  *
253  * The main loop of the thread. The thread will iterate
254  * while the state is GST_THREAD_STATE_SPINNING
255  */
256 static void *gst_thread_main_loop(void *arg) {
257   GstThread *thread = GST_THREAD(arg);
258
259   gst_info("gstthread: thread \"%s\" is running with PID %d\n",
260                   gst_element_get_name(GST_ELEMENT(thread)), getpid());
261
262   if (GST_BIN_CLASS(parent_class)->create_plan)
263     GST_BIN_CLASS(parent_class)->create_plan(GST_BIN(thread));
264
265   while(!GST_FLAG_IS_SET(thread,GST_THREAD_STATE_REAPING)) {
266     if (GST_FLAG_IS_SET(thread,GST_THREAD_STATE_SPINNING))
267       gst_bin_iterate(GST_BIN(thread));
268     else {
269       g_mutex_lock(thread->lock);
270       g_cond_wait(thread->cond,thread->lock);
271       g_mutex_unlock(thread->lock);
272     }
273   }
274
275   GST_FLAG_UNSET(thread,GST_THREAD_STATE_REAPING);
276   pthread_join(thread->thread_id,0);
277
278   gst_info("gstthread: thread \"%s\" is stopped\n",
279                   gst_element_get_name(GST_ELEMENT(thread)));
280   return NULL;
281 }
282
283 static void gst_thread_signal_thread(GstThread *thread) {
284   g_mutex_lock(thread->lock);
285   g_cond_signal(thread->cond);
286   g_mutex_unlock(thread->lock);
287 }
288
289 static void gst_thread_restore_thyself(GstElement *element,xmlNodePtr parent, GHashTable *elements) {
290
291   g_print("gstthread: restore\n");
292
293   if (GST_ELEMENT_CLASS(parent_class)->restore_thyself)
294     GST_ELEMENT_CLASS(parent_class)->restore_thyself(element,parent, elements);
295 }
296
297 static xmlNodePtr gst_thread_save_thyself(GstElement *element,xmlNodePtr parent) {
298
299   if (GST_ELEMENT_CLASS(parent_class)->save_thyself)
300     GST_ELEMENT_CLASS(parent_class)->save_thyself(element,parent);
301   return NULL;
302 }