tee: Check for the removed pad flag also in the slow pushing path
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include <stdio.h>
23
24 #include <gst/gst.h>
25 #include <gst/controller/gstinterpolationcontrolsource.h>
26 #include <gst/controller/gstdirectcontrolbinding.h>
27
28 /* a song in buzztard can easily reach 30000 here */
29 #define NUM_CP 15000
30 #define BLOCK_SIZE 64
31
32 static void
33 event_loop (GstElement * pipe)
34 {
35   GstBus *bus;
36   GstMessage *message = NULL;
37   gboolean running = TRUE;
38
39   bus = gst_element_get_bus (GST_ELEMENT (pipe));
40
41   while (running) {
42     message = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
43
44     g_assert (message != NULL);
45
46     switch (message->type) {
47       case GST_MESSAGE_EOS:
48         running = FALSE;
49         break;
50       case GST_MESSAGE_WARNING:{
51         GError *gerror;
52         gchar *debug;
53
54         gst_message_parse_warning (message, &gerror, &debug);
55         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
56         g_error_free (gerror);
57         g_free (debug);
58         break;
59       }
60       case GST_MESSAGE_ERROR:{
61         GError *gerror;
62         gchar *debug;
63
64         gst_message_parse_error (message, &gerror, &debug);
65         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
66         g_error_free (gerror);
67         g_free (debug);
68         running = FALSE;
69         break;
70       }
71       default:
72         break;
73     }
74     gst_message_unref (message);
75   }
76   gst_object_unref (bus);
77 }
78
79 gint
80 main (gint argc, gchar * argv[])
81 {
82   gint res = 1;
83   gint i, j;
84   GstElement *src, *sink;
85   GstElement *bin;
86   GstControlSource *cs;
87   GstTimedValueControlSource *tvcs;
88   GstClockTime bt, ct;
89   GstClockTimeDiff elapsed;
90   GstClockTime tick;
91
92   gst_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   /* create and configure control source */
115   cs = gst_interpolation_control_source_new ();
116   tvcs = (GstTimedValueControlSource *) cs;
117
118   gst_object_add_control_binding (GST_OBJECT (src),
119       gst_direct_control_binding_new (GST_OBJECT (src), "freq", cs));
120   g_object_set (cs, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
121
122   /* set control values, we set them in a linear order as we would when loading
123    * a stored project
124    */
125   bt = gst_util_get_timestamp ();
126
127   for (i = 0; i < NUM_CP; i++) {
128     gst_timed_value_control_source_set (tvcs, i * tick,
129         g_random_double_range (50.0, 3000.0));
130   }
131
132   ct = gst_util_get_timestamp ();
133   elapsed = GST_CLOCK_DIFF (bt, ct);
134   printf ("linear insert of control-points: %" GST_TIME_FORMAT "\n",
135       GST_TIME_ARGS (elapsed));
136
137
138   /* set extra control values, we set them in arbitrary order to simulate
139    * the user editing a project from the ui
140    */
141   bt = gst_util_get_timestamp ();
142
143   for (i = 0; i < 100; i++) {
144     j = g_random_int_range (0, NUM_CP - 1);
145     gst_timed_value_control_source_set (tvcs, j * tick,
146         g_random_double_range (50.0, 3000.0));
147   }
148
149   ct = gst_util_get_timestamp ();
150   elapsed = GST_CLOCK_DIFF (bt, ct);
151   printf ("random insert of control-points: %" GST_TIME_FORMAT "\n",
152       GST_TIME_ARGS (elapsed));
153
154   {
155     GstClockTime sample_duration =
156         gst_util_uint64_scale_int (1, GST_SECOND, 44100);
157     gdouble *values = g_new0 (gdouble, BLOCK_SIZE * NUM_CP);
158
159     bt = gst_util_get_timestamp ();
160     gst_control_source_get_value_array (cs, 0, sample_duration,
161         BLOCK_SIZE * NUM_CP, values);
162     ct = gst_util_get_timestamp ();
163     g_free (values);
164     elapsed = GST_CLOCK_DIFF (bt, ct);
165     printf ("linear array for control-points: %" GST_TIME_FORMAT "\n",
166         GST_TIME_ARGS (elapsed));
167   }
168
169   /* play, this test sequential reads */
170   bt = gst_util_get_timestamp ();
171
172   if (gst_element_set_state (bin, GST_STATE_PLAYING)) {
173     /* wait for EOS */
174     event_loop (bin);
175     gst_element_set_state (bin, GST_STATE_NULL);
176   }
177
178   ct = gst_util_get_timestamp ();
179   elapsed = GST_CLOCK_DIFF (bt, ct);
180   printf ("linear read of control-points  : %" GST_TIME_FORMAT "\n",
181       GST_TIME_ARGS (elapsed));
182
183   /* cleanup */
184   gst_object_unref (cs);
185   gst_object_unref (bin);
186   res = 0;
187 Error:
188   return res;
189 }