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
event_probe_cb(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)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   /* 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);
56   if (next == NULL) {
57     GST_DEBUG_OBJECT (pad, "no more effects");
58     g_main_loop_quit (loop);
59     return GST_PAD_PROBE_DROP;
60   }
61 
62   g_print ("Switching from '%s' to '%s'..\n", GST_OBJECT_NAME (cur_effect),
63       GST_OBJECT_NAME (next));
64 
65   gst_element_set_state (cur_effect, GST_STATE_NULL);
66 
67   /* remove unlinks automatically */
68   GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, cur_effect);
69   gst_bin_remove (GST_BIN (pipeline), cur_effect);
70 
71   GST_DEBUG_OBJECT (pipeline, "adding   %" GST_PTR_FORMAT, next);
72   gst_bin_add (GST_BIN (pipeline), next);
73 
74   GST_DEBUG_OBJECT (pipeline, "linking..");
75   gst_element_link_many (conv_before, next, conv_after, NULL);
76 
77   gst_element_set_state (next, GST_STATE_PLAYING);
78 
79   cur_effect = next;
80   GST_DEBUG_OBJECT (pipeline, "done");
81 
82   return GST_PAD_PROBE_DROP;
83 }
84 
85 static GstPadProbeReturn
pad_probe_cb(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)86 pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
87 {
88   GstPad *srcpad, *sinkpad;
89 
90   GST_DEBUG_OBJECT (pad, "pad is blocked now");
91 
92   /* remove the probe first */
93   gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
94 
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);
100 
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);
106 
107   return GST_PAD_PROBE_OK;
108 }
109 
110 static gboolean
timeout_cb(gpointer user_data)111 timeout_cb (gpointer user_data)
112 {
113   gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
114       pad_probe_cb, user_data, NULL);
115 
116   return TRUE;
117 }
118 
119 static gboolean
bus_cb(GstBus * bus,GstMessage * msg,gpointer user_data)120 bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
121 {
122   GMainLoop *loop = user_data;
123 
124   switch (GST_MESSAGE_TYPE (msg)) {
125     case GST_MESSAGE_ERROR:{
126       GError *err = NULL;
127       gchar *dbg;
128 
129       gst_message_parse_error (msg, &err, &dbg);
130       gst_object_default_error (msg->src, err, dbg);
131       g_clear_error (&err);
132       g_free (dbg);
133       g_main_loop_quit (loop);
134       break;
135     }
136     default:
137       break;
138   }
139   return TRUE;
140 }
141 
142 int
main(int argc,char ** argv)143 main (int argc, char **argv)
144 {
145   GOptionEntry options[] = {
146     {"effects", 'e', 0, G_OPTION_ARG_STRING, &opt_effects,
147         "Effects to use (comma-separated list of element names)", NULL},
148     {NULL}
149   };
150   GOptionContext *ctx;
151   GError *err = NULL;
152   GMainLoop *loop;
153   GstElement *src, *q1, *q2, *effect, *filter1, *filter2, *sink;
154   gchar **effect_names, **e;
155 
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);
163     return 1;
164   }
165   g_option_context_free (ctx);
166 
167   GST_FIXME ("Multiple things to check/fix, see source code");
168 
169   if (opt_effects != NULL)
170     effect_names = g_strsplit (opt_effects, ",", -1);
171   else
172     effect_names = g_strsplit (DEFAULT_EFFECTS, ",", -1);
173 
174   for (e = effect_names; e != NULL && *e != NULL; ++e) {
175     GstElement *el;
176 
177     el = gst_element_factory_make (*e, NULL);
178     if (el) {
179       g_print ("Adding effect '%s'\n", *e);
180       g_queue_push_tail (&effects, el);
181     }
182   }
183 
184   pipeline = gst_pipeline_new ("pipeline");
185 
186   src = gst_element_factory_make ("videotestsrc", NULL);
187   g_object_set (src, "is-live", TRUE, NULL);
188 
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 }");
194 
195   q1 = gst_element_factory_make ("queue", NULL);
196 
197   blockpad = gst_element_get_static_pad (q1, "src");
198 
199   conv_before = gst_element_factory_make ("videoconvert", NULL);
200 
201   effect = g_queue_pop_head (&effects);
202   cur_effect = effect;
203 
204   conv_after = gst_element_factory_make ("videoconvert", NULL);
205 
206   q2 = gst_element_factory_make ("queue", NULL);
207 
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 }");
212 
213   sink = gst_element_factory_make ("ximagesink", NULL);
214 
215   gst_bin_add_many (GST_BIN (pipeline), src, filter1, q1, conv_before, effect,
216       conv_after, q2, sink, NULL);
217 
218   gst_element_link_many (src, filter1, q1, conv_before, effect, conv_after,
219       q2, sink, NULL);
220 
221   gst_element_set_state (pipeline, GST_STATE_PLAYING);
222 
223   loop = g_main_loop_new (NULL, FALSE);
224 
225   gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, loop);
226 
227   g_timeout_add_seconds (1, timeout_cb, loop);
228 
229   g_main_loop_run (loop);
230 
231   gst_element_set_state (pipeline, GST_STATE_NULL);
232   gst_object_unref (pipeline);
233 
234   return 0;
235 }
236