benchmarks: add a benchmark for capsnegotiation
[platform/upstream/gstreamer.git] / tests / benchmarks / capsnego.c
1 /* GStreamer
2  * Copyright (C) 2010 Stefan Kost <ensonic@users.sf.net>
3  *
4  * capsnego.c: benchmark for caps negotiation
5  *
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #include <gst/gst.h>
24
25 /* the code below recursively builds a pipeline, the GRAPH_DEPTH is the depth
26  * of the tree, NUM_CHILDREN is the number of branches on each level
27  */
28 #define GRAPH_DEPTH 4
29 #define NUM_CHILDREN 3
30
31 static gboolean
32 create_node (GstBin * bin, GstElement * sink, GstElement ** adder,
33     GstElement ** vol, GstElement ** ac)
34 {
35
36   *adder = gst_element_factory_make ("adder", NULL);
37   if (!*adder) {
38     GST_WARNING ("need adder from gst-plugins-base");
39     return FALSE;
40   }
41   *vol = gst_element_factory_make ("volume", NULL);
42   if (!*vol) {
43     GST_WARNING ("need volume from gst-plugins-base");
44     return FALSE;
45   }
46   *ac = gst_element_factory_make ("audioconvert", NULL);
47   if (!*ac) {
48     GST_WARNING ("need audioconvert from gst-plugins-base");
49     return FALSE;
50   }
51   gst_bin_add_many (bin, *adder, *vol, *ac, NULL);
52   if (!gst_element_link_many (*adder, *vol, *ac, sink, NULL)) {
53     GST_WARNING ("can't link elements");
54     return FALSE;
55   }
56   return TRUE;
57 }
58
59 static gboolean
60 create_nodes (GstBin * bin, GstElement * sink, gint depth)
61 {
62   GstElement *adder, *vol, *ac, *src;
63   gint i;
64
65   for (i = 0; i < NUM_CHILDREN; i++) {
66     if (depth > 0) {
67       if (!create_node (bin, sink, &adder, &vol, &ac)) {
68         return FALSE;
69       }
70       if (!create_nodes (bin, adder, depth - 1)) {
71         return FALSE;
72       }
73     } else {
74       src = gst_element_factory_make ("audiotestsrc", NULL);
75       if (!src) {
76         GST_WARNING ("need audiotestsrc from gst-plugins-base");
77         return FALSE;
78       }
79       gst_bin_add (bin, src);
80       if (!gst_element_link (src, sink)) {
81         GST_WARNING ("can't link elements");
82         return FALSE;
83       }
84     }
85   }
86   return TRUE;
87 }
88
89 static void
90 event_loop (GstElement * bin)
91 {
92   GstBus *bus;
93   GstMessage *msg = NULL;
94   gboolean running = TRUE;
95
96   bus = gst_element_get_bus (bin);
97
98   while (running) {
99     msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
100
101     if (GST_MESSAGE_SRC (msg) == (GstObject *) bin) {
102       GstState old_state, new_state;
103
104       gst_message_parse_state_changed (msg, &old_state, &new_state, NULL);
105       if (old_state == GST_STATE_READY && new_state == GST_STATE_PAUSED) {
106         running = FALSE;
107       }
108     }
109     gst_message_unref (msg);
110   }
111 }
112
113
114 gint
115 main (gint argc, gchar * argv[])
116 {
117   GstBin *bin;
118   GstClockTime start, end;
119   GstElement *sink;
120   GstElement *adder, *vol, *ac;
121
122   gst_init (&argc, &argv);
123
124   bin = GST_BIN (gst_pipeline_new ("pipeline"));
125
126   g_print ("building pipeline\n");
127   sink = gst_element_factory_make ("fakesink", NULL);
128   gst_bin_add (bin, sink);
129   if (!create_node (bin, sink, &adder, &vol, &ac)) {
130     goto Error;
131   }
132   if (!create_nodes (bin, adder, GRAPH_DEPTH)) {
133     goto Error;
134   }
135   g_print ("built pipeline with %d elements\n", GST_BIN_NUMCHILDREN (bin));
136
137   g_print ("starting pipeline\n");
138   gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
139   GST_DEBUG_BIN_TO_DOT_FILE (bin, GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE, "capsnego");
140   start = gst_util_get_timestamp ();
141   gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
142   event_loop (GST_ELEMENT (bin));
143   end = gst_util_get_timestamp ();
144   g_print ("%" GST_TIME_FORMAT " reached paused\n",
145       GST_TIME_ARGS (end - start));
146
147 Error:
148   gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
149   gst_object_unref (bin);
150   return 0;
151 }