tests/benchmarks/: Set a good example and don't leak messages.
[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 #define SRC_ELEMENT "fakesrc"
26 #define SINK_ELEMENT "fakesink"
27
28
29 static GstClockTime
30 gst_get_current_time (void)
31 {
32   GTimeVal tv;
33
34   g_get_current_time (&tv);
35   return GST_TIMEVAL_TO_TIME (tv);
36 }
37
38 gint
39 main (gint argc, gchar * argv[])
40 {
41   GstMessage *msg;
42   GstElement *pipeline, *src, *e;
43   GSList *saved_src_list, *src_list, *new_src_list;
44   guint complexity_order, n_elements, i, j, max_this_level;
45   GstClockTime start, end;
46   gboolean all_srcs_linked;
47
48   gst_init (&argc, &argv);
49
50   if (argc != 3) {
51     g_print ("usage: %s COMPLEXITY_ORDER N_ELEMENTS\n", argv[0]);
52     return 1;
53   }
54
55   complexity_order = atoi (argv[1]);
56   n_elements = atoi (argv[2]);
57
58   start = gst_get_current_time ();
59
60   pipeline = gst_element_factory_make ("pipeline", NULL);
61   g_assert (pipeline);
62
63   e = gst_element_factory_make ("fakesrc", NULL);
64   g_object_set (e, "num-buffers", BUFFER_COUNT, NULL);
65   g_object_set (e, "silent", TRUE, NULL);
66   gst_bin_add (GST_BIN (pipeline), e);
67   src_list = saved_src_list = g_slist_append (NULL, e);
68
69   new_src_list = NULL;
70
71   max_this_level = 1;
72   j = 0;
73   i = 0;
74   all_srcs_linked = FALSE;
75   for (i = 0, j = 0; i < n_elements; i++, j++) {
76     if (j >= max_this_level) {
77       g_slist_free (saved_src_list);
78       saved_src_list = g_slist_reverse (new_src_list);
79       new_src_list = NULL;
80       j = 0;
81       all_srcs_linked = FALSE;
82       max_this_level *= complexity_order;
83     }
84
85     if (!src_list) {
86       if (j)
87         all_srcs_linked = TRUE;
88       src_list = saved_src_list;
89     }
90
91     src = (GstElement *) src_list->data;
92     src_list = src_list->next;
93
94     if (i + max_this_level < n_elements) {
95       e = gst_element_factory_make ("tee", NULL);
96     } else {
97       e = gst_element_factory_make ("fakesink", NULL);
98       g_object_set (e, "preroll-queue-len", 1, NULL);
99     }
100     g_object_set (e, "silent", TRUE, NULL);
101     new_src_list = g_slist_prepend (new_src_list, e);
102
103     gst_bin_add (GST_BIN (pipeline), e);
104     if (!gst_element_link (src, e))
105       g_assert_not_reached ();
106   }
107
108   g_slist_free (saved_src_list);
109   g_slist_free (new_src_list);
110
111   end = gst_get_current_time ();
112   g_print ("%" GST_TIME_FORMAT " - creating and linking %d elements\n",
113       GST_TIME_ARGS (end - start), i);
114
115   start = gst_get_current_time ();
116   if (gst_element_set_state (pipeline,
117           GST_STATE_PLAYING) != GST_STATE_CHANGE_SUCCESS)
118     g_assert_not_reached ();
119   end = gst_get_current_time ();
120   g_print ("%" GST_TIME_FORMAT " - setting pipeline to playing\n",
121       GST_TIME_ARGS (end - start));
122
123   start = gst_get_current_time ();
124   msg = gst_bus_poll (gst_element_get_bus (pipeline),
125       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
126   end = gst_get_current_time ();
127   gst_message_unref (msg);
128   g_print ("%" GST_TIME_FORMAT " - putting %u buffers through\n",
129       GST_TIME_ARGS (end - start), BUFFER_COUNT);
130
131   start = gst_get_current_time ();
132   g_object_unref (pipeline);
133   end = gst_get_current_time ();
134   g_print ("%" GST_TIME_FORMAT " - unreffing pipeline\n",
135       GST_TIME_ARGS (end - start));
136
137   return 0;
138 }