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