Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / tests / examples / opencv / gstfacedetect_test.c
1 /* GStreamer
2  * Copyright (C) 2015 Vanessa Chipirrás <vchipirras6@gmail.com>
3  *
4  *  gstfacedetect_test: gstreamer facedetect plugin demo application,
5  *  part work of Outreachy 2015 project
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gst/gst.h>
28
29 GstElement *playbin, *pipeline;
30 GstElement *v4l2src, *videoscale, *videoconvert_in, *facedetect,
31     *videoconvert_out, *autovideosink;
32 static gboolean ctrlvol = FALSE;
33 static gboolean silent = FALSE;
34
35 static GstBusSyncReply
36 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
37 {
38   const GstStructure *structure;
39   const GValue *value;
40   gchar *contents;
41   gint i;
42   guint size = 0;
43
44   /* select msg */
45   if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT ||
46       !gst_structure_has_name (gst_message_get_structure (message),
47           "facedetect"))
48     return GST_BUS_PASS;
49
50   /* parse msg structure */
51   structure = gst_message_get_structure (message);
52
53   /* if facedetect is into buffer */
54   if (structure &&
55       strcmp (gst_structure_get_name (structure), "facedetect") == 0) {
56     if (!silent) {
57       /* print message type and structure name */
58       g_print ("Type message, name message: %s{{%s}}\n",
59           gst_message_type_get_name (message->type),
60           gst_structure_get_name (structure));
61
62       /* print msg structure names and type */
63       for (i = 0; i < gst_structure_n_fields (structure); i++) {
64         const gchar *name = gst_structure_nth_field_name (structure, i);
65         GType type = gst_structure_get_field_type (structure, name);
66         g_print ("-Name field, type: %s[%s]\n", name, g_type_name (type));
67       }
68     }
69
70     /* get structure of faces */
71     value = gst_structure_get_value (structure, "faces");
72     /* obtain the contents into the structure */
73     contents = g_strdup_value_contents (value);
74     if (!silent)
75       g_print ("Detected objects: %s\n\n", *(&contents));
76
77     /* list size */
78     size = gst_value_list_get_size (value);
79
80     /* if face is detected, obtain the values X and Y of mouth and of nose. */
81     if (size != 0) {
82       GstState state;
83
84       /* if paused, set to playing */
85       gst_element_get_state (GST_ELEMENT (playbin), &state, NULL,
86           GST_CLOCK_TIME_NONE);
87       if (state != GST_STATE_PLAYING) {
88         gst_element_set_state (GST_ELEMENT (playbin), GST_STATE_PLAYING);
89       }
90
91       if (ctrlvol) {
92         gdouble volume;
93
94         const GValue *faces_value = gst_value_list_get_value (value, 0);
95         const GstStructure *faces_structure =
96             gst_value_get_structure (faces_value);
97         gboolean have_mouth_y =
98             gst_structure_has_field (faces_structure, "mouth->y");
99         gboolean have_mouth_x =
100             gst_structure_has_field (faces_structure, "mouth->x");
101         gboolean have_nose_y =
102             gst_structure_has_field (faces_structure, "nose->y");
103         gboolean have_nose_x =
104             gst_structure_has_field (faces_structure, "nose->x");
105
106         /* get the volume value */
107         g_object_get (G_OBJECT (playbin), "volume", &volume, NULL);
108
109         /* media operation - hide your mouth for down the volume of the video */
110         if (have_mouth_y == 0 && have_mouth_x == 0) {
111           volume = volume - 0.5;
112           if (volume <= 0.5)
113             volume = 0.0;
114           g_object_set (G_OBJECT (playbin), "volume", volume, NULL);
115         }
116         /* media operation - hide your nose for up the volume of the video */
117         if (have_nose_y == 0 && have_nose_x == 0) {
118           volume = volume + 0.5;
119           if (volume >= 9.5)
120             volume = 10.0;
121           g_object_set (G_OBJECT (playbin), "volume", volume, NULL);
122         }
123       }
124       /* if face is not detected */
125     } else {
126       /* media operation - hide your face to stop media play */
127       gst_element_set_state (playbin, GST_STATE_PAUSED);
128     }
129   }
130   gst_message_unref (message);
131   return GST_BUS_DROP;
132 }
133
134 int
135 main (gint argc, gchar ** argv)
136 {
137   static GMainLoop *loop;
138   GstCaps *caps;
139   GstBus *bus;
140   gchar *uri;
141
142   GOptionEntry options[] = {
143     {"control-volume", 'c', 0, G_OPTION_ARG_NONE, &ctrlvol,
144         "Control the volume by hiding the nose or mouth", NULL},
145     {"silent", 's', 0, G_OPTION_ARG_NONE, &silent,
146         "Don't output the messages and detected faces structure", NULL},
147     {NULL}
148   };
149   GOptionContext *ctx;
150   GError *err = NULL;
151
152   ctx = g_option_context_new ("<video file>\n\nfacedetect test application.");
153   g_option_context_add_main_entries (ctx, options, NULL);
154   g_option_context_add_group (ctx, gst_init_get_option_group ());
155   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
156     g_print ("Error initializing: %s\n", err->message);
157     g_option_context_free (ctx);
158     g_clear_error (&err);
159     exit (1);
160   }
161   g_option_context_free (ctx);
162
163   if (argc < 2) {
164     fprintf (stderr, "oops, please give a file to play\n");
165     return -1;
166   }
167
168   uri = g_filename_to_uri (argv[1], NULL, NULL);
169   if (!uri) {
170     fprintf (stderr, "failed to create the uri\n");
171     return -1;
172   }
173
174   /* init gst */
175   gst_init (&argc, &argv);
176
177   loop = g_main_loop_new (NULL, FALSE);
178   /* init elements */
179   playbin = gst_element_factory_make ("playbin", "app_playbin");
180   pipeline = gst_pipeline_new ("app_pipeline");
181   v4l2src = gst_element_factory_make ("v4l2src", "app_v4l2src");
182   videoscale = gst_element_factory_make ("videoscale", "app_videoscale");
183   videoconvert_in =
184       gst_element_factory_make ("videoconvert", "app_videoconvert_in");
185   facedetect = gst_element_factory_make ("facedetect", "app_facedetect");
186   videoconvert_out =
187       gst_element_factory_make ("videoconvert", "app_videoconvert_out");
188   autovideosink =
189       gst_element_factory_make ("autovideosink", "app_autovideosink");
190
191   /* check init results */
192   if (!playbin || !pipeline || !v4l2src || !videoscale || !videoconvert_in
193       || !facedetect || !videoconvert_out || !autovideosink)
194     g_error ("ERROR: element init failed.\n");
195
196   /* set values */
197   g_object_set (G_OBJECT (playbin), "uri", uri, NULL);
198
199   /* set caps */
200   caps =
201       gst_caps_from_string
202       ("video/x-raw, format=(string)RGB, width=320, height=240, framerate=(fraction)30/1");
203
204   /* set bus */
205   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
206   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler, pipeline,
207       NULL);
208   gst_object_unref (bus);
209
210   /* add elements to pipeline */
211   gst_bin_add_many (GST_BIN (pipeline),
212       v4l2src,
213       videoscale,
214       videoconvert_in, facedetect, videoconvert_out, autovideosink, NULL);
215
216   /* negotiate caps */
217   if (!gst_element_link_filtered (v4l2src, videoscale, caps)) {
218     g_printerr ("ERROR:v4l2src -> videoscale caps\n");
219     return 0;
220   }
221   gst_caps_unref (caps);
222
223   /* link elements */
224   gst_element_link_many (videoscale,
225       videoconvert_in, facedetect, videoconvert_out, autovideosink, NULL);
226
227   /* change states */
228   gst_element_set_state (pipeline, GST_STATE_PLAYING);
229
230   /* start main loop */
231   g_main_loop_run (loop);
232
233   /* clean all */
234   gst_element_set_state (pipeline, GST_STATE_NULL);
235   gst_object_unref (GST_OBJECT (pipeline));
236   gst_element_set_state (playbin, GST_STATE_NULL);
237   gst_object_unref (GST_OBJECT (playbin));
238
239   return 0;
240 }