2010-09-09 11:44:54 +00:00
|
|
|
/* Small helper element for format conversion
|
|
|
|
* Copyright (C) 2005 Tim-Philipp Müller <tim centricular net>
|
|
|
|
* Copyright (C) 2010 Brandon Lewis <brandon.lewis@collabora.co.uk>
|
|
|
|
* Copyright (C) 2010 Edward Hervey <edward.hervey@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-09-09 11:44:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "video.h"
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
caps_are_raw (const GstCaps * caps)
|
|
|
|
{
|
|
|
|
guint i, len;
|
|
|
|
|
|
|
|
len = gst_caps_get_size (caps);
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
GstStructure *st = gst_caps_get_structure (caps, i);
|
2011-08-25 14:30:49 +00:00
|
|
|
if (gst_structure_has_name (st, "video/x-raw"))
|
2010-09-09 11:44:54 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
create_element (const gchar * factory_name, GstElement ** element,
|
|
|
|
GError ** err)
|
|
|
|
{
|
|
|
|
*element = gst_element_factory_make (factory_name, NULL);
|
|
|
|
if (*element)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (err && *err == NULL) {
|
|
|
|
*err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
|
|
|
|
"cannot create element '%s' - please check your GStreamer installation",
|
|
|
|
factory_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstElement *
|
|
|
|
get_encoder (const GstCaps * caps, GError ** err)
|
|
|
|
{
|
|
|
|
GList *encoders = NULL;
|
|
|
|
GList *filtered = NULL;
|
|
|
|
GstElementFactory *factory = NULL;
|
|
|
|
GstElement *encoder = NULL;
|
|
|
|
|
|
|
|
encoders =
|
|
|
|
gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER |
|
|
|
|
GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE, GST_RANK_NONE);
|
|
|
|
|
|
|
|
if (encoders == NULL) {
|
|
|
|
*err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
|
|
|
|
"Cannot find any image encoder");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO ("got factory list %p", encoders);
|
|
|
|
gst_plugin_feature_list_debug (encoders);
|
|
|
|
|
|
|
|
filtered =
|
|
|
|
gst_element_factory_list_filter (encoders, caps, GST_PAD_SRC, FALSE);
|
|
|
|
GST_INFO ("got filtered list %p", filtered);
|
|
|
|
|
|
|
|
if (filtered == NULL) {
|
|
|
|
gchar *tmp = gst_caps_to_string (caps);
|
|
|
|
*err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
|
|
|
|
"Cannot find any image encoder for caps %s", tmp);
|
|
|
|
g_free (tmp);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_plugin_feature_list_debug (filtered);
|
|
|
|
|
|
|
|
factory = (GstElementFactory *) filtered->data;
|
|
|
|
|
|
|
|
GST_INFO ("got factory %p", factory);
|
|
|
|
encoder = gst_element_factory_create (factory, NULL);
|
|
|
|
|
|
|
|
GST_INFO ("created encoder element %p, %s", encoder,
|
2013-12-09 15:34:22 +00:00
|
|
|
GST_ELEMENT_NAME (encoder));
|
2010-09-09 11:44:54 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (encoders)
|
|
|
|
gst_plugin_feature_list_free (encoders);
|
|
|
|
if (filtered)
|
|
|
|
gst_plugin_feature_list_free (filtered);
|
|
|
|
|
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
2010-09-12 14:36:15 +00:00
|
|
|
static GstElement *
|
|
|
|
build_convert_frame_pipeline (GstElement ** src_element,
|
|
|
|
GstElement ** sink_element, const GstCaps * from_caps,
|
2015-12-11 13:42:00 +00:00
|
|
|
GstVideoCropMeta * cmeta, const GstCaps * to_caps, GError ** err)
|
2010-09-09 11:44:54 +00:00
|
|
|
{
|
2015-12-11 13:42:00 +00:00
|
|
|
GstElement *vcrop = NULL, *csp = NULL, *csp2 = NULL, *vscale = NULL;
|
|
|
|
GstElement *src = NULL, *sink = NULL, *encoder = NULL, *pipeline;
|
|
|
|
GstVideoInfo info;
|
2010-09-09 11:44:54 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2015-12-11 13:42:00 +00:00
|
|
|
if (cmeta) {
|
|
|
|
if (!create_element ("videocrop", &vcrop, &error)) {
|
2017-07-07 00:18:30 +00:00
|
|
|
g_error_free (error);
|
2015-12-11 13:42:00 +00:00
|
|
|
g_warning
|
|
|
|
("build_convert_frame_pipeline: Buffer has crop metadata but videocrop element is not found. Cropping will be disabled");
|
|
|
|
} else {
|
|
|
|
if (!create_element ("videoconvert", &csp2, &error))
|
|
|
|
goto no_elements;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-09 11:44:54 +00:00
|
|
|
/* videoscale is here to correct for the pixel-aspect-ratio for us */
|
|
|
|
GST_DEBUG ("creating elements");
|
|
|
|
if (!create_element ("appsrc", &src, &error) ||
|
2011-07-07 20:24:38 +00:00
|
|
|
!create_element ("videoconvert", &csp, &error) ||
|
2010-09-09 11:44:54 +00:00
|
|
|
!create_element ("videoscale", &vscale, &error) ||
|
|
|
|
!create_element ("appsink", &sink, &error))
|
|
|
|
goto no_elements;
|
|
|
|
|
|
|
|
pipeline = gst_pipeline_new ("videoconvert-pipeline");
|
|
|
|
if (pipeline == NULL)
|
|
|
|
goto no_pipeline;
|
|
|
|
|
|
|
|
/* Add black borders if necessary to keep the DAR */
|
|
|
|
g_object_set (vscale, "add-borders", TRUE, NULL);
|
|
|
|
|
|
|
|
GST_DEBUG ("adding elements");
|
|
|
|
gst_bin_add_many (GST_BIN (pipeline), src, csp, vscale, sink, NULL);
|
2015-12-11 13:42:00 +00:00
|
|
|
if (vcrop)
|
|
|
|
gst_bin_add_many (GST_BIN (pipeline), vcrop, csp2, NULL);
|
2010-09-09 11:44:54 +00:00
|
|
|
|
|
|
|
/* set caps */
|
|
|
|
g_object_set (src, "caps", from_caps, NULL);
|
2015-12-11 13:42:00 +00:00
|
|
|
if (vcrop) {
|
|
|
|
gst_video_info_from_caps (&info, from_caps);
|
|
|
|
g_object_set (vcrop, "left", cmeta->x, NULL);
|
|
|
|
g_object_set (vcrop, "top", cmeta->y, NULL);
|
|
|
|
g_object_set (vcrop, "right", GST_VIDEO_INFO_WIDTH (&info) - cmeta->width,
|
|
|
|
NULL);
|
|
|
|
g_object_set (vcrop, "bottom",
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&info) - cmeta->height, NULL);
|
|
|
|
GST_DEBUG ("crop meta [x,y,width,height]: %d %d %d %d", cmeta->x, cmeta->y,
|
|
|
|
cmeta->width, cmeta->height);
|
|
|
|
}
|
2010-09-09 11:44:54 +00:00
|
|
|
g_object_set (sink, "caps", to_caps, NULL);
|
|
|
|
|
|
|
|
/* FIXME: linking is still way too expensive, profile this properly */
|
2015-12-11 13:42:00 +00:00
|
|
|
if (vcrop) {
|
|
|
|
GST_DEBUG ("linking src->csp2");
|
|
|
|
if (!gst_element_link_pads (src, "src", csp2, "sink"))
|
|
|
|
goto link_failed;
|
|
|
|
|
|
|
|
GST_DEBUG ("linking csp2->vcrop");
|
|
|
|
if (!gst_element_link_pads (csp2, "src", vcrop, "sink"))
|
|
|
|
goto link_failed;
|
|
|
|
|
|
|
|
GST_DEBUG ("linking vcrop->csp");
|
|
|
|
if (!gst_element_link_pads (vcrop, "src", csp, "sink"))
|
|
|
|
goto link_failed;
|
|
|
|
} else {
|
|
|
|
GST_DEBUG ("linking src->csp");
|
|
|
|
if (!gst_element_link_pads (src, "src", csp, "sink"))
|
|
|
|
goto link_failed;
|
|
|
|
}
|
2010-09-09 11:44:54 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("linking csp->vscale");
|
2011-12-01 15:48:49 +00:00
|
|
|
if (!gst_element_link_pads_full (csp, "src", vscale, "sink",
|
|
|
|
GST_PAD_LINK_CHECK_NOTHING))
|
2010-09-09 11:44:54 +00:00
|
|
|
goto link_failed;
|
|
|
|
|
|
|
|
if (caps_are_raw (to_caps)) {
|
|
|
|
GST_DEBUG ("linking vscale->sink");
|
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
if (!gst_element_link_pads_full (vscale, "src", sink, "sink",
|
|
|
|
GST_PAD_LINK_CHECK_NOTHING))
|
2010-09-09 11:44:54 +00:00
|
|
|
goto link_failed;
|
|
|
|
} else {
|
|
|
|
encoder = get_encoder (to_caps, &error);
|
|
|
|
if (!encoder)
|
|
|
|
goto no_encoder;
|
|
|
|
gst_bin_add (GST_BIN (pipeline), encoder);
|
|
|
|
|
|
|
|
GST_DEBUG ("linking vscale->encoder");
|
|
|
|
if (!gst_element_link (vscale, encoder))
|
|
|
|
goto link_failed;
|
|
|
|
|
|
|
|
GST_DEBUG ("linking encoder->sink");
|
|
|
|
if (!gst_element_link_pads (encoder, "src", sink, "sink"))
|
|
|
|
goto link_failed;
|
|
|
|
}
|
|
|
|
|
2010-09-14 06:39:20 +00:00
|
|
|
g_object_set (src, "emit-signals", TRUE, NULL);
|
|
|
|
g_object_set (sink, "emit-signals", TRUE, NULL);
|
|
|
|
|
2010-09-12 14:36:15 +00:00
|
|
|
*src_element = src;
|
|
|
|
*sink_element = sink;
|
|
|
|
|
|
|
|
return pipeline;
|
|
|
|
/* ERRORS */
|
|
|
|
no_encoder:
|
|
|
|
{
|
|
|
|
gst_object_unref (pipeline);
|
|
|
|
|
|
|
|
GST_ERROR ("could not find an encoder for provided caps");
|
|
|
|
if (err)
|
|
|
|
*err = error;
|
|
|
|
else
|
|
|
|
g_error_free (error);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
no_elements:
|
|
|
|
{
|
|
|
|
if (src)
|
|
|
|
gst_object_unref (src);
|
2015-12-11 13:42:00 +00:00
|
|
|
if (vcrop)
|
|
|
|
gst_object_unref (vcrop);
|
2010-09-12 14:36:15 +00:00
|
|
|
if (csp)
|
|
|
|
gst_object_unref (csp);
|
2015-12-11 13:42:00 +00:00
|
|
|
if (csp2)
|
|
|
|
gst_object_unref (csp2);
|
2010-09-12 14:36:15 +00:00
|
|
|
if (vscale)
|
|
|
|
gst_object_unref (vscale);
|
|
|
|
if (sink)
|
|
|
|
gst_object_unref (sink);
|
|
|
|
GST_ERROR ("Could not convert video frame: %s", error->message);
|
|
|
|
if (err)
|
|
|
|
*err = error;
|
|
|
|
else
|
|
|
|
g_error_free (error);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
no_pipeline:
|
|
|
|
{
|
|
|
|
gst_object_unref (src);
|
2015-12-11 13:42:00 +00:00
|
|
|
if (vcrop)
|
|
|
|
gst_object_unref (vcrop);
|
2010-09-12 14:36:15 +00:00
|
|
|
gst_object_unref (csp);
|
2015-12-11 13:42:00 +00:00
|
|
|
if (csp2)
|
|
|
|
gst_object_unref (csp2);
|
2010-09-12 14:36:15 +00:00
|
|
|
gst_object_unref (vscale);
|
|
|
|
gst_object_unref (sink);
|
|
|
|
|
|
|
|
GST_ERROR ("Could not convert video frame: no pipeline (unknown error)");
|
|
|
|
if (err)
|
|
|
|
*err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
|
|
|
|
"Could not convert video frame: no pipeline (unknown error)");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
link_failed:
|
|
|
|
{
|
|
|
|
gst_object_unref (pipeline);
|
|
|
|
|
|
|
|
GST_ERROR ("Could not convert video frame: failed to link elements");
|
|
|
|
if (err)
|
|
|
|
*err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
|
|
|
|
"Could not convert video frame: failed to link elements");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-01 15:48:49 +00:00
|
|
|
* gst_video_convert_sample:
|
|
|
|
* @sample: a #GstSample
|
2010-09-12 14:36:15 +00:00
|
|
|
* @to_caps: the #GstCaps to convert to
|
|
|
|
* @timeout: the maximum amount of time allowed for the processing.
|
2011-05-23 12:21:59 +00:00
|
|
|
* @error: pointer to a #GError. Can be %NULL.
|
2010-09-12 14:36:15 +00:00
|
|
|
*
|
|
|
|
* Converts a raw video buffer into the specified output caps.
|
|
|
|
*
|
|
|
|
* The output caps can be any raw video formats or any image formats (jpeg, png, ...).
|
|
|
|
*
|
|
|
|
* The width, height and pixel-aspect-ratio can also be specified in the output caps.
|
|
|
|
*
|
2011-12-01 15:48:49 +00:00
|
|
|
* Returns: The converted #GstSample, or %NULL if an error happened (in which case @err
|
2010-09-12 14:36:15 +00:00
|
|
|
* will point to the #GError).
|
|
|
|
*/
|
2011-12-01 15:48:49 +00:00
|
|
|
GstSample *
|
|
|
|
gst_video_convert_sample (GstSample * sample, const GstCaps * to_caps,
|
|
|
|
GstClockTime timeout, GError ** error)
|
2010-09-12 14:36:15 +00:00
|
|
|
{
|
|
|
|
GstMessage *msg;
|
2011-12-01 15:48:49 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
GstSample *result = NULL;
|
2011-05-23 12:21:59 +00:00
|
|
|
GError *err = NULL;
|
2010-09-12 14:36:15 +00:00
|
|
|
GstBus *bus;
|
2011-12-01 15:48:49 +00:00
|
|
|
GstCaps *from_caps, *to_caps_copy = NULL;
|
2010-09-12 14:36:15 +00:00
|
|
|
GstFlowReturn ret;
|
|
|
|
GstElement *pipeline, *src, *sink;
|
2010-09-12 14:51:52 +00:00
|
|
|
guint i, n;
|
2010-09-12 14:36:15 +00:00
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
g_return_val_if_fail (sample != NULL, NULL);
|
2010-09-12 14:36:15 +00:00
|
|
|
g_return_val_if_fail (to_caps != NULL, NULL);
|
2011-12-01 15:48:49 +00:00
|
|
|
|
|
|
|
buf = gst_sample_get_buffer (sample);
|
|
|
|
g_return_val_if_fail (buf != NULL, NULL);
|
|
|
|
|
|
|
|
from_caps = gst_sample_get_caps (sample);
|
2011-05-09 11:05:12 +00:00
|
|
|
g_return_val_if_fail (from_caps != NULL, NULL);
|
2010-09-12 14:36:15 +00:00
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
|
2010-09-12 14:51:52 +00:00
|
|
|
to_caps_copy = gst_caps_new_empty ();
|
|
|
|
n = gst_caps_get_size (to_caps);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
GstStructure *s = gst_caps_get_structure (to_caps, i);
|
|
|
|
|
|
|
|
s = gst_structure_copy (s);
|
|
|
|
gst_structure_remove_field (s, "framerate");
|
|
|
|
gst_caps_append_structure (to_caps_copy, s);
|
|
|
|
}
|
|
|
|
|
2010-09-12 14:36:15 +00:00
|
|
|
pipeline =
|
2015-12-11 13:42:00 +00:00
|
|
|
build_convert_frame_pipeline (&src, &sink, from_caps,
|
|
|
|
gst_buffer_get_video_crop_meta (buf), to_caps_copy, &err);
|
2010-09-12 14:36:15 +00:00
|
|
|
if (!pipeline)
|
|
|
|
goto no_pipeline;
|
|
|
|
|
2010-09-09 11:44:54 +00:00
|
|
|
/* now set the pipeline to the paused state, after we push the buffer into
|
|
|
|
* appsrc, this should preroll the converted buffer in appsink */
|
2010-09-12 14:51:52 +00:00
|
|
|
GST_DEBUG ("running conversion pipeline to caps %" GST_PTR_FORMAT,
|
|
|
|
to_caps_copy);
|
2010-09-09 11:44:54 +00:00
|
|
|
gst_element_set_state (pipeline, GST_STATE_PAUSED);
|
|
|
|
|
|
|
|
/* feed buffer in appsrc */
|
2011-08-09 14:39:31 +00:00
|
|
|
GST_DEBUG ("feeding buffer %p, size %" G_GSIZE_FORMAT ", caps %"
|
|
|
|
GST_PTR_FORMAT, buf, gst_buffer_get_size (buf), from_caps);
|
2010-09-09 11:44:54 +00:00
|
|
|
g_signal_emit_by_name (src, "push-buffer", buf, &ret);
|
|
|
|
|
|
|
|
/* now see what happens. We either got an error somewhere or the pipeline
|
|
|
|
* prerolled */
|
|
|
|
bus = gst_element_get_bus (pipeline);
|
|
|
|
msg = gst_bus_timed_pop_filtered (bus,
|
|
|
|
timeout, GST_MESSAGE_ERROR | GST_MESSAGE_ASYNC_DONE);
|
|
|
|
|
|
|
|
if (msg) {
|
|
|
|
switch (GST_MESSAGE_TYPE (msg)) {
|
|
|
|
case GST_MESSAGE_ASYNC_DONE:
|
|
|
|
{
|
|
|
|
/* we're prerolled, get the frame from appsink */
|
|
|
|
g_signal_emit_by_name (sink, "pull-preroll", &result);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
GST_DEBUG ("conversion successful: result = %p", result);
|
|
|
|
} else {
|
|
|
|
GST_ERROR ("prerolled but no result frame?!");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_MESSAGE_ERROR:{
|
|
|
|
gchar *dbg = NULL;
|
|
|
|
|
2011-05-23 12:21:59 +00:00
|
|
|
gst_message_parse_error (msg, &err, &dbg);
|
|
|
|
if (err) {
|
|
|
|
GST_ERROR ("Could not convert video frame: %s", err->message);
|
|
|
|
GST_DEBUG ("%s [debug: %s]", err->message, GST_STR_NULL (dbg));
|
|
|
|
if (error)
|
|
|
|
*error = err;
|
2010-09-09 11:44:54 +00:00
|
|
|
else
|
2011-05-23 12:21:59 +00:00
|
|
|
g_error_free (err);
|
2010-09-09 11:44:54 +00:00
|
|
|
}
|
|
|
|
g_free (dbg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:{
|
|
|
|
g_return_val_if_reached (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_message_unref (msg);
|
|
|
|
} else {
|
|
|
|
GST_ERROR ("Could not convert video frame: timeout during conversion");
|
2011-05-23 12:21:59 +00:00
|
|
|
if (error)
|
|
|
|
*error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
|
2010-09-09 11:44:54 +00:00
|
|
|
"Could not convert video frame: timeout during conversion");
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (bus);
|
|
|
|
gst_object_unref (pipeline);
|
2010-09-12 14:51:52 +00:00
|
|
|
gst_caps_unref (to_caps_copy);
|
2010-09-09 11:44:54 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* ERRORS */
|
2010-09-12 14:36:15 +00:00
|
|
|
no_pipeline:
|
2010-09-09 11:44:54 +00:00
|
|
|
{
|
2010-09-12 14:51:52 +00:00
|
|
|
gst_caps_unref (to_caps_copy);
|
|
|
|
|
2011-05-23 12:21:59 +00:00
|
|
|
if (error)
|
|
|
|
*error = err;
|
2010-09-09 11:44:54 +00:00
|
|
|
else
|
2011-05-23 12:21:59 +00:00
|
|
|
g_error_free (err);
|
2010-09-12 14:36:15 +00:00
|
|
|
|
2010-09-09 11:44:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-09-09 23:34:56 +00:00
|
|
|
GMutex mutex;
|
2010-09-14 06:39:20 +00:00
|
|
|
GstElement *pipeline;
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleCallback callback;
|
2010-09-14 06:39:20 +00:00
|
|
|
gpointer user_data;
|
2010-09-15 09:26:48 +00:00
|
|
|
GDestroyNotify destroy_notify;
|
2010-09-14 06:39:20 +00:00
|
|
|
GMainContext *context;
|
2011-12-01 15:48:49 +00:00
|
|
|
GstSample *sample;
|
|
|
|
//GstBuffer *buffer;
|
2017-03-20 11:50:36 +00:00
|
|
|
GSource *timeout_source;
|
2010-09-14 06:39:20 +00:00
|
|
|
gboolean finished;
|
2011-12-01 15:48:49 +00:00
|
|
|
} GstVideoConvertSampleContext;
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleCallback callback;
|
|
|
|
GstSample *sample;
|
|
|
|
//GstBuffer *buffer;
|
2010-09-14 06:39:20 +00:00
|
|
|
GError *error;
|
|
|
|
gpointer user_data;
|
2010-09-15 09:26:48 +00:00
|
|
|
GDestroyNotify destroy_notify;
|
2010-09-14 06:39:20 +00:00
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleContext *context;
|
|
|
|
} GstVideoConvertSampleCallbackContext;
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
static void
|
2011-12-01 15:48:49 +00:00
|
|
|
gst_video_convert_frame_context_free (GstVideoConvertSampleContext * ctx)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
|
|
|
/* Wait until all users of the mutex are done */
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_lock (&ctx->mutex);
|
|
|
|
g_mutex_unlock (&ctx->mutex);
|
|
|
|
g_mutex_clear (&ctx->mutex);
|
2017-03-20 11:50:36 +00:00
|
|
|
if (ctx->timeout_source)
|
|
|
|
g_source_destroy (ctx->timeout_source);
|
2011-12-01 15:48:49 +00:00
|
|
|
//if (ctx->buffer)
|
|
|
|
// gst_buffer_unref (ctx->buffer);
|
|
|
|
if (ctx->sample)
|
|
|
|
gst_sample_unref (ctx->sample);
|
2010-09-14 06:39:20 +00:00
|
|
|
g_main_context_unref (ctx->context);
|
|
|
|
|
|
|
|
gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (ctx->pipeline);
|
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
g_slice_free (GstVideoConvertSampleContext, ctx);
|
2010-09-14 06:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_video_convert_frame_callback_context_free
|
2011-12-01 15:48:49 +00:00
|
|
|
(GstVideoConvertSampleCallbackContext * ctx)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
|
|
|
if (ctx->context)
|
|
|
|
gst_video_convert_frame_context_free (ctx->context);
|
2011-12-01 15:48:49 +00:00
|
|
|
g_slice_free (GstVideoConvertSampleCallbackContext, ctx);
|
2010-09-14 06:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-12-01 15:48:49 +00:00
|
|
|
convert_frame_dispatch_callback (GstVideoConvertSampleCallbackContext * ctx)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
2011-12-01 15:48:49 +00:00
|
|
|
ctx->callback (ctx->sample, ctx->error, ctx->user_data);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
2010-09-15 09:26:48 +00:00
|
|
|
if (ctx->destroy_notify)
|
|
|
|
ctx->destroy_notify (ctx->user_data);
|
|
|
|
|
2010-09-14 06:39:20 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-12-01 15:48:49 +00:00
|
|
|
convert_frame_finish (GstVideoConvertSampleContext * context,
|
|
|
|
GstSample * sample, GError * error)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
|
|
|
GSource *source;
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleCallbackContext *ctx;
|
2010-09-14 06:39:20 +00:00
|
|
|
|
2017-03-20 11:50:36 +00:00
|
|
|
if (context->timeout_source)
|
|
|
|
g_source_destroy (context->timeout_source);
|
|
|
|
context->timeout_source = NULL;
|
2010-09-14 06:39:20 +00:00
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
ctx = g_slice_new (GstVideoConvertSampleCallbackContext);
|
2010-09-14 06:39:20 +00:00
|
|
|
ctx->callback = context->callback;
|
|
|
|
ctx->user_data = context->user_data;
|
2010-09-15 09:26:48 +00:00
|
|
|
ctx->destroy_notify = context->destroy_notify;
|
2011-12-01 15:48:49 +00:00
|
|
|
ctx->sample = sample;
|
|
|
|
//ctx->buffer = buffer;
|
2010-09-14 06:39:20 +00:00
|
|
|
ctx->error = error;
|
|
|
|
ctx->context = context;
|
|
|
|
|
|
|
|
source = g_timeout_source_new (0);
|
|
|
|
g_source_set_callback (source,
|
|
|
|
(GSourceFunc) convert_frame_dispatch_callback, ctx,
|
|
|
|
(GDestroyNotify) gst_video_convert_frame_callback_context_free);
|
|
|
|
g_source_attach (source, context->context);
|
|
|
|
g_source_unref (source);
|
|
|
|
|
|
|
|
context->finished = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-12-01 15:48:49 +00:00
|
|
|
convert_frame_timeout_callback (GstVideoConvertSampleContext * context)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
|
|
|
GError *error;
|
|
|
|
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_lock (&context->mutex);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
if (context->finished)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
GST_ERROR ("Could not convert video frame: timeout");
|
|
|
|
|
|
|
|
error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
|
|
|
|
"Could not convert video frame: timeout");
|
|
|
|
|
|
|
|
convert_frame_finish (context, NULL, error);
|
|
|
|
|
|
|
|
done:
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_unlock (&context->mutex);
|
2010-09-14 06:39:20 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
convert_frame_bus_callback (GstBus * bus, GstMessage * message,
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleContext * context)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_lock (&context->mutex);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
if (context->finished)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE (message)) {
|
|
|
|
case GST_MESSAGE_ERROR:{
|
|
|
|
GError *error;
|
|
|
|
gchar *dbg = NULL;
|
|
|
|
|
|
|
|
gst_message_parse_error (message, &error, &dbg);
|
|
|
|
|
|
|
|
GST_ERROR ("Could not convert video frame: %s", error->message);
|
|
|
|
GST_DEBUG ("%s [debug: %s]", error->message, GST_STR_NULL (dbg));
|
|
|
|
|
|
|
|
convert_frame_finish (context, NULL, error);
|
|
|
|
|
|
|
|
g_free (dbg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_unlock (&context->mutex);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
convert_frame_need_data_callback (GstElement * src, guint size,
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleContext * context)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_ERROR;
|
|
|
|
GError *error;
|
2011-12-01 15:48:49 +00:00
|
|
|
GstBuffer *buffer;
|
2010-09-14 06:39:20 +00:00
|
|
|
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_lock (&context->mutex);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
if (context->finished)
|
|
|
|
goto done;
|
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
buffer = gst_sample_get_buffer (context->sample);
|
|
|
|
g_signal_emit_by_name (src, "push-buffer", buffer, &ret);
|
|
|
|
gst_sample_unref (context->sample);
|
|
|
|
context->sample = NULL;
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_ERROR ("Could not push video frame: %s", gst_flow_get_name (ret));
|
|
|
|
|
|
|
|
error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
|
|
|
|
"Could not push video frame: %s", gst_flow_get_name (ret));
|
|
|
|
|
|
|
|
convert_frame_finish (context, NULL, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (src, convert_frame_need_data_callback,
|
|
|
|
context);
|
|
|
|
|
|
|
|
done:
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_unlock (&context->mutex);
|
2010-09-14 06:39:20 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 11:39:20 +00:00
|
|
|
static GstFlowReturn
|
2011-12-01 15:48:49 +00:00
|
|
|
convert_frame_new_preroll_callback (GstElement * sink,
|
|
|
|
GstVideoConvertSampleContext * context)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
2011-12-01 15:48:49 +00:00
|
|
|
GstSample *sample = NULL;
|
2010-09-14 06:39:20 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_lock (&context->mutex);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
if (context->finished)
|
|
|
|
goto done;
|
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
g_signal_emit_by_name (sink, "pull-preroll", &sample);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
if (!sample) {
|
2010-09-14 06:39:20 +00:00
|
|
|
error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
|
2011-12-01 15:48:49 +00:00
|
|
|
"Could not get converted video sample");
|
2010-09-14 06:39:20 +00:00
|
|
|
}
|
2011-12-01 15:48:49 +00:00
|
|
|
convert_frame_finish (context, sample, error);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (sink, convert_frame_need_data_callback,
|
|
|
|
context);
|
|
|
|
|
|
|
|
done:
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_unlock (&context->mutex);
|
2012-09-14 11:39:20 +00:00
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2010-09-14 06:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-01 15:48:49 +00:00
|
|
|
* gst_video_convert_sample_async:
|
|
|
|
* @sample: a #GstSample
|
2010-09-14 06:39:20 +00:00
|
|
|
* @to_caps: the #GstCaps to convert to
|
|
|
|
* @timeout: the maximum amount of time allowed for the processing.
|
2011-12-01 15:48:49 +00:00
|
|
|
* @callback: %GstVideoConvertSampleCallback that will be called after conversion.
|
2011-05-23 12:21:59 +00:00
|
|
|
* @user_data: extra data that will be passed to the @callback
|
2010-09-15 09:26:48 +00:00
|
|
|
* @destroy_notify: %GDestroyNotify to be called after @user_data is not needed anymore
|
2010-09-14 06:39:20 +00:00
|
|
|
*
|
|
|
|
* Converts a raw video buffer into the specified output caps.
|
|
|
|
*
|
|
|
|
* The output caps can be any raw video formats or any image formats (jpeg, png, ...).
|
|
|
|
*
|
|
|
|
* The width, height and pixel-aspect-ratio can also be specified in the output caps.
|
|
|
|
*
|
|
|
|
* @callback will be called after conversion, when an error occured or if conversion didn't
|
|
|
|
* finish after @timeout. @callback will always be called from the thread default
|
|
|
|
* %GMainContext, see g_main_context_get_thread_default(). If GLib before 2.22 is used,
|
|
|
|
* this will always be the global default main context.
|
|
|
|
*
|
2010-09-15 09:26:48 +00:00
|
|
|
* @destroy_notify will be called after the callback was called and @user_data is not needed
|
|
|
|
* anymore.
|
2010-09-14 06:39:20 +00:00
|
|
|
*/
|
|
|
|
void
|
2011-12-01 15:48:49 +00:00
|
|
|
gst_video_convert_sample_async (GstSample * sample,
|
2011-05-09 11:05:12 +00:00
|
|
|
const GstCaps * to_caps, GstClockTime timeout,
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleCallback callback, gpointer user_data,
|
2011-05-09 11:05:12 +00:00
|
|
|
GDestroyNotify destroy_notify)
|
2010-09-14 06:39:20 +00:00
|
|
|
{
|
|
|
|
GMainContext *context = NULL;
|
|
|
|
GError *error = NULL;
|
|
|
|
GstBus *bus;
|
2011-12-01 15:48:49 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
GstCaps *from_caps, *to_caps_copy = NULL;
|
2010-09-14 06:39:20 +00:00
|
|
|
GstElement *pipeline, *src, *sink;
|
|
|
|
guint i, n;
|
|
|
|
GSource *source;
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleContext *ctx;
|
2010-09-14 06:39:20 +00:00
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
g_return_if_fail (sample != NULL);
|
|
|
|
buf = gst_sample_get_buffer (sample);
|
2010-09-14 06:39:20 +00:00
|
|
|
g_return_if_fail (buf != NULL);
|
2011-12-01 15:48:49 +00:00
|
|
|
|
2010-09-14 06:39:20 +00:00
|
|
|
g_return_if_fail (to_caps != NULL);
|
2011-12-01 15:48:49 +00:00
|
|
|
|
|
|
|
from_caps = gst_sample_get_caps (sample);
|
2011-05-09 11:05:12 +00:00
|
|
|
g_return_if_fail (from_caps != NULL);
|
2010-09-15 09:26:48 +00:00
|
|
|
g_return_if_fail (callback != NULL);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
context = g_main_context_get_thread_default ();
|
2010-12-04 14:45:58 +00:00
|
|
|
|
2010-09-14 06:39:20 +00:00
|
|
|
if (!context)
|
|
|
|
context = g_main_context_default ();
|
|
|
|
|
|
|
|
to_caps_copy = gst_caps_new_empty ();
|
|
|
|
n = gst_caps_get_size (to_caps);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
GstStructure *s = gst_caps_get_structure (to_caps, i);
|
|
|
|
|
|
|
|
s = gst_structure_copy (s);
|
|
|
|
gst_structure_remove_field (s, "framerate");
|
|
|
|
gst_caps_append_structure (to_caps_copy, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline =
|
2015-12-11 13:42:00 +00:00
|
|
|
build_convert_frame_pipeline (&src, &sink, from_caps,
|
|
|
|
gst_buffer_get_video_crop_meta (buf), to_caps_copy, &error);
|
2010-09-14 06:39:20 +00:00
|
|
|
if (!pipeline)
|
|
|
|
goto no_pipeline;
|
|
|
|
|
|
|
|
bus = gst_element_get_bus (pipeline);
|
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
ctx = g_slice_new0 (GstVideoConvertSampleContext);
|
2012-09-09 23:34:56 +00:00
|
|
|
g_mutex_init (&ctx->mutex);
|
2011-12-01 15:48:49 +00:00
|
|
|
//ctx->buffer = gst_buffer_ref (buf);
|
|
|
|
ctx->sample = gst_sample_ref (sample);
|
2010-09-14 06:39:20 +00:00
|
|
|
ctx->callback = callback;
|
|
|
|
ctx->user_data = user_data;
|
2010-09-15 09:26:48 +00:00
|
|
|
ctx->destroy_notify = destroy_notify;
|
2010-09-14 06:39:20 +00:00
|
|
|
ctx->context = g_main_context_ref (context);
|
|
|
|
ctx->finished = FALSE;
|
|
|
|
ctx->pipeline = pipeline;
|
|
|
|
|
|
|
|
if (timeout != GST_CLOCK_TIME_NONE) {
|
2017-03-20 11:50:36 +00:00
|
|
|
ctx->timeout_source = g_timeout_source_new (timeout / GST_MSECOND);
|
|
|
|
g_source_set_callback (ctx->timeout_source,
|
2010-09-14 06:39:20 +00:00
|
|
|
(GSourceFunc) convert_frame_timeout_callback, ctx, NULL);
|
2017-03-20 11:50:36 +00:00
|
|
|
g_source_attach (ctx->timeout_source, context);
|
2010-09-14 06:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_signal_connect (src, "need-data",
|
|
|
|
G_CALLBACK (convert_frame_need_data_callback), ctx);
|
|
|
|
g_signal_connect (sink, "new-preroll",
|
2011-12-01 15:48:49 +00:00
|
|
|
G_CALLBACK (convert_frame_new_preroll_callback), ctx);
|
2010-09-14 06:39:20 +00:00
|
|
|
|
|
|
|
source = gst_bus_create_watch (bus);
|
|
|
|
g_source_set_callback (source, (GSourceFunc) convert_frame_bus_callback,
|
|
|
|
ctx, NULL);
|
|
|
|
g_source_attach (source, context);
|
|
|
|
g_source_unref (source);
|
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_PLAYING);
|
|
|
|
|
|
|
|
gst_object_unref (bus);
|
|
|
|
gst_caps_unref (to_caps_copy);
|
|
|
|
|
|
|
|
return;
|
|
|
|
/* ERRORS */
|
|
|
|
no_pipeline:
|
|
|
|
{
|
2011-12-01 15:48:49 +00:00
|
|
|
GstVideoConvertSampleCallbackContext *ctx;
|
2010-09-14 06:39:20 +00:00
|
|
|
GSource *source;
|
|
|
|
|
|
|
|
gst_caps_unref (to_caps_copy);
|
|
|
|
|
2011-12-01 15:48:49 +00:00
|
|
|
ctx = g_slice_new0 (GstVideoConvertSampleCallbackContext);
|
2010-09-14 06:39:20 +00:00
|
|
|
ctx->callback = callback;
|
|
|
|
ctx->user_data = user_data;
|
2010-09-15 09:26:48 +00:00
|
|
|
ctx->destroy_notify = destroy_notify;
|
2011-12-01 15:48:49 +00:00
|
|
|
ctx->sample = NULL;
|
2010-09-14 06:39:20 +00:00
|
|
|
ctx->error = error;
|
|
|
|
|
|
|
|
source = g_timeout_source_new (0);
|
|
|
|
g_source_set_callback (source,
|
|
|
|
(GSourceFunc) convert_frame_dispatch_callback, ctx,
|
|
|
|
(GDestroyNotify) gst_video_convert_frame_callback_context_free);
|
|
|
|
g_source_attach (source, context);
|
|
|
|
g_source_unref (source);
|
|
|
|
}
|
|
|
|
}
|