1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2018, Intel Corporation
3 * Copyright (c) 2018, Igalia S.L.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGDECE
29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "gstmsdkcontextutil.h"
35 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
38 _init_context_debug (void)
40 #ifndef GST_DISABLE_GST_DEBUG
41 static volatile gsize _init = 0;
43 if (g_once_init_enter (&_init)) {
44 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
45 g_once_init_leave (&_init, 1);
51 context_pad_query (const GValue * item, GValue * value, gpointer user_data)
53 GstPad *const pad = g_value_get_object (item);
54 GstQuery *const query = user_data;
56 if (gst_pad_peer_query (pad, query)) {
57 g_value_set_boolean (value, TRUE);
61 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "context pad peer query failed");
66 _gst_context_run_query (GstElement * element, GstQuery * query,
67 GstPadDirection direction)
69 GstIteratorFoldFunction const func = context_pad_query;
73 g_value_init (&res, G_TYPE_BOOLEAN);
74 g_value_set_boolean (&res, FALSE);
77 if (direction == GST_PAD_SRC)
78 it = gst_element_iterate_src_pads (element);
80 it = gst_element_iterate_sink_pads (element);
82 while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
83 gst_iterator_resync (it);
84 gst_iterator_free (it);
86 return g_value_get_boolean (&res);
90 _gst_context_get_from_query (GstElement * element, GstQuery * query,
91 GstPadDirection direction)
95 if (!_gst_context_run_query (element, query, direction))
98 gst_query_parse_context (query, &ctxt);
102 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
103 "found context (%" GST_PTR_FORMAT ") in %s query", ctxt,
104 direction == GST_PAD_SRC ? "downstream" : "upstream");
106 gst_element_set_context (element, ctxt);
111 _gst_context_query (GstElement * element, const gchar * context_type)
116 /* 2) Query downstream with GST_QUERY_CONTEXT for the context and
117 check if downstream already has a context of the specific
120 /* 3) Query upstream with GST_QUERY_CONTEXT for the context and
121 check if upstream already has a context of the specific
123 query = gst_query_new_context (context_type);
124 if (_gst_context_get_from_query (element, query, GST_PAD_SRC))
126 if (_gst_context_get_from_query (element, query, GST_PAD_SINK))
129 /* 4) Post a GST_MESSAGE_NEED_CONTEXT message on the bus with
130 the required context types and afterwards check if an
131 usable context was set now as in 1). The message could
132 be handled by the parent bins of the element and the
134 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
135 "posting `need-context' message");
137 msg = gst_message_new_need_context (GST_OBJECT_CAST (element), context_type);
138 if (!gst_element_post_message (element, msg))
139 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element, "No bus attached");
141 /* Whomever responds to the need-context message performs a
142 GstElement::set_context() with the required context in which the
143 element is required to update the display_ptr */
146 gst_query_unref (query);
150 gst_msdk_context_find (GstElement * element, GstMsdkContext ** context_ptr)
152 g_return_val_if_fail (element != NULL, FALSE);
153 g_return_val_if_fail (context_ptr != NULL, FALSE);
155 _init_context_debug ();
157 /* 1) Check if the element already has a context of the specific type. */
159 GST_LOG_OBJECT (element, "already have a context %" GST_PTR_FORMAT,
164 /* This may indirectly set *context_ptr, see function body */
165 _gst_context_query (element, GST_MSDK_CONTEXT_TYPE_NAME);
168 GST_LOG_OBJECT (element, "found a context %" GST_PTR_FORMAT, *context_ptr);
170 return *context_ptr != NULL;
174 gst_msdk_context_get_context (GstContext * context,
175 GstMsdkContext ** msdk_context)
177 const GstStructure *structure;
180 g_return_val_if_fail (GST_IS_CONTEXT (context), FALSE);
182 type = gst_context_get_context_type (context);
184 if (!g_strcmp0 (type, GST_MSDK_CONTEXT_TYPE_NAME)) {
185 structure = gst_context_get_structure (context);
186 return gst_structure_get (structure, GST_MSDK_CONTEXT_TYPE_NAME,
187 GST_TYPE_MSDK_CONTEXT, msdk_context, NULL);
194 gst_msdk_context_propagate (GstElement * element, GstMsdkContext * msdk_context)
197 GstStructure *structure;
200 context = gst_context_new (GST_MSDK_CONTEXT_TYPE_NAME, FALSE);
202 structure = gst_context_writable_structure (context);
203 gst_structure_set (structure, GST_MSDK_CONTEXT_TYPE_NAME,
204 GST_TYPE_MSDK_CONTEXT, msdk_context, NULL);
206 gst_element_set_context (element, context);
208 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
209 "posting `have-context' message with MSDK context %" GST_PTR_FORMAT,
212 msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
213 if (!gst_element_post_message (element, msg)) {
214 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element, "No bus attached");
219 gst_msdk_context_ensure_context (GstElement * element, gboolean hardware,
220 GstMsdkContextJobType job)
222 GstMsdkContext *msdk_context;
224 msdk_context = gst_msdk_context_new (hardware, job);
226 GST_ERROR_OBJECT (element, "Context creation failed");
230 GST_INFO_OBJECT (element, "New MSDK Context %p", msdk_context);
232 gst_msdk_context_propagate (element, msdk_context);
233 gst_object_unref (msdk_context);