docs: add missing docs, fixing doc errors
[platform/upstream/gstreamer.git] / gst / gstcontext.c
1 /* GStreamer
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>
5  *
6  * gstcontext.h: Header for GstContext subsystem
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:gstcontext
26  * @short_description: Lightweight objects to represent element contexts
27  * @see_also: #GstMiniObject, #GstElement
28  *
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.
32  *
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 #GstElementClass.set_context()
36  * and merge them with the context information they already have.
37  *
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  * 2) Query upstream with GST_QUERY_CONTEXT for the context
43  * 3) Post a GST_MESSAGE_NEED_CONTEXT message on the bus with the required
44  *    context types and afterwards check if a usable context was set now
45  * 4) Create a context by itself and post a GST_MESSAGE_HAVE_CONTEXT message
46  *    on the bus.
47  *
48  * Bins will catch GST_MESSAGE_NEED_CONTEXT messages and will set any previously
49  * known context on the element that asks for it if possible. Otherwise the
50  * application should provide one if it can.
51  *
52  * Since: 1.2
53  */
54
55 #include "gst_private.h"
56 #include <string.h>
57 #include "gstcontext.h"
58 #include "gstquark.h"
59
60 struct _GstContext
61 {
62   GstMiniObject mini_object;
63
64   gchar *context_type;
65   GstStructure *structure;
66   gboolean persistent;
67 };
68
69 #define GST_CONTEXT_STRUCTURE(c)  (((GstContext *)(c))->structure)
70
71 static GType _gst_context_type = 0;
72 GST_DEFINE_MINI_OBJECT_TYPE (GstContext, gst_context);
73
74 void
75 _priv_gst_context_initialize (void)
76 {
77   GST_CAT_INFO (GST_CAT_GST_INIT, "init contexts");
78
79   /* the GstMiniObject types need to be class_ref'd once before it can be
80    * done from multiple threads;
81    * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
82   gst_context_get_type ();
83
84   _gst_context_type = gst_context_get_type ();
85 }
86
87 static void
88 _gst_context_free (GstContext * context)
89 {
90   GstStructure *structure;
91
92   g_return_if_fail (context != NULL);
93
94   GST_CAT_LOG (GST_CAT_CONTEXT, "finalize context %p: %" GST_PTR_FORMAT,
95       context, GST_CONTEXT_STRUCTURE (context));
96
97   structure = GST_CONTEXT_STRUCTURE (context);
98   if (structure) {
99     gst_structure_set_parent_refcount (structure, NULL);
100     gst_structure_free (structure);
101   }
102   g_free (context->context_type);
103
104   g_slice_free1 (sizeof (GstContext), context);
105 }
106
107 static void gst_context_init (GstContext * context);
108
109 static GstContext *
110 _gst_context_copy (GstContext * context)
111 {
112   GstContext *copy;
113   GstStructure *structure;
114
115   GST_CAT_LOG (GST_CAT_CONTEXT, "copy context %p: %" GST_PTR_FORMAT, context,
116       GST_CONTEXT_STRUCTURE (context));
117
118   copy = g_slice_new0 (GstContext);
119
120   gst_context_init (copy);
121
122   copy->context_type = g_strdup (context->context_type);
123
124   structure = GST_CONTEXT_STRUCTURE (context);
125   GST_CONTEXT_STRUCTURE (copy) = gst_structure_copy (structure);
126   gst_structure_set_parent_refcount (GST_CONTEXT_STRUCTURE (copy),
127       &copy->mini_object.refcount);
128
129   copy->persistent = context->persistent;
130
131   return GST_CONTEXT_CAST (copy);
132 }
133
134 static void
135 gst_context_init (GstContext * context)
136 {
137   gst_mini_object_init (GST_MINI_OBJECT_CAST (context), 0, _gst_context_type,
138       (GstMiniObjectCopyFunction) _gst_context_copy, NULL,
139       (GstMiniObjectFreeFunction) _gst_context_free);
140 }
141
142 /**
143  * gst_context_new:
144  * @context_type: Context type
145  * @persistent: Persistent context
146  *
147  * Create a new context.
148  *
149  * Returns: (transfer full): The new context.
150  *
151  * Since: 1.2
152  */
153 GstContext *
154 gst_context_new (const gchar * context_type, gboolean persistent)
155 {
156   GstContext *context;
157   GstStructure *structure;
158
159   g_return_val_if_fail (context_type != NULL, NULL);
160
161   context = g_slice_new0 (GstContext);
162
163   GST_CAT_LOG (GST_CAT_CONTEXT, "creating new context %p", context);
164
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);
168
169   context->context_type = g_strdup (context_type);
170   GST_CONTEXT_STRUCTURE (context) = structure;
171   context->persistent = persistent;
172
173   return context;
174 }
175
176 /**
177  * gst_context_get_context_type:
178  * @context: The #GstContext.
179  *
180  * Get the type of @context.
181  *
182  * Returns: The type of the context.
183  *
184  * Since: 1.2
185  */
186 const gchar *
187 gst_context_get_context_type (const GstContext * context)
188 {
189   g_return_val_if_fail (GST_IS_CONTEXT (context), NULL);
190
191   return context->context_type;
192 }
193
194 /**
195  * gst_context_has_context_type:
196  * @context: The #GstContext.
197  * @context_type: Context type to check.
198  *
199  * Checks if @context has @context_type.
200  *
201  * Returns: %TRUE if @context has @context_type.
202  *
203  * Since: 1.2
204  */
205 gboolean
206 gst_context_has_context_type (const GstContext * context,
207     const gchar * context_type)
208 {
209   g_return_val_if_fail (GST_IS_CONTEXT (context), FALSE);
210   g_return_val_if_fail (context_type != NULL, FALSE);
211
212   return strcmp (context->context_type, context_type) == 0;
213 }
214
215 /**
216  * gst_context_get_structure:
217  * @context: The #GstContext.
218  *
219  * Access the structure of the context.
220  *
221  * Returns: (transfer none): The structure of the context. The structure is
222  * still owned by the context, which means that you should not modify it,
223  * free it and that the pointer becomes invalid when you free the context.
224  *
225  * Since: 1.2
226  */
227 const GstStructure *
228 gst_context_get_structure (const GstContext * context)
229 {
230   g_return_val_if_fail (GST_IS_CONTEXT (context), NULL);
231
232   return GST_CONTEXT_STRUCTURE (context);
233 }
234
235 /**
236  * gst_context_writable_structure:
237  * @context: The #GstContext.
238  *
239  * Get a writable version of the structure.
240  *
241  * Returns: The structure of the context. The structure is still
242  * owned by the event, which means that you should not free it and
243  * that the pointer becomes invalid when you free the event.
244  * This function checks if @context is writable.
245  *
246  * Since: 1.2
247  */
248 GstStructure *
249 gst_context_writable_structure (GstContext * context)
250 {
251   g_return_val_if_fail (GST_IS_CONTEXT (context), NULL);
252   g_return_val_if_fail (gst_context_is_writable (context), NULL);
253
254   return GST_CONTEXT_STRUCTURE (context);
255 }
256
257 /**
258  * gst_context_is_persistent:
259  * @context: The #GstContext.
260  *
261  * Check if @context is persistent.
262  *
263  * Returns: %TRUE if the context is persistent.
264  *
265  * Since: 1.2
266  */
267 gboolean
268 gst_context_is_persistent (const GstContext * context)
269 {
270   g_return_val_if_fail (GST_IS_CONTEXT (context), FALSE);
271
272   return context->persistent;
273 }