summaryrefslogtreecommitdiff
path: root/gst/gstthread.c
blob: 04e695c603c55ecccd2c800cc5d3a37f1782db31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstthread.c: Threaded container object
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <unistd.h>

//#define GST_DEBUG_ENABLED
#include "gst_private.h"

#include "gstthread.h"


GstElementDetails gst_thread_details = {
  "Threaded container",
  "Bin",
  "Container that creates/manages a thread",
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>",
  "(C) 1999, 2000",
};


/* Thread signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_CREATE_THREAD,
};


static void			gst_thread_class_init		(GstThreadClass *klass);
static void			gst_thread_init			(GstThread *thread);

static void			gst_thread_set_arg		(GtkObject *object, GtkArg *arg, guint id);
static void			gst_thread_get_arg		(GtkObject *object, GtkArg *arg, guint id);

static GstElementStateReturn	gst_thread_change_state		(GstElement *element);

static xmlNodePtr		gst_thread_save_thyself		(GstObject *object, xmlNodePtr parent);
static void			gst_thread_restore_thyself	(GstObject *object, xmlNodePtr self);

static void			gst_thread_signal_thread	(GstThread *thread);
static void			gst_thread_wait_thread		(GstThread *thread);
static void			gst_thread_schedule_dummy	(GstBin *bin);

static void*			gst_thread_main_loop		(void *arg);

static GstBinClass *parent_class = NULL;
//static guint gst_thread_signals[LAST_SIGNAL] = { 0 };

GtkType
gst_thread_get_type(void) {
  static GtkType thread_type = 0;

  if (!thread_type) {
    static const GtkTypeInfo thread_info = {
      "GstThread",
      sizeof(GstThread),
      sizeof(GstThreadClass),
      (GtkClassInitFunc)gst_thread_class_init,
      (GtkObjectInitFunc)gst_thread_init,
      (GtkArgSetFunc)NULL,
      (GtkArgGetFunc)NULL,
      (GtkClassInitFunc)NULL,
    };
    thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
  }
  return thread_type;
}

static void
gst_thread_class_init (GstThreadClass *klass)
{
  GtkObjectClass *gtkobject_class;
  GstObjectClass *gstobject_class;
  GstElementClass *gstelement_class;
  GstBinClass *gstbin_class;

  gtkobject_class =	(GtkObjectClass*)klass;
  gstobject_class =	(GstObjectClass*)klass;
  gstelement_class =	(GstElementClass*)klass;
  gstbin_class =	(GstBinClass*)klass;

  parent_class = gtk_type_class (GST_TYPE_BIN);

  gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
                           GTK_ARG_READWRITE, ARG_CREATE_THREAD);

  gstobject_class->save_thyself =	gst_thread_save_thyself;
  gstobject_class->restore_thyself =	gst_thread_restore_thyself;

  gstelement_class->change_state =	gst_thread_change_state;

  gstbin_class->schedule = gst_thread_schedule_dummy;

  gtkobject_class->set_arg = gst_thread_set_arg;
  gtkobject_class->get_arg = gst_thread_get_arg;

}

static void
gst_thread_init (GstThread *thread)
{
  GST_DEBUG (0,"initializing thread '%s'\n",GST_ELEMENT_NAME (thread));

  // we're a manager by default
  GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);

  // default is to create a thread
  GST_FLAG_SET (thread, GST_THREAD_CREATE);
  GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);

  thread->lock = g_mutex_new();
  thread->cond = g_cond_new();
}

static void
gst_thread_schedule_dummy (GstBin *bin)
{
  g_return_if_fail (GST_IS_THREAD (bin));

  if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING))
    GST_INFO (GST_CAT_THREAD,"gstthread: scheduling delayed until thread starts");
}

static void
gst_thread_set_arg (GtkObject *object,
		    GtkArg *arg,
		    guint id)
{
  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_THREAD (object));

  switch(id) {
    case ARG_CREATE_THREAD:
      if (GTK_VALUE_BOOL (*arg)) {
        GST_INFO (GST_CAT_THREAD,"gstthread: turning ON the creation of the thread");
        GST_FLAG_SET (object, GST_THREAD_CREATE);
        GST_DEBUG (0,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
      } else {
        GST_INFO (GST_CAT_THREAD,"gstthread: turning OFF the creation of the thread");
        GST_FLAG_UNSET (object, GST_THREAD_CREATE);
        GST_DEBUG (0,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
      }
      break;
    default:
      break;
  }
}

static void
gst_thread_get_arg (GtkObject *object,
		    GtkArg *arg,
		    guint id)
{
  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_THREAD (object));

  switch (id) {
    case ARG_CREATE_THREAD:
      GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
      break;
    default:
      break;
  }
}


/**
 * gst_thread_new:
 * @name: the name of the thread
 *
 * Create a new thread with the given name.
 *
 * Returns: The new thread
 */
GstElement*
gst_thread_new (guchar *name)
{
  return gst_elementfactory_make ("thread", name);
}



static GstElementStateReturn
gst_thread_change_state (GstElement *element)
{
  GstThread *thread;
  gboolean stateset = GST_STATE_SUCCESS;
  gint pending, transition;

  g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
  GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME(element));

  thread = GST_THREAD (element);

  GST_INFO (GST_CAT_THREAD,"gstthread: thread \"%s\" change state %d",
               GST_ELEMENT_NAME (GST_ELEMENT (element)),
	       GST_STATE_PENDING (element));

  pending = GST_STATE_PENDING (element);
  transition = GST_STATE_TRANSITION (element);

//  if (pending == GST_STATE (element)) return GST_STATE_SUCCESS;

  GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);

  if (GST_ELEMENT_CLASS (parent_class)->change_state)
    stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);

  GST_INFO (GST_CAT_THREAD, "gstthread: stateset %d %d %d %02x", GST_STATE (element), stateset,
		  GST_STATE_PENDING (element), GST_STATE_TRANSITION (element));

  switch (transition) {
    case GST_STATE_NULL_TO_READY:
//      if (!stateset) return FALSE;
      // we want to prepare our internal state for doing the iterations
      GST_INFO (GST_CAT_THREAD, "gstthread: preparing thread \"%s\" for iterations:",
               GST_ELEMENT_NAME (GST_ELEMENT (element)));

      // set the state to idle
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
      // create the thread if that's what we're supposed to do
      GST_INFO (GST_CAT_THREAD, "gstthread: flags are 0x%08x", GST_FLAGS (thread));

      if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
        GST_INFO (GST_CAT_THREAD, "gstthread: starting thread \"%s\"",
                 GST_ELEMENT_NAME (GST_ELEMENT (element)));

        // create the thread
        pthread_create (&thread->thread_id, NULL,
                        gst_thread_main_loop, thread);

        // wait for it to 'spin up'
        //gst_thread_wait_thread (thread);
      } else {
        GST_INFO (GST_CAT_THREAD, "gstthread: NOT starting thread \"%s\"",
                GST_ELEMENT_NAME (GST_ELEMENT (element)));
      }
      break;
    case GST_STATE_PAUSED_TO_PLAYING:
    case GST_STATE_READY_TO_PLAYING:
      if (!stateset) return FALSE;
      GST_INFO (GST_CAT_THREAD, "gstthread: starting thread \"%s\"",
              GST_ELEMENT_NAME (GST_ELEMENT (element)));

      GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
      gst_thread_signal_thread (thread);
      break;
    case GST_STATE_PLAYING_TO_PAUSED:
      GST_INFO (GST_CAT_THREAD,"gstthread: pausing thread \"%s\"",
              GST_ELEMENT_NAME (GST_ELEMENT (element)));

      //GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
      gst_thread_signal_thread (thread);
      break;
    case GST_STATE_READY_TO_NULL:
      GST_INFO (GST_CAT_THREAD,"gstthread: stopping thread \"%s\"",
              GST_ELEMENT_NAME (GST_ELEMENT (element)));

      GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
      gst_thread_signal_thread (thread);
      break;
    default:
      break;
  }

  return stateset;
}

/**
 * gst_thread_main_loop:
 * @arg: the thread to start
 *
 * The main loop of the thread. The thread will iterate
 * while the state is GST_THREAD_STATE_SPINNING
 */
static void *
gst_thread_main_loop (void *arg)
{
  GstThread *thread = GST_THREAD (arg);

  GST_INFO (GST_CAT_THREAD,"gstthread: thread \"%s\" is running with PID %d",
		  GST_ELEMENT_NAME (GST_ELEMENT (thread)), getpid ());

  // construct the plan and signal back
  if (GST_BIN_CLASS (parent_class)->schedule)
    GST_BIN_CLASS (parent_class)->schedule (GST_BIN (thread));

  gst_thread_signal_thread (thread);

  while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
    if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
      if (!gst_bin_iterate (GST_BIN (thread))) {
	GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
      }
    }
    else {
      GST_DEBUG (0, "thread \"%s\" waiting\n", GST_ELEMENT_NAME (GST_ELEMENT (thread)));
      gst_thread_wait_thread (thread);
    }
  }

  GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
  //pthread_join (thread->thread_id, 0);

  GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
		  GST_ELEMENT_NAME (thread));
  return NULL;
}

static void
gst_thread_signal_thread (GstThread *thread)
{
  GST_DEBUG (0,"signaling thread\n");
  g_mutex_lock (thread->lock);
  g_cond_signal (thread->cond);
  g_mutex_unlock (thread->lock);
}

static void
gst_thread_wait_thread (GstThread *thread)
{
  GST_DEBUG (0,"waiting for thread\n");
  g_mutex_lock (thread->lock);
  g_cond_wait (thread->cond, thread->lock);
  g_mutex_unlock (thread->lock);
}


static void
gst_thread_restore_thyself (GstObject *object,
		            xmlNodePtr self)
{
  GST_DEBUG (0,"gstthread: restore\n");

  if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
    GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
}

static xmlNodePtr
gst_thread_save_thyself (GstObject *object,
		         xmlNodePtr self)
{
  if (GST_OBJECT_CLASS (parent_class)->save_thyself)
    GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
  return NULL;
}