1 /* GStreamer interactive test for the videocrop element
2 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
29 GST_DEBUG_CATEGORY_STATIC (videocrop_test_debug);
30 #define GST_CAT_DEFAULT videocrop_test_debug
33 #define OUT_HEIGHT 480
34 #define TIME_PER_TEST 10 /* seconds each format is tested */
35 #define FRAMERATE 15 /* frames per second */
37 #ifndef DEFAULT_VIDEOSINK
38 #define DEFAULT_VIDEOSINK "xvimagesink"
42 check_bus_for_errors (GstBus * bus, GstClockTime max_wait_time)
46 msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, max_wait_time);
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);
58 gst_message_unref (msg);
65 test_with_caps (GstElement * src, GstElement * videocrop, GstCaps * caps)
67 GstClockTime time_run;
75 /* caps must be writable, we can't check that here though */
76 g_assert (GST_CAPS_REFCOUNT_VALUE (caps) == 1);
78 timer = g_timer_new ();
82 pipeline = GST_ELEMENT (gst_element_get_parent (videocrop));
83 g_assert (GST_IS_PIPELINE (pipeline));
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));
90 pad = gst_element_get_static_pad (src, "src");
94 GstClockTime wait_time, waited_for_block;
96 if (check_bus_for_errors (bus, 0))
99 wait_time = GST_SECOND / FRAMERATE;
101 GST_LOG ("hcrop = %3d, vcrop = %3d", vcrop, hcrop);
103 g_timer_reset (timer);
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); */
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)); */
119 if (wait_time > waited_for_block) {
120 g_usleep ((wait_time - waited_for_block) / GST_MSECOND);
123 time_run += wait_time;
125 while (time_run < (TIME_PER_TEST * GST_SECOND));
127 g_timer_destroy (timer);
128 gst_object_unref (bus);
129 gst_object_unref (pad);
132 /* return a list of caps where we only need to set
133 * width and height to get fixed caps */
135 video_crop_get_test_caps (GstElement * videocrop)
137 const GstCaps *allowed_caps;
142 srcpad = gst_element_get_static_pad (videocrop, "src");
143 g_assert (srcpad != NULL);
144 allowed_caps = gst_pad_get_pad_template_caps (srcpad);
145 g_assert (allowed_caps != NULL);
147 for (i = 0; i < gst_caps_get_size (allowed_caps); ++i) {
148 GstStructure *new_structure;
149 GstCaps *single_caps;
151 single_caps = gst_caps_new_empty ();
153 gst_structure_copy (gst_caps_get_structure (allowed_caps, i));
154 gst_structure_set (new_structure, "framerate", GST_TYPE_FRACTION,
156 gst_structure_remove_field (new_structure, "width");
157 gst_structure_remove_field (new_structure, "height");
158 gst_caps_append_structure (single_caps, new_structure);
160 /* should be fixed without width/height */
161 g_assert (gst_caps_is_fixed (single_caps));
163 list = g_list_prepend (list, single_caps);
166 gst_object_unref (srcpad);
171 static gchar *opt_videosink_str; /* NULL */
172 static gchar *opt_filtercaps_str; /* NULL */
173 static gboolean opt_with_ffmpegcolorspace; /* FALSE */
176 main (int argc, char **argv)
178 static const GOptionEntry test_goptions[] = {
179 {"videosink", '\0', 0, G_OPTION_ARG_STRING, &opt_videosink_str,
180 "videosink to use (default: " DEFAULT_VIDEOSINK ")", NULL},
181 {"caps", '\0', 0, G_OPTION_ARG_STRING, &opt_filtercaps_str,
182 "filter caps to narrow down formats to test", NULL},
183 {"with-ffmpegcolorspace", '\0', 0, G_OPTION_ARG_NONE,
184 &opt_with_ffmpegcolorspace,
185 "whether to add an ffmpegcolorspace element in front of the sink",
187 {NULL, '\0', 0, 0, NULL, NULL, NULL}
190 GError *opt_err = NULL;
192 GstElement *pipeline, *src, *filter1, *crop, *scale, *filter2, *csp, *sink;
193 GstCaps *filter_caps = NULL;
194 GList *caps_list, *l;
196 if (!g_thread_supported ())
197 g_thread_init (NULL);
199 /* command line option parsing */
200 ctx = g_option_context_new ("");
201 g_option_context_add_group (ctx, gst_init_get_option_group ());
202 g_option_context_add_main_entries (ctx, test_goptions, NULL);
204 if (!g_option_context_parse (ctx, &argc, &argv, &opt_err)) {
205 g_error ("Error parsing command line options: %s", opt_err->message);
209 GST_DEBUG_CATEGORY_INIT (videocrop_test_debug, "videocroptest", 0, "vctest");
211 pipeline = gst_pipeline_new ("pipeline");
212 src = gst_element_factory_make ("videotestsrc", "videotestsrc");
213 g_assert (src != NULL);
214 filter1 = gst_element_factory_make ("capsfilter", "capsfilter1");
215 g_assert (filter1 != NULL);
216 crop = gst_element_factory_make ("videocrop", "videocrop");
217 g_assert (crop != NULL);
218 scale = gst_element_factory_make ("videoscale", "videoscale");
219 g_assert (scale != NULL);
220 filter2 = gst_element_factory_make ("capsfilter", "capsfilter2");
221 g_assert (filter2 != NULL);
223 if (opt_with_ffmpegcolorspace) {
224 g_print ("Adding ffmpegcolorspace\n");
225 csp = gst_element_factory_make ("ffmpegcolorspace", "colorspace");
227 csp = gst_element_factory_make ("identity", "colorspace");
229 g_assert (csp != NULL);
231 if (opt_filtercaps_str) {
232 filter_caps = gst_caps_from_string (opt_filtercaps_str);
233 if (filter_caps == NULL) {
234 g_error ("Invalid filter caps string '%s'", opt_filtercaps_str);
236 g_print ("Using filter caps '%s'\n", opt_filtercaps_str);
240 if (opt_videosink_str) {
241 g_print ("Trying videosink '%s' ...", opt_videosink_str);
242 sink = gst_element_factory_make (opt_videosink_str, "sink");
243 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
249 g_print ("Trying videosink '%s' ...", DEFAULT_VIDEOSINK);
250 sink = gst_element_factory_make (DEFAULT_VIDEOSINK, "sink");
251 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
254 g_print ("Trying videosink '%s' ...", "xvimagesink");
255 sink = gst_element_factory_make ("xvimagesink", "sink");
256 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
259 g_print ("Trying videosink '%s' ...", "ximagesink");
260 sink = gst_element_factory_make ("ximagesink", "sink");
261 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
264 g_assert (sink != NULL);
266 gst_bin_add_many (GST_BIN (pipeline), src, filter1, crop, scale, filter2,
269 if (!gst_element_link (src, filter1))
270 g_error ("Failed to link videotestsrc to capsfilter1");
272 if (!gst_element_link (filter1, crop))
273 g_error ("Failed to link capsfilter1 to videocrop");
275 if (!gst_element_link (crop, scale))
276 g_error ("Failed to link videocrop to videoscale");
278 if (!gst_element_link (scale, filter2))
279 g_error ("Failed to link videoscale to capsfilter2");
281 if (!gst_element_link (filter2, csp))
282 g_error ("Failed to link capsfilter2 to ffmpegcolorspace");
284 if (!gst_element_link (csp, sink))
285 g_error ("Failed to link ffmpegcolorspace to video sink");
287 caps_list = video_crop_get_test_caps (crop);
288 for (l = caps_list; l != NULL; l = l->next) {
289 GstStateChangeReturn ret;
290 GstCaps *caps, *out_caps;
291 gboolean skip = FALSE;
297 icaps = gst_caps_intersect (filter_caps, GST_CAPS (l->data));
298 skip = gst_caps_is_empty (icaps);
299 gst_caps_unref (icaps);
302 /* this is the size of our window (stays fixed) */
303 out_caps = gst_caps_copy (GST_CAPS (l->data));
304 gst_structure_set (gst_caps_get_structure (out_caps, 0), "width",
305 G_TYPE_INT, OUT_WIDTH, "height", G_TYPE_INT, OUT_HEIGHT, NULL);
307 g_object_set (filter2, "caps", out_caps, NULL);
309 /* filter1 gets these too to prevent videotestsrc from renegotiating */
310 g_object_set (filter1, "caps", out_caps, NULL);
311 gst_caps_unref (out_caps);
313 caps = gst_caps_copy (GST_CAPS (l->data));
314 GST_INFO ("testing format: %" GST_PTR_FORMAT, caps);
316 s = gst_caps_to_string (caps);
319 g_print ("Skipping format: %s\n", s);
324 g_print ("Format: %s\n", s);
326 caps = gst_caps_make_writable (caps);
328 /* FIXME: check return values */
329 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
330 if (ret != GST_STATE_CHANGE_FAILURE) {
331 ret = gst_element_get_state (pipeline, NULL, NULL, -1);
333 if (ret != GST_STATE_CHANGE_FAILURE) {
334 test_with_caps (src, crop, caps);
336 g_print ("Format: %s not supported (failed to go to PLAYING)\n", s);
339 g_print ("Format: %s not supported\n", s);
342 gst_element_set_state (pipeline, GST_STATE_NULL);
344 gst_caps_unref (caps);
348 g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
349 g_list_free (caps_list);
351 gst_element_set_state (pipeline, GST_STATE_NULL);
352 gst_object_unref (pipeline);