Index: gst/matroska/matroska-mux.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-good/gst/matroska/matroska-mux.c,v
retrieving revision 1.52
diff -u -u -r1.52 matroska-mux.c
--- gst/matroska/matroska-mux.c	24 Dec 2006 11:24:59 -0000	1.52
+++ gst/matroska/matroska-mux.c	23 Apr 2007 14:36:00 -0000
@@ -1651,11 +1651,14 @@
     }
 
     /* if we have a buffer check if it is better then the current best one */
+    /* FIXXXX */
     if (collect_pad->buffer != NULL) {
       if (best == NULL || !GST_BUFFER_TIMESTAMP_IS_VALID (collect_pad->buffer)
           || (GST_BUFFER_TIMESTAMP_IS_VALID (best->buffer)
-              && GST_BUFFER_TIMESTAMP (collect_pad->buffer) <
-              GST_BUFFER_TIMESTAMP (best->buffer))) {
+              && GST_BUFFER_TIMESTAMP (collect_pad->buffer) 
+                   - best->collect.segment.start 
+                   < GST_BUFFER_TIMESTAMP (best->buffer) 
+                       - best->collect.segment.start)) {
         best = collect_pad;
       }
     }
@@ -1735,7 +1738,8 @@
   }
 
   /* set the timestamp for outgoing buffers */
-  ebml->timestamp = GST_BUFFER_TIMESTAMP (buf);
+  ebml->timestamp = GST_BUFFER_TIMESTAMP (buf) - 
+      collect_pad->collect.segment.start;
 
   if (collect_pad->track->type == GST_MATROSKA_TRACK_TYPE_VIDEO &&
       !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
@@ -1746,7 +1750,7 @@
 
   if (mux->cluster) {
     /* start a new cluster every two seconds or at keyframe */
-    if (mux->cluster_time + GST_SECOND * 2 < GST_BUFFER_TIMESTAMP (buf)
+    if (mux->cluster_time + GST_SECOND * 2 < ebml->timestamp
         || is_video_keyframe) {
       GstMatroskaMetaSeekIndex *idx;
 
@@ -1755,8 +1759,8 @@
       mux->cluster =
           gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_CLUSTER);
       gst_ebml_write_uint (ebml, GST_MATROSKA_ID_CLUSTERTIMECODE,
-          GST_BUFFER_TIMESTAMP (buf) / mux->time_scale);
-      mux->cluster_time = GST_BUFFER_TIMESTAMP (buf);
+          ebml->timestamp / mux->time_scale);
+      mux->cluster_time = ebml->timestamp;
 
       if (mux->num_meta_indexes % 32 == 0) {
         mux->meta_index = g_renew (GstMatroskaMetaSeekIndex, mux->meta_index,
@@ -1773,8 +1777,8 @@
     mux->cluster_pos = ebml->pos;
     mux->cluster = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_CLUSTER);
     gst_ebml_write_uint (ebml, GST_MATROSKA_ID_CLUSTERTIMECODE,
-        GST_BUFFER_TIMESTAMP (buf) / mux->time_scale);
-    mux->cluster_time = GST_BUFFER_TIMESTAMP (buf);
+        ebml->timestamp / mux->time_scale);
+    mux->cluster_time = ebml->timestamp;
 
     if (mux->num_meta_indexes % 32 == 0) {
       mux->meta_index = g_renew (GstMatroskaMetaSeekIndex, mux->meta_index,
@@ -1805,7 +1809,7 @@
     idx = &mux->index[mux->num_indexes++];
 
     idx->pos = mux->cluster_pos;
-    idx->time = GST_BUFFER_TIMESTAMP (buf);
+    idx->time = ebml->timestamp;
     idx->track = collect_pad->track->num;
   } else if ((collect_pad->track->type == GST_MATROSKA_TRACK_TYPE_AUDIO) &&
       (mux->num_streams == 1)) {
@@ -1818,7 +1822,7 @@
     idx = &mux->index[mux->num_indexes++];
 
     idx->pos = mux->cluster_pos;
-    idx->time = GST_BUFFER_TIMESTAMP (buf);
+    idx->time = ebml->timestamp;
     idx->track = collect_pad->track->num;
   }
 
@@ -1834,7 +1838,7 @@
   /* write the block, for matroska v2 use SimpleBlock if possible
    * one slice (*breath*).
    * FIXME: lacing, etc. */
-  relative_timestamp64 = GST_BUFFER_TIMESTAMP (buf) - mux->cluster_time;
+  relative_timestamp64 = ebml->timestamp - mux->cluster_time;
   if (relative_timestamp64 >= 0) {
     /* round the timestamp */
     relative_timestamp64 += mux->time_scale / 2;
