? core.8934
? foo.dv
? check/elements/gstfakesrc
? check/gst/gstevents
? check/states/sinks2
? check/states/sinks2.c
? docs/gst/tmpl/gst.sgml
? docs/gst/tmpl/gstadapter.sgml
? docs/gst/tmpl/gstbasesink.sgml
? docs/gst/tmpl/gstbasesrc.sgml
? docs/gst/tmpl/gstbasetransform.sgml
? docs/gst/tmpl/gstbin.sgml
? docs/gst/tmpl/gstbuffer.sgml
? docs/gst/tmpl/gstbus.sgml
? docs/gst/tmpl/gstcaps.sgml
? docs/gst/tmpl/gstcheck.sgml
? docs/gst/tmpl/gstchildproxy.sgml
? docs/gst/tmpl/gstclock.sgml
? docs/gst/tmpl/gstcollectpads.sgml
? docs/gst/tmpl/gstcompat.sgml
? docs/gst/tmpl/gstconfig.sgml
? docs/gst/tmpl/gstelement.sgml
? docs/gst/tmpl/gstelementfactory.sgml
? docs/gst/tmpl/gsterror.sgml
? docs/gst/tmpl/gstevent.sgml
? docs/gst/tmpl/gstfakesink.sgml
? docs/gst/tmpl/gstfakesrc.sgml
? docs/gst/tmpl/gstfilesink.sgml
? docs/gst/tmpl/gstfilesrc.sgml
? docs/gst/tmpl/gstfilter.sgml
? docs/gst/tmpl/gstformat.sgml
? docs/gst/tmpl/gstghostpad.sgml
? docs/gst/tmpl/gstimplementsinterface.sgml
? docs/gst/tmpl/gstindex.sgml
? docs/gst/tmpl/gstindexfactory.sgml
? docs/gst/tmpl/gstinfo.sgml
? docs/gst/tmpl/gstiterator.sgml
? docs/gst/tmpl/gstmacros.sgml
? docs/gst/tmpl/gstmemchunk.sgml
? docs/gst/tmpl/gstmessage.sgml
? docs/gst/tmpl/gstminiobject.sgml
? docs/gst/tmpl/gstobject.sgml
? docs/gst/tmpl/gstpad.sgml
? docs/gst/tmpl/gstpadtemplate.sgml
? docs/gst/tmpl/gstparse.sgml
? docs/gst/tmpl/gstpipeline.sgml
? docs/gst/tmpl/gstplugin.sgml
? docs/gst/tmpl/gstpluginfeature.sgml
? docs/gst/tmpl/gstprobe.sgml
? docs/gst/tmpl/gstpushsrc.sgml
? docs/gst/tmpl/gstquery.sgml
? docs/gst/tmpl/gstqueue.sgml
? docs/gst/tmpl/gstreamer-unused.sgml
? docs/gst/tmpl/gstregistry.sgml
? docs/gst/tmpl/gstregistrypool.sgml
? docs/gst/tmpl/gststructure.sgml
? docs/gst/tmpl/gstsystemclock.sgml
? docs/gst/tmpl/gsttaglist.sgml
? docs/gst/tmpl/gsttagsetter.sgml
? docs/gst/tmpl/gsttask.sgml
? docs/gst/tmpl/gsttrace.sgml
? docs/gst/tmpl/gsttrashstack.sgml
? docs/gst/tmpl/gsttypefind.sgml
? docs/gst/tmpl/gsttypefindfactory.sgml
? docs/gst/tmpl/gsturihandler.sgml
? docs/gst/tmpl/gsturitype.sgml
? docs/gst/tmpl/gstutils.sgml
? docs/gst/tmpl/gstvalue.sgml
? docs/gst/tmpl/gstversion.sgml
? docs/gst/tmpl/gstxml.sgml
? docs/gst/tmpl/gstxmlregistry.sgml
? docs/plugins/gstreamer-plugins.types
? docs/random/wingo/gst-newsletter-05-10-2005
? docs/random/wingo/gst-newsletter-20-9-2005
? docs/random/wingo/gst-newsletter-28-9-2005
? tests/memchunk/gmemchunktest
? tests/memchunk/gstmemchunktest
? tests/memchunk/memchunk.c
? tests/memchunk/memchunk.h
Index: check/Makefile.am
===================================================================
RCS file: /cvs/gstreamer/gstreamer/check/Makefile.am,v
retrieving revision 1.63
diff -u -u -r1.63 Makefile.am
--- check/Makefile.am	22 Sep 2005 12:05:04 -0000	1.63
+++ check/Makefile.am	10 Oct 2005 14:07:23 -0000
@@ -54,6 +54,7 @@
 	pipelines/simple_launch_lines		\
 	pipelines/cleanup			\
 	states/sinks				\
+	states/sinks2				\
 	gst-libs/controller			\
 	gst-libs/gdp
 
Index: check/generic/states.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/check/generic/states.c,v
retrieving revision 1.5
diff -u -u -r1.5 states.c
--- check/generic/states.c	20 Sep 2005 00:27:37 -0000	1.5
+++ check/generic/states.c	10 Oct 2005 14:07:23 -0000
@@ -42,8 +42,7 @@
     gst_element_set_state (element, GST_STATE_READY);
     gst_element_set_state (element, GST_STATE_PAUSED);
     gst_element_set_state (element, GST_STATE_PLAYING);
-    /* Sleep to give any pad tasks time to start */
-    g_usleep (0.2 * G_USEC_PER_SEC);
+
     gst_element_set_state (element, GST_STATE_PAUSED);
     gst_element_set_state (element, GST_STATE_READY);
     gst_element_set_state (element, GST_STATE_NULL);
Index: check/gst/gstbin.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/check/gst/gstbin.c,v
retrieving revision 1.36
diff -u -u -r1.36 gstbin.c
--- check/gst/gstbin.c	10 Oct 2005 10:50:12 -0000	1.36
+++ check/gst/gstbin.c	10 Oct 2005 14:07:23 -0000
@@ -262,9 +262,8 @@
   /* change state to PAUSED, spawning three messages */
   GST_DEBUG ("setting pipeline to PAUSED");
   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
-  fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
-  ret =
-      gst_element_get_state (GST_ELEMENT (pipeline), &current, &pending, NULL);
+  fail_unless (ret == GST_STATE_CHANGE_ASYNC);
+  ret = gst_element_get_state (GST_ELEMENT (pipeline), &current, &pending, NULL);
   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
   fail_unless (current == GST_STATE_PAUSED);
   fail_unless (pending == GST_STATE_VOID_PENDING);
@@ -288,8 +287,7 @@
   GST_DEBUG ("setting pipeline to PLAYING");
   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
-  ret =
-      gst_element_get_state (GST_ELEMENT (pipeline), &current, &pending, NULL);
+  ret = gst_element_get_state (GST_ELEMENT (pipeline), &current, &pending, NULL);
   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
   fail_unless (current == GST_STATE_PLAYING);
   fail_unless (pending == GST_STATE_VOID_PENDING);
@@ -364,19 +362,13 @@
 
   fail_unless (gst_element_link (src, sink), "could not link src and sink");
 
-  /* change state, spawning two times three messages, minus one async */
+  /* change state, spawning two times three messages */
   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
   fail_unless (ret == GST_STATE_CHANGE_ASYNC);
+  ret = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, NULL);
+  fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
 
-  pop_messages (bus, 5);
-
-  fail_unless (gst_bus_have_pending (bus) == FALSE,
-      "Unexpected messages on bus");
-
-  gst_bin_watch_for_state_change (GST_BIN (bin));
-
-  /* should get the bin's state change message now */
-  pop_messages (bus, 1);
+  pop_messages (bus, 6);
 
   fail_unless (gst_bus_have_pending (bus) == FALSE,
       "Unexpected messages on bus");
@@ -388,8 +380,7 @@
 
   /* this one might return either SUCCESS or ASYNC, likely SUCCESS */
   gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
-
-  gst_bin_watch_for_state_change (GST_BIN (bin));
+  gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, NULL);
 
   pop_messages (bus, 3);
 
@@ -482,6 +473,7 @@
 {
   GstElement *src, *identity, *sink, *pipeline;
   GstStateChangeReturn ret;
+  GstState current, pending;
   GstBus *bus;
 
   pipeline = gst_pipeline_new (NULL);
@@ -506,7 +498,11 @@
 
   /* (1) Test state change with fakesink being a regular sink */
   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
+  fail_if (ret != GST_STATE_CHANGE_ASYNC, "State change to PLAYING did not return ASYNC");
+  ret = gst_element_get_state (pipeline, &current, &pending, NULL);
   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
+  fail_if (current != GST_STATE_PLAYING, "State change to PLAYING failed");
+  fail_if (pending != GST_STATE_VOID_PENDING, "State change to PLAYING failed");
 
   /* NULL => READY */
   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_NULL, GST_STATE_READY, 101);
@@ -602,7 +598,7 @@
   GST_FLAG_UNSET (sink, GST_ELEMENT_IS_SINK);   /* <======== */
 
   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
-  fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
+  fail_if (ret != GST_STATE_CHANGE_ASYNC, "State change to PLAYING not ASYNC");
   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
   fail_if (current != GST_STATE_PLAYING, "State change to PLAYING failed");
Index: check/gst/gstpipeline.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/check/gst/gstpipeline.c,v
retrieving revision 1.8
diff -u -u -r1.8 gstpipeline.c
--- check/gst/gstpipeline.c	8 Oct 2005 08:58:45 -0000	1.8
+++ check/gst/gstpipeline.c	10 Oct 2005 14:07:23 -0000
@@ -28,7 +28,6 @@
 
   pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
   fail_unless (pipeline != NULL, "Could not create pipeline");
-  g_object_set (G_OBJECT (pipeline), "play-timeout", 0LL, NULL);
 
   fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline),
           GST_STATE_PLAYING), GST_STATE_CHANGE_SUCCESS);
@@ -45,7 +44,6 @@
 
   pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
   fail_unless (pipeline != NULL, "Could not create pipeline");
-  g_object_set (G_OBJECT (pipeline), "play-timeout", 0LL, NULL);
 
   src = gst_element_factory_make ("fakesrc", NULL);
   sink = gst_element_factory_make ("fakesink", NULL);
@@ -71,7 +69,6 @@
 
   pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
   fail_unless (pipeline != NULL, "Could not create pipeline");
-  g_object_set (G_OBJECT (pipeline), "play-timeout", 0LL, NULL);
 
   src = gst_element_factory_make ("fakesrc", NULL);
   sink = gst_element_factory_make ("fakesink", NULL);
@@ -81,7 +78,7 @@
 
   bus = gst_pipeline_get_bus (pipeline);
 
-  fail_unless_equals_int (gst_element_set_state_async (GST_ELEMENT (pipeline),
+  fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline),
           GST_STATE_PLAYING), GST_STATE_CHANGE_ASYNC);
 
   while (!done) {
@@ -98,7 +95,6 @@
     }
   }
 
-  g_object_set (G_OBJECT (pipeline), "play-timeout", 3 * GST_SECOND, NULL);
   fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline),
           GST_STATE_NULL), GST_STATE_CHANGE_SUCCESS);
 
@@ -175,7 +171,6 @@
   pipeline = gst_pipeline_new (NULL);
   fail_unless (pipeline != NULL, "Could not create pipeline");
   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
-  g_object_set (pipeline, "play-timeout", 0LL, NULL);
 
   src = gst_element_factory_make ("fakesrc", NULL);
   fail_unless (src != NULL);
@@ -193,7 +188,7 @@
   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline after add_watch", 1);
   ASSERT_OBJECT_REFCOUNT (bus, "bus after add_watch", 3);
 
-  gst_element_set_state_async (pipeline, GST_STATE_PLAYING);
+  gst_element_set_state (pipeline, GST_STATE_PLAYING);
   loop = g_main_loop_new (NULL, FALSE);
   GST_DEBUG ("going into main loop");
   g_main_loop_run (loop);
@@ -207,9 +202,6 @@
   /* cleanup */
   GST_DEBUG ("cleanup");
 
-  /* current semantics require us to go step by step; this will change */
-  gst_element_set_state (pipeline, GST_STATE_PAUSED);
-  gst_element_set_state (pipeline, GST_STATE_READY);
   gst_element_set_state (pipeline, GST_STATE_NULL);
   fail_unless (gst_element_get_state (pipeline, &current, NULL, NULL) ==
       GST_STATE_CHANGE_SUCCESS);
@@ -238,6 +230,8 @@
   Suite *s = suite_create ("GstPipeline");
   TCase *tc_chain = tcase_create ("pipeline tests");
 
+  tcase_set_timeout (tc_chain, 0);
+
   suite_add_tcase (s, tc_chain);
   tcase_add_test (tc_chain, test_async_state_change_empty);
   tcase_add_test (tc_chain, test_async_state_change_fake_ready);
Index: check/states/sinks.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/check/states/sinks.c,v
retrieving revision 1.10
diff -u -u -r1.10 sinks.c
--- check/states/sinks.c	10 Oct 2005 11:04:55 -0000	1.10
+++ check/states/sinks.c	10 Oct 2005 14:07:23 -0000
@@ -42,7 +42,7 @@
 
   ret = gst_element_get_state (sink, &current, &pending, &tv);
   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "not changing state async");
-  fail_unless (current == GST_STATE_PAUSED, "bad current state");
+  fail_unless (current == GST_STATE_READY, "bad current state");
   fail_unless (pending == GST_STATE_PLAYING, "bad pending state");
 
   ret = gst_element_set_state (sink, GST_STATE_PAUSED);
@@ -81,6 +81,7 @@
   gst_object_unref (sinkpad);
 
   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
+  fail_unless (ret == GST_STATE_CHANGE_ASYNC, "no async state return");
   ret = gst_element_get_state (pipeline, NULL, NULL, NULL);
   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "no success state return");
 
@@ -91,14 +92,14 @@
   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "not playing");
   fail_unless (current == GST_STATE_PLAYING, "not playing");
   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
+
   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "cannot null pipeline");
 
   gst_object_unref (pipeline);
-
 }
-
 GST_END_TEST
+
 /* a pipeline with live source should return NO_PREROLL in
  * PAUSE. When removing the live source it should return ASYNC
  * from the sink */
@@ -139,8 +140,7 @@
   ret = gst_element_get_state (pipeline, &current, &pending, &tv);
   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "not async");
   fail_unless (current == GST_STATE_PAUSED, "not paused");
-  fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
-
+  fail_unless (pending == GST_STATE_PAUSED, "not paused");
 }
 
 GST_END_TEST
Index: gst/gstbin.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstbin.c,v
retrieving revision 1.280
diff -u -u -r1.280 gstbin.c
--- gst/gstbin.c	10 Oct 2005 11:52:58 -0000	1.280
+++ gst/gstbin.c	10 Oct 2005 14:07:23 -0000
@@ -86,6 +86,7 @@
 
 static void gst_bin_dispose (GObject * object);
 
+static void gst_bin_recalc_state (GstBin * bin, gboolean force);
 static GstStateChangeReturn gst_bin_change_state_func (GstElement * element,
     GstStateChange transition);
 static GstStateChangeReturn gst_bin_get_state_func (GstElement * element,
@@ -292,6 +293,8 @@
   bin->children = NULL;
   bin->children_cookie = 0;
   bin->eosed = NULL;
+  bin->polling = FALSE;
+  bin->state_dirty = FALSE;
 
   /* Set up a bus for listening to child elements */
   bus = g_object_new (gst_bus_get_type (), NULL);
@@ -483,7 +486,7 @@
   gst_element_set_base_time (element, GST_ELEMENT (bin)->base_time);
   GST_DEBUG_OBJECT (element, "setting clock %p", GST_ELEMENT_CLOCK (bin));
   gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
-
+  bin->state_dirty = TRUE;
   GST_UNLOCK (bin);
 
   /* unlink all linked pads */
@@ -619,6 +622,7 @@
       GST_FLAG_UNSET (bin, GST_ELEMENT_IS_SINK);
     }
   }
+  bin->state_dirty = TRUE;
   GST_UNLOCK (bin);
 
   GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
@@ -872,14 +876,7 @@
   return result;
 }
 
-/* 2 phases:
- *  1) check state of all children with 0 timeout to find ERROR and
- *     NO_PREROLL elements. return if found.
- *  2) perform full blocking wait with requested timeout.
- *
- * 2) cannot be performed when 1) returns results as the sinks might
- *    not be able to complete the state change making 2) block forever.
- *
+/*
  * MT safe
  */
 static GstStateChangeReturn
@@ -887,6 +884,18 @@
     GstState * pending, GTimeVal * timeout)
 {
   GstBin *bin = GST_BIN (element);
+
+  GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
+
+  /* do a non forced recalculation of the state */
+  gst_bin_recalc_state (bin, FALSE);
+
+  return parent_class->get_state (element, state, pending, timeout);
+}
+
+static void
+gst_bin_recalc_state (GstBin * bin, gboolean force)
+{
   GstStateChangeReturn ret;
   GList *children;
   guint32 children_cookie;
@@ -894,12 +903,21 @@
   gboolean have_async;
   GTimeVal tv;
 
-  GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
-
   ret = GST_STATE_CHANGE_SUCCESS;
 
   /* lock bin, no element can be added or removed while we have this lock */
   GST_LOCK (bin);
+  /* no point in scanning if nothing changed and it's no forced recalc */
+  if (!force && !bin->state_dirty)
+    goto not_dirty;
+
+  /* no point in having two scans run concurrently */
+  if (bin->polling)
+    goto was_polling;
+
+  bin->polling = TRUE;
+
+  GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "recalc state");
 
 restart:
   have_no_preroll = FALSE;
@@ -957,59 +975,15 @@
   /* if we have NO_PREROLL, return that */
   if (have_no_preroll) {
     ret = GST_STATE_CHANGE_NO_PREROLL;
-    goto done;
   }
-  /* else return SUCCESS if no async elements were found */
-  else if (!have_async) {
-    ret = GST_STATE_CHANGE_SUCCESS;
-    goto done;
-  }
-
-  /* next we poll all children for their state to see if one of them
-   * is still busy with its state change. We did not release the bin lock
-   * yet so the elements are the same as the ones from the quick scan. */
-  children = bin->children;
-  children_cookie = bin->children_cookie;
-  while (children) {
-    GstElement *child = GST_ELEMENT_CAST (children->data);
-
-    gst_object_ref (child);
-    /* now we release the lock to enter the potentialy blocking wait */
-    GST_UNLOCK (bin);
-
-    /* ret is ASYNC if some child is still performing the state change
-     * ater the timeout. */
-    ret = gst_element_get_state (child, NULL, NULL, timeout);
-
-    gst_object_unref (child);
-
-    /* now grab the lock to iterate to the next child */
-    GST_LOCK (bin);
-    if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
-      /* child added/removed during state change, restart. We need
-       * to restart with the quick check as a no-preroll element could
-       * have been added here and we don't want to block on sinks then.*/
-      goto restart;
-    }
-
-    switch (ret) {
-      case GST_STATE_CHANGE_SUCCESS:
-        break;
-      case GST_STATE_CHANGE_FAILURE:
-      case GST_STATE_CHANGE_NO_PREROLL:
-        /* report FAILURE and NO_PREROLL immediatly */
-        goto done;
-      case GST_STATE_CHANGE_ASYNC:
-        goto done;
-      default:
-        g_assert_not_reached ();
-    }
-    children = g_list_next (children);
+  /* else return ASYNC if async elements were found */
+  else if (have_async) {
+    ret = GST_STATE_CHANGE_ASYNC;
   }
-  /* if we got here, all elements can do preroll */
-  have_no_preroll = FALSE;
 
 done:
+  bin->state_dirty = FALSE;
+  bin->polling = FALSE;
   GST_UNLOCK (bin);
 
   /* now we can take the state lock, it is possible that new elements
@@ -1019,33 +993,45 @@
   GST_STATE_LOCK (bin);
   switch (ret) {
     case GST_STATE_CHANGE_SUCCESS:
-      gst_element_commit_state (element);
+    case GST_STATE_CHANGE_NO_PREROLL:
+      gst_element_commit_state (GST_ELEMENT_CAST (bin));
+      break;
+    case GST_STATE_CHANGE_ASYNC:
+      gst_element_lost_state (GST_ELEMENT_CAST (bin));
       break;
     case GST_STATE_CHANGE_FAILURE:
-      gst_element_abort_state (element);
+      gst_element_abort_state (GST_ELEMENT_CAST (bin));
       break;
     default:
-      /* other cases are just passed along */
-      break;
+      goto unknown_state;
   }
-
-  /* and report the state if needed */
-  if (state)
-    *state = GST_STATE (element);
-  if (pending)
-    *pending = GST_STATE_PENDING (element);
-
-  GST_STATE_NO_PREROLL (element) = have_no_preroll;
-
-  GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-      "state current: %s, pending: %s, error: %d, no_preroll: %d, result: %d",
-      gst_element_state_get_name (GST_STATE (element)),
-      gst_element_state_get_name (GST_STATE_PENDING (element)),
-      GST_STATE_ERROR (element), GST_STATE_NO_PREROLL (element), ret);
-
+  GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "return now %d",
+      GST_STATE_RETURN (bin));
   GST_STATE_UNLOCK (bin);
+  return;
 
-  return ret;
+not_dirty:
+  {
+    GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "not dirty");
+    GST_UNLOCK (bin);
+    return;
+  }
+was_polling:
+  {
+    GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "was polling");
+    GST_UNLOCK (bin);
+    return;
+  }
+unknown_state:
+  {
+    /* somebody added a GST_STATE_ and forgot to do stuff here ! */
+    GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
+        "unknown return value %d from a state change function", ret);
+    g_critical ("unknown return value %d from a state change function", ret);
+    GST_STATE_RETURN (bin) = GST_STATE_CHANGE_FAILURE;
+    GST_STATE_UNLOCK (bin);
+    return;
+  }
 }
 
 /***********************************************
@@ -1340,16 +1326,13 @@
   gboolean done;
 
   /* we don't need to take the STATE_LOCK, it is already taken */
-  current = GST_STATE (element);
-  next = GST_STATE_PENDING (element);
+  current = GST_STATE_TRANSITION_CURRENT (transition);
+  next = GST_STATE_TRANSITION_NEXT (transition);
 
   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
       "changing state of children from %s to %s",
       gst_element_state_get_name (current), gst_element_state_get_name (next));
 
-  if (next == GST_STATE_VOID_PENDING)
-    return GST_STATE_CHANGE_SUCCESS;
-
   bin = GST_BIN_CAST (element);
 
   /* Clear eosed element list on next PAUSED */
@@ -1435,12 +1418,14 @@
     }
   }
 
+  ret = parent_class->change_state (element, transition);
+  if (ret == GST_STATE_CHANGE_FAILURE)
+    goto done;
+
   if (have_no_preroll) {
     ret = GST_STATE_CHANGE_NO_PREROLL;
   } else if (have_async) {
     ret = GST_STATE_CHANGE_ASYNC;
-  } else {
-    ret = parent_class->change_state (element, transition);
   }
 
 done:
@@ -1563,6 +1548,60 @@
       gst_message_unref (message);
       break;
     }
+    case GST_MESSAGE_STATE_CHANGED:
+    {
+      GstState old, new, pending;
+      GstObject *src;
+
+      gst_message_parse_state_changed (message, &old, &new, &pending);
+      src = GST_MESSAGE_SRC (message);
+      /* ref src, as we need it after we post the message up */
+      gst_object_ref (src);
+
+      GST_DEBUG_OBJECT (bin, "%s gave state change, %s -> %s, pending %s",
+          GST_ELEMENT_NAME (src),
+          gst_element_state_get_name (old),
+          gst_element_state_get_name (new),
+          gst_element_state_get_name (pending));
+
+      /* post message up */
+      gst_element_post_message (GST_ELEMENT_CAST (bin), message);
+
+      /* we only act on our own children */
+      GST_LOCK (bin);
+      if (!g_list_find (bin->children, src))
+        goto not_our_child;
+      GST_UNLOCK (bin);
+
+      gst_object_unref (src);
+
+      /* we can lock, either the state change is sync and we can
+       * recursively lock or the state change is async and we 
+       * lock when the bin has done it state change. We can check which
+       * case it is by looking at the CHANGING_STATE flag. */
+      GST_STATE_LOCK (bin);
+      GST_DEBUG_OBJECT (bin, "locked");
+
+      if (!GST_FLAG_IS_SET (bin, GST_ELEMENT_CHANGING_STATE)) {
+        GST_DEBUG_OBJECT (bin, "got ASYNC message, forcing recalc state");
+        GST_STATE_UNLOCK (bin);
+
+        /* force bin state recalculation on async messages. */
+        gst_bin_recalc_state (bin, TRUE);
+      } else {
+        GST_STATE_UNLOCK (bin);
+        GST_DEBUG_OBJECT (bin, "got SYNC message");
+      }
+      break;
+
+    not_our_child:
+      {
+        GST_UNLOCK (bin);
+        GST_DEBUG_OBJECT (bin, "not our child");
+        gst_object_unref (src);
+        break;
+      }
+    }
     default:
       /* Send all other messages upward */
       GST_DEBUG_OBJECT (bin, "posting message upward");
Index: gst/gstbin.h
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstbin.h,v
retrieving revision 1.79
diff -u -u -r1.79 gstbin.h
--- gst/gstbin.h	27 Sep 2005 16:16:39 -0000	1.79
+++ gst/gstbin.h	10 Oct 2005 14:07:23 -0000
@@ -92,6 +92,8 @@
   GstBus        *child_bus;	/* Bus we set on our children */
   GList         *eosed;         /* list of elements that posted EOS */
 
+  gboolean	 polling;
+  gboolean       state_dirty;
   /*< private >*/
   gpointer _gst_reserved[GST_PADDING];
 };
Index: gst/gstelement.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstelement.c,v
retrieving revision 1.379
diff -u -u -r1.379 gstelement.c
--- gst/gstelement.c	10 Oct 2005 14:03:25 -0000	1.379
+++ gst/gstelement.c	10 Oct 2005 14:07:23 -0000
@@ -55,7 +55,6 @@
  * 
  * Each element has a state (see #GstState).  You can get and set the state
  * of an element with gst_element_get_state() and gst_element_set_state().  
- * You can wait for an element to change it's state with gst_element_wait_state_change().
  * To get a string representation of a #GstState, use 
  * gst_element_state_get_name().
  * 
@@ -88,7 +87,6 @@
 /* Element signals and args */
 enum
 {
-  STATE_CHANGE,
   NEW_PAD,
   PAD_REMOVED,
   NO_MORE_PADS,
@@ -239,9 +237,13 @@
 static void
 gst_element_init (GstElement * element)
 {
-  element->current_state = GST_STATE_NULL;
-  element->pending_state = GST_STATE_VOID_PENDING;
-  element->state_lock = g_mutex_new ();
+  GST_STATE (element) = GST_STATE_NULL;
+  GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
+  GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
+  GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
+
+  element->state_lock = g_new0 (GStaticRecMutex, 1);
+  g_static_rec_mutex_init (element->state_lock);
   element->state_cond = g_cond_new ();
 }
 
@@ -1617,17 +1619,19 @@
   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
 
   GST_STATE_LOCK (element);
+  ret = GST_STATE_RETURN (element);
+
   /* we got an error, report immediatly */
-  if (GST_STATE_NO_PREROLL (element)) {
-    ret = GST_STATE_CHANGE_NO_PREROLL;
+  if (ret == GST_STATE_CHANGE_FAILURE)
     goto done;
-  }
 
-  /* we got an error, report immediatly */
-  if (GST_STATE_ERROR (element)) {
-    ret = GST_STATE_CHANGE_FAILURE;
+  /* we got no_preroll, report immediatly */
+  if (ret == GST_STATE_CHANGE_NO_PREROLL)
+    goto done;
+
+  /* no need to wait async if we are not async */
+  if (ret != GST_STATE_CHANGE_ASYNC)
     goto done;
-  }
 
   old_pending = GST_STATE_PENDING (element);
   if (old_pending != GST_STATE_VOID_PENDING) {
@@ -1636,6 +1640,9 @@
     if (timeout) {
       glong add = timeout->tv_sec * G_USEC_PER_SEC + timeout->tv_usec;
 
+      if (add == 0)
+        goto done;
+
       /* make timeout absolute */
       g_get_current_time (&abstimeout);
       g_time_val_add (&abstimeout, add);
@@ -1673,10 +1680,9 @@
     *pending = GST_STATE_PENDING (element);
 
   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-      "state current: %s, pending: %s, error: %d, no_preroll: %d, result: %d",
+      "state current: %s, pending: %s, result: %d",
       gst_element_state_get_name (GST_STATE (element)),
-      gst_element_state_get_name (GST_STATE_PENDING (element)),
-      GST_STATE_ERROR (element), GST_STATE_NO_PREROLL (element), ret);
+      gst_element_state_get_name (GST_STATE_PENDING (element)), ret);
 
   GST_STATE_UNLOCK (element);
 
@@ -1701,6 +1707,9 @@
  * an error, this function returns immediatly with a return value of
  * GST_STATE_CHANGE_SUCCESS or GST_STATE_CHANGE_FAILURE respectively. 
  *
+ * For elements that did not return ASYNC, this function returns the 
+ * current and pending state immediatly.
+ *
  * Returns: GST_STATE_CHANGE_SUCCESS if the element has no more pending state and
  *          the last state change succeeded, GST_STATE_CHANGE_ASYNC
  *          if the element is still performing a state change or 
@@ -1746,7 +1755,8 @@
 
   pending = GST_STATE_PENDING (element);
 
-  if (pending != GST_STATE_VOID_PENDING && !GST_STATE_ERROR (element)) {
+  if (pending != GST_STATE_VOID_PENDING &&
+      GST_STATE_RETURN (element) != GST_STATE_CHANGE_FAILURE) {
 #ifndef GST_DISABLE_GST_DEBUG
     GstState old_state = GST_STATE (element);
 #endif
@@ -1756,7 +1766,7 @@
         gst_element_state_get_name (pending));
 
     /* flag error */
-    GST_STATE_ERROR (element) = TRUE;
+    GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
 
     GST_STATE_BROADCAST (element);
   }
@@ -1768,38 +1778,93 @@
  *
  * Commit the state change of the element. This function is used
  * by elements that do asynchronous state changes.
+ * The core will normally call this method automatically when an 
+ * element returned SUCCESS from the state change function.
+ * Elements that return ASYNC from the change_state function should
+ * eventually call this method from the streaming thread to signal
+ * successfull state change completion.
+ *
+ * If after calling this method the element still has not reached
+ * the pending state, the next state change is performed.
  *
  * This function can only be called with the STATE_LOCK held.
  *
+ * Returns: The result of the commit state change.
+ *
  * MT safe.
  */
-void
+GstStateChangeReturn
 gst_element_commit_state (GstElement * element)
 {
   GstState pending;
+  GstStateChangeReturn ret;
 
-  g_return_if_fail (GST_IS_ELEMENT (element));
+  g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
+
+  ret = GST_STATE_CHANGE_SUCCESS;
 
   pending = GST_STATE_PENDING (element);
 
+  /* check if there is something to commit */
   if (pending != GST_STATE_VOID_PENDING) {
-    GstState old_state = GST_STATE (element);
+    GstState old_state;
+    GstState current, next;
     GstMessage *message;
 
+    GST_FLAG_SET (element, GST_ELEMENT_CHANGING_STATE);
+
+    old_state = GST_STATE (element);
+    /* this is the state we should go to next */
+    next = GST_STATE_NEXT (element);
+    /* update current state */
+    current = GST_STATE (element) = next;
+
+    /* see if we reached the final state */
+    if (pending == next) {
+      pending = GST_STATE_VOID_PENDING;
+      GST_STATE_PENDING (element) = pending;
+      GST_STATE_NEXT (element) = pending;
+      ret = GST_STATE_CHANGE_SUCCESS;
+    } else {
+      /* not there yet, will get there ASYNC */
+      ret = GST_STATE_CHANGE_ASYNC;
+    }
+
+    GST_STATE_RETURN (element) = ret;
+
     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-        "committing state from %s to %s",
+        "committing state from %s to %s, pending %s",
         gst_element_state_get_name (old_state),
+        gst_element_state_get_name (next),
         gst_element_state_get_name (pending));
 
-    GST_STATE (element) = pending;
-    GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
-    GST_STATE_ERROR (element) = FALSE;
-
     message = gst_message_new_state_changed (GST_OBJECT (element),
-        old_state, pending, pending);
+        old_state, next, pending);
     gst_element_post_message (element, message);
-    GST_STATE_BROADCAST (element);
+
+    if (pending != GST_STATE_VOID_PENDING) {
+      GstStateChange transition;
+
+      /* calc new next state */
+      next = GST_STATE_GET_NEXT (current, pending);
+
+      GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
+          "continue state change %s to %s, final %s",
+          gst_element_state_get_name (current),
+          gst_element_state_get_name (next),
+          gst_element_state_get_name (pending));
+
+      /* create transition */
+      transition = GST_STATE_TRANSITION (current, next);
+
+      /* perform next transition */
+      ret = gst_element_change_state (element, transition);
+    } else {
+      GST_STATE_BROADCAST (element);
+    }
+    GST_FLAG_UNSET (element, GST_ELEMENT_CHANGING_STATE);
   }
+  return ret;
 }
 
 /**
@@ -1826,7 +1891,7 @@
   g_return_if_fail (GST_IS_ELEMENT (element));
 
   if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING &&
-      !GST_STATE_ERROR (element)) {
+      GST_STATE_RETURN (element) != GST_STATE_CHANGE_FAILURE) {
     GstState current_state;
     GstMessage *message;
 
@@ -1835,8 +1900,9 @@
     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
         "lost state of %s", gst_element_state_get_name (current_state));
 
+    GST_STATE_NEXT (element) = current_state;
     GST_STATE_PENDING (element) = current_state;
-    GST_STATE_ERROR (element) = FALSE;
+    GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
 
     message = gst_message_new_state_changed (GST_OBJECT (element),
         current_state, current_state, current_state);
@@ -1853,6 +1919,11 @@
  * requested state by going through all the intermediary states and calling
  * the class's state change function for each.
  *
+ * This function can return GST_STATE_CHANGE_ASYNC, in which case the
+ * element will perform the remainder of the state change asynchronously.
+ * An application can use gst_element_get_state() to wait for the completion
+ * of the state change or it can wait for a state change message on the bus.
+ *
  * Returns: Result of the state change using #GstStateChangeReturn.
  *
  * MT safe.
@@ -1860,40 +1931,59 @@
 GstStateChangeReturn
 gst_element_set_state (GstElement * element, GstState state)
 {
-  GstState current, old_pending;
+  GstState current, next, old_pending;
   GstStateChangeReturn ret;
   GstStateChange transition;
-  GTimeVal tv;
 
   g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
 
   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
       gst_element_state_get_name (state));
 
-  /* get current element state,  need to call the method so that
-   * we call the virtual method and subclasses can implement their
-   * own algorithms */
-  GST_TIME_TO_TIMEVAL (0, tv);
-  ret = gst_element_get_state (element, &current, &old_pending, &tv);
-
   GST_STATE_LOCK (element);
+  GST_FLAG_SET (element, GST_ELEMENT_CHANGING_STATE);
+
+  current = GST_STATE (element);
+  next = GST_STATE_NEXT (element);
+  old_pending = GST_STATE_PENDING (element);
 
   /* this is the (new) state we should go to */
-  GST_STATE_FINAL (element) = state;
-  if (ret == GST_STATE_CHANGE_ASYNC) {
-    /* force next state keeping ASYNC, this is atomic as we hold
-     * the STATE_LOCK */
-    gst_element_commit_state (element);
-    gst_element_lost_state (element);
-    if (state == GST_STATE_PENDING (element))
+  GST_STATE_PENDING (element) = state;
+
+  GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
+      "current %s, old_pending %s, next %s",
+      gst_element_state_get_name (current),
+      gst_element_state_get_name (old_pending),
+      gst_element_state_get_name (next));
+
+  /* if the element was busy doing a state change, we just update the
+   * target state, it'll get to it async then. */
+  if (old_pending != GST_STATE_VOID_PENDING) {
+    /* upwards state change will happen ASYNC */
+    if (old_pending <= state)
       goto was_busy;
+    /* element is going to this state already */
+    else if (next == state)
+      goto was_busy;
+    /* element was performing an ASYNC upward state change and
+     * we request to go downward again. Start from the next pending
+     * state then. */
+    else if (next > state) {
+      current = next;
+    }
   }
+  next = GST_STATE_GET_NEXT (current, state);
 
-  /* fixme, not right */
-  transition = GST_STATE_CHANGE (element);
+  GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
+      "%s: setting state from %s to %s",
+      (next != state ? "intermediate" : "final"),
+      gst_element_state_get_name (current), gst_element_state_get_name (next));
+
+  transition = GST_STATE_TRANSITION (current, next);
 
   ret = gst_element_change_state (element, transition);
 
+  GST_FLAG_UNSET (element, GST_ELEMENT_CHANGING_STATE);
   GST_STATE_UNLOCK (element);
 
   GST_DEBUG_OBJECT (element, "returned %d", ret);
@@ -1902,12 +1992,15 @@
 
 was_busy:
   {
+    GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
+    GST_FLAG_UNSET (element, GST_ELEMENT_CHANGING_STATE);
     GST_STATE_UNLOCK (element);
 
     GST_DEBUG_OBJECT (element, "element was busy with async state change");
 
     return GST_STATE_CHANGE_ASYNC;
   }
+
 }
 
 /* with STATE_LOCK */
@@ -1916,90 +2009,71 @@
 {
   GstElementClass *oclass;
   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
-  GstState current, next, final;
+  GstState current, next;
 
   oclass = GST_ELEMENT_GET_CLASS (element);
 
   /* start with the current state. */
-  current = GST_STATE (element);
-  next = GST_STATE_PENDING (element);
-  final = GST_STATE_FINAL (element);
+  current = GST_STATE_TRANSITION_CURRENT (transition);
+  next = GST_STATE_TRANSITION_NEXT (transition);
 
-  /* We always perform at least one state change, even if the
-   * current state is equal to the required state. This is needed
-   * for bins that sync their children. */
-  do {
-    GstState pending;
-
-    /* calculate the pending state */
-    if (current < final)
-      pending = current + 1;
-    else if (current > final)
-      pending = current - 1;
-    else
-      pending = current;
+  /* now we store the next state */
+  GST_STATE_NEXT (element) = next;
 
-    /* set the pending state variable */
-    GST_STATE_PENDING (element) = pending;
+  /* call the state change function so it can set the state */
+  if (oclass->change_state)
+    ret = (oclass->change_state) (element, transition);
+  else
+    ret = GST_STATE_CHANGE_FAILURE;
 
-    GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
-        "%s: setting state from %s to %s",
-        (pending != final ? "intermediate" : "final"),
-        gst_element_state_get_name (current),
-        gst_element_state_get_name (pending));
+  switch (ret) {
+    case GST_STATE_CHANGE_FAILURE:
+      GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
+          "have FAILURE change_state return");
+      /* state change failure */
+      gst_element_abort_state (element);
+      break;
+    case GST_STATE_CHANGE_ASYNC:
+      GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
+          "element will change state ASYNC");
+
+      /* if we go upwards, we give the app a change to wait for
+       * completion */
+      if (current < next)
+        goto exit;
 
-    /* call the state change function so it can set the state */
-    if (oclass->change_state)
-      ret = (oclass->change_state) (element, GST_STATE_CHANGE (element));
-    else
-      ret = GST_STATE_CHANGE_FAILURE;
+      /* else we just continue the state change downwards */
+      GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
+          "forcing commit state %s < %s",
+          gst_element_state_get_name (current),
+          gst_element_state_get_name (next));
 
-    /* clear the error and preroll flag, we need to do that after
-     * calling the virtual change_state function so that it can use the
-     * old previous value. */
-    GST_STATE_ERROR (element) = FALSE;
-    GST_STATE_NO_PREROLL (element) = FALSE;
-
-    switch (ret) {
-      case GST_STATE_CHANGE_FAILURE:
-        GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-            "have failed change_state return");
-        /* state change failure exits the loop */
-        gst_element_abort_state (element);
-        goto exit;
-      case GST_STATE_CHANGE_ASYNC:
-        GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-            "element will change state async");
-        /* an async state change exits the loop, we can only
-         * go to the next state change when this one completes. */
-        goto exit;
-      case GST_STATE_CHANGE_SUCCESS:
-        GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-            "element changed state successfully");
-        /* we can commit the state now and proceed to the next state */
-        gst_element_commit_state (element);
-        GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "committed state");
-        break;
-      case GST_STATE_CHANGE_NO_PREROLL:
-        GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-            "element changed state successfully and can't preroll");
-        /* we can commit the state now and proceed to the next state */
-        gst_element_commit_state (element);
-        GST_STATE_NO_PREROLL (element) = TRUE;
-        GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "committed state");
-        break;
-      default:
-        ret = GST_STATE_CHANGE_FAILURE;
-        goto invalid_return;
-    }
-    /* get the current state of the element and see if we need to do more
-     * state changes */
-    current = GST_STATE (element);
+      GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
+
+      ret = gst_element_commit_state (element);
+      break;
+    case GST_STATE_CHANGE_SUCCESS:
+      GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
+          "element changed state SUCCESS");
+      /* we can commit the state now which will proceeed to 
+       * the next state */
+      ret = gst_element_commit_state (element);
+      break;
+    case GST_STATE_CHANGE_NO_PREROLL:
+      GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
+          "element changed state NO_PREROLL");
+      /* we can commit the state now which will proceeed to 
+       * the next state */
+      gst_element_commit_state (element);
+      ret = GST_STATE_CHANGE_NO_PREROLL;
+      break;
+    default:
+      ret = GST_STATE_CHANGE_FAILURE;
+      goto invalid_return;
   }
-  while (current != final);
 
 exit:
-  GST_STATE_FINAL (element) = GST_STATE_VOID_PENDING;
+  GST_STATE_RETURN (element) = ret;
 
   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
 
@@ -2008,7 +2082,7 @@
   /* ERROR */
 invalid_return:
   {
-    GST_STATE_FINAL (element) = GST_STATE_VOID_PENDING;
+    GST_STATE_RETURN (element) = ret;
     /* somebody added a GST_STATE_ and forgot to do stuff here ! */
     g_critical ("unknown return value %d from a state change function", ret);
     return ret;
@@ -2096,8 +2170,8 @@
 
   g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
 
-  state = GST_STATE (element);
-  next = GST_STATE_PENDING (element);
+  state = GST_STATE_TRANSITION_CURRENT (transition);
+  next = GST_STATE_TRANSITION_NEXT (transition);
 
   /* if the element already is in the given state, we just return success */
   if (next == GST_STATE_VOID_PENDING || state == next)
@@ -2148,10 +2222,7 @@
     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
         "element is already in the %s state",
         gst_element_state_get_name (state));
-    if (GST_STATE_NO_PREROLL (element))
-      return GST_STATE_CHANGE_NO_PREROLL;
-    else
-      return GST_STATE_CHANGE_SUCCESS;
+    return GST_STATE_RETURN (element);
   }
 }
 
@@ -2213,7 +2284,7 @@
     g_cond_free (element->state_cond);
   element->state_cond = NULL;
   GST_STATE_UNLOCK (element);
-  g_mutex_free (element->state_lock);
+  g_static_rec_mutex_free (element->state_lock);
 
   GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
 
Index: gst/gstelement.h
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstelement.h,v
retrieving revision 1.214
diff -u -u -r1.214 gstelement.h
--- gst/gstelement.h	8 Oct 2005 14:57:08 -0000	1.214
+++ gst/gstelement.h	10 Oct 2005 14:07:23 -0000
@@ -94,34 +94,35 @@
 #define GST_STATE(obj)			(GST_ELEMENT(obj)->current_state)
 
 /**
+ * GST_STATE_NEXT:
+ * @obj: Element to return the next state for.
+ *
+ * This macro returns the next state of the element.
+ */
+#define GST_STATE_NEXT(obj)		(GST_ELEMENT(obj)->next_state)
+
+/**
  * GST_STATE_PENDING:
  * @obj: Element to return the pending state for.
  *
  * This macro returns the currently pending state of the element.
  */
 #define GST_STATE_PENDING(obj)		(GST_ELEMENT(obj)->pending_state)
-#define GST_STATE_FINAL(obj)		(GST_ELEMENT(obj)->final_state)
-#define GST_STATE_ERROR(obj)		(GST_ELEMENT(obj)->state_error)
-#define GST_STATE_NO_PREROLL(obj)	(GST_ELEMENT(obj)->no_preroll)
-
-#ifndef GST_DEBUG_STATE_CHANGE
-#define GST_STATE_CHANGE(obj) ((1<<(GST_STATE(obj)+8)) | 1<<GST_STATE_PENDING(obj))
-#else
-inline GstStateChange
-_gst_element_get_state_change (GstElement *e)
-{
-  if (e->state < GST_STATE_NULL || e->state > GST_STATE_PLAYING)
-    g_assert_not_reached ();
-  if (e->pending_state < GST_STATE_NULL || e->pending_state > GST_STATE_PLAYING)
-    g_assert_not_reached ();
-  if (e->state - e->pending_state != 1 && e->pending_state - e->state != 1)
-    g_assert_not_reached ();
-  return (1<<(GST_STATE(obj)+8)) | 1<<GST_STATE_PENDING(obj);
-}
-#define GST_STATE_CHANGE(obj) _gst_element_get_state_change(obj)
-#endif
 
-/* FIXME: How to deal with lost_state ? */
+/**
+ * GST_STATE_RETURN:
+ * @obj: Element to return the last state result for.
+ *
+ * This macro returns the last state change return value.
+ */
+#define GST_STATE_RETURN(obj)		(GST_ELEMENT(obj)->last_return)
+
+#define GST_SIGN(val)				((val) < 0 ? -1 : ((val) > 0 ? 1 : 0))
+#define GST_STATE_GET_NEXT(cur,pending) 	((cur)+GST_SIGN ((gint)(pending)-(gint)(cur)))
+#define GST_STATE_TRANSITION(cur,next)  	(((cur)<<3)|(next))
+#define GST_STATE_TRANSITION_CURRENT(trans)  	((trans)>>3)
+#define GST_STATE_TRANSITION_NEXT(trans)  	((trans)&0x7)
+
 /**
  * GstStateChange:
  * @GST_STATE_CHANGE_NULL_TO_READY    : state change from NULL to READY
@@ -133,12 +134,12 @@
  */
 typedef enum /*< flags=0 >*/
 {
-  GST_STATE_CHANGE_NULL_TO_READY	= 1<<(GST_STATE_NULL+8) | 1<<GST_STATE_READY,
-  GST_STATE_CHANGE_READY_TO_PAUSED	= 1<<(GST_STATE_READY+8) | 1<<GST_STATE_PAUSED,
-  GST_STATE_CHANGE_PAUSED_TO_PLAYING	= 1<<(GST_STATE_PAUSED+8) | 1<<GST_STATE_PLAYING,
-  GST_STATE_CHANGE_PLAYING_TO_PAUSED	= 1<<(GST_STATE_PLAYING+8) | 1<<GST_STATE_PAUSED,
-  GST_STATE_CHANGE_PAUSED_TO_READY	= 1<<(GST_STATE_PAUSED+8) | 1<<GST_STATE_READY,
-  GST_STATE_CHANGE_READY_TO_NULL	= 1<<(GST_STATE_READY+8) | 1<<GST_STATE_NULL
+  GST_STATE_CHANGE_NULL_TO_READY	= (GST_STATE_NULL<<3) | GST_STATE_READY,
+  GST_STATE_CHANGE_READY_TO_PAUSED	= (GST_STATE_READY<<3) | GST_STATE_PAUSED,
+  GST_STATE_CHANGE_PAUSED_TO_PLAYING	= (GST_STATE_PAUSED<<3) | GST_STATE_PLAYING,
+  GST_STATE_CHANGE_PLAYING_TO_PAUSED	= (GST_STATE_PLAYING<<3) | GST_STATE_PAUSED,
+  GST_STATE_CHANGE_PAUSED_TO_READY	= (GST_STATE_PAUSED<<3) | GST_STATE_READY,
+  GST_STATE_CHANGE_READY_TO_NULL	= (GST_STATE_READY<<3) | GST_STATE_NULL
 } GstStateChange;
 
 /**
@@ -156,6 +157,7 @@
   GST_ELEMENT_LOCKED_STATE 	= GST_OBJECT_FLAG_LAST,
   GST_ELEMENT_IS_SINK,
   GST_ELEMENT_UNPARENTING,
+  GST_ELEMENT_CHANGING_STATE,
   GST_ELEMENT_FLAG_LAST		= GST_OBJECT_FLAG_LAST + 16
 } GstElementFlags;
 
@@ -274,13 +276,16 @@
  * This function is used by the core.  It is taken while getting or setting
  * the state, during state changes, and while finalizing.
  */
-#define GST_STATE_LOCK(elem)                   g_mutex_lock(GST_STATE_GET_LOCK(elem))
-#define GST_STATE_TRYLOCK(elem)                g_mutex_trylock(GST_STATE_GET_LOCK(elem))
-#define GST_STATE_UNLOCK(elem)                 g_mutex_unlock(GST_STATE_GET_LOCK(elem))
+#define GST_STATE_LOCK(elem)                   g_static_rec_mutex_lock(GST_STATE_GET_LOCK(elem))
+#define GST_STATE_TRYLOCK(elem)                g_static_rec_mutex_trylock(GST_STATE_GET_LOCK(elem))
+#define GST_STATE_UNLOCK(elem)                 g_static_rec_mutex_unlock(GST_STATE_GET_LOCK(elem))
+#define GST_STATE_UNLOCK_FULL(elem)            g_static_rec_mutex_unlock_full(GST_STATE_GET_LOCK(elem))
+#define GST_STATE_LOCK_FULL(elem,t)            g_static_rec_mutex_lock_full(GST_STATE_GET_LOCK(elem), t)
 #define GST_STATE_GET_COND(elem)               (GST_ELEMENT_CAST(elem)->state_cond)
-#define GST_STATE_WAIT(elem)                   g_cond_wait (GST_STATE_GET_COND (elem), GST_STATE_GET_LOCK (elem))
-#define GST_STATE_TIMED_WAIT(elem, timeval)    g_cond_timed_wait (GST_STATE_GET_COND (elem), GST_STATE_GET_LOCK (elem),\
-		                                                timeval)
+#define GST_STATE_WAIT(elem)                   g_static_rec_cond_wait (GST_STATE_GET_COND (elem), \
+							GST_STATE_GET_LOCK (elem))
+#define GST_STATE_TIMED_WAIT(elem, timeval)    g_static_rec_cond_timed_wait (GST_STATE_GET_COND (elem), \
+							GST_STATE_GET_LOCK (elem), timeval)
 #define GST_STATE_SIGNAL(elem)                 g_cond_signal (GST_STATE_GET_COND (elem));
 #define GST_STATE_BROADCAST(elem)              g_cond_broadcast (GST_STATE_GET_COND (elem));
 
@@ -290,17 +295,13 @@
 
   /*< public >*/ /* with STATE_LOCK */
   /* element state */
-  GMutex               *state_lock;
+  GStaticRecMutex      *state_lock;
   GCond                *state_cond;
-  guint8                current_state;
-  guint8                pending_state;
-  guint8                final_state;
-  gboolean              state_error; /* Flag is set when the element has an
-                                      * error in the last state change.
-                                      * It is cleared when doing another
-                                      * state change. */
-  gboolean		no_preroll;  /* Flag is set when the element cannot
-                                      * preroll */
+  GstState              current_state;
+  GstState              next_state;
+  GstState              pending_state;
+  GstStateChangeReturn  last_return;
+
   /*< public >*/ /* with LOCK */
   GstBus	       *bus;
 
@@ -488,7 +489,7 @@
 GstStateChangeReturn	gst_element_set_state		(GstElement *element, GstState state);
 
 void			gst_element_abort_state		(GstElement * element);
-void			gst_element_commit_state	(GstElement * element);
+GstStateChangeReturn	gst_element_commit_state	(GstElement * element);
 void			gst_element_lost_state	        (GstElement * element);
 
 /* factory management */
Index: gst/gstpipeline.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstpipeline.c,v
retrieving revision 1.115
diff -u -u -r1.115 gstpipeline.c
--- gst/gstpipeline.c	8 Oct 2005 18:21:20 -0000	1.115
+++ gst/gstpipeline.c	10 Oct 2005 14:07:23 -0000
@@ -60,12 +60,11 @@
 };
 
 #define DEFAULT_DELAY 0
-#define DEFAULT_PLAY_TIMEOUT  (2*GST_SECOND)
+
 enum
 {
   PROP_0,
   PROP_DELAY,
-  PROP_PLAY_TIMEOUT,
   /* FILL ME */
 };
 
@@ -141,18 +140,13 @@
           "Expected delay needed for elements "
           "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
           G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PLAY_TIMEOUT,
-      g_param_spec_uint64 ("play-timeout", "Play Timeout",
-          "Max timeout for going to PLAYING in nanoseconds", 0, G_MAXUINT64,
-          DEFAULT_PLAY_TIMEOUT, G_PARAM_READWRITE));
 
   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pipeline_dispose);
 
   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_pipeline_send_event);
   gstelement_class->change_state =
       GST_DEBUG_FUNCPTR (gst_pipeline_change_state);
-  gstelement_class->provide_clock =
-      GST_DEBUG_FUNCPTR (gst_pipeline_provide_clock_func);
+  gstelement_class->provide_clock = GST_DEBUG_FUNCPTR (gst_pipeline_provide_clock_func);
 }
 
 static void
@@ -162,7 +156,6 @@
   GstBus *bus;
 
   pipeline->delay = DEFAULT_DELAY;
-  pipeline->play_timeout = DEFAULT_PLAY_TIMEOUT;
 
   bus = g_object_new (gst_bus_get_type (), NULL);
   gst_element_set_bus (GST_ELEMENT_CAST (pipeline), bus);
@@ -192,9 +185,6 @@
     case PROP_DELAY:
       pipeline->delay = g_value_get_uint64 (value);
       break;
-    case PROP_PLAY_TIMEOUT:
-      pipeline->play_timeout = g_value_get_uint64 (value);
-      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -213,9 +203,6 @@
     case PROP_DELAY:
       g_value_set_uint64 (value, pipeline->delay);
       break;
-    case PROP_PLAY_TIMEOUT:
-      g_value_set_uint64 (value, pipeline->play_timeout);
-      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -246,20 +233,20 @@
     gst_element_get_state (element, &state, NULL, &timeout);
     was_playing = state == GST_STATE_PLAYING;
 
-    if (was_playing)
+    if (was_playing) {
       gst_element_set_state (element, GST_STATE_PAUSED);
   }
+  }
 
   res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
 
   if (flush && res) {
     /* need to reset the stream time to 0 after a flushing seek */
     gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
-    if (was_playing) {
+    if (was_playing)
       /* and continue playing */
       gst_element_set_state (element, GST_STATE_PLAYING);
     }
-  }
   return res;
 }
 
@@ -306,7 +293,6 @@
 {
   GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
   GstPipeline *pipeline = GST_PIPELINE (element);
-  GstClockTime play_timeout;
   GstClock *clock;
 
   switch (transition) {
@@ -400,40 +386,6 @@
       GST_UNLOCK (element);
       break;
   }
-
-  if (result == GST_STATE_CHANGE_ASYNC) {
-    GST_LOCK (pipeline);
-    play_timeout = pipeline->play_timeout;
-    GST_UNLOCK (pipeline);
-  } else {
-    play_timeout = 0;
-  }
-
-  /* we wait for async state changes ourselves when we are in an
-   * intermediate state. */
-  if (play_timeout > 0) {
-    GTimeVal *timeval, timeout;
-
-    GST_STATE_UNLOCK (pipeline);
-
-    if (play_timeout == G_MAXUINT64) {
-      timeval = NULL;
-    } else {
-      GST_TIME_TO_TIMEVAL (play_timeout, timeout);
-      timeval = &timeout;
-    }
-
-    result = gst_element_get_state (element, NULL, NULL, timeval);
-    if (result == GST_STATE_CHANGE_ASYNC) {
-      GST_WARNING_OBJECT (pipeline,
-          "timeout in PREROLL, forcing next state change");
-      g_warning ("timeout in PREROLL, forcing next state change");
-      result = GST_STATE_CHANGE_SUCCESS;
-    }
-
-    GST_STATE_LOCK (pipeline);
-  }
-
   return result;
 }
 
@@ -521,8 +473,7 @@
   } else {
     GST_UNLOCK (pipeline);
     clock =
-        GST_ELEMENT_CLASS (parent_class)->
-        provide_clock (GST_ELEMENT (pipeline));
+        GST_ELEMENT_CLASS (parent_class)->provide_clock (GST_ELEMENT (pipeline));
     /* no clock, use a system clock */
     if (!clock) {
       clock = gst_system_clock_obtain ();
Index: gst/gstpipeline.h
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstpipeline.h,v
retrieving revision 1.30
diff -u -u -r1.30 gstpipeline.h
--- gst/gstpipeline.h	27 Sep 2005 20:40:35 -0000	1.30
+++ gst/gstpipeline.h	10 Oct 2005 14:07:23 -0000
@@ -59,7 +59,6 @@
   GstClock      *fixed_clock;	/* fixed clock if any */
   GstClockTime   stream_time;
   GstClockTime   delay;
-  GstClockTime   play_timeout;
 
   /*< private >*/
   gpointer _gst_reserved[GST_PADDING];
Index: gst/gstutils.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstutils.c,v
retrieving revision 1.95
diff -u -u -r1.95 gstutils.c
--- gst/gstutils.c	8 Oct 2005 06:49:09 -0000	1.95
+++ gst/gstutils.c	10 Oct 2005 14:07:23 -0000
@@ -1816,6 +1816,66 @@
   g_free (name);
 }
 
+
+#ifdef GST_COMPILE_STATIC_REC_COND_WAIT
+
+#if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
+# define g_system_thread_equal_simple(thread1, thread2)                 \
+   ((thread1).dummy_pointer == (thread2).dummy_pointer)
+# define g_system_thread_assign(dest, src)                              \
+   ((dest).dummy_pointer = (src).dummy_pointer)
+#else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
+# define g_system_thread_equal_simple(thread1, thread2)                 \
+   (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
+# define g_system_thread_assign(dest, src)                              \
+   (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
+#endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
+
+#define g_system_thread_equal(thread1, thread2)                         \
+  (g_thread_functions_for_glib_use.thread_equal ?                       \
+   g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
+   g_system_thread_equal_simple((thread1), (thread2)))
+
+static GSystemThread zero_thread;
+
+gboolean 
+g_static_rec_cond_timed_wait (GCond *cond, 
+		GStaticRecMutex *mutex, GTimeVal *end_time)
+{
+  GMutex *smutex;
+  guint depth;
+  GSystemThread self;
+  gboolean res;
+
+  if (!g_thread_supported ())
+    return FALSE;
+
+  G_THREAD_UF (thread_self, (&self));
+
+  g_return_val_if_fail (g_system_thread_equal (self, mutex->owner), FALSE);
+
+  depth = mutex->depth;
+
+  g_system_thread_assign (mutex->owner, zero_thread);
+  mutex->depth = 0;
+  smutex = g_static_mutex_get_mutex (&mutex->mutex);
+
+  res = g_cond_timed_wait (cond, smutex, end_time);
+
+  g_system_thread_assign (mutex->owner, self);
+  mutex->depth = depth;
+
+  return res;
+}
+
+void
+g_static_rec_cond_wait (GCond *cond, GStaticRecMutex *mutex)
+{
+  g_static_rec_cond_timed_wait (cond, mutex, NULL);
+}
+
+#endif /* GST_COMPILE_STATIC_REC_COND_WAIT */
+
 /**
  * gst_bin_add_many:
  * @bin: the bin to add the elements to
@@ -1873,154 +1933,6 @@
 }
 
 static void
-get_state_func (GstElement * element, gpointer unused)
-{
-  GstStateChangeReturn ret = GST_STATE_CHANGE_ASYNC;
-
-  GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-      "new thread waiting on state change");
-
-  /* wait indefinitely */
-  while (ret == GST_STATE_CHANGE_ASYNC)
-    ret = gst_element_get_state (element, NULL, NULL, NULL);
-
-  GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-      "thread done waiting on state change");
-
-  gst_object_unref (element);
-}
-
-/**
- * gst_bin_watch_for_state_change:
- * @bin: the bin to watch for state changes
- *
- * Spawns a thread calling gst_element_get_state on @bin with infinite timeout.
- *
- * In practice this is done because if a bin returns %GST_STATE_CHANGE_ASYNC
- * from a state change, it will not commit its state until someone calls
- * gst_element_get_state() on it. Thus having another thread checking the bin's
- * state will ensure that a state-changed message gets posted on the bus
- * eventually.
- *
- * This function is admittedly a bit of a hack. Bins should always post
- * messages. However this behavior was broken out into this function to avoid
- * spawning threads when scrubbing, when the bin's state is changing quickly and
- * asynchronously.
- */
-void
-gst_bin_watch_for_state_change (GstBin * bin)
-{
-  static GThreadPool *pool = NULL;
-  static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
-
-  g_static_mutex_lock (&mutex);
-  if (pool == NULL)
-    pool = g_thread_pool_new ((GFunc) get_state_func, NULL, -1, FALSE, NULL);
-  g_static_mutex_unlock (&mutex);
-
-  g_thread_pool_push (pool, gst_object_ref (bin), NULL);
-}
-
-struct _GstAsyncThreadData
-{
-  GstElement *element;
-  GstState state;
-};
-
-static void
-set_state_async_thread_func (struct _GstAsyncThreadData *data, gpointer unused)
-{
-  GstState current, pending;
-  GstStateChangeReturn ret = GST_STATE_CHANGE_ASYNC;
-
-  GST_CAT_INFO_OBJECT (GST_CAT_STATES, data->element,
-      "new thread ensuring state change to %s",
-      gst_element_state_get_name (data->state));
-
-  while (TRUE) {
-    /* wait indefinitely */
-    ret = gst_element_get_state (data->element, &current, &pending, NULL);
-    GST_CAT_INFO_OBJECT (GST_CAT_STATES, data->element,
-        "get_state returned %d, current %s, pending %s", ret,
-        gst_element_state_get_name (current),
-        gst_element_state_get_name (pending));
-
-    /* can only be SUCCESS or FAILURE */
-    if (ret == GST_STATE_CHANGE_FAILURE) {
-      /* we can only break, hopefully an error message was posted as well */
-      GST_CAT_INFO_OBJECT (GST_CAT_STATES, data->element,
-          "FAILURE during state change");
-      break;
-    } else if (ret == GST_STATE_CHANGE_SUCCESS) {
-      if (current == data->state) {
-        GST_CAT_INFO_OBJECT (GST_CAT_STATES, data->element,
-            "successfully reached final state");
-        break;
-      }
-      GST_CAT_INFO_OBJECT (GST_CAT_STATES, data->element,
-          "setting target state %s again",
-          gst_element_state_get_name (data->state));
-      gst_element_set_state (data->element, data->state);
-    } else {
-      g_assert_not_reached ();
-    }
-  }
-
-  GST_CAT_INFO_OBJECT (GST_CAT_STATES, data->element,
-      "thread done waiting on state change");
-
-  gst_object_unref (data->element);
-  g_free (data);
-}
-
-/**
- * gst_element_set_state_async:
- * @element: a #GstElement to change state of
- * @state: the element's new #GstState
- *
- * Sets the state of the element. This function will try to set the
- * requested state by going through all the intermediary states and calling
- * the class's state change function for each.  If the state change returns
- * #GST_STATE_CHANGE_ASYNC at any time, a thread will be started to
- * monitor the state change and make sure the element is brought to the
- * requested state.
- *
- * Returns: Result of the state change using #GstStateChangeReturn.
- *
- * MT safe.
- */
-GstStateChangeReturn
-gst_element_set_state_async (GstElement * element, GstState state)
-{
-  GstStateChangeReturn ret;
-  struct _GstAsyncThreadData *data;
-
-  ret = gst_element_set_state (element, state);
-  if (ret == GST_STATE_CHANGE_ASYNC) {
-    static GThreadPool *pool = NULL;
-    static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
-
-    GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
-        "starting new thread to ensure state change to %s",
-        gst_element_state_get_name (state));
-    g_static_mutex_lock (&mutex);
-    if (pool == NULL)
-      pool = g_thread_pool_new ((GFunc) set_state_async_thread_func,
-          NULL, -1, FALSE, NULL);
-    g_static_mutex_unlock (&mutex);
-
-    data = g_new0 (struct _GstAsyncThreadData, 1);
-
-    gst_object_ref (element);
-    data->element = element;
-    data->state = state;
-    g_thread_pool_push (pool, data, NULL);
-  }
-
-  return ret;
-}
-
-static void
 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
     guint arg_id, GParamFlags flags)
 {
Index: gst/gstutils.h
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstutils.h,v
retrieving revision 1.53
diff -u -u -r1.53 gstutils.h
--- gst/gstutils.h	8 Oct 2005 14:48:17 -0000	1.53
+++ gst/gstutils.h	10 Oct 2005 14:07:24 -0000
@@ -457,6 +457,15 @@
 void			gst_object_default_error	(GstObject * source,
 							 GError * error, gchar * debug);
 
+/* rec mutex */
+#ifndef g_static_rec_cond_wait
+#define GST_COMPILE_STATIC_REC_COND_WAIT
+void     		g_static_rec_cond_wait        	(GCond                *cond,
+		                                         GStaticRecMutex      *mutex);
+gboolean 		g_static_rec_cond_timed_wait  	(GCond                *cond,
+		                                         GStaticRecMutex      *mutex,
+						         GTimeVal             *end_time);
+#endif
 
 /* element functions */
 GstFlowReturn		gst_element_abort_preroll	(GstElement *element);
@@ -488,8 +497,6 @@
 gboolean		gst_element_link_pads_filtered	(GstElement * src, const gchar * srcpadname,
                                                          GstElement * dest, const gchar * destpadname,
                                                          GstCaps *filter);
-GstStateChangeReturn    gst_element_set_state_async     (GstElement * element, GstState state);
-
 /* util elementfactory functions */
 gboolean		gst_element_factory_can_src_caps(GstElementFactory *factory, const GstCaps *caps);
 gboolean		gst_element_factory_can_sink_caps(GstElementFactory *factory, const GstCaps *caps);
@@ -527,7 +534,6 @@
 /* bin functions */
 void			gst_bin_add_many                (GstBin *bin, GstElement *element_1, ...);
 void			gst_bin_remove_many             (GstBin *bin, GstElement *element_1, ...);
-void			gst_bin_watch_for_state_change  (GstBin *bin);
 
 /* buffer functions */
 GstBuffer *		gst_buffer_merge		(GstBuffer * buf1, GstBuffer * buf2);
Index: gst/base/gstbasesink.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/base/gstbasesink.c,v
retrieving revision 1.63
diff -u -u -r1.63 gstbasesink.c
--- gst/base/gstbasesink.c	8 Oct 2005 14:57:09 -0000	1.63
+++ gst/base/gstbasesink.c	10 Oct 2005 14:07:24 -0000
@@ -622,6 +622,7 @@
 
   if (length == 1) {
     gint t;
+    GstTask *task;
 
     basesink->have_preroll = TRUE;
     /* we are prerolling */
@@ -636,8 +637,21 @@
       g_warning ("STREAM_LOCK should have been locked !!");
     }
 
-    /* now we commit our state */
+    /* now we commit our state, this will also automatically proceed to
+     * the next pending state. */
+    /* FIXME */
+    if ((task = GST_PAD_TASK (pad))) {
+      while (!GST_STATE_TRYLOCK (basesink)) {
+        GST_DEBUG_OBJECT (basesink, "state change happening, checking shutdown");
+	GST_LOCK (pad);
+        if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
+          goto task_stopped;
+	GST_UNLOCK (pad);
+      }
+    }
+    else {
     GST_STATE_LOCK (basesink);
+    }
     GST_DEBUG_OBJECT (basesink, "commit state");
     gst_element_commit_state (GST_ELEMENT (basesink));
     GST_STATE_UNLOCK (basesink);
@@ -742,6 +756,12 @@
 
     return ret;
   }
+task_stopped:
+  {
+    GST_UNLOCK (pad);
+    GST_DEBUG_OBJECT (basesink, "task is stopped");
+    return GST_FLOW_WRONG_STATE;
+  }
 flushing:
   {
     GST_UNLOCK (pad);
@@ -1514,7 +1534,7 @@
        * we need to wait for a preroll */
       GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
           basesink->have_preroll, basesink->eos);
-      if (!basesink->have_preroll && !basesink->eos) {
+      if (!basesink->have_preroll && !basesink->eos && GST_STATE_PENDING (basesink) == GST_STATE_PAUSED) {
         GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
         basesink->need_preroll = TRUE;
         ret = GST_STATE_CHANGE_ASYNC;
Index: tests/sched/Makefile.am
===================================================================
RCS file: /cvs/gstreamer/gstreamer/tests/sched/Makefile.am,v
retrieving revision 1.11
diff -u -u -r1.11 Makefile.am
--- tests/sched/Makefile.am	4 May 2004 12:38:35 -0000	1.11
+++ tests/sched/Makefile.am	10 Oct 2005 14:07:24 -0000
@@ -1,7 +1,7 @@
 if GST_DISABLE_LOADSAVE
 noinst_PROGRAMS =
 else
-noinst_PROGRAMS = runxml dynamic-pipeline sched-stress interrupt1 interrupt2 interrupt3
+noinst_PROGRAMS = runxml dynamic-pipeline sched-stress interrupt1 interrupt2 interrupt3 sort
 endif
 
 dynamic_pipeline_SOURCES = dynamic-pipeline.c 
Index: tools/gst-launch.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/tools/gst-launch.c,v
retrieving revision 1.90
diff -u -u -r1.90 gst-launch.c
--- tools/gst-launch.c	8 Oct 2005 12:36:36 -0000	1.90
+++ tools/gst-launch.c	10 Oct 2005 14:07:24 -0000
@@ -617,8 +617,12 @@
       pipeline = real_pipeline;
     }
 
+#if 0
     fprintf (stderr, _("PAUSE pipeline ...\n"));
     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
+#endif
+    fprintf (stderr, _("PLAYING pipeline ...\n"));
+    ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
 
     switch (ret) {
       case GST_STATE_CHANGE_FAILURE:
