check/gst/gstevents.c: Provide more error margin in clock measurements to allow for...
[platform/upstream/gstreamer.git] / tests / complexity.c
1 /*
2  * Copyright (C) 2004 Benjamin Otte <otte@gnome.org>
3  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <gst/gst.h>
21
22 #define BUFFER_COUNT (1000)
23 #define SRC_ELEMENT "fakesrc"
24 #define SINK_ELEMENT "fakesink"
25
26
27 static GstClockTime
28 gst_get_current_time (void)
29 {
30   GTimeVal tv;
31
32   g_get_current_time (&tv);
33   return GST_TIMEVAL_TO_TIME (tv);
34 }
35
36 gint
37 main (gint argc, gchar * argv[])
38 {
39   GstElement *pipeline, *src, *e;
40   GSList *saved_src_list, *src_list, *new_src_list;
41   guint complexity_order, n_elements, i, j, max_this_level;
42   GstClockTime start, end;
43   gboolean all_srcs_linked;
44
45   gst_init (&argc, &argv);
46
47   if (argc != 3) {
48     g_print ("usage: %s COMPLEXITY_ORDER N_ELEMENTS\n", argv[0]);
49     return 1;
50   }
51
52   complexity_order = atoi (argv[1]);
53   n_elements = atoi (argv[2]);
54
55   start = gst_get_current_time ();
56
57   pipeline = gst_element_factory_make ("pipeline", NULL);
58   g_assert (pipeline);
59
60   e = gst_element_factory_make ("fakesrc", NULL);
61   g_object_set (e, "num-buffers", BUFFER_COUNT, NULL);
62   g_object_set (e, "silent", TRUE, NULL);
63   gst_bin_add (GST_BIN (pipeline), e);
64   src_list = saved_src_list = g_slist_append (NULL, e);
65
66   new_src_list = NULL;
67
68   max_this_level = 1;
69   j = 0;
70   i = 0;
71   all_srcs_linked = FALSE;
72   for (i = 0, j = 0; i < n_elements; i++, j++) {
73     if (j >= max_this_level) {
74       g_slist_free (saved_src_list);
75       saved_src_list = g_slist_reverse (new_src_list);
76       new_src_list = NULL;
77       j = 0;
78       all_srcs_linked = FALSE;
79       max_this_level *= complexity_order;
80     }
81
82     if (!src_list) {
83       if (j)
84         all_srcs_linked = TRUE;
85       src_list = saved_src_list;
86     }
87
88     src = (GstElement *) src_list->data;
89     src_list = src_list->next;
90
91     if (i + max_this_level < n_elements) {
92       e = gst_element_factory_make ("tee", NULL);
93     } else {
94       e = gst_element_factory_make ("fakesink", NULL);
95       g_object_set (e, "preroll-queue-len", 1, NULL);
96     }
97     g_object_set (e, "silent", TRUE, NULL);
98     new_src_list = g_slist_prepend (new_src_list, e);
99
100     gst_bin_add (GST_BIN (pipeline), e);
101     if (!gst_element_link (src, e))
102       g_assert_not_reached ();
103   }
104
105   g_slist_free (saved_src_list);
106   g_slist_free (new_src_list);
107
108   end = gst_get_current_time ();
109   g_print ("%" GST_TIME_FORMAT " - creating and linking %d elements\n",
110       GST_TIME_ARGS (end - start), i);
111
112   start = gst_get_current_time ();
113   if (gst_element_set_state (pipeline, GST_STATE_PLAYING) != GST_STATE_SUCCESS)
114     g_assert_not_reached ();
115   end = gst_get_current_time ();
116   g_print ("%" GST_TIME_FORMAT " - setting pipeline to playing\n",
117       GST_TIME_ARGS (end - start));
118
119   start = gst_get_current_time ();
120   gst_bus_poll (gst_element_get_bus (pipeline),
121       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
122   end = gst_get_current_time ();
123   g_print ("%" GST_TIME_FORMAT " - putting %u buffers through\n",
124       GST_TIME_ARGS (end - start), BUFFER_COUNT);
125
126   start = gst_get_current_time ();
127   g_object_unref (pipeline);
128   end = gst_get_current_time ();
129   g_print ("%" GST_TIME_FORMAT " - unreffing pipeline\n",
130       GST_TIME_ARGS (end - start));
131
132   return 0;
133 }