1 /* GStreamer dynamic effect change test app
2 * Copyright (C) 2012 Tim-Philipp Müller <tim centricular net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
19 * Based on python script by Thiago Sousa Santos
28 static gchar *opt_effects = NULL;
30 #define DEFAULT_EFFECTS "identity,exclusion,navigationtest," \
31 "agingtv,videoflip,vertigotv,gaussianblur,shagadelictv,edgetv"
33 static GstPad *blockpad;
34 static GstElement *conv_before;
35 static GstElement *conv_after;
36 static GstElement *cur_effect;
37 static GstElement *pipeline;
39 static GQueue effects = G_QUEUE_INIT;
41 static GstPadProbeReturn
42 event_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
44 GMainLoop *loop = user_data;
47 if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_DATA (info)) != GST_EVENT_EOS)
48 return GST_PAD_PROBE_OK;
50 gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
52 /* take next effect from the queue */
53 next = g_queue_pop_head (&effects);
55 GST_DEBUG_OBJECT (pad, "no more effects");
56 g_main_loop_quit (loop);
57 return GST_PAD_PROBE_DROP;
60 g_print ("Switching from '%s' to '%s'..\n", GST_OBJECT_NAME (cur_effect),
61 GST_OBJECT_NAME (next));
63 gst_element_set_state (cur_effect, GST_STATE_NULL);
65 /* remove unlinks automatically */
66 GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, cur_effect);
67 gst_bin_remove (GST_BIN (pipeline), cur_effect);
69 /* push current effect back into the queue */
70 g_queue_push_tail (&effects, g_steal_pointer (&cur_effect));
72 /* add, link and start the new effect */
73 GST_DEBUG_OBJECT (pipeline, "adding %" GST_PTR_FORMAT, next);
74 gst_bin_add (GST_BIN (pipeline), next);
76 GST_DEBUG_OBJECT (pipeline, "linking..");
77 gst_element_link_many (conv_before, next, conv_after, NULL);
79 gst_element_set_state (next, GST_STATE_PLAYING);
82 GST_DEBUG_OBJECT (pipeline, "done");
84 return GST_PAD_PROBE_DROP;
87 static GstPadProbeReturn
88 pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
90 GstPad *srcpad, *sinkpad;
92 GST_DEBUG_OBJECT (pad, "pad is blocked now");
94 /* remove the probe first */
95 gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
97 /* install new probe for EOS */
98 srcpad = gst_element_get_static_pad (cur_effect, "src");
99 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK |
100 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, event_probe_cb, user_data, NULL);
101 gst_object_unref (srcpad);
103 /* push EOS into the element, the probe will be fired when the
104 * EOS leaves the effect and it has thus drained all of its data */
105 sinkpad = gst_element_get_static_pad (cur_effect, "sink");
106 gst_pad_send_event (sinkpad, gst_event_new_eos ());
107 gst_object_unref (sinkpad);
109 return GST_PAD_PROBE_OK;
113 timeout_cb (gpointer user_data)
115 gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
116 pad_probe_cb, user_data, NULL);
122 bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
124 GMainLoop *loop = user_data;
126 switch (GST_MESSAGE_TYPE (msg)) {
127 case GST_MESSAGE_ERROR:{
131 gst_message_parse_error (msg, &err, &dbg);
132 gst_object_default_error (msg->src, err, dbg);
133 g_clear_error (&err);
135 g_main_loop_quit (loop);
145 main (int argc, char **argv)
147 GOptionEntry options[] = {
148 {"effects", 'e', 0, G_OPTION_ARG_STRING, &opt_effects,
149 "Effects to use (comma-separated list of element names)", NULL},
155 GstElement *src, *q1, *q2, *effect, *filter, *sink;
156 gchar **effect_names, **e;
158 ctx = g_option_context_new ("");
159 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
160 g_option_context_add_group (ctx, gst_init_get_option_group ());
161 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
162 g_error ("Error initializing: %s\n", err->message);
165 g_option_context_free (ctx);
167 if (opt_effects != NULL)
168 effect_names = g_strsplit (opt_effects, ",", -1);
170 effect_names = g_strsplit (DEFAULT_EFFECTS, ",", -1);
172 for (e = effect_names; e != NULL && *e != NULL; ++e) {
175 el = gst_element_factory_make (*e, NULL);
177 g_print ("Adding effect '%s'\n", *e);
178 g_queue_push_tail (&effects, gst_object_ref_sink (el));
182 pipeline = gst_pipeline_new ("pipeline");
184 src = gst_element_factory_make ("videotestsrc", NULL);
185 g_object_set (src, "is-live", TRUE, NULL);
187 filter = gst_element_factory_make ("capsfilter", NULL);
188 gst_util_set_object_arg (G_OBJECT (filter), "caps",
189 "video/x-raw, width=320, height=240, "
190 "format={ I420, YV12, YUY2, UYVY, AYUV, Y41B, Y42B, "
191 "YVYU, Y444, v210, v216, NV12, NV21, UYVP, A420, YUV9, YVU9, IYU1 }");
193 q1 = gst_element_factory_make ("queue", NULL);
195 blockpad = gst_element_get_static_pad (q1, "src");
197 conv_before = gst_element_factory_make ("videoconvert", NULL);
199 effect = g_queue_pop_head (&effects);
202 conv_after = gst_element_factory_make ("videoconvert", NULL);
204 q2 = gst_element_factory_make ("queue", NULL);
206 sink = gst_element_factory_make ("autovideosink", NULL);
208 gst_bin_add_many (GST_BIN (pipeline), src, filter, q1, conv_before, effect,
209 conv_after, q2, sink, NULL);
211 gst_element_link_many (src, filter, q1, conv_before, effect, conv_after,
214 if (gst_element_set_state (pipeline,
215 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
216 g_error ("Error starting pipeline");
220 loop = g_main_loop_new (NULL, FALSE);
222 gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, loop);
224 g_timeout_add_seconds (1, timeout_cb, loop);
226 g_main_loop_run (loop);
228 gst_element_set_state (pipeline, GST_STATE_NULL);
230 gst_object_unref (blockpad);
231 gst_bus_remove_watch (GST_ELEMENT_BUS (pipeline));
232 gst_object_unref (pipeline);
233 g_main_loop_unref (loop);
235 g_queue_clear_full (&effects, (GDestroyNotify) gst_object_unref);
236 gst_object_unref (cur_effect);
237 g_strfreev (effect_names);