]> git.sesse.net Git - vlc/blob - src/input/stats.c
decoder: reorder to avoid forward declation, no functional changes
[vlc] / src / input / stats.c
1 /*****************************************************************************
2  * stats.c: Statistics handling
3  *****************************************************************************
4  * Copyright (C) 2006 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include "input/input_internal.h"
30
31 /**
32  * Create a statistics counter
33  * \param i_compute_type the aggregation type. One of STATS_LAST (always
34  * keep the last value), STATS_COUNTER (increment by the passed value),
35  * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
36  * (keep a time derivative of the value)
37  */
38 counter_t * stats_CounterCreate( int i_compute_type )
39 {
40     counter_t *p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
41
42     if( !p_counter ) return NULL;
43     p_counter->i_compute_type = i_compute_type;
44     p_counter->i_samples = 0;
45     p_counter->pp_samples = NULL;
46
47     p_counter->last_update = 0;
48
49     return p_counter;
50 }
51
52 static inline int64_t stats_GetTotal(const counter_t *counter)
53 {
54     if (counter == NULL || counter->i_samples == 0)
55         return 0;
56     return counter->pp_samples[0]->value;
57 }
58
59 static inline float stats_GetRate(const counter_t *counter)
60 {
61     if (counter == NULL || counter->i_samples < 2)
62         return 0.;
63
64     return (counter->pp_samples[0]->value - counter->pp_samples[1]->value)
65         / (float)(counter->pp_samples[0]->date - counter->pp_samples[1]->date);
66 }
67
68 input_stats_t *stats_NewInputStats( input_thread_t *p_input )
69 {
70     (void)p_input;
71     input_stats_t *p_stats = calloc( 1, sizeof(input_stats_t) );
72     if( !p_stats )
73         return NULL;
74
75     vlc_mutex_init( &p_stats->lock );
76     stats_ReinitInputStats( p_stats );
77
78     return p_stats;
79 }
80
81 void stats_ComputeInputStats(input_thread_t *input, input_stats_t *st)
82 {
83     if (!libvlc_stats(input))
84         return;
85
86     vlc_mutex_lock(&input->p->counters.counters_lock);
87     vlc_mutex_lock(&st->lock);
88
89     /* Input */
90     st->i_read_packets = stats_GetTotal(input->p->counters.p_read_packets);
91     st->i_read_bytes = stats_GetTotal(input->p->counters.p_read_bytes);
92     st->f_input_bitrate = stats_GetRate(input->p->counters.p_input_bitrate);
93     st->i_demux_read_bytes = stats_GetTotal(input->p->counters.p_demux_read);
94     st->f_demux_bitrate = stats_GetRate(input->p->counters.p_demux_bitrate);
95     st->i_demux_corrupted = stats_GetTotal(input->p->counters.p_demux_corrupted);
96     st->i_demux_discontinuity = stats_GetTotal(input->p->counters.p_demux_discontinuity);
97
98     /* Decoders */
99     st->i_decoded_video = stats_GetTotal(input->p->counters.p_decoded_video);
100     st->i_decoded_audio = stats_GetTotal(input->p->counters.p_decoded_audio);
101
102     /* Sout */
103     if (input->p->counters.p_sout_send_bitrate)
104     {
105         st->i_sent_packets = stats_GetTotal(input->p->counters.p_sout_sent_packets);
106         st->i_sent_bytes = stats_GetTotal(input->p->counters.p_sout_sent_bytes);
107         st->f_send_bitrate = stats_GetRate(input->p->counters.p_sout_send_bitrate);
108     }
109
110     /* Aout */
111     st->i_played_abuffers = stats_GetTotal(input->p->counters.p_played_abuffers);
112     st->i_lost_abuffers = stats_GetTotal(input->p->counters.p_lost_abuffers);
113
114     /* Vouts */
115     st->i_displayed_pictures = stats_GetTotal(input->p->counters.p_displayed_pictures);
116     st->i_lost_pictures = stats_GetTotal(input->p->counters.p_lost_pictures);
117
118     vlc_mutex_unlock(&st->lock);
119     vlc_mutex_unlock(&input->p->counters.counters_lock);
120 }
121
122 void stats_ReinitInputStats( input_stats_t *p_stats )
123 {
124     vlc_mutex_lock( &p_stats->lock );
125     p_stats->i_read_packets = p_stats->i_read_bytes =
126     p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
127     p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
128     p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
129     p_stats->i_demux_corrupted = p_stats->i_demux_discontinuity =
130     p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
131     p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
132     p_stats->i_decoded_video = p_stats->i_decoded_audio =
133     p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
134      = 0;
135     vlc_mutex_unlock( &p_stats->lock );
136 }
137
138 void stats_CounterClean( counter_t *p_c )
139 {
140     if( p_c )
141     {
142         int i = p_c->i_samples - 1 ;
143         while( i >= 0 )
144         {
145             counter_sample_t *p_s = p_c->pp_samples[i];
146             REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
147             free( p_s );
148             i--;
149         }
150         free( p_c );
151     }
152 }
153
154
155 /** Update a counter element with new values
156  * \param p_counter the counter to update
157  * \param val the vlc_value union containing the new value to aggregate. For
158  * more information on how data is aggregated, \see stats_Create
159  * \param val_new a pointer that will be filled with new data
160  */
161 void stats_Update( counter_t *p_counter, uint64_t val, uint64_t *new_val )
162 {
163     if( !p_counter )
164         return;
165
166     switch( p_counter->i_compute_type )
167     {
168     case STATS_DERIVATIVE:
169     {
170         counter_sample_t *p_new, *p_old;
171         mtime_t now = mdate();
172         if( now - p_counter->last_update < CLOCK_FREQ )
173             return;
174         p_counter->last_update = now;
175         /* Insert the new one at the beginning */
176         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
177         p_new->value = val;
178         p_new->date = p_counter->last_update;
179         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
180                      0, p_new );
181
182         if( p_counter->i_samples == 3 )
183         {
184             p_old = p_counter->pp_samples[2];
185             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
186             free( p_old );
187         }
188         break;
189     }
190     case STATS_COUNTER:
191         if( p_counter->i_samples == 0 )
192         {
193             counter_sample_t *p_new = (counter_sample_t*)malloc(
194                                                sizeof( counter_sample_t ) );
195             p_new->value = 0;
196
197             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
198                          p_counter->i_samples, p_new );
199         }
200         if( p_counter->i_samples == 1 )
201         {
202             p_counter->pp_samples[0]->value += val;
203             if( new_val )
204                 *new_val = p_counter->pp_samples[0]->value;
205         }
206         break;
207     }
208 }