• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #include "gstglelements.h"
30 #include "gstgluploadelement.h"
31 
32 GST_DEBUG_CATEGORY_STATIC (gst_gl_upload_element_debug);
33 #define GST_CAT_DEFAULT gst_gl_upload_element_debug
34 
35 #define gst_gl_upload_element_parent_class parent_class
36 G_DEFINE_TYPE_WITH_CODE (GstGLUploadElement, gst_gl_upload_element,
37     GST_TYPE_GL_BASE_FILTER,
38     GST_DEBUG_CATEGORY_INIT (gst_gl_upload_element_debug, "gluploadelement", 0,
39         "glupload Element"););
40 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (glupload, "glupload",
41     GST_RANK_NONE, GST_TYPE_GL_UPLOAD_ELEMENT, gl_element_init (plugin));
42 
43 static gboolean gst_gl_upload_element_get_unit_size (GstBaseTransform * trans,
44     GstCaps * caps, gsize * size);
45 static GstCaps *_gst_gl_upload_element_transform_caps (GstBaseTransform * bt,
46     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
47 static gboolean _gst_gl_upload_element_set_caps (GstBaseTransform * bt,
48     GstCaps * in_caps, GstCaps * out_caps);
49 static gboolean gst_gl_upload_element_filter_meta (GstBaseTransform * trans,
50     GstQuery * query, GType api, const GstStructure * params);
51 static gboolean _gst_gl_upload_element_propose_allocation (GstBaseTransform *
52     bt, GstQuery * decide_query, GstQuery * query);
53 static gboolean _gst_gl_upload_element_decide_allocation (GstBaseTransform *
54     trans, GstQuery * query);
55 static GstFlowReturn
56 gst_gl_upload_element_prepare_output_buffer (GstBaseTransform * bt,
57     GstBuffer * buffer, GstBuffer ** outbuf);
58 static GstFlowReturn gst_gl_upload_element_transform (GstBaseTransform * bt,
59     GstBuffer * buffer, GstBuffer * outbuf);
60 static gboolean gst_gl_upload_element_stop (GstBaseTransform * bt);
61 static GstStateChangeReturn
62 gst_gl_upload_element_change_state (GstElement * element,
63     GstStateChange transition);
64 
65 
66 static GstStaticPadTemplate gst_gl_upload_element_src_pad_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
68     GST_PAD_SRC,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS ("video/x-raw(ANY)"));
71 
72 static void
_gst_gl_upload_element_clear_upload(GstGLUploadElement * upload)73 _gst_gl_upload_element_clear_upload (GstGLUploadElement * upload)
74 {
75   GstGLUpload *ul = NULL;
76 
77   GST_OBJECT_LOCK (upload);
78   ul = upload->upload;
79   upload->upload = NULL;
80   GST_OBJECT_UNLOCK (upload);
81 
82   if (ul)
83     gst_object_unref (ul);
84 }
85 
86 static void
gst_gl_upload_element_finalize(GObject * object)87 gst_gl_upload_element_finalize (GObject * object)
88 {
89   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (object);
90 
91   _gst_gl_upload_element_clear_upload (upload);
92 
93   G_OBJECT_CLASS (parent_class)->finalize (object);
94 }
95 
96 static void
gst_gl_upload_element_class_init(GstGLUploadElementClass * klass)97 gst_gl_upload_element_class_init (GstGLUploadElementClass * klass)
98 {
99   GstBaseTransformClass *bt_class = GST_BASE_TRANSFORM_CLASS (klass);
100   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
101   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
102   GstCaps *upload_caps;
103 
104   bt_class->transform_caps = _gst_gl_upload_element_transform_caps;
105   bt_class->set_caps = _gst_gl_upload_element_set_caps;
106   bt_class->filter_meta = gst_gl_upload_element_filter_meta;
107   bt_class->propose_allocation = _gst_gl_upload_element_propose_allocation;
108   bt_class->decide_allocation = _gst_gl_upload_element_decide_allocation;
109   bt_class->get_unit_size = gst_gl_upload_element_get_unit_size;
110   bt_class->prepare_output_buffer = gst_gl_upload_element_prepare_output_buffer;
111   bt_class->transform = gst_gl_upload_element_transform;
112   bt_class->stop = gst_gl_upload_element_stop;
113 
114   element_class->change_state = gst_gl_upload_element_change_state;
115 
116   gst_element_class_add_static_pad_template (element_class,
117       &gst_gl_upload_element_src_pad_template);
118 
119   upload_caps = gst_gl_upload_get_input_template_caps ();
120   gst_element_class_add_pad_template (element_class,
121       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, upload_caps));
122   gst_caps_unref (upload_caps);
123 
124   gst_element_class_set_metadata (element_class,
125       "OpenGL uploader", "Filter/Video",
126       "Uploads data into OpenGL", "Matthew Waters <matthew@centricular.com>");
127 
128   gobject_class->finalize = gst_gl_upload_element_finalize;
129 }
130 
131 static void
gst_gl_upload_element_init(GstGLUploadElement * upload)132 gst_gl_upload_element_init (GstGLUploadElement * upload)
133 {
134   gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (upload), TRUE);
135 }
136 
137 static gboolean
gst_gl_upload_element_stop(GstBaseTransform * bt)138 gst_gl_upload_element_stop (GstBaseTransform * bt)
139 {
140   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
141 
142   _gst_gl_upload_element_clear_upload (upload);
143 
144   return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (bt);
145 }
146 
147 static gboolean
gst_gl_upload_element_get_unit_size(GstBaseTransform * trans,GstCaps * caps,gsize * size)148 gst_gl_upload_element_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
149     gsize * size)
150 {
151   gboolean ret = FALSE;
152   GstVideoInfo info;
153 
154   ret = gst_video_info_from_caps (&info, caps);
155   if (ret)
156     *size = GST_VIDEO_INFO_SIZE (&info);
157 
158   return TRUE;
159 }
160 
161 static GstCaps *
_gst_gl_upload_element_transform_caps(GstBaseTransform * bt,GstPadDirection direction,GstCaps * caps,GstCaps * filter)162 _gst_gl_upload_element_transform_caps (GstBaseTransform * bt,
163     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
164 {
165   GstGLBaseFilter *base_filter = GST_GL_BASE_FILTER (bt);
166   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
167   GstGLContext *context;
168   GstGLUpload *ul = NULL;
169   GstCaps *ret_caps;
170 
171   if (base_filter->display && !gst_gl_base_filter_find_gl_context (base_filter))
172     return NULL;
173 
174   context = gst_gl_base_filter_get_gl_context (base_filter);
175 
176   GST_OBJECT_LOCK (upload);
177   if (upload->upload == NULL) {
178     GST_OBJECT_UNLOCK (upload);
179 
180     ul = gst_gl_upload_new (context);
181 
182     GST_OBJECT_LOCK (upload);
183     if (upload->upload) {
184       gst_object_unref (ul);
185       ul = upload->upload;
186     } else {
187       upload->upload = ul;
188     }
189   } else {
190     ul = upload->upload;
191   }
192 
193   gst_object_ref (ul);
194   GST_OBJECT_UNLOCK (upload);
195 
196   ret_caps =
197       gst_gl_upload_transform_caps (ul, context, direction, caps, filter);
198 
199   gst_object_unref (ul);
200   if (context)
201     gst_object_unref (context);
202 
203   return ret_caps;
204 }
205 
206 static gboolean
gst_gl_upload_element_filter_meta(GstBaseTransform * trans,GstQuery * query,GType api,const GstStructure * params)207 gst_gl_upload_element_filter_meta (GstBaseTransform * trans, GstQuery * query,
208     GType api, const GstStructure * params)
209 {
210   /* propose all metadata upstream */
211   return TRUE;
212 }
213 
214 static gboolean
_gst_gl_upload_element_propose_allocation(GstBaseTransform * bt,GstQuery * decide_query,GstQuery * query)215 _gst_gl_upload_element_propose_allocation (GstBaseTransform * bt,
216     GstQuery * decide_query, GstQuery * query)
217 {
218   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
219   GstGLUpload *ul;
220   GstGLContext *context;
221   gboolean ret;
222 
223   GST_OBJECT_LOCK (upload);
224   if (!upload->upload) {
225     GST_OBJECT_UNLOCK (upload);
226     return FALSE;
227   }
228   ul = gst_object_ref (upload->upload);
229   GST_OBJECT_UNLOCK (upload);
230 
231   context = gst_gl_base_filter_get_gl_context (GST_GL_BASE_FILTER (bt));
232   if (!context) {
233     gst_object_unref (ul);
234     return FALSE;
235   }
236 
237   gst_gl_upload_set_context (ul, context);
238 
239   ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (bt,
240       decide_query, query);
241   gst_gl_upload_propose_allocation (ul, decide_query, query);
242 
243   gst_object_unref (ul);
244   gst_object_unref (context);
245 
246   return ret;
247 }
248 
249 static gboolean
_gst_gl_upload_element_decide_allocation(GstBaseTransform * trans,GstQuery * query)250 _gst_gl_upload_element_decide_allocation (GstBaseTransform * trans,
251     GstQuery * query)
252 {
253   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (trans);
254   GstGLContext *context = GST_GL_BASE_FILTER (trans)->context;
255 
256   if (upload->upload && context)
257     gst_gl_upload_set_context (upload->upload, context);
258 
259   return
260       GST_BASE_TRANSFORM_CLASS
261       (gst_gl_upload_element_parent_class)->decide_allocation (trans, query);
262 }
263 
264 static gboolean
_gst_gl_upload_element_set_caps(GstBaseTransform * bt,GstCaps * in_caps,GstCaps * out_caps)265 _gst_gl_upload_element_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
266     GstCaps * out_caps)
267 {
268   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
269 
270   return gst_gl_upload_set_caps (upload->upload, in_caps, out_caps);
271 }
272 
273 GstFlowReturn
gst_gl_upload_element_prepare_output_buffer(GstBaseTransform * bt,GstBuffer * buffer,GstBuffer ** outbuf)274 gst_gl_upload_element_prepare_output_buffer (GstBaseTransform * bt,
275     GstBuffer * buffer, GstBuffer ** outbuf)
276 {
277   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
278   GstGLUploadReturn ret;
279   GstBaseTransformClass *bclass;
280 
281   bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
282 
283   if (gst_base_transform_is_passthrough (bt)) {
284     *outbuf = buffer;
285     return GST_FLOW_OK;
286   }
287 
288   if (!upload->upload)
289     return GST_FLOW_NOT_NEGOTIATED;
290 
291 again:
292   ret = gst_gl_upload_perform_with_buffer (upload->upload, buffer, outbuf);
293   if (ret == GST_GL_UPLOAD_RECONFIGURE) {
294     GstPad *sinkpad = GST_BASE_TRANSFORM_SINK_PAD (bt);
295     GstCaps *incaps = gst_pad_get_current_caps (sinkpad);
296     GST_DEBUG_OBJECT (bt,
297         "Failed to upload with curren caps -- reconfiguring.");
298     /* Note: gst_base_transform_reconfigure_src() cannot be used here.
299      * Reconfiguring must be synchronous to avoid dropping the current
300      * buffer */
301     gst_pad_send_event (sinkpad, gst_event_new_caps (incaps));
302     gst_caps_unref (incaps);
303     if (!gst_pad_needs_reconfigure (GST_BASE_TRANSFORM_SRC_PAD (bt))) {
304       GST_DEBUG_OBJECT (bt, "Retry uploading with new caps");
305       goto again;
306     }
307     return GST_FLOW_OK;
308   }
309 
310   if (ret != GST_GL_UPLOAD_DONE || *outbuf == NULL) {
311     GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND, ("%s",
312             "Failed to upload buffer"), (NULL));
313     if (*outbuf)
314       gst_buffer_unref (*outbuf);
315     return GST_FLOW_ERROR;
316   }
317 
318   /* basetransform doesn't unref if they're the same */
319   if (buffer == *outbuf)
320     gst_buffer_unref (*outbuf);
321   else
322     bclass->copy_metadata (bt, buffer, *outbuf);
323 
324   return GST_FLOW_OK;
325 }
326 
327 static GstFlowReturn
gst_gl_upload_element_transform(GstBaseTransform * bt,GstBuffer * buffer,GstBuffer * outbuf)328 gst_gl_upload_element_transform (GstBaseTransform * bt, GstBuffer * buffer,
329     GstBuffer * outbuf)
330 {
331   return GST_FLOW_OK;
332 }
333 
334 static GstStateChangeReturn
gst_gl_upload_element_change_state(GstElement * element,GstStateChange transition)335 gst_gl_upload_element_change_state (GstElement * element,
336     GstStateChange transition)
337 {
338   GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (element);
339   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
340 
341   GST_DEBUG_OBJECT (upload, "changing state: %s => %s",
342       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
343       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
344 
345   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
346   if (ret == GST_STATE_CHANGE_FAILURE)
347     return ret;
348 
349   switch (transition) {
350     case GST_STATE_CHANGE_READY_TO_NULL:
351       _gst_gl_upload_element_clear_upload (upload);
352       break;
353     default:
354       break;
355   }
356 
357   return ret;
358 }
359