2 * Copyright (C) 2013 Collabora Ltd.
3 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * Copyright (C) 2013 Sebastian Dröge <slomo@circular-chaos.org>
6 * gstcontext.h: Header for GstContext subsystem
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 * @short_description: Lightweight objects to represent element contexts
27 * @see_also: #GstMiniObject, #GstElement
29 * #GstContext is a container object used to store contexts like a device
30 * context, a display server connection and similar concepts that should
31 * be shared between multiple elements.
33 * Applications can set a context on a complete pipeline by using
34 * gst_element_set_context(), which will then be propagated to all
35 * child elements. Elements can handle these in GstElement::set_context()
36 * and merge them with the context information they already have.
38 * When an element needs a context it will do the following actions in this
39 * order until one step succeeds:
40 * 1) Check if the element already has a context
41 * 2) Query downstream with GST_QUERY_CONTEXT for the context
42 * 3) Post a GST_MESSAGE_NEED_CONTEXT message on the bus with the required
43 * context types and afterwards check if a usable context was set now
44 * 4) Create a context by itself and post a GST_MESSAGE_HAVE_CONTEXT message
45 * and send a GST_EVENT_CONTEXT event downstream, containing the complete
46 * context information at this time.
48 * Applications should catch the GST_MESSAGE_HAVE_CONTEXT messages and remember
49 * any content from it unless it has a custom version of a specific context. If
50 * later an element is posting a GST_MESSAGE_NEED_CONTEXT message for a specific
51 * context that was created by an element before, the application should pass it
52 * to the element or the complete pipeline.
57 #include "gst_private.h"
59 #include "gstcontext.h"
64 GstMiniObject mini_object;
67 GstStructure *structure;
71 #define GST_CONTEXT_STRUCTURE(c) (((GstContext *)(c))->structure)
73 static GType _gst_context_type = 0;
74 GST_DEFINE_MINI_OBJECT_TYPE (GstContext, gst_context);
77 _priv_gst_context_initialize (void)
79 GST_CAT_INFO (GST_CAT_GST_INIT, "init contexts");
81 /* the GstMiniObject types need to be class_ref'd once before it can be
82 * done from multiple threads;
83 * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
84 gst_context_get_type ();
86 _gst_context_type = gst_context_get_type ();
90 _gst_context_free (GstContext * context)
92 GstStructure *structure;
94 g_return_if_fail (context != NULL);
96 GST_CAT_LOG (GST_CAT_CONTEXT, "finalize context %p: %" GST_PTR_FORMAT,
97 context, GST_CONTEXT_STRUCTURE (context));
99 structure = GST_CONTEXT_STRUCTURE (context);
101 gst_structure_set_parent_refcount (structure, NULL);
102 gst_structure_free (structure);
105 g_slice_free1 (sizeof (GstContext), context);
108 static void gst_context_init (GstContext * context);
111 _gst_context_copy (GstContext * context)
114 GstStructure *structure;
116 GST_CAT_LOG (GST_CAT_CONTEXT, "copy context %p: %" GST_PTR_FORMAT, context,
117 GST_CONTEXT_STRUCTURE (context));
119 copy = g_slice_new0 (GstContext);
121 gst_context_init (copy);
123 copy->context_type = g_strdup (context->context_type);
125 structure = GST_CONTEXT_STRUCTURE (context);
126 GST_CONTEXT_STRUCTURE (copy) = gst_structure_copy (structure);
127 gst_structure_set_parent_refcount (GST_CONTEXT_STRUCTURE (copy),
128 ©->mini_object.refcount);
130 copy->persistent = context->persistent;
132 return GST_CONTEXT_CAST (copy);
136 gst_context_init (GstContext * context)
138 gst_mini_object_init (GST_MINI_OBJECT_CAST (context), 0, _gst_context_type,
139 (GstMiniObjectCopyFunction) _gst_context_copy, NULL,
140 (GstMiniObjectFreeFunction) _gst_context_free);
145 * @persistent: Persistent context
147 * Create a new context.
149 * Returns: (transfer full): The new context.
154 gst_context_new (const gchar * context_type, gboolean persistent)
157 GstStructure *structure;
159 g_return_val_if_fail (context_type != NULL, NULL);
161 context = g_slice_new0 (GstContext);
163 GST_CAT_LOG (GST_CAT_CONTEXT, "creating new context %p", context);
165 structure = gst_structure_new_id_empty (GST_QUARK (CONTEXT));
166 gst_structure_set_parent_refcount (structure, &context->mini_object.refcount);
167 gst_context_init (context);
169 context->context_type = g_strdup (context_type);
170 GST_CONTEXT_STRUCTURE (context) = structure;
171 context->persistent = persistent;
177 * gst_context_get_context_type:
178 * @context: The #GstContext.
180 * Get the type of @context.
182 * Returns: The type of the context.
187 gst_context_get_context_type (const GstContext * context)
189 g_return_val_if_fail (GST_IS_CONTEXT (context), NULL);
191 return context->context_type;
195 * gst_context_get_structure:
196 * @context: The #GstContext.
198 * Access the structure of the context.
200 * Returns: (transfer none): The structure of the context. The structure is
201 * still owned by the context, which means that you should not modify it,
202 * free it and that the pointer becomes invalid when you free the context.
207 gst_context_get_structure (const GstContext * context)
209 g_return_val_if_fail (GST_IS_CONTEXT (context), NULL);
211 return GST_CONTEXT_STRUCTURE (context);
215 * gst_context_writable_structure:
216 * @context: The #GstContext.
218 * Get a writable version of the structure.
220 * Returns: The structure of the context. The structure is still
221 * owned by the event, which means that you should not free it and
222 * that the pointer becomes invalid when you free the event.
223 * This function checks if @context is writable.
228 gst_context_writable_structure (GstContext * context)
230 g_return_val_if_fail (GST_IS_CONTEXT (context), NULL);
231 g_return_val_if_fail (gst_context_is_writable (context), NULL);
233 return GST_CONTEXT_STRUCTURE (context);
237 * gst_context_is_persistent:
238 * @context: The #GstContext.
240 * Check if @context is persistent.
242 * Returns: %TRUE if the context is persistent.
247 gst_context_is_persistent (const GstContext * context)
249 g_return_val_if_fail (GST_IS_CONTEXT (context), NULL);
251 return context->persistent;