Convert %lld and %llu in printf formats to G_G[U]INT64_FORMAT. Fix pointer<->int...
[platform/upstream/gstreamer.git] / tests / lat.c
1 #include <gst/gst.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 /* FIXME: WTF does this do? */
6
7 static guint64 max = 0, min = -1, total = 0;
8 static guint count = 0;
9 static guint print_del = 1;
10 static guint iterations = 0;
11 static guint mhz = 0;
12
13 void handoff_src(GstElement *src, GstBuffer *buf, gpointer user_data) {
14   gst_trace_read_tsc(&GST_BUFFER_TIMESTAMP(buf));
15 }
16
17 void handoff_sink(GstElement *sink, GstBuffer *buf, gpointer user_data) {
18   guint64 end, d, avg;
19   guint avg_ns;
20
21   gst_trace_read_tsc(&end);
22   d = end - GST_BUFFER_TIMESTAMP(buf);
23   if (d > max) max = d;
24   if (d < min) min = d;
25   total += d;
26   count++;
27   avg = total/count;
28   avg_ns = (guint)(1000.0*(double)avg/(double)mhz);
29   
30   if ((count % print_del) == 0) {
31     g_print("%07d:%08" G_GUINT64_FORMAT " min:%08" G_GUINT64_FORMAT " max:%08" G_GUINT64_FORMAT " avg:%08" G_GUINT64_FORMAT " avg-s:0.%09d\r",
32         count, d, min, max, avg, avg_ns);
33   }
34 }
35
36 GstElement *identity_add(GstPipeline *pipeline, GstElement *first, int count) {
37   GstElement *last, *ident;
38   int i;
39   char buf[20];
40
41   last = first;
42
43   for (i=0; i<count; i++) {
44     snprintf(buf, 20, "identity_%03d", i);
45     ident = gst_element_factory_make("identity",buf);
46     g_return_val_if_fail(ident != NULL,NULL);
47     g_object_set(G_OBJECT(ident),"silent",TRUE,NULL);
48     gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(ident));
49     gst_pad_link(gst_element_get_pad(last,"src"),
50                     gst_element_get_pad(ident,"sink"));
51     last = ident;
52   }
53
54   return last;
55 }
56
57 GstElement *fakesrc(void) {
58   GstElement *src;
59
60   src = gst_element_factory_make("fakesrc","src");
61   g_return_val_if_fail(src != NULL,NULL);
62   g_object_set(G_OBJECT(src),"silent",TRUE,NULL);
63   g_object_set(G_OBJECT(src),"num_buffers",iterations,NULL);
64   g_signal_connect(G_OBJECT(src),
65       "handoff",G_CALLBACK(handoff_src),NULL);
66
67   return src;
68 }
69
70 GstElement *fakesink(void) {
71   GstElement *sink;
72
73   sink = gst_element_factory_make("fakesink","fakesink");
74   g_return_val_if_fail(sink != NULL,NULL);
75   g_object_set(G_OBJECT(sink),"silent",TRUE,NULL);
76   g_signal_connect(G_OBJECT(sink),
77       "handoff",G_CALLBACK(handoff_sink),NULL);
78
79   return sink;
80 }
81
82 GstPipeline *simple(int argc, int argi, char *argv[]) {
83   GstPipeline *pipeline;
84   GstElement *last, *src, *sink;
85   int idents;
86
87   if ((argc - argi) < 1) {
88     fprintf(stderr, "bad params");
89     return NULL;
90   }
91   idents = atoi(argv[argi]);
92   if ((argc - argi) == 2) {
93     gst_scheduler_factory_set_default_name (argv[argi+1]);
94   }
95
96   pipeline = GST_PIPELINE(gst_pipeline_new("pipeline"));
97   g_return_val_if_fail(pipeline != NULL,NULL);
98
99   src = fakesrc();
100   gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(src));
101   last = identity_add(pipeline, src, idents);
102   sink = fakesink();
103   gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(sink));
104   gst_pad_link(gst_element_get_pad(last,"src"),
105                   gst_element_get_pad(sink,"sink"));
106
107   return pipeline;
108 }
109
110 GstPipeline *queue(int argc, int argi, char *argv[]) {
111   GstPipeline *pipeline;
112   GstElement *last, *src, *sink, *src_thr, *src_q, *sink_q, *sink_thr;
113   int idents;
114
115   if ((argc - argi) < 1) {
116     fprintf(stderr, "bad params");
117     return NULL;
118   }
119   idents = atoi(argv[argi]);
120
121   if ((argc - argi) == 2) {
122     gst_scheduler_factory_set_default_name (argv[argi+1]);
123   }
124
125   pipeline = GST_PIPELINE(gst_pipeline_new("pipeline"));
126   g_return_val_if_fail(pipeline != NULL,NULL);
127
128   src_thr = GST_ELEMENT(gst_thread_new("src_thread"));
129   g_return_val_if_fail(src_thr != NULL,NULL);
130
131   src = fakesrc();
132   g_return_val_if_fail(src != NULL,NULL);
133   gst_bin_add(GST_BIN(src_thr),GST_ELEMENT(src));
134
135   src_q = gst_element_factory_make("queue","src_q");
136   g_return_val_if_fail(src_q != NULL,NULL);
137   gst_bin_add(GST_BIN(src_thr),GST_ELEMENT(src_q));
138   gst_pad_link(gst_element_get_pad(src,"src"),
139                   gst_element_get_pad(src_q,"sink"));
140
141   gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(src_thr));
142
143   last = identity_add(pipeline, src_q, idents);
144
145   sink_q = gst_element_factory_make("queue","sink_q");
146   g_return_val_if_fail(sink_q != NULL,NULL);
147   gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(sink_q));
148   gst_pad_link(gst_element_get_pad(last,"src"),
149                   gst_element_get_pad(sink_q,"sink"));
150
151   sink_thr = GST_ELEMENT(gst_thread_new("sink_thread"));
152   g_return_val_if_fail(sink_thr != NULL,NULL);
153
154   sink = fakesink();
155   g_return_val_if_fail(sink != NULL,NULL);
156   gst_bin_add(GST_BIN(sink_thr),GST_ELEMENT(sink));
157
158   gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(sink_thr));
159
160   gst_pad_link(gst_element_get_pad(sink_q,"src"),
161                   gst_element_get_pad(sink,"sink"));
162
163   return pipeline;
164 }
165
166 struct test {
167   char *name;
168   char *params;
169   GstPipeline *(*func)(int argc, int argi, char *argv[]);
170 };
171
172 static struct test tests[] = {
173   {"simple", "ident_count [scheduler_name]", simple},
174   {"queue", "ident_count [scheduler_name]", queue},
175   {NULL, NULL, NULL}
176 };
177
178 int main(int argc,char *argv[]) {
179   GstPipeline *pipeline;
180   int i;
181   char *name;
182
183   gst_init(&argc,&argv);
184
185   if (argc < 3) {
186     fprintf(stderr, "usage: %s iterations print_del mhz test_name [test_params...]\n",
187         argv[0]);
188     for (i=0; tests[i].name; i++) {
189       fprintf(stderr, "  %s %s\n", tests[i].name, tests[i].params);
190     }
191     exit(1);
192   } else {
193     iterations = atoi(argv[1]);
194     print_del = atoi(argv[2]);
195     mhz = atoi(argv[3]);
196     name = argv[4];
197   }
198
199   pipeline = NULL;
200   for (i=0; tests[i].name && !pipeline; i++) {
201     if (!strcmp(name, tests[i].name)) {
202         pipeline = tests[i].func(argc,5,argv);
203     }
204   }
205   g_return_val_if_fail(pipeline != NULL, -1);
206
207   /*xmlSaveFile("lat.gst", gst_xml_write(GST_ELEMENT(pipeline))); */
208
209   gst_element_set_state(GST_ELEMENT(pipeline),GST_STATE_PLAYING);
210
211   while (count < iterations) {
212     gst_bin_iterate(GST_BIN(pipeline));
213   }
214   g_print("\n");
215
216   return 0;
217 }