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