This is a megapatch with the following changes:
[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, 2000",
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         (GstObject *object, xmlNodePtr parent);
62 static void                     gst_thread_restore_thyself      (GstObject *object, xmlNodePtr self);
63
64 static void                     gst_thread_signal_thread        (GstThread *thread);
65 static void                     gst_thread_wait_thread          (GstThread *thread);
66 static void                     gst_thread_schedule_dummy       (GstBin *bin);
67
68 static void*                    gst_thread_main_loop            (void *arg);
69
70 static GstBinClass *parent_class = NULL;
71 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
72
73 GtkType
74 gst_thread_get_type(void) {
75   static GtkType thread_type = 0;
76
77   if (!thread_type) {
78     static const GtkTypeInfo thread_info = {
79       "GstThread",
80       sizeof(GstThread),
81       sizeof(GstThreadClass),
82       (GtkClassInitFunc)gst_thread_class_init,
83       (GtkObjectInitFunc)gst_thread_init,
84       (GtkArgSetFunc)NULL,
85       (GtkArgGetFunc)NULL,
86       (GtkClassInitFunc)NULL,
87     };
88     thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
89   }
90   return thread_type;
91 }
92
93 static void
94 gst_thread_class_init (GstThreadClass *klass)
95 {
96   GtkObjectClass *gtkobject_class;
97   GstObjectClass *gstobject_class;
98   GstElementClass *gstelement_class;
99   GstBinClass *gstbin_class;
100
101   gtkobject_class =     (GtkObjectClass*)klass;
102   gstobject_class =     (GstObjectClass*)klass;
103   gstelement_class =    (GstElementClass*)klass;
104   gstbin_class =        (GstBinClass*)klass;
105
106   parent_class = gtk_type_class (GST_TYPE_BIN);
107
108   gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
109                            GTK_ARG_READWRITE, ARG_CREATE_THREAD);
110
111   gstobject_class->save_thyself =       gst_thread_save_thyself;
112   gstobject_class->restore_thyself =    gst_thread_restore_thyself;
113
114   gstelement_class->change_state =      gst_thread_change_state;
115
116   gstbin_class->schedule = gst_thread_schedule_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   GST_DEBUG (0,"initializing thread '%s'\n",GST_ELEMENT_NAME (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_schedule_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 (GST_CAT_THREAD,"gstthread: scheduling delayed until thread starts");
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 (GST_CAT_THREAD,"gstthread: turning ON the creation of the thread");
160         GST_FLAG_SET (object, GST_THREAD_CREATE);
161         GST_DEBUG (0,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
162       } else {
163         GST_INFO (GST_CAT_THREAD,"gstthread: turning OFF the creation of the thread");
164         GST_FLAG_UNSET (object, GST_THREAD_CREATE);
165         GST_DEBUG (0,"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 thread 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   GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME(element));
216
217   thread = GST_THREAD (element);
218
219   GST_INFO (GST_CAT_THREAD,"gstthread: thread \"%s\" change state %d",
220                GST_ELEMENT_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 (GST_CAT_THREAD, "gstthread: stateset %d %d %d %02x", 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 (GST_CAT_THREAD, "gstthread: preparing thread \"%s\" for iterations:",
241                GST_ELEMENT_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 (GST_CAT_THREAD, "gstthread: flags are 0x%08x", GST_FLAGS (thread));
247
248       if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
249         GST_INFO (GST_CAT_THREAD, "gstthread: starting thread \"%s\"",
250                  GST_ELEMENT_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 (GST_CAT_THREAD, "gstthread: NOT starting thread \"%s\"",
260                 GST_ELEMENT_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 (GST_CAT_THREAD, "gstthread: starting thread \"%s\"",
267               GST_ELEMENT_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 (GST_CAT_THREAD,"gstthread: pausing thread \"%s\"",
274               GST_ELEMENT_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 (GST_CAT_THREAD,"gstthread: stopping thread \"%s\"",
281               GST_ELEMENT_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 (GST_CAT_THREAD,"gstthread: thread \"%s\" is running with PID %d",
306                   GST_ELEMENT_NAME (GST_ELEMENT (thread)), getpid ());
307
308   // construct the plan and signal back
309   if (GST_BIN_CLASS (parent_class)->schedule)
310     GST_BIN_CLASS (parent_class)->schedule (GST_BIN (thread));
311
312   gst_thread_signal_thread (thread);
313
314   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
315     if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
316       if (!gst_bin_iterate (GST_BIN (thread))) {
317         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
318       }
319     }
320     else {
321       GST_DEBUG (0, "thread \"%s\" waiting\n", GST_ELEMENT_NAME (GST_ELEMENT (thread)));
322       gst_thread_wait_thread (thread);
323     }
324   }
325
326   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
327   //pthread_join (thread->thread_id, 0);
328
329   GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
330                   GST_ELEMENT_NAME (thread));
331   return NULL;
332 }
333
334 static void
335 gst_thread_signal_thread (GstThread *thread)
336 {
337   GST_DEBUG (0,"signaling thread\n");
338   g_mutex_lock (thread->lock);
339   g_cond_signal (thread->cond);
340   g_mutex_unlock (thread->lock);
341 }
342
343 static void
344 gst_thread_wait_thread (GstThread *thread)
345 {
346   GST_DEBUG (0,"waiting for thread\n");
347   g_mutex_lock (thread->lock);
348   g_cond_wait (thread->cond, thread->lock);
349   g_mutex_unlock (thread->lock);
350 }
351
352
353 static void
354 gst_thread_restore_thyself (GstObject *object,
355                             xmlNodePtr self)
356 {
357   GST_DEBUG (0,"gstthread: restore\n");
358
359   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
360     GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
361 }
362
363 static xmlNodePtr
364 gst_thread_save_thyself (GstObject *object,
365                          xmlNodePtr self)
366 {
367   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
368     GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
369   return NULL;
370 }