2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstthread.c: Threaded container object
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.
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.
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.
25 //#define GST_DEBUG_ENABLED
26 #include "gst_private.h"
28 #include "gstthread.h"
31 GstElementDetails gst_thread_details = {
34 "Container that creates/manages a thread",
36 "Erik Walthinsen <omega@cse.ogi.edu>",
41 /* Thread signals and args */
53 static void gst_thread_class_init (GstThreadClass *klass);
54 static void gst_thread_init (GstThread *thread);
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);
59 static GstElementStateReturn gst_thread_change_state (GstElement *element);
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);
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);
69 static void* gst_thread_main_loop (void *arg);
71 static GstBinClass *parent_class = NULL;
72 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
75 gst_thread_get_type(void) {
76 static GtkType thread_type = 0;
79 static const GtkTypeInfo thread_info = {
82 sizeof(GstThreadClass),
83 (GtkClassInitFunc)gst_thread_class_init,
84 (GtkObjectInitFunc)gst_thread_init,
87 (GtkClassInitFunc)NULL,
89 thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
95 gst_thread_class_init (GstThreadClass *klass)
97 GtkObjectClass *gtkobject_class;
98 GstObjectClass *gstobject_class;
99 GstElementClass *gstelement_class;
100 GstBinClass *gstbin_class;
102 gtkobject_class = (GtkObjectClass*)klass;
103 gstobject_class = (GstObjectClass*)klass;
104 gstelement_class = (GstElementClass*)klass;
105 gstbin_class = (GstBinClass*)klass;
107 parent_class = gtk_type_class (GST_TYPE_BIN);
109 gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
110 GTK_ARG_READWRITE, ARG_CREATE_THREAD);
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;
116 gstbin_class->create_plan = gst_thread_create_plan_dummy;
118 gtkobject_class->set_arg = gst_thread_set_arg;
119 gtkobject_class->get_arg = gst_thread_get_arg;
124 gst_thread_init (GstThread *thread)
126 DEBUG("initializing thread '%s'\n",gst_element_get_name(GST_ELEMENT(thread)));
128 // we're a manager by default
129 GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
131 // default is to create a thread
132 GST_FLAG_SET (thread, GST_THREAD_CREATE);
133 GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
135 thread->lock = g_mutex_new();
136 thread->cond = g_cond_new();
140 gst_thread_create_plan_dummy (GstBin *bin)
142 g_return_if_fail (GST_IS_THREAD (bin));
144 if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING))
145 INFO(GST_INFO_THREAD,"gstthread: create plan delayed until thread starts");
149 gst_thread_set_arg (GtkObject *object,
153 /* it's not null if we got it, but it might not be ours */
154 g_return_if_fail (GST_IS_THREAD (object));
157 case ARG_CREATE_THREAD:
158 if (GTK_VALUE_BOOL (*arg)) {
159 INFO(GST_INFO_THREAD,"gstthread: turning ON the creation of the thread");
160 GST_FLAG_SET (object, GST_THREAD_CREATE);
161 DEBUG("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
163 INFO(GST_INFO_THREAD,"gstthread: turning OFF the creation of the thread");
164 GST_FLAG_UNSET (object, GST_THREAD_CREATE);
165 DEBUG("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
174 gst_thread_get_arg (GtkObject *object,
178 /* it's not null if we got it, but it might not be ours */
179 g_return_if_fail (GST_IS_THREAD (object));
182 case ARG_CREATE_THREAD:
183 GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
193 * @name: the name of the thread
195 * Create a new thrad with the given name
197 * Returns: The new thread
200 gst_thread_new (guchar *name)
202 return gst_elementfactory_make ("thread", name);
207 static GstElementStateReturn
208 gst_thread_change_state (GstElement *element)
211 gboolean stateset = GST_STATE_SUCCESS;
212 gint pending, transition;
214 g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
215 DEBUG_ENTER("(\"%s\")",gst_element_get_name(element));
217 thread = GST_THREAD (element);
219 INFO (GST_INFO_THREAD,"gstthread: thread \"%s\" change state %d",
220 gst_element_get_name (GST_ELEMENT (element)),
221 GST_STATE_PENDING (element));
223 pending = GST_STATE_PENDING (element);
224 transition = GST_STATE_TRANSITION (element);
226 // if (pending == GST_STATE (element)) return GST_STATE_SUCCESS;
228 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
230 if (GST_ELEMENT_CLASS (parent_class)->change_state)
231 stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
233 INFO (GST_INFO_THREAD, "gstthread: stateset %d %d %d %02x", GST_STATE (element), stateset,
234 GST_STATE_PENDING (element), GST_STATE_TRANSITION (element));
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 INFO(GST_INFO_THREAD, "gstthread: preparing thread \"%s\" for iterations:",
241 gst_element_get_name (GST_ELEMENT (element)));
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 INFO(GST_INFO_THREAD, "gstthread: flags are 0x%08x", GST_FLAGS (thread));
248 if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
249 INFO(GST_INFO_THREAD, "gstthread: starting thread \"%s\"",
250 gst_element_get_name (GST_ELEMENT (element)));
253 pthread_create (&thread->thread_id, NULL,
254 gst_thread_main_loop, thread);
256 // wait for it to 'spin up'
257 // gst_thread_wait_thread (thread);
259 INFO(GST_INFO_THREAD, "gstthread: NOT starting thread \"%s\"",
260 gst_element_get_name (GST_ELEMENT (element)));
263 case GST_STATE_PAUSED_TO_PLAYING:
264 case GST_STATE_READY_TO_PLAYING:
265 if (!stateset) return FALSE;
266 INFO(GST_INFO_THREAD, "gstthread: starting thread \"%s\"",
267 gst_element_get_name (GST_ELEMENT (element)));
269 GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
270 gst_thread_signal_thread (thread);
272 case GST_STATE_PLAYING_TO_PAUSED:
273 INFO(GST_INFO_THREAD,"gstthread: pausing thread \"%s\"",
274 gst_element_get_name (GST_ELEMENT (element)));
276 //GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
277 gst_thread_signal_thread (thread);
279 case GST_STATE_READY_TO_NULL:
280 INFO(GST_INFO_THREAD,"gstthread: stopping thread \"%s\"",
281 gst_element_get_name (GST_ELEMENT (element)));
283 GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
284 gst_thread_signal_thread (thread);
294 * gst_thread_main_loop:
295 * @arg: the thread to start
297 * The main loop of the thread. The thread will iterate
298 * while the state is GST_THREAD_STATE_SPINNING
301 gst_thread_main_loop (void *arg)
303 GstThread *thread = GST_THREAD (arg);
305 INFO(GST_INFO_THREAD,"gstthread: thread \"%s\" is running with PID %d",
306 gst_element_get_name (GST_ELEMENT (thread)), getpid ());
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);
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));
317 gst_thread_wait_thread (thread);
321 GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
322 // pthread_join (thread->thread_id, 0);
324 INFO(GST_INFO_THREAD, "gstthread: thread \"%s\" is stopped",
325 gst_element_get_name (GST_ELEMENT (thread)));
330 gst_thread_signal_thread (GstThread *thread)
332 DEBUG("signaling thread\n");
333 g_mutex_lock (thread->lock);
334 g_cond_signal (thread->cond);
335 g_mutex_unlock (thread->lock);
339 gst_thread_wait_thread (GstThread *thread)
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);
349 gst_thread_restore_thyself (GstElement *element,
351 GHashTable *elements)
353 DEBUG("gstthread: restore\n");
355 if (GST_ELEMENT_CLASS (parent_class)->restore_thyself)
356 GST_ELEMENT_CLASS (parent_class)->restore_thyself (element,parent, elements);
360 gst_thread_save_thyself (GstElement *element,
363 if (GST_ELEMENT_CLASS (parent_class)->save_thyself)
364 GST_ELEMENT_CLASS (parent_class)->save_thyself (element,parent);