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 /* push current event back into the queue */
53 g_queue_push_tail (&effects, gst_object_ref (cur_effect));
54 /* take next effect from the queue */
55 next = g_queue_pop_head (&effects);
57 GST_DEBUG_OBJECT (pad, "no more effects");
58 g_main_loop_quit (loop);
59 return GST_PAD_PROBE_DROP;
62 g_print ("Switching from '%s' to '%s'..\n", GST_OBJECT_NAME (cur_effect),
63 GST_OBJECT_NAME (next));
65 gst_element_set_state (cur_effect, GST_STATE_NULL);
67 /* remove unlinks automatically */
68 GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, cur_effect);
69 gst_bin_remove (GST_BIN (pipeline), cur_effect);
71 GST_DEBUG_OBJECT (pipeline, "adding %" GST_PTR_FORMAT, next);
72 gst_bin_add (GST_BIN (pipeline), next);
74 GST_DEBUG_OBJECT (pipeline, "linking..");
75 gst_element_link_many (conv_before, next, conv_after, NULL);
77 gst_element_set_state (next, GST_STATE_PLAYING);
80 GST_DEBUG_OBJECT (pipeline, "done");
82 return GST_PAD_PROBE_DROP;
85 static GstPadProbeReturn
86 pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
88 GstPad *srcpad, *sinkpad;
90 GST_DEBUG_OBJECT (pad, "pad is blocked now");
92 /* remove the probe first */
93 gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
95 /* install new probe for EOS */
96 srcpad = gst_element_get_static_pad (cur_effect, "src");
97 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK |
98 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, event_probe_cb, user_data, NULL);
99 gst_object_unref (srcpad);
101 /* push EOS into the element, the probe will be fired when the
102 * EOS leaves the effect and it has thus drained all of its data */
103 sinkpad = gst_element_get_static_pad (cur_effect, "sink");
104 gst_pad_send_event (sinkpad, gst_event_new_eos ());
105 gst_object_unref (sinkpad);
107 return GST_PAD_PROBE_OK;
111 timeout_cb (gpointer user_data)
113 gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
114 pad_probe_cb, user_data, NULL);
120 bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
122 GMainLoop *loop = user_data;
124 switch (GST_MESSAGE_TYPE (msg)) {
125 case GST_MESSAGE_ERROR:{
129 gst_message_parse_error (msg, &err, &dbg);
130 gst_object_default_error (msg->src, err, dbg);
131 g_clear_error (&err);
133 g_main_loop_quit (loop);
143 main (int argc, char **argv)
145 GOptionEntry options[] = {
146 {"effects", 'e', 0, G_OPTION_ARG_STRING, &opt_effects,
147 "Effects to use (comma-separated list of element names)", NULL},
153 GstElement *src, *q1, *q2, *effect, *filter1, *filter2, *sink;
154 gchar **effect_names, **e;
156 ctx = g_option_context_new ("");
157 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
158 g_option_context_add_group (ctx, gst_init_get_option_group ());
159 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
160 g_print ("Error initializing: %s\n", err->message);
161 g_option_context_free (ctx);
162 g_clear_error (&err);
165 g_option_context_free (ctx);
167 GST_FIXME ("Multiple things to check/fix, see source code");
169 if (opt_effects != NULL)
170 effect_names = g_strsplit (opt_effects, ",", -1);
172 effect_names = g_strsplit (DEFAULT_EFFECTS, ",", -1);
174 for (e = effect_names; e != NULL && *e != NULL; ++e) {
177 el = gst_element_factory_make (*e, NULL);
179 g_print ("Adding effect '%s'\n", *e);
180 g_queue_push_tail (&effects, el);
184 pipeline = gst_pipeline_new ("pipeline");
186 src = gst_element_factory_make ("videotestsrc", NULL);
187 g_object_set (src, "is-live", TRUE, NULL);
189 filter1 = gst_element_factory_make ("capsfilter", NULL);
190 gst_util_set_object_arg (G_OBJECT (filter1), "caps",
191 "video/x-raw, width=320, height=240, "
192 "format={ I420, YV12, YUY2, UYVY, AYUV, Y41B, Y42B, "
193 "YVYU, Y444, v210, v216, NV12, NV21, UYVP, A420, YUV9, YVU9, IYU1 }");
195 q1 = gst_element_factory_make ("queue", NULL);
197 blockpad = gst_element_get_static_pad (q1, "src");
199 conv_before = gst_element_factory_make ("videoconvert", NULL);
201 effect = g_queue_pop_head (&effects);
204 conv_after = gst_element_factory_make ("videoconvert", NULL);
206 q2 = gst_element_factory_make ("queue", NULL);
208 filter2 = gst_element_factory_make ("capsfilter", NULL);
209 gst_util_set_object_arg (G_OBJECT (filter2), "caps",
210 "video/x-raw, width=320, height=240, "
211 "format={ RGBx, BGRx, xRGB, xBGR, RGBA, BGRA, ARGB, ABGR, RGB, BGR }");
213 sink = gst_element_factory_make ("ximagesink", NULL);
215 gst_bin_add_many (GST_BIN (pipeline), src, filter1, q1, conv_before, effect,
216 conv_after, q2, sink, NULL);
218 gst_element_link_many (src, filter1, q1, conv_before, effect, conv_after,
221 gst_element_set_state (pipeline, GST_STATE_PLAYING);
223 loop = g_main_loop_new (NULL, FALSE);
225 gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, loop);
227 g_timeout_add_seconds (1, timeout_cb, loop);
229 g_main_loop_run (loop);
231 gst_element_set_state (pipeline, GST_STATE_NULL);
232 gst_object_unref (pipeline);