context: add helper object to manage events

Add a helper object to manage the events that define the context of a buffer and
a stream.
This commit is contained in:
Wim Taymans 2011-05-04 15:20:10 +02:00
parent 7d3fabc521
commit 0fee137dbc
4 changed files with 331 additions and 1 deletions

View file

@ -56,6 +56,7 @@ libgstreamer_@GST_MAJORMINOR@_la_SOURCES = \
gstcaps.c \
gstchildproxy.c \
gstclock.c \
gstcontext.c \
gstdatetime.c \
gstdebugutils.c \
gstelement.c \
@ -151,6 +152,7 @@ gst_headers = \
gstchildproxy.h \
gstclock.h \
gstcompat.h \
gstcontext.h \
gstdatetime.h \
gstdebugutils.h \
gstelement.h \

183
gst/gstcontext.c Normal file
View file

@ -0,0 +1,183 @@
/* GStreamer
* Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
*
* gstcontext.c: GstContext subsystem
*
* 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:gstcontext
* @short_description: Structure containing events describing the context
* for buffers in a pipeline
* @see_also: #GstPad, #GstBuffer
*
* Last reviewed on 2011-05-4 (0.11.0)
*/
#include "gst_private.h"
#include "gstinfo.h"
#include "gstcontext.h"
#include "gstutils.h"
#include "gstquark.h"
struct _GstContext
{
GstMiniObject mini_object;
/*< private > */
GstEvent *events[GST_EVENT_MAX_STICKY];
gpointer _gst_reserved[GST_PADDING];
};
static GType _gst_context_type = 0;
GType
gst_context_get_type (void)
{
if (G_UNLIKELY (_gst_context_type == 0)) {
_gst_context_type = gst_mini_object_register ("GstContext");
}
return _gst_context_type;
}
static void
_gst_context_free (GstContext * context)
{
g_return_if_fail (context != NULL);
g_return_if_fail (GST_IS_CONTEXT (context));
GST_LOG ("freeing context %p", context);
gst_context_clear (context);
g_slice_free1 (GST_MINI_OBJECT_SIZE (context), context);
}
static void gst_context_init (GstContext * context, gsize size);
static GstContext *
_gst_context_copy (GstContext * context)
{
GstContext *copy;
guint i;
copy = g_slice_new0 (GstContext);
gst_context_init (copy, sizeof (GstContext));
for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
gst_event_replace (&copy->events[i], context->events[i]);
return copy;
}
static void
gst_context_init (GstContext * context, gsize size)
{
gst_mini_object_init (GST_MINI_OBJECT_CAST (context), _gst_context_type,
size);
context->mini_object.copy = (GstMiniObjectCopyFunction) _gst_context_copy;
context->mini_object.free = (GstMiniObjectFreeFunction) _gst_context_free;
}
/**
* gst_context_new:
*
* Create a new #GstContext object that can be used to manage events.
*
* Returns: (transfer full): a new #GstContext
*/
GstContext *
gst_context_new (void)
{
GstContext *context;
context = g_slice_new0 (GstContext);
GST_DEBUG ("creating new context %p", context);
gst_context_init (context, sizeof (GstContext));
return context;
}
/**
* gst_context_update:
* @context: a #GstContext
* @event: a #GstEvent
*
* Update @context with @event. The context must be writable.
*/
void
gst_context_update (GstContext * context, GstEvent * event)
{
guint idx;
g_return_if_fail (context != NULL);
g_return_if_fail (gst_context_is_writable (context));
idx = GST_EVENT_STICKY_IDX (event);
GST_LOG ("storing event %s at index %u", GST_EVENT_TYPE_NAME (event), idx);
gst_event_replace (&context->events[idx], event);
}
/**
* gst_context_get:
* @context: a #GstContext
* @type: a #GstEventType
*
* Get the event of @type from @context.
*
* Returns: the last #GstEvent of @type that was updated on @context. This
* function returns NULL when there is no event with the given type.
*/
GstEvent *
gst_context_get (GstContext * context, GstEventType type)
{
guint idx;
GstEvent *event = NULL;
g_return_val_if_fail (context != NULL, NULL);
idx = GST_EVENT_STICKY_IDX_TYPE (type);
if ((event = context->events[idx]))
gst_event_ref (event);
return event;
}
/**
* gst_context_clear:
* @context: a #GstContext
*
* Clear all stored events in @context
*/
void
gst_context_clear (GstContext * context)
{
guint i;
for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
gst_event_replace (&context->events[i], NULL);
}

144
gst/gstcontext.h Normal file
View file

@ -0,0 +1,144 @@
/* GStreamer
* Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
*
* gstcontext.h: Header for GstContext subsystem
*
* 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_CONTEXT_H__
#define __GST_CONTEXT_H__
#include <gst/gstminiobject.h>
#include <gst/gstevent.h>
G_BEGIN_DECLS
#define GST_CONTEXT_TRACE_NAME "GstContext"
typedef struct _GstContext GstContext;
#define GST_TYPE_CONTEXT (gst_context_get_type())
#define GST_IS_CONTEXT(obj) (GST_IS_MINI_OBJECT_TYPE (obj, GST_TYPE_CONTEXT))
#define GST_CONTEXT(obj) ((GstContext *)(obj))
#define GST_CONTEXT_CAST(obj) ((GstContext *)(obj))
/**
* gst_context_is_writable:
* @ctx: a #GstContext
*
* Tests if you can safely update @ctx with new events.
*/
#define gst_context_is_writable(ctx) gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (ctx))
/**
* gst_context_make_writable:
* @ctx: (transfer full): a #GstContext
*
* Makes a writable context from the given context. If the source context is
* already writable, this will simply return the same context. A copy will
* otherwise be made using gst_context_copy().
*
* Returns: (transfer full): a writable context which may or may not be the
* same as @ctx
*/
#define gst_context_make_writable(ctx) GST_CONTEXT_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (buf)))
/**
* gst_context_replace:
* @old_ctx: (inout) (transfer full): pointer to a pointer to a #GstContext
* to be replaced.
* @new_ctx: (allow-none) (transfer none): pointer to a #GstContext that will
* replace the context pointed to by @old_context.
*
* Modifies a pointer to a #GstContext to point to a different #GstContext. The
* modification is done atomically (so this is useful for ensuring thread safety
* in some cases), and the reference counts are updated appropriately (the old
* context is unreffed, the new one is reffed).
*
* Either @new_context or the #GstContext pointed to by @old_context may be NULL.
*/
#define gst_context_replace(old_ctx,new_ctx) \
gst_mini_object_replace ((GstMiniObject **)(old_ctx), GST_MINI_OBJECT_CAST (new_ctx))
GType gst_context_get_type (void);
/* refcounting */
/**
* gst_context_ref:
* @context: The context to refcount
*
* Increase the refcount of this context.
*
* Returns: (transfer full): @context (for convenience when doing assignments)
*/
#ifdef _FOOL_GTK_DOC_
G_INLINE_FUNC GstContext * gst_context_ref (GstContext * context);
#endif
static inline GstContext *
gst_context_ref (GstContext * context)
{
return (GstContext *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (context));
}
/**
* gst_context_unref:
* @context: (transfer full): the context to refcount
*
* Decrease the refcount of an context, freeing it if the refcount reaches 0.
*/
#ifdef _FOOL_GTK_DOC_
G_INLINE_FUNC void gst_context_unref (GstContext * context);
#endif
static inline void
gst_context_unref (GstContext * context)
{
gst_mini_object_unref (GST_MINI_OBJECT_CAST (context));
}
/* copy context */
/**
* gst_context_copy:
* @context: The context to copy
*
* Copy the context using the context specific copy function.
*
* Returns: (transfer full): the new context
*/
#ifdef _FOOL_GTK_DOC_
G_INLINE_FUNC GstContext * gst_context_copy (const GstContext * context);
#endif
static inline GstContext *
gst_context_copy (const GstContext * context)
{
return GST_CONTEXT_CAST (gst_mini_object_copy (GST_MINI_OBJECT_CONST_CAST (context)));
}
GstContext * gst_context_new (void);
void gst_context_update (GstContext *context, GstEvent *event);
GstEvent * gst_context_get (GstContext *context, GstEventType type);
void gst_context_clear (GstContext *context);
G_END_DECLS
#endif /* __GST_CONTEXT_H__ */

View file

@ -80,7 +80,8 @@ typedef enum {
#define FLAG(name) GST_EVENT_TYPE_##name
#define GST_EVENT_STICKY_IDX(ev) ((GST_EVENT_TYPE(ev) >> GST_EVENT_STICKY_SHIFT) & 0xf)
#define GST_EVENT_STICKY_IDX_TYPE(type) (((type) >> GST_EVENT_STICKY_SHIFT) & 0xf)
#define GST_EVENT_STICKY_IDX(ev) GST_EVENT_STICKY_IDX_TYPE(GST_EVENT_TYPE(ev))
/**
* GstEventType: