blob: 8e0d0cb88c5f1c8b39ef3c6deb547d12495b39b6 [file] [log] [blame]
Hyunjun Kodddb8482018-02-13 13:50:08 -09001/* 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
35GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
36
37static void
38_init_context_debug (void)
39{
40#ifndef GST_DISABLE_GST_DEBUG
41 static volatile 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
50static gboolean
51context_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
65static gboolean
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
89static gboolean
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
110static void
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
145found:
146 gst_query_unref (query);
147}
148
149gboolean
150gst_msdk_context_prepare (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 _gst_context_query (element, GST_MSDK_CONTEXT_TYPE_NAME);
165
166 if (*context_ptr)
167 GST_LOG_OBJECT (element, "found a context %" GST_PTR_FORMAT, *context_ptr);
168
169 return *context_ptr != NULL;
170}
171
172gboolean
173gst_msdk_context_get_context (GstContext * context,
174 GstMsdkContext ** msdk_context)
175{
176 const GstStructure *structure;
177 const gchar *type;
178
179 g_return_val_if_fail (GST_IS_CONTEXT (context), FALSE);
180
181 type = gst_context_get_context_type (context);
182
183 if (!g_strcmp0 (type, GST_MSDK_CONTEXT_TYPE_NAME)) {
184 structure = gst_context_get_structure (context);
185 return gst_structure_get (structure, GST_MSDK_CONTEXT_TYPE_NAME,
186 GST_TYPE_MSDK_CONTEXT, msdk_context, NULL);
187 }
188
189 return FALSE;
190}
191
192static void
193gst_msdk_context_propagate (GstElement * element, GstMsdkContext * msdk_context)
194{
195 GstContext *context;
196 GstStructure *structure;
197 GstMessage *msg;
198
199 context = gst_context_new (GST_MSDK_CONTEXT_TYPE_NAME, FALSE);
200
201 structure = gst_context_writable_structure (context);
202 gst_structure_set (structure, GST_MSDK_CONTEXT_TYPE_NAME,
203 GST_TYPE_MSDK_CONTEXT, msdk_context, NULL);
204
205 gst_element_set_context (element, context);
206
207 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
208 "posting `have-context' message with MSDK context %" GST_PTR_FORMAT,
209 msdk_context);
210
211 msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
212 if (!gst_element_post_message (element, msg)) {
213 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element, "No bus attached");
214 }
215}
216
217gboolean
Hyunjun Kodc82ccb2018-02-13 13:50:48 -0900218gst_msdk_context_ensure_context (GstElement * element, gboolean hardware,
219 GstMsdkContextJobType job)
Hyunjun Kodddb8482018-02-13 13:50:08 -0900220{
221 GstMsdkContext *msdk_context;
222
Hyunjun Kodc82ccb2018-02-13 13:50:48 -0900223 msdk_context = gst_msdk_context_new (hardware, job);
Hyunjun Kodddb8482018-02-13 13:50:08 -0900224 if (!msdk_context) {
225 GST_ERROR_OBJECT (element, "Context creation failed");
226 return FALSE;
227 }
228
229 GST_INFO_OBJECT (element, "New MSDK Context %p", msdk_context);
230
231 gst_msdk_context_propagate (element, msdk_context);
232 gst_object_unref (msdk_context);
233
234 return TRUE;
235}