diff -u -p -r1.76 gstoggmux.c
--- ext/ogg/gstoggmux.c	14 Jun 2006 09:13:54 -0000	1.76
+++ ext/ogg/gstoggmux.c	5 Jul 2006 10:23:32 -0000
@@ -65,8 +65,10 @@ GstOggPadState;
 typedef struct
 {
   GstCollectData collect;       /* we extend the CollectData */
+  gboolean removed;             /* whether the pad have been removed */
 
   GstBuffer *buffer;            /* the queued buffer for this pad */
+  GstBuffer *next_buffer;       /* and the one after that */
 
   gint serial;
   ogg_stream_state stream;
@@ -105,6 +107,9 @@ struct _GstOggMux
   /* sinkpads */
   GstCollectPads *collect;
 
+  /* number of active pads */
+  gint active_pads;
+
   /* the pad we are currently using to fill a page */
   GstOggPad *pulling;
 
@@ -393,6 +398,7 @@ gst_ogg_mux_request_new_pad (GstElement 
       oggpad = (GstOggPad *)
           gst_collect_pads_add_pad (ogg_mux->collect, newpad,
           sizeof (GstOggPad));
+      g_atomic_int_inc (&ogg_mux->active_pads);
 
       oggpad->serial = serial;
       ogg_stream_init (&oggpad->stream, serial);
@@ -436,32 +442,18 @@ gst_ogg_mux_release_pad (GstElement * el
 
   ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));
 
-  /* FIXME: When a request pad is released while paused or playing, 
-   * we probably need to do something to finalise its stream in the
-   * ogg data we're producing, but I'm not sure what */
-
   /* Find out GstOggPad in the collect pads info and clean it up */
 
   GST_OBJECT_LOCK (ogg_mux->collect);
   for (walk = ogg_mux->collect->data; walk; walk = g_slist_next (walk)) {
     GstOggPad *oggpad = (GstOggPad *) walk->data;
     GstCollectData *cdata = (GstCollectData *) walk->data;
-    GstBuffer *buf;
 
     if (cdata->pad == pad) {
-      ogg_stream_clear (&oggpad->stream);
-
-      while ((buf = g_queue_pop_head (oggpad->pagebuffers)) != NULL) {
-        gst_buffer_unref (buf);
-      }
-
-      g_queue_free (oggpad->pagebuffers);
-      oggpad->pagebuffers = NULL;
+      oggpad->removed = TRUE;
     }
   }
   GST_OBJECT_UNLOCK (ogg_mux->collect);
-
-  gst_collect_pads_remove_pad (ogg_mux->collect, pad);
 }
 
 /* handle events */
@@ -692,18 +684,24 @@ gst_ogg_mux_compare_pads (GstOggMux * og
 
   /* if the first pad doesn't contain anything or is even NULL, return
    * the second pad as best candidate and vice versa */
-  if (first == NULL || first->buffer == NULL)
+  if (first == NULL || (first->buffer == NULL && first->next_buffer == NULL))
     return 1;
-  if (second == NULL || second->buffer == NULL)
+  if (second == NULL || (second->buffer == NULL && second->next_buffer == NULL))
     return -1;
 
   /* no timestamp on first buffer, it must go first */
-  firsttime = GST_BUFFER_TIMESTAMP (first->buffer);
+  if (first->buffer)
+    firsttime = GST_BUFFER_TIMESTAMP (first->buffer);
+  else
+    firsttime = GST_BUFFER_TIMESTAMP (first->next_buffer);
   if (firsttime == GST_CLOCK_TIME_NONE)
     return -1;
 
   /* no timestamp on second buffer, it must go first */
-  secondtime = GST_BUFFER_TIMESTAMP (second->buffer);
+  if (second->buffer)
+    secondtime = GST_BUFFER_TIMESTAMP (second->buffer);
+  else
+    secondtime = GST_BUFFER_TIMESTAMP (second->next_buffer);
   if (secondtime == GST_CLOCK_TIME_NONE)
     return 1;
 
@@ -726,9 +724,19 @@ gst_ogg_mux_compare_pads (GstOggMux * og
   return 0;
 }
 
-/* make sure a buffer is queued on all pads, returns a pointer to an oggpad
- * that holds the best buffer or NULL when no pad was usable.
- * "best" means the buffer marked with the lowest timestamp */
+/* make sure at least one buffer is queued on all pads, two if possible
+ * 
+ * if pad->buffer == NULL, pad->next_buffer !=  NULL, then
+ *   we do not know if the buffer is the last or not
+ * if pad->buffer != NULL, pad->next_buffer != NULL, then
+ *   pad->buffer is not the last buffer for the pad
+ * if pad->buffer != NULL, pad->next_buffer == NULL, then
+ *   pad->buffer if the last buffer for the pad
+ * 
+ * returns a pointer to an oggpad that holds the best buffer or
+ * NULL when no pad was usable. "best" means the buffer marked
+ * with the lowest timestamp. If best->buffer == NULL then nothing
+ * should be done until more data arrives */
 static GstOggPad *
 gst_ogg_mux_queue_pads (GstOggMux * ogg_mux)
 {
@@ -753,6 +761,28 @@ gst_ogg_mux_queue_pads (GstOggMux * ogg_
       GstBuffer *buf;
       gboolean incaps;
 
+      if (pad->removed) {
+        ogg_stream_clear (&oggpad->stream);
+
+        while ((buf = g_queue_pop_head (pad->pagebuffers)) != NULL) {
+          gst_buffer_unref (buf);
+        }
+
+        g_queue_free (pad->pagebuffers);
+        pad->pagebuffers = NULL;
+
+        /* we can free it now */
+        gst_collect_pads_remove_pad (ogg_mux->collect, data->pad);
+        continue;
+      }
+
+      /* shift the buffer along if needed (and is okay if the next_buffer is NULL) */
+      if (pad->buffer == NULL) {
+        GST_LOG_OBJECT (data->pad, "shifting buffer %" GST_PTR_FORMAT, pad->next_buffer);
+        pad->buffer = pad->next_buffer;
+	pad->next_buffer = NULL;
+      }
+
       buf = gst_collect_pads_pop (ogg_mux->collect, data);
       GST_LOG_OBJECT (data->pad, "popped buffer %" GST_PTR_FORMAT, buf);
 
@@ -781,6 +811,9 @@ gst_ogg_mux_queue_pads (GstOggMux * ogg_
           ogg_page page;
           GstFlowReturn ret;
 
+          /* it's no longer active */
+          g_atomic_int_dec_and_test (&ogg_mux->active_pads);
+
           /* Just gone to EOS. Flush existing page(s) */
           pad->eos = TRUE;
 
@@ -796,14 +829,15 @@ gst_ogg_mux_queue_pads (GstOggMux * ogg_
         }
       }
 
-      pad->buffer = buf;
+      pad->next_buffer = buf;
     }
 
     /* we should have a buffer now, see if it is the best pad to
      * pull on */
-    if (pad->buffer) {
+    if (pad->buffer || pad->next_buffer) {
       if (gst_ogg_mux_compare_pads (ogg_mux, bestpad, pad) > 0) {
-        GST_LOG_OBJECT (data->pad, "new best pad");
+        GST_LOG_OBJECT (data->pad, "new best pad, with buffers %" GST_PTR_FORMAT " and %" GST_PTR_FORMAT,
+			pad->buffer, pad->next_buffer);
 
         bestpad = pad;
       }
@@ -947,7 +981,7 @@ gst_ogg_mux_send_headers (GstOggMux * mu
     GST_LOG_OBJECT (mux, "looking at pad %s:%s", GST_DEBUG_PAD_NAME (thepad));
 
     /* if the pad has no buffer, we don't care */
-    if (pad->buffer == NULL)
+    if (pad->buffer == NULL && pad->next_buffer == NULL)
       continue;
 
     /* now figure out the headers */
@@ -983,6 +1017,9 @@ gst_ogg_mux_send_headers (GstOggMux * mu
     } else if (pad->buffer) {
       buf = pad->buffer;
       gst_buffer_ref (buf);
+    } else if (pad->next_buffer) {
+      buf = pad->next_buffer;
+      gst_buffer_ref (buf);
     } else {
       /* fixme -- should be caught in the previous list traversal. */
       GST_OBJECT_LOCK (pad);
@@ -1130,10 +1167,8 @@ gst_ogg_mux_send_headers (GstOggMux * mu
  * 3) before filling a page, read ahead one more buffer to see if this
  *    packet is the last of the stream. We need to do this because the ogg
  *    spec mandates that the last packet should have the EOS flag set before
- *    sending it to ogg. FIXME: Apparently we're allowed to send empty 'nil'
- *    pages with the EOS flag set for EOS, so we could do this. Not sure how
- *    that works, though. TODO: 'read ahead one more buffer' is a bit funky
- *    with collectpads. Rethink this.
+ *    sending it to ogg. if pad->buffer is NULL we need to wait to find out
+ *    whether there are any more buffers.`
  * 4) pages get queued on a per-pad queue. Every time a page is queued, a
  *    dequeue is called, which will dequeue the oldest page on any pad, provided
  *    that ALL pads have at least one marked page in the queue (or remaining
@@ -1169,6 +1204,12 @@ gst_ogg_mux_collected (GstCollectPads * 
       ", currently pulling from %" GST_PTR_FORMAT, best->collect.pad,
       ogg_mux->pulling);
 
+  /* best->buffer is non-NULL, either the pad is EOS's or there is a next buffer */
+  if (best->next_buffer == NULL && !best->eos) {
+    GST_WARNING_OBJECT (ogg_mux, "no subsequent buffer and EOS not reached");
+    return GST_FLOW_WRONG_STATE;
+  }
+
   /* if we were already pulling from one pad, but the new "best" buffer is
    * from another pad, we need to check if we have reason to flush a page
    * for the pad we were pulling from before */
@@ -1270,7 +1311,7 @@ gst_ogg_mux_collected (GstCollectPads * 
         " packet %" G_GINT64_FORMAT " (%ld bytes) created from buffer",
         packet.granulepos, packet.packetno, packet.bytes);
 
-    packet.e_o_s = 0;
+    packet.e_o_s = (pad->eos ? 1 : 0);
     tmpbuf = NULL;
 
     /* we flush when we see a new keyframe */
@@ -1431,7 +1472,13 @@ gst_ogg_mux_collected (GstCollectPads * 
     }
   }
 
-  return GST_FLOW_OK;
+  /* if all the pads have been removed, recurse to clear data */
+  if (g_atomic_int_get (&ogg_mux->active_pads) > 0) {
+    return GST_FLOW_OK;
+  } else {
+    GST_LOG_OBJECT (ogg_mux, "no pads remaining, recursing to flush data");
+    return gst_ogg_mux_collected (pads, ogg_mux);
+  }
 }
 
 static void
