Fix some more variable-set-but-not-used compiler warnings
[platform/upstream/gstreamer.git] / tests / examples / opencv / gstmotioncells_dynamic_test.c
1 /* GStreamer
2  * Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
3  *
4  *
5  *  gst_motioncells_dynamic_test(): a test tool what can to do dynamic change properties
6  *
7  *
8  *  This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #include <gst/gst.h>
23 #include <glib.h>
24 #include <glib/gprintf.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <locale.h>
29 #include <gst/interfaces/propertyprobe.h>
30 #include "gstmotioncells_dynamic_test.h"
31 #include "gst_element_print_properties.h"
32
33 const guint c2w = 21;           // column 2 width
34 const guint c3w = 19;           // column 3 width
35 const guint c4w = 23;           // column 4 width
36
37 void
38 setProperty (GstElement * mcells, char *property, char *prop_value, GType type,
39     GValue * value)
40 {
41
42   switch (type) {
43     case G_TYPE_STRING:
44     {
45       g_object_set (G_OBJECT (mcells), property, prop_value, NULL);
46       break;
47     }
48     case G_TYPE_BOOLEAN:
49     {
50       gboolean flag = (g_strcmp0 (prop_value, "true") == 0) ? TRUE : FALSE;
51       g_object_set (G_OBJECT (mcells), property, flag, NULL);
52       break;
53     }
54     case G_TYPE_ULONG:
55     {
56       unsigned long ulongval = strtoul (prop_value, NULL, 0);
57       g_object_set (G_OBJECT (mcells), property, ulongval, NULL);
58       break;
59     }
60     case G_TYPE_LONG:
61     {
62       long longval = atol (prop_value);
63       g_object_set (G_OBJECT (mcells), property, longval, NULL);
64       break;
65     }
66     case G_TYPE_UINT:
67     {
68       unsigned int uintval = atoi (prop_value);
69       g_object_set (G_OBJECT (mcells), property, uintval, NULL);
70       break;
71     }
72     case G_TYPE_INT:
73     {
74       int intval = atoi (prop_value);
75       g_object_set (G_OBJECT (mcells), property, intval, NULL);
76       break;
77     }
78     case G_TYPE_UINT64:
79     {
80       guint64 guint64val = atoi (prop_value);
81       g_object_set (G_OBJECT (mcells), property, guint64val, NULL);
82       break;
83     }
84     case G_TYPE_INT64:
85     {
86       gint64 gint64val = atoi (prop_value);
87       g_object_set (G_OBJECT (mcells), property, gint64val, NULL);
88       break;
89     }
90     case G_TYPE_FLOAT:
91     {
92       float floatval = atof (prop_value);
93       g_object_set (G_OBJECT (mcells), property, floatval, NULL);
94       break;
95     }
96     case G_TYPE_DOUBLE:
97     {
98       double doubleval = strtod (prop_value, NULL);
99       g_object_set (G_OBJECT (mcells), property, doubleval, NULL);
100       break;
101     }
102     default:
103       fprintf (stderr, "You gave me something wrong type of data !!! \n");
104       break;
105   }
106 }
107
108 // gst-launch v4l2src ! videoscale ! videorate ! capsfilter "caps=video/x-raw-yuv,width=320,height=240,framerate=10/1" ! ffmpegcolorspace ! motioncells ! ffmpegcolorspace ! xvimagesink
109 int
110 main (int argc, char *argv[])
111 {
112   GstElement *pipeline, *source, *videor, *videos, *decodebin, *capsf,
113       *colorsp0, *colorsp1, *mcells, *sink;
114   GstCaps *caps;
115   gchar property[20];
116   gchar prop_value[100];
117   GParamSpec **property_specs;
118   guint num_properties, i;
119   GValue value = { 0, };
120   gboolean found_property = FALSE;
121   int ret;
122
123   // Initialisation //
124   gst_init (&argc, &argv);
125   fprintf (stderr, "Usage: %s test or rtsp rtsp://your/cam/address\n", argv[0]);
126   // Create gstreamer elements //
127   pipeline = gst_pipeline_new ("moitoncells-pipeline");
128   if (argc == 2 && (g_strcmp0 (argv[1], "test") == 0))
129     source = gst_element_factory_make ("videotestsrc", "vidsrc");
130   else if (argc == 3 && (g_strcmp0 (argv[1], "rtsp") == 0))
131     source = gst_element_factory_make ("rtspsrc", "rtspsrc0");
132   else if (argc == 1)
133     source = gst_element_factory_make ("v4l2src", "v4l2");
134   else {
135     fprintf (stderr, "Usage: %s test or rtsp rtsp://your/cam/address\n",
136         argv[0]);
137     exit (-1);
138   }
139
140   videor = gst_element_factory_make ("videorate", "videor");
141   videos = gst_element_factory_make ("videoscale", "videos");
142   capsf = gst_element_factory_make ("capsfilter", "capsf");
143   if (argc == 3 && (g_strcmp0 (argv[1], "rtsp") == 0))
144     decodebin = gst_element_factory_make ("decodebin", "decode");
145   else
146     decodebin = NULL;
147   colorsp0 = gst_element_factory_make ("ffmpegcolorspace", "colorspace0");
148   mcells = gst_element_factory_make ("motioncells", "mcells");
149   colorsp1 = gst_element_factory_make ("ffmpegcolorspace", "colorspace1");
150   sink = gst_element_factory_make ("xvimagesink", "xv-image-sink");
151   if (!pipeline || !source || !videor || !videos || !capsf || !colorsp0
152       || !mcells || !colorsp1 || !sink) {
153     g_printerr ("One element could not be created. Exiting.\n");
154     return -1;
155   }
156   if (argc == 3 && (g_strcmp0 (argv[1], "rtsp") == 0) && !decodebin) {
157     g_printerr ("Decodebin could not be created. Exiting.\n");
158     return -1;
159   }
160   if ((g_strcmp0 (argv[1], "rtsp") == 0)) {
161     g_object_set (G_OBJECT (source), "location", argv[2], NULL);
162     g_object_set (G_OBJECT (source), "latency", 1000, NULL);
163   } else if ((g_strcmp0 (argv[1], "test") == 0))
164     g_object_set (G_OBJECT (source), "pattern", 18, NULL);
165
166   caps =
167       gst_caps_from_string
168       ("video/x-raw-yuv,width=320,height=240,framerate=10/1");
169   g_object_set (G_OBJECT (capsf), "caps", caps, NULL);
170   //g_object_set (G_OBJECT (sink), "sync",FALSE,NULL);
171
172   if (argc > 1) {
173     if (g_strcmp0 (argv[1], "test") == 0) {
174       gst_bin_add_many (GST_BIN (pipeline),
175           source, videor, videos, capsf, colorsp0, mcells, colorsp1, sink,
176           NULL);
177
178       gst_element_link_many (source, videor, videos, capsf, colorsp0, mcells,
179           colorsp1, sink, NULL);
180     } else if (g_strcmp0 (argv[1], "rtsp") == 0) {
181       gst_bin_add_many (GST_BIN (pipeline),
182           source, videor, videos, capsf, decodebin, colorsp0, mcells, colorsp1,
183           sink, NULL);
184
185       gst_element_link_many (source, videor, videos, capsf, decodebin, colorsp0,
186           mcells, colorsp1, sink, NULL);
187     }
188   } else {                      //default
189     gst_bin_add_many (GST_BIN (pipeline),
190         source, videor, videos, capsf, colorsp0, mcells, colorsp1, sink, NULL);
191
192     gst_element_link_many (source, videor, videos, capsf, colorsp0, mcells,
193         colorsp1, sink, NULL);
194   }
195
196   g_print ("Now playing\n");
197   gst_element_set_state (pipeline, GST_STATE_PLAYING);
198   g_print ("Running...\n");
199   g_print ("You can use these properties : \n");
200   gst_element_print_properties (mcells);
201   g_print ("change property here: example  some_property property_value \n");
202   g_print ("Quit with 'q' \n");
203   //get all properties
204   property_specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (mcells),
205       &num_properties);
206   while (TRUE) {
207     found_property = FALSE;
208     i = 0;
209
210     ret = scanf ("%19s %99s", property, prop_value);
211
212     if (ret < 1)
213       g_printerr ("Error parsing command.\n");
214
215     if ((g_strcmp0 (property, "q") == 0) || (g_strcmp0 (prop_value, "q") == 0))
216       break;
217     printf ("property: %s -> value: %s \n", property, prop_value);
218     for (i = 0; i < num_properties; i++) {
219       GParamSpec *param = property_specs[i];
220       g_value_init (&value, param->value_type);
221       g_object_get_property (G_OBJECT (mcells), param->name, &value);
222       //fprintf(stderr,"property: %s and param name: %s and property value: %s \n",property,param->name,prop_value);
223       if ((g_strcmp0 (property, param->name) == 0) && !found_property &&
224           (g_strcmp0 (prop_value, "") != 0)
225           && (g_strcmp0 (prop_value, "\"") != 0)
226           && (g_strcmp0 (prop_value, "\'") != 0)) {
227         GType type;
228         found_property = TRUE;
229         type = param->value_type;
230         setProperty (mcells, property, prop_value, type, &value);
231       }
232       g_value_unset (&value);
233       if (found_property)
234         break;
235     }
236   }
237
238   gst_element_set_state (pipeline, GST_STATE_NULL);
239   gst_object_unref (pipeline);
240   return 0;
241 }