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