2 * Copyright (C) 2015 Vanessa Chipirrás <vchipirras6@gmail.com>
4 * gstfacedetect_test: gstreamer facedetect plugin demo application,
5 * part work of Outreachy 2015 project
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.
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.
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.
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;
35 static GstBusSyncReply
36 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
38 const GstStructure *structure;
45 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT ||
46 !gst_structure_has_name (gst_message_get_structure (message),
50 /* parse msg structure */
51 structure = gst_message_get_structure (message);
53 /* if facedetect is into buffer */
55 strcmp (gst_structure_get_name (structure), "facedetect") == 0) {
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));
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));
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);
75 g_print ("Detected objects: %s\n\n", *(&contents));
78 size = gst_value_list_get_size (value);
80 /* if face is detected, obtain the values X and Y of mouth and of nose. */
84 /* if paused, set to playing */
85 gst_element_get_state (GST_ELEMENT (playbin), &state, NULL,
87 if (state != GST_STATE_PLAYING) {
88 gst_element_set_state (GST_ELEMENT (playbin), GST_STATE_PLAYING);
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");
106 /* get the volume value */
107 g_object_get (G_OBJECT (playbin), "volume", &volume, NULL);
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;
114 g_object_set (G_OBJECT (playbin), "volume", volume, NULL);
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;
121 g_object_set (G_OBJECT (playbin), "volume", volume, NULL);
124 /* if face is not detected */
126 /* media operation - hide your face to stop media play */
127 gst_element_set_state (playbin, GST_STATE_PAUSED);
130 gst_message_unref (message);
135 main (gint argc, gchar ** argv)
137 static GMainLoop *loop;
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},
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);
161 g_option_context_free (ctx);
164 fprintf (stderr, "oops, please give a file to play\n");
168 uri = g_filename_to_uri (argv[1], NULL, NULL);
170 fprintf (stderr, "failed to create the uri\n");
175 gst_init (&argc, &argv);
177 loop = g_main_loop_new (NULL, FALSE);
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");
184 gst_element_factory_make ("videoconvert", "app_videoconvert_in");
185 facedetect = gst_element_factory_make ("facedetect", "app_facedetect");
187 gst_element_factory_make ("videoconvert", "app_videoconvert_out");
189 gst_element_factory_make ("autovideosink", "app_autovideosink");
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");
197 g_object_set (G_OBJECT (playbin), "uri", uri, NULL);
202 ("video/x-raw, format=(string)RGB, width=320, height=240, framerate=(fraction)30/1");
205 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
206 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler, pipeline,
208 gst_object_unref (bus);
210 /* add elements to pipeline */
211 gst_bin_add_many (GST_BIN (pipeline),
214 videoconvert_in, facedetect, videoconvert_out, autovideosink, NULL);
217 if (!gst_element_link_filtered (v4l2src, videoscale, caps)) {
218 g_printerr ("ERROR:v4l2src -> videoscale caps\n");
221 gst_caps_unref (caps);
224 gst_element_link_many (videoscale,
225 videoconvert_in, facedetect, videoconvert_out, autovideosink, NULL);
228 gst_element_set_state (pipeline, GST_STATE_PLAYING);
230 /* start main loop */
231 g_main_loop_run (loop);
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));