element: Enforce that elements created by gst_element_factory_create/make() are floating
[platform/upstream/gstreamer.git] / gst / gstbufferlist.c
index dabcd6a..2c13597 100644 (file)
@@ -23,6 +23,7 @@
 
 /**
  * SECTION:gstbufferlist
+ * @title: GstBufferList
  * @short_description: Lists of buffers for data-passing
  * @see_also: #GstPad, #GstMiniObject
  *
@@ -87,8 +88,11 @@ _gst_buffer_list_copy (GstBufferList * list)
   copy = gst_buffer_list_new_sized (list->n_allocated);
 
   /* add and ref all buffers in the array */
-  for (i = 0; i < len; i++)
+  for (i = 0; i < len; i++) {
     copy->buffers[i] = gst_buffer_ref (list->buffers[i]);
+    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (copy->buffers[i]),
+        GST_MINI_OBJECT_CAST (copy));
+  }
 
   copy->n_buffers = len;
 
@@ -99,18 +103,28 @@ static void
 _gst_buffer_list_free (GstBufferList * list)
 {
   guint i, len;
+  gsize slice_size;
 
   GST_LOG ("free %p", list);
 
   /* unrefs all buffers too */
   len = list->n_buffers;
-  for (i = 0; i < len; i++)
+  for (i = 0; i < len; i++) {
+    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[i]),
+        GST_MINI_OBJECT_CAST (list));
     gst_buffer_unref (list->buffers[i]);
+  }
 
   if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list))
     g_free (list->buffers);
 
-  g_slice_free1 (list->slice_size, list);
+  slice_size = list->slice_size;
+
+#ifdef USE_POISONING
+  memset (list, 0xff, slice_size);
+#endif
+
+  g_slice_free1 (slice_size, list);
 }
 
 static void
@@ -148,6 +162,9 @@ gst_buffer_list_new_sized (guint size)
   gsize slice_size;
   guint n_allocated;
 
+  if (size == 0)
+    size = 1;
+
   n_allocated = GST_ROUND_UP_16 (size);
 
   slice_size = sizeof (GstBufferList) + (n_allocated - 1) * sizeof (gpointer);
@@ -201,8 +218,11 @@ gst_buffer_list_remove_range_internal (GstBufferList * list, guint idx,
   guint i;
 
   if (unref_old) {
-    for (i = idx; i < idx + length; ++i)
+    for (i = idx; i < idx + length; ++i) {
+      gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[i]),
+          GST_MINI_OBJECT_CAST (list));
       gst_buffer_unref (list->buffers[i]);
+    }
   }
 
   if (idx + length != list->n_buffers) {
@@ -234,25 +254,72 @@ gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
 {
   guint i, len;
   gboolean ret = TRUE;
+  gboolean list_was_writable, first_warning = TRUE;
 
   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
   g_return_val_if_fail (func != NULL, FALSE);
 
+  list_was_writable = gst_buffer_list_is_writable (list);
+
   len = list->n_buffers;
   for (i = 0; i < len;) {
     GstBuffer *buf, *buf_ret;
+    gboolean was_writable;
 
     buf = buf_ret = list->buffers[i];
+
+    /* If the buffer is writable, we remove us as parent for now to
+     * allow the callback to destroy the buffer. If we get the buffer
+     * back, we add ourselves as parent again.
+     *
+     * Non-writable buffers just get another reference as they were not
+     * writable to begin with, and they would possibly become writable
+     * by removing ourselves as parent
+     */
+    was_writable = list_was_writable && gst_buffer_is_writable (buf);
+
+    if (was_writable)
+      gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (buf),
+          GST_MINI_OBJECT_CAST (list));
+    else
+      gst_buffer_ref (buf);
+
     ret = func (&buf_ret, i, user_data);
 
     /* Check if the function changed the buffer */
     if (buf != buf_ret) {
-      if (buf_ret == NULL) {
-        gst_buffer_list_remove_range_internal (list, i, 1, FALSE);
+      /* If the list was not writable but the callback was actually changing
+       * our buffer, then it wouldn't have been allowed to do so.
+       *
+       * Fortunately we still have a reference to the old buffer in that case
+       * and just not modify the list, unref the new buffer (if any) and warn
+       * about this */
+      if (!list_was_writable) {
+        if (first_warning) {
+          g_critical
+              ("gst_buffer_list_foreach: non-writable list %p was changed from callback",
+              list);
+          first_warning = FALSE;
+        }
+        if (buf_ret)
+          gst_buffer_unref (buf_ret);
+      } else if (buf_ret == NULL) {
+        gst_buffer_list_remove_range_internal (list, i, 1, !was_writable);
         --len;
       } else {
+        if (!was_writable)
+          gst_buffer_unref (buf);
+
         list->buffers[i] = buf_ret;
+        gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buf_ret),
+            GST_MINI_OBJECT_CAST (list));
       }
+    } else {
+      if (was_writable)
+        gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buf),
+            GST_MINI_OBJECT_CAST (list));
+      else
+        gst_buffer_unref (buf);
     }
 
     if (!ret)
@@ -272,6 +339,9 @@ gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
  *
  * Get the buffer at @idx.
  *
+ * You must make sure that @idx does not exceed the number of
+ * buffers available.
+ *
  * Returns: (transfer none) (nullable): the buffer at @idx in @group
  *     or %NULL when there is no buffer. The buffer remains valid as
  *     long as @list is valid and buffer is not removed from the list.
@@ -286,6 +356,47 @@ gst_buffer_list_get (GstBufferList * list, guint idx)
 }
 
 /**
+ * gst_buffer_list_get_writable:
+ * @list: a (writable) #GstBufferList
+ * @idx: the index
+ *
+ * Gets the buffer at @idx, ensuring it is a writable buffer.
+ *
+ * You must make sure that @idx does not exceed the number of
+ * buffers available.
+ *
+ * Returns: (transfer none) (nullable): the buffer at @idx in @group.
+ *     The returned  buffer remains valid as long as @list is valid and
+ *     the buffer is not removed from the list.
+ *
+ * Since: 1.14
+ */
+GstBuffer *
+gst_buffer_list_get_writable (GstBufferList * list, guint idx)
+{
+  GstBuffer *new_buf;
+
+  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
+  g_return_val_if_fail (gst_buffer_list_is_writable (list), NULL);
+  g_return_val_if_fail (idx < list->n_buffers, NULL);
+
+  /* We have to implement this manually here to correctly add/remove the
+   * parent */
+  if (gst_buffer_is_writable (list->buffers[idx]))
+    return list->buffers[idx];
+
+  gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[idx]),
+      GST_MINI_OBJECT_CAST (list));
+  new_buf = gst_buffer_copy (list->buffers[idx]);
+  gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (new_buf),
+      GST_MINI_OBJECT_CAST (list));
+  gst_buffer_unref (list->buffers[idx]);
+  list->buffers[idx] = new_buf;
+
+  return new_buf;
+}
+
+/**
  * gst_buffer_list_add:
  * @l: a #GstBufferList
  * @b: a #GstBuffer
@@ -313,6 +424,8 @@ gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
   g_return_if_fail (gst_buffer_list_is_writable (list));
 
   if (idx == -1 && list->n_buffers < list->n_allocated) {
+    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buffer),
+        GST_MINI_OBJECT_CAST (list));
     list->buffers[list->n_buffers++] = buffer;
     return;
   }
@@ -343,6 +456,8 @@ gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
 
   ++list->n_buffers;
   list->buffers[idx] = buffer;
+  gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buffer),
+      GST_MINI_OBJECT_CAST (list));
 }
 
 /**
@@ -403,3 +518,32 @@ gst_buffer_list_copy_deep (const GstBufferList * list)
 
   return result;
 }
+
+/**
+ * gst_buffer_list_calculate_size:
+ * @list: a #GstBufferList
+ *
+ * Calculates the size of the data contained in buffer list by adding the
+ * size of all buffers.
+ *
+ * Returns: the size of the data contained in buffer list in bytes.
+ *
+ * Since: 1.14
+ */
+gsize
+gst_buffer_list_calculate_size (GstBufferList * list)
+{
+  GstBuffer **buffers;
+  gsize size = 0;
+  guint i, n;
+
+  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
+
+  n = list->n_buffers;
+  buffers = list->buffers;
+
+  for (i = 0; i < n; ++i)
+    size += gst_buffer_get_size (buffers[i]);
+
+  return size;
+}