Index: gst-plugins-good/gst/matroska/matroska-mux.c
===================================================================
RCS file: /usr/local/cvs/linux/libs/gst-plugins-good/gst-plugins-good/gst/matroska/matroska-mux.c,v
retrieving revision 1.5
retrieving revision 1.5.8.2
diff -u -r1.5 -r1.5.8.2
--- gst-plugins-good/gst/matroska/matroska-mux.c	25 Feb 2009 16:56:23 -0000	1.5
+++ gst-plugins-good/gst/matroska/matroska-mux.c	22 Sep 2009 10:10:44 -0000	1.5.8.2
@@ -215,7 +215,7 @@
     guint prop_id, GValue * value, GParamSpec * pspec);
 
 /* reset muxer */
-static void gst_matroska_mux_reset (GstElement * element);
+static void gst_matroska_mux_reset (GstElement * element, gboolean reset_pad);
 
 /* uid generation */
 static guint64 gst_matroska_mux_create_uid ();
@@ -322,7 +322,7 @@
   mux->num_v_streams = 0;
 
   /* initialize remaining variables */
-  gst_matroska_mux_reset (GST_ELEMENT (mux));
+  gst_matroska_mux_reset (GST_ELEMENT (mux), TRUE);
 }
 
 
@@ -478,7 +478,7 @@
  * Reset matroska muxer back to initial state.
  */
 static void
-gst_matroska_mux_reset (GstElement * element)
+gst_matroska_mux_reset (GstElement * element, gboolean reset_pad)
 {
   GstMatroskaMux *mux = GST_MATROSKA_MUX (element);
   GSList *walk;
@@ -499,7 +499,8 @@
     thepad = collect_pad->collect.pad;
 
     /* reset collect pad to pristine state */
-    gst_matroska_pad_reset (collect_pad, FALSE);
+    if (reset_pad)
+      gst_matroska_pad_reset (collect_pad, FALSE);
   }
 
   /* reset indexes */
@@ -520,6 +521,9 @@
   gst_tag_setter_reset_tags (GST_TAG_SETTER (mux));
 }
 
+static void
+gst_matroska_mux_finish (GstMatroskaMux * mux);
+
 /**
  * gst_matroska_mux_handle_src_event:
  * @pad: Pad which received the event.
@@ -533,6 +537,8 @@
 gst_matroska_mux_handle_src_event (GstPad * pad, GstEvent * event)
 {
   GstEventType type;
+  GstMatroskaMux * mux;
+  const GstStructure *evn_struct;
 
   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
 
@@ -540,6 +546,28 @@
     case GST_EVENT_SEEK:
       /* disable seeking for now */
       return FALSE;
+    case GST_EVENT_CUSTOM_BOTH:
+      evn_struct = gst_event_get_structure (event);
+
+      if (evn_struct && gst_structure_has_name (evn_struct,
+          "FinalizeMatroskaFile")) {
+        /* finalizing file and resetting matroska muxer */
+        mux = GST_MATROSKA_MUX (gst_pad_get_parent (pad));
+
+        /* finish current matroska file */
+        gst_matroska_mux_finish (mux);
+
+        /* initialize internal variables */
+        mux->index = NULL;
+        mux->num_streams = 0;
+        mux->num_a_streams = 0;
+        mux->num_t_streams = 0;
+        mux->num_v_streams = 0;
+
+        /* reset matroska muxer, but not its collect pad */
+        gst_matroska_mux_reset (GST_ELEMENT (mux), FALSE);
+      }
+      return TRUE;
     default:
       break;
   }
@@ -2436,7 +2464,7 @@
     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
       break;
     case GST_STATE_CHANGE_PAUSED_TO_READY:
-      gst_matroska_mux_reset (GST_ELEMENT (mux));
+      gst_matroska_mux_reset (GST_ELEMENT (mux), TRUE);
       break;
     case GST_STATE_CHANGE_READY_TO_NULL:
       break;
