Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / tests / benchmarks / 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 Library 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  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <stdlib.h>
22 #include <gst/gst.h>
23
24 #define BUFFER_COUNT (1000)
25
26 gint
27 main (gint argc, gchar * argv[])
28 {
29   GstMessage *msg;
30   GstElement *pipeline, *src, *e;
31   GSList *saved_src_list, *src_list, *new_src_list;
32   guint complexity_order, n_elements, i, j, max_this_level;
33   GstClockTime start, end;
34
35   gst_init (&argc, &argv);
36
37   if (argc != 3) {
38     g_print ("usage: %s COMPLEXITY_ORDER N_ELEMENTS\n", argv[0]);
39     return 1;
40   }
41
42   complexity_order = atoi (argv[1]);
43   n_elements = atoi (argv[2]);
44
45   start = gst_util_get_timestamp ();
46
47   pipeline = gst_element_factory_make ("pipeline", NULL);
48   g_assert (pipeline);
49
50   e = gst_element_factory_make ("fakesrc", NULL);
51   g_object_set (e, "num-buffers", BUFFER_COUNT, NULL);
52   g_object_set (e, "silent", TRUE, NULL);
53   gst_bin_add (GST_BIN (pipeline), e);
54   src_list = saved_src_list = g_slist_append (NULL, e);
55
56   new_src_list = NULL;
57
58   max_this_level = 1;
59   for (i = 0, j = 0; i < n_elements; i++, j++) {
60     if (j >= max_this_level) {
61       g_slist_free (saved_src_list);
62       saved_src_list = g_slist_reverse (new_src_list);
63       new_src_list = NULL;
64       j = 0;
65       max_this_level *= complexity_order;
66     }
67
68     if (!src_list) {
69       src_list = saved_src_list;
70     }
71
72     src = (GstElement *) src_list->data;
73     src_list = src_list->next;
74
75     if (i + max_this_level < n_elements) {
76       e = gst_element_factory_make ("tee", NULL);
77     } else {
78       e = gst_element_factory_make ("fakesink", NULL);
79       g_object_set (e, "preroll-queue-len", 1, NULL);
80     }
81     g_object_set (e, "silent", TRUE, NULL);
82     new_src_list = g_slist_prepend (new_src_list, e);
83
84     gst_bin_add (GST_BIN (pipeline), e);
85     if (!gst_element_link (src, e))
86       g_assert_not_reached ();
87   }
88
89   g_slist_free (saved_src_list);
90   g_slist_free (new_src_list);
91
92   end = gst_util_get_timestamp ();
93   g_print ("%" GST_TIME_FORMAT " - creating and linking %d elements\n",
94       GST_TIME_ARGS (end - start), i);
95
96   start = gst_util_get_timestamp ();
97   if (gst_element_set_state (pipeline,
98           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
99     g_assert_not_reached ();
100   if (gst_element_get_state (pipeline, NULL, NULL,
101           GST_CLOCK_TIME_NONE) == GST_STATE_CHANGE_FAILURE)
102     g_assert_not_reached ();
103   end = gst_util_get_timestamp ();
104   g_print ("%" GST_TIME_FORMAT " - setting pipeline to playing\n",
105       GST_TIME_ARGS (end - start));
106
107   start = gst_util_get_timestamp ();
108   msg = gst_bus_poll (gst_element_get_bus (pipeline),
109       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
110   end = gst_util_get_timestamp ();
111   gst_message_unref (msg);
112   g_print ("%" GST_TIME_FORMAT " - putting %u buffers through\n",
113       GST_TIME_ARGS (end - start), BUFFER_COUNT);
114
115   start = gst_util_get_timestamp ();
116   if (gst_element_set_state (pipeline,
117           GST_STATE_NULL) != GST_STATE_CHANGE_SUCCESS)
118     g_assert_not_reached ();
119   end = gst_util_get_timestamp ();
120   g_print ("%" GST_TIME_FORMAT " - setting pipeline to NULL\n",
121       GST_TIME_ARGS (end - start));
122
123   start = gst_util_get_timestamp ();
124   g_object_unref (pipeline);
125   end = gst_util_get_timestamp ();
126   g_print ("%" GST_TIME_FORMAT " - unreffing pipeline\n",
127       GST_TIME_ARGS (end - start));
128
129   return 0;
130 }