diff -u -p -r1.76 gstoggmux.c
--- ext/ogg/gstoggmux.c	14 Jun 2006 09:13:54 -0000	1.76
+++ ext/ogg/gstoggmux.c	23 Jun 2006 08:12:27 -0000
@@ -67,6 +67,7 @@ typedef struct
   GstCollectData collect;       /* we extend the CollectData */
 
   GstBuffer *buffer;            /* the queued buffer for this pad */
+  GstBuffer *next_buffer;       /* and the one after that */
 
   gint serial;
   ogg_stream_state stream;
@@ -692,18 +693,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 && first->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->next_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 +733,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 +770,12 @@ gst_ogg_mux_queue_pads (GstOggMux * ogg_
       GstBuffer *buf;
       gboolean incaps;
 
+      /* shift the buffer along if needed (and is okay if the next_buffer is NULL) */
+      if (pad->buffer == NULL) {
+        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);
 
@@ -796,12 +819,12 @@ 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");
 
@@ -947,7 +970,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 +1006,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 +1156,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 +1193,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 +1300,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 */
