2000-12-28 22:12:02 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
*
|
|
|
|
* gstthread.c: Threaded container object
|
2000-01-30 09:03:00 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2000-12-15 01:57:34 +00:00
|
|
|
#include <unistd.h>
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-12-28 22:12:02 +00:00
|
|
|
//#define GST_DEBUG_ENABLED
|
|
|
|
#include "gst_private.h"
|
|
|
|
|
2000-12-15 01:57:34 +00:00
|
|
|
#include "gstthread.h"
|
2001-03-09 18:33:14 +00:00
|
|
|
#include "gstscheduler.h"
|
2001-04-27 20:55:47 +00:00
|
|
|
#include "gstqueue.h"
|
2000-08-18 20:35:48 +00:00
|
|
|
|
2000-01-30 09:03:00 +00:00
|
|
|
GstElementDetails gst_thread_details = {
|
|
|
|
"Threaded container",
|
|
|
|
"Bin",
|
|
|
|
"Container that creates/manages a thread",
|
|
|
|
VERSION,
|
|
|
|
"Erik Walthinsen <omega@cse.ogi.edu>",
|
2001-01-21 16:06:42 +00:00
|
|
|
"(C) 1999, 2000",
|
2000-01-30 09:03:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Thread signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2001-03-12 00:28:30 +00:00
|
|
|
enum {
|
|
|
|
SPINUP=0,
|
|
|
|
STATECHANGE,
|
|
|
|
STARTUP
|
|
|
|
};
|
|
|
|
|
2000-01-30 09:03:00 +00:00
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
ARG_CREATE_THREAD,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-01-21 16:06:42 +00:00
|
|
|
static void gst_thread_class_init (GstThreadClass *klass);
|
|
|
|
static void gst_thread_init (GstThread *thread);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-05-14 21:20:31 +00:00
|
|
|
static void gst_thread_real_destroy (GtkObject *gtk_object);
|
|
|
|
|
2001-01-29 00:06:02 +00:00
|
|
|
static void gst_thread_set_arg (GtkObject *object, GtkArg *arg, guint id);
|
|
|
|
static void gst_thread_get_arg (GtkObject *object, GtkArg *arg, guint id);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-01-21 16:06:42 +00:00
|
|
|
static GstElementStateReturn gst_thread_change_state (GstElement *element);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-01-29 00:06:02 +00:00
|
|
|
static xmlNodePtr gst_thread_save_thyself (GstObject *object, xmlNodePtr parent);
|
|
|
|
static void gst_thread_restore_thyself (GstObject *object, xmlNodePtr self);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-03-30 00:06:43 +00:00
|
|
|
static void gst_thread_signal_thread (GstThread *thread, gboolean spinning);
|
2000-11-04 18:54:07 +00:00
|
|
|
|
|
|
|
static void* gst_thread_main_loop (void *arg);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
WARNING: Don't grab this updated unless you're really, REALLY sure.
Original commit message from CVS:
WARNING: Don't grab this updated unless you're really, REALLY sure.
WARNING: Wait for the next one.
Whole lotta changes here, including a few random bits:
examples/*/Makefile: updated to use `libtool gcc`, not just `gcc`
gst/
gstbuffer.h: updated to new flag style
gst.c, gstdebug.h: added new debugging for function ptrs
gstpipeline.c: set type of parent_class to the class, not the object
gstthread.c: ditto
plugins/
cdparanoia/cdparanoia.c: added an argument type, updated some defaults
cobin/spindentity.c: updated to new do/while loopfunction style
mp3encode/lame/gstlame.c: argument types, whole lotta lame options
tests/: various changes
Now, for the big changes: Once again, the scheduling system has changed.
And once again, it broke a whole bunch of things. The gist of the change
is that there is now a function pointer for gst_pad_push and gst_pad_pull,
instead of a hard-wired function. Well, currently they are functions, but
that's for debugging purposes only, they just call the function pointer
after spewing lots of DEBUG().
This changed the GstPad structure a bit, and the GstPad API as well.
Where elements used to provide chain() and pull() functions, they provide
chain() and get() functions. gst_pad_set_pull[region]_function has been
changed to get_pad_set_get[region]_function. This means all the elements
out there that used to have pull functions need to be updated. The calls
to that function have been changed in the normal elements, but the names
of the functions passed is still _pull[region](), which is an aesthetic
issue more than anything.
As for what doesn't work yet, just about anything dealing with Connections
is hosed, meaning threaded stuff won't work. This will be fixed about 12
hours from now, after I've slept, etc. The simplefake.c test works in
both cothreaded and chained cases, but not much else will work due to the
Connection problem. Needless to say, don't grab this unless you *need*
these features *now*, else wait to update this stuff until tomorrow.
I'm going to sleep now.
2000-12-16 10:18:09 +00:00
|
|
|
static GstBinClass *parent_class = NULL;
|
2000-02-27 23:18:38 +00:00
|
|
|
//static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
2000-09-27 19:33:10 +00:00
|
|
|
thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
return thread_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-01-21 16:06:42 +00:00
|
|
|
gst_thread_class_init (GstThreadClass *klass)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
GtkObjectClass *gtkobject_class;
|
|
|
|
GstObjectClass *gstobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
2000-09-24 14:29:49 +00:00
|
|
|
GstBinClass *gstbin_class;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-01-21 16:06:42 +00:00
|
|
|
gtkobject_class = (GtkObjectClass*)klass;
|
|
|
|
gstobject_class = (GstObjectClass*)klass;
|
|
|
|
gstelement_class = (GstElementClass*)klass;
|
|
|
|
gstbin_class = (GstBinClass*)klass;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-11-04 18:54:07 +00:00
|
|
|
parent_class = gtk_type_class (GST_TYPE_BIN);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-11-04 18:54:07 +00:00
|
|
|
gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
|
|
|
|
GTK_ARG_READWRITE, ARG_CREATE_THREAD);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-05-14 21:20:31 +00:00
|
|
|
gtkobject_class->destroy = gst_thread_real_destroy;
|
|
|
|
|
2001-01-29 00:06:02 +00:00
|
|
|
gstobject_class->save_thyself = gst_thread_save_thyself;
|
|
|
|
gstobject_class->restore_thyself = gst_thread_restore_thyself;
|
|
|
|
|
2001-01-21 16:06:42 +00:00
|
|
|
gstelement_class->change_state = gst_thread_change_state;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-02-12 23:08:07 +00:00
|
|
|
// gstbin_class->schedule = gst_thread_schedule_dummy;
|
2000-09-24 14:29:49 +00:00
|
|
|
|
2000-01-30 09:03:00 +00:00
|
|
|
gtkobject_class->set_arg = gst_thread_set_arg;
|
|
|
|
gtkobject_class->get_arg = gst_thread_get_arg;
|
2000-09-24 22:45:48 +00:00
|
|
|
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2001-01-21 16:06:42 +00:00
|
|
|
static void
|
|
|
|
gst_thread_init (GstThread *thread)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2001-03-13 00:39:35 +00:00
|
|
|
|
2001-05-06 22:12:46 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD,"initializing thread\n");
|
2000-12-26 23:51:04 +00:00
|
|
|
|
2000-12-20 09:39:43 +00:00
|
|
|
// we're a manager by default
|
|
|
|
GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
|
|
|
|
|
|
|
|
// default is to create a thread
|
2001-03-12 00:37:33 +00:00
|
|
|
GST_FLAG_SET (thread, GST_THREAD_CREATE);
|
2001-04-15 23:59:02 +00:00
|
|
|
|
2001-03-13 01:44:44 +00:00
|
|
|
thread->lock = g_mutex_new();
|
2000-01-30 09:03:00 +00:00
|
|
|
thread->cond = g_cond_new();
|
2001-02-12 23:08:07 +00:00
|
|
|
|
2001-03-09 18:33:14 +00:00
|
|
|
GST_ELEMENT_SCHED(thread) = gst_schedule_new(GST_ELEMENT(thread));
|
2001-05-06 22:12:46 +00:00
|
|
|
GST_DEBUG(GST_CAT_THREAD, "thread's scheduler is %p\n",GST_ELEMENT_SCHED(thread));
|
2001-03-09 18:33:14 +00:00
|
|
|
|
2001-02-17 07:11:12 +00:00
|
|
|
// gst_element_set_manager(GST_ELEMENT(thread),GST_ELEMENT(thread));
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2001-05-14 21:20:31 +00:00
|
|
|
static void
|
|
|
|
gst_thread_real_destroy (GtkObject *gtk_object)
|
|
|
|
{
|
|
|
|
GstThread *thread = GST_THREAD (gtk_object);
|
|
|
|
|
|
|
|
GST_DEBUG (GST_CAT_REFCOUNTING,"destroy()\n");
|
|
|
|
|
|
|
|
g_mutex_free (thread->lock);
|
|
|
|
g_cond_free (thread->cond);
|
|
|
|
|
|
|
|
if (GTK_OBJECT_CLASS (parent_class)->destroy)
|
|
|
|
GTK_OBJECT_CLASS (parent_class)->destroy (gtk_object);
|
2001-05-16 22:08:40 +00:00
|
|
|
|
|
|
|
gst_object_destroy (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
|
|
|
|
gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
|
2001-05-14 21:20:31 +00:00
|
|
|
}
|
2001-01-21 16:06:42 +00:00
|
|
|
|
|
|
|
static void
|
2000-11-04 18:54:07 +00:00
|
|
|
gst_thread_set_arg (GtkObject *object,
|
|
|
|
GtkArg *arg,
|
2001-01-21 16:06:42 +00:00
|
|
|
guint id)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
/* it's not null if we got it, but it might not be ours */
|
2000-11-04 18:54:07 +00:00
|
|
|
g_return_if_fail (GST_IS_THREAD (object));
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
switch(id) {
|
|
|
|
case ARG_CREATE_THREAD:
|
2000-11-04 18:54:07 +00:00
|
|
|
if (GTK_VALUE_BOOL (*arg)) {
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD,"turning ON the creation of the thread");
|
2000-11-04 18:54:07 +00:00
|
|
|
GST_FLAG_SET (object, GST_THREAD_CREATE);
|
2001-04-28 02:54:53 +00:00
|
|
|
// GST_DEBUG (GST_CAT_THREAD,"flags are 0x%08x\n", GST_FLAGS (object));
|
2000-01-30 09:03:00 +00:00
|
|
|
} else {
|
2001-01-01 03:14:40 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD,"gstthread: turning OFF the creation of the thread");
|
2000-11-04 18:54:07 +00:00
|
|
|
GST_FLAG_UNSET (object, GST_THREAD_CREATE);
|
2001-04-28 02:54:53 +00:00
|
|
|
// GST_DEBUG (GST_CAT_THREAD,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-21 16:06:42 +00:00
|
|
|
static void
|
2000-11-04 18:54:07 +00:00
|
|
|
gst_thread_get_arg (GtkObject *object,
|
|
|
|
GtkArg *arg,
|
2001-01-21 16:06:42 +00:00
|
|
|
guint id)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
/* it's not null if we got it, but it might not be ours */
|
2000-11-04 18:54:07 +00:00
|
|
|
g_return_if_fail (GST_IS_THREAD (object));
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
switch (id) {
|
2000-01-30 09:03:00 +00:00
|
|
|
case ARG_CREATE_THREAD:
|
2000-11-04 18:54:07 +00:00
|
|
|
GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
|
2000-01-30 09:03:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-17 17:14:15 +00:00
|
|
|
|
2000-03-27 19:53:43 +00:00
|
|
|
/**
|
|
|
|
* gst_thread_new:
|
|
|
|
* @name: the name of the thread
|
|
|
|
*
|
2001-01-06 22:05:15 +00:00
|
|
|
* Create a new thread with the given name.
|
2000-03-27 19:53:43 +00:00
|
|
|
*
|
2000-12-28 21:42:23 +00:00
|
|
|
* Returns: The new thread
|
2000-03-27 19:53:43 +00:00
|
|
|
*/
|
2000-11-04 18:54:07 +00:00
|
|
|
GstElement*
|
2001-05-12 18:04:20 +00:00
|
|
|
gst_thread_new (const guchar *name)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2000-12-26 20:49:46 +00:00
|
|
|
return gst_elementfactory_make ("thread", name);
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-21 16:06:42 +00:00
|
|
|
static GstElementStateReturn
|
|
|
|
gst_thread_change_state (GstElement *element)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
GstThread *thread;
|
2000-11-01 22:11:48 +00:00
|
|
|
gboolean stateset = GST_STATE_SUCCESS;
|
2001-03-09 18:33:14 +00:00
|
|
|
gint transition;
|
2001-04-28 01:46:00 +00:00
|
|
|
pthread_t self = pthread_self();
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-11-04 18:54:07 +00:00
|
|
|
g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
|
2001-01-29 00:06:02 +00:00
|
|
|
GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME(element));
|
2000-12-08 23:38:12 +00:00
|
|
|
|
2000-11-04 18:54:07 +00:00
|
|
|
thread = GST_THREAD (element);
|
2001-05-10 08:16:36 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "**** THREAD %ld changing THREAD %ld ****\n",self,thread->thread_id);
|
2001-04-28 01:46:00 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "**** current pid=%d\n",getpid());
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-03-09 18:33:14 +00:00
|
|
|
transition = GST_STATE_TRANSITION (element);
|
2000-08-14 10:55:35 +00:00
|
|
|
|
2001-03-13 02:20:03 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD,"thread \"%s\" changing state to %s from %s",
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_ELEMENT_NAME (GST_ELEMENT (element)),
|
2001-03-13 02:20:03 +00:00
|
|
|
_gst_print_statename(GST_STATE_PENDING (element)),
|
2001-03-16 00:49:05 +00:00
|
|
|
_gst_print_statename(GST_STATE (element)));
|
2000-09-22 23:35:14 +00:00
|
|
|
|
2001-03-12 00:28:30 +00:00
|
|
|
//GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
|
2000-11-01 13:49:41 +00:00
|
|
|
|
2001-03-09 18:33:14 +00:00
|
|
|
switch (transition) {
|
2000-12-23 03:17:52 +00:00
|
|
|
case GST_STATE_NULL_TO_READY:
|
2000-07-17 17:14:15 +00:00
|
|
|
// we want to prepare our internal state for doing the iterations
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD, "preparing thread \"%s\" for iterations:",
|
2001-01-29 00:06:02 +00:00
|
|
|
GST_ELEMENT_NAME (GST_ELEMENT (element)));
|
2000-12-28 22:12:02 +00:00
|
|
|
|
2000-07-17 17:14:15 +00:00
|
|
|
// set the state to idle
|
2000-11-04 18:54:07 +00:00
|
|
|
GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
|
2000-07-17 17:14:15 +00:00
|
|
|
// create the thread if that's what we're supposed to do
|
2001-02-12 23:08:07 +00:00
|
|
|
// GST_INFO (GST_CAT_THREAD, "flags are 0x%08x", GST_FLAGS (thread));
|
2000-12-28 22:12:02 +00:00
|
|
|
|
2000-11-04 18:54:07 +00:00
|
|
|
if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
|
2001-03-09 18:33:14 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "creating thread \"%s\"\n",
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_ELEMENT_NAME (GST_ELEMENT (element)));
|
2000-12-28 22:12:02 +00:00
|
|
|
|
2001-04-27 20:55:47 +00:00
|
|
|
g_mutex_lock(thread->lock);
|
|
|
|
|
2000-12-28 22:12:02 +00:00
|
|
|
// create the thread
|
2000-11-04 18:54:07 +00:00
|
|
|
pthread_create (&thread->thread_id, NULL,
|
|
|
|
gst_thread_main_loop, thread);
|
2000-12-28 22:12:02 +00:00
|
|
|
|
|
|
|
// wait for it to 'spin up'
|
2001-03-09 18:33:14 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: waiting for spinup\n");
|
2001-03-30 00:06:43 +00:00
|
|
|
g_cond_wait(thread->cond,thread->lock);
|
2001-03-09 18:33:14 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: thread claims to be up\n");
|
2001-04-27 20:55:47 +00:00
|
|
|
g_mutex_unlock(thread->lock);
|
2000-01-30 09:03:00 +00:00
|
|
|
} else {
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD, "NOT starting thread \"%s\"",
|
2001-01-29 00:06:02 +00:00
|
|
|
GST_ELEMENT_NAME (GST_ELEMENT (element)));
|
2001-02-12 23:08:07 +00:00
|
|
|
|
|
|
|
// punt and change state on all the children
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-12-23 03:17:52 +00:00
|
|
|
case GST_STATE_PAUSED_TO_PLAYING:
|
|
|
|
case GST_STATE_READY_TO_PLAYING:
|
2001-03-09 18:33:14 +00:00
|
|
|
// if (!stateset) return FALSE;
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD, "starting thread \"%s\"",
|
2001-01-29 00:06:02 +00:00
|
|
|
GST_ELEMENT_NAME (GST_ELEMENT (element)));
|
2000-12-28 22:12:02 +00:00
|
|
|
|
2001-04-28 01:46:00 +00:00
|
|
|
if (pthread_equal(self, thread->thread_id))
|
|
|
|
{
|
|
|
|
//FIXME this should not happen
|
|
|
|
g_assert(!pthread_equal(self, thread->thread_id));
|
|
|
|
GST_FLAG_SET(thread, GST_THREAD_STATE_SPINNING);
|
|
|
|
GST_DEBUG(GST_CAT_THREAD,"no sync: setting own thread's state to spinning\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GST_DEBUG(GST_CAT_THREAD,"sync: telling thread to start spinning\n");
|
|
|
|
g_mutex_lock(thread->lock);
|
|
|
|
gst_thread_signal_thread(thread,TRUE);
|
|
|
|
}
|
2001-01-21 16:06:42 +00:00
|
|
|
break;
|
2000-12-23 03:17:52 +00:00
|
|
|
case GST_STATE_PLAYING_TO_PAUSED:
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD,"pausing thread \"%s\"",
|
2001-01-29 00:06:02 +00:00
|
|
|
GST_ELEMENT_NAME (GST_ELEMENT (element)));
|
2001-01-21 16:06:42 +00:00
|
|
|
|
2001-04-28 01:46:00 +00:00
|
|
|
|
|
|
|
if (pthread_equal(self, thread->thread_id))
|
2001-04-27 20:55:47 +00:00
|
|
|
{
|
2001-04-28 01:46:00 +00:00
|
|
|
//FIXME this should not happen
|
|
|
|
g_assert(!pthread_equal(self, thread->thread_id));
|
|
|
|
GST_DEBUG(GST_CAT_THREAD,"no sync: setting own thread's state to paused\n");
|
|
|
|
GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
|
|
|
|
}
|
|
|
|
else
|
2001-04-27 20:55:47 +00:00
|
|
|
{
|
2001-04-28 01:46:00 +00:00
|
|
|
GList *elements = (element->sched)->elements;
|
|
|
|
|
|
|
|
// the following code ensures that the bottom half of thread will run
|
|
|
|
// to perform each elements' change_state() (by calling gstbin.c::
|
|
|
|
// change_state()).
|
|
|
|
// + the pending state was already set by gstelement.c::set_state()
|
|
|
|
// + find every queue we manage, and signal its empty and full conditions
|
|
|
|
g_mutex_lock(thread->lock);
|
|
|
|
while (elements)
|
2001-04-27 20:55:47 +00:00
|
|
|
{
|
2001-04-28 01:46:00 +00:00
|
|
|
GstElement *e = GST_ELEMENT(elements->data);
|
|
|
|
g_assert(e);
|
|
|
|
GST_DEBUG(GST_CAT_THREAD," element %s\n",GST_ELEMENT_NAME(e));
|
|
|
|
elements = g_list_next(elements);
|
|
|
|
if (GST_IS_QUEUE(e))
|
|
|
|
{
|
|
|
|
//FIXME make this more efficient by only waking queues that are asleep
|
|
|
|
//FIXME and only waking the appropriate condition (depending on if it's
|
|
|
|
//FIXME on up- or down-stream side)
|
|
|
|
//
|
|
|
|
//FIXME also make this more efficient by keeping list of managed queues
|
|
|
|
GST_DEBUG(GST_CAT_THREAD,"sync: waking queue \"%s\"\n",
|
|
|
|
GST_ELEMENT_NAME(e));
|
|
|
|
g_cond_signal((GST_QUEUE(e)->emptycond));
|
|
|
|
g_cond_signal((GST_QUEUE(e)->fullcond));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GList *pads = GST_ELEMENT_PADS(e);
|
|
|
|
while (pads)
|
|
|
|
{
|
|
|
|
GstPad *p = GST_PAD(pads->data);
|
|
|
|
pads = g_list_next(pads);
|
|
|
|
if (GST_IS_REAL_PAD(p) &&
|
|
|
|
GST_ELEMENT_SCHED(e) != GST_ELEMENT_SCHED(GST_ELEMENT(GST_PAD_PARENT(GST_PAD_PEER(p)))))
|
|
|
|
{
|
|
|
|
GST_DEBUG(GST_CAT_THREAD," element \"%s\" has pad cross sched boundary\n",GST_ELEMENT_NAME(e));
|
|
|
|
// FIXME i assume this signals our own (current) thread so don't need to lock
|
|
|
|
// FIXME however, this *may* go to yet another thread for which we need locks
|
|
|
|
// FIXME i'm too tired to deal with this now
|
|
|
|
g_cond_signal(GST_QUEUE(GST_ELEMENT(GST_PAD_PARENT(GST_PAD_PEER(p))))->emptycond);
|
|
|
|
g_cond_signal(GST_QUEUE(GST_ELEMENT(GST_PAD_PARENT(GST_PAD_PEER(p))))->fullcond);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-04-27 20:55:47 +00:00
|
|
|
}
|
2001-04-28 01:46:00 +00:00
|
|
|
GST_DEBUG(GST_CAT_THREAD,"sync: telling thread to pause\n");
|
|
|
|
gst_thread_signal_thread(thread,FALSE);
|
2001-04-27 20:55:47 +00:00
|
|
|
}
|
2001-03-16 00:49:05 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_PLAYING_TO_READY:
|
2001-04-28 01:46:00 +00:00
|
|
|
if (pthread_equal(self, thread->thread_id))
|
|
|
|
{
|
|
|
|
//FIXME this should not happen
|
|
|
|
g_assert(!pthread_equal(self, thread->thread_id));
|
|
|
|
GST_DEBUG(GST_CAT_THREAD,"no sync: setting own thread's state to ready (paused)\n");
|
|
|
|
GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GST_DEBUG(GST_CAT_THREAD,"sync: telling thread to pause (ready)\n");
|
|
|
|
g_mutex_lock(thread->lock);
|
|
|
|
gst_thread_signal_thread(thread,FALSE);
|
|
|
|
}
|
2000-01-30 09:03:00 +00:00
|
|
|
break;
|
2000-12-23 03:17:52 +00:00
|
|
|
case GST_STATE_READY_TO_NULL:
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD,"stopping thread \"%s\"",
|
2001-01-29 00:06:02 +00:00
|
|
|
GST_ELEMENT_NAME (GST_ELEMENT (element)));
|
2001-01-21 16:06:42 +00:00
|
|
|
|
2001-03-30 00:06:43 +00:00
|
|
|
GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
|
2001-04-28 01:46:00 +00:00
|
|
|
if (pthread_equal(self, thread->thread_id))
|
|
|
|
{
|
|
|
|
//FIXME this should not happen
|
|
|
|
g_assert(!pthread_equal(self, thread->thread_id));
|
|
|
|
GST_DEBUG(GST_CAT_THREAD,"no sync: setting own thread's state to NULL (paused)\n");
|
|
|
|
GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GST_DEBUG(GST_CAT_THREAD,"sync: telling thread to pause (null) - and joining\n");
|
|
|
|
//MattH FIXME revisit
|
|
|
|
// g_mutex_lock(thread->lock);
|
|
|
|
// gst_thread_signal_thread(thread,FALSE);
|
|
|
|
pthread_join(thread->thread_id,NULL);
|
|
|
|
}
|
2001-03-12 00:28:30 +00:00
|
|
|
|
|
|
|
GST_FLAG_UNSET(thread,GST_THREAD_STATE_REAPING);
|
|
|
|
GST_FLAG_UNSET(thread,GST_THREAD_STATE_STARTED);
|
|
|
|
GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
|
|
|
|
GST_FLAG_UNSET(thread,GST_THREAD_STATE_ELEMENT_CHANGED);
|
|
|
|
|
2001-03-13 02:20:03 +00:00
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
2001-03-30 00:06:43 +00:00
|
|
|
stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
|
2001-03-13 02:20:03 +00:00
|
|
|
|
2000-09-09 16:36:10 +00:00
|
|
|
break;
|
2000-01-30 09:03:00 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateset;
|
|
|
|
}
|
|
|
|
|
2001-03-30 00:06:43 +00:00
|
|
|
static void gst_thread_update_state (GstThread *thread)
|
|
|
|
{
|
|
|
|
// check for state change
|
|
|
|
if (GST_STATE_PENDING(thread) != GST_STATE_NONE_PENDING) {
|
|
|
|
// punt and change state on all the children
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-27 19:53:43 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2000-11-01 22:11:48 +00:00
|
|
|
static void *
|
2001-01-21 16:06:42 +00:00
|
|
|
gst_thread_main_loop (void *arg)
|
2000-11-01 22:11:48 +00:00
|
|
|
{
|
2000-11-04 18:54:07 +00:00
|
|
|
GstThread *thread = GST_THREAD (arg);
|
2001-02-12 23:08:07 +00:00
|
|
|
gint stateset;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD,"thread \"%s\" is running with PID %d",
|
2001-01-29 00:06:02 +00:00
|
|
|
GST_ELEMENT_NAME (GST_ELEMENT (thread)), getpid ());
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-02-12 23:08:07 +00:00
|
|
|
// first we need to change the state of all the children
|
|
|
|
GST_DEBUG (GST_CAT_THREAD,"thread started, setting children's state\n");
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
|
|
|
|
|
2000-12-28 22:12:02 +00:00
|
|
|
// construct the plan and signal back
|
2001-02-12 23:08:07 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD,"creating plan for thread\n");
|
2001-01-21 16:06:42 +00:00
|
|
|
if (GST_BIN_CLASS (parent_class)->schedule)
|
|
|
|
GST_BIN_CLASS (parent_class)->schedule (GST_BIN (thread));
|
|
|
|
|
2001-03-30 00:06:43 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: indicating spinup\n");
|
|
|
|
g_mutex_lock (thread->lock);
|
|
|
|
g_cond_signal (thread->cond);
|
|
|
|
// don't unlock the mutex because we hold it into the top of the while loop
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: thread has indicated spinup to main process\n");
|
2001-02-12 23:08:07 +00:00
|
|
|
|
2000-11-04 18:54:07 +00:00
|
|
|
while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
|
2001-03-30 00:06:43 +00:00
|
|
|
// start out by waiting for a state change into spinning
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: waiting at top of while for signal from main process\n");
|
|
|
|
g_cond_wait (thread->cond,thread->lock);
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: main process has signaled at top of while\n");
|
|
|
|
// now is a good time to change the state of the children and the thread itself
|
|
|
|
gst_thread_update_state (thread);
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: done changing state, signaling back\n");
|
|
|
|
g_cond_signal (thread->cond);
|
|
|
|
g_mutex_unlock (thread->lock);
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: done syncing with main thread at top of while\n");
|
|
|
|
|
|
|
|
while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
|
2001-01-20 17:59:25 +00:00
|
|
|
if (!gst_bin_iterate (GST_BIN (thread))) {
|
|
|
|
GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
|
2001-04-28 02:54:53 +00:00
|
|
|
GST_DEBUG(GST_CAT_THREAD,"sync: removed spinning state due to failed iteration\n");
|
2001-01-20 17:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
2001-03-30 00:06:43 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: waiting at bottom of while for signal from main process\n");
|
|
|
|
g_mutex_lock (thread->lock);
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: signaling that the thread is out of the SPINNING loop\n");
|
|
|
|
g_cond_signal (thread->cond);
|
|
|
|
g_cond_wait (thread->cond, thread->lock);
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: main process has signaled at bottom of while\n");
|
|
|
|
// now change the children's and thread's state
|
|
|
|
gst_thread_update_state (thread);
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: done changing state, signaling back\n");
|
|
|
|
g_cond_signal (thread->cond);
|
|
|
|
// don't release the mutex, we hold that into the top of the loop
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync: done syncning with main thread at bottom of while\n");
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2001-03-30 00:06:43 +00:00
|
|
|
// since we don't unlock at the end of the while loop, do it here
|
|
|
|
g_mutex_unlock (thread->lock);
|
|
|
|
|
2001-01-01 03:14:40 +00:00
|
|
|
GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
|
2001-01-29 00:06:02 +00:00
|
|
|
GST_ELEMENT_NAME (thread));
|
2000-01-30 09:03:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-03-12 02:44:17 +00:00
|
|
|
// the set flag is to say whether it should set TRUE or FALSE
|
2001-04-27 20:55:47 +00:00
|
|
|
//
|
|
|
|
// WARNING: this has synchronization built in! if you remove or add any
|
|
|
|
// locks, waits, signals, or unlocks you need to be sure they match the
|
|
|
|
// code above (in gst_thread_main_loop()). basically, don't change anything.
|
2001-01-20 17:59:25 +00:00
|
|
|
static void
|
2001-03-30 00:06:43 +00:00
|
|
|
gst_thread_signal_thread (GstThread *thread, gboolean spinning)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2001-03-30 00:06:43 +00:00
|
|
|
// set the spinning state
|
|
|
|
if (spinning) GST_FLAG_SET(thread,GST_THREAD_STATE_SPINNING);
|
2001-04-15 23:59:02 +00:00
|
|
|
else GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-04-28 01:46:00 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync-main: thread locked\n");
|
|
|
|
// g_mutex_lock(thread->lock);
|
2000-12-28 22:12:02 +00:00
|
|
|
|
2001-03-30 00:06:43 +00:00
|
|
|
if (!spinning) {
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync-main: waiting for spindown\n");
|
|
|
|
g_cond_wait (thread->cond, thread->lock);
|
2001-03-13 01:44:44 +00:00
|
|
|
}
|
2001-03-30 00:06:43 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync-main: signaling\n");
|
|
|
|
g_cond_signal (thread->cond);
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync-main: waiting for ack\n");
|
|
|
|
g_cond_wait (thread->cond,thread->lock);
|
|
|
|
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync-main: unlocking\n");
|
|
|
|
g_mutex_unlock(thread->lock);
|
|
|
|
GST_DEBUG (GST_CAT_THREAD, "sync-main: unlocked\n");
|
2001-03-13 01:44:44 +00:00
|
|
|
}
|
|
|
|
|
2000-12-28 22:12:02 +00:00
|
|
|
|
2001-01-20 17:59:25 +00:00
|
|
|
static void
|
2001-01-29 00:06:02 +00:00
|
|
|
gst_thread_restore_thyself (GstObject *object,
|
|
|
|
xmlNodePtr self)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2001-04-28 02:54:53 +00:00
|
|
|
GST_DEBUG (GST_CAT_THREAD,"gstthread: restore\n");
|
2000-09-27 19:33:10 +00:00
|
|
|
|
2001-01-29 00:06:02 +00:00
|
|
|
if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
|
|
|
|
GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
|
2000-09-27 19:33:10 +00:00
|
|
|
}
|
|
|
|
|
2001-01-20 17:59:25 +00:00
|
|
|
static xmlNodePtr
|
2001-01-29 00:06:02 +00:00
|
|
|
gst_thread_save_thyself (GstObject *object,
|
|
|
|
xmlNodePtr self)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2001-01-29 00:06:02 +00:00
|
|
|
if (GST_OBJECT_CLASS (parent_class)->save_thyself)
|
|
|
|
GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
|
2000-09-27 19:33:10 +00:00
|
|
|
return NULL;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|