tests: interactive: test-effect-switch: use autovideosink
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / tests / interactive / test-effect-switch.c
1 /* GStreamer dynamic effect change test app
2  * Copyright (C) 2012 Tim-Philipp Müller <tim centricular net>
3
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.
8  *
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.
13  *
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.
18  *
19  * Based on python script by Thiago Sousa Santos
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst.h>
27
28 static gchar *opt_effects = NULL;
29
30 #define DEFAULT_EFFECTS "identity,exclusion,navigationtest," \
31     "agingtv,videoflip,vertigotv,gaussianblur,shagadelictv,edgetv"
32
33 static GstPad *blockpad;
34 static GstElement *conv_before;
35 static GstElement *conv_after;
36 static GstElement *cur_effect;
37 static GstElement *pipeline;
38
39 static GQueue effects = G_QUEUE_INIT;
40
41 static GstPadProbeReturn
42 event_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
43 {
44   GMainLoop *loop = user_data;
45   GstElement *next;
46
47   if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_DATA (info)) != GST_EVENT_EOS)
48     return GST_PAD_PROBE_OK;
49
50   gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
51
52   /* take next effect from the queue */
53   next = g_queue_pop_head (&effects);
54   if (next == NULL) {
55     GST_DEBUG_OBJECT (pad, "no more effects");
56     g_main_loop_quit (loop);
57     return GST_PAD_PROBE_DROP;
58   }
59
60   g_print ("Switching from '%s' to '%s'..\n", GST_OBJECT_NAME (cur_effect),
61       GST_OBJECT_NAME (next));
62
63   gst_element_set_state (cur_effect, GST_STATE_NULL);
64
65   /* remove unlinks automatically */
66   GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, cur_effect);
67   gst_bin_remove (GST_BIN (pipeline), cur_effect);
68
69   /* push current effect back into the queue */
70   g_queue_push_tail (&effects, g_steal_pointer (&cur_effect));
71
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);
75
76   GST_DEBUG_OBJECT (pipeline, "linking..");
77   gst_element_link_many (conv_before, next, conv_after, NULL);
78
79   gst_element_set_state (next, GST_STATE_PLAYING);
80
81   cur_effect = next;
82   GST_DEBUG_OBJECT (pipeline, "done");
83
84   return GST_PAD_PROBE_DROP;
85 }
86
87 static GstPadProbeReturn
88 pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
89 {
90   GstPad *srcpad, *sinkpad;
91
92   GST_DEBUG_OBJECT (pad, "pad is blocked now");
93
94   /* remove the probe first */
95   gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
96
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);
102
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);
108
109   return GST_PAD_PROBE_OK;
110 }
111
112 static gboolean
113 timeout_cb (gpointer user_data)
114 {
115   gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
116       pad_probe_cb, user_data, NULL);
117
118   return TRUE;
119 }
120
121 static gboolean
122 bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
123 {
124   GMainLoop *loop = user_data;
125
126   switch (GST_MESSAGE_TYPE (msg)) {
127     case GST_MESSAGE_ERROR:{
128       GError *err = NULL;
129       gchar *dbg;
130
131       gst_message_parse_error (msg, &err, &dbg);
132       gst_object_default_error (msg->src, err, dbg);
133       g_clear_error (&err);
134       g_free (dbg);
135       g_main_loop_quit (loop);
136       break;
137     }
138     default:
139       break;
140   }
141   return TRUE;
142 }
143
144 int
145 main (int argc, char **argv)
146 {
147   GOptionEntry options[] = {
148     {"effects", 'e', 0, G_OPTION_ARG_STRING, &opt_effects,
149         "Effects to use (comma-separated list of element names)", NULL},
150     {NULL}
151   };
152   GOptionContext *ctx;
153   GError *err = NULL;
154   GMainLoop *loop;
155   GstElement *src, *q1, *q2, *effect, *filter, *sink;
156   gchar **effect_names, **e;
157
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);
163     return 1;
164   }
165   g_option_context_free (ctx);
166
167   if (opt_effects != NULL)
168     effect_names = g_strsplit (opt_effects, ",", -1);
169   else
170     effect_names = g_strsplit (DEFAULT_EFFECTS, ",", -1);
171
172   for (e = effect_names; e != NULL && *e != NULL; ++e) {
173     GstElement *el;
174
175     el = gst_element_factory_make (*e, NULL);
176     if (el) {
177       g_print ("Adding effect '%s'\n", *e);
178       g_queue_push_tail (&effects, gst_object_ref_sink (el));
179     }
180   }
181
182   pipeline = gst_pipeline_new ("pipeline");
183
184   src = gst_element_factory_make ("videotestsrc", NULL);
185   g_object_set (src, "is-live", TRUE, NULL);
186
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 }");
192
193   q1 = gst_element_factory_make ("queue", NULL);
194
195   blockpad = gst_element_get_static_pad (q1, "src");
196
197   conv_before = gst_element_factory_make ("videoconvert", NULL);
198
199   effect = g_queue_pop_head (&effects);
200   cur_effect = effect;
201
202   conv_after = gst_element_factory_make ("videoconvert", NULL);
203
204   q2 = gst_element_factory_make ("queue", NULL);
205
206   sink = gst_element_factory_make ("autovideosink", NULL);
207
208   gst_bin_add_many (GST_BIN (pipeline), src, filter, q1, conv_before, effect,
209       conv_after, q2, sink, NULL);
210
211   gst_element_link_many (src, filter, q1, conv_before, effect, conv_after,
212       q2, sink, NULL);
213
214   if (gst_element_set_state (pipeline,
215           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
216     g_error ("Error starting pipeline");
217     return 1;
218   }
219
220   loop = g_main_loop_new (NULL, FALSE);
221
222   gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, loop);
223
224   g_timeout_add_seconds (1, timeout_cb, loop);
225
226   g_main_loop_run (loop);
227
228   gst_element_set_state (pipeline, GST_STATE_NULL);
229
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);
234
235   g_queue_clear_full (&effects, (GDestroyNotify) gst_object_unref);
236   gst_object_unref (cur_effect);
237   g_strfreev (effect_names);
238
239   return 0;
240 }