gluploadelement: Avoid race condition in propose_allocation().
[platform/upstream/gstreamer.git] / ext / gl / gstgluploadelement.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include <gst/gl/gl.h>
28 #include "gstgluploadelement.h"
29
30 GST_DEBUG_CATEGORY_STATIC (gst_gl_upload_element_debug);
31 #define GST_CAT_DEFAULT gst_gl_upload_element_debug
32
33 #define gst_gl_upload_element_parent_class parent_class
34 G_DEFINE_TYPE_WITH_CODE (GstGLUploadElement, gst_gl_upload_element,
35     GST_TYPE_GL_BASE_FILTER,
36     GST_DEBUG_CATEGORY_INIT (gst_gl_upload_element_debug, "gluploadelement", 0,
37         "glupload Element"););
38
39 static gboolean gst_gl_upload_element_get_unit_size (GstBaseTransform * trans,
40     GstCaps * caps, gsize * size);
41 static GstCaps *_gst_gl_upload_element_transform_caps (GstBaseTransform * bt,
42     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
43 static gboolean _gst_gl_upload_element_set_caps (GstBaseTransform * bt,
44     GstCaps * in_caps, GstCaps * out_caps);
45 static gboolean gst_gl_upload_element_filter_meta (GstBaseTransform * trans,
46     GstQuery * query, GType api, const GstStructure * params);
47 static gboolean _gst_gl_upload_element_propose_allocation (GstBaseTransform *
48     bt, GstQuery * decide_query, GstQuery * query);
49 static gboolean _gst_gl_upload_element_decide_allocation (GstBaseTransform *
50     trans, GstQuery * query);
51 static GstFlowReturn
52 gst_gl_upload_element_prepare_output_buffer (GstBaseTransform * bt,
53     GstBuffer * buffer, GstBuffer ** outbuf);
54 static GstFlowReturn gst_gl_upload_element_transform (GstBaseTransform * bt,
55     GstBuffer * buffer, GstBuffer * outbuf);
56 static gboolean gst_gl_upload_element_stop (GstBaseTransform * bt);
57 static GstStateChangeReturn
58 gst_gl_upload_element_change_state (GstElement * element,
59     GstStateChange transition);
60
61
62 static GstStaticPadTemplate gst_gl_upload_element_src_pad_template =
63 GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("video/x-raw(ANY)"));
67
68 static void
69 _gst_gl_upload_element_clear_upload (GstGLUploadElement * upload)
70 {
71   GstGLUpload *ul = NULL;
72
73   GST_OBJECT_LOCK (upload);
74   ul = upload->upload;
75   upload->upload = NULL;
76   GST_OBJECT_UNLOCK (upload);
77
78   if (ul)
79     gst_object_unref (ul);
80 }
81
82 static void
83 gst_gl_upload_element_finalize (GObject * object)
84 {
85   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (object);
86
87   _gst_gl_upload_element_clear_upload (upload);
88
89   G_OBJECT_CLASS (parent_class)->finalize (object);
90 }
91
92 static void
93 gst_gl_upload_element_class_init (GstGLUploadElementClass * klass)
94 {
95   GstBaseTransformClass *bt_class = GST_BASE_TRANSFORM_CLASS (klass);
96   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
97   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98   GstCaps *upload_caps;
99
100   bt_class->transform_caps = _gst_gl_upload_element_transform_caps;
101   bt_class->set_caps = _gst_gl_upload_element_set_caps;
102   bt_class->filter_meta = gst_gl_upload_element_filter_meta;
103   bt_class->propose_allocation = _gst_gl_upload_element_propose_allocation;
104   bt_class->decide_allocation = _gst_gl_upload_element_decide_allocation;
105   bt_class->get_unit_size = gst_gl_upload_element_get_unit_size;
106   bt_class->prepare_output_buffer = gst_gl_upload_element_prepare_output_buffer;
107   bt_class->transform = gst_gl_upload_element_transform;
108   bt_class->stop = gst_gl_upload_element_stop;
109
110   element_class->change_state = gst_gl_upload_element_change_state;
111
112   gst_element_class_add_static_pad_template (element_class,
113       &gst_gl_upload_element_src_pad_template);
114
115   upload_caps = gst_gl_upload_get_input_template_caps ();
116   gst_element_class_add_pad_template (element_class,
117       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, upload_caps));
118   gst_caps_unref (upload_caps);
119
120   gst_element_class_set_metadata (element_class,
121       "OpenGL uploader", "Filter/Video",
122       "Uploads data into OpenGL", "Matthew Waters <matthew@centricular.com>");
123
124   gobject_class->finalize = gst_gl_upload_element_finalize;
125 }
126
127 static void
128 gst_gl_upload_element_init (GstGLUploadElement * upload)
129 {
130   gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (upload), TRUE);
131 }
132
133 static gboolean
134 gst_gl_upload_element_stop (GstBaseTransform * bt)
135 {
136   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
137
138   _gst_gl_upload_element_clear_upload (upload);
139
140   return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (bt);
141 }
142
143 static gboolean
144 gst_gl_upload_element_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
145     gsize * size)
146 {
147   gboolean ret = FALSE;
148   GstVideoInfo info;
149
150   ret = gst_video_info_from_caps (&info, caps);
151   if (ret)
152     *size = GST_VIDEO_INFO_SIZE (&info);
153
154   return TRUE;
155 }
156
157 static GstCaps *
158 _gst_gl_upload_element_transform_caps (GstBaseTransform * bt,
159     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
160 {
161   GstGLBaseFilter *base_filter = GST_GL_BASE_FILTER (bt);
162   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
163   GstGLContext *context;
164   GstGLUpload *ul = NULL;
165   GstCaps *ret_caps;
166
167   if (base_filter->display && !gst_gl_base_filter_find_gl_context (base_filter))
168     return NULL;
169
170   context = gst_gl_base_filter_get_gl_context (base_filter);
171
172   GST_OBJECT_LOCK (upload);
173   if (upload->upload == NULL) {
174     GST_OBJECT_UNLOCK (upload);
175
176     ul = gst_gl_upload_new (context);
177
178     GST_OBJECT_LOCK (upload);
179     if (upload->upload) {
180       gst_object_unref (ul);
181       ul = upload->upload;
182     } else {
183       upload->upload = ul;
184     }
185   } else {
186     ul = upload->upload;
187   }
188
189   gst_object_ref (ul);
190   GST_OBJECT_UNLOCK (upload);
191
192   ret_caps =
193       gst_gl_upload_transform_caps (ul, context, direction, caps, filter);
194
195   gst_object_unref (ul);
196   if (context)
197     gst_object_unref (context);
198
199   return ret_caps;
200 }
201
202 static gboolean
203 gst_gl_upload_element_filter_meta (GstBaseTransform * trans, GstQuery * query,
204     GType api, const GstStructure * params)
205 {
206   /* propose all metadata upstream */
207   return TRUE;
208 }
209
210 static gboolean
211 _gst_gl_upload_element_propose_allocation (GstBaseTransform * bt,
212     GstQuery * decide_query, GstQuery * query)
213 {
214   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
215   GstGLUpload *ul;
216   GstGLContext *context;
217   gboolean ret;
218
219   GST_OBJECT_LOCK (upload);
220   if (!upload->upload) {
221     GST_OBJECT_UNLOCK (upload);
222     return FALSE;
223   }
224   ul = gst_object_ref (upload->upload);
225   GST_OBJECT_UNLOCK (upload);
226
227   context = gst_gl_base_filter_get_gl_context (GST_GL_BASE_FILTER (bt));
228   if (!context) {
229     gst_object_unref (ul);
230     return FALSE;
231   }
232
233   gst_gl_upload_set_context (ul, context);
234
235   ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (bt,
236       decide_query, query);
237   gst_gl_upload_propose_allocation (ul, decide_query, query);
238
239   gst_object_unref (ul);
240   gst_object_unref (context);
241
242   return ret;
243 }
244
245 static gboolean
246 _gst_gl_upload_element_decide_allocation (GstBaseTransform * trans,
247     GstQuery * query)
248 {
249   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (trans);
250   GstGLContext *context = GST_GL_BASE_FILTER (trans)->context;
251
252   if (upload->upload && context)
253     gst_gl_upload_set_context (upload->upload, context);
254
255   return
256       GST_BASE_TRANSFORM_CLASS
257       (gst_gl_upload_element_parent_class)->decide_allocation (trans, query);
258 }
259
260 static gboolean
261 _gst_gl_upload_element_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
262     GstCaps * out_caps)
263 {
264   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
265
266   return gst_gl_upload_set_caps (upload->upload, in_caps, out_caps);
267 }
268
269 GstFlowReturn
270 gst_gl_upload_element_prepare_output_buffer (GstBaseTransform * bt,
271     GstBuffer * buffer, GstBuffer ** outbuf)
272 {
273   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
274   GstGLUploadReturn ret;
275   GstBaseTransformClass *bclass;
276
277   bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
278
279   if (gst_base_transform_is_passthrough (bt)) {
280     *outbuf = buffer;
281     return GST_FLOW_OK;
282   }
283
284   if (!upload->upload)
285     return GST_FLOW_NOT_NEGOTIATED;
286
287 again:
288   ret = gst_gl_upload_perform_with_buffer (upload->upload, buffer, outbuf);
289   if (ret == GST_GL_UPLOAD_RECONFIGURE) {
290     GstPad *sinkpad = GST_BASE_TRANSFORM_SINK_PAD (bt);
291     GstCaps *incaps = gst_pad_get_current_caps (sinkpad);
292     GST_DEBUG_OBJECT (bt,
293         "Failed to upload with curren caps -- reconfiguring.");
294     /* Note: gst_base_transform_reconfigure_src() cannot be used here.
295      * Reconfiguring must be synchronous to avoid dropping the current
296      * buffer */
297     gst_pad_send_event (sinkpad, gst_event_new_caps (incaps));
298     gst_caps_unref (incaps);
299     if (!gst_pad_needs_reconfigure (GST_BASE_TRANSFORM_SRC_PAD (bt))) {
300       GST_DEBUG_OBJECT (bt, "Retry uploading with new caps");
301       goto again;
302     }
303     return GST_FLOW_OK;
304   }
305
306   if (ret != GST_GL_UPLOAD_DONE || *outbuf == NULL) {
307     GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND, ("%s",
308             "Failed to upload buffer"), (NULL));
309     if (*outbuf)
310       gst_buffer_unref (*outbuf);
311     return GST_FLOW_ERROR;
312   }
313
314   /* basetransform doesn't unref if they're the same */
315   if (buffer == *outbuf)
316     gst_buffer_unref (*outbuf);
317   else
318     bclass->copy_metadata (bt, buffer, *outbuf);
319
320   return GST_FLOW_OK;
321 }
322
323 static GstFlowReturn
324 gst_gl_upload_element_transform (GstBaseTransform * bt, GstBuffer * buffer,
325     GstBuffer * outbuf)
326 {
327   return GST_FLOW_OK;
328 }
329
330 static GstStateChangeReturn
331 gst_gl_upload_element_change_state (GstElement * element,
332     GstStateChange transition)
333 {
334   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (element);
335   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
336
337   GST_DEBUG_OBJECT (upload, "changing state: %s => %s",
338       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
339       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
340
341   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
342   if (ret == GST_STATE_CHANGE_FAILURE)
343     return ret;
344
345   switch (transition) {
346     case GST_STATE_CHANGE_READY_TO_NULL:
347       _gst_gl_upload_element_clear_upload (upload);
348       break;
349     default:
350       break;
351   }
352
353   return ret;
354 }