Index: gst/gstscheduler.c
===================================================================
RCS file: /cvs/gstreamer/gstreamer/gst/gstscheduler.c,v
retrieving revision 1.103
diff -u -B -b -p -r1.103 gstscheduler.c
--- gst/gstscheduler.c	12 Aug 2004 10:56:40 -0000	1.103
+++ gst/gstscheduler.c	2 Nov 2004 12:30:13 -0000
@@ -215,6 +215,7 @@ void
 gst_scheduler_add_element (GstScheduler * sched, GstElement * element)
 {
   GstSchedulerClass *sclass;
+  gboolean redistribute_clock = FALSE;
 
   g_return_if_fail (GST_IS_SCHEDULER (sched));
   g_return_if_fail (GST_IS_ELEMENT (element));
@@ -233,15 +234,24 @@ gst_scheduler_add_element (GstScheduler 
     sched->clock_providers = g_list_prepend (sched->clock_providers, element);
     GST_CAT_DEBUG (GST_CAT_CLOCK, "added clock provider %s",
         GST_ELEMENT_NAME (element));
+    redistribute_clock = TRUE;
   }
   if (gst_element_requires_clock (element)) {
     sched->clock_receivers = g_list_prepend (sched->clock_receivers, element);
     GST_CAT_DEBUG (GST_CAT_CLOCK, "added clock receiver %s",
         GST_ELEMENT_NAME (element));
+    redistribute_clock = TRUE;
   }
 
   gst_element_set_scheduler (element, sched);
 
+  if (redistribute_clock) {
+    GstClock *clock;
+
+    clock = gst_scheduler_get_clock (sched);
+    gst_scheduler_set_clock (sched, clock);
+  }
+
   sclass = GST_SCHEDULER_GET_CLASS (sched);
 
   if (sclass->add_element)
@@ -259,12 +269,33 @@ void
 gst_scheduler_remove_element (GstScheduler * sched, GstElement * element)
 {
   GstSchedulerClass *sclass;
+  GList *link;
+  gboolean redistribute_clock = FALSE;
 
   g_return_if_fail (GST_IS_SCHEDULER (sched));
   g_return_if_fail (GST_IS_ELEMENT (element));
 
-  sched->clock_providers = g_list_remove (sched->clock_providers, element);
-  sched->clock_receivers = g_list_remove (sched->clock_receivers, element);
+  link = g_list_find (sched->clock_providers, element);
+  if (link) {
+    sched->clock_providers = g_list_delete_link (sched->clock_providers, link);
+    GST_CAT_DEBUG (GST_CAT_CLOCK, "removed clock provider %s",
+        GST_ELEMENT_NAME (element));
+    redistribute_clock = TRUE;
+  }
+  link = g_list_find (sched->clock_receivers, element);
+  if (link) {
+    sched->clock_receivers = g_list_delete_link (sched->clock_receivers, link);
+    GST_CAT_DEBUG (GST_CAT_CLOCK, "removed clock receiver %s",
+        GST_ELEMENT_NAME (element));
+    redistribute_clock = TRUE;
+  }
+
+  if (redistribute_clock) {
+    GstClock *clock;
+
+    clock = gst_scheduler_get_clock (sched);
+    gst_scheduler_set_clock (sched, clock);
+  }
 
   sclass = GST_SCHEDULER_GET_CLASS (sched);
 
@@ -295,6 +326,8 @@ gst_scheduler_state_transition (GstSched
   g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_FAILURE);
 
   if (element == sched->parent && sched->parent_sched == NULL) {
+    /* FIXME is distributing the clock in the state change still needed
+     * when we distribute as soon as we add/remove elements? I think not.*/
     switch (transition) {
       case GST_STATE_READY_TO_PAUSED:
       {
