1 /* GStreamer interactive test for the videocrop element
2 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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 <gst/gst.h>
25
26 #include <stdlib.h>
27 #include <math.h>
28
29 GST_DEBUG_CATEGORY_STATIC (videocrop_test_debug);
30 #define GST_CAT_DEFAULT videocrop_test_debug
31
32 #define OUT_WIDTH 640
33 #define OUT_HEIGHT 480
34 #define TIME_PER_TEST 10 /* seconds each format is tested */
35 #define FRAMERATE 15 /* frames per second */
36
37 #ifndef DEFAULT_VIDEOSINK
38 #define DEFAULT_VIDEOSINK "xvimagesink"
39 #endif
40
41 static gboolean
check_bus_for_errors(GstBus * bus,GstClockTime max_wait_time)42 check_bus_for_errors (GstBus * bus, GstClockTime max_wait_time)
43 {
44 GstMessage *msg;
45
46 msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, max_wait_time);
47
48 if (msg) {
49 GError *err = NULL;
50 gchar *debug = NULL;
51
52 g_assert (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
53 gst_message_parse_error (msg, &err, &debug);
54 GST_ERROR ("ERROR: %s [%s]", err->message, debug);
55 g_print ("\n===========> ERROR: %s\n%s\n\n", err->message, debug);
56 g_clear_error (&err);
57 g_free (debug);
58 gst_message_unref (msg);
59 }
60
61 return (msg != NULL);
62 }
63
64 static void
test_with_caps(GstElement * src,GstElement * videocrop,GstCaps * caps)65 test_with_caps (GstElement * src, GstElement * videocrop, GstCaps * caps)
66 {
67 GstClockTime time_run;
68 GstElement *pipeline;
69 GTimer *timer;
70 GstBus *bus;
71 GstPad *pad;
72 guint hcrop;
73 guint vcrop;
74
75 /* caps must be writable, we can't check that here though */
76 g_assert (GST_CAPS_REFCOUNT_VALUE (caps) == 1);
77
78 timer = g_timer_new ();
79 vcrop = 0;
80 hcrop = 0;
81
82 pipeline = GST_ELEMENT (gst_element_get_parent (videocrop));
83 g_assert (GST_IS_PIPELINE (pipeline));
84
85 /* at this point the pipeline is in PLAYING state; we only want to capture
86 * errors resulting from our on-the-fly changing of the filtercaps */
87 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
88
89 /* pad to block */
90 pad = gst_element_get_static_pad (src, "src");
91
92 time_run = 0;
93 do {
94 GstClockTime wait_time, waited_for_block;
95
96 if (check_bus_for_errors (bus, 0))
97 break;
98
99 wait_time = GST_SECOND / FRAMERATE;
100
101 GST_LOG ("hcrop = %3d, vcrop = %3d", vcrop, hcrop);
102
103 g_timer_reset (timer);
104
105 /* need to block the streaming thread while changing these properties,
106 * otherwise we might get random not-negotiated errors (when caps are
107 * changed in between upstream calling pad_alloc_buffer() and pushing
108 * the processed buffer?) FIXME should not be needed */
109 /* gst_pad_set_blocked (pad, TRUE); */
110 g_object_set (videocrop, "left", hcrop, "top", vcrop, NULL);
111 /* gst_pad_set_blocked (pad, FALSE); */
112
113 waited_for_block = g_timer_elapsed (timer, NULL) * (double) GST_SECOND;
114 /* GST_LOG ("waited: %" GST_TIME_FORMAT ", frame len: %" GST_TIME_FORMAT,
115 GST_TIME_ARGS (waited_for_block), GST_TIME_ARGS (wait_time)); */
116 ++vcrop;
117 ++hcrop;
118
119 if (wait_time > waited_for_block) {
120 g_usleep ((wait_time - waited_for_block) / GST_MSECOND);
121 }
122
123 time_run += wait_time;
124 }
125 while (time_run < (TIME_PER_TEST * GST_SECOND));
126
127 g_timer_destroy (timer);
128 gst_object_unref (bus);
129 gst_object_unref (pad);
130 gst_object_unref (pipeline);
131 }
132
133 /* return a list of caps where we only need to set
134 * width and height to get fixed caps */
135 static GList *
video_crop_get_test_caps(GstElement * videocrop)136 video_crop_get_test_caps (GstElement * videocrop)
137 {
138 const GstCaps *allowed_caps;
139 GstPad *srcpad;
140 GList *list = NULL;
141 guint i;
142
143 srcpad = gst_element_get_static_pad (videocrop, "src");
144 g_assert (srcpad != NULL);
145 allowed_caps = gst_pad_get_pad_template_caps (srcpad);
146 g_assert (allowed_caps != NULL);
147
148 for (i = 0; i < gst_caps_get_size (allowed_caps); ++i) {
149 GstStructure *new_structure;
150 GstCaps *single_caps;
151
152 single_caps = gst_caps_new_empty ();
153 new_structure =
154 gst_structure_copy (gst_caps_get_structure (allowed_caps, i));
155 gst_structure_set (new_structure, "framerate", GST_TYPE_FRACTION,
156 FRAMERATE, 1, NULL);
157 gst_structure_remove_field (new_structure, "width");
158 gst_structure_remove_field (new_structure, "height");
159 gst_structure_remove_field (new_structure, "format");
160 gst_caps_append_structure (single_caps, new_structure);
161
162 /* should be fixed without width/height */
163 g_assert (gst_caps_is_fixed (single_caps));
164
165 list = g_list_prepend (list, single_caps);
166 }
167
168 gst_object_unref (srcpad);
169
170 return list;
171 }
172
173 static gchar *opt_videosink_str; /* NULL */
174 static gchar *opt_filtercaps_str; /* NULL */
175 static gboolean opt_with_videoconvert; /* FALSE */
176
177 int
main(int argc,char ** argv)178 main (int argc, char **argv)
179 {
180 static const GOptionEntry test_goptions[] = {
181 {"videosink", '\0', 0, G_OPTION_ARG_STRING, &opt_videosink_str,
182 "videosink to use (default: " DEFAULT_VIDEOSINK ")", NULL},
183 {"caps", '\0', 0, G_OPTION_ARG_STRING, &opt_filtercaps_str,
184 "filter caps to narrow down formats to test", NULL},
185 {"with-videoconvert", '\0', 0, G_OPTION_ARG_NONE,
186 &opt_with_videoconvert,
187 "whether to add an videoconvert element in front of the sink",
188 NULL},
189 {NULL, '\0', 0, 0, NULL, NULL, NULL}
190 };
191 GOptionContext *ctx;
192 GError *opt_err = NULL;
193
194 GstElement *pipeline, *src, *filter1, *crop, *scale, *filter2, *csp, *sink;
195 GstCaps *filter_caps = NULL;
196 GList *caps_list, *l;
197
198 /* command line option parsing */
199 ctx = g_option_context_new ("");
200 g_option_context_add_group (ctx, gst_init_get_option_group ());
201 g_option_context_add_main_entries (ctx, test_goptions, NULL);
202
203 if (!g_option_context_parse (ctx, &argc, &argv, &opt_err)) {
204 g_error ("Error parsing command line options: %s", opt_err->message);
205 g_option_context_free (ctx);
206 g_clear_error (&opt_err);
207 return -1;
208 }
209 g_option_context_free (ctx);
210
211 GST_DEBUG_CATEGORY_INIT (videocrop_test_debug, "videocroptest", 0, "vctest");
212
213 pipeline = gst_pipeline_new ("pipeline");
214 src = gst_element_factory_make ("videotestsrc", "videotestsrc");
215 g_assert (src != NULL);
216 filter1 = gst_element_factory_make ("capsfilter", "capsfilter1");
217 g_assert (filter1 != NULL);
218 crop = gst_element_factory_make ("videocrop", "videocrop");
219 g_assert (crop != NULL);
220 scale = gst_element_factory_make ("videoscale", "videoscale");
221 g_assert (scale != NULL);
222 filter2 = gst_element_factory_make ("capsfilter", "capsfilter2");
223 g_assert (filter2 != NULL);
224
225 if (opt_with_videoconvert) {
226 g_print ("Adding videoconvert\n");
227 csp = gst_element_factory_make ("videoconvert", "colorspace");
228 } else {
229 csp = gst_element_factory_make ("identity", "colorspace");
230 }
231 g_assert (csp != NULL);
232
233 if (opt_filtercaps_str) {
234 filter_caps = gst_caps_from_string (opt_filtercaps_str);
235 if (filter_caps == NULL) {
236 g_error ("Invalid filter caps string '%s'", opt_filtercaps_str);
237 } else {
238 g_print ("Using filter caps '%s'\n", opt_filtercaps_str);
239 }
240 }
241
242 if (opt_videosink_str) {
243 g_print ("Trying videosink '%s' ...", opt_videosink_str);
244 sink = gst_element_factory_make (opt_videosink_str, "sink");
245 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
246 } else {
247 sink = NULL;
248 }
249
250 if (sink == NULL) {
251 g_print ("Trying videosink '%s' ...", DEFAULT_VIDEOSINK);
252 sink = gst_element_factory_make (DEFAULT_VIDEOSINK, "sink");
253 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
254 }
255 if (sink == NULL) {
256 g_print ("Trying videosink '%s' ...", "xvimagesink");
257 sink = gst_element_factory_make ("xvimagesink", "sink");
258 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
259 }
260 if (sink == NULL) {
261 g_print ("Trying videosink '%s' ...", "ximagesink");
262 sink = gst_element_factory_make ("ximagesink", "sink");
263 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
264 }
265
266 g_assert (sink != NULL);
267
268 gst_bin_add_many (GST_BIN (pipeline), src, filter1, crop, scale, filter2,
269 csp, sink, NULL);
270
271 if (!gst_element_link (src, filter1))
272 g_error ("Failed to link videotestsrc to capsfilter1");
273
274 if (!gst_element_link (filter1, crop))
275 g_error ("Failed to link capsfilter1 to videocrop");
276
277 if (!gst_element_link (crop, scale))
278 g_error ("Failed to link videocrop to videoscale");
279
280 if (!gst_element_link (scale, filter2))
281 g_error ("Failed to link videoscale to capsfilter2");
282
283 if (!gst_element_link (filter2, csp))
284 g_error ("Failed to link capsfilter2 to videoconvert");
285
286 if (!gst_element_link (csp, sink))
287 g_error ("Failed to link videoconvert to video sink");
288
289 caps_list = video_crop_get_test_caps (crop);
290 for (l = caps_list; l != NULL; l = l->next) {
291 GstStateChangeReturn ret;
292 GstCaps *caps, *out_caps;
293 gboolean skip = FALSE;
294 gchar *s;
295
296 if (filter_caps) {
297 GstCaps *icaps;
298
299 icaps = gst_caps_intersect (filter_caps, GST_CAPS (l->data));
300 skip = gst_caps_is_empty (icaps);
301 gst_caps_unref (icaps);
302 }
303
304 /* this is the size of our window (stays fixed) */
305 out_caps = gst_caps_copy (GST_CAPS (l->data));
306 gst_structure_set (gst_caps_get_structure (out_caps, 0), "width",
307 G_TYPE_INT, OUT_WIDTH, "height", G_TYPE_INT, OUT_HEIGHT, NULL);
308
309 g_object_set (filter2, "caps", out_caps, NULL);
310
311 /* filter1 gets these too to prevent videotestsrc from renegotiating */
312 g_object_set (filter1, "caps", out_caps, NULL);
313 gst_caps_unref (out_caps);
314
315 caps = gst_caps_copy (GST_CAPS (l->data));
316 GST_INFO ("testing format: %" GST_PTR_FORMAT, caps);
317
318 s = gst_caps_to_string (caps);
319
320 if (skip) {
321 g_print ("Skipping format: %s\n", s);
322 g_free (s);
323 continue;
324 }
325
326 g_print ("Format: %s\n", s);
327
328 caps = gst_caps_make_writable (caps);
329
330 /* FIXME: check return values */
331 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
332 if (ret != GST_STATE_CHANGE_FAILURE) {
333 ret = gst_element_get_state (pipeline, NULL, NULL, -1);
334
335 if (ret != GST_STATE_CHANGE_FAILURE) {
336 test_with_caps (src, crop, caps);
337 } else {
338 g_print ("Format: %s not supported (failed to go to PLAYING)\n", s);
339 }
340 } else {
341 g_print ("Format: %s not supported\n", s);
342 }
343
344 gst_element_set_state (pipeline, GST_STATE_NULL);
345
346 gst_caps_unref (caps);
347 g_free (s);
348 }
349
350 g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
351 g_list_free (caps_list);
352
353 gst_element_set_state (pipeline, GST_STATE_NULL);
354 gst_object_unref (pipeline);
355
356 return 0;
357 }
358