• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2018, Intel Corporation
3  * Copyright (c) 2018, Igalia S.L.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32 
33 #include "gstmsdkcontextutil.h"
34 
35 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
36 
37 static void
_init_context_debug(void)38 _init_context_debug (void)
39 {
40 #ifndef GST_DISABLE_GST_DEBUG
41   static gsize _init = 0;
42 
43   if (g_once_init_enter (&_init)) {
44     GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
45     g_once_init_leave (&_init, 1);
46   }
47 #endif
48 }
49 
50 static gboolean
context_pad_query(const GValue * item,GValue * value,gpointer user_data)51 context_pad_query (const GValue * item, GValue * value, gpointer user_data)
52 {
53   GstPad *const pad = g_value_get_object (item);
54   GstQuery *const query = user_data;
55 
56   if (gst_pad_peer_query (pad, query)) {
57     g_value_set_boolean (value, TRUE);
58     return FALSE;
59   }
60 
61   GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "context pad peer query failed");
62   return TRUE;
63 }
64 
65 static gboolean
_gst_context_run_query(GstElement * element,GstQuery * query,GstPadDirection direction)66 _gst_context_run_query (GstElement * element, GstQuery * query,
67     GstPadDirection direction)
68 {
69   GstIteratorFoldFunction const func = context_pad_query;
70   GstIterator *it;
71   GValue res = { 0 };
72 
73   g_value_init (&res, G_TYPE_BOOLEAN);
74   g_value_set_boolean (&res, FALSE);
75 
76   /* Ask neighbour */
77   if (direction == GST_PAD_SRC)
78     it = gst_element_iterate_src_pads (element);
79   else
80     it = gst_element_iterate_sink_pads (element);
81 
82   while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
83     gst_iterator_resync (it);
84   gst_iterator_free (it);
85 
86   return g_value_get_boolean (&res);
87 }
88 
89 static gboolean
_gst_context_get_from_query(GstElement * element,GstQuery * query,GstPadDirection direction)90 _gst_context_get_from_query (GstElement * element, GstQuery * query,
91     GstPadDirection direction)
92 {
93   GstContext *ctxt;
94 
95   if (!_gst_context_run_query (element, query, direction))
96     return FALSE;
97 
98   gst_query_parse_context (query, &ctxt);
99   if (!ctxt)
100     return FALSE;
101 
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");
105 
106   gst_element_set_context (element, ctxt);
107   return TRUE;
108 }
109 
110 static void
_gst_context_query(GstElement * element,const gchar * context_type)111 _gst_context_query (GstElement * element, const gchar * context_type)
112 {
113   GstQuery *query;
114   GstMessage *msg;
115 
116   /* 2) Query downstream with GST_QUERY_CONTEXT for the context and
117      check if downstream already has a context of the specific
118      type */
119 
120   /* 3) Query upstream with GST_QUERY_CONTEXT for the context and
121      check if upstream already has a context of the specific
122      type */
123   query = gst_query_new_context (context_type);
124   if (_gst_context_get_from_query (element, query, GST_PAD_SRC))
125     goto found;
126   if (_gst_context_get_from_query (element, query, GST_PAD_SINK))
127     goto found;
128 
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
133      application. */
134   GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
135       "posting `need-context' message");
136 
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");
140 
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 */
144 
145 found:
146   gst_query_unref (query);
147 }
148 
149 gboolean
gst_msdk_context_find(GstElement * element,GstMsdkContext ** context_ptr)150 gst_msdk_context_find (GstElement * element, GstMsdkContext ** context_ptr)
151 {
152   g_return_val_if_fail (element != NULL, FALSE);
153   g_return_val_if_fail (context_ptr != NULL, FALSE);
154 
155   _init_context_debug ();
156 
157   /* 1) Check if the element already has a context of the specific type. */
158   if (*context_ptr) {
159     GST_LOG_OBJECT (element, "already have a context %" GST_PTR_FORMAT,
160         *context_ptr);
161     return TRUE;
162   }
163 
164   /* This may indirectly set *context_ptr, see function body */
165   _gst_context_query (element, GST_MSDK_CONTEXT_TYPE_NAME);
166 
167   if (*context_ptr)
168     GST_LOG_OBJECT (element, "found a context %" GST_PTR_FORMAT, *context_ptr);
169 
170   return *context_ptr != NULL;
171 }
172 
173 gboolean
gst_msdk_context_get_context(GstContext * context,GstMsdkContext ** msdk_context)174 gst_msdk_context_get_context (GstContext * context,
175     GstMsdkContext ** msdk_context)
176 {
177   const GstStructure *structure;
178   const gchar *type;
179 
180   g_return_val_if_fail (GST_IS_CONTEXT (context), FALSE);
181 
182   type = gst_context_get_context_type (context);
183 
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);
188   }
189 
190   return FALSE;
191 }
192 
193 static void
gst_msdk_context_propagate(GstElement * element,GstMsdkContext * msdk_context)194 gst_msdk_context_propagate (GstElement * element, GstMsdkContext * msdk_context)
195 {
196   GstContext *context;
197   GstStructure *structure;
198   GstMessage *msg;
199 
200   context = gst_context_new (GST_MSDK_CONTEXT_TYPE_NAME, FALSE);
201 
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);
205 
206   gst_element_set_context (element, context);
207 
208   GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
209       "posting `have-context' message with MSDK context %" GST_PTR_FORMAT,
210       msdk_context);
211 
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");
215   }
216 }
217 
218 gboolean
gst_msdk_context_ensure_context(GstElement * element,gboolean hardware,GstMsdkContextJobType job)219 gst_msdk_context_ensure_context (GstElement * element, gboolean hardware,
220     GstMsdkContextJobType job)
221 {
222   GstMsdkContext *msdk_context;
223 
224   msdk_context = gst_msdk_context_new (hardware, job);
225   if (!msdk_context) {
226     GST_ERROR_OBJECT (element, "Context creation failed");
227     return FALSE;
228   }
229 
230   GST_INFO_OBJECT (element, "New MSDK Context %p", msdk_context);
231 
232   gst_msdk_context_propagate (element, msdk_context);
233   gst_object_unref (msdk_context);
234 
235   return TRUE;
236 }
237