2012-06-30 15:50:10 +00:00
|
|
|
/* GStreamer dynamic effect change test app
|
|
|
|
* Copyright (C) 2012 Tim-Philipp Müller <tim centricular net>
|
|
|
|
|
|
|
|
* 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.
|
2012-06-30 15:50:10 +00:00
|
|
|
*
|
|
|
|
* Based on python script by Thiago Sousa Santos
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
|
|
|
static gchar *opt_effects = NULL;
|
|
|
|
|
|
|
|
#define DEFAULT_EFFECTS "identity,exclusion,navigationtest," \
|
2012-10-05 08:59:30 +00:00
|
|
|
"agingtv,videoflip,vertigotv,gaussianblur,shagadelictv,edgetv"
|
2012-06-30 15:50:10 +00:00
|
|
|
|
2012-10-05 08:59:30 +00:00
|
|
|
static GstPad *blockpad;
|
2012-06-30 15:50:10 +00:00
|
|
|
static GstElement *conv_before;
|
|
|
|
static GstElement *conv_after;
|
|
|
|
static GstElement *cur_effect;
|
|
|
|
static GstElement *pipeline;
|
|
|
|
|
|
|
|
static GQueue effects = G_QUEUE_INIT;
|
|
|
|
|
|
|
|
static GstPadProbeReturn
|
2012-10-05 08:59:30 +00:00
|
|
|
event_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
|
2012-06-30 15:50:10 +00:00
|
|
|
{
|
|
|
|
GMainLoop *loop = user_data;
|
|
|
|
GstElement *next;
|
|
|
|
|
2012-10-05 08:59:30 +00:00
|
|
|
if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_DATA (info)) != GST_EVENT_EOS)
|
|
|
|
return GST_PAD_PROBE_OK;
|
2012-06-30 15:50:10 +00:00
|
|
|
|
2012-10-05 08:59:30 +00:00
|
|
|
gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
|
2012-06-30 15:50:10 +00:00
|
|
|
|
2012-10-05 08:59:30 +00:00
|
|
|
/* take next effect from the queue */
|
|
|
|
next = g_queue_pop_head (&effects);
|
2012-06-30 15:50:10 +00:00
|
|
|
if (next == NULL) {
|
2012-10-05 08:59:30 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "no more effects");
|
2012-06-30 15:50:10 +00:00
|
|
|
g_main_loop_quit (loop);
|
2012-10-05 08:59:30 +00:00
|
|
|
return GST_PAD_PROBE_DROP;
|
2012-06-30 15:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_print ("Switching from '%s' to '%s'..\n", GST_OBJECT_NAME (cur_effect),
|
|
|
|
GST_OBJECT_NAME (next));
|
|
|
|
|
|
|
|
gst_element_set_state (cur_effect, GST_STATE_NULL);
|
|
|
|
|
|
|
|
/* remove unlinks automatically */
|
2012-10-05 08:59:30 +00:00
|
|
|
GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, cur_effect);
|
2012-06-30 15:50:10 +00:00
|
|
|
gst_bin_remove (GST_BIN (pipeline), cur_effect);
|
2012-10-05 08:59:30 +00:00
|
|
|
|
2022-09-29 11:36:38 +00:00
|
|
|
/* push current effect back into the queue */
|
|
|
|
g_queue_push_tail (&effects, g_steal_pointer (&cur_effect));
|
|
|
|
|
|
|
|
/* add, link and start the new effect */
|
2012-10-05 08:59:30 +00:00
|
|
|
GST_DEBUG_OBJECT (pipeline, "adding %" GST_PTR_FORMAT, next);
|
2012-06-30 15:50:10 +00:00
|
|
|
gst_bin_add (GST_BIN (pipeline), next);
|
2012-10-05 08:59:30 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pipeline, "linking..");
|
2012-06-30 15:50:10 +00:00
|
|
|
gst_element_link_many (conv_before, next, conv_after, NULL);
|
2012-10-05 08:59:30 +00:00
|
|
|
|
2012-06-30 15:50:10 +00:00
|
|
|
gst_element_set_state (next, GST_STATE_PLAYING);
|
2012-10-05 08:59:30 +00:00
|
|
|
|
2012-06-30 15:50:10 +00:00
|
|
|
cur_effect = next;
|
2012-10-05 08:59:30 +00:00
|
|
|
GST_DEBUG_OBJECT (pipeline, "done");
|
|
|
|
|
|
|
|
return GST_PAD_PROBE_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstPadProbeReturn
|
|
|
|
pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstPad *srcpad, *sinkpad;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "pad is blocked now");
|
|
|
|
|
|
|
|
/* remove the probe first */
|
|
|
|
gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
|
|
|
|
|
|
|
|
/* install new probe for EOS */
|
|
|
|
srcpad = gst_element_get_static_pad (cur_effect, "src");
|
|
|
|
gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK |
|
|
|
|
GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, event_probe_cb, user_data, NULL);
|
|
|
|
gst_object_unref (srcpad);
|
|
|
|
|
|
|
|
/* push EOS into the element, the probe will be fired when the
|
|
|
|
* EOS leaves the effect and it has thus drained all of its data */
|
|
|
|
sinkpad = gst_element_get_static_pad (cur_effect, "sink");
|
|
|
|
gst_pad_send_event (sinkpad, gst_event_new_eos ());
|
|
|
|
gst_object_unref (sinkpad);
|
|
|
|
|
|
|
|
return GST_PAD_PROBE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
timeout_cb (gpointer user_data)
|
|
|
|
{
|
2013-12-31 08:37:36 +00:00
|
|
|
gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
|
2012-10-05 08:59:30 +00:00
|
|
|
pad_probe_cb, user_data, NULL);
|
2012-06-30 15:50:10 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
|
|
|
|
{
|
|
|
|
GMainLoop *loop = user_data;
|
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE (msg)) {
|
|
|
|
case GST_MESSAGE_ERROR:{
|
|
|
|
GError *err = NULL;
|
|
|
|
gchar *dbg;
|
|
|
|
|
|
|
|
gst_message_parse_error (msg, &err, &dbg);
|
|
|
|
gst_object_default_error (msg->src, err, dbg);
|
2015-08-20 06:59:15 +00:00
|
|
|
g_clear_error (&err);
|
2012-06-30 15:50:10 +00:00
|
|
|
g_free (dbg);
|
|
|
|
g_main_loop_quit (loop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
GOptionEntry options[] = {
|
|
|
|
{"effects", 'e', 0, G_OPTION_ARG_STRING, &opt_effects,
|
|
|
|
"Effects to use (comma-separated list of element names)", NULL},
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
GOptionContext *ctx;
|
|
|
|
GError *err = NULL;
|
|
|
|
GMainLoop *loop;
|
2022-09-29 11:36:38 +00:00
|
|
|
GstElement *src, *q1, *q2, *effect, *filter, *sink;
|
2012-06-30 15:50:10 +00:00
|
|
|
gchar **effect_names, **e;
|
|
|
|
|
|
|
|
ctx = g_option_context_new ("");
|
|
|
|
g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
|
|
|
|
g_option_context_add_group (ctx, gst_init_get_option_group ());
|
|
|
|
if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
|
2022-09-29 11:36:38 +00:00
|
|
|
g_error ("Error initializing: %s\n", err->message);
|
2012-06-30 15:50:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
g_option_context_free (ctx);
|
|
|
|
|
|
|
|
if (opt_effects != NULL)
|
|
|
|
effect_names = g_strsplit (opt_effects, ",", -1);
|
|
|
|
else
|
|
|
|
effect_names = g_strsplit (DEFAULT_EFFECTS, ",", -1);
|
|
|
|
|
|
|
|
for (e = effect_names; e != NULL && *e != NULL; ++e) {
|
2012-10-05 08:59:30 +00:00
|
|
|
GstElement *el;
|
|
|
|
|
|
|
|
el = gst_element_factory_make (*e, NULL);
|
|
|
|
if (el) {
|
|
|
|
g_print ("Adding effect '%s'\n", *e);
|
2022-09-29 11:36:38 +00:00
|
|
|
g_queue_push_tail (&effects, gst_object_ref_sink (el));
|
2012-10-05 08:59:30 +00:00
|
|
|
}
|
2012-06-30 15:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pipeline = gst_pipeline_new ("pipeline");
|
|
|
|
|
|
|
|
src = gst_element_factory_make ("videotestsrc", NULL);
|
|
|
|
g_object_set (src, "is-live", TRUE, NULL);
|
|
|
|
|
2022-09-29 11:36:38 +00:00
|
|
|
filter = gst_element_factory_make ("capsfilter", NULL);
|
|
|
|
gst_util_set_object_arg (G_OBJECT (filter), "caps",
|
2013-12-30 18:40:29 +00:00
|
|
|
"video/x-raw, width=320, height=240, "
|
2012-06-30 15:50:10 +00:00
|
|
|
"format={ I420, YV12, YUY2, UYVY, AYUV, Y41B, Y42B, "
|
|
|
|
"YVYU, Y444, v210, v216, NV12, NV21, UYVP, A420, YUV9, YVU9, IYU1 }");
|
|
|
|
|
|
|
|
q1 = gst_element_factory_make ("queue", NULL);
|
|
|
|
|
2012-10-05 08:59:30 +00:00
|
|
|
blockpad = gst_element_get_static_pad (q1, "src");
|
|
|
|
|
2012-06-30 15:50:10 +00:00
|
|
|
conv_before = gst_element_factory_make ("videoconvert", NULL);
|
|
|
|
|
|
|
|
effect = g_queue_pop_head (&effects);
|
|
|
|
cur_effect = effect;
|
|
|
|
|
|
|
|
conv_after = gst_element_factory_make ("videoconvert", NULL);
|
|
|
|
|
|
|
|
q2 = gst_element_factory_make ("queue", NULL);
|
|
|
|
|
2023-01-10 13:06:37 +00:00
|
|
|
sink = gst_element_factory_make ("autovideosink", NULL);
|
2012-06-30 15:50:10 +00:00
|
|
|
|
2022-09-29 11:36:38 +00:00
|
|
|
gst_bin_add_many (GST_BIN (pipeline), src, filter, q1, conv_before, effect,
|
2012-06-30 15:50:10 +00:00
|
|
|
conv_after, q2, sink, NULL);
|
|
|
|
|
2022-09-29 11:36:38 +00:00
|
|
|
gst_element_link_many (src, filter, q1, conv_before, effect, conv_after,
|
2012-06-30 15:50:10 +00:00
|
|
|
q2, sink, NULL);
|
|
|
|
|
2022-09-29 11:36:38 +00:00
|
|
|
if (gst_element_set_state (pipeline,
|
|
|
|
GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
|
|
|
|
g_error ("Error starting pipeline");
|
|
|
|
return 1;
|
|
|
|
}
|
2012-06-30 15:50:10 +00:00
|
|
|
|
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
|
|
|
|
gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, loop);
|
|
|
|
|
|
|
|
g_timeout_add_seconds (1, timeout_cb, loop);
|
|
|
|
|
|
|
|
g_main_loop_run (loop);
|
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
2022-09-29 11:36:38 +00:00
|
|
|
|
|
|
|
gst_object_unref (blockpad);
|
|
|
|
gst_bus_remove_watch (GST_ELEMENT_BUS (pipeline));
|
2012-06-30 15:50:10 +00:00
|
|
|
gst_object_unref (pipeline);
|
2022-09-29 11:36:38 +00:00
|
|
|
g_main_loop_unref (loop);
|
|
|
|
|
|
|
|
g_queue_clear_full (&effects, (GDestroyNotify) gst_object_unref);
|
|
|
|
gst_object_unref (cur_effect);
|
|
|
|
g_strfreev (effect_names);
|
2012-06-30 15:50:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|