2011-12-01 15:37:46 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
*
|
|
|
|
* gstsample.c: media sample
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-12-01 15:37:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gstsample
|
2017-01-16 14:26:16 +00:00
|
|
|
* @title: GstSample
|
2011-12-01 15:37:46 +00:00
|
|
|
* @short_description: A media sample
|
|
|
|
* @see_also: #GstBuffer, #GstCaps, #GstSegment
|
|
|
|
*
|
|
|
|
* A #GstSample is a small object containing data, a type, timing and
|
|
|
|
* extra arbitrary information.
|
|
|
|
*/
|
|
|
|
#include "gst_private.h"
|
|
|
|
|
|
|
|
#include "gstsample.h"
|
|
|
|
|
2012-12-14 10:09:30 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_sample_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_sample_debug
|
|
|
|
|
2011-12-01 15:37:46 +00:00
|
|
|
struct _GstSample
|
|
|
|
{
|
|
|
|
GstMiniObject mini_object;
|
|
|
|
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstCaps *caps;
|
|
|
|
GstSegment segment;
|
|
|
|
GstStructure *info;
|
2015-06-22 10:35:40 +00:00
|
|
|
GstBufferList *buffer_list;
|
2011-12-01 15:37:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GType _gst_sample_type = 0;
|
|
|
|
|
|
|
|
GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
|
|
|
|
|
|
|
|
void
|
|
|
|
_priv_gst_sample_initialize (void)
|
|
|
|
{
|
|
|
|
_gst_sample_type = gst_sample_get_type ();
|
2012-12-14 10:09:30 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_sample_debug, "sample", 0, "GstSample debug");
|
2011-12-01 15:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstSample *
|
|
|
|
_gst_sample_copy (GstSample * sample)
|
|
|
|
{
|
|
|
|
GstSample *copy;
|
|
|
|
|
|
|
|
copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
|
2012-09-10 15:52:46 +00:00
|
|
|
(sample->info) ? gst_structure_copy (sample->info) : NULL);
|
2011-12-01 15:37:46 +00:00
|
|
|
|
2018-07-03 17:07:31 +00:00
|
|
|
if (sample->buffer_list) {
|
|
|
|
copy->buffer_list = gst_buffer_list_ref (sample->buffer_list);
|
|
|
|
gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (copy->buffer_list),
|
|
|
|
GST_MINI_OBJECT_CAST (copy));
|
|
|
|
}
|
2015-06-22 10:35:40 +00:00
|
|
|
|
2011-12-01 15:37:46 +00:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gst_sample_free (GstSample * sample)
|
|
|
|
{
|
|
|
|
GST_LOG ("free %p", sample);
|
|
|
|
|
2018-07-03 17:07:31 +00:00
|
|
|
if (sample->buffer) {
|
|
|
|
gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
2011-12-01 15:37:46 +00:00
|
|
|
gst_buffer_unref (sample->buffer);
|
2018-07-03 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sample->caps) {
|
|
|
|
gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->caps),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
2011-12-01 15:37:46 +00:00
|
|
|
gst_caps_unref (sample->caps);
|
2018-07-03 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 15:52:46 +00:00
|
|
|
if (sample->info) {
|
|
|
|
gst_structure_set_parent_refcount (sample->info, NULL);
|
|
|
|
gst_structure_free (sample->info);
|
|
|
|
}
|
2018-07-03 17:07:31 +00:00
|
|
|
if (sample->buffer_list) {
|
|
|
|
gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
|
|
|
gst_buffer_list_unref (sample->buffer_list);
|
|
|
|
}
|
2018-08-03 10:16:21 +00:00
|
|
|
#ifdef USE_POISONING
|
|
|
|
memset (sample, 0xff, sizeof (GstSample));
|
|
|
|
#endif
|
2015-06-22 10:35:40 +00:00
|
|
|
|
2012-06-14 22:38:54 +00:00
|
|
|
g_slice_free1 (sizeof (GstSample), sample);
|
2011-12-01 15:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_new:
|
2014-05-29 21:54:34 +00:00
|
|
|
* @buffer: (transfer none) (allow-none): a #GstBuffer, or %NULL
|
|
|
|
* @caps: (transfer none) (allow-none): a #GstCaps, or %NULL
|
|
|
|
* @segment: (transfer none) (allow-none): a #GstSegment, or %NULL
|
|
|
|
* @info: (transfer full) (allow-none): a #GstStructure, or %NULL
|
2012-01-03 14:03:38 +00:00
|
|
|
*
|
|
|
|
* Create a new #GstSample with the provided details.
|
2011-12-01 15:37:46 +00:00
|
|
|
*
|
|
|
|
* Free-function: gst_sample_unref
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): the new #GstSample. gst_sample_unref()
|
|
|
|
* after usage.
|
|
|
|
*/
|
|
|
|
GstSample *
|
|
|
|
gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
|
|
|
|
GstStructure * info)
|
|
|
|
{
|
|
|
|
GstSample *sample;
|
|
|
|
|
|
|
|
sample = g_slice_new0 (GstSample);
|
|
|
|
|
|
|
|
GST_LOG ("new %p", sample);
|
|
|
|
|
2012-07-04 14:38:15 +00:00
|
|
|
gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
|
2012-06-23 18:56:12 +00:00
|
|
|
(GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
|
|
|
|
(GstMiniObjectFreeFunction) _gst_sample_free);
|
2011-12-01 15:37:46 +00:00
|
|
|
|
2018-07-03 17:07:31 +00:00
|
|
|
if (buffer) {
|
|
|
|
sample->buffer = gst_buffer_ref (buffer);
|
|
|
|
gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (caps) {
|
|
|
|
sample->caps = gst_caps_ref (caps);
|
|
|
|
gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
|
|
|
}
|
2011-12-01 15:37:46 +00:00
|
|
|
|
2018-04-10 22:49:02 +00:00
|
|
|
/* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
|
2011-12-01 15:37:46 +00:00
|
|
|
if (segment)
|
|
|
|
gst_segment_copy_into (segment, &sample->segment);
|
|
|
|
else
|
|
|
|
gst_segment_init (&sample->segment, GST_FORMAT_TIME);
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
if (!gst_structure_set_parent_refcount (info,
|
|
|
|
&sample->mini_object.refcount))
|
|
|
|
goto had_parent;
|
|
|
|
|
|
|
|
sample->info = info;
|
|
|
|
}
|
|
|
|
return sample;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
had_parent:
|
|
|
|
{
|
|
|
|
gst_sample_unref (sample);
|
|
|
|
g_warning ("structure is already owned by another object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_get_buffer:
|
|
|
|
* @sample: a #GstSample
|
|
|
|
*
|
|
|
|
* Get the buffer associated with @sample
|
|
|
|
*
|
2014-06-11 22:21:34 +00:00
|
|
|
* Returns: (transfer none) (nullable): the buffer of @sample or %NULL
|
|
|
|
* when there is no buffer. The buffer remains valid as long as
|
|
|
|
* @sample is valid. If you need to hold on to it for longer than
|
|
|
|
* that, take a ref to the buffer with gst_buffer_ref().
|
2011-12-01 15:37:46 +00:00
|
|
|
*/
|
|
|
|
GstBuffer *
|
|
|
|
gst_sample_get_buffer (GstSample * sample)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
|
|
|
|
|
|
|
|
return sample->buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_get_caps:
|
|
|
|
* @sample: a #GstSample
|
|
|
|
*
|
|
|
|
* Get the caps associated with @sample
|
|
|
|
*
|
2014-06-11 22:21:34 +00:00
|
|
|
* Returns: (transfer none) (nullable): the caps of @sample or %NULL
|
|
|
|
* when there is no caps. The caps remain valid as long as @sample is
|
|
|
|
* valid. If you need to hold on to the caps for longer than that,
|
|
|
|
* take a ref to the caps with gst_caps_ref().
|
2011-12-01 15:37:46 +00:00
|
|
|
*/
|
|
|
|
GstCaps *
|
|
|
|
gst_sample_get_caps (GstSample * sample)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
|
|
|
|
|
|
|
|
return sample->caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_get_segment:
|
|
|
|
* @sample: a #GstSample
|
|
|
|
*
|
|
|
|
* Get the segment associated with @sample
|
|
|
|
*
|
|
|
|
* Returns: (transfer none): the segment of @sample.
|
|
|
|
* The segment remains valid as long as @sample is valid.
|
|
|
|
*/
|
2011-12-01 17:49:34 +00:00
|
|
|
GstSegment *
|
2011-12-01 15:37:46 +00:00
|
|
|
gst_sample_get_segment (GstSample * sample)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
|
|
|
|
|
|
|
|
return &sample->segment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_get_info:
|
|
|
|
* @sample: a #GstSample
|
|
|
|
*
|
|
|
|
* Get extra information associated with @sample.
|
|
|
|
*
|
2017-10-22 12:35:30 +00:00
|
|
|
* Returns: (transfer none) (nullable): the extra info of @sample.
|
2011-12-01 15:37:46 +00:00
|
|
|
* The info remains valid as long as @sample is valid.
|
|
|
|
*/
|
|
|
|
const GstStructure *
|
|
|
|
gst_sample_get_info (GstSample * sample)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
|
|
|
|
|
|
|
|
return sample->info;
|
|
|
|
}
|
2015-06-22 10:35:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_get_buffer_list:
|
|
|
|
* @sample: a #GstSample
|
|
|
|
*
|
|
|
|
* Get the buffer list associated with @sample
|
|
|
|
*
|
|
|
|
* Returns: (transfer none) (nullable): the buffer list of @sample or %NULL
|
|
|
|
* when there is no buffer list. The buffer list remains valid as long as
|
|
|
|
* @sample is valid. If you need to hold on to it for longer than
|
|
|
|
* that, take a ref to the buffer list with gst_mini_object_ref ().
|
|
|
|
*
|
|
|
|
* Since: 1.6
|
|
|
|
*/
|
|
|
|
GstBufferList *
|
|
|
|
gst_sample_get_buffer_list (GstSample * sample)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
|
|
|
|
|
|
|
|
return sample->buffer_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_set_buffer_list:
|
|
|
|
* @sample: a #GstSample
|
|
|
|
* @buffer_list: a #GstBufferList
|
|
|
|
*
|
2018-04-10 22:49:02 +00:00
|
|
|
* Set the buffer list associated with @sample. @sample must be writable.
|
2015-06-22 10:35:40 +00:00
|
|
|
*
|
|
|
|
* Since: 1.6
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_sample_set_buffer_list (GstSample * sample, GstBufferList * buffer_list)
|
|
|
|
{
|
|
|
|
GstBufferList *old = NULL;
|
|
|
|
g_return_if_fail (GST_IS_SAMPLE (sample));
|
2018-04-10 22:49:02 +00:00
|
|
|
g_return_if_fail (gst_sample_is_writable (sample));
|
|
|
|
|
2015-06-22 10:35:40 +00:00
|
|
|
old = sample->buffer_list;
|
|
|
|
|
2018-07-03 17:07:31 +00:00
|
|
|
if (old == buffer_list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (buffer_list) {
|
|
|
|
sample->buffer_list = gst_buffer_list_ref (buffer_list);
|
|
|
|
gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
2018-07-10 06:48:47 +00:00
|
|
|
} else {
|
|
|
|
sample->buffer_list = NULL;
|
2018-07-03 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old) {
|
|
|
|
gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
|
|
|
gst_buffer_list_unref (old);
|
|
|
|
}
|
2015-06-22 10:35:40 +00:00
|
|
|
}
|
2018-04-10 22:49:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_set_buffer:
|
2018-05-21 09:36:42 +00:00
|
|
|
* @sample: A #GstSample
|
|
|
|
* @buffer: (transfer none): A #GstBuffer
|
2018-04-10 22:49:02 +00:00
|
|
|
*
|
|
|
|
* Set the buffer associated with @sample. @sample must be writable.
|
|
|
|
*
|
|
|
|
* Since: 1.16
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_sample_set_buffer (GstSample * sample, GstBuffer * buffer)
|
|
|
|
{
|
2018-07-03 17:07:31 +00:00
|
|
|
GstBuffer *old = NULL;
|
|
|
|
|
2018-04-10 22:49:02 +00:00
|
|
|
g_return_if_fail (GST_IS_SAMPLE (sample));
|
|
|
|
g_return_if_fail (gst_sample_is_writable (sample));
|
|
|
|
|
2018-07-03 17:07:31 +00:00
|
|
|
old = sample->buffer;
|
|
|
|
|
|
|
|
if (old == buffer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (buffer) {
|
|
|
|
sample->buffer = gst_buffer_ref (buffer);
|
|
|
|
gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
2018-07-10 06:48:47 +00:00
|
|
|
} else {
|
|
|
|
sample->buffer = NULL;
|
2018-07-03 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old) {
|
|
|
|
gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
|
|
|
gst_buffer_unref (old);
|
|
|
|
}
|
2018-04-10 22:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_set_caps:
|
2018-05-21 09:36:42 +00:00
|
|
|
* @sample: A #GstSample
|
|
|
|
* @caps: (transfer none): A #GstCaps
|
2018-04-10 22:49:02 +00:00
|
|
|
*
|
|
|
|
* Set the caps associated with @sample. @sample must be writable.
|
|
|
|
*
|
|
|
|
* Since: 1.16
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_sample_set_caps (GstSample * sample, GstCaps * caps)
|
|
|
|
{
|
2018-07-03 17:07:31 +00:00
|
|
|
GstCaps *old = NULL;
|
|
|
|
|
2018-04-10 22:49:02 +00:00
|
|
|
g_return_if_fail (GST_IS_SAMPLE (sample));
|
|
|
|
g_return_if_fail (gst_sample_is_writable (sample));
|
|
|
|
|
2018-07-03 17:07:31 +00:00
|
|
|
old = sample->caps;
|
|
|
|
|
|
|
|
if (old == caps)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (caps) {
|
|
|
|
sample->caps = gst_caps_ref (caps);
|
|
|
|
gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
2018-07-10 06:48:47 +00:00
|
|
|
} else {
|
|
|
|
sample->caps = NULL;
|
2018-07-03 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old) {
|
|
|
|
gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
|
|
|
|
GST_MINI_OBJECT_CAST (sample));
|
|
|
|
gst_caps_unref (old);
|
|
|
|
}
|
2018-04-10 22:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_set_segment:
|
2018-05-21 09:36:42 +00:00
|
|
|
* @sample: A #GstSample
|
|
|
|
* @segment: (transfer none): A #GstSegment
|
2018-04-10 22:49:02 +00:00
|
|
|
*
|
|
|
|
* Set the segment associated with @sample. @sample must be writable.
|
|
|
|
*
|
|
|
|
* Since: 1.16
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_sample_set_segment (GstSample * sample, const GstSegment * segment)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_SAMPLE (sample));
|
|
|
|
g_return_if_fail (gst_sample_is_writable (sample));
|
|
|
|
|
|
|
|
/* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
|
|
|
|
if (segment)
|
|
|
|
gst_segment_copy_into (segment, &sample->segment);
|
|
|
|
else
|
|
|
|
gst_segment_init (&sample->segment, GST_FORMAT_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_sample_set_info:
|
2018-05-21 09:36:42 +00:00
|
|
|
* @sample: A #GstSample
|
|
|
|
* @info: (transfer full): A #GstStructure
|
2018-04-10 22:49:02 +00:00
|
|
|
*
|
|
|
|
* Set the info structure associated with @sample. @sample must be writable,
|
|
|
|
* and @info must not have a parent set already.
|
|
|
|
*
|
|
|
|
* Since: 1.16
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_sample_set_info (GstSample * sample, GstStructure * info)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_SAMPLE (sample), FALSE);
|
|
|
|
g_return_val_if_fail (gst_sample_is_writable (sample), FALSE);
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
if (!gst_structure_set_parent_refcount (info,
|
|
|
|
&sample->mini_object.refcount))
|
|
|
|
goto had_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sample->info) {
|
|
|
|
gst_structure_set_parent_refcount (sample->info, NULL);
|
|
|
|
gst_structure_free (sample->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
sample->info = info;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
had_parent:
|
|
|
|
g_warning ("structure is already owned by another object");
|
|
|
|
return FALSE;
|
|
|
|
}
|