1 /* GStreamer interactive videocrop test
2  *
3  * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <stdlib.h>
26 
27 #include <gst/gst.h>
28 
29 static GstElement *
make_pipeline(gint type)30 make_pipeline (gint type)
31 {
32   GstElement *result;
33   gchar *pstr;
34 
35   switch (type) {
36     case 0:
37       pstr = g_strdup_printf ("videotestsrc ! videocrop name=crop ! "
38           "xvimagesink");
39       break;
40     default:
41       return NULL;
42   }
43 
44   result = gst_parse_launch_full (pstr, NULL, GST_PARSE_FLAG_NONE, NULL);
45   g_print ("created test %d: \"%s\"\n", type, pstr);
46   g_free (pstr);
47 
48   return result;
49 }
50 
51 #define MAX_ROUND 500
52 
53 int
main(int argc,char ** argv)54 main (int argc, char **argv)
55 {
56   GstElement *pipe, *crop;
57   gint left, right;
58   gint top, bottom;
59   gint ldir, rdir;
60   gint tdir, bdir;
61   gint round, type, stop;
62 
63   gst_init (&argc, &argv);
64 
65   type = 0;
66   stop = -1;
67 
68   if (argc > 1) {
69     type = atoi (argv[1]);
70     stop = type + 1;
71   }
72 
73   while (TRUE) {
74     GstMessage *message;
75 
76     pipe = make_pipeline (type);
77     if (pipe == NULL)
78       break;
79 
80     crop = gst_bin_get_by_name (GST_BIN (pipe), "crop");
81     g_assert (crop);
82 
83     top = bottom = left = right = 0;
84     tdir = bdir = 10;
85     ldir = rdir = 10;
86 
87     for (round = 0; round < MAX_ROUND; round++) {
88       g_print ("crop to %4d %4d %4d %4d (%d/%d)   \r", top, bottom, left, right,
89           round, MAX_ROUND);
90 
91       g_object_set (crop, "top", top, "bottom", bottom, "left", left, "right",
92           right, NULL);
93 
94       if (round == 0)
95         gst_element_set_state (pipe, GST_STATE_PLAYING);
96 
97       top += tdir;
98       if (top >= 80)
99         tdir = -10;
100       else if (top < 10)
101         tdir = 10;
102 
103       bottom += bdir;
104       if (bottom >= 60)
105         bdir = -10;
106       else if (bottom < 10)
107         bdir = 10;
108 
109       left += ldir;
110       if (left >= 100)
111         ldir = -10;
112       else if (left < 10)
113         ldir = 10;
114 
115       right += rdir;
116       if (right >= 80)
117         rdir = -10;
118       else if (right < 10)
119         rdir = 10;
120 
121       message =
122           gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR,
123           50 * GST_MSECOND);
124       if (message) {
125         g_print ("got error           \n");
126 
127         gst_message_unref (message);
128       }
129     }
130     g_print ("test %d done                    \n", type);
131 
132     gst_object_unref (crop);
133     gst_element_set_state (pipe, GST_STATE_NULL);
134     gst_object_unref (pipe);
135 
136     type++;
137     if (type == stop)
138       break;
139   }
140   return 0;
141 }
142