Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / tests / benchmarks / controller.c
1 /* GStreamer
2  * Copyright (C) 2009 Stefan Kost <ensonic@users.sf.net>
3  *
4  * controller.c: benchmark for interpolation control-source
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 #include <gst/gst.h>
23 #include <gst/controller/gstcontroller.h>
24 #include <gst/controller/gstinterpolationcontrolsource.h>
25
26 /* a song in buzztard can easily reach 30000 here */
27 #define NUM_CP 15000
28 #define BLOCK_SIZE 64
29
30 static void
31 event_loop (GstElement * pipe)
32 {
33   GstBus *bus;
34   GstMessage *message = NULL;
35   gboolean running = TRUE;
36
37   bus = gst_element_get_bus (GST_ELEMENT (pipe));
38
39   while (running) {
40     message = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
41
42     g_assert (message != NULL);
43
44     switch (message->type) {
45       case GST_MESSAGE_EOS:
46         running = FALSE;
47         break;
48       case GST_MESSAGE_WARNING:{
49         GError *gerror;
50         gchar *debug;
51
52         gst_message_parse_warning (message, &gerror, &debug);
53         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
54         g_error_free (gerror);
55         g_free (debug);
56         break;
57       }
58       case GST_MESSAGE_ERROR:{
59         GError *gerror;
60         gchar *debug;
61
62         gst_message_parse_error (message, &gerror, &debug);
63         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
64         g_error_free (gerror);
65         g_free (debug);
66         running = FALSE;
67         break;
68       }
69       default:
70         break;
71     }
72     gst_message_unref (message);
73   }
74   gst_object_unref (bus);
75 }
76
77 gint
78 main (gint argc, gchar * argv[])
79 {
80   gint res = 1;
81   gint i, j;
82   GstElement *src, *sink;
83   GstElement *bin;
84   GstController *ctrl;
85   GstInterpolationControlSource *csource;
86   GValue freq = { 0, };
87   GstClockTime bt, ct;
88   GstClockTimeDiff elapsed;
89   GstClockTime tick;
90
91   gst_init (&argc, &argv);
92   gst_controller_init (&argc, &argv);
93
94   /* build pipeline */
95   bin = gst_pipeline_new ("pipeline");
96   src = gst_element_factory_make ("audiotestsrc", "gen_audio");
97   if (!src) {
98     GST_WARNING ("need audiotestsrc from gst-plugins-base");
99     goto Error;
100   }
101   sink = gst_element_factory_make ("fakesink", "swallow_audio");
102
103   gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
104   if (!gst_element_link (src, sink)) {
105     GST_WARNING ("can't link elements");
106     goto Error;
107   }
108
109   g_object_set (G_OBJECT (src), "wave", 7,      /* sine table - we don't want to benchmark the fpu */
110       "num-buffers", NUM_CP, "samplesperbuffer", BLOCK_SIZE, NULL);
111
112   tick = BLOCK_SIZE * GST_SECOND / 44100;
113
114   /* add a controller to the source */
115   if (!(ctrl = gst_controller_new (G_OBJECT (src), "freq", NULL))) {
116     GST_WARNING ("can't control source element");
117     goto Error;
118   }
119
120   /* create and configure control source */
121   csource = gst_interpolation_control_source_new ();
122   gst_controller_set_control_source (ctrl, "freq",
123       GST_CONTROL_SOURCE (csource));
124   gst_interpolation_control_source_set_interpolation_mode (csource,
125       GST_INTERPOLATE_LINEAR);
126   g_value_init (&freq, G_TYPE_DOUBLE);
127
128
129   /* set control values, we set them in a linear order as we would when loading
130    * a stored project
131    */
132   bt = gst_util_get_timestamp ();
133
134   for (i = 0; i < NUM_CP; i++) {
135     g_value_set_double (&freq, g_random_double_range (50.0, 3000.0));
136     gst_interpolation_control_source_set (csource, i * tick, &freq);
137   }
138
139   ct = gst_util_get_timestamp ();
140   elapsed = GST_CLOCK_DIFF (bt, ct);
141   printf ("linear insert of control-points: %" GST_TIME_FORMAT "\n",
142       GST_TIME_ARGS (elapsed));
143
144
145   /* set extra control values, we set them in arbitrary order to simulate
146    * the user editing a project from the ui
147    */
148   bt = gst_util_get_timestamp ();
149
150   for (i = 0; i < 100; i++) {
151     j = g_random_int_range (0, NUM_CP - 1);
152     g_value_set_double (&freq, g_random_double_range (50.0, 3000.0));
153     gst_interpolation_control_source_set (csource, j * tick, &freq);
154   }
155
156   ct = gst_util_get_timestamp ();
157   elapsed = GST_CLOCK_DIFF (bt, ct);
158   printf ("random insert of control-points: %" GST_TIME_FORMAT "\n",
159       GST_TIME_ARGS (elapsed));
160
161   {
162     GstClockTime sample_duration =
163         gst_util_uint64_scale_int (1, GST_SECOND, 44100);
164     GstValueArray va = { "freq",
165       BLOCK_SIZE * NUM_CP,
166       sample_duration,
167       NULL
168     };
169
170     gdouble *values = g_new0 (gdouble, BLOCK_SIZE * NUM_CP);
171     va.values = (gpointer *) values;
172
173     bt = gst_util_get_timestamp ();
174     gst_control_source_get_value_array (GST_CONTROL_SOURCE (csource), 0, &va);
175     ct = gst_util_get_timestamp ();
176     g_free (values);
177     elapsed = GST_CLOCK_DIFF (bt, ct);
178     printf ("linear array for control-points: %" GST_TIME_FORMAT "\n",
179         GST_TIME_ARGS (elapsed));
180   }
181
182   g_object_unref (csource);
183
184   /* play, this test sequential reads */
185   bt = gst_util_get_timestamp ();
186
187   if (gst_element_set_state (bin, GST_STATE_PLAYING)) {
188     /* wait for EOS */
189     event_loop (bin);
190     gst_element_set_state (bin, GST_STATE_NULL);
191   }
192
193   ct = gst_util_get_timestamp ();
194   elapsed = GST_CLOCK_DIFF (bt, ct);
195   printf ("linear read of control-points  : %" GST_TIME_FORMAT "\n",
196       GST_TIME_ARGS (elapsed));
197
198   /* cleanup */
199   g_object_unref (G_OBJECT (ctrl));
200   gst_object_unref (G_OBJECT (bin));
201   res = 0;
202 Error:
203   return res;
204 }