2010-11-24 20:28:57 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@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 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-11-24 20:28:57 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* SECTION:element-gstviewfinderbin
|
|
|
|
*
|
2010-12-02 04:33:19 +00:00
|
|
|
* The gstviewfinderbin element is a displaying element for camerabin2.
|
2010-11-24 20:28:57 +00:00
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example launch line</title>
|
|
|
|
* |[
|
|
|
|
* gst-launch -v videotestsrc ! viewfinderbin
|
|
|
|
* ]|
|
2010-12-02 04:33:19 +00:00
|
|
|
* Feeds the viewfinderbin with video test data.
|
2010-11-24 20:28:57 +00:00
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstviewfinderbin.h"
|
2011-03-23 21:41:02 +00:00
|
|
|
#include "camerabingeneral.h"
|
2011-04-28 19:05:53 +00:00
|
|
|
#include <gst/pbutils/pbutils.h>
|
2011-03-23 21:41:02 +00:00
|
|
|
|
|
|
|
#include <gst/gst-i18n-plugin.h>
|
2010-11-24 20:28:57 +00:00
|
|
|
|
2010-12-02 04:33:19 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_viewfinder_bin_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_viewfinder_bin_debug
|
|
|
|
|
2010-11-24 20:28:57 +00:00
|
|
|
enum
|
|
|
|
{
|
2010-12-10 15:08:48 +00:00
|
|
|
PROP_0,
|
|
|
|
PROP_VIDEO_SINK,
|
2011-08-23 14:19:51 +00:00
|
|
|
PROP_DISABLE_CONVERTERS
|
2010-11-24 20:28:57 +00:00
|
|
|
};
|
|
|
|
|
2011-08-23 14:19:51 +00:00
|
|
|
#define DEFAULT_DISABLE_CONVERTERS FALSE
|
|
|
|
|
2010-11-24 20:28:57 +00:00
|
|
|
/* pad templates */
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2014-04-08 18:39:20 +00:00
|
|
|
GST_STATIC_CAPS ("video/x-raw(ANY)")
|
2010-11-24 20:28:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* class initialization */
|
2011-12-23 19:33:45 +00:00
|
|
|
#define gst_viewfinder_bin_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstViewfinderBin, gst_viewfinder_bin, GST_TYPE_BIN);
|
2010-11-24 20:28:57 +00:00
|
|
|
|
2010-12-10 15:08:48 +00:00
|
|
|
static void gst_viewfinder_bin_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * spec);
|
|
|
|
static void gst_viewfinder_bin_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * spec);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_viewfinder_bin_set_video_sink (GstViewfinderBin * vfbin, GstElement * sink);
|
|
|
|
|
|
|
|
|
2010-11-24 20:28:57 +00:00
|
|
|
/* Element class functions */
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_viewfinder_bin_change_state (GstElement * element, GstStateChange trans);
|
|
|
|
|
2010-12-20 05:58:44 +00:00
|
|
|
static void
|
|
|
|
gst_viewfinder_bin_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstViewfinderBin *viewfinderbin = GST_VIEWFINDER_BIN_CAST (object);
|
|
|
|
|
|
|
|
if (viewfinderbin->user_video_sink) {
|
|
|
|
gst_object_unref (viewfinderbin->user_video_sink);
|
|
|
|
viewfinderbin->user_video_sink = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (viewfinderbin->video_sink) {
|
|
|
|
gst_object_unref (viewfinderbin->video_sink);
|
|
|
|
viewfinderbin->video_sink = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose ((GObject *) viewfinderbin);
|
|
|
|
}
|
|
|
|
|
2010-11-24 20:28:57 +00:00
|
|
|
static void
|
|
|
|
gst_viewfinder_bin_class_init (GstViewfinderBinClass * klass)
|
|
|
|
{
|
2010-12-10 15:08:48 +00:00
|
|
|
GObjectClass *gobject_klass;
|
2010-11-24 20:28:57 +00:00
|
|
|
GstElementClass *element_class;
|
|
|
|
|
2010-12-10 15:08:48 +00:00
|
|
|
gobject_klass = (GObjectClass *) klass;
|
2010-11-24 20:28:57 +00:00
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
|
|
|
element_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_viewfinder_bin_change_state);
|
2010-12-10 15:08:48 +00:00
|
|
|
|
2010-12-20 05:58:44 +00:00
|
|
|
gobject_klass->dispose = gst_viewfinder_bin_dispose;
|
2010-12-10 15:08:48 +00:00
|
|
|
gobject_klass->set_property = gst_viewfinder_bin_set_property;
|
|
|
|
gobject_klass->get_property = gst_viewfinder_bin_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_klass, PROP_VIDEO_SINK,
|
|
|
|
g_param_spec_object ("video-sink", "Video Sink",
|
|
|
|
"the video output element to use (NULL = default)",
|
|
|
|
GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2011-08-23 14:19:51 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_klass, PROP_DISABLE_CONVERTERS,
|
|
|
|
g_param_spec_boolean ("disable-converters", "Disable conversion elements",
|
|
|
|
"If video converters should be disabled (must be set on NULL)",
|
|
|
|
DEFAULT_DISABLE_CONVERTERS,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2011-12-23 19:33:45 +00:00
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sink_template));
|
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class, "Viewfinder Bin",
|
2011-12-23 19:33:45 +00:00
|
|
|
"Sink/Video", "Viewfinder Bin used in camerabin2",
|
|
|
|
"Thiago Santos <thiago.sousa.santos@collabora.com>");
|
2010-11-24 20:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-12-23 19:33:45 +00:00
|
|
|
gst_viewfinder_bin_init (GstViewfinderBin * viewfinderbin)
|
2010-11-24 20:28:57 +00:00
|
|
|
{
|
2010-12-20 05:58:44 +00:00
|
|
|
GstPadTemplate *templ = gst_static_pad_template_get (&sink_template);
|
2010-11-24 20:28:57 +00:00
|
|
|
viewfinderbin->ghostpad = gst_ghost_pad_new_no_target_from_template ("sink",
|
2010-12-20 05:58:44 +00:00
|
|
|
templ);
|
|
|
|
gst_object_unref (templ);
|
2010-11-24 20:28:57 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT_CAST (viewfinderbin),
|
|
|
|
viewfinderbin->ghostpad);
|
2011-08-23 14:19:51 +00:00
|
|
|
|
|
|
|
viewfinderbin->disable_converters = DEFAULT_DISABLE_CONVERTERS;
|
2010-11-24 20:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_viewfinder_bin_create_elements (GstViewfinderBin * vfbin)
|
|
|
|
{
|
2010-12-02 04:33:19 +00:00
|
|
|
GstElement *csp = NULL;
|
|
|
|
GstElement *videoscale = NULL;
|
2011-08-23 14:19:51 +00:00
|
|
|
GstPad *firstpad = NULL;
|
2011-04-28 19:05:53 +00:00
|
|
|
const gchar *missing_element_name;
|
2011-08-23 14:19:51 +00:00
|
|
|
gboolean newsink = FALSE;
|
|
|
|
gboolean updated_converters = FALSE;
|
2010-12-02 04:33:19 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (vfbin, "Creating internal elements");
|
2010-11-24 20:28:57 +00:00
|
|
|
|
2011-08-23 14:19:51 +00:00
|
|
|
/* First check if we need to add/replace the internal sink */
|
2010-12-12 22:33:24 +00:00
|
|
|
if (vfbin->video_sink) {
|
|
|
|
if (vfbin->user_video_sink && vfbin->video_sink != vfbin->user_video_sink) {
|
|
|
|
gst_bin_remove (GST_BIN_CAST (vfbin), vfbin->video_sink);
|
|
|
|
gst_object_unref (vfbin->video_sink);
|
|
|
|
vfbin->video_sink = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vfbin->video_sink) {
|
|
|
|
if (vfbin->user_video_sink)
|
|
|
|
vfbin->video_sink = gst_object_ref (vfbin->user_video_sink);
|
2011-03-23 21:41:02 +00:00
|
|
|
else {
|
2010-12-12 22:33:24 +00:00
|
|
|
vfbin->video_sink = gst_element_factory_make ("autovideosink",
|
|
|
|
"vfbin-sink");
|
2011-04-28 19:05:53 +00:00
|
|
|
if (!vfbin->video_sink) {
|
|
|
|
missing_element_name = "autovideosink";
|
|
|
|
goto missing_element;
|
|
|
|
}
|
2011-03-23 21:41:02 +00:00
|
|
|
}
|
2010-12-02 04:33:19 +00:00
|
|
|
|
2010-12-12 22:33:24 +00:00
|
|
|
gst_bin_add (GST_BIN_CAST (vfbin), gst_object_ref (vfbin->video_sink));
|
2011-08-23 14:19:51 +00:00
|
|
|
newsink = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if we want add/remove the conversion elements */
|
|
|
|
if (vfbin->elements_created && vfbin->disable_converters) {
|
|
|
|
/* remove the elements, user doesn't want them */
|
|
|
|
|
|
|
|
gst_ghost_pad_set_target (GST_GHOST_PAD (vfbin->ghostpad), NULL);
|
2011-11-28 21:47:50 +00:00
|
|
|
csp = gst_bin_get_by_name (GST_BIN_CAST (vfbin), "vfbin-csp");
|
|
|
|
videoscale = gst_bin_get_by_name (GST_BIN_CAST (vfbin), "vfbin-videoscale");
|
2011-08-23 14:19:51 +00:00
|
|
|
|
2011-11-28 21:47:50 +00:00
|
|
|
gst_bin_remove (GST_BIN_CAST (vfbin), csp);
|
|
|
|
gst_bin_remove (GST_BIN_CAST (vfbin), videoscale);
|
2011-08-23 14:19:51 +00:00
|
|
|
|
|
|
|
gst_object_unref (csp);
|
|
|
|
gst_object_unref (videoscale);
|
2010-11-24 20:28:57 +00:00
|
|
|
|
2011-08-23 14:19:51 +00:00
|
|
|
updated_converters = TRUE;
|
|
|
|
} else if (!vfbin->elements_created && !vfbin->disable_converters) {
|
|
|
|
gst_ghost_pad_set_target (GST_GHOST_PAD (vfbin->ghostpad), NULL);
|
|
|
|
|
|
|
|
/* add the elements, user wants them */
|
2012-05-01 13:18:52 +00:00
|
|
|
csp = gst_element_factory_make ("videoconvert", "vfbin-csp");
|
2011-08-23 14:19:51 +00:00
|
|
|
if (!csp) {
|
2012-05-01 13:18:52 +00:00
|
|
|
missing_element_name = "videoconvert";
|
2011-08-23 14:19:51 +00:00
|
|
|
goto missing_element;
|
|
|
|
}
|
2011-11-28 21:47:50 +00:00
|
|
|
gst_bin_add (GST_BIN_CAST (vfbin), csp);
|
2011-08-23 14:19:51 +00:00
|
|
|
|
2011-11-28 21:43:40 +00:00
|
|
|
videoscale = gst_element_factory_make ("videoscale", "vfbin->videoscale");
|
2011-08-23 14:19:51 +00:00
|
|
|
if (!videoscale) {
|
|
|
|
missing_element_name = "videoscale";
|
|
|
|
goto missing_element;
|
|
|
|
}
|
2011-11-28 21:47:50 +00:00
|
|
|
gst_bin_add (GST_BIN_CAST (vfbin), videoscale);
|
2011-11-28 21:43:40 +00:00
|
|
|
|
|
|
|
gst_element_link_pads_full (csp, "src", videoscale, "sink",
|
|
|
|
GST_PAD_LINK_CHECK_NOTHING);
|
2011-08-23 14:19:51 +00:00
|
|
|
|
|
|
|
vfbin->elements_created = TRUE;
|
|
|
|
GST_DEBUG_OBJECT (vfbin, "Elements succesfully created and linked");
|
|
|
|
|
|
|
|
updated_converters = TRUE;
|
|
|
|
}
|
|
|
|
/* otherwise, just leave it as is */
|
|
|
|
|
|
|
|
/* if sink was replaced -> link it to the internal converters */
|
|
|
|
if (newsink && !vfbin->disable_converters) {
|
|
|
|
gboolean unref = FALSE;
|
|
|
|
if (!videoscale) {
|
2010-12-12 22:33:24 +00:00
|
|
|
videoscale = gst_bin_get_by_name (GST_BIN_CAST (vfbin),
|
2011-08-23 14:19:51 +00:00
|
|
|
"vfbin-videscale");
|
|
|
|
unref = TRUE;
|
|
|
|
}
|
2010-12-12 22:33:24 +00:00
|
|
|
|
2011-11-28 21:43:40 +00:00
|
|
|
if (!gst_element_link_pads_full (videoscale, "src", vfbin->video_sink,
|
|
|
|
"sink", GST_PAD_LINK_CHECK_CAPS)) {
|
2011-03-23 21:41:02 +00:00
|
|
|
GST_ELEMENT_ERROR (vfbin, CORE, NEGOTIATION, (NULL),
|
|
|
|
("linking videoscale and viewfindersink failed"));
|
2010-12-12 22:33:24 +00:00
|
|
|
}
|
2011-03-23 21:41:02 +00:00
|
|
|
|
2011-08-23 14:19:51 +00:00
|
|
|
if (unref)
|
|
|
|
gst_object_unref (videoscale);
|
2011-03-23 21:41:02 +00:00
|
|
|
videoscale = NULL;
|
2010-12-12 22:33:24 +00:00
|
|
|
}
|
2010-11-24 20:28:57 +00:00
|
|
|
|
2011-08-23 14:19:51 +00:00
|
|
|
/* Check if we need a new ghostpad target */
|
|
|
|
if (updated_converters || (newsink && vfbin->disable_converters)) {
|
|
|
|
if (vfbin->disable_converters) {
|
|
|
|
firstpad = gst_element_get_static_pad (vfbin->video_sink, "sink");
|
|
|
|
} else {
|
|
|
|
/* csp should always exist at this point */
|
|
|
|
firstpad = gst_element_get_static_pad (csp, "sink");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need to change the ghostpad target if firstpad is set */
|
|
|
|
if (firstpad) {
|
|
|
|
if (!gst_ghost_pad_set_target (GST_GHOST_PAD (vfbin->ghostpad), firstpad))
|
|
|
|
goto error;
|
|
|
|
gst_object_unref (firstpad);
|
|
|
|
firstpad = NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-24 20:28:57 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2011-04-28 19:05:53 +00:00
|
|
|
missing_element:
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (vfbin),
|
|
|
|
gst_missing_element_message_new (GST_ELEMENT_CAST (vfbin),
|
|
|
|
missing_element_name));
|
|
|
|
GST_ELEMENT_ERROR (vfbin, CORE, MISSING_PLUGIN,
|
|
|
|
(_("Missing element '%s' - check your GStreamer installation."),
|
|
|
|
missing_element_name), (NULL));
|
|
|
|
goto error;
|
|
|
|
|
2010-11-24 20:28:57 +00:00
|
|
|
error:
|
2010-12-02 04:33:19 +00:00
|
|
|
GST_WARNING_OBJECT (vfbin, "Creating internal elements failed");
|
2011-08-23 14:19:51 +00:00
|
|
|
if (firstpad)
|
|
|
|
gst_object_unref (firstpad);
|
2010-11-24 20:28:57 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_viewfinder_bin_change_state (GstElement * element, GstStateChange trans)
|
|
|
|
{
|
|
|
|
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
|
|
|
|
GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (element);
|
|
|
|
|
|
|
|
switch (trans) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
if (!gst_viewfinder_bin_create_elements (vfbin)) {
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
|
|
|
|
|
|
|
|
switch (trans) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-10 15:08:48 +00:00
|
|
|
static void
|
|
|
|
gst_viewfinder_bin_set_video_sink (GstViewfinderBin * vfbin, GstElement * sink)
|
|
|
|
{
|
|
|
|
GST_INFO_OBJECT (vfbin, "Setting video sink to %" GST_PTR_FORMAT, sink);
|
|
|
|
|
2010-12-16 13:51:38 +00:00
|
|
|
if (vfbin->user_video_sink != sink) {
|
|
|
|
if (vfbin->user_video_sink) {
|
|
|
|
gst_object_unref (vfbin->user_video_sink);
|
2010-12-12 22:33:24 +00:00
|
|
|
}
|
2010-12-16 13:51:38 +00:00
|
|
|
vfbin->user_video_sink = sink;
|
|
|
|
if (sink)
|
|
|
|
gst_object_ref (sink);
|
2010-12-10 15:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_viewfinder_bin_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_VIDEO_SINK:
|
|
|
|
gst_viewfinder_bin_set_video_sink (vfbin, g_value_get_object (value));
|
|
|
|
break;
|
2011-08-23 14:19:51 +00:00
|
|
|
case PROP_DISABLE_CONVERTERS:
|
|
|
|
vfbin->disable_converters = g_value_get_boolean (value);
|
|
|
|
break;
|
2010-12-10 15:08:48 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_viewfinder_bin_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_VIDEO_SINK:
|
2010-12-12 22:33:24 +00:00
|
|
|
g_value_set_object (value, vfbin->video_sink);
|
2010-12-10 15:08:48 +00:00
|
|
|
break;
|
2011-08-23 14:19:51 +00:00
|
|
|
case PROP_DISABLE_CONVERTERS:
|
|
|
|
g_value_set_boolean (value, vfbin->disable_converters);
|
|
|
|
break;
|
2010-12-10 15:08:48 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 23:15:38 +00:00
|
|
|
gboolean
|
|
|
|
gst_viewfinder_bin_plugin_init (GstPlugin * plugin)
|
2010-11-24 20:28:57 +00:00
|
|
|
{
|
2010-12-02 04:33:19 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_viewfinder_bin_debug, "viewfinderbin", 0,
|
|
|
|
"ViewFinderBin");
|
2010-11-24 23:15:38 +00:00
|
|
|
return gst_element_register (plugin, "viewfinderbin", GST_RANK_NONE,
|
2010-11-24 20:28:57 +00:00
|
|
|
gst_viewfinder_bin_get_type ());
|
|
|
|
}
|