Index: gstasfdemux.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-ugly/gst/asfdemux/gstasfdemux.c,v
retrieving revision 1.121
diff -a -u -r1.121 gstasfdemux.c
--- gstasfdemux.c	15 Feb 2007 19:55:55 -0000	1.121
+++ gstasfdemux.c	7 Mar 2007 18:33:56 -0000
@@ -439,7 +439,6 @@
 
   /* process pending stream objects and create pads for those */
   gst_asf_demux_process_queued_extended_stream_objects (demux);
-  gst_element_no_more_pads (GST_ELEMENT (demux));
 
   if (demux->num_streams == 0)
     goto no_streams;
@@ -825,7 +824,7 @@
 
   for (i = 0; i < demux->num_streams; i++) {
     stream = &demux->stream[i];
-    if (stream->id == id) {
+    if ((stream->id & 0x7fff) == id) {
       /* We've found the one with the matching id */
       return &demux->stream[i];
     }
@@ -872,7 +871,7 @@
   stream = &demux->stream[demux->num_streams];
   stream->caps = caps;
   stream->pad = src_pad;
-  stream->id = id;
+  stream->id = id | 0x8000; /* mark stream as not activated */
   stream->frag_offset = 0;
   stream->sequence = 0;
   stream->delay = 0;
@@ -889,8 +888,6 @@
       GST_PAD_NAME (src_pad), demux->num_streams, caps);
 
   ++demux->num_streams;
-  gst_pad_set_active (src_pad, TRUE);
-  gst_element_add_pad (GST_ELEMENT (demux), src_pad);
 }
 
 static void
@@ -2182,6 +2179,20 @@
 {
   GstFlowReturn ret;
 
+  /* Check if pads needs to be added */
+  if (stream->id & 0x8000) {
+    int n;
+    gst_pad_set_active (stream->pad, TRUE);
+    gst_element_add_pad (GST_ELEMENT (demux), stream->pad);
+    stream->id &= 0x7fff;
+    /* Check for no more pads */
+    for (n = 0; n < demux->num_streams; n++)
+      if (demux->stream[n].id & 0x8000)
+        break;
+    if (n == demux->num_streams)
+      gst_element_no_more_pads (GST_ELEMENT (demux));
+  }
+
   /* do we need to send a newsegment event? */
   if (stream->need_newsegment) {
     GST_DEBUG ("sending new-segment event on pad %s: %" GST_TIME_FORMAT " - %"
@@ -2381,7 +2392,7 @@
     GST_BUFFER_TIMESTAMP (stream->payload) =
         (GST_SECOND / 1000) * stream->last_pts;
 
-    GST_DEBUG ("sending stream %d of size %d", stream->id,
+    GST_DEBUG ("sending stream %d of size %d", stream->id & 0x7fff,
         segment_info->chunk_size);
 
     if (!stream->fps_known) {
@@ -2915,6 +2926,7 @@
 
   switch (transition) {
     case GST_STATE_CHANGE_PAUSED_TO_READY:{
+      int n;
       gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
       gst_adapter_clear (demux->adapter);
       g_object_unref (demux->adapter);
@@ -2934,7 +2946,11 @@
       g_slist_foreach (demux->mut_ex_streams, (GFunc) g_free, NULL);
       g_slist_free (demux->mut_ex_streams);
       demux->ext_stream_props = NULL;
-      memset (demux->stream, 0, sizeof (demux->stream));
+      /* Unref pads that were not added to the element */
+      for (n = 0; n < demux->num_streams; n++)
+        if (demux->stream[n].id & 0x8000)
+          gst_object_unref (demux->stream[n].pad);
+       memset (demux->stream, 0, sizeof (demux->stream));
       demux->seekable = FALSE;
       break;
     }
