Blame gst/gstsample.c

Packit a6ee4b
/* GStreamer
Packit a6ee4b
 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
Packit a6ee4b
 *
Packit a6ee4b
 * gstsample.c: media sample
Packit a6ee4b
 *
Packit a6ee4b
 * This library is free software; you can redistribute it and/or
Packit a6ee4b
 * modify it under the terms of the GNU Library General Public
Packit a6ee4b
 * License as published by the Free Software Foundation; either
Packit a6ee4b
 * version 2 of the License, or (at your option) any later version.
Packit a6ee4b
 *
Packit a6ee4b
 * This library is distributed in the hope that it will be useful,
Packit a6ee4b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a6ee4b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a6ee4b
 * Library General Public License for more details.
Packit a6ee4b
 *
Packit a6ee4b
 * You should have received a copy of the GNU Library General Public
Packit a6ee4b
 * License along with this library; if not, write to the
Packit a6ee4b
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit a6ee4b
 * Boston, MA 02110-1301, USA.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * SECTION:gstsample
Packit a6ee4b
 * @title: GstSample
Packit a6ee4b
 * @short_description: A media sample
Packit a6ee4b
 * @see_also: #GstBuffer, #GstCaps, #GstSegment
Packit a6ee4b
 *
Packit a6ee4b
 * A #GstSample is a small object containing data, a type, timing and
Packit a6ee4b
 * extra arbitrary information.
Packit a6ee4b
 */
Packit a6ee4b
#include "gst_private.h"
Packit a6ee4b
Packit a6ee4b
#include "gstsample.h"
Packit a6ee4b
Packit a6ee4b
GST_DEBUG_CATEGORY_STATIC (gst_sample_debug);
Packit a6ee4b
#define GST_CAT_DEFAULT gst_sample_debug
Packit a6ee4b
Packit a6ee4b
struct _GstSample
Packit a6ee4b
{
Packit a6ee4b
  GstMiniObject mini_object;
Packit a6ee4b
Packit a6ee4b
  GstBuffer *buffer;
Packit a6ee4b
  GstCaps *caps;
Packit a6ee4b
  GstSegment segment;
Packit a6ee4b
  GstStructure *info;
Packit a6ee4b
  GstBufferList *buffer_list;
Packit a6ee4b
};
Packit a6ee4b
Packit a6ee4b
GType _gst_sample_type = 0;
Packit a6ee4b
Packit a6ee4b
GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
Packit a6ee4b
Packit a6ee4b
void
Packit a6ee4b
_priv_gst_sample_initialize (void)
Packit a6ee4b
{
Packit a6ee4b
  _gst_sample_type = gst_sample_get_type ();
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_CATEGORY_INIT (gst_sample_debug, "sample", 0, "GstSample debug");
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static GstSample *
Packit a6ee4b
_gst_sample_copy (GstSample * sample)
Packit a6ee4b
{
Packit a6ee4b
  GstSample *copy;
Packit a6ee4b
Packit a6ee4b
  copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
Packit a6ee4b
      (sample->info) ? gst_structure_copy (sample->info) : NULL);
Packit a6ee4b
Packit a6ee4b
  if (sample->buffer_list) {
Packit a6ee4b
    copy->buffer_list = gst_buffer_list_ref (sample->buffer_list);
Packit a6ee4b
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (copy->buffer_list),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (copy));
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return copy;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
_gst_sample_free (GstSample * sample)
Packit a6ee4b
{
Packit a6ee4b
  GST_LOG ("free %p", sample);
Packit a6ee4b
Packit a6ee4b
  if (sample->buffer) {
Packit a6ee4b
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
    gst_buffer_unref (sample->buffer);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (sample->caps) {
Packit a6ee4b
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->caps),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
    gst_caps_unref (sample->caps);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (sample->info) {
Packit a6ee4b
    gst_structure_set_parent_refcount (sample->info, NULL);
Packit a6ee4b
    gst_structure_free (sample->info);
Packit a6ee4b
  }
Packit a6ee4b
  if (sample->buffer_list) {
Packit a6ee4b
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
    gst_buffer_list_unref (sample->buffer_list);
Packit a6ee4b
  }
Packit a6ee4b
#ifdef USE_POISONING
Packit a6ee4b
  memset (sample, 0xff, sizeof (GstSample));
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
  g_slice_free1 (sizeof (GstSample), sample);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_new:
Packit a6ee4b
 * @buffer: (transfer none) (allow-none): a #GstBuffer, or %NULL
Packit a6ee4b
 * @caps: (transfer none) (allow-none): a #GstCaps, or %NULL
Packit a6ee4b
 * @segment: (transfer none) (allow-none): a #GstSegment, or %NULL
Packit a6ee4b
 * @info: (transfer full) (allow-none): a #GstStructure, or %NULL
Packit a6ee4b
 *
Packit a6ee4b
 * Create a new #GstSample with the provided details.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_sample_unref
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full): the new #GstSample. gst_sample_unref()
Packit a6ee4b
 *     after usage.
Packit a6ee4b
 */
Packit a6ee4b
GstSample *
Packit a6ee4b
gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
Packit a6ee4b
    GstStructure * info)
Packit a6ee4b
{
Packit a6ee4b
  GstSample *sample;
Packit a6ee4b
Packit a6ee4b
  sample = g_slice_new0 (GstSample);
Packit a6ee4b
Packit a6ee4b
  GST_LOG ("new %p", sample);
Packit a6ee4b
Packit a6ee4b
  gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
Packit a6ee4b
      (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
Packit a6ee4b
      (GstMiniObjectFreeFunction) _gst_sample_free);
Packit a6ee4b
Packit a6ee4b
  if (buffer) {
Packit a6ee4b
    sample->buffer = gst_buffer_ref (buffer);
Packit a6ee4b
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (caps) {
Packit a6ee4b
    sample->caps = gst_caps_ref (caps);
Packit a6ee4b
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
Packit a6ee4b
  if (segment)
Packit a6ee4b
    gst_segment_copy_into (segment, &sample->segment);
Packit a6ee4b
  else
Packit a6ee4b
    gst_segment_init (&sample->segment, GST_FORMAT_TIME);
Packit a6ee4b
Packit a6ee4b
  if (info) {
Packit a6ee4b
    if (!gst_structure_set_parent_refcount (info,
Packit a6ee4b
            &sample->mini_object.refcount))
Packit a6ee4b
      goto had_parent;
Packit a6ee4b
Packit a6ee4b
    sample->info = info;
Packit a6ee4b
  }
Packit a6ee4b
  return sample;
Packit a6ee4b
Packit a6ee4b
  /* ERRORS */
Packit a6ee4b
had_parent:
Packit a6ee4b
  {
Packit a6ee4b
    gst_sample_unref (sample);
Packit a6ee4b
    g_warning ("structure is already owned by another object");
Packit a6ee4b
    return NULL;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_get_buffer:
Packit a6ee4b
 * @sample: a #GstSample
Packit a6ee4b
 *
Packit a6ee4b
 * Get the buffer associated with @sample
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer none) (nullable): the buffer of @sample or %NULL
Packit a6ee4b
 *  when there is no buffer. The buffer remains valid as long as
Packit a6ee4b
 *  @sample is valid.  If you need to hold on to it for longer than
Packit a6ee4b
 *  that, take a ref to the buffer with gst_buffer_ref().
Packit a6ee4b
 */
Packit a6ee4b
GstBuffer *
Packit a6ee4b
gst_sample_get_buffer (GstSample * sample)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
Packit a6ee4b
Packit a6ee4b
  return sample->buffer;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_get_caps:
Packit a6ee4b
 * @sample: a #GstSample
Packit a6ee4b
 *
Packit a6ee4b
 * Get the caps associated with @sample
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer none) (nullable): the caps of @sample or %NULL
Packit a6ee4b
 *  when there is no caps. The caps remain valid as long as @sample is
Packit a6ee4b
 *  valid.  If you need to hold on to the caps for longer than that,
Packit a6ee4b
 *  take a ref to the caps with gst_caps_ref().
Packit a6ee4b
 */
Packit a6ee4b
GstCaps *
Packit a6ee4b
gst_sample_get_caps (GstSample * sample)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
Packit a6ee4b
Packit a6ee4b
  return sample->caps;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_get_segment:
Packit a6ee4b
 * @sample: a #GstSample
Packit a6ee4b
 *
Packit a6ee4b
 * Get the segment associated with @sample
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer none): the segment of @sample.
Packit a6ee4b
 *  The segment remains valid as long as @sample is valid.
Packit a6ee4b
 */
Packit a6ee4b
GstSegment *
Packit a6ee4b
gst_sample_get_segment (GstSample * sample)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
Packit a6ee4b
Packit a6ee4b
  return &sample->segment;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_get_info:
Packit a6ee4b
 * @sample: a #GstSample
Packit a6ee4b
 *
Packit a6ee4b
 * Get extra information associated with @sample.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer none) (nullable): the extra info of @sample.
Packit a6ee4b
 *  The info remains valid as long as @sample is valid.
Packit a6ee4b
 */
Packit a6ee4b
const GstStructure *
Packit a6ee4b
gst_sample_get_info (GstSample * sample)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
Packit a6ee4b
Packit a6ee4b
  return sample->info;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_get_buffer_list:
Packit a6ee4b
 * @sample: a #GstSample
Packit a6ee4b
 *
Packit a6ee4b
 * Get the buffer list associated with @sample
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer none) (nullable): the buffer list of @sample or %NULL
Packit a6ee4b
 *  when there is no buffer list. The buffer list remains valid as long as
Packit a6ee4b
 *  @sample is valid.  If you need to hold on to it for longer than
Packit a6ee4b
 *  that, take a ref to the buffer list with gst_mini_object_ref ().
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.6
Packit a6ee4b
 */
Packit a6ee4b
GstBufferList *
Packit a6ee4b
gst_sample_get_buffer_list (GstSample * sample)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
Packit a6ee4b
Packit a6ee4b
  return sample->buffer_list;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_set_buffer_list:
Packit a6ee4b
 * @sample: a #GstSample
Packit a6ee4b
 * @buffer_list: a #GstBufferList
Packit a6ee4b
 *
Packit a6ee4b
 * Set the buffer list associated with @sample. @sample must be writable.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.6
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_sample_set_buffer_list (GstSample * sample, GstBufferList * buffer_list)
Packit a6ee4b
{
Packit a6ee4b
  GstBufferList *old = NULL;
Packit a6ee4b
  g_return_if_fail (GST_IS_SAMPLE (sample));
Packit a6ee4b
  g_return_if_fail (gst_sample_is_writable (sample));
Packit a6ee4b
Packit a6ee4b
  old = sample->buffer_list;
Packit a6ee4b
Packit a6ee4b
  if (old == buffer_list)
Packit a6ee4b
    return;
Packit a6ee4b
Packit a6ee4b
  if (buffer_list) {
Packit a6ee4b
    sample->buffer_list = gst_buffer_list_ref (buffer_list);
Packit a6ee4b
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
  } else {
Packit a6ee4b
    sample->buffer_list = NULL;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (old) {
Packit a6ee4b
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
    gst_buffer_list_unref (old);
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_set_buffer:
Packit a6ee4b
 * @sample: A #GstSample
Packit a6ee4b
 * @buffer: (transfer none): A #GstBuffer
Packit a6ee4b
 *
Packit a6ee4b
 * Set the buffer associated with @sample. @sample must be writable.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.16
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_sample_set_buffer (GstSample * sample, GstBuffer * buffer)
Packit a6ee4b
{
Packit a6ee4b
  GstBuffer *old = NULL;
Packit a6ee4b
Packit a6ee4b
  g_return_if_fail (GST_IS_SAMPLE (sample));
Packit a6ee4b
  g_return_if_fail (gst_sample_is_writable (sample));
Packit a6ee4b
Packit a6ee4b
  old = sample->buffer;
Packit a6ee4b
Packit a6ee4b
  if (old == buffer)
Packit a6ee4b
    return;
Packit a6ee4b
Packit a6ee4b
  if (buffer) {
Packit a6ee4b
    sample->buffer = gst_buffer_ref (buffer);
Packit a6ee4b
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
  } else {
Packit a6ee4b
    sample->buffer = NULL;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (old) {
Packit a6ee4b
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
    gst_buffer_unref (old);
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_set_caps:
Packit a6ee4b
 * @sample: A #GstSample
Packit a6ee4b
 * @caps: (transfer none): A #GstCaps
Packit a6ee4b
 *
Packit a6ee4b
 * Set the caps associated with @sample. @sample must be writable.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.16
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_sample_set_caps (GstSample * sample, GstCaps * caps)
Packit a6ee4b
{
Packit a6ee4b
  GstCaps *old = NULL;
Packit a6ee4b
Packit a6ee4b
  g_return_if_fail (GST_IS_SAMPLE (sample));
Packit a6ee4b
  g_return_if_fail (gst_sample_is_writable (sample));
Packit a6ee4b
Packit a6ee4b
  old = sample->caps;
Packit a6ee4b
Packit a6ee4b
  if (old == caps)
Packit a6ee4b
    return;
Packit a6ee4b
Packit a6ee4b
  if (caps) {
Packit a6ee4b
    sample->caps = gst_caps_ref (caps);
Packit a6ee4b
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
  } else {
Packit a6ee4b
    sample->caps = NULL;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (old) {
Packit a6ee4b
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
Packit a6ee4b
        GST_MINI_OBJECT_CAST (sample));
Packit a6ee4b
    gst_caps_unref (old);
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_set_segment:
Packit a6ee4b
 * @sample: A #GstSample
Packit a6ee4b
 * @segment: (transfer none): A #GstSegment
Packit a6ee4b
 *
Packit a6ee4b
 * Set the segment associated with @sample. @sample must be writable.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.16
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_sample_set_segment (GstSample * sample, const GstSegment * segment)
Packit a6ee4b
{
Packit a6ee4b
  g_return_if_fail (GST_IS_SAMPLE (sample));
Packit a6ee4b
  g_return_if_fail (gst_sample_is_writable (sample));
Packit a6ee4b
Packit a6ee4b
  /* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
Packit a6ee4b
  if (segment)
Packit a6ee4b
    gst_segment_copy_into (segment, &sample->segment);
Packit a6ee4b
  else
Packit a6ee4b
    gst_segment_init (&sample->segment, GST_FORMAT_TIME);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_sample_set_info:
Packit a6ee4b
 * @sample: A #GstSample
Packit a6ee4b
 * @info: (transfer full): A #GstStructure
Packit a6ee4b
 *
Packit a6ee4b
 * Set the info structure associated with @sample. @sample must be writable,
Packit a6ee4b
 * and @info must not have a parent set already.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.16
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_sample_set_info (GstSample * sample, GstStructure * info)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_SAMPLE (sample), FALSE);
Packit a6ee4b
  g_return_val_if_fail (gst_sample_is_writable (sample), FALSE);
Packit a6ee4b
Packit a6ee4b
  if (info) {
Packit a6ee4b
    if (!gst_structure_set_parent_refcount (info,
Packit a6ee4b
            &sample->mini_object.refcount))
Packit a6ee4b
      goto had_parent;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (sample->info) {
Packit a6ee4b
    gst_structure_set_parent_refcount (sample->info, NULL);
Packit a6ee4b
    gst_structure_free (sample->info);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  sample->info = info;
Packit a6ee4b
Packit a6ee4b
  return TRUE;
Packit a6ee4b
Packit a6ee4b
had_parent:
Packit a6ee4b
  g_warning ("structure is already owned by another object");
Packit a6ee4b
  return FALSE;
Packit a6ee4b
}