Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstcontext.c
1 /* GStreamer
2  * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * gstcontext.c: GstContext subsystem
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gstcontext
24  * @short_description: Structure containing events describing the context
25  *                     for buffers in a pipeline
26  * @see_also: #GstPad, #GstBuffer
27  *
28  * Last reviewed on 2011-05-4 (0.11.0)
29  */
30
31
32 #include "gst_private.h"
33
34 #include "gstinfo.h"
35 #include "gstcontext.h"
36 #include "gstutils.h"
37 #include "gstquark.h"
38
39 struct _GstContext
40 {
41   GstMiniObject mini_object;
42
43   /*< private > */
44   GstEvent *events[GST_EVENT_MAX_STICKY];
45
46   gpointer _gst_reserved[GST_PADDING];
47 };
48
49 static GType _gst_context_type = 0;
50
51 GType
52 gst_context_get_type (void)
53 {
54   if (G_UNLIKELY (_gst_context_type == 0)) {
55     _gst_context_type = gst_mini_object_register ("GstContext");
56   }
57   return _gst_context_type;
58 }
59
60 static void
61 _gst_context_free (GstContext * context)
62 {
63   GST_LOG ("freeing context %p", context);
64
65   g_return_if_fail (context != NULL);
66   g_return_if_fail (GST_IS_CONTEXT (context));
67
68   gst_context_clear (context);
69
70   g_slice_free1 (GST_MINI_OBJECT_SIZE (context), context);
71 }
72
73 static void gst_context_init (GstContext * context, gsize size);
74
75 static GstContext *
76 _gst_context_copy (GstContext * context)
77 {
78   GstContext *copy;
79   guint i;
80
81   copy = g_slice_new0 (GstContext);
82
83   gst_context_init (copy, sizeof (GstContext));
84
85   for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
86     gst_event_replace (&copy->events[i], context->events[i]);
87
88   return copy;
89 }
90
91 static void
92 gst_context_init (GstContext * context, gsize size)
93 {
94   gst_mini_object_init (GST_MINI_OBJECT_CAST (context), _gst_context_type,
95       size);
96
97   context->mini_object.copy = (GstMiniObjectCopyFunction) _gst_context_copy;
98   context->mini_object.free = (GstMiniObjectFreeFunction) _gst_context_free;
99 }
100
101 /**
102  * gst_context_new:
103  *
104  * Create a new #GstContext object that can be used to manage events.
105  *
106  * Returns: (transfer full): a new #GstContext
107  */
108 GstContext *
109 gst_context_new (void)
110 {
111   GstContext *context;
112
113   context = g_slice_new0 (GstContext);
114
115   GST_DEBUG ("creating new context %p", context);
116
117   gst_context_init (context, sizeof (GstContext));
118
119   return context;
120 }
121
122 /**
123  * gst_context_update:
124  * @context: a #GstContext
125  * @event: a #GstEvent
126  *
127  * Update @context with @event. The context must be writable.
128  */
129 void
130 gst_context_update (GstContext * context, GstEvent * event)
131 {
132   guint idx;
133
134   g_return_if_fail (context != NULL);
135   g_return_if_fail (gst_context_is_writable (context));
136
137   idx = GST_EVENT_STICKY_IDX (event);
138
139   GST_LOG ("storing event %s at index %u", GST_EVENT_TYPE_NAME (event), idx);
140
141   gst_event_replace (&context->events[idx], event);
142 }
143
144 /**
145  * gst_context_get:
146  * @context: a #GstContext
147  * @type: a #GstEventType
148  *
149  * Get the event of @type from @context.
150  *
151  * Returns: the last #GstEvent of @type that was updated on @context. This
152  * function returns NULL when there is no event with the given type.
153  */
154 GstEvent *
155 gst_context_get (GstContext * context, GstEventType type)
156 {
157   guint idx;
158   GstEvent *event = NULL;
159
160   g_return_val_if_fail (context != NULL, NULL);
161
162   idx = GST_EVENT_STICKY_IDX_TYPE (type);
163
164   if ((event = context->events[idx]))
165     gst_event_ref (event);
166
167   return event;
168 }
169
170 /**
171  * gst_context_clear:
172  * @context: a #GstContext
173  *
174  * Clear all stored events in @context
175  */
176 void
177 gst_context_clear (GstContext * context)
178 {
179   guint i;
180
181   for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
182     gst_event_replace (&context->events[i], NULL);
183 }
184
185 /**
186  * gst_context_foreach:
187  * @context: a #GstContext
188  * @func: a #GFunc
189  * @user_data: user data
190  *
191  * Call @func with the non NULL event and @user_data.
192  */
193 void
194 gst_context_foreach (GstContext * context, GFunc func, gpointer user_data)
195 {
196   guint i;
197   GstEvent *event;
198
199   for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
200     if ((event = context->events[i]))
201       func (event, user_data);
202 }