sample: add new sample miniobject
authorWim Taymans <wim.taymans@collabora.co.uk>
Thu, 1 Dec 2011 15:37:46 +0000 (16:37 +0100)
committerWim Taymans <wim.taymans@collabora.co.uk>
Thu, 1 Dec 2011 15:37:46 +0000 (16:37 +0100)
Add a new simple miniobject that is a combination of a GstBuffer, GstCaps,
GstSegment and other arbitrary info organized in a GstStructure. This object can
be used to exchange samples between an element and the application or for
storing album art in tags etc.

gst/Makefile.am
gst/gst.c
gst/gst.h
gst/gst_private.h
gst/gstsample.c [new file with mode: 0644]
gst/gstsample.h [new file with mode: 0644]
win32/common/libgstreamer.def

index 5e43042..e797c63 100644 (file)
@@ -87,6 +87,7 @@ libgstreamer_@GST_MAJORMINOR@_la_SOURCES = \
        gstquery.c              \
        gstregistry.c           \
        gstregistrychunks.c     \
+       gstsample.c             \
        gstsegment.c            \
        gststructure.c          \
        gstsystemclock.c        \
@@ -178,6 +179,7 @@ gst_headers =                       \
        gstpoll.h               \
        gstpreset.h             \
        gstquery.h              \
+       gstsample.h             \
        gstsegment.h            \
        gststructure.h          \
        gstsystemclock.h        \
index 93a5ca7..1e3962f 100644 (file)
--- a/gst/gst.c
+++ b/gst/gst.c
@@ -767,6 +767,7 @@ init_post (GOptionContext * context, GOptionGroup * group, gpointer data,
   _priv_gst_buffer_initialize ();
   _priv_gst_message_initialize ();
   _priv_gst_buffer_list_initialize ();
+  _priv_gst_sample_initialize ();
   _priv_gst_value_initialize ();
   g_type_class_ref (gst_param_spec_fraction_get_type ());
   _priv_gst_tag_initialize ();
index c6c6cbf..125f873 100644 (file)
--- a/gst/gst.h
+++ b/gst/gst.h
@@ -69,6 +69,7 @@
 #include <gst/gstpreset.h>
 #include <gst/gstquery.h>
 #include <gst/gstregistry.h>
+#include <gst/gstsample.h>
 #include <gst/gstsegment.h>
 #include <gst/gststructure.h>
 #include <gst/gstsystemclock.h>
index e0c27fe..8eba370 100644 (file)
@@ -105,6 +105,7 @@ void  _priv_gst_memory_initialize (void);
 void  _priv_gst_meta_initialize (void);
 void  _priv_gst_plugin_initialize (void);
 void  _priv_gst_query_initialize (void);
+void  _priv_gst_sample_initialize (void);
 void  _priv_gst_tag_initialize (void);
 void  _priv_gst_value_initialize (void);
 void  _priv_gst_debug_init (void);
diff --git a/gst/gstsample.c b/gst/gstsample.c
new file mode 100644 (file)
index 0000000..227f9eb
--- /dev/null
@@ -0,0 +1,200 @@
+/* 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
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * SECTION:gstsample
+ * @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"
+
+/**
+ * GstSample:
+ *
+ */
+struct _GstSample
+{
+  GstMiniObject mini_object;
+
+  GstBuffer *buffer;
+  GstCaps *caps;
+  GstSegment segment;
+  GstStructure *info;
+};
+
+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 ();
+}
+
+static GstSample *
+_gst_sample_copy (GstSample * sample)
+{
+  GstSample *copy;
+
+  copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
+      gst_structure_copy (sample->info));
+
+  return copy;
+}
+
+static void
+_gst_sample_free (GstSample * sample)
+{
+  GST_LOG ("free %p", sample);
+
+  if (sample->buffer)
+    gst_buffer_unref (sample->buffer);
+  if (sample->caps)
+    gst_caps_unref (sample->caps);
+
+  g_slice_free1 (GST_MINI_OBJECT_SIZE (sample), sample);
+}
+
+/**
+ * gst_sample_new:
+ *
+ * Free-function: gst_sample_unref
+ *
+ * Returns: (transfer full): the new #GstSample. gst_sample_unref()
+ *     after usage.
+ *
+ * Since: 0.10.24
+ */
+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);
+
+  gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), _gst_sample_type,
+      sizeof (GstSample));
+
+  sample->mini_object.copy = (GstMiniObjectCopyFunction) _gst_sample_copy;
+  sample->mini_object.free = (GstMiniObjectFreeFunction) _gst_sample_free;
+
+  sample->buffer = buffer ? gst_buffer_ref (buffer) : NULL;
+  sample->caps = caps ? gst_caps_ref (caps) : NULL;
+
+  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
+ *
+ * Returns: (transfer none): the buffer of @sample or NULL when there
+ *  is no buffer. The buffer remains valid as long as @sample is valid.
+ */
+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
+ *
+ * Returns: (transfer none): the caps of @sample or NULL when there
+ *  is no caps. The caps remain valid as long as @sample is valid.
+ */
+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.
+ */
+const GstSegment *
+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.
+ *
+ * Returns: (transfer none): the extra info of @sample.
+ *  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;
+}
diff --git a/gst/gstsample.h b/gst/gstsample.h
new file mode 100644 (file)
index 0000000..8b25595
--- /dev/null
@@ -0,0 +1,136 @@
+/* GStreamer
+ * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
+ *                    2000 Wim Taymans <wtay@chello.be>
+ *
+ * gstsample.h: Header for GstSample object
+ *
+ * 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.
+ */
+
+
+#ifndef __GST_SAMPLE_H__
+#define __GST_SAMPLE_H__
+
+#include <gst/gstbuffer.h>
+#include <gst/gstcaps.h>
+#include <gst/gstsegment.h>
+
+G_BEGIN_DECLS
+
+extern GType _gst_sample_type;
+
+#define GST_TYPE_SAMPLE      (_gst_sample_type)
+#define GST_IS_SAMPLE(obj)   (GST_IS_MINI_OBJECT_TYPE(obj, GST_TYPE_SAMPLE))
+#define GST_SAMPLE_CAST(obj) ((GstSample *)obj)
+#define GST_SAMPLE(obj)      (GST_SAMPLE_CAST(obj))
+
+/**
+ * GstSample:
+ *
+ * The opaque structure of a #GstSample. A sample contains a typed memory
+ * block and the associated timing information. It is mainly used to
+ * exchange buffers with and application.
+ */
+typedef struct _GstSample GstSample;
+
+/**
+ * GST_SAMPLE_TRACE_NAME:
+ *
+ * The name used for tracing memory allocations.
+ */
+#define GST_SAMPLE_TRACE_NAME           "GstSample"
+
+GType gst_sample_get_type            (void);
+
+/* allocation */
+GstSample *          gst_sample_new           (GstBuffer          *buffer,
+                                               GstCaps            *caps,
+                                               const GstSegment   *segment,
+                                               GstStructure       *info);
+
+GstBuffer *          gst_sample_get_buffer    (GstSample *sample);
+GstCaps *            gst_sample_get_caps      (GstSample *sample);
+const GstSegment *   gst_sample_get_segment   (GstSample *sample);
+const GstStructure * gst_sample_get_info      (GstSample *sample);
+
+/* refcounting */
+/**
+ * gst_sample_ref:
+ * @sample: a #GstSample
+ *
+ * Increases the refcount of the given sample by one.
+ *
+ * Returns: (transfer full): @sample
+ */
+#ifdef _FOOL_GTK_DOC_
+G_INLINE_FUNC GstSample * gst_sample_ref (GstSample * sample);
+#endif
+
+static inline GstSample *
+gst_sample_ref (GstSample * sample)
+{
+  return GST_SAMPLE_CAST (gst_mini_object_ref (GST_MINI_OBJECT_CAST (
+      sample)));
+}
+
+/**
+ * gst_sample_unref:
+ * @sample: (transfer full): a #GstSample
+ *
+ * Decreases the refcount of the sample. If the refcount reaches 0, the
+ * sample will be freed.
+ */
+#ifdef _FOOL_GTK_DOC_
+G_INLINE_FUNC void gst_sample_unref (GstSample * sample);
+#endif
+
+static inline void
+gst_sample_unref (GstSample * sample)
+{
+  gst_mini_object_unref (GST_MINI_OBJECT_CAST (sample));
+}
+
+/**
+ * gst_value_set_sample:
+ * @v: a #GValue to receive the data
+ * @b: (transfer none): a #GstSample to assign to the GstValue
+ *
+ * Sets @b as the value of @v.  Caller retains reference to sample.
+ */
+#define         gst_value_set_sample(v,b)       g_value_set_boxed((v),(b))
+/**
+ * gst_value_take_sample:
+ * @v: a #GValue to receive the data
+ * @b: (transfer full): a #GstSample to assign to the GstValue
+ *
+ * Sets @b as the value of @v.  Caller gives away reference to sample.
+ */
+#define         gst_value_take_sample(v,b)      g_value_take_boxed(v,(b))
+/**
+ * gst_value_get_sample:
+ * @v: a #GValue to query
+ *
+ * Receives a #GstSample as the value of @v. Does not return a reference to
+ * the sample, so the pointer is only valid for as long as the caller owns
+ * a reference to @v.
+ *
+ * Returns: (transfer none): sample
+ */
+#define         gst_value_get_sample(v)         GST_SAMPLE_CAST (g_value_get_boxed(v))
+
+G_END_DECLS
+
+#endif /* __GST_SAMPLE_H__ */
index 4677626..0c14f5c 100644 (file)
@@ -47,6 +47,7 @@ EXPORTS
        _gst_element_error_printf
        _gst_event_type DATA
        _gst_plugin_loader_client_run
+       _gst_sample_type DATA
        _gst_structure_type DATA
        _gst_trace_add_entry
        _gst_trace_mutex DATA
@@ -942,6 +943,12 @@ EXPORTS
        gst_registry_scan_path
        gst_resource_error_get_type
        gst_resource_error_quark
+       gst_sample_get_buffer
+       gst_sample_get_caps
+       gst_sample_get_info
+       gst_sample_get_segment
+       gst_sample_get_type
+       gst_sample_new
        gst_scheduling_flags_get_type
        gst_search_mode_get_type
        gst_seek_flags_get_type