1 /* GStreamer interactive videoscale test
2  * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.com>
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 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <stdlib.h>
25 
26 #include <gst/gst.h>
27 
28 static GstElement *
make_pipeline(gint type)29 make_pipeline (gint type)
30 {
31   GstElement *result;
32   gchar *pstr;
33 
34   switch (type) {
35     case 0:
36       pstr = g_strdup_printf ("videotestsrc ! capsfilter name=filter ! "
37           "ximagesink");
38       break;
39     case 1:
40       pstr = g_strdup_printf ("videotestsrc ! queue ! capsfilter name=filter ! "
41           "ximagesink");
42       break;
43     case 2:
44       pstr = g_strdup_printf ("videotestsrc ! videoscale ! "
45           "capsfilter name=filter ! " "ximagesink");
46       break;
47     case 3:
48       pstr =
49           g_strdup_printf ("videotestsrc ! queue ! videoscale ! "
50           "capsfilter name=filter ! " "ximagesink");
51       break;
52     case 4:
53       pstr =
54           g_strdup_printf ("videotestsrc ! videoscale ! queue ! "
55           "capsfilter name=filter ! " "ximagesink");
56       break;
57     case 5:
58       pstr = g_strdup_printf ("v4l2src ! videoconvert ! videoscale ! "
59           "capsfilter name=filter ! " "ximagesink");
60       break;
61     default:
62       return NULL;
63   }
64 
65   result = gst_parse_launch_full (pstr, NULL, GST_PARSE_FLAG_NONE, NULL);
66   g_print ("created test %d: \"%s\"\n", type, pstr);
67   g_free (pstr);
68 
69   return result;
70 }
71 
72 #define MAX_ROUND 100
73 
74 int
main(int argc,char ** argv)75 main (int argc, char **argv)
76 {
77   GstElement *pipe, *filter;
78   GstCaps *caps;
79   gint width, height;
80   gint xdir, ydir;
81   gint round, type, stop;
82 
83   gst_init (&argc, &argv);
84 
85   type = 0;
86   stop = -1;
87 
88   if (argc > 1) {
89     type = atoi (argv[1]);
90     stop = type + 1;
91   }
92 
93   while (TRUE) {
94     GstMessage *message;
95 
96     pipe = make_pipeline (type);
97     if (pipe == NULL)
98       break;
99 
100     filter = gst_bin_get_by_name (GST_BIN (pipe), "filter");
101     g_assert (filter);
102 
103     width = 320;
104     height = 240;
105     xdir = ydir = -10;
106 
107     for (round = 0; round < MAX_ROUND; round++) {
108       gchar *capsstr;
109       g_print ("resize to %dx%d (%d/%d)   \r", width, height, round, MAX_ROUND);
110 
111       /* we prefer our fixed width and height but allow other dimensions to pass
112        * as well */
113       capsstr =
114           g_strdup_printf ("video/x-raw, width=(int)%d, height=(int)%d;"
115           "video/x-raw", width, height);
116 
117       caps = gst_caps_from_string (capsstr);
118       g_free (capsstr);
119       g_object_set (filter, "caps", caps, NULL);
120       gst_caps_unref (caps);
121 
122       if (round == 0)
123         gst_element_set_state (pipe, GST_STATE_PLAYING);
124 
125       width += xdir;
126       if (width >= 320)
127         xdir = -10;
128       else if (width < 200)
129         xdir = 10;
130 
131       height += ydir;
132       if (height >= 240)
133         ydir = -10;
134       else if (height < 150)
135         ydir = 10;
136 
137       message =
138           gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR,
139           50 * GST_MSECOND);
140       if (message) {
141         g_print ("got error           \n");
142 
143         gst_message_unref (message);
144       }
145     }
146     g_print ("test %d done                    \n", type);
147 
148     gst_object_unref (filter);
149     gst_element_set_state (pipe, GST_STATE_NULL);
150     gst_object_unref (pipe);
151 
152     type++;
153     if (type == stop)
154       break;
155   }
156   return 0;
157 }
158