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