2015-02-11 12:29:01 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <gst/gl/gl.h>
|
2020-12-11 14:42:32 +00:00
|
|
|
|
|
|
|
#include "gstglelements.h"
|
2015-02-11 12:29:01 +00:00
|
|
|
#include "gstgluploadelement.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_gl_upload_element_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_gl_upload_element_debug
|
|
|
|
|
|
|
|
#define gst_gl_upload_element_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLUploadElement, gst_gl_upload_element,
|
|
|
|
GST_TYPE_GL_BASE_FILTER,
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_upload_element_debug, "gluploadelement", 0,
|
2015-09-17 06:55:11 +00:00
|
|
|
"glupload Element"););
|
2020-12-11 14:42:32 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (glupload, "glupload",
|
|
|
|
GST_RANK_NONE, GST_TYPE_GL_UPLOAD_ELEMENT, gl_element_init (plugin));
|
2015-02-11 12:29:01 +00:00
|
|
|
|
|
|
|
static gboolean gst_gl_upload_element_get_unit_size (GstBaseTransform * trans,
|
|
|
|
GstCaps * caps, gsize * size);
|
|
|
|
static GstCaps *_gst_gl_upload_element_transform_caps (GstBaseTransform * bt,
|
|
|
|
GstPadDirection direction, GstCaps * caps, GstCaps * filter);
|
|
|
|
static gboolean _gst_gl_upload_element_set_caps (GstBaseTransform * bt,
|
|
|
|
GstCaps * in_caps, GstCaps * out_caps);
|
2015-08-21 00:27:34 +00:00
|
|
|
static gboolean gst_gl_upload_element_filter_meta (GstBaseTransform * trans,
|
|
|
|
GstQuery * query, GType api, const GstStructure * params);
|
2015-02-11 12:29:01 +00:00
|
|
|
static gboolean _gst_gl_upload_element_propose_allocation (GstBaseTransform *
|
|
|
|
bt, GstQuery * decide_query, GstQuery * query);
|
|
|
|
static gboolean _gst_gl_upload_element_decide_allocation (GstBaseTransform *
|
|
|
|
trans, GstQuery * query);
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_gl_upload_element_prepare_output_buffer (GstBaseTransform * bt,
|
|
|
|
GstBuffer * buffer, GstBuffer ** outbuf);
|
|
|
|
static GstFlowReturn gst_gl_upload_element_transform (GstBaseTransform * bt,
|
|
|
|
GstBuffer * buffer, GstBuffer * outbuf);
|
2015-04-17 08:38:16 +00:00
|
|
|
static gboolean gst_gl_upload_element_stop (GstBaseTransform * bt);
|
2023-10-13 13:34:19 +00:00
|
|
|
static GstCaps *gst_gl_upload_element_fixate_caps (GstBaseTransform * bt,
|
|
|
|
GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
|
2019-03-06 15:01:09 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_gl_upload_element_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
|
|
|
|
2015-02-11 12:29:01 +00:00
|
|
|
static GstStaticPadTemplate gst_gl_upload_element_src_pad_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/x-raw(ANY)"));
|
|
|
|
|
2020-11-03 12:19:16 +00:00
|
|
|
static void
|
|
|
|
_gst_gl_upload_element_clear_upload (GstGLUploadElement * upload)
|
|
|
|
{
|
|
|
|
GstGLUpload *ul = NULL;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (upload);
|
|
|
|
ul = upload->upload;
|
|
|
|
upload->upload = NULL;
|
|
|
|
GST_OBJECT_UNLOCK (upload);
|
|
|
|
|
|
|
|
if (ul)
|
|
|
|
gst_object_unref (ul);
|
|
|
|
}
|
|
|
|
|
2016-10-05 07:28:48 +00:00
|
|
|
static void
|
|
|
|
gst_gl_upload_element_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (object);
|
|
|
|
|
2020-11-03 12:19:16 +00:00
|
|
|
_gst_gl_upload_element_clear_upload (upload);
|
2016-10-05 07:28:48 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2015-02-11 12:29:01 +00:00
|
|
|
static void
|
|
|
|
gst_gl_upload_element_class_init (GstGLUploadElementClass * klass)
|
|
|
|
{
|
|
|
|
GstBaseTransformClass *bt_class = GST_BASE_TRANSFORM_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2016-10-05 07:28:48 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2015-04-28 10:11:07 +00:00
|
|
|
GstCaps *upload_caps;
|
2015-02-11 12:29:01 +00:00
|
|
|
|
|
|
|
bt_class->transform_caps = _gst_gl_upload_element_transform_caps;
|
|
|
|
bt_class->set_caps = _gst_gl_upload_element_set_caps;
|
2015-08-21 00:27:34 +00:00
|
|
|
bt_class->filter_meta = gst_gl_upload_element_filter_meta;
|
2015-02-11 12:29:01 +00:00
|
|
|
bt_class->propose_allocation = _gst_gl_upload_element_propose_allocation;
|
|
|
|
bt_class->decide_allocation = _gst_gl_upload_element_decide_allocation;
|
|
|
|
bt_class->get_unit_size = gst_gl_upload_element_get_unit_size;
|
|
|
|
bt_class->prepare_output_buffer = gst_gl_upload_element_prepare_output_buffer;
|
|
|
|
bt_class->transform = gst_gl_upload_element_transform;
|
2015-04-17 08:38:16 +00:00
|
|
|
bt_class->stop = gst_gl_upload_element_stop;
|
2023-10-13 13:34:19 +00:00
|
|
|
bt_class->fixate_caps = gst_gl_upload_element_fixate_caps;
|
2015-02-11 12:29:01 +00:00
|
|
|
|
2019-03-06 15:01:09 +00:00
|
|
|
element_class->change_state = gst_gl_upload_element_change_state;
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_gl_upload_element_src_pad_template);
|
2015-04-28 10:11:07 +00:00
|
|
|
|
|
|
|
upload_caps = gst_gl_upload_get_input_template_caps ();
|
2015-02-11 12:29:01 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
2015-04-28 10:11:07 +00:00
|
|
|
gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, upload_caps));
|
|
|
|
gst_caps_unref (upload_caps);
|
2015-02-11 12:29:01 +00:00
|
|
|
|
|
|
|
gst_element_class_set_metadata (element_class,
|
|
|
|
"OpenGL uploader", "Filter/Video",
|
|
|
|
"Uploads data into OpenGL", "Matthew Waters <matthew@centricular.com>");
|
2016-10-05 07:28:48 +00:00
|
|
|
|
|
|
|
gobject_class->finalize = gst_gl_upload_element_finalize;
|
2015-02-11 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_upload_element_init (GstGLUploadElement * upload)
|
|
|
|
{
|
|
|
|
gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (upload), TRUE);
|
|
|
|
}
|
|
|
|
|
2015-04-17 08:38:16 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_upload_element_stop (GstBaseTransform * bt)
|
2015-02-11 12:29:01 +00:00
|
|
|
{
|
2015-04-17 08:38:16 +00:00
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
|
2015-02-11 12:29:01 +00:00
|
|
|
|
2020-11-03 12:19:16 +00:00
|
|
|
_gst_gl_upload_element_clear_upload (upload);
|
2015-02-11 12:29:01 +00:00
|
|
|
|
2015-04-17 08:38:16 +00:00
|
|
|
return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (bt);
|
2015-02-11 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gl_upload_element_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
|
|
|
|
gsize * size)
|
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
GstVideoInfo info;
|
|
|
|
|
|
|
|
ret = gst_video_info_from_caps (&info, caps);
|
|
|
|
if (ret)
|
|
|
|
*size = GST_VIDEO_INFO_SIZE (&info);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
_gst_gl_upload_element_transform_caps (GstBaseTransform * bt,
|
|
|
|
GstPadDirection direction, GstCaps * caps, GstCaps * filter)
|
|
|
|
{
|
2018-05-03 03:59:07 +00:00
|
|
|
GstGLBaseFilter *base_filter = GST_GL_BASE_FILTER (bt);
|
2016-08-22 07:18:27 +00:00
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
|
2018-05-03 03:59:07 +00:00
|
|
|
GstGLContext *context;
|
2020-11-03 12:19:16 +00:00
|
|
|
GstGLUpload *ul = NULL;
|
|
|
|
GstCaps *ret_caps;
|
2015-02-11 12:29:01 +00:00
|
|
|
|
2018-05-03 03:59:07 +00:00
|
|
|
if (base_filter->display && !gst_gl_base_filter_find_gl_context (base_filter))
|
|
|
|
return NULL;
|
|
|
|
|
2020-11-04 09:02:13 +00:00
|
|
|
context = gst_gl_base_filter_get_gl_context (base_filter);
|
2016-09-05 04:44:24 +00:00
|
|
|
|
2020-11-03 12:19:16 +00:00
|
|
|
GST_OBJECT_LOCK (upload);
|
|
|
|
if (upload->upload == NULL) {
|
|
|
|
GST_OBJECT_UNLOCK (upload);
|
|
|
|
|
|
|
|
ul = gst_gl_upload_new (context);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (upload);
|
|
|
|
if (upload->upload) {
|
|
|
|
gst_object_unref (ul);
|
|
|
|
ul = upload->upload;
|
|
|
|
} else {
|
|
|
|
upload->upload = ul;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ul = upload->upload;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_object_ref (ul);
|
|
|
|
GST_OBJECT_UNLOCK (upload);
|
|
|
|
|
|
|
|
ret_caps =
|
|
|
|
gst_gl_upload_transform_caps (ul, context, direction, caps, filter);
|
2020-11-04 09:02:13 +00:00
|
|
|
|
2020-11-03 12:19:16 +00:00
|
|
|
gst_object_unref (ul);
|
2020-11-04 09:02:13 +00:00
|
|
|
if (context)
|
|
|
|
gst_object_unref (context);
|
2020-11-03 12:19:16 +00:00
|
|
|
|
|
|
|
return ret_caps;
|
2015-02-11 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 00:27:34 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_upload_element_filter_meta (GstBaseTransform * trans, GstQuery * query,
|
|
|
|
GType api, const GstStructure * params)
|
|
|
|
{
|
|
|
|
/* propose all metadata upstream */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-02-11 12:29:01 +00:00
|
|
|
static gboolean
|
|
|
|
_gst_gl_upload_element_propose_allocation (GstBaseTransform * bt,
|
|
|
|
GstQuery * decide_query, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
|
2020-11-04 15:05:27 +00:00
|
|
|
GstGLUpload *ul;
|
|
|
|
GstGLContext *context;
|
2015-08-21 00:27:34 +00:00
|
|
|
gboolean ret;
|
2015-02-11 12:29:01 +00:00
|
|
|
|
2020-11-04 15:05:27 +00:00
|
|
|
GST_OBJECT_LOCK (upload);
|
|
|
|
if (!upload->upload) {
|
|
|
|
GST_OBJECT_UNLOCK (upload);
|
2015-07-16 20:47:05 +00:00
|
|
|
return FALSE;
|
2020-11-04 15:05:27 +00:00
|
|
|
}
|
|
|
|
ul = gst_object_ref (upload->upload);
|
|
|
|
GST_OBJECT_UNLOCK (upload);
|
|
|
|
|
|
|
|
context = gst_gl_base_filter_get_gl_context (GST_GL_BASE_FILTER (bt));
|
|
|
|
if (!context) {
|
|
|
|
gst_object_unref (ul);
|
2018-05-03 03:59:07 +00:00
|
|
|
return FALSE;
|
2020-11-04 15:05:27 +00:00
|
|
|
}
|
2018-05-03 03:59:07 +00:00
|
|
|
|
2020-11-04 15:05:27 +00:00
|
|
|
gst_gl_upload_set_context (ul, context);
|
2015-07-16 20:47:05 +00:00
|
|
|
|
2015-08-21 00:27:34 +00:00
|
|
|
ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (bt,
|
|
|
|
decide_query, query);
|
2020-11-04 15:05:27 +00:00
|
|
|
gst_gl_upload_propose_allocation (ul, decide_query, query);
|
|
|
|
|
|
|
|
gst_object_unref (ul);
|
|
|
|
gst_object_unref (context);
|
2015-02-11 12:29:01 +00:00
|
|
|
|
2015-08-21 00:27:34 +00:00
|
|
|
return ret;
|
2015-02-11 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_gst_gl_upload_element_decide_allocation (GstBaseTransform * trans,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
2018-07-12 03:13:04 +00:00
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (trans);
|
|
|
|
GstGLContext *context = GST_GL_BASE_FILTER (trans)->context;
|
|
|
|
|
|
|
|
if (upload->upload && context)
|
|
|
|
gst_gl_upload_set_context (upload->upload, context);
|
|
|
|
|
2018-05-03 03:59:07 +00:00
|
|
|
return
|
2015-02-11 12:29:01 +00:00
|
|
|
GST_BASE_TRANSFORM_CLASS
|
|
|
|
(gst_gl_upload_element_parent_class)->decide_allocation (trans, query);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_gst_gl_upload_element_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
|
|
|
|
GstCaps * out_caps)
|
|
|
|
{
|
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
|
|
|
|
|
2018-05-03 03:59:07 +00:00
|
|
|
return gst_gl_upload_set_caps (upload->upload, in_caps, out_caps);
|
2015-02-11 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstFlowReturn
|
|
|
|
gst_gl_upload_element_prepare_output_buffer (GstBaseTransform * bt,
|
|
|
|
GstBuffer * buffer, GstBuffer ** outbuf)
|
|
|
|
{
|
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
|
|
|
|
GstGLUploadReturn ret;
|
2015-08-24 23:47:01 +00:00
|
|
|
GstBaseTransformClass *bclass;
|
|
|
|
|
|
|
|
bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
|
2015-02-11 12:29:01 +00:00
|
|
|
|
|
|
|
if (gst_base_transform_is_passthrough (bt)) {
|
|
|
|
*outbuf = buffer;
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!upload->upload)
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
|
2018-07-04 07:29:57 +00:00
|
|
|
again:
|
2015-02-11 12:29:01 +00:00
|
|
|
ret = gst_gl_upload_perform_with_buffer (upload->upload, buffer, outbuf);
|
2016-08-22 07:18:27 +00:00
|
|
|
if (ret == GST_GL_UPLOAD_RECONFIGURE) {
|
2018-07-04 07:29:57 +00:00
|
|
|
GstPad *sinkpad = GST_BASE_TRANSFORM_SINK_PAD (bt);
|
|
|
|
GstCaps *incaps = gst_pad_get_current_caps (sinkpad);
|
|
|
|
GST_DEBUG_OBJECT (bt,
|
|
|
|
"Failed to upload with curren caps -- reconfiguring.");
|
|
|
|
/* Note: gst_base_transform_reconfigure_src() cannot be used here.
|
|
|
|
* Reconfiguring must be synchronous to avoid dropping the current
|
|
|
|
* buffer */
|
|
|
|
gst_pad_send_event (sinkpad, gst_event_new_caps (incaps));
|
2019-03-29 03:42:45 +00:00
|
|
|
gst_caps_unref (incaps);
|
2018-07-04 07:29:57 +00:00
|
|
|
if (!gst_pad_needs_reconfigure (GST_BASE_TRANSFORM_SRC_PAD (bt))) {
|
|
|
|
GST_DEBUG_OBJECT (bt, "Retry uploading with new caps");
|
|
|
|
goto again;
|
|
|
|
}
|
2016-08-22 07:18:27 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2015-02-11 12:29:01 +00:00
|
|
|
|
2015-09-17 06:55:11 +00:00
|
|
|
if (ret != GST_GL_UPLOAD_DONE || *outbuf == NULL) {
|
|
|
|
GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND, ("%s",
|
|
|
|
"Failed to upload buffer"), (NULL));
|
|
|
|
if (*outbuf)
|
|
|
|
gst_buffer_unref (*outbuf);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-03-13 12:28:36 +00:00
|
|
|
/* basetransform doesn't unref if they're the same */
|
|
|
|
if (buffer == *outbuf)
|
|
|
|
gst_buffer_unref (*outbuf);
|
2015-08-24 23:47:01 +00:00
|
|
|
else
|
|
|
|
bclass->copy_metadata (bt, buffer, *outbuf);
|
2015-03-13 12:28:36 +00:00
|
|
|
|
2015-09-17 06:55:11 +00:00
|
|
|
return GST_FLOW_OK;
|
2015-02-11 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_gl_upload_element_transform (GstBaseTransform * bt, GstBuffer * buffer,
|
|
|
|
GstBuffer * outbuf)
|
|
|
|
{
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2019-03-06 15:01:09 +00:00
|
|
|
|
2023-10-13 13:34:19 +00:00
|
|
|
static GstCaps *
|
|
|
|
gst_gl_upload_element_fixate_caps (GstBaseTransform * bt,
|
|
|
|
GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
|
|
|
|
{
|
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (bt);
|
|
|
|
|
|
|
|
return gst_gl_upload_fixate_caps (upload->upload, direction, caps, othercaps);
|
|
|
|
}
|
|
|
|
|
2019-03-06 15:01:09 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_gl_upload_element_change_state (GstElement * element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstGLUploadElement *upload = GST_GL_UPLOAD_ELEMENT (element);
|
|
|
|
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (upload, "changing state: %s => %s",
|
|
|
|
gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
|
|
|
|
gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
2020-11-03 12:19:16 +00:00
|
|
|
_gst_gl_upload_element_clear_upload (upload);
|
2019-03-06 15:01:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|