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
gst_gl_upload_element_finalize(GObject * object)69 gst_gl_upload_element_finalize (GObject * object)
70 {
71 GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (object);
72
73 if (upload->upload)
74 gst_object_unref (upload->upload);
75 upload->upload = NULL;
76
77 G_OBJECT_CLASS (parent_class)->finalize (object);
78 }
79
80 static void
gst_gl_upload_element_class_init(GstGLUploadElementClass * klass)81 gst_gl_upload_element_class_init (GstGLUploadElementClass * klass)
82 {
83 GstBaseTransformClass *bt_class = GST_BASE_TRANSFORM_CLASS (klass);
84 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
85 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
86 GstCaps *upload_caps;
87
88 bt_class->transform_caps = _gst_gl_upload_element_transform_caps;
89 bt_class->set_caps = _gst_gl_upload_element_set_caps;
90 bt_class->filter_meta = gst_gl_upload_element_filter_meta;
91 bt_class->propose_allocation = _gst_gl_upload_element_propose_allocation;
92 bt_class->decide_allocation = _gst_gl_upload_element_decide_allocation;
93 bt_class->get_unit_size = gst_gl_upload_element_get_unit_size;
94 bt_class->prepare_output_buffer = gst_gl_upload_element_prepare_output_buffer;
95 bt_class->transform = gst_gl_upload_element_transform;
96 bt_class->stop = gst_gl_upload_element_stop;
97
98 bt_class->passthrough_on_same_caps = TRUE;
99
100 element_class->change_state = gst_gl_upload_element_change_state;
101
102 gst_element_class_add_static_pad_template (element_class,
103 &gst_gl_upload_element_src_pad_template);
104
105 upload_caps = gst_gl_upload_get_input_template_caps ();
106 gst_element_class_add_pad_template (element_class,
107 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, upload_caps));
108 gst_caps_unref (upload_caps);
109
110 gst_element_class_set_metadata (element_class,
111 "OpenGL uploader", "Filter/Video",
112 "Uploads data into OpenGL", "Matthew Waters <matthew@centricular.com>");
113
114 gobject_class->finalize = gst_gl_upload_element_finalize;
115 }
116
117 static void
gst_gl_upload_element_init(GstGLUploadElement * upload)118 gst_gl_upload_element_init (GstGLUploadElement * upload)
119 {
120 gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (upload), TRUE);
121 }
122
123 static gboolean
gst_gl_upload_element_stop(GstBaseTransform * bt)124 gst_gl_upload_element_stop (GstBaseTransform * bt)
125 {
126 GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
127
128 if (upload->upload) {
129 gst_object_unref (upload->upload);
130 upload->upload = NULL;
131 }
132
133 return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (bt);
134 }
135
136 static gboolean
gst_gl_upload_element_get_unit_size(GstBaseTransform * trans,GstCaps * caps,gsize * size)137 gst_gl_upload_element_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
138 gsize * size)
139 {
140 gboolean ret = FALSE;
141 GstVideoInfo info;
142
143 ret = gst_video_info_from_caps (&info, caps);
144 if (ret)
145 *size = GST_VIDEO_INFO_SIZE (&info);
146
147 return TRUE;
148 }
149
150 static GstCaps *
_gst_gl_upload_element_transform_caps(GstBaseTransform * bt,GstPadDirection direction,GstCaps * caps,GstCaps * filter)151 _gst_gl_upload_element_transform_caps (GstBaseTransform * bt,
152 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
153 {
154 GstGLBaseFilter *base_filter = GST_GL_BASE_FILTER (bt);
155 GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
156 GstGLContext *context;
157
158 if (base_filter->display && !gst_gl_base_filter_find_gl_context (base_filter))
159 return NULL;
160
161 context = GST_GL_BASE_FILTER (bt)->context;
162 if (upload->upload == NULL)
163 upload->upload = gst_gl_upload_new (context);
164
165 return gst_gl_upload_transform_caps (upload->upload, context, direction, caps,
166 filter);
167 }
168
169 static gboolean
gst_gl_upload_element_filter_meta(GstBaseTransform * trans,GstQuery * query,GType api,const GstStructure * params)170 gst_gl_upload_element_filter_meta (GstBaseTransform * trans, GstQuery * query,
171 GType api, const GstStructure * params)
172 {
173 /* propose all metadata upstream */
174 return TRUE;
175 }
176
177 static gboolean
_gst_gl_upload_element_propose_allocation(GstBaseTransform * bt,GstQuery * decide_query,GstQuery * query)178 _gst_gl_upload_element_propose_allocation (GstBaseTransform * bt,
179 GstQuery * decide_query, GstQuery * query)
180 {
181 GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
182 GstGLContext *context = GST_GL_BASE_FILTER (bt)->context;
183 gboolean ret;
184
185 if (!upload->upload)
186 return FALSE;
187 if (!context)
188 return FALSE;
189
190 gst_gl_upload_set_context (upload->upload, context);
191
192 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (bt,
193 decide_query, query);
194 gst_gl_upload_propose_allocation (upload->upload, decide_query, query);
195
196 return ret;
197 }
198
199 static gboolean
_gst_gl_upload_element_decide_allocation(GstBaseTransform * trans,GstQuery * query)200 _gst_gl_upload_element_decide_allocation (GstBaseTransform * trans,
201 GstQuery * query)
202 {
203 GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (trans);
204 GstGLContext *context = GST_GL_BASE_FILTER (trans)->context;
205
206 if (upload->upload && context)
207 gst_gl_upload_set_context (upload->upload, context);
208
209 return
210 GST_BASE_TRANSFORM_CLASS
211 (gst_gl_upload_element_parent_class)->decide_allocation (trans, query);
212 }
213
214 static gboolean
_gst_gl_upload_element_set_caps(GstBaseTransform * bt,GstCaps * in_caps,GstCaps * out_caps)215 _gst_gl_upload_element_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
216 GstCaps * out_caps)
217 {
218 GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
219
220 return gst_gl_upload_set_caps (upload->upload, in_caps, out_caps);
221 }
222
223 GstFlowReturn
gst_gl_upload_element_prepare_output_buffer(GstBaseTransform * bt,GstBuffer * buffer,GstBuffer ** outbuf)224 gst_gl_upload_element_prepare_output_buffer (GstBaseTransform * bt,
225 GstBuffer * buffer, GstBuffer ** outbuf)
226 {
227 GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
228 GstGLUploadReturn ret;
229 GstBaseTransformClass *bclass;
230
231 bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
232
233 if (gst_base_transform_is_passthrough (bt)) {
234 *outbuf = buffer;
235 return GST_FLOW_OK;
236 }
237
238 if (!upload->upload)
239 return GST_FLOW_NOT_NEGOTIATED;
240
241 again:
242 ret = gst_gl_upload_perform_with_buffer (upload->upload, buffer, outbuf);
243 if (ret == GST_GL_UPLOAD_RECONFIGURE) {
244 GstPad *sinkpad = GST_BASE_TRANSFORM_SINK_PAD (bt);
245 GstCaps *incaps = gst_pad_get_current_caps (sinkpad);
246 GST_DEBUG_OBJECT (bt,
247 "Failed to upload with curren caps -- reconfiguring.");
248 /* Note: gst_base_transform_reconfigure_src() cannot be used here.
249 * Reconfiguring must be synchronous to avoid dropping the current
250 * buffer */
251 gst_pad_send_event (sinkpad, gst_event_new_caps (incaps));
252 gst_caps_unref (incaps);
253 if (!gst_pad_needs_reconfigure (GST_BASE_TRANSFORM_SRC_PAD (bt))) {
254 GST_DEBUG_OBJECT (bt, "Retry uploading with new caps");
255 goto again;
256 }
257 return GST_FLOW_OK;
258 }
259
260 if (ret != GST_GL_UPLOAD_DONE || *outbuf == NULL) {
261 GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND, ("%s",
262 "Failed to upload buffer"), (NULL));
263 if (*outbuf)
264 gst_buffer_unref (*outbuf);
265 return GST_FLOW_ERROR;
266 }
267
268 /* basetransform doesn't unref if they're the same */
269 if (buffer == *outbuf)
270 gst_buffer_unref (*outbuf);
271 else
272 bclass->copy_metadata (bt, buffer, *outbuf);
273
274 return GST_FLOW_OK;
275 }
276
277 static GstFlowReturn
gst_gl_upload_element_transform(GstBaseTransform * bt,GstBuffer * buffer,GstBuffer * outbuf)278 gst_gl_upload_element_transform (GstBaseTransform * bt, GstBuffer * buffer,
279 GstBuffer * outbuf)
280 {
281 return GST_FLOW_OK;
282 }
283
284 static GstStateChangeReturn
gst_gl_upload_element_change_state(GstElement * element,GstStateChange transition)285 gst_gl_upload_element_change_state (GstElement * element,
286 GstStateChange transition)
287 {
288 GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (element);
289 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
290
291 GST_DEBUG_OBJECT (upload, "changing state: %s => %s",
292 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
293 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
294
295 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
296 if (ret == GST_STATE_CHANGE_FAILURE)
297 return ret;
298
299 switch (transition) {
300 case GST_STATE_CHANGE_READY_TO_NULL:
301 if (upload->upload) {
302 gst_object_unref (upload->upload);
303 upload->upload = NULL;
304 }
305 break;
306 default:
307 break;
308 }
309
310 return ret;
311 }
312