9426f042288c12c499031dd9224cf63c1a1b7326
[platform/upstream/gstreamer.git] / gst / 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 #include <gststatistics.h>
25
26
27 GstElementDetails gst_statistics_details = {
28   "Statistics",
29   "Generic",
30   "LGPL",
31   "Statistics on buffers/bytes/events",
32   VERSION,
33   "David I. Lehn <dlehn@users.sourceforge.net>",
34   "(C) 2001",
35 };
36
37
38 /* Statistics signals and args */
39 enum {
40   SIGNAL_UPDATE,
41   LAST_SIGNAL
42 };
43
44 enum {
45   ARG_0,
46   ARG_BUFFERS,
47   ARG_BYTES,
48   ARG_EVENTS,
49   ARG_BUFFER_UPDATE_FREQ,
50   ARG_BYTES_UPDATE_FREQ,
51   ARG_EVENT_UPDATE_FREQ,
52   ARG_UPDATE_ON_EOS,
53   ARG_UPDATE,
54   ARG_SILENT,
55 };
56
57
58 static void gst_statistics_class_init   (GstStatisticsClass *klass);
59 static void gst_statistics_init         (GstStatistics *statistics);
60
61 static void gst_statistics_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
62 static void gst_statistics_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
63
64 static void gst_statistics_chain                (GstPad *pad, GstBuffer *buf);
65 static void gst_statistics_reset                (GstStatistics *statistics);
66 static void gst_statistics_print                (GstStatistics *statistics);
67
68 static GstElementClass *parent_class = NULL;
69 static guint gst_statistics_signals[LAST_SIGNAL] = { 0, };
70
71 static stats zero_stats = { 0, };
72
73 GType
74 gst_statistics_get_type (void) 
75 {
76   static GType statistics_type = 0;
77
78   if (!statistics_type) {
79     static const GTypeInfo statistics_info = {
80       sizeof(GstStatisticsClass),      NULL,
81       NULL,
82       (GClassInitFunc)gst_statistics_class_init,
83       NULL,
84       NULL,
85       sizeof(GstStatistics),
86       0,
87       (GInstanceInitFunc)gst_statistics_init,
88     };
89     statistics_type = g_type_register_static (GST_TYPE_ELEMENT, "GstStatistics", &statistics_info, 0);
90   }
91   return statistics_type;
92 }
93
94 static void 
95 gst_statistics_class_init (GstStatisticsClass *klass) 
96 {
97   GObjectClass *gobject_class;
98
99   gobject_class = (GObjectClass*)klass;
100
101   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
102
103   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFERS,
104     g_param_spec_int64 ("buffers", "buffers", "total buffers count",
105                           0, G_MAXINT64, 0, G_PARAM_READABLE));
106   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BYTES,
107     g_param_spec_int64 ("bytes", "bytes", "total bytes count",
108                           0, G_MAXINT64, 0, G_PARAM_READABLE));
109   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EVENTS,
110     g_param_spec_int64 ("events", "events", "total event count",
111                           0, G_MAXINT64, 0, G_PARAM_READABLE));
112   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_UPDATE_FREQ,
113     g_param_spec_int64 ("buffer_update_freq", "buffer update freq", "buffer update frequency",
114                           0, G_MAXINT64, 0, G_PARAM_READWRITE));
115   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BYTES_UPDATE_FREQ,
116     g_param_spec_int64 ("bytes_update_freq", "bytes update freq", "bytes update frequency",
117                           0, G_MAXINT64, 0, G_PARAM_READWRITE));
118   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EVENT_UPDATE_FREQ,
119     g_param_spec_int64 ("event_update_freq", "event update freq", "event update frequency",
120                           0, G_MAXINT64, 0, G_PARAM_READWRITE));
121   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_UPDATE_ON_EOS,
122     g_param_spec_boolean ("update_on_eos", "update on EOS", "update on EOS event",
123                           TRUE,G_PARAM_READWRITE)); 
124   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_UPDATE,
125     g_param_spec_boolean ("update", "update", "update",
126                           TRUE,G_PARAM_READWRITE)); 
127   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
128     g_param_spec_boolean ("silent", "silent", "silent",
129                           TRUE,G_PARAM_READWRITE)); 
130
131   gst_statistics_signals[SIGNAL_UPDATE] =
132     g_signal_new ("update", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
133                    G_STRUCT_OFFSET (GstStatisticsClass, update), NULL, NULL,
134                    g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
135
136   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_statistics_set_property);
137   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_statistics_get_property);
138 }
139
140 static GstBufferPool*
141 gst_statistics_get_bufferpool (GstPad *pad)
142 {
143   GstStatistics *statistics;
144
145   statistics = GST_STATISTICS (gst_pad_get_parent (pad));
146
147   return gst_pad_get_bufferpool (statistics->srcpad);
148 }
149
150 static void 
151 gst_statistics_init (GstStatistics *statistics) 
152 {
153   statistics->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
154   gst_element_add_pad (GST_ELEMENT (statistics), statistics->sinkpad);
155   gst_pad_set_chain_function (statistics->sinkpad, GST_DEBUG_FUNCPTR (gst_statistics_chain));
156   gst_pad_set_bufferpool_function (statistics->sinkpad, GST_DEBUG_FUNCPTR (gst_statistics_get_bufferpool));
157   
158   statistics->srcpad = gst_pad_new ("src", GST_PAD_SRC);
159   gst_element_add_pad (GST_ELEMENT (statistics), statistics->srcpad);
160
161   statistics->timer = NULL;
162   statistics->last_timer = NULL;
163   gst_statistics_reset(statistics);
164 }
165
166 static void
167 gst_statistics_reset (GstStatistics *statistics)
168 {
169   g_return_if_fail (statistics != NULL);
170   g_return_if_fail (GST_IS_STATISTICS (statistics));
171
172   statistics->stats.buffers = 0;
173   statistics->stats.bytes = 0;
174   statistics->stats.events = 0;
175
176   statistics->last_stats.buffers = 0;
177   statistics->last_stats.bytes = 0;
178   statistics->last_stats.events = 0;
179
180   statistics->update_count.buffers = 0;
181   statistics->update_count.bytes = 0;
182   statistics->update_count.events = 0;
183
184   statistics->update_freq.buffers = 0;
185   statistics->update_freq.bytes = 0;
186   statistics->update_freq.events = 0;
187
188   statistics->update_on_eos = TRUE;
189   statistics->update = TRUE;
190   statistics->silent = FALSE;
191
192   if (!statistics->timer) {
193     statistics->timer = g_timer_new();
194   }
195   if (!statistics->last_timer) {
196     statistics->last_timer = g_timer_new();
197   }
198 }
199
200 static void
201 print_stats(gboolean first, const gchar *name, const gchar *type, stats *base, stats *final, double time)
202 {
203   const gchar *header0 = "statistics";
204   const gchar *headerN = "          ";
205   stats delta;
206
207   delta.buffers = final->buffers - base->buffers;
208   delta.bytes = final->bytes - base->bytes;
209   delta.events = final->events - base->events;
210
211   g_print("%s: (%s) %s: s:%g buffers:%lld bytes:%lld events:%lld\n",
212     first ? header0 : headerN,
213     name, type, time,
214     final->buffers,
215     final->bytes,
216     final->events);
217   g_print("%s: (%s) %s: buf/s:%g B/s:%g e/s:%g B/buf:%g\n",
218     headerN,
219     name, type,
220     delta.buffers/time,
221     delta.bytes/time,
222     delta.events/time,
223     ((double)delta.bytes/(double)delta.buffers));
224 }
225
226 static void
227 gst_statistics_print (GstStatistics *statistics)
228 {
229   const gchar *name;
230   double elapsed;
231   double last_elapsed;
232
233   g_return_if_fail (statistics != NULL);
234   g_return_if_fail (GST_IS_STATISTICS (statistics));
235
236   name = gst_object_get_name(GST_OBJECT(statistics));
237   if (!name) {
238     name = "";
239   }
240
241   elapsed = g_timer_elapsed(statistics->timer, NULL);
242   last_elapsed = g_timer_elapsed(statistics->last_timer, NULL);
243
244   print_stats(1, name, "total", &zero_stats, &statistics->stats, elapsed);
245   print_stats(0, name, "last", &statistics->last_stats, &statistics->stats, last_elapsed);
246   statistics->last_stats = statistics->stats;
247   g_timer_reset(statistics->last_timer);
248 }
249
250 static void 
251 gst_statistics_chain (GstPad *pad, GstBuffer *buf) 
252 {
253   GstStatistics *statistics;
254   gboolean update = FALSE;
255
256   g_return_if_fail (pad != NULL);
257   g_return_if_fail (GST_IS_PAD (pad));
258   g_return_if_fail (buf != NULL);
259
260   statistics = GST_STATISTICS (gst_pad_get_parent (pad));
261
262   if (GST_IS_EVENT(buf)) {
263     GstEvent *event = GST_EVENT (buf);
264     statistics->stats.events += 1;
265     if (GST_EVENT_TYPE(event) == GST_EVENT_EOS) {
266       gst_element_set_eos (GST_ELEMENT (statistics));
267       if (statistics->update_on_eos) {
268         update = TRUE;
269       }
270     }
271     if (statistics->update_freq.events) {
272       statistics->update_count.events += 1;
273       if (statistics->update_count.events == statistics->update_freq.events) {
274         statistics->update_count.events = 0;
275         update = TRUE;
276       }
277     }
278   } else {
279     statistics->stats.buffers += 1;
280     if (statistics->update_freq.buffers) {
281       statistics->update_count.buffers += 1;
282       if (statistics->update_count.buffers == statistics->update_freq.buffers) {
283         statistics->update_count.buffers = 0;
284         update = TRUE;
285       }
286     }
287
288     statistics->stats.bytes += GST_BUFFER_SIZE(buf);
289     if (statistics->update_freq.bytes) {
290       statistics->update_count.bytes += GST_BUFFER_SIZE(buf);
291       if (statistics->update_count.bytes >= statistics->update_freq.bytes) {
292         statistics->update_count.bytes = 0;
293         update = TRUE;
294       }
295     }
296   }
297
298   if (update) {
299     if (statistics->update) {
300       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, statistics, "pre update emit\n");
301       g_signal_emit (G_OBJECT (statistics), gst_statistics_signals[SIGNAL_UPDATE], 0);
302       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, statistics, "post update emit\n");
303     }
304     if (!statistics->silent) {
305       gst_statistics_print(statistics);
306     }
307   }
308   gst_pad_push (statistics->srcpad, buf);
309 }
310
311 static void 
312 gst_statistics_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
313 {
314   GstStatistics *statistics;
315
316   /* it's not null if we got it, but it might not be ours */
317   g_return_if_fail (GST_IS_STATISTICS (object));
318   
319   statistics = GST_STATISTICS (object);
320
321   switch (prop_id) {
322     case ARG_BUFFER_UPDATE_FREQ:
323       statistics->update_freq.buffers = g_value_get_int64 (value);
324       break;
325     case ARG_BYTES_UPDATE_FREQ:
326       statistics->update_freq.bytes = g_value_get_int64 (value);
327       break;
328     case ARG_EVENT_UPDATE_FREQ:
329       statistics->update_freq.events = g_value_get_int64 (value);
330       break;
331     case ARG_UPDATE_ON_EOS:
332       statistics->update_on_eos = g_value_get_boolean (value);
333       break;
334     case ARG_UPDATE:
335       statistics->update = g_value_get_boolean (value);
336       break;
337     case ARG_SILENT:
338       statistics->silent = g_value_get_boolean (value);
339       break;
340     default:
341       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342       break;
343   }
344 }
345
346 static void gst_statistics_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
347   GstStatistics *statistics;
348
349   /* it's not null if we got it, but it might not be ours */
350   g_return_if_fail (GST_IS_STATISTICS (object));
351   
352   statistics = GST_STATISTICS (object);
353
354   switch (prop_id) {
355     case ARG_BUFFERS:
356       g_value_set_int64 (value, statistics->stats.buffers);
357       break;
358     case ARG_BYTES:
359       g_value_set_int64 (value, statistics->stats.bytes);
360       break;
361     case ARG_EVENTS:
362       g_value_set_int64 (value, statistics->stats.events);
363       break;
364     case ARG_BUFFER_UPDATE_FREQ:
365       g_value_set_int64 (value, statistics->update_freq.buffers);
366       break;
367     case ARG_BYTES_UPDATE_FREQ:
368       g_value_set_int64 (value, statistics->update_freq.bytes);
369       break;
370     case ARG_EVENT_UPDATE_FREQ:
371       g_value_set_int64 (value, statistics->update_freq.events);
372       break;
373     case ARG_UPDATE_ON_EOS:
374       g_value_set_boolean (value, statistics->update_on_eos);
375       break;
376     case ARG_UPDATE:
377       g_value_set_boolean (value, statistics->update);
378       break;
379     case ARG_SILENT:
380       g_value_set_boolean (value, statistics->silent);
381       break;
382     default:
383       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
384       break;
385   }
386 }