688727c123d2e9e8d5b9598749f880fae724fafa
[platform/upstream/gstreamer.git] / plugins / elements / gststatistics.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gststatistics.c: 
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "gststatistics.h"
29
30 GST_DEBUG_CATEGORY_STATIC (gst_statistics_debug);
31 #define GST_CAT_DEFAULT gst_statistics_debug
32
33 GstElementDetails gst_statistics_details = GST_ELEMENT_DETAILS (
34   "Statistics",
35   "Generic",
36   "Statistics on buffers/bytes/events",
37   "David I. Lehn <dlehn@users.sourceforge.net>"
38 );
39
40
41 /* Statistics signals and args */
42 enum {
43   SIGNAL_UPDATE,
44   LAST_SIGNAL
45 };
46
47 enum {
48   ARG_0,
49   ARG_BUFFERS,
50   ARG_BYTES,
51   ARG_EVENTS,
52   ARG_BUFFER_UPDATE_FREQ,
53   ARG_BYTES_UPDATE_FREQ,
54   ARG_EVENT_UPDATE_FREQ,
55   ARG_UPDATE_ON_EOS,
56   ARG_UPDATE,
57   ARG_SILENT,
58 };
59
60
61 #define _do_init(bla) \
62     GST_DEBUG_CATEGORY_INIT (gst_statistics_debug, "statistics", 0, "statistics element");
63
64 GST_BOILERPLATE_FULL (GstStatistics, gst_statistics, GstElement, GST_TYPE_ELEMENT, _do_init);
65
66 static void gst_statistics_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
67 static void gst_statistics_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
68
69 static void gst_statistics_chain                (GstPad *pad, GstData *_data);
70 static void gst_statistics_reset                (GstStatistics *statistics);
71 static void gst_statistics_print                (GstStatistics *statistics);
72
73 static guint gst_statistics_signals[LAST_SIGNAL] = { 0, };
74
75 static stats zero_stats = { 0, };
76
77
78 static void
79 gst_statistics_base_init (gpointer g_class)
80 {
81   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
82   
83   gst_element_class_set_details (gstelement_class, &gst_statistics_details);
84 }
85 static void 
86 gst_statistics_class_init (GstStatisticsClass *klass) 
87 {
88   GObjectClass *gobject_class;
89   
90   gobject_class = G_OBJECT_CLASS (klass);
91
92
93   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFERS,
94     g_param_spec_int64 ("buffers", "buffers", "total buffers count",
95                           0, G_MAXINT64, 0, G_PARAM_READABLE));
96   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BYTES,
97     g_param_spec_int64 ("bytes", "bytes", "total bytes count",
98                           0, G_MAXINT64, 0, G_PARAM_READABLE));
99   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EVENTS,
100     g_param_spec_int64 ("events", "events", "total event count",
101                           0, G_MAXINT64, 0, G_PARAM_READABLE));
102   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_UPDATE_FREQ,
103     g_param_spec_int64 ("buffer_update_freq", "buffer update freq", "buffer update frequency",
104                           0, G_MAXINT64, 0, G_PARAM_READWRITE));
105   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BYTES_UPDATE_FREQ,
106     g_param_spec_int64 ("bytes_update_freq", "bytes update freq", "bytes update frequency",
107                           0, G_MAXINT64, 0, G_PARAM_READWRITE));
108   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EVENT_UPDATE_FREQ,
109     g_param_spec_int64 ("event_update_freq", "event update freq", "event update frequency",
110                           0, G_MAXINT64, 0, G_PARAM_READWRITE));
111   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_UPDATE_ON_EOS,
112     g_param_spec_boolean ("update_on_eos", "update on EOS", "update on EOS event",
113                           TRUE,G_PARAM_READWRITE)); 
114   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_UPDATE,
115     g_param_spec_boolean ("update", "update", "update",
116                           TRUE,G_PARAM_READWRITE)); 
117   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
118     g_param_spec_boolean ("silent", "silent", "silent",
119                           TRUE,G_PARAM_READWRITE)); 
120
121   gst_statistics_signals[SIGNAL_UPDATE] =
122     g_signal_new ("update", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
123                    G_STRUCT_OFFSET (GstStatisticsClass, update), NULL, NULL,
124                    g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
125
126   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_statistics_set_property);
127   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_statistics_get_property);
128 }
129
130 static void 
131 gst_statistics_init (GstStatistics *statistics) 
132 {
133   statistics->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
134   gst_element_add_pad (GST_ELEMENT (statistics), statistics->sinkpad);
135   gst_pad_set_chain_function (statistics->sinkpad, GST_DEBUG_FUNCPTR (gst_statistics_chain));
136   
137   statistics->srcpad = gst_pad_new ("src", GST_PAD_SRC);
138   gst_element_add_pad (GST_ELEMENT (statistics), statistics->srcpad);
139
140   statistics->timer = NULL;
141   statistics->last_timer = NULL;
142   gst_statistics_reset(statistics);
143 }
144
145 static void
146 gst_statistics_reset (GstStatistics *statistics)
147 {
148   g_return_if_fail (statistics != NULL);
149   g_return_if_fail (GST_IS_STATISTICS (statistics));
150
151   statistics->stats.buffers = 0;
152   statistics->stats.bytes = 0;
153   statistics->stats.events = 0;
154
155   statistics->last_stats.buffers = 0;
156   statistics->last_stats.bytes = 0;
157   statistics->last_stats.events = 0;
158
159   statistics->update_count.buffers = 0;
160   statistics->update_count.bytes = 0;
161   statistics->update_count.events = 0;
162
163   statistics->update_freq.buffers = 0;
164   statistics->update_freq.bytes = 0;
165   statistics->update_freq.events = 0;
166
167   statistics->update_on_eos = TRUE;
168   statistics->update = TRUE;
169   statistics->silent = FALSE;
170
171   if (!statistics->timer) {
172     statistics->timer = g_timer_new();
173   }
174   if (!statistics->last_timer) {
175     statistics->last_timer = g_timer_new();
176   }
177 }
178
179 static void
180 print_stats(gboolean first, const gchar *name, const gchar *type, stats *base, stats *final, double time)
181 {
182   const gchar *header0 = "statistics";
183   const gchar *headerN = "          ";
184   stats delta;
185
186   delta.buffers = final->buffers - base->buffers;
187   delta.bytes = final->bytes - base->bytes;
188   delta.events = final->events - base->events;
189
190   g_print("%s: (%s) %s: s:%g buffers:%" G_GINT64_FORMAT 
191           " bytes:%" G_GINT64_FORMAT
192           " events:%" G_GINT64_FORMAT "\n",
193     first ? header0 : headerN,
194     name, type, time,
195     final->buffers,
196     final->bytes,
197     final->events);
198   g_print("%s: (%s) %s: buf/s:%g B/s:%g e/s:%g B/buf:%g\n",
199     headerN,
200     name, type,
201     delta.buffers/time,
202     delta.bytes/time,
203     delta.events/time,
204     ((double)delta.bytes/(double)delta.buffers));
205 }
206
207 static void
208 gst_statistics_print (GstStatistics *statistics)
209 {
210   const gchar *name;
211   double elapsed;
212   double last_elapsed;
213
214   g_return_if_fail (statistics != NULL);
215   g_return_if_fail (GST_IS_STATISTICS (statistics));
216
217   name = gst_object_get_name(GST_OBJECT(statistics));
218   if (!name) {
219     name = "";
220   }
221
222   elapsed = g_timer_elapsed(statistics->timer, NULL);
223   last_elapsed = g_timer_elapsed(statistics->last_timer, NULL);
224
225   print_stats(1, name, "total", &zero_stats, &statistics->stats, elapsed);
226   print_stats(0, name, "last", &statistics->last_stats, &statistics->stats, last_elapsed);
227   statistics->last_stats = statistics->stats;
228   g_timer_reset(statistics->last_timer);
229 }
230
231 static void 
232 gst_statistics_chain (GstPad *pad, GstData *_data) 
233 {
234   GstBuffer *buf = GST_BUFFER (_data);
235   GstStatistics *statistics;
236   gboolean update = FALSE;
237
238   g_return_if_fail (pad != NULL);
239   g_return_if_fail (GST_IS_PAD (pad));
240   g_return_if_fail (buf != NULL);
241
242   statistics = GST_STATISTICS (gst_pad_get_parent (pad));
243
244   if (GST_IS_EVENT(buf)) {
245     GstEvent *event = GST_EVENT (buf);
246     statistics->stats.events += 1;
247     if (GST_EVENT_TYPE(event) == GST_EVENT_EOS) {
248       gst_element_set_eos (GST_ELEMENT (statistics));
249       if (statistics->update_on_eos) {
250         update = TRUE;
251       }
252     }
253     if (statistics->update_freq.events) {
254       statistics->update_count.events += 1;
255       if (statistics->update_count.events == statistics->update_freq.events) {
256         statistics->update_count.events = 0;
257         update = TRUE;
258       }
259     }
260   } else {
261     statistics->stats.buffers += 1;
262     if (statistics->update_freq.buffers) {
263       statistics->update_count.buffers += 1;
264       if (statistics->update_count.buffers == statistics->update_freq.buffers) {
265         statistics->update_count.buffers = 0;
266         update = TRUE;
267       }
268     }
269
270     statistics->stats.bytes += GST_BUFFER_SIZE(buf);
271     if (statistics->update_freq.bytes) {
272       statistics->update_count.bytes += GST_BUFFER_SIZE(buf);
273       if (statistics->update_count.bytes >= statistics->update_freq.bytes) {
274         statistics->update_count.bytes = 0;
275         update = TRUE;
276       }
277     }
278   }
279
280   if (update) {
281     if (statistics->update) {
282       GST_DEBUG ("[%s]: pre update emit", GST_ELEMENT_NAME (statistics));
283       g_signal_emit (G_OBJECT (statistics), gst_statistics_signals[SIGNAL_UPDATE], 0);
284       GST_DEBUG ("[%s]: post update emit", GST_ELEMENT_NAME (statistics));
285     }
286     if (!statistics->silent) {
287       gst_statistics_print(statistics);
288     }
289   }
290   gst_pad_push (statistics->srcpad, GST_DATA (buf));
291 }
292
293 static void 
294 gst_statistics_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
295 {
296   GstStatistics *statistics;
297
298   /* it's not null if we got it, but it might not be ours */
299   g_return_if_fail (GST_IS_STATISTICS (object));
300   
301   statistics = GST_STATISTICS (object);
302
303   switch (prop_id) {
304     case ARG_BUFFER_UPDATE_FREQ:
305       statistics->update_freq.buffers = g_value_get_int64 (value);
306       break;
307     case ARG_BYTES_UPDATE_FREQ:
308       statistics->update_freq.bytes = g_value_get_int64 (value);
309       break;
310     case ARG_EVENT_UPDATE_FREQ:
311       statistics->update_freq.events = g_value_get_int64 (value);
312       break;
313     case ARG_UPDATE_ON_EOS:
314       statistics->update_on_eos = g_value_get_boolean (value);
315       break;
316     case ARG_UPDATE:
317       statistics->update = g_value_get_boolean (value);
318       break;
319     case ARG_SILENT:
320       statistics->silent = g_value_get_boolean (value);
321       break;
322     default:
323       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324       break;
325   }
326 }
327
328 static void gst_statistics_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
329   GstStatistics *statistics;
330
331   /* it's not null if we got it, but it might not be ours */
332   g_return_if_fail (GST_IS_STATISTICS (object));
333   
334   statistics = GST_STATISTICS (object);
335
336   switch (prop_id) {
337     case ARG_BUFFERS:
338       g_value_set_int64 (value, statistics->stats.buffers);
339       break;
340     case ARG_BYTES:
341       g_value_set_int64 (value, statistics->stats.bytes);
342       break;
343     case ARG_EVENTS:
344       g_value_set_int64 (value, statistics->stats.events);
345       break;
346     case ARG_BUFFER_UPDATE_FREQ:
347       g_value_set_int64 (value, statistics->update_freq.buffers);
348       break;
349     case ARG_BYTES_UPDATE_FREQ:
350       g_value_set_int64 (value, statistics->update_freq.bytes);
351       break;
352     case ARG_EVENT_UPDATE_FREQ:
353       g_value_set_int64 (value, statistics->update_freq.events);
354       break;
355     case ARG_UPDATE_ON_EOS:
356       g_value_set_boolean (value, statistics->update_on_eos);
357       break;
358     case ARG_UPDATE:
359       g_value_set_boolean (value, statistics->update);
360       break;
361     case ARG_SILENT:
362       g_value_set_boolean (value, statistics->silent);
363       break;
364     default:
365       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366       break;
367   }
368 }