flvmux: Add negative runtime DTS support
[platform/upstream/gst-plugins-good.git] / gst / flv / gstflvmux.c
index 4b0e56b..e13b24f 100644 (file)
@@ -14,8 +14,8 @@
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -26,8 +26,8 @@
  * <refsect2>
  * <title>Example launch line</title>
  * |[
- * gst-launch -v filesrc location=/path/to/audio ! decodebin2 ! queue ! flvmux name=m ! filesink location=file.flv   filesrc location=/path/to/video ! decodebin2 ! queue ! m.
- * ]| This pipeline muxes an audio and video file into a single FLV file.
+ * gst-launch-1.0 -v flvmux name=mux ! filesink location=test.flv  audiotestsrc samplesperbuffer=44100 num-buffers=10 ! faac ! mux.  videotestsrc num-buffers=250 ! video/x-raw,framerate=25/1 ! x264enc ! mux.
+ * ]| This pipeline encodes a test audio and video stream and muxes both into an FLV file.
  * </refsect2>
  */
 
 #include <math.h>
 #include <string.h>
 
+#include <gst/audio/audio.h>
+
 #include "gstflvmux.h"
+#include "amfdefs.h"
 
 GST_DEBUG_CATEGORY_STATIC (flvmux_debug);
 #define GST_CAT_DEFAULT flvmux_debug
@@ -73,38 +76,35 @@ static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
     GST_STATIC_CAPS
     ("audio/x-adpcm, layout = (string) swf, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
         "audio/mpeg, mpegversion = (int) 1, layer = (int) 3, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 22050, 44100 }, parsed = (boolean) TRUE; "
-        "audio/mpeg, mpegversion = (int) { 2, 4 }, framed = (boolean) TRUE; "
+        "audio/mpeg, mpegversion = (int) { 4, 2 }, stream-format = (string) raw; "
         "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
-        "audio/x-raw-int, endianness = (int) LITTLE_ENDIAN, channels = (int) { 1, 2 }, width = (int) 8, depth = (int) 8, rate = (int) { 5512, 11025, 22050, 44100 }, signed = (boolean) FALSE; "
-        "audio/x-raw-int, endianness = (int) LITTLE_ENDIAN, channels = (int) { 1, 2 }, width = (int) 16, depth = (int) 16, rate = (int) { 5512, 11025, 22050, 44100 }, signed = (boolean) TRUE; "
+        "audio/x-raw, format = (string) { U8, S16LE}, layout = (string) interleaved, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
         "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
         "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
-        "audio/x-speex, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 };")
+        "audio/x-speex, channels = (int) 1, rate = (int) 16000;")
     );
 
-#define _do_init(type)                                                          \
-  G_STMT_START{                                                                 \
-    static const GInterfaceInfo tag_setter_info = {                             \
-      NULL,                                                                     \
-      NULL,                                                                     \
-      NULL                                                                      \
-    };                                                                          \
-    g_type_add_interface_static (type, GST_TYPE_TAG_SETTER,                     \
-                                 &tag_setter_info);                             \
-  }G_STMT_END
-
-GST_BOILERPLATE_FULL (GstFlvMux, gst_flv_mux, GstElement, GST_TYPE_ELEMENT,
-    _do_init);
+#define gst_flv_mux_parent_class parent_class
+G_DEFINE_TYPE_WITH_CODE (GstFlvMux, gst_flv_mux, GST_TYPE_ELEMENT,
+    G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
 
 static void gst_flv_mux_finalize (GObject * object);
 static GstFlowReturn
-gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data);
+gst_flv_mux_handle_buffer (GstCollectPads * pads, GstCollectData * cdata,
+    GstBuffer * buf, gpointer user_data);
+static gboolean
+gst_flv_mux_handle_sink_event (GstCollectPads * pads, GstCollectData * data,
+    GstEvent * event, gpointer user_data);
 
-static gboolean gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event);
+static gboolean gst_flv_mux_handle_src_event (GstPad * pad, GstObject * parent,
+    GstEvent * event);
 static GstPad *gst_flv_mux_request_new_pad (GstElement * element,
-    GstPadTemplate * templ, const gchar * name);
+    GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps);
 static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);
 
+static gboolean gst_flv_mux_video_pad_setcaps (GstPad * pad, GstCaps * caps);
+static gboolean gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps);
+
 static void gst_flv_mux_get_property (GObject * object,
     guint prop_id, GValue * value, GParamSpec * pspec);
 static void gst_flv_mux_set_property (GObject * object,
@@ -129,23 +129,27 @@ gst_flv_mux_index_entry_free (GstFlvMuxIndexEntry * entry)
   g_slice_free (GstFlvMuxIndexEntry, entry);
 }
 
-static void
-gst_flv_mux_base_init (gpointer g_class)
+static GstBuffer *
+_gst_buffer_new_wrapped (gpointer mem, gsize size, GFreeFunc free_func)
 {
-  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+  GstBuffer *buf;
 
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&videosink_templ));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&audiosink_templ));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&src_templ));
-  gst_element_class_set_details_simple (element_class, "FLV muxer",
-      "Codec/Muxer",
-      "Muxes video/audio streams into a FLV stream",
-      "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
+  buf = gst_buffer_new ();
+  gst_buffer_append_memory (buf,
+      gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
+          mem, size, 0, size, mem, free_func));
 
-  GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
+  return buf;
+}
+
+static void
+_gst_buffer_new_and_alloc (gsize size, GstBuffer ** buffer, guint8 ** data)
+{
+  g_return_if_fail (data != NULL);
+  g_return_if_fail (buffer != NULL);
+
+  *data = g_malloc (size);
+  *buffer = _gst_buffer_new_wrapped (*data, size, g_free);
 }
 
 static void
@@ -170,9 +174,7 @@ gst_flv_mux_class_init (GstFlvMuxClass * klass)
    *
    * If True, the output will be streaming friendly. (ie without indexes and
    * duration)
-   *
-   * Since: 0.10.24
-   **/
+   */
   g_object_class_install_property (gobject_class, PROP_STREAMABLE,
       g_param_spec_boolean ("streamable", "streamable",
           "If set to true, the output should be as if it is to be streamed "
@@ -183,10 +185,23 @@ gst_flv_mux_class_init (GstFlvMuxClass * klass)
   gstelement_class->request_new_pad =
       GST_DEBUG_FUNCPTR (gst_flv_mux_request_new_pad);
   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_flv_mux_release_pad);
+
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&videosink_templ));
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&audiosink_templ));
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&src_templ));
+  gst_element_class_set_static_metadata (gstelement_class, "FLV muxer",
+      "Codec/Muxer",
+      "Muxes video/audio streams into a FLV stream",
+      "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
+
+  GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
 }
 
 static void
-gst_flv_mux_init (GstFlvMux * mux, GstFlvMuxClass * g_class)
+gst_flv_mux_init (GstFlvMux * mux)
 {
   mux->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
   gst_pad_set_event_function (mux->srcpad, gst_flv_mux_handle_src_event);
@@ -198,8 +213,12 @@ gst_flv_mux_init (GstFlvMux * mux, GstFlvMuxClass * g_class)
   mux->new_tags = FALSE;
 
   mux->collect = gst_collect_pads_new ();
-  gst_collect_pads_set_function (mux->collect,
-      (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_flv_mux_collected), mux);
+  gst_collect_pads_set_buffer_function (mux->collect,
+      GST_DEBUG_FUNCPTR (gst_flv_mux_handle_buffer), mux);
+  gst_collect_pads_set_event_function (mux->collect,
+      GST_DEBUG_FUNCPTR (gst_flv_mux_handle_sink_event), mux);
+  gst_collect_pads_set_clip_function (mux->collect,
+      GST_DEBUG_FUNCPTR (gst_collect_pads_clip_running_time), mux);
 
   gst_flv_mux_reset (GST_ELEMENT (mux));
 }
@@ -234,6 +253,7 @@ gst_flv_mux_reset (GstElement * element)
   mux->have_audio = mux->have_video = FALSE;
   mux->duration = GST_CLOCK_TIME_NONE;
   mux->new_tags = FALSE;
+  mux->first_timestamp = GST_CLOCK_STIME_NONE;
 
   mux->state = GST_FLV_MUX_STATE_HEADER;
 
@@ -242,7 +262,8 @@ gst_flv_mux_reset (GstElement * element)
 }
 
 static gboolean
-gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event)
+gst_flv_mux_handle_src_event (GstPad * pad, GstObject * parent,
+    GstEvent * event)
 {
   GstEventType type;
 
@@ -256,16 +277,38 @@ gst_flv_mux_handle_src_event (GstPad * pad, GstEvent * event)
       break;
   }
 
-  return gst_pad_event_default (pad, event);
+  return gst_pad_event_default (pad, parent, event);
 }
 
 static gboolean
-gst_flv_mux_handle_sink_event (GstPad * pad, GstEvent * event)
+gst_flv_mux_handle_sink_event (GstCollectPads * pads, GstCollectData * data,
+    GstEvent * event, gpointer user_data)
 {
-  GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
+  GstFlvMux *mux = GST_FLV_MUX (user_data);
   gboolean ret = TRUE;
 
   switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_CAPS:
+    {
+      GstCaps *caps;
+      GstFlvPad *flvpad;
+
+      gst_event_parse_caps (event, &caps);
+
+      /* find stream data */
+      flvpad = (GstFlvPad *) data;
+      g_assert (flvpad);
+
+      if (flvpad->video) {
+        ret = gst_flv_mux_video_pad_setcaps (data->pad, caps);
+      } else {
+        ret = gst_flv_mux_audio_pad_setcaps (data->pad, caps);
+      }
+      /* and eat */
+      gst_event_unref (event);
+      event = NULL;
+      break;
+    }
     case GST_EVENT_TAG:{
       GstTagList *list;
       GstTagSetter *setter = GST_TAG_SETTER (mux);
@@ -274,16 +317,17 @@ gst_flv_mux_handle_sink_event (GstPad * pad, GstEvent * event)
       gst_event_parse_tag (event, &list);
       gst_tag_setter_merge_tags (setter, list, mode);
       mux->new_tags = TRUE;
+      ret = TRUE;
+      gst_event_unref (event);
+      event = NULL;
       break;
     }
     default:
       break;
   }
 
-  /* now GstCollectPads can take care of the rest, e.g. EOS */
-  if (ret)
-    ret = mux->collect_event (pad, event);
-  gst_object_unref (mux);
+  if (event != NULL)
+    return gst_collect_pads_event_default (pads, data, event, FALSE);
 
   return ret;
 }
@@ -380,13 +424,19 @@ gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
     } else {
       cpad->audio_codec = 6;
     }
-  } else if (strcmp (gst_structure_get_name (s), "audio/x-raw-int") == 0) {
-    gint endianness;
+  } else if (strcmp (gst_structure_get_name (s), "audio/x-raw") == 0) {
+    GstAudioInfo info;
 
-    if (gst_structure_get_int (s, "endianness", &endianness)
-        && endianness == G_LITTLE_ENDIAN)
+    if (gst_audio_info_from_caps (&info, caps)) {
       cpad->audio_codec = 3;
-    else
+
+      if (GST_AUDIO_INFO_WIDTH (&info) == 8)
+        cpad->width = 0;
+      else if (GST_AUDIO_INFO_WIDTH (&info) == 16)
+        cpad->width = 1;
+      else
+        ret = FALSE;
+    } else
       ret = FALSE;
   } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
     cpad->audio_codec = 7;
@@ -399,7 +449,7 @@ gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
   }
 
   if (ret) {
-    gint rate, channels, width;
+    gint rate, channels;
 
     if (gst_structure_get_int (s, "rate", &rate)) {
       if (cpad->audio_codec == 10)
@@ -415,7 +465,8 @@ gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
       else if (rate == 8000 && (cpad->audio_codec == 5
               || cpad->audio_codec == 14))
         cpad->rate = 0;
-      else if (rate == 16000 && cpad->audio_codec == 4)
+      else if (rate == 16000 && (cpad->audio_codec == 4
+              || cpad->audio_codec == 11))
         cpad->rate = 0;
       else
         ret = FALSE;
@@ -427,7 +478,7 @@ gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
 
     if (gst_structure_get_int (s, "channels", &channels)) {
       if (cpad->audio_codec == 4 || cpad->audio_codec == 5
-          || cpad->audio_codec == 6)
+          || cpad->audio_codec == 6 || cpad->audio_codec == 11)
         cpad->channels = 0;
       else if (cpad->audio_codec == 10)
         cpad->channels = 1;
@@ -446,20 +497,8 @@ gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
       ret = FALSE;
     }
 
-    if (gst_structure_get_int (s, "width", &width)) {
-      if (cpad->audio_codec != 3)
-        cpad->width = 1;
-      else if (width == 8)
-        cpad->width = 0;
-      else if (width == 16)
-        cpad->width = 1;
-      else
-        ret = FALSE;
-    } else if (cpad->audio_codec != 3) {
+    if (cpad->audio_codec != 3)
       cpad->width = 1;
-    } else {
-      ret = FALSE;
-    }
   }
 
   if (ret && gst_structure_has_field (s, "codec_data")) {
@@ -492,18 +531,19 @@ gst_flv_mux_reset_pad (GstFlvMux * mux, GstFlvPad * cpad, gboolean video)
   cpad->video_codec_data = NULL;
   cpad->video_codec = G_MAXUINT;
   cpad->last_timestamp = 0;
+  cpad->pts = GST_CLOCK_STIME_NONE;
+  cpad->dts = GST_CLOCK_STIME_NONE;
 }
 
 static GstPad *
 gst_flv_mux_request_new_pad (GstElement * element,
-    GstPadTemplate * templ, const gchar * pad_name)
+    GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
 {
   GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
   GstFlvMux *mux = GST_FLV_MUX (element);
   GstFlvPad *cpad;
   GstPad *pad = NULL;
   const gchar *name = NULL;
-  GstPadSetCapsFunction setcapsfunc = NULL;
   gboolean video;
 
   if (mux->state != GST_FLV_MUX_STATE_HEADER) {
@@ -519,7 +559,6 @@ gst_flv_mux_request_new_pad (GstElement * element,
     mux->have_audio = TRUE;
     name = "audio";
     video = FALSE;
-    setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_audio_pad_setcaps);
   } else if (templ == gst_element_class_get_pad_template (klass, "video")) {
     if (mux->have_video) {
       GST_WARNING_OBJECT (mux, "Already have a video pad");
@@ -528,29 +567,19 @@ gst_flv_mux_request_new_pad (GstElement * element,
     mux->have_video = TRUE;
     name = "video";
     video = TRUE;
-    setcapsfunc = GST_DEBUG_FUNCPTR (gst_flv_mux_video_pad_setcaps);
   } else {
     GST_WARNING_OBJECT (mux, "Invalid template");
     return NULL;
   }
 
   pad = gst_pad_new_from_template (templ, name);
-  cpad = (GstFlvPad *)
-      gst_collect_pads_add_pad (mux->collect, pad, sizeof (GstFlvPad));
+  cpad = (GstFlvPad *) gst_collect_pads_add_pad (mux->collect, pad,
+      sizeof (GstFlvPad), NULL, TRUE);
 
   cpad->audio_codec_data = NULL;
   cpad->video_codec_data = NULL;
   gst_flv_mux_reset_pad (mux, cpad, video);
 
-  /* FIXME: hacked way to override/extend the event function of
-   * GstCollectPads; because it sets its own event function giving the
-   * element no access to events.
-   */
-  mux->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (pad);
-  gst_pad_set_event_function (pad,
-      GST_DEBUG_FUNCPTR (gst_flv_mux_handle_sink_event));
-
-  gst_pad_set_setcaps_function (pad, setcapsfunc);
   gst_pad_set_active (pad, TRUE);
   gst_element_add_pad (element, pad);
 
@@ -571,10 +600,9 @@ gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
 static GstFlowReturn
 gst_flv_mux_push (GstFlvMux * mux, GstBuffer * buffer)
 {
-  gst_buffer_set_caps (buffer, GST_PAD_CAPS (mux->srcpad));
   /* pushing the buffer that rewrites the header will make it no longer be the
    * total output size in bytes, but it doesn't matter at that point */
-  mux->byte_count += GST_BUFFER_SIZE (buffer);
+  mux->byte_count += gst_buffer_get_size (buffer);
 
   return gst_pad_push (mux->srcpad, buffer);
 }
@@ -585,8 +613,7 @@ gst_flv_mux_create_header (GstFlvMux * mux)
   GstBuffer *header;
   guint8 *data;
 
-  header = gst_buffer_new_and_alloc (9 + 4);
-  data = GST_BUFFER_DATA (header);
+  _gst_buffer_new_and_alloc (9 + 4, &header, &data);
 
   data[0] = 'F';
   data[1] = 'L';
@@ -618,13 +645,12 @@ gst_flv_mux_preallocate_index (GstFlvMux * mux)
   GST_DEBUG_OBJECT (mux, "preallocating %d bytes for the index",
       preallocate_size);
 
-  tmp = gst_buffer_new_and_alloc (preallocate_size);
-  data = GST_BUFFER_DATA (tmp);
+  _gst_buffer_new_and_alloc (preallocate_size, &tmp, &data);
 
   /* prefill the space with a gstfiller: <spaces> script tag variable */
   GST_WRITE_UINT16_BE (data, 9);        /* 9 characters */
   memcpy (data + 2, "gstfiller", 9);
-  GST_WRITE_UINT8 (data + 11, 2);       /* a string value */
+  GST_WRITE_UINT8 (data + 11, AMF0_STRING_MARKER);      /* a string value */
   GST_WRITE_UINT16_BE (data + 12, preallocate_size - 14);
   memset (data + 14, ' ', preallocate_size - 14);       /* the rest is spaces */
   return tmp;
@@ -633,22 +659,28 @@ gst_flv_mux_preallocate_index (GstFlvMux * mux)
 static GstBuffer *
 gst_flv_mux_create_number_script_value (const gchar * name, gdouble value)
 {
-  GstBuffer *tmp = gst_buffer_new_and_alloc (2 + strlen (name) + 1 + 8);
-  guint8 *data = GST_BUFFER_DATA (tmp);
+  GstBuffer *tmp;
+  guint8 *data;
+  gsize len = strlen (name);
 
-  GST_WRITE_UINT16_BE (data, strlen (name));    /* name length */
-  memcpy (&data[2], name, strlen (name));
-  data[2 + strlen (name)] = 0;  /* double */
-  GST_WRITE_DOUBLE_BE (data + 2 + strlen (name) + 1, value);
+  _gst_buffer_new_and_alloc (2 + len + 1 + 8, &tmp, &data);
+
+  GST_WRITE_UINT16_BE (data, len);
+  data += 2;                    /* name length */
+  memcpy (data, name, len);
+  data += len;
+  *data++ = AMF0_NUMBER_MARKER; /* double type */
+  GST_WRITE_DOUBLE_BE (data, value);
 
   return tmp;
 }
 
 static GstBuffer *
-gst_flv_mux_create_metadata (GstFlvMux * mux)
+gst_flv_mux_create_metadata (GstFlvMux * mux, gboolean full)
 {
   const GstTagList *tags;
   GstBuffer *script_tag, *tmp;
+  GstMapInfo map;
   guint8 *data;
   gint i, n_tags, tags_written = 0;
 
@@ -656,8 +688,9 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
 
   GST_DEBUG_OBJECT (mux, "tags = %" GST_PTR_FORMAT, tags);
 
-  script_tag = gst_buffer_new_and_alloc (11);
-  data = GST_BUFFER_DATA (script_tag);
+  /* FIXME perhaps some bytewriter'ing here ... */
+
+  _gst_buffer_new_and_alloc (11, &script_tag, &data);
 
   data[0] = 18;
 
@@ -672,48 +705,46 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
   /* Stream ID */
   data[8] = data[9] = data[10] = 0;
 
-  tmp = gst_buffer_new_and_alloc (13);
-  data = GST_BUFFER_DATA (tmp);
-  data[0] = 2;                  /* string */
+  _gst_buffer_new_and_alloc (13, &tmp, &data);
+  data[0] = AMF0_STRING_MARKER; /* string */
   data[1] = 0;
   data[2] = 10;                 /* length 10 */
   memcpy (&data[3], "onMetaData", 10);
 
-  script_tag = gst_buffer_join (script_tag, tmp);
+  script_tag = gst_buffer_append (script_tag, tmp);
 
-  n_tags = (tags) ? gst_structure_n_fields ((GstStructure *) tags) : 0;
-  tmp = gst_buffer_new_and_alloc (5);
-  data = GST_BUFFER_DATA (tmp);
+  n_tags = (tags) ? gst_tag_list_n_tags (tags) : 0;
+  _gst_buffer_new_and_alloc (5, &tmp, &data);
   data[0] = 8;                  /* ECMA array */
   GST_WRITE_UINT32_BE (data + 1, n_tags);
-  script_tag = gst_buffer_join (script_tag, tmp);
+  script_tag = gst_buffer_append (script_tag, tmp);
+
+  if (!full)
+    goto tags;
 
   /* Some players expect the 'duration' to be always set. Fill it out later,
      after querying the pads or after getting EOS */
   if (!mux->streamable) {
     tmp = gst_flv_mux_create_number_script_value ("duration", 86400);
-    script_tag = gst_buffer_join (script_tag, tmp);
+    script_tag = gst_buffer_append (script_tag, tmp);
     tags_written++;
-  }
 
-  /* Sometimes the information about the total file size is useful for the
-     player. It will be filled later, after getting EOS */
-  if (!mux->streamable) {
+    /* Sometimes the information about the total file size is useful for the
+       player. It will be filled later, after getting EOS */
     tmp = gst_flv_mux_create_number_script_value ("filesize", 0);
-    script_tag = gst_buffer_join (script_tag, tmp);
+    script_tag = gst_buffer_append (script_tag, tmp);
     tags_written++;
-  }
 
-  if (!mux->streamable) {
+    /* Preallocate space for the index to be written at EOS */
     tmp = gst_flv_mux_preallocate_index (mux);
-    script_tag = gst_buffer_join (script_tag, tmp);
+    script_tag = gst_buffer_append (script_tag, tmp);
   } else {
     GST_DEBUG_OBJECT (mux, "not preallocating index, streamable mode");
   }
 
+tags:
   for (i = 0; tags && i < n_tags; i++) {
-    const gchar *tag_name =
-        gst_structure_nth_field_name ((const GstStructure *) tags, i);
+    const gchar *tag_name = gst_tag_list_nth_tag_name (tags, i);
     if (!strcmp (tag_name, GST_TAG_DURATION)) {
       guint64 dur;
 
@@ -733,8 +764,8 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
       if (!gst_tag_list_get_string (tags, tag_name, &s))
         continue;
 
-      tmp = gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s));
-      data = GST_BUFFER_DATA (tmp);
+      _gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s),
+          &tmp, &data);
       data[0] = 0;              /* tag name length */
       data[1] = strlen (t);
       memcpy (&data[2], t, strlen (t));
@@ -742,26 +773,25 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
       data[3 + strlen (t)] = (strlen (s) >> 8) & 0xff;
       data[4 + strlen (t)] = (strlen (s)) & 0xff;
       memcpy (&data[5 + strlen (t)], s, strlen (s));
-      script_tag = gst_buffer_join (script_tag, tmp);
+      script_tag = gst_buffer_append (script_tag, tmp);
 
       g_free (s);
       tags_written++;
     }
   }
 
+  if (!full)
+    goto end;
+
   if (mux->duration == GST_CLOCK_TIME_NONE) {
     GSList *l;
-
-    GstFormat fmt = GST_FORMAT_TIME;
     guint64 dur;
 
     for (l = mux->collect->data; l; l = l->next) {
       GstCollectData *cdata = l->data;
 
-      fmt = GST_FORMAT_TIME;
-
-      if (gst_pad_query_peer_duration (cdata->pad, &fmt, (gint64 *) & dur) &&
-          fmt == GST_FORMAT_TIME && dur != GST_CLOCK_TIME_NONE) {
+      if (gst_pad_peer_query_duration (cdata->pad, GST_FORMAT_TIME,
+              (gint64 *) & dur) && dur != GST_CLOCK_TIME_NONE) {
         if (mux->duration == GST_CLOCK_TIME_NONE)
           mux->duration = dur;
         else
@@ -772,12 +802,15 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
 
   if (!mux->streamable && mux->duration != GST_CLOCK_TIME_NONE) {
     gdouble d;
+    GstMapInfo map;
+
     d = gst_guint64_to_gdouble (mux->duration);
     d /= (gdouble) GST_SECOND;
 
     GST_DEBUG_OBJECT (mux, "determined the duration to be %f", d);
-    data = GST_BUFFER_DATA (script_tag);
-    GST_WRITE_DOUBLE_BE (data + 29 + 2 + 8 + 1, d);
+    gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
+    GST_WRITE_DOUBLE_BE (map.data + 29 + 2 + 8 + 1, d);
+    gst_buffer_unmap (script_tag, &map);
   }
 
   if (mux->have_video) {
@@ -793,8 +826,9 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
       }
     }
 
-    if (video_pad && GST_PAD_CAPS (video_pad)) {
-      GstStructure *s = gst_caps_get_structure (GST_PAD_CAPS (video_pad), 0);
+    if (video_pad && gst_pad_has_current_caps (video_pad)) {
+      GstCaps *caps;
+      GstStructure *s;
       gint size;
       gint num, den;
 
@@ -803,14 +837,18 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
 
       tmp = gst_flv_mux_create_number_script_value ("videocodecid",
           cpad->video_codec);
-      script_tag = gst_buffer_join (script_tag, tmp);
+      script_tag = gst_buffer_append (script_tag, tmp);
       tags_written++;
 
+      caps = gst_pad_get_current_caps (video_pad);
+      s = gst_caps_get_structure (caps, 0);
+      gst_caps_unref (caps);
+
       if (gst_structure_get_int (s, "width", &size)) {
         GST_DEBUG_OBJECT (mux, "putting width %d in the metadata", size);
 
         tmp = gst_flv_mux_create_number_script_value ("width", size);
-        script_tag = gst_buffer_join (script_tag, tmp);
+        script_tag = gst_buffer_append (script_tag, tmp);
         tags_written++;
       }
 
@@ -818,7 +856,7 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
         GST_DEBUG_OBJECT (mux, "putting height %d in the metadata", size);
 
         tmp = gst_flv_mux_create_number_script_value ("height", size);
-        script_tag = gst_buffer_join (script_tag, tmp);
+        script_tag = gst_buffer_append (script_tag, tmp);
         tags_written++;
       }
 
@@ -829,14 +867,14 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
         GST_DEBUG_OBJECT (mux, "putting AspectRatioX %f in the metadata", d);
 
         tmp = gst_flv_mux_create_number_script_value ("AspectRatioX", d);
-        script_tag = gst_buffer_join (script_tag, tmp);
+        script_tag = gst_buffer_append (script_tag, tmp);
         tags_written++;
 
         d = den;
         GST_DEBUG_OBJECT (mux, "putting AspectRatioY %f in the metadata", d);
 
         tmp = gst_flv_mux_create_number_script_value ("AspectRatioY", d);
-        script_tag = gst_buffer_join (script_tag, tmp);
+        script_tag = gst_buffer_append (script_tag, tmp);
         tags_written++;
       }
 
@@ -847,7 +885,7 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
         GST_DEBUG_OBJECT (mux, "putting framerate %f in the metadata", d);
 
         tmp = gst_flv_mux_create_number_script_value ("framerate", d);
-        script_tag = gst_buffer_join (script_tag, tmp);
+        script_tag = gst_buffer_append (script_tag, tmp);
         tags_written++;
       }
     }
@@ -872,7 +910,7 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
 
       tmp = gst_flv_mux_create_number_script_value ("audiocodecid",
           cpad->audio_codec);
-      script_tag = gst_buffer_join (script_tag, tmp);
+      script_tag = gst_buffer_append (script_tag, tmp);
       tags_written++;
     }
   }
@@ -880,8 +918,7 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
   {
     const gchar *s = "GStreamer FLV muxer";
 
-    tmp = gst_buffer_new_and_alloc (2 + 15 + 1 + 2 + strlen (s));
-    data = GST_BUFFER_DATA (tmp);
+    _gst_buffer_new_and_alloc (2 + 15 + 1 + 2 + strlen (s), &tmp, &data);
     data[0] = 0;                /* 15 bytes name */
     data[1] = 15;
     memcpy (&data[2], "metadatacreator", 15);
@@ -889,7 +926,7 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
     data[18] = (strlen (s) >> 8) & 0xff;
     data[19] = (strlen (s)) & 0xff;
     memcpy (&data[20], s, strlen (s));
-    script_tag = gst_buffer_join (script_tag, tmp);
+    script_tag = gst_buffer_append (script_tag, tmp);
 
     tags_written++;
   }
@@ -915,8 +952,7 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
         months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
         tm->tm_year + 1900);
 
-    tmp = gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s));
-    data = GST_BUFFER_DATA (tmp);
+    _gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s), &tmp, &data);
     data[0] = 0;                /* 12 bytes name */
     data[1] = 12;
     memcpy (&data[2], "creationdate", 12);
@@ -924,32 +960,40 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
     data[15] = (strlen (s) >> 8) & 0xff;
     data[16] = (strlen (s)) & 0xff;
     memcpy (&data[17], s, strlen (s));
-    script_tag = gst_buffer_join (script_tag, tmp);
+    script_tag = gst_buffer_append (script_tag, tmp);
 
     g_free (s);
     tags_written++;
   }
 
-  tmp = gst_buffer_new_and_alloc (2 + 0 + 1);
-  data = GST_BUFFER_DATA (tmp);
+end:
+
+  if (!tags_written) {
+    gst_buffer_unref (script_tag);
+    script_tag = NULL;
+    goto exit;
+  }
+
+  _gst_buffer_new_and_alloc (2 + 0 + 1, &tmp, &data);
   data[0] = 0;                  /* 0 byte size */
   data[1] = 0;
   data[2] = 9;                  /* end marker */
-  script_tag = gst_buffer_join (script_tag, tmp);
+  script_tag = gst_buffer_append (script_tag, tmp);
   tags_written++;
 
-  tmp = gst_buffer_new_and_alloc (4);
-  data = GST_BUFFER_DATA (tmp);
-  GST_WRITE_UINT32_BE (data, GST_BUFFER_SIZE (script_tag));
-  script_tag = gst_buffer_join (script_tag, tmp);
+  _gst_buffer_new_and_alloc (4, &tmp, &data);
+  GST_WRITE_UINT32_BE (data, gst_buffer_get_size (script_tag));
+  script_tag = gst_buffer_append (script_tag, tmp);
 
-  data = GST_BUFFER_DATA (script_tag);
-  data[1] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 16) & 0xff;
-  data[2] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 8) & 0xff;
-  data[3] = ((GST_BUFFER_SIZE (script_tag) - 11 - 4) >> 0) & 0xff;
+  gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
+  map.data[1] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 16) & 0xff;
+  map.data[2] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 8) & 0xff;
+  map.data[3] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 0) & 0xff;
 
-  GST_WRITE_UINT32_BE (data + 11 + 13 + 1, tags_written);
+  GST_WRITE_UINT32_BE (map.data + 11 + 13 + 1, tags_written);
+  gst_buffer_unmap (script_tag, &map);
 
+exit:
   return script_tag;
 }
 
@@ -958,31 +1002,54 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
     GstFlvPad * cpad, gboolean is_codec_data)
 {
   GstBuffer *tag;
-  guint8 *data;
+  GstMapInfo map;
   guint size;
-  guint32 timestamp =
-      (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) ? GST_BUFFER_TIMESTAMP (buffer) /
-      GST_MSECOND : cpad->last_timestamp / GST_MSECOND;
+  guint32 pts, dts, cts;
+  guint8 *data, *bdata;
+  gsize bsize;
+
+  if (GST_CLOCK_STIME_IS_VALID (cpad->dts)) {
+    pts = dts = cpad->last_timestamp / GST_MSECOND;
+  } else {
+    pts = cpad->pts / GST_MSECOND;
+    dts = cpad->dts / GST_MSECOND;
+  }
+
+  /* Be safe in case TS are buggy */
+  if (pts > dts)
+    cts = pts - dts;
+  else
+    cts = 0;
+
+  /* Timestamp must start at zero */
+  if (GST_CLOCK_STIME_IS_VALID (mux->first_timestamp)) {
+    dts -= mux->first_timestamp / GST_MSECOND;
+    pts = dts + cts;
+  }
+
+  GST_LOG_OBJECT (mux, "got pts %i dts %i cts %i\n", pts, dts, cts);
+
+  gst_buffer_map (buffer, &map, GST_MAP_READ);
+  bdata = map.data;
+  bsize = map.size;
 
   size = 11;
   if (cpad->video) {
     size += 1;
     if (cpad->video_codec == 7)
-      size += 4 + GST_BUFFER_SIZE (buffer);
+      size += 4 + bsize;
     else
-      size += GST_BUFFER_SIZE (buffer);
+      size += bsize;
   } else {
     size += 1;
     if (cpad->audio_codec == 10)
-      size += 1 + GST_BUFFER_SIZE (buffer);
+      size += 1 + bsize;
     else
-      size += GST_BUFFER_SIZE (buffer);
+      size += bsize;
   }
   size += 4;
 
-  tag = gst_buffer_new_and_alloc (size);
-  GST_BUFFER_TIMESTAMP (tag) = timestamp * GST_MSECOND;
-  data = GST_BUFFER_DATA (tag);
+  _gst_buffer_new_and_alloc (size, &tag, &data);
   memset (data, 0, size);
 
   data[0] = (cpad->video) ? 9 : 8;
@@ -991,12 +1058,8 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
   data[2] = ((size - 11 - 4) >> 8) & 0xff;
   data[3] = ((size - 11 - 4) >> 0) & 0xff;
 
-  /* wrap the timestamp every G_MAXINT32 miliseconds */
-  timestamp &= 0x7fffffff;
-  data[4] = (timestamp >> 16) & 0xff;
-  data[5] = (timestamp >> 8) & 0xff;
-  data[6] = (timestamp >> 0) & 0xff;
-  data[7] = (timestamp >> 24) & 0xff;
+  GST_WRITE_UINT24_BE (data + 4, dts);
+  data[7] = (((guint) dts) >> 24) & 0xff;
 
   data[8] = data[9] = data[10] = 0;
 
@@ -1009,16 +1072,17 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
     data[11] |= cpad->video_codec & 0x0f;
 
     if (cpad->video_codec == 7) {
-      data[12] = is_codec_data ? 0 : 1;
-
-      /* FIXME: what to do about composition time */
-      data[13] = data[14] = data[15] = 0;
-
-      memcpy (data + 11 + 1 + 4, GST_BUFFER_DATA (buffer),
-          GST_BUFFER_SIZE (buffer));
+      if (is_codec_data) {
+        data[12] = 0;
+        GST_WRITE_UINT24_BE (data + 13, 0);
+      } else {
+        /* ACV NALU */
+        data[12] = 1;
+        GST_WRITE_UINT24_BE (data + 13, cts);
+      }
+      memcpy (data + 11 + 1 + 4, bdata, bsize);
     } else {
-      memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
-          GST_BUFFER_SIZE (buffer));
+      memcpy (data + 11 + 1, bdata, bsize);
     }
   } else {
     data[11] |= (cpad->audio_codec << 4) & 0xf0;
@@ -1029,17 +1093,22 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
     if (cpad->audio_codec == 10) {
       data[12] = is_codec_data ? 0 : 1;
 
-      memcpy (data + 11 + 1 + 1, GST_BUFFER_DATA (buffer),
-          GST_BUFFER_SIZE (buffer));
+      memcpy (data + 11 + 1 + 1, bdata, bsize);
     } else {
-      memcpy (data + 11 + 1, GST_BUFFER_DATA (buffer),
-          GST_BUFFER_SIZE (buffer));
+      memcpy (data + 11 + 1, bdata, bsize);
     }
   }
 
+  gst_buffer_unmap (buffer, &map);
+
   GST_WRITE_UINT32_BE (data + size - 4, size - 4);
 
-  gst_buffer_copy_metadata (tag, buffer, GST_BUFFER_COPY_TIMESTAMPS);
+  GST_BUFFER_PTS (tag) = GST_CLOCK_TIME_NONE;
+  GST_BUFFER_DTS (tag) = GST_CLOCK_TIME_NONE;
+  GST_BUFFER_DURATION (tag) = GST_CLOCK_TIME_NONE;
+  GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET (buffer);
+  GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_END (buffer);
+
   /* mark the buffer if it's an audio buffer and there's also video being muxed
    * or it's a video interframe */
   if ((mux->have_video && !cpad->video) ||
@@ -1091,9 +1160,35 @@ gst_flv_mux_write_header (GstFlvMux * mux)
   GValue streamheader = { 0 };
   GSList *l;
   GstFlowReturn ret;
+  GstSegment segment;
+  gchar s_id[32];
+
+  /* if not streaming, check if downstream is seekable */
+  if (!mux->streamable) {
+    gboolean seekable;
+    GstQuery *query;
+
+    query = gst_query_new_seeking (GST_FORMAT_BYTES);
+    if (gst_pad_peer_query (mux->srcpad, query)) {
+      gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
+      GST_INFO_OBJECT (mux, "downstream is %sseekable", seekable ? "" : "not ");
+    } else {
+      /* have to assume seeking is supported if query not handled downstream */
+      GST_WARNING_OBJECT (mux, "downstream did not handle seeking query");
+      seekable = FALSE;
+    }
+    if (!seekable) {
+      mux->streamable = TRUE;
+      g_object_notify (G_OBJECT (mux), "streamable");
+      GST_WARNING_OBJECT (mux, "downstream is not seekable, but "
+          "streamable=false. Will ignore that and create streamable output "
+          "instead");
+    }
+    gst_query_unref (query);
+  }
 
   header = gst_flv_mux_create_header (mux);
-  metadata = gst_flv_mux_create_metadata (mux);
+  metadata = gst_flv_mux_create_metadata (mux, TRUE);
   video_codec_data = NULL;
   audio_codec_data = NULL;
 
@@ -1121,16 +1216,16 @@ gst_flv_mux_write_header (GstFlvMux * mux)
   }
 
   /* mark buffers that will go in the streamheader */
-  GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_IN_CAPS);
-  GST_BUFFER_FLAG_SET (metadata, GST_BUFFER_FLAG_IN_CAPS);
+  GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_HEADER);
+  GST_BUFFER_FLAG_SET (metadata, GST_BUFFER_FLAG_HEADER);
   if (video_codec_data != NULL) {
-    GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_IN_CAPS);
+    GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_HEADER);
     /* mark as a delta unit, so downstream will not try to synchronize on that
      * buffer - to actually start playback you need a real video keyframe */
     GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_DELTA_UNIT);
   }
   if (audio_codec_data != NULL) {
-    GST_BUFFER_FLAG_SET (audio_codec_data, GST_BUFFER_FLAG_IN_CAPS);
+    GST_BUFFER_FLAG_SET (audio_codec_data, GST_BUFFER_FLAG_HEADER);
   }
 
   /* put buffers in streamheader */
@@ -1142,18 +1237,24 @@ gst_flv_mux_write_header (GstFlvMux * mux)
   if (audio_codec_data != NULL)
     gst_flv_mux_put_buffer_in_streamheader (&streamheader, audio_codec_data);
 
+  /* stream-start (FIXME: create id based on input ids) */
+  g_snprintf (s_id, sizeof (s_id), "flvmux-%08x", g_random_int ());
+  gst_pad_push_event (mux->srcpad, gst_event_new_stream_start (s_id));
+
   /* create the caps and put the streamheader in them */
-  caps = gst_caps_new_simple ("video/x-flv", NULL);
-  caps = gst_caps_make_writable (caps);
+  caps = gst_caps_new_empty_simple ("video/x-flv");
   structure = gst_caps_get_structure (caps, 0);
   gst_structure_set_value (structure, "streamheader", &streamheader);
   g_value_unset (&streamheader);
 
-  if (GST_PAD_CAPS (mux->srcpad) == NULL)
-    gst_pad_set_caps (mux->srcpad, caps);
+  gst_pad_set_caps (mux->srcpad, caps);
 
   gst_caps_unref (caps);
 
+  /* segment */
+  gst_segment_init (&segment, GST_FORMAT_BYTES);
+  gst_pad_push_event (mux->srcpad, gst_event_new_segment (&segment));
+
   /* push the header buffer, the metadata and the codec info, if any */
   ret = gst_flv_mux_push (mux, header);
   if (ret != GST_FLOW_OK)
@@ -1187,28 +1288,22 @@ gst_flv_mux_update_index (GstFlvMux * mux, GstBuffer * buffer, GstFlvPad * cpad)
           GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)))
     return;
 
-  if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
+  if (GST_BUFFER_PTS_IS_VALID (buffer)) {
     GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
     entry->position = mux->byte_count;
-    entry->time =
-        gst_guint64_to_gdouble (GST_BUFFER_TIMESTAMP (buffer)) / GST_SECOND;
+    entry->time = gst_guint64_to_gdouble (GST_BUFFER_PTS (buffer)) / GST_SECOND;
     mux->index = g_list_prepend (mux->index, entry);
   }
 }
 
 static GstFlowReturn
-gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad)
+gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad, GstBuffer * buffer)
 {
   GstBuffer *tag;
-  GstBuffer *buffer =
-      gst_collect_pads_pop (mux->collect, (GstCollectData *) cpad);
   GstFlowReturn ret;
+  GstClockTime dts = GST_BUFFER_DTS (buffer);
 
-  /* arrange downstream running time */
-  buffer = gst_buffer_make_metadata_writable (buffer);
-  GST_BUFFER_TIMESTAMP (buffer) =
-      gst_segment_to_running_time (&cpad->collect.segment,
-      GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buffer));
+  /* clipping function arranged for running_time */
 
   if (!mux->streamable)
     gst_flv_mux_update_index (mux, buffer, cpad);
@@ -1219,8 +1314,9 @@ gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad)
 
   ret = gst_flv_mux_push (mux, tag);
 
-  if (ret == GST_FLOW_OK && GST_BUFFER_TIMESTAMP_IS_VALID (tag))
-    cpad->last_timestamp = GST_BUFFER_TIMESTAMP (tag);
+  if (ret == GST_FLOW_OK && GST_CLOCK_TIME_IS_VALID (dts))
+    cpad->last_timestamp = dts;
+
 
   return ret;
 }
@@ -1245,12 +1341,6 @@ gst_flv_mux_determine_duration (GstFlvMux * mux)
     }
   }
 
-  if (duration == GST_CLOCK_TIME_NONE) {
-    GST_DEBUG_OBJECT (mux, "not able to determine duration "
-        "from pad timestamps, assuming 0");
-    return 0;
-  }
-
   return duration;
 }
 
@@ -1264,21 +1354,27 @@ gst_flv_mux_rewrite_header (GstFlvMux * mux)
   GList *l;
   guint32 index_len, allocate_size;
   guint32 i, index_skip;
+  GstSegment segment;
+  GstClockTime dur;
 
   if (mux->streamable)
     return GST_FLOW_OK;
 
   /* seek back to the preallocated index space */
-  event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
-      13 + 29, GST_CLOCK_TIME_NONE, 13 + 29);
+  gst_segment_init (&segment, GST_FORMAT_BYTES);
+  segment.start = segment.time = 13 + 29;
+  event = gst_event_new_segment (&segment);
   if (!gst_pad_push_event (mux->srcpad, event)) {
     GST_WARNING_OBJECT (mux, "Seek to rewrite header failed");
     return GST_FLOW_OK;
   }
 
-  /* if we were not able to determine the duration before, set it now */
-  if (mux->duration == GST_CLOCK_TIME_NONE)
-    mux->duration = gst_flv_mux_determine_duration (mux);
+  /* determine duration now based on our own timestamping,
+   * so that it is likely many times better and consistent
+   * than whatever obtained by some query */
+  dur = gst_flv_mux_determine_duration (mux);
+  if (dur != GST_CLOCK_TIME_NONE)
+    mux->duration = dur;
 
   /* rewrite the duration tag */
   d = gst_guint64_to_gdouble (mux->duration);
@@ -1294,7 +1390,7 @@ gst_flv_mux_rewrite_header (GstFlvMux * mux)
   GST_DEBUG_OBJECT (mux, "putting total filesize %f in the metadata", d);
 
   tmp = gst_flv_mux_create_number_script_value ("filesize", d);
-  rewrite = gst_buffer_join (rewrite, tmp);
+  rewrite = gst_buffer_append (rewrite, tmp);
 
   if (!mux->index) {
     /* no index, so push buffer and return */
@@ -1317,8 +1413,7 @@ gst_flv_mux_rewrite_header (GstFlvMux * mux)
   /* see size calculation in gst_flv_mux_preallocate_index */
   allocate_size = 11 + 8 + 22 + 10 + index_len * 18;
   GST_DEBUG_OBJECT (mux, "Allocating %d bytes for index", allocate_size);
-  index = gst_buffer_new_and_alloc (allocate_size);
-  data = GST_BUFFER_DATA (index);
+  _gst_buffer_new_and_alloc (allocate_size, &index, &data);
 
   GST_WRITE_UINT16_BE (data, 9);        /* the 'keyframes' key */
   memcpy (data + 2, "keyframes", 9);
@@ -1367,8 +1462,7 @@ gst_flv_mux_rewrite_header (GstFlvMux * mux)
     guint8 *data;
     guint32 remaining_filler_size;
 
-    tmp = gst_buffer_new_and_alloc (14);
-    data = GST_BUFFER_DATA (tmp);
+    _gst_buffer_new_and_alloc (14, &tmp, &data);
     GST_WRITE_UINT16_BE (data, 9);
     memcpy (data + 2, "gstfiller", 9);
     GST_WRITE_UINT8 (data + 11, 2);     /* string */
@@ -1380,24 +1474,22 @@ gst_flv_mux_rewrite_header (GstFlvMux * mux)
     GST_DEBUG_OBJECT (mux, "Remaining filler size is %d bytes",
         remaining_filler_size);
     GST_WRITE_UINT16_BE (data + 12, remaining_filler_size);
-    index = gst_buffer_join (index, tmp);
+    index = gst_buffer_append (index, tmp);
   }
 
-  rewrite = gst_buffer_join (rewrite, index);
+  rewrite = gst_buffer_append (rewrite, index);
 
-  gst_buffer_set_caps (rewrite, GST_PAD_CAPS (mux->srcpad));
   return gst_flv_mux_push (mux, rewrite);
 }
 
 static GstFlowReturn
-gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data)
+gst_flv_mux_handle_buffer (GstCollectPads * pads, GstCollectData * cdata,
+    GstBuffer * buffer, gpointer user_data)
 {
   GstFlvMux *mux = GST_FLV_MUX (user_data);
   GstFlvPad *best;
-  GstClockTime best_time;
+  gint64 best_time;
   GstFlowReturn ret;
-  GSList *sl;
-  gboolean eos = TRUE;
 
   if (mux->state == GST_FLV_MUX_STATE_HEADER) {
     if (mux->collect->data == NULL) {
@@ -1406,82 +1498,61 @@ gst_flv_mux_collected (GstCollectPads * pads, gpointer user_data)
       return GST_FLOW_ERROR;
     }
 
-    if (gst_pad_push_event (mux->srcpad,
-            gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0)))
-      ret = gst_flv_mux_write_header (mux);
-    else
-      ret = GST_FLOW_ERROR;
-
+    ret = gst_flv_mux_write_header (mux);
     if (ret != GST_FLOW_OK)
       return ret;
     mux->state = GST_FLV_MUX_STATE_DATA;
+
+    if (GST_COLLECT_PADS_DTS_IS_VALID (cdata))
+      mux->first_timestamp = GST_COLLECT_PADS_DTS (cdata);
+    else
+      mux->first_timestamp = 0;
   }
 
   if (mux->new_tags) {
-    GstBuffer *buf = gst_flv_mux_create_metadata (mux);
-    gst_flv_mux_push (mux, buf);
+    GstBuffer *buf = gst_flv_mux_create_metadata (mux, FALSE);
+    if (buf)
+      gst_flv_mux_push (mux, buf);
     mux->new_tags = FALSE;
   }
 
+  best = (GstFlvPad *) cdata;
+  if (best) {
+    g_assert (buffer);
+    best->dts = GST_COLLECT_PADS_DTS (cdata);
 
-  best = NULL;
-  best_time = GST_CLOCK_TIME_NONE;
-  for (sl = mux->collect->data; sl; sl = sl->next) {
-    GstFlvPad *cpad = sl->data;
-    GstBuffer *buffer = gst_collect_pads_peek (pads, (GstCollectData *) cpad);
-    GstClockTime time;
-
-    if (!buffer)
-      continue;
+    if (GST_CLOCK_STIME_IS_VALID (best->dts))
+      best_time = best->dts - mux->first_timestamp;
 
-    eos = FALSE;
-
-    time = GST_BUFFER_TIMESTAMP (buffer);
-    gst_buffer_unref (buffer);
-
-    /* Use buffers without valid timestamp first */
-    if (!GST_CLOCK_TIME_IS_VALID (time)) {
-      GST_WARNING_OBJECT (pads, "Buffer without valid timestamp");
-
-      best_time = cpad->last_timestamp;
-      best = cpad;
-      break;
-    }
-
-    time = gst_segment_to_running_time (&cpad->collect.segment,
-        GST_FORMAT_TIME, time);
-    if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
-      GST_DEBUG_OBJECT (mux, "clipping buffer on pad %s outside segment",
-          GST_PAD_NAME (cpad->collect.pad));
-      buffer = gst_collect_pads_pop (pads, (GstCollectData *) cpad);
-      gst_buffer_unref (buffer);
-      return GST_FLOW_OK;
-    }
+    if (GST_BUFFER_PTS_IS_VALID (buffer))
+      best->pts = GST_BUFFER_PTS (buffer);
+    else
+      best->pts = best->dts;
 
-    if (best == NULL || (GST_CLOCK_TIME_IS_VALID (best_time)
-            && time < best_time)) {
-      best = cpad;
-      best_time = time;
-    }
+    GST_LOG_OBJECT (mux, "got buffer PTS %" GST_TIME_FORMAT " DTS %"
+        GST_STIME_FORMAT "\n", GST_TIME_ARGS (best->pts),
+        GST_STIME_ARGS (best->dts));
+  } else {
+    best_time = GST_CLOCK_STIME_NONE;
   }
 
   /* The FLV timestamp is an int32 field. For non-live streams error out if a
      bigger timestamp is seen, for live the timestamp will get wrapped in
      gst_flv_mux_buffer_to_tag */
-  if (!mux->streamable && GST_CLOCK_TIME_IS_VALID (best_time)
+  if (!mux->streamable && (GST_CLOCK_STIME_IS_VALID (best_time))
       && best_time / GST_MSECOND > G_MAXINT32) {
     GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
-    eos = TRUE;
+    gst_buffer_unref (buffer);
+    buffer = NULL;
+    best = NULL;
   }
 
-  if (!eos && best) {
-    return gst_flv_mux_write_buffer (mux, best);
-  } else if (eos) {
+  if (best) {
+    return gst_flv_mux_write_buffer (mux, best, buffer);
+  } else {
     gst_flv_mux_rewrite_header (mux);
     gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
-    return GST_FLOW_UNEXPECTED;
-  } else {
-    return GST_FLOW_OK;
+    return GST_FLOW_EOS;
   }
 }