2002-07-08 19:12:38 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
2005-03-07 18:27:42 +00:00
|
|
|
* 2000 Wim Taymans <wim@fluendo.com>
|
2002-07-08 19:12:38 +00:00
|
|
|
*
|
|
|
|
* gstdata.c: Data operations
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gst_private.h"
|
|
|
|
|
|
|
|
#include "gstdata.h"
|
|
|
|
#include "gstdata_private.h"
|
2003-06-29 14:05:49 +00:00
|
|
|
#include "gstinfo.h"
|
2005-04-24 22:49:45 +00:00
|
|
|
#include "gstutils.h"
|
2002-07-08 19:12:38 +00:00
|
|
|
|
2004-02-27 17:58:24 +00:00
|
|
|
GType
|
|
|
|
gst_data_get_type (void)
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2004-02-27 17:58:24 +00:00
|
|
|
if (!type)
|
|
|
|
type = g_boxed_type_register_static ("GstData",
|
2004-03-15 19:27:17 +00:00
|
|
|
(GBoxedCopyFunc) gst_data_copy, (GBoxedFreeFunc) gst_data_unref);
|
2004-03-13 15:27:01 +00:00
|
|
|
return type;
|
2004-02-27 17:58:24 +00:00
|
|
|
}
|
|
|
|
|
2002-07-13 23:12:22 +00:00
|
|
|
/**
|
|
|
|
* gst_data_init:
|
|
|
|
* @data: a #GstData to initialize
|
|
|
|
* @type: the type of this data
|
|
|
|
* @flags: flags for this data
|
2005-03-07 18:27:42 +00:00
|
|
|
* @free: a free function
|
|
|
|
* @copy: a copy function
|
2002-07-13 23:12:22 +00:00
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* Initialize the given data structure with the given parameters.
|
|
|
|
* The free and copy function will be called when this data is freed
|
|
|
|
* or copied, respectively.
|
2002-07-13 23:12:22 +00:00
|
|
|
*/
|
2002-07-08 19:12:38 +00:00
|
|
|
void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_data_init (GstData * data, GType type, guint16 flags,
|
|
|
|
GstDataFreeFunction free, GstDataCopyFunction copy)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_if_fail (data != NULL);
|
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
_GST_DATA_INIT (data, type, flags, free, copy);
|
|
|
|
}
|
|
|
|
|
2002-07-13 23:12:22 +00:00
|
|
|
/**
|
|
|
|
* gst_data_copy_into:
|
|
|
|
* @data: a #GstData to copy
|
|
|
|
* @target: the target #GstData to copy into
|
|
|
|
*
|
|
|
|
* Copy the GstData into the specified target GstData structure.
|
2005-03-07 18:27:42 +00:00
|
|
|
* This method is mainly used by subclasses when they want to copy
|
2002-07-13 23:12:22 +00:00
|
|
|
* the relevant GstData info.
|
|
|
|
*/
|
|
|
|
void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_data_copy_into (const GstData * data, GstData * target)
|
2002-07-13 23:12:22 +00:00
|
|
|
{
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_if_fail (data != NULL);
|
2002-07-13 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_data_dispose:
|
|
|
|
* @data: a #GstData to dispose
|
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* Free all the resources allocated in the gst_data_init() function,
|
2002-07-13 23:12:22 +00:00
|
|
|
* mainly used by subclass implementors.
|
|
|
|
*/
|
2002-07-08 19:12:38 +00:00
|
|
|
void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_data_dispose (GstData * data)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_if_fail (data != NULL);
|
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
_GST_DATA_DISPOSE (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_data_copy:
|
|
|
|
* @data: a #GstData to copy
|
|
|
|
*
|
2002-07-13 23:12:22 +00:00
|
|
|
* Copies the given #GstData. This function will call the custom subclass
|
|
|
|
* copy function or return NULL if no function was provided by the subclass.
|
2002-07-08 19:12:38 +00:00
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* Returns: a copy of the data or NULL if the data cannot be copied.
|
|
|
|
* The refcount of the original buffer is not changed so you should unref it
|
|
|
|
* when you don't need it anymore.
|
|
|
|
*
|
|
|
|
* MT safe.
|
2002-07-08 19:12:38 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstData *
|
|
|
|
gst_data_copy (const GstData * data)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_val_if_fail (data != NULL, NULL);
|
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
if (data->copy)
|
2004-03-13 15:27:01 +00:00
|
|
|
return data->copy (data);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-11-19 02:29:18 +00:00
|
|
|
/**
|
2003-04-16 18:35:57 +00:00
|
|
|
* gst_data_is_writable:
|
2005-03-07 18:27:42 +00:00
|
|
|
* @data: a #GstData to check
|
2002-11-19 02:29:18 +00:00
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* Query if the data needs to be copied before it can safely be modified.
|
2002-12-14 13:02:16 +00:00
|
|
|
*
|
2003-04-15 21:33:08 +00:00
|
|
|
* Returns: FALSE if the given #GstData is potentially shared and needs to
|
2002-11-19 02:29:18 +00:00
|
|
|
* be copied before it can be modified safely.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2002-11-19 02:29:18 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_data_is_writable (GstData * data)
|
2002-11-19 02:29:18 +00:00
|
|
|
{
|
|
|
|
gint refcount;
|
|
|
|
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_val_if_fail (data != NULL, FALSE);
|
|
|
|
|
2005-04-24 22:49:45 +00:00
|
|
|
refcount = g_atomic_int_get (&data->refcount);
|
2002-11-19 02:29:18 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* if we have the only ref and the data is not readonly, we can
|
|
|
|
* safely write */
|
|
|
|
if (refcount == 1 && !GST_DATA_FLAG_IS_SET (data, GST_DATA_READONLY))
|
|
|
|
return TRUE;
|
2002-11-19 02:29:18 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
return FALSE;
|
2002-11-19 02:29:18 +00:00
|
|
|
}
|
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
/**
|
|
|
|
* gst_data_copy_on_write:
|
|
|
|
* @data: a #GstData to copy
|
|
|
|
*
|
|
|
|
* Copies the given #GstData if the refcount is greater than 1 so that the
|
|
|
|
* #GstData object can be written to safely.
|
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* Returns: a copy of the data if the refcount is > 1 or the buffer is
|
2002-09-15 13:45:26 +00:00
|
|
|
* marked READONLY, data if the refcount == 1,
|
2005-03-07 18:27:42 +00:00
|
|
|
* or NULL if the data could not be copied.
|
|
|
|
*
|
|
|
|
* The refcount of the passed @data is decreased when a copy is made, so
|
|
|
|
* you are not supposed to use it anymore after a call to this function.
|
|
|
|
*
|
|
|
|
* MT safe.
|
2002-07-08 19:12:38 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstData *
|
|
|
|
gst_data_copy_on_write (GstData * data)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
gint refcount;
|
|
|
|
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_val_if_fail (data != NULL, NULL);
|
|
|
|
|
2005-04-24 22:49:45 +00:00
|
|
|
refcount = g_atomic_int_get (&data->refcount);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
/* if we have the only ref and the data is not readonly, we can
|
|
|
|
* safely write, so we return the input data */
|
2002-07-25 18:47:48 +00:00
|
|
|
if (refcount == 1 && !GST_DATA_FLAG_IS_SET (data, GST_DATA_READONLY))
|
2002-07-08 19:12:38 +00:00
|
|
|
return GST_DATA (data);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-07-25 18:47:48 +00:00
|
|
|
if (data->copy) {
|
2004-03-13 15:27:01 +00:00
|
|
|
GstData *copy = data->copy (data);
|
|
|
|
|
2002-07-25 18:47:48 +00:00
|
|
|
gst_data_unref (data);
|
2004-03-13 15:27:01 +00:00
|
|
|
return copy;
|
2002-07-25 18:47:48 +00:00
|
|
|
}
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_data_ref:
|
|
|
|
* @data: a #GstData to reference
|
|
|
|
*
|
|
|
|
* Increments the reference count of this data.
|
|
|
|
*
|
|
|
|
* Returns: the data
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2002-07-08 19:12:38 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstData *
|
|
|
|
gst_data_ref (GstData * data)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (data != NULL, NULL);
|
2004-03-13 15:27:01 +00:00
|
|
|
g_return_val_if_fail (GST_DATA_REFCOUNT_VALUE (data) > 0, NULL);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
2004-02-20 13:18:32 +00:00
|
|
|
GST_CAT_LOG (GST_CAT_BUFFER, "%p %d->%d", data,
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_DATA_REFCOUNT_VALUE (data), GST_DATA_REFCOUNT_VALUE (data) + 1);
|
|
|
|
|
2005-04-24 22:49:45 +00:00
|
|
|
g_atomic_int_inc (&data->refcount);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_data_ref_by_count:
|
|
|
|
* @data: a #GstData to reference
|
|
|
|
* @count: the number to increment the reference count by
|
|
|
|
*
|
|
|
|
* Increments the reference count of this data by the given number.
|
|
|
|
*
|
|
|
|
* Returns: the data
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2002-07-08 19:12:38 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstData *
|
|
|
|
gst_data_ref_by_count (GstData * data, gint count)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (data != NULL, NULL);
|
|
|
|
g_return_val_if_fail (count >= 0, NULL);
|
2004-03-13 15:27:01 +00:00
|
|
|
g_return_val_if_fail (GST_DATA_REFCOUNT_VALUE (data) > 0, NULL);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
2004-02-20 13:18:32 +00:00
|
|
|
GST_CAT_LOG (GST_CAT_BUFFER, "%p %d->%d", data,
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_DATA_REFCOUNT_VALUE (data), GST_DATA_REFCOUNT_VALUE (data) + count);
|
2004-02-20 13:18:32 +00:00
|
|
|
|
2005-04-24 22:49:45 +00:00
|
|
|
g_atomic_int_add (&data->refcount, count);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_data_unref:
|
|
|
|
* @data: a #GstData to unreference
|
|
|
|
*
|
|
|
|
* Decrements the refcount of this data. If the refcount is
|
2002-11-28 00:10:38 +00:00
|
|
|
* zero, the data will be freed.
|
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* When you move data out of your element into the pipeline,
|
|
|
|
* the pipeline takes ownership of the
|
|
|
|
* data. When the data has been consumed by some element, it must unref() it.
|
|
|
|
* Applications usually don't need to unref() @data.
|
|
|
|
*
|
|
|
|
* MT safe.
|
2002-07-08 19:12:38 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
void
|
|
|
|
gst_data_unref (GstData * data)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
gint zero;
|
|
|
|
|
|
|
|
g_return_if_fail (data != NULL);
|
|
|
|
|
2004-02-20 13:18:32 +00:00
|
|
|
GST_CAT_LOG (GST_CAT_BUFFER, "%p %d->%d", data,
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_DATA_REFCOUNT_VALUE (data), GST_DATA_REFCOUNT_VALUE (data) - 1);
|
2002-08-29 10:29:51 +00:00
|
|
|
g_return_if_fail (GST_DATA_REFCOUNT_VALUE (data) > 0);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
2005-04-24 22:49:45 +00:00
|
|
|
zero = g_atomic_int_dec_and_test (&data->refcount);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
/* if we ended up with the refcount at zero, free the data */
|
|
|
|
if (zero) {
|
2004-03-13 15:27:01 +00:00
|
|
|
if (data->free)
|
|
|
|
data->free (data);
|
2002-07-08 19:12:38 +00:00
|
|
|
}
|
|
|
|
}
|