2 * Copyright (C) 2010 Stefan Kost <ensonic@users.sf.net>
4 * capsnego.c: benchmark for caps negotiation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 /* This benchmark recursively builds a pipeline and measures the time to go
23 * from READY to PAUSED state.
25 * The graph size and type can be controlled with a few command line options:
27 * -d depth: is the depth of the tree
28 * -c children: is the number of branches on each level
29 * -f <flavour>: can be "audio" or "video" and is controlling the kind of
30 * elements that are used.
53 static const gchar *factories[NUM_FLAVOURS][NUM_ELEM] = {
54 {"audiotestsrc", "adder", "volume", "audioconvert"},
55 {"videotestsrc", "videomixer", "videoscale", "videoconvert"}
58 static const gchar *sink_pads[NUM_FLAVOURS][NUM_ELEM] = {
59 {NULL, "sink_%u", NULL, NULL},
60 {NULL, "sink_%u", NULL, NULL}
65 create_node (GstBin * bin, GstElement * sink, const gchar * sinkpadname,
66 GstElement ** new_sink, gint children, gint flavour)
68 GstElement *mix, *proc, *conv;
71 mix = gst_element_factory_make (factories[flavour][ELEM_MIX], NULL);
73 GST_WARNING ("need element '%s'", factories[flavour][ELEM_MIX]);
77 mix = gst_element_factory_make ("identity", NULL);
79 proc = gst_element_factory_make (factories[flavour][ELEM_PROC], NULL);
81 GST_WARNING ("need element '%s'", factories[flavour][ELEM_PROC]);
84 conv = gst_element_factory_make (factories[flavour][ELEM_CONV], NULL);
86 GST_WARNING ("need element '%s'", factories[flavour][ELEM_CONV]);
89 gst_bin_add_many (bin, mix, proc, conv, NULL);
90 if (!gst_element_link_pads_full (mix, "src", proc, "sink",
91 GST_PAD_LINK_CHECK_NOTHING)
92 || !gst_element_link_pads_full (proc, "src", conv, "sink",
93 GST_PAD_LINK_CHECK_NOTHING)
94 || !gst_element_link_pads_full (conv, "src", sink, sinkpadname,
95 GST_PAD_LINK_CHECK_NOTHING)) {
96 GST_WARNING ("can't link elements");
104 create_nodes (GstBin * bin, GstElement * sink, gint depth, gint children,
107 GstElement *new_sink, *src;
110 for (i = 0; i < children; i++) {
112 if (!create_node (bin, sink, sink_pads[flavour][ELEM_MIX], &new_sink,
113 children, flavour)) {
116 if (!create_nodes (bin, new_sink, depth - 1, children, flavour)) {
120 src = gst_element_factory_make (factories[flavour][ELEM_SRC], NULL);
122 GST_WARNING ("need element '%s'", factories[flavour][ELEM_SRC]);
125 gst_bin_add (bin, src);
126 if (!gst_element_link_pads_full (src, "src", sink,
127 sink_pads[flavour][ELEM_MIX], GST_PAD_LINK_CHECK_NOTHING)) {
128 GST_WARNING ("can't link elements");
137 event_loop (GstElement * bin)
140 GstMessage *msg = NULL;
141 gboolean running = TRUE;
143 bus = gst_element_get_bus (bin);
146 msg = gst_bus_poll (bus,
147 GST_MESSAGE_ASYNC_DONE | GST_MESSAGE_ERROR | GST_MESSAGE_WARNING, -1);
149 switch (GST_MESSAGE_TYPE (msg)) {
150 case GST_MESSAGE_ASYNC_DONE:
153 case GST_MESSAGE_WARNING:{
157 gst_message_parse_warning (msg, &err, &dbg);
158 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg), "%s (%s)", err->message,
159 (dbg ? dbg : "no details"));
160 g_clear_error (&err);
164 case GST_MESSAGE_ERROR:{
168 gst_message_parse_error (msg, &err, &dbg);
169 GST_ERROR_OBJECT (GST_MESSAGE_SRC (msg), "%s (%s)", err->message,
170 (dbg ? dbg : "no details"));
171 g_clear_error (&err);
179 gst_message_unref (msg);
181 gst_object_unref (bus);
185 main (gint argc, gchar * argv[])
187 /* default parameters */
188 gchar *flavour_str = g_strdup("audio");
189 gint flavour = FLAVOUR_AUDIO;
195 GOptionEntry options[] = {
196 {"children", 'c', 0, G_OPTION_ARG_INT, &children,
197 "Number of children (branches on each level) (default: 3)", NULL},
198 {"depth", 'd', 0, G_OPTION_ARG_INT, &depth,
199 "Depth of pipeline hierarchy tree (default: 4)", NULL},
200 {"flavour", 'f', 0, G_OPTION_ARG_STRING, &flavour_str,
201 "Flavour (video|audio) controlling the kind of elements used "
202 "(default: audio)", NULL},
203 {"loops", 'l', 0, G_OPTION_ARG_INT, &loops,
204 "How many loops to run (default: 50)", NULL},
209 GstClockTime start, end;
210 GstElement *sink, *new_sink;
213 g_set_prgname ("capsnego");
215 /* check command line options */
216 ctx = g_option_context_new ("");
217 g_option_context_add_main_entries (ctx, options, NULL);
218 g_option_context_add_group (ctx, gst_init_get_option_group ());
219 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
220 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
221 g_clear_error (&err);
222 g_option_context_free (ctx);
225 g_option_context_free (ctx);
227 if (strcmp (flavour_str, "video") == 0)
228 flavour = FLAVOUR_VIDEO;
231 g_print ("building %s pipeline with depth = %d and children = %d\n",
232 flavour_str, depth, children);
233 g_free (flavour_str);
235 start = gst_util_get_timestamp ();
236 bin = GST_BIN (gst_pipeline_new ("pipeline"));
237 sink = gst_element_factory_make ("fakesink", NULL);
238 gst_bin_add (bin, sink);
239 if (!create_node (bin, sink, "sink", &new_sink, children, flavour)) {
242 if (!create_nodes (bin, new_sink, depth, children, flavour)) {
245 end = gst_util_get_timestamp ();
246 /* num-threads = num-sources = pow (children, depth) */
247 g_print ("%" GST_TIME_FORMAT " built pipeline with %d elements\n",
248 GST_TIME_ARGS (end - start), GST_BIN_NUMCHILDREN (bin));
251 g_print ("starting pipeline\n");
252 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
253 GST_DEBUG_BIN_TO_DOT_FILE (bin, GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE, "capsnego");
255 start = gst_util_get_timestamp ();
256 for (i = 0; i < loops; ++i) {
257 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
258 event_loop (GST_ELEMENT (bin));
259 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
261 end = gst_util_get_timestamp ();
262 g_print ("%" GST_TIME_FORMAT " reached PAUSED state (%d loop iterations)\n",
263 GST_TIME_ARGS (end - start), loops);
266 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
267 gst_object_unref (bin);