2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
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.
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.
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.
28 #include "gststatistics.h"
30 GST_DEBUG_CATEGORY_STATIC (gst_statistics_debug);
31 #define GST_CAT_DEFAULT gst_statistics_debug
33 GstElementDetails gst_statistics_details = GST_ELEMENT_DETAILS (
36 "Statistics on buffers/bytes/events",
37 "David I. Lehn <dlehn@users.sourceforge.net>"
41 /* Statistics signals and args */
52 ARG_BUFFER_UPDATE_FREQ,
53 ARG_BYTES_UPDATE_FREQ,
54 ARG_EVENT_UPDATE_FREQ,
61 #define _do_init(bla) \
62 GST_DEBUG_CATEGORY_INIT (gst_statistics_debug, "statistics", 0, "statistics element");
64 GST_BOILERPLATE_FULL (GstStatistics, gst_statistics, GstElement, GST_TYPE_ELEMENT, _do_init);
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);
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);
73 static guint gst_statistics_signals[LAST_SIGNAL] = { 0, };
75 static stats zero_stats = { 0, };
79 gst_statistics_base_init (gpointer g_class)
81 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
83 gst_element_class_set_details (gstelement_class, &gst_statistics_details);
86 gst_statistics_class_init (GstStatisticsClass *klass)
88 GObjectClass *gobject_class;
90 gobject_class = G_OBJECT_CLASS (klass);
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));
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);
126 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_statistics_set_property);
127 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_statistics_get_property);
131 gst_statistics_init (GstStatistics *statistics)
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));
137 statistics->srcpad = gst_pad_new ("src", GST_PAD_SRC);
138 gst_element_add_pad (GST_ELEMENT (statistics), statistics->srcpad);
140 statistics->timer = NULL;
141 statistics->last_timer = NULL;
142 gst_statistics_reset(statistics);
146 gst_statistics_reset (GstStatistics *statistics)
148 g_return_if_fail (statistics != NULL);
149 g_return_if_fail (GST_IS_STATISTICS (statistics));
151 statistics->stats.buffers = 0;
152 statistics->stats.bytes = 0;
153 statistics->stats.events = 0;
155 statistics->last_stats.buffers = 0;
156 statistics->last_stats.bytes = 0;
157 statistics->last_stats.events = 0;
159 statistics->update_count.buffers = 0;
160 statistics->update_count.bytes = 0;
161 statistics->update_count.events = 0;
163 statistics->update_freq.buffers = 0;
164 statistics->update_freq.bytes = 0;
165 statistics->update_freq.events = 0;
167 statistics->update_on_eos = TRUE;
168 statistics->update = TRUE;
169 statistics->silent = FALSE;
171 if (!statistics->timer) {
172 statistics->timer = g_timer_new();
174 if (!statistics->last_timer) {
175 statistics->last_timer = g_timer_new();
180 print_stats(gboolean first, const gchar *name, const gchar *type, stats *base, stats *final, double time)
182 const gchar *header0 = "statistics";
183 const gchar *headerN = " ";
186 delta.buffers = final->buffers - base->buffers;
187 delta.bytes = final->bytes - base->bytes;
188 delta.events = final->events - base->events;
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,
198 g_print("%s: (%s) %s: buf/s:%g B/s:%g e/s:%g B/buf:%g\n",
204 ((double)delta.bytes/(double)delta.buffers));
208 gst_statistics_print (GstStatistics *statistics)
214 g_return_if_fail (statistics != NULL);
215 g_return_if_fail (GST_IS_STATISTICS (statistics));
217 name = gst_object_get_name(GST_OBJECT(statistics));
222 elapsed = g_timer_elapsed(statistics->timer, NULL);
223 last_elapsed = g_timer_elapsed(statistics->last_timer, NULL);
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);
232 gst_statistics_chain (GstPad *pad, GstData *_data)
234 GstBuffer *buf = GST_BUFFER (_data);
235 GstStatistics *statistics;
236 gboolean update = FALSE;
238 g_return_if_fail (pad != NULL);
239 g_return_if_fail (GST_IS_PAD (pad));
240 g_return_if_fail (buf != NULL);
242 statistics = GST_STATISTICS (gst_pad_get_parent (pad));
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) {
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;
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;
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;
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));
286 if (!statistics->silent) {
287 gst_statistics_print(statistics);
290 gst_pad_push (statistics->srcpad, GST_DATA (buf));
294 gst_statistics_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
296 GstStatistics *statistics;
298 /* it's not null if we got it, but it might not be ours */
299 g_return_if_fail (GST_IS_STATISTICS (object));
301 statistics = GST_STATISTICS (object);
304 case ARG_BUFFER_UPDATE_FREQ:
305 statistics->update_freq.buffers = g_value_get_int64 (value);
307 case ARG_BYTES_UPDATE_FREQ:
308 statistics->update_freq.bytes = g_value_get_int64 (value);
310 case ARG_EVENT_UPDATE_FREQ:
311 statistics->update_freq.events = g_value_get_int64 (value);
313 case ARG_UPDATE_ON_EOS:
314 statistics->update_on_eos = g_value_get_boolean (value);
317 statistics->update = g_value_get_boolean (value);
320 statistics->silent = g_value_get_boolean (value);
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
328 static void gst_statistics_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
329 GstStatistics *statistics;
331 /* it's not null if we got it, but it might not be ours */
332 g_return_if_fail (GST_IS_STATISTICS (object));
334 statistics = GST_STATISTICS (object);
338 g_value_set_int64 (value, statistics->stats.buffers);
341 g_value_set_int64 (value, statistics->stats.bytes);
344 g_value_set_int64 (value, statistics->stats.events);
346 case ARG_BUFFER_UPDATE_FREQ:
347 g_value_set_int64 (value, statistics->update_freq.buffers);
349 case ARG_BYTES_UPDATE_FREQ:
350 g_value_set_int64 (value, statistics->update_freq.bytes);
352 case ARG_EVENT_UPDATE_FREQ:
353 g_value_set_int64 (value, statistics->update_freq.events);
355 case ARG_UPDATE_ON_EOS:
356 g_value_set_boolean (value, statistics->update_on_eos);
359 g_value_set_boolean (value, statistics->update);
362 g_value_set_boolean (value, statistics->silent);
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);