Merge commit 'a2337b8af45cb5e8c091ff0e1c3ef4b6cc7b20a3' into 0.11
[platform/upstream/gstreamer.git] / gst / flv / gstflvmux.c
index aac52fd..e002f9e 100644 (file)
@@ -39,6 +39,7 @@
 #include <string.h>
 
 #include "gstflvmux.h"
+#include "amfdefs.h"
 
 GST_DEBUG_CATEGORY_STATIC (flvmux_debug);
 #define GST_CAT_DEFAULT flvmux_debug
@@ -90,7 +91,8 @@ static void gst_flv_mux_finalize (GObject * object);
 static GstFlowReturn
 gst_flv_mux_collected (GstCollectPads * pads, 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 * req_name, const GstCaps * caps);
 static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);
@@ -252,7 +254,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;
 
@@ -266,13 +269,14 @@ 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 (GstPad * pad, GstObject * parent,
+    GstEvent * event)
 {
-  GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
+  GstFlvMux *mux = GST_FLV_MUX (parent);
   gboolean ret = TRUE;
 
   switch (GST_EVENT_TYPE (event)) {
@@ -313,8 +317,7 @@ gst_flv_mux_handle_sink_event (GstPad * pad, GstEvent * event)
 
   /* now GstCollectPads can take care of the rest, e.g. EOS */
   if (ret)
-    ret = mux->collect_event (pad, event);
-  gst_object_unref (mux);
+    ret = mux->collect_event (pad, parent, event);
 
   return ret;
 }
@@ -564,7 +567,7 @@ gst_flv_mux_request_new_pad (GstElement * element,
 
   pad = gst_pad_new_from_template (templ, name);
   cpad = (GstFlvPad *)
-      gst_collect_pads_add_pad (mux->collect, pad, sizeof (GstFlvPad));
+      gst_collect_pads_add_pad (mux->collect, pad, sizeof (GstFlvPad), NULL);
 
   cpad->audio_codec_data = NULL;
   cpad->video_codec_data = NULL;
@@ -648,7 +651,7 @@ gst_flv_mux_preallocate_index (GstFlvMux * mux)
   /* 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;
@@ -659,13 +662,16 @@ gst_flv_mux_create_number_script_value (const gchar * name, gdouble value)
 {
   GstBuffer *tmp;
   guint8 *data;
+  gsize len = strlen (name);
 
-  _gst_buffer_new_and_alloc (2 + strlen (name) + 1 + 8, &tmp, &data);
+  _gst_buffer_new_and_alloc (2 + len + 1 + 8, &tmp, &data);
 
-  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_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;
 }
@@ -700,7 +706,7 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
   data[8] = data[9] = data[10] = 0;
 
   _gst_buffer_new_and_alloc (13, &tmp, &data);
-  data[0] = 2;                  /* string */
+  data[0] = AMF0_STRING_MARKER; /* string */
   data[1] = 0;
   data[2] = 10;                 /* length 10 */
   memcpy (&data[3], "onMetaData", 10);
@@ -719,17 +725,14 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
     tmp = gst_flv_mux_create_number_script_value ("duration", 86400);
     script_tag = gst_buffer_join (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);
     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);
   } else {
@@ -781,7 +784,7 @@ gst_flv_mux_create_metadata (GstFlvMux * mux)
     for (l = mux->collect->data; l; l = l->next) {
       GstCollectData *cdata = l->data;
 
-      if (gst_pad_query_peer_duration (cdata->pad, GST_FORMAT_TIME,
+      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;
@@ -1172,8 +1175,7 @@ gst_flv_mux_write_header (GstFlvMux * mux)
     gst_flv_mux_put_buffer_in_streamheader (&streamheader, audio_codec_data);
 
   /* 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);